#include<bits/stdc++.h>
#define int long long
using namespace std;


const int INF = 1e18;

void solve() {
    int n, k, ans = -INF;
    cin >> n;
    vector<int> a(n + 1), b = a, p = a, pref(n + 1), b2 = b;
    for (int i = 1; i <= n; i++) {
        cin >> a[i] >> b[i];
        pref[i] = pref[i - 1] + a[i];
    }
    for (int i = 1; i <= n; i++) cin >> p[i];
    int i = 1;
    for (int t = 1; t <= n; t++) {
        if (p[t] != -1) {
            while (i <= p[t]) {
                b2[i] = max(i - 1, t - 1);
                i++;
            }
        }
    }
    while (i <= n) {
        b2[i] = i - 1;
        i++;
    }
    for (int t = n; t >= 0; t--) {
        if (p[t] == -1) continue;
        int s = 0;
        for (int i = 1; i < p[t]; i++) {
            s += b2[i] * b[i];
        }
        for (int i = p[t]; i <= n; i++) {
            s += b2[i] * b[i];
            ans = max(ans, s + pref[i]);
        }
        break;
    }
    cout << ans;
}

int32_t main() {
    cin.tie(0);
    ios::sync_with_stdio(0);

    solve();
}
