#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 mx[MAXN];
int mn[MAXN];
void solve(){
    int n;
    cin >> n;
    for (int i = 0; i < n; i++){
        cin >> a[i] >> b[i];
    }
    for (int i = 0; i < n; i++){
        cin >> p[i];
    }
    for (int i = 0; i < n; i++){
        mx[i] = i;
        mn[i] = 0;
    }
    int pr = n;
    for (int i = n - 1; i > -1; i--){
        if (p[i] != -1){
            for (int j = p[i]; j < pr; j++){
                mn[j] = max(mn[j], i + 1);
            }
            pr = p[i];
        }
    }
    pr = 0;
    for (int i = 0; i < n; i++){
        if (p[i] != -1){
            for (int j = pr; j < p[i]; j++){
                mx[j] = min(mx[j], i);
            }
            pr = p[i];
        }
    }
    long long ans = 0;
    long long res = 0;
    for (int i = 0; i < n; i++){
        ans += max(a[i] + mn[i] * b[i], a[i] + mx[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();
    }
}



