#define GLIBCXX_DEBUG 1
#include <bits/stdc++.h>

using namespace std;
using ll = long long;


int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    ll n;
    cin >> n;
    vector<ll> a(n), b(n);
    for (ll i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
    }
    ll mx = -1;
    vector<ll> p(n);
    for (ll i = 0; i < n; ++i) {
        cin >> p[i];
        mx = max(mx, p[i]);
    }
    vector<ll> pref(n);
    pref[n - 1] = p[n - 1];
    if (p[n - 1] == -1) {
        pref[n - 1] = n;
    }
    for (ll i = n - 2; i >= 0; --i) {
        pref[i] = p[i];
        if (p[i] == -1) {
            pref[i] = pref[i + 1];
        }
    }
    ll ans = -1e18, cur = 0, last = -1;
    if (mx == -1) {
        ans = 0;
    }
    for (ll i = 0; i < n; ++i) {
        last++;
        while (last < p[i]) {
            if (last >= i) {
                cur += a[last] + i * b[last];
            }
            last++;
        }
        if (last >= mx) {
            ans = max(ans, cur);
        }
        ll l = last, c = cur, mn = 0, sum = 0;
        while (last < pref[i]) {
            if (last >= i) {
                cur += a[last] + i * b[last];
                sum += b[last];
                if (sum < mn) {
                    mn = sum;
                }
            }
            last++;
            if (last >= mx) {
                ans = max(ans, cur);
            }
        }
        last = l;
        cur = c;
        sum = 0;
        if (mn < 0) {
            while (last < pref[i]) {
                if (last >= i) {
                    cur += a[last] + i * b[last];
                    sum += b[last];
                    if (sum == mn) {
                        last++;
                        break;
                    }
                }
                last++;
            }
        }
        if (last == i && a[i] + i * b[i] > 0) {
            cur += a[i] + i * b[i];
            last++;
        }
        if (last >= mx) {
            ans = max(ans, cur);
        }
        last--;
    }
    cout << ans;
}
