#include <iostream>
#include <vector>
using namespace std;
vector<long long> A;
vector<long long> B;
int main()
{
    long long n,a,b,p;
    cin >> n;
    vector<long long> mn(n,0);
    vector<long long> mx(n);
    vector<bool> up(n,true);
    for(long long i=0;i<n;i++)
    {
        mx[i]=i;
    }
    for(long long i=0;i<n;i++)
    {
        cin >> a >> b;
        A.push_back(a);
        B.push_back(b);
    }
    for(long long i=0;i<n;i++)
    {
        cin >> p;
        if(p!=-1)
        {
            for(long long j=0;j<p;j++)
            {
                mx[j]=min(mx[j],i);
                up[j]=false;
            }
            for(long long j=p;j<n;j++)
            {
                mn[j]=max(mn[j],i+1);
            }
        }
    }
    vector<vector<long long>> dp(n,vector<long long>(n+1,-1e18));
    vector<long long> pref(n+1,0);
    for(long long i=0;i<n;i++)
    {
        for(long long j=mn[i];j<=mx[i];j++)
        {
            dp[i][j]=pref[j];
            dp[i][j]+=A[i]+B[i]*j;
        }
        if(up[i])
        {
            dp[i][n]=pref[n];
        }
        for(long long j=0;j<=n;j++)
        {
            pref[j]=-1e18;
        }
        pref[0]=dp[i][0];
        for(long long j=1;j<=n;j++)
        {
            pref[j]=max(pref[j-1],dp[i][j]);
        }
    }
    cout << pref[n];
    return 0;
}
