#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 j = 0, pi = 0, ans = 0;
    while (pi < n && p[pi] != -1) {
        while (j < p[pi]) {
            ans += a[j].a + pi * a[j].b;
            j++;
        }
        pi++;
    }

    ll now = ans;
    for (ll i = j; i < n; ++i) {
        now += a[i].a + i * a[i].b;
        ans = max(ans, now);
    }

    cout << ans;
}