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

#define int long long
#define ll long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ff first
#define ss second

void solve() {
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
    }
    vector<int> p(n);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
    }

    int ans = -1e18;

    vector<int> t(n, 0);
    for (int i = 0; i < n; ++i) {
        if (b[i] < 0) t[i] = 0;
        else t[i] = i;
    }
    int pr = n;
    for (int i = n - 1; i >= 0; --i) {
        if (p[i] == -1) continue;
        int k = i + 1;
        pr = min(pr, p[i]);
        while (k < pr) {
            t[k] = i;
            ++k;
        }
    }

    vector<int> pref(n, 0);
    for (int i = 0; i < n; ++i) {
        //cout << t[i] << ' ';
        pref[i] = a[i] + t[i] * b[i];
        if (i > 0) pref[i] += pref[i - 1];
    }
    //cout << '\n';
    for (int i = 0; i < n; ++i) {
        if (p[i] == -1 || i + 1 >= p[i]) {
            ans = max(ans, pref[i]);
        }
    }
    cout << ans << '\n';
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
