#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
long long int n,a[1000005],dp[5005][5005],can1[5005],can2[5005],otg=0;
int b[1000005],c[1000005];
void read()
{
    cin>>n;
    long long int p=0;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i]>>b[i];
        if(b[i]<=0)p+=a[i];
        else p+=a[i]+b[i]*(i-1);
        otg=max(otg,p);
    }
    int to=1,last=0;
    for(int i=1;i<=n;i++)
    {
        cin>>c[i];
        if(c[i]!=-1)
        {
            for(int j=to;j<=c[i];j++)
            {
                can1[j]=last+1;
            }
            for(int j=to;j<=c[i];j++)
            {
                can2[j]=i;
            }
            to=c[i]+1;
            last=i;
        }
    }
    cout<<otg<<endl;
    return;
    //cout<<endl;
    for(int i=to;i<=n;i++)
    {
        can1[i]=last+1;
        can2[i]=i;
    }
    can2[0]=n;
    cout<<endl;
    to=1;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            dp[j][i]=-1e18;
            if(can1[j-1]<=i)
            {
                //cout<<1<<endl;
                dp[j][i]=max(dp[j][i],dp[j-1][i]+a[j]+b[j]*(i-1));
                //cout<<dp[j][i]<<endl;
            }
            if(i>1)
            {
                //cout<<2<<endl;
                if(can1[j]<=i-1&&can2[j]>=i)dp[j][i]=max(dp[j][i],dp[j][i-1]+max(0,b[j]));
                else if(can1[j]<=i-1)dp[j][i]=dp[j][i-1];
            }
            //cout<<dp[j][i]<<" "<<j<<" "<<i<<endl;
            if(c[i]!=-1&&j==c[i])
            {
                otg=dp[j][i];
                break;
            }
            if(c[i]==-1)otg=max(otg,dp[j][i]);
        }
        if(c[i]!=-1)to=c[i];
    }
    cout<<otg<<endl;
}
int main()
{
    speed();
    read();
	return 0;
}
