#include<bits/stdc++.h>
#define ll long long
#define ld long double

using namespace std;

ll n;
vector<ll> a, b, p;

void input() {
    cin >> n;
    a.resize(n);
    b.resize(n);
    p.resize(n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    for (ll i = 0; i < n; i++) {
        cin >> p[i];
    }
}

void solve() {
    vector<ll> real(n, n);
    real[n - 1] = n;
    for (ll i = n - 2; i > -1; i--) {
        if (p[i] != -1) {
            real[i] = p[i];
        } else {
            real[i] = real[i + 1];
        }
    }
    ll lst = 0, l = 0, ans = 0, sv = -1e18 - 2;
    for (ll i = 0; i < n; i++) {
        if (real[i] > lst) {
            if (real[i] == n && p[i] != n) {
                sv = max(sv, ans);
                for (ll j = lst; j < real[i]; j++) {
                    ans += a[j] + b[j] * i;
                    sv = max(sv, ans);
                }
                break;
            }
            for (ll j = lst; j < real[i]; j++) {
                ans += a[j] + b[j] * i;
            }
        }
        lst = real[i];
    }
    cout << max(sv, ans);
}

void output() {

}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    input();
    solve();
    output();
    return 0;
}
