#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e18, MOD = 998244353;
mt19937 rnd(time(0));

void solve();

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    vector<int> pa(n + 1);
    vector<int> pb(n + 1);
    vector<int> aa(n);
    vector<int> bb(n);
    for (int i = 0; i < n; i++){
        int a, b;
        cin >> a >> b;
        pa[i + 1] = pa[i] + a;
        pb[i + 1] = pb[i] + b;
        aa[i] = a;
        bb[i] = b;
    }
    vector<int> p(n + 1, -1);
    for (int i = 0; i < n; i++){cin >> p[i];}
    int pr = -1, ans = 0, mm = 0;
    for (int i = 0; i < n; i++){
        if (p[i] == -1){continue;}
        for (int j = mm + 1; j <= p[i]; j++){
            ans += aa[j] + bb[j] * (pr + 1);
        }
        mm = max(mm, p[i]);
        pr = i;
    }
    vector<int> pp = {0};
    for (int i = mm + 1; i < n; i++){
        pp.push_back(pp.back() + aa[i] + bb[i] * (pr + 1));
    }
    int mx = 0;
    for (auto x : pp){
        mx = max(mx, x);
    }
    ans += mx;
    cout << ans;
}





