#include <bits/stdc++.h>

using namespace std;

void solve();

// #include<ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;

int main() {
#ifdef LOCAL
    freopen("input.txt", "r",stdin);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    //cin>>t;
    while (t--) {
        solve();
    }
    return 0;
}

#define int long long
#define endl '\n'
const int N = 1e3, MOD = 998244353, INF = 2e18;
const double EPS = 1e-6, pi = acos(-1);

void solve() {
    int n;
    cin >> n;
    vector<pair<int,int> > a(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> a[i].first >> a[i].second;
    }
    vector<int> p(n + 2,-1);
    vector<int> val(n+2,-1);
    vector<int> val2(n+2,-1);
    bool fl=false;
    vector<int> pos;
    for (int i = 1; i <= n; i++) {
        cin >> p[i];
        if (p[i]!=-1) {
            pos.push_back(p[i]);
            fl=true;
            if (val[p[i]]==-1) {
                val[p[i]]=i-1;
            }
            val2[p[i]+1]=max(val2[p[i]+1],i);
        }
    }
    int ans=0,best=-INF;
    if (pos.empty()) {
        best=0;
        for (int i=1;i<=n;i++) {
            if (a[i].second>=0) {
                ans+=a[i].first+a[i].second*(i-1);
            }else {
                ans+=a[i].first;
            }
            best=max(best,ans);
        }
        cout<<best<<endl;
        return;
    }
    vector<int> mn(n+2,0),mx(n+2,n);
    for (int t = 1; t <= n; t++) {
        if (val2[t] != -1) {
            mn[t] = val2[t];
        }
        mn[t] = max(mn[t], mn[t - 1]);
    }
    for (int t = n; t >= 0; t--) {
        if (val[t] != -1) {
            mx[t] = val[t];
        }
        mx[t] = min(mx[t], mx[t + 1]);
    }
    int last=pos.back();
    
    for (int i=1;i<=n;i++) {
        if (a[i].second>=0) {
            ans+=a[i].first+a[i].second*min(mx[i],i-1);
        }else {
            ans+=a[i].first+a[i].second*mn[i];
        }
        if (i>=last) {
            best=max(best,ans);
        }
    }
    cout<<best<<endl;
}
