#include <experimental/random>
#include <bits/stdc++.h>

using namespace std;
//experimental::randint(1, 10);
#define ll long long

ll MOD = 998244353;
ll INF = 1e18;


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ll n;
    cin >> n;
    vector<ll> a(n);
    vector<ll> b(n);
    vector<ll> pref_a(n + 1);
    vector<ll> pref_b(n + 1);
    for (ll i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
        pref_a[i + 1] = pref_a[i] + a[i];
        pref_b[i + 1] = pref_b[i] + b[i];
    }
    vector<ll> p(n);
    ll ls = -1;
    ll mx = 0;
    for (ll i = 0; i < n; i++) {
        cin >> p[i];
        if (p[i] != -1) {
            ls = i;
        }
    }
    ll ans = -INF;
    ll last = 0;
    ll cnt = 0;
    ll j = n;
    for (ll i = 0; i < n; i++) {
        if (p[i] != -1) {
            cnt += ((pref_b[p[i]] - pref_b[last]) * i) + (pref_a[p[i]] - pref_a[last]);
            last = p[i];
        } else {
            j = i;
            break;
        }
    }
    if (j == n) {
        ans = cnt;
        cout << ans << endl;
        return 0;
    }
    if (j != 0) {
        ans = cnt;
    }
    for (ll i = max(last + 1, j-1); i <= n; i++) {
        ans = max(ans, a[i - 1] + cnt + b[i - 1] * (i - 1));
        cnt += a[i - 1] + b[i - 1] * (i - 1);
    }
    cout << ans << endl;
}
