#include <bits/stdc++.h>
using namespace std;
#define int long long

vector<int> prefa, prefb;

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    vector<pair<int, int>> a(n);
    for(int i = 0; i < n; i++){
        cin >> a[i].first >> a[i].second;
    }
    prefb.resize(n + 1);
    prefa.resize(n + 1);
    for(int i = 0; i < n; i++){
        prefb[i + 1] = prefb[i] + a[i].second;
        prefa[i + 1] = prefa[i] + a[i].first;
    }
    vector<int> p(n), hlp(n, -1), hlp2(n, 0);
    for(int i = 0; i < n; i++){
        hlp[i] = i;
    }
    int maxp = 0;
    int prv = 0, prvh = 0;
    for(int i = 0; i < n; i++){
        cin >> p[i];
        maxp = max(maxp, p[i]);
        if(p[i] == -1){
            continue;
        }
        for(int j = prv; j < p[i]; j++){
            hlp[j] = min(hlp[j], i);
            hlp2[j] = prvh;
        }
        prv = p[i];
        prvh = i + 1;
    }

    vector<int> prefans(n + 1, -1e9);
    prefans[0] = 0;
    for(int i = 0; i < n; i++){
        //prefans[i + 1] = prefans[i] + a[i].first + i * a[i].second;
        for(int j = i; j >= hlp2[i]; j--){
            prefans[i + 1] = max(prefans[i + 1], prefans[j] + prefa[i + 1] - prefa[j] + min(hlp[i], j) * (prefb[i + 1] - prefb[j]));
        }

    }

    int ans = -1e9;
    for(int i = maxp; i <= n; i++){
        ans = max(ans, prefans[i]);
    }
    cout << ans << endl;
}
