#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e18, MOD = 998244353;
mt19937 rnd(time(0));

void solve();

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    vector<int> pa(n + 1);
    vector<int> pb(n + 1);
    for (int i = 0; i < n; i++){
        int a, b;
        cin >> a >> b;
        pa[i + 1] = pa[i] + a;
        pb[i + 1] = pb[i] + b;
    }
    vector<vector<int>> dp(n + 1, vector<int> (n + 1, -INF));
    vector<vector<int>> pr(n + 2, vector<int> (n + 2, -INF));
    vector<int> p(n + 1, -1);
    for (int i = 1; i < n + 1; i++){cin >> p[i];}
    dp[0][0] = 0;
    for (int i = 0; i <= n; i++){
        pr[0][i] = 0;
    }
    int mx = 0;
    for (int i = 1; i <= n; i++){
        mx = max(mx, p[i]);
        if (p[i] == -1){
            for (int j = 0; j <= n; j++){
                dp[i][j] = max(pr[i - 1][j] + pa[j] + pb[j] * (i - 1), dp[i][j]);
                if (j >= mx){
                    dp[i][j] = max(dp[i][j], dp[i - 1][j]);
                }
                if (j >= i){
                    if (j == 0){
                        pr[i][j] = max(pr[i][j], dp[i][j] - pa[j] - i * pb[j]);
                    }
                    else{
                        pr[i][j] = max(pr[i][j - 1], dp[i][j] - pa[j] - i * pb[j]);
                    }
                }
            }
        }
        else{
            dp[i][p[i]] =  max(pr[i - 1][p[i]] + pa[p[i]] + pb[p[i]] * (i - 1), dp[i][p[i]]);
            dp[i][p[i]] = max(dp[i][p[i]], dp[i - 1][p[i]]);
            for (int j = i; j <= n; j++){
                if (j == 0){
                    pr[i][j] = max(pr[i][j], dp[i][j] - pa[j] - i * pb[j]);
                }
                else{
                    pr[i][j] = max(pr[i][j - 1], dp[i][j] - pa[j] - i * pb[j]);
                }
            }
        }
    }
    /*for (auto x : dp){
        for (auto y : x){
            cout << y << " ";
        }
        cout << "\n";
    }*/
    int ans = -INF;
    for (int i = 0; i < n + 1; i++){
        ans = max(ans, dp[n][i]);
    }
    cout << ans;
}




