#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;
    cin >> n;
    vector<pair<long long, long long>> arr(n);
    for(int i = 0; i < n; i++) cin >> arr[i].first >> arr[i].second;
    // vector<vector<long long>> dp(n, vector<long long>(n, LLONG_MIN / 2));

    vector<int> p(n);
    for(int i = 0; i < n; i++) cin >> p[i];
    vector<pair<int, int>> tms(n);
    int lstt = 0, ltm = 0;
    for(int i = 0; i < n; i++) {
        if(p[i] == -1) continue;
        int tmm = i, cnt = p[i];
        for(int j = lstt; j < cnt; j++) {
            tms[j] = {ltm, tmm};
        }
        ltm = tmm + 1;
        lstt = cnt;
    }
    for(int i = lstt; i < n; i++) tms[i] = {ltm, n};

    long long csB = 0;
    long long ans = LLONG_MIN / 2;
    int mxx = *max_element(p.begin(), p.end()) - 1;
    if(mxx < 0) ans = 0;
    long long cs = 0;
    for(int i = 0; i < n; i++) {
        cs += arr[i].first;
        csB += arr[i].second * tms[i].first;
        if(i < mxx) continue;
        ans = max(ans, csB + cs);
    }
    cout << ans << '\n';
}
