#include<bits/stdc++.h>
#define all(p) p.begin(),p.end()
using namespace std;
#define int long long
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    int a[n + 1], b[n + 1];
    for (int i = 1; i <= n; i++) {
        cin >> a[i] >> b[i];
    }

    int p[n + 1];
    int l[n + 1];
    int r[n + 1];
    iota(r + 1, r + n + 1, 0);
    fill(l + 1, l + n + 1, 0);
    int mxx = 0;
    for (int i = 1; i <= n; i++) {
        cin >> p[i];
        mxx = max(mxx, p[i]);
        r[p[i]] = min(r[p[i]], i - 1);
        if (p[i] != n) {
            l[p[i] + 1] = max(l[p[i] + 1], i);
        }
    }
    for (int i = 2; i <= n; i++) {
        l[i] = max(l[i], l[i - 1]);
    }
    for (int i = n - 1; i >= 1; i--) {
        r[i] = min(r[i + 1], r[i]);
    }
    int ans = -LLONG_MAX;
    if (mxx == 0) {
        ans = 0;
    }
    int dp[11][n + 1];
    int prefb[n + 1];
    fill(prefb, prefb + n + 1, 0);
    prefb[0] = 0;
    int prefa[n + 1];
    fill(prefa, prefa + n + 1, 0);
    prefa[0] = 0;
    for (int i = 1; i <= n; i++) {
        prefb[i] = prefb[i - 1] + b[i];
        prefa[i] = prefa[i - 1] + a[i];
    }
    for (int i = 0; i <= 10; i++) {
        for (int j = 0; j <= n; j++) {
            dp[i][j] = -LLONG_MAX / 2;
        }
    }
    if (mxx == 0) {
        ans = 0;
    }
    dp[0][0] = 0;
    vector<int>gr = {0};
    for (int i = 1; i <= n; i++) {
        if (b[i] > 0) {
            gr.push_back(i);
        }
    }
    // cout << gr.size() << endl;
    for (int i = 1; i < gr.size(); i++) {
        int mx = -LLONG_MAX / 2;
        int mxdp = -LLONG_MAX, mxi = 0;
        int L = gr[i - 1] + 1, R = gr[i] - 1;
        for (int j = 0; j < gr[i]; j++) {
            mx = max(mx, dp[i - 1][j] + prefa[R] - prefa[L - 1] + j * (prefb[R] - prefb[L - 1]));
            // if (l[i] <= j && j <= r[i]) {
            dp[i][j] = max(dp[i][j], mx + j * b[gr[i]] + a[gr[i]]);
            // if (i >= mxx) {
            ans = max(ans, dp[i][j]);
            // }
            // }
            // cout << dp[i][j] << ' ';
        }
        // cout << endl;
    }
    cout << ans;
}