#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 = 0;
    for (int i = 0; i < n; i++) {
        int p;
        cin >> p;
        if (p != -1) {
            at_least = max(at_least, p);
            p--;
            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] = min(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]);
    int ans = -(int) 1e17;
    if (at_least == 0) ans = max(ans, 0ll);
    at_least = max(0ll, at_least - 1);
    for (int i = at_least; i < n; i++) {
        ans = max(ans, s[i]);
    }
    cout << ans;
    return 0;
}
