#include <bits/stdc++.h>

#define int long long

using namespace std;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<pair<int, int>> mass(n);
    for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        mass[i] = {a, b};
    }
    vector<int> p(n);
    int maxi = -1;
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        if (p[i] != -1) {
            maxi = i;
        }
    }
    int ii = 0;
    int last = 1;
    while (ii < maxi) {
        if (p[ii] != -1) {
            last = p[ii];
            ii++;
            continue;
        }
        int jj = ii;
        while (jj < maxi && p[jj] == -1) {
            p[jj] = max(jj + 1, last);
            jj++;
        }
        ii = jj;
    }
    last = 0;
    int i = 0;
    int ans = 0;
    while (i < n && p[i] != -1) {
        for (int j = last; j < p[i]; j++) {
            ans += mass[j].first + i * mass[j].second;
        }
        last = p[i];
        i++;
    }
    int cur = ans;
    for (int j = last; j < n; j++) {
        cur += mass[j].first + j * mass[j].second;
        ans = max(ans, cur);
    }
    cout << ans << endl;
    return 0;
}
