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

using ll = long long;

int main() {
    ll n;
    cin >> n;

    vector<ll> a(n), b(n), p(n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    vector<ll> mn(n, 0);
    for (ll i = 0; i < n; i++) {
        cin >> p[i];
        p[i]--;
        if (p[i] != -2 && p[i] + 1 < n)
            mn[p[i] + 1] = i + 1;
    }
    for (ll i = 1; i < n; i++) {
        mn[i] = max(mn[i - 1], mn[i]);
    }

    ll ans = 0, cur = 0;
    for (ll i = 0; i < n; i++) {
        if (b[i] > 0)
            cur += a[i] + b[i] * i;
        else
            cur += a[i] + b[i] * mn[i];
        ans = max(ans, cur);
    }

    cout << ans << '\n';
}