#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 + 1);
    for (int i = 1; i <= n; i++) {
        cin >> q[i].first >> q[i].second;
    }
    vector<int> p(n + 2, 0);
    for (int i = 1; i <= n; i++) cin >> p[i];
    p[n] = n;
    int indlimit = 0;
    int lastlimit = 0;
    for (int i = 1; i <= n; i++) {
        if (p[i] != -1) {
            indlimit = i;
            lastlimit = p[i];
        }
    }
    int prev = 0;
    int next = 1;
    vector<int> t(n + 1);
    for (int i = 1; i <= n; i++) {
        if (q[i].second <= 0) {
            t[i] = prev;
        }
        else {
            t[i] = next - 1;
        }
        if (i == p[next]) {
            prev ++;
            next ++;
        }
    }
    vector<int> pref(n + 2, 0);
    for (int i = 1; i <= n; i++) {
        pref[i] = pref[i - 1] + q[i].first + t[i] * q[i].second;
    }
    int ind = n;
    for (int i = lastlimit + 1; i <= n; i++) {
        if (pref[ind] < pref[i]) {
            ind = i;
        }
    }
    cout << pref[ind];
    return 0;        
}