#include <bits/stdc++.h>

using ll = long long;

const ll INF = 2e18;
const ll MOD = 1e9 + 7;
const ll mod = 998244353;

using namespace std;

void solve() {
    int n;
    cin >> n;
    vector<ll> a(n), b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    vector<int> p(n), l(n), R(n);
    int mx = 0;
    for (int i = 0; i < n; i++) {
        l[i] = i;
    }
    bool fl = 1;
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        for (int j = 0; j < p[i]; j++) {
            l[j] = min(l[j], i);
        }
        if (p[i] == -1) {
            continue;
        } else {
            fl = 0;
        }
        for (int j = p[i]; j < n; j++) {
            R[j] = max(R[j], i + 1);
        }
        mx = max(mx, p[i]);
    }
    ll ans = 0;
    vector<ll> suff(n);
    suff[n - 1] = a[n - 1] + b[n - 1] * l[n - 1];
    for (int i = n - 2; i >= 0; i--) {
        suff[i] = suff[i + 1] + a[i] + b[i] * l[i];
    }
    for (int i = mx; i < n; i++) {
        ans = min(ans, suff[i]);
    }
    if (fl) {
        cout << max(0ll, suff[0] - ans);
    } else {
        cout << suff[0] - ans;
    }
}

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

    int t = 1;
    bool MULTITEST = 0;
    #ifdef egg
        MULTITEST = 1;
        freopen("test.txt", "r", stdin);
    #endif
    if (MULTITEST) {
        cin >> t;
    }
    while (t--) {
        solve();
    }
    return 0;
}
