#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n; cin >> n;
    vector<ll> a(n + 1), b(n + 1);
    for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];
    vector<int> event(n + 2);
    
    vector<int> p(n + 1, -1);
    int mx = 0;
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        if (p[i] != -1) {
            mx = max(mx, i);
            event[p[i] + 1] = i;
        }
    }
    int tek = 0;
    vector<ll> pref(n + 1);
    ll answer = 0;
    for (int i = 1; i <= n; i++) {
        tek = max(tek, event[i]);
        pref[i] = pref[i - 1] + a[i] + b[i] * tek;
        if (tek >= mx) answer = max(answer, pref[i]);
    }
    
    cout << answer << '\n'; return 0;
    vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, -LLONG_MAX));
    dp[0][0] = 0;

    for (int t = 0; t < n; t++) {
        for (int i = max(1, t); i <= n; i++) {
            if (t > 0) dp[t][i] = dp[t - 1][i];
            if (dp[t][i - 1] == -LLONG_MAX) continue;
            dp[t][i] = max(dp[t][i], dp[t][i - 1] + b[i] * t + a[i]);
        } 
        if (p[t] != -1) {
            for (int i = 1; i <= n; i++) {
                if (i != p[t]) dp[t][i] = -LLONG_MAX;
            }
        }
    }
    ll ans = -LLONG_MAX;

    for (int t = mx; t < n; t++) {
        for (int i = mx; i <= n; i++) {
            ans = max(ans, dp[t][i]);
        }
    }
    cout << ans << '\n';
    return 0;
}