#include<bits/stdc++.h>

#define int long long

using namespace std;

const int INF = 1e18;

signed main() {
    ios_base::sync_with_stdio();
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (int i = 0; i < n; ++i) cin >> a[i] >> b[i];
    vector<int> p(n);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
    }
    vector<int> upr(n);
    vector<int> lwr(n);
    int mxp = -1;
    for (int i = 0; i < n; ++i) {
        upr[i] = i;
    }
    for (int i = 0; i < n; ++i) {
        mxp = max(mxp, p[i]);
        if (p[i] != -1) {
            for (int j = 0; j < p[i]; ++j) {
                upr[j] = min(upr[j], i);
            }
            for (int j = p[i]; j < n; ++j) {
                lwr[j] = max(lwr[j], i + 1);
            }
        }
    }
    int ans = -INF;
    if (mxp == -1) {
        ans = 0;
    }
    vector<int> anst(n, 0);
    for (int i = 0; i < n; ++i) {
        vector<int> nanst(n, -INF);
        for (int j = lwr[i]; j <= upr[i]; ++j) {
            nanst[j] = max(nanst[j], anst[j] + j * b[i] + a[i]);
        }
        for (int j = 1; j < n; ++j) {
            nanst[j] = max(nanst[j], nanst[j - 1]);
        }
        //cout << i << " " << lwr[i] << " " << upr[i] << " " << nanst.back() << endl;
        if (i >= mxp - 1) {
            ans = max(ans, nanst.back());
        }
        anst = nanst;
    }
    cout << ans << endl;
    return 0;
}
