#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(time(0));
constexpr int MAXN = 1e6 + 5;
long long a[MAXN];
long long b[MAXN];
long long pref[MAXN];
int p[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;
        tl[i] = 0;
    }
    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];
        }
    }
    long long ans = 0;
    long long res = 0;
    for (int i = 1; i <= n; i++){
        ans += max(a[i] + tl[i] * b[i], a[i] + tr[i] * b[i]);
        //ans += a[i] + mn[i] * b[i];
        if (i >= pr - 1){
            res = max(res, ans);
        }
    }
    cout << res << '\n';
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    while (t--){
        solve();
    }
}



