#ifndef WILEYNE
#pragma GCC optimize("Ofast")
#endif
#include <bits/extc++.h>

using ll = long long;
const ll LL_INF = 2e18;
using namespace std;
using ordered_set = __gnu_pbds::tree<ll,__gnu_pbds::null_type,less<>,__gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;
const int MOD = 998244353;
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();



int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#ifdef WILEYNE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif
    int n;
    cin>>n;
    vector<vector<ll>>dp(n,vector<ll>(n+1,-LL_INF));
    vector<vector<bool>>can(n,vector<bool>(n+1));
    vector<pair<ll,ll>>v(n);
    for (auto &c:v) cin>>c.first>>c.second;
    vector<int>p(n+2,0);
    int min_pick = 0;
    int maxs_pick = 0;
    for (int i = 1 ;i<=n;++i) {
        cin>>p[i];
        min_pick = max(min_pick,p[i]);
    }
    maxs_pick = min_pick;
    if (p[n] == -1) {
        maxs_pick = n;
    }
    p[n+1] = n;
    int last = 0;
    for (int i = 1;i<=n+1;++i) {
        if (p[i]!=-1) {
            for (int j = p[last];j<p[i];++j) {
                for (int k = last;k<i;++k) {
                    can[j][k] = true;
                }
            }
            last = i;
        }
    }
    dp[0][0] = v[0].first;
    for (int i = 1;i<n;++i) {
        vector<ll>pref_maxs(n+1);
        pref_maxs[0] = dp[i-1][0];
        for (int j = 1;j<=i;++j) {
            pref_maxs[j] = max(pref_maxs[j-1],dp[i-1][j]);
        }
        for (int j = 0;j<=i;++j) {
            if (can[i][j]) {
                dp[i][j] = pref_maxs[j]+v[i].first+v[i].second*j;
            }
        }
    }
    ll res = -LL_INF;
    for (int i = max(min_pick-1,0);i<min(maxs_pick,n);++i) {
        for (int j = 0;j<=i;++j) {
            res = max(res,dp[i][j]);
        }
    }
    cout<<res;
    return 0;
}