#include <bits/stdc++.h>

using namespace std;

#define int long long
const int mod = 998244353;

int dp[5000 + 1][5000 + 1];

int binpow(int a, int x) {
    int res = 1;
    while (x) {
        if (x & 1) {
            res *= a;
            res %= mod;
        }
        a *= a;
        a %= mod;
        x >>= 1;
    }
    return res;
}

void solve() {
    int n;
    cin >> n;
    vector<int>a(n);
    vector<int>b(n);
    vector<int>l(n);
    vector<int>r(n);
    int ln = 0;
    int mink = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
        r[i] = i;
    }
    int p;
    int ind = 0;
    int pp = 0;
    for (int i = 0; i < n; i++) {
        cin >> p;
        if (p != -1) {
            mink = max(mink, p);
            ln = pp;
            for (; ind < p; ind++) {
                l[ind] = ln;
                r[ind] = i;
            }
            pp = i + 1;
        }
    }
    for (; ind < n; ind++) {
        l[ind] = pp;
    }
    for (int i = 0; i < n + 1; i++) {
        for (int j  = 0; j < n + 1; j++) {
            dp[i][j] = -1e18;
        }
    }
    dp[0][0] = 0;
    int dpmx = 0;
    int ans = -1e18;
    if (mink == 0) {
        ans = 0;
    }
    for (int i = 1; i <= n; i++) {
        dpmx = -1e18;
        int t;
        if (i == 1) {
            t = 0;
        } else {
            t = l[i - 2];
        }
        for (; t <= r[i - 1]; t++) {
            dpmx = max(dp[i - 1][t], dpmx);
            if (t >= l[i - 1]) {
                dp[i][t] = dpmx + a[i - 1] + t * b[i - 1];
                if (mink <= i) {
                    ans = max(ans, dp[i][t]);
                }
            }
        }
    }
    cout << ans << endl;
}

signed main() {
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
    solve();
}