#include <bits/stdc++.h>

using namespace std;
#define int long long

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (int i = 0; i < n; i++)
        cin >> a[i] >> b[i];
    vector<int> h(n), mn(n);
    for (int i = 0; i < n; i++) h[i] = i, mn[i] = 0;
    int at_least = -1;
    vector<int> q(n);
    for (int i = 0; i < n; i++) {
        int p;
        cin >> p;
        q[i] = p;
        if (p != -1) {
            p--;
            at_least = max(at_least, p);
            h[p] = min(h[p], i);
            if (p + 1 < n)
                mn[p + 1] = i + 1;
            // for (int j = p; j >= 0; j--)
            //     h[j] = min(h[j], i);
            // for (int j = p + 1; j < n; j++)
            //     mn[j] = max(mn[j], i + 1);
        }
    }
    for (int j = n - 2; j >= 0; j--)
        h[j] = min(h[j], h[j + 1]);
    for (int j = 1; j < n; j++)
        mn[j] = max(mn[j], mn[j - 1]);
    vector<int> s(n);
    for (int i = 0; i < n; i++)
        s[i] = (i > 0 ? s[i - 1] : 0) + (a[i] + h[i] * b[i]);
    vector<int> s2(n);
    for (int i = 0; i < n; i++)
        s2[i] = (i > 0 ? s2[i - 1] : 0) + (a[i] + mn[i] * b[i]);
    int ans = -(int) 1e17;
    if (at_least == -1) ans = max(ans, 0ll);
    vector<int> mx(n);
    mx[n - 1] = s[n - 1];
    for (int i = n - 2; i >= 0; i--)
        mx[i] = max(s[i], mx[i + 1]);
    int smx = 0;
    for (int i = 0; i < n; i++) {
        smx = max(smx, q[i] - 1);
        if (i >= smx) {
            int res = s2[i];
            ans = max(ans, res);
        }
    }
    cout << ans;
    return 0;
}
