#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

const int INF = 1e18, MOD = 998244353;


void solve(){
    int n; cin >> n;
    vector<pair<int, int>> ev(n);
    for (auto &[el1, el2]: ev) cin >> el1 >> el2;
    vector<int> a(n); for (auto &ell: a) cin >> ell;
    int l = 0, sm = 0, ind = 0;
    for (int i = 0; i < n; i++){
        if (a[i] == -1) break;
        while (l < a[i]){
            sm += ev[l].ff + ev[l].ss * i;
            l++;
        }
        ind = i+1;
    }

    vector<int> mx(n, -INF);

    int dop = 0;
    for (int i = l; i < n; i++){
        for (int j = ind; j <= i; j++){
            if (i == l) mx[j] = 0;
            mx[j] = mx[j] + ev[i].ff + ev[i].ss * j;
            dop = max(dop, mx[j]);
        }
        for (int j = ind+1; j < n; j++){
            mx[j] = max(mx[j], mx[j-1]);
        }
    }

    cout << sm + dop;
}

signed main(){
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    int t = 1; //cin >> t;
    while (t--){
        solve();
    }
    return 0;
}
