#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;

void solve() {
    int n;
    cin >> n;
    vector<ll> a(n), b(n);
    ll mx = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
        mx = max(mx, b[i]);
    }
    vector<int> p(n);
    int mp = -1;
    int l = n;
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        mp = max(p[i], mp);
    }
    vector<int> ogr(n + 1);
    ogr[n] = n;
    for (int i = n - 1; i >= 0; i--) {
        if (p[i] == -1) {
            ogr[i] = ogr[i + 1];
        }
        else {
            ogr[i] = p[i];
        }
    }
    if (mp > -1) {
        vector<ll> lev(n);
        vector<int> used(n);
        for (int i = 0; i < n; i++) {
            if (!used[i]) {
                lev[i] = a[i] + b[i] * i;
                used[i] = 1;
            }
            if (i) {
                lev[i] += lev[i - 1];
            }
            for (int j = i + 1; j < p[i]; j++) {
                if (!used[j]) {
                    lev[i] += a[j] + b[j] * i;
                    used[j] = 1;
                }
            }
        }
        ll ans = 0;
        bool ok = 1;
        for (int i = 0; i < n; i++) {
            if (ogr[i] < i + 1) {
                ok = 0;
            }
            if (p[i] > -1 && !ok) {
                break;
            }
            ans = max(ans, lev[i]);
        }
        cout << ans;
        return;
    }
    if (mx <= 0) {
        vector<ll> lev(n + 1);
        for (int i = 0; i < n; i++) {
            lev[i] = a[i];
            if (i) {
                lev[i] += lev[i - 1];
            }
        }
        ll ans = 0;
        for (int i = 0; i < n; i++) {
            ans = max(ans, lev[i]);
        }
        cout << ans;
        return;
    }
    ll ans = 0;
    vector<ll> lev(n);
    int pr = 0;
    for (int i = 0; i < n; i++) {
        lev[i] = a[i] + b[i] * i;
        if (i) {
            lev[i] += lev[i - 1];
        }
    }
    for (int i = 0; i < n; i++) {
        ans = max(ans, lev[i]);
    }
    cout << ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int T = 1;
    //cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}
