#include <experimental/random>
#include <bits/stdc++.h>

using namespace std;
//experimental::randint(1, 10);
#define ll long long

ll MOD = 998244353;
ll INF = 1e18;


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ll n;
    cin >> n;
    vector<ll> a(n);
    vector<ll> b(n);
    vector<ll> pref_a(n + 1);
    vector<ll> pref_b(n + 1);
    for (ll i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
        pref_a[i + 1] = pref_a[i] + a[i];
        pref_b[i + 1] = pref_b[i] + b[i];
    }
    vector<ll> p(n);
    ll ls = -1;
    for (ll i = 0; i < n; i++) {
        cin >> p[i];
        if (p[i] != -1) {
            ls = i;
        }
    }
    vector<vector<ll> > dp(n + 1, vector<ll>(n + 1, -INF));
    for (ll i = 0; i <= n; i++) {
        dp[0][i] = pref_a[i];
    }
    ll ans = -INF;
    if (ls == -1) {
        for (ll ii = 0; ii <= n; ii++) {
            ans = max(ans, dp[0][ii]);
        }
    }
    ll last = 0;
    for (ll i = 1; i <= n; i++) {
        last = max(last, p[i - 1]);
        if (p[i - 1] != -1) {
            for (ll e = max(last, i); e <= p[i - 1]; e++) {
                dp[i][p[i - 1]] = max(dp[i][p[i - 1]],
                                      dp[i - 1][e] + pref_a[p[i - 1]] - pref_a[e] + (
                                          (pref_b[p[i - 1]] - pref_b[e]) * i));
            }
        } else {
            for (ll e = max(last, i); e <= n; e++) {
                for (ll k = max(e, i); k <= n; k++) {
                    dp[i][k] = max(dp[i][k], dp[i - 1][e] + pref_a[k] - pref_a[e] + ((pref_b[k] - pref_b[e]) * i));
                }
            }
        }
        if (ls <= i + 1) {
            for (ll ii = 0; ii <= n; ii++) {
                ans = max(ans, dp[i][ii]);
            }
        }
    }
    cout << ans << endl;
}
