#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(time(0));
constexpr int MAXN = 1e6 + 5;
constexpr long long INF = 1e18;
long long a[MAXN];
long long b[MAXN];
int p[MAXN];
int mx[MAXN];
int tl[MAXN];
int tr[MAXN];
void solve(){
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++){
        cin >> a[i] >> b[i];
    }
    for (int i = 1; i <= n; i++){
        cin >> p[i];
    }
    for (int i = 1; i <= n; i++){
        tr[i] = i - 1;
    }
    int pr = n;
    for (int i = n; i >= 1; i--){
        if (p[i] != -1){
            for (int j = p[i] + 1; j <= pr; j++){
                tl[j] = i;
            }
            pr = p[i];
        }
    }
    pr = 0;
    for (int i = 1; i <= n; i++){
        if (p[i] != -1){
            for (int j = pr + 1; j <= p[i]; j++){
                tr[j] = min(tr[j], i - 1);
            }
            pr = p[i];
        }
    }
    vector <long long> dp(n + 2, -INF);
    dp[0] = 0;
    long long mx = -INF;
    if (pr == 0){
        mx = 0;
    }
    for (int i = 1; i <= n; i++){
        long long nwmx = -INF;
        vector <long long> dp2(n + 2, -INF);
        for (int j = 0; j < 0; j++){
            nwmx = max(nwmx, dp[j]);
        }
        for (int j = 0; j < i; j++){
            nwmx = max(nwmx, dp[j]);
            dp2[j] = nwmx + a[i] + b[i] * j;
            //if (i >= pr)
                mx = max(mx, dp2[j]);
        }
        /*for (auto v: dp2){
            cout << v << " ";
        }
        cout << endl;*/
        swap(dp, dp2);
    }
    cout << mx << '\n';
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    while (t--){
        solve();
    }
}




