#include <bits/stdc++.h>

using namespace std;
using ll = long long;

struct Node {
    ll a, b;
};

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    ll n;
    cin >> n;

    vector<Node> a(n);
    for (ll i = 0; i < n; ++i)
        cin >> a[i].a >> a[i].b;

    vector<ll> p(n);
    for (ll i = 0; i < n; ++i)
        cin >> p[i];

    ll last_pi = 0;
    ll j = 0, pi = 0, ans = 0, now = 0;
    while (pi < n) {
        if (p[pi] == -1) {
            if (j == pi) {
                now += a[j].a + last_pi * a[j].b;
                ans = max(ans, now);
                j++;
            }
        }
        else {
            while (j < p[pi]) {
                now += a[j].a + last_pi * a[j].b;
                j++;
            }
            ans = now;
            last_pi = pi;
        }
        pi++;
    }

    cout << ans;
}