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

int main() {
    ll i,j,n,k,t,q;
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    vector<ll>A(n),B(n),P(n),L(n),R(n);
    for (i=0;i<n;i++) {
        cin>>A[i]>>B[i];
        L[i]=0,R[i]=i;
    }
    for (i=0;i<n;i++) {
        cin>>P[i];
    }
    ll last=0,pos=-1;
    ll mnk=0;
    for (i=0;i<n;i++) {
        if (P[i]!=-1) {
            mnk=max(mnk,P[i]);
            for (j=last;j<P[i];j++) {
                L[j]=max(L[j],pos+1);
                R[j]=min(R[j],i);
            }
            last=P[i];
            pos=i;
        }
    }
    vector<ll>best(n+1);
    best[0]=0;
    for (i=1;i<=n;i++) {
        best[i]=best[i-1];
        if (B[i-1]>0) {
            best[i]+=(A[i-1]+R[i-1]*B[i-1]);
        }
        else {
            best[i]+=(A[i-1]+L[i-1]*B[i-1]);
        }
    }
    ll ans=best[mnk];
    for (i=mnk+1;i<=n;i++) {
        ans=max(ans,best[i]);
    }
    cout<<ans<<'\n';
    return 0;
}