#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define fi first
#define se second
#define pb push_back
#define pob pop_back
#define endl '\n'
#define fori(s,n) for(int i=s;i<n;i++)
#define forj(s,n) for(int j=s;j<n;j++)
#define int long long
#define ull unsigned long long
#define ll long long
#define ld long double
using namespace std;
using namespace __gnu_pbds;
random_device rd;
mt19937 rnd(rd());
void solve(){
    int n;cin>>n;
    vector<pair<int,int>>ab(n);
    vector<int>p(n);
    fori(0,n) cin>>ab[i].fi>>ab[i].se;
    fori(0,n) cin>>p[i];
    vector<vector<int>>dp(n+1,vector<int>(n+1,-1e18));
    dp[0][0]=0;
    int xx=0;
    fori(0,n){
        forj(1,n+1){
            int s=0;
            for(int k=j-1;k>=0;k--){
                s+=ab[k].fi+ab[k].se*min(k,i);
                dp[j][i+1]=max(dp[j][i+1],dp[k][i]+s);
            }
        }
        forj(0,n+1) dp[j][i+1]=max(dp[j][i+1],dp[j][i]);
        if(p[i]!=-1){
            xx=i+1;
            forj(0,n+1){
                if(j==p[i]) continue;
                dp[j][i+1]=-1e18;
            }
        }
    }
    int ans=-1e18;
    forj(xx,n+1){
        fori(0,n+1) ans=max(ans,dp[i][j]);
    }
    cout<<ans<<endl;
}
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int tt=1;
    //cin>>tt;
    while(tt--) solve();
    return 0;
}
