#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define all(v) v.begin(),v.end()

void fast_in_out() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
#ifdef LOCAL
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif
}

const ll N=1e6+10,INF=1e18;


ll a[N],b[N],l[N],r[N],p[N],p1[N],p2[N],dp[N],only[N];
int main() {
    fast_in_out();
    ll n,i,j;
    cin >> n;
    for (i=1;i<=n;i++)cin >> a[i] >> b[i];
    ll to=0;
    for (i=1;i<=n;i++)cin >> p[i],p1[i]=i-1,p2[i]=0;
    for (i=1;i<=n;i++) {
        if (p[i]==-1)continue;
        p1[p[i]]=min(p1[p[i]],i-1);
        p2[p[i]+1]=max(p2[p[i]+1],i);
        to=max(to,p[i]);
    }
    for (i=2;i<=n;i++)p2[i]=max(p2[i],p2[i-1]);
    for (i=n-1;i>=1;i--)p1[i]=min(p1[i],p1[i+1]);
    for (i=1;i<=n;i++) {
        l[i]=p2[i];
        r[i]=p1[i];
        if (b[i]<=0)only[i]=l[i];
        else
            only[i]=r[i];
    }
    for (i=0;i<=n;i++) {
        dp[i]=-INF;
    }
    dp[0]=0;
    ll ans=-INF;
    for (i=1;i<=n;i++) {
        dp[i]=dp[i-1]+a[i]+b[i]*only[i];
    }
    for (i=n;i>=to;i--) {
        for (j=0;j<=n;j++)ans=max(ans,dp[i]);
    }
    cout << ans;
    return 0;
}