#include<bits/stdc++.h>

using namespace std;
#define int long long
const int INF = 1e9;
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int n;
    cin >> n;
    vector<pair<int, int>> q(n);
    for (int i = 0; i < n; i++) {
        cin >> q[i].first >> q[i].second;
    }
    vector<int> p(n);
    for (int i = 0; i < n; i++) cin >> p[i];
    int lastlimit = 0;
    for (int i: p) {
        if (i != -1)
            lastlimit = i;
    }
    p.push_back(n);
    for (int i = n - 1; i >= 0; i--) {
        if (p[i] == -1) {
            p[i] = p[i + 1];
        }
    }
    vector<int> t(n, -INF);
    int lastp = 0;
    for (int i = 0; i < n; i++) {
        for (int j = lastp; j < p[i]; j++) {
            t[j] = i;
        }
        lastp = p[i];
    }
    vector<int> pref(n + 1, 0);
    pref[0] = 0;
    for (int i = 0; i < n; i++) {
        pref[i + 1] = pref[i] + q[i].first + t[i] * q[i].second;
    }
    int ind = n;
    for (int i = lastlimit + 2; i <= n; i++) {
        if (pref[ind] < pref[i]) ind = i;
    }
    cout << pref[ind];
    return 0;        
}