#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);
    int maxp = 0;
    vector<int> rev(n, 1e9);
    int prv = 0;
    for(int i = 0; i < n; i++){
        cin >> p[i];
        maxp = max(maxp, p[i]);
        if(p[i] != -1){
            for(int j = 0; j < p[i]; j++){
                if(rev[j] == 1e9){
                    rev[j] = i;
                }
            }
            prv = p[i];
        }
    }
    vector<int> prefans(n + 1, -1e18);
    prefans[0] = 0;
    for(int i = 0; i < n; i++){

        for(int j = i; j >= 0; j--){
            if(rev[j] != rev[i]){
                break;
            }
            prefans[i + 1] = max(prefans[i + 1], prefans[j] + (prefa[i + 1] - prefa[j]) + min(rev[i], j) * (prefb[i + 1] - prefb[j]));
        }
    }
    int ans = -1e18;
    for(int i = maxp; i <= n; i++){
        ans = max(ans, prefans[i]);
    }
    cout << ans << endl;
}
