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

signed main(){
    int n;
    cin >> n;
    vector<pair<int, int>> a(n);
    for(int i = 0; i < n; i++){
        cin >> a[i].first >> a[i].second;
    }
    vector<int> p(n);
    for(int i = 0; i < n; i++){
        cin >> p[i];
    }
    int lch = -1;
    int ans = 0;
    for(int i = 0; i < n; i++){
        if(p[i] == -1){
            continue;
        }
        for(int j = lch + 1; j < p[i]; j++){
            ans += a[j].first + min(i, j) * a[j].second;
        }
        lch = p[i] - 1;
    }
    int mx = 0;
    int nowsm = 0;
    for(int i = lch + 1; i < n; i++){
        nowsm += a[i].first + i * a[i].second;
        mx = max(mx, nowsm);
    }
    cout << ans + mx << endl;
}
