#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);
    int ans=0;
    int best=0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i].first >> a[i].second;
        if (a[i].second<0) {
            ans+=a[i].first;
        }else {
            ans+=a[i].first+a[i].second*(i-1);
        }
        best=max(best,ans);
    }
    vector<int> p(n + 2,-1);
    vector<int> val(n+2,-1);
    vector<int> val2(n+2,-1);
    bool fl=false;
    int pos=0;
    for (int i = 1; i <= n; i++) {
        cin >> p[i];
        pos=max(pos,p[i]);
        if (p[i]!=-1) {
            fl=true;
            if (val[p[i]]==-1) {
                val[p[i]]=i-1;
            }
            val2[p[i]+1]=max(val2[p[i]+1],i);
        }
    }
    cout<<best<<endl;
}
