#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

int main(){
    int n;
    cin >> n;
    vector<ll> a(n + 1),b(n + 1),p(n + 1);
    for(int i = 1;i <= n;i++){
        cin >> a[i] >> b[i];
    }
    int aa = 0;
    vector<int> ind(n + 1,-1),t1(n + 1,-1),t2(n + 2,-1),ma(n + 1);
    for(int i = 1;i <= n;i++){
        cin >> p[i];
        if(p[i] != -1){
            ind[p[i]] = i;
            if(t1[p[i]] == -1){
                t1[p[i]] = i;
            }

            t2[p[i] + 1] = i;
            aa = p[i];
        }
    }
    //ll an = -1e18;
    vector<ll> dp(n + 1);
    for(int i = 1;i <= n;i++){
        ma[i] = i - 1;
        if(t1[i] != -1){
            ma[i] = min(ma[i],t1[i] - 1);
        }
        dp[i] = dp[i-1] + a[i] +b[i]*ma[i];
    }
    ll an = -1e18;
    for(int i = aa;i <= n;i++){
        an = max(an,dp[i]);
    }
    cout << an;
    return 0;
}
