#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define ins insert

using namespace std;

struct ve {
    int v1;
    int v2;
};

int32_t main()
{
    int n, m, i, j, k, k1, k2, k3, x, x1, x2, x3, t, g;
    cin >> n;
    vector <ve> a;
    for (i = 0; i < n; i++) {
        cin >> x1 >> x2;
        a.pb({x1, x2});
    }
    vector <int> b(n);
    for (auto &it : b)
        cin >> it;
    vector <int> pref;
    //pref.pb(0);
    k = 0;
    vector <int> s(n);
    j = 0;
    for (i = 0; i < n; i++) {
        x = b[i];
        if (b[i] != -1) {
            for (j = j; j < b[i]; j++)
                s[j] = i;
            j = b[i];
        }
        else {
            for (j = j; j < n; j++) {
                if (a[j].v2 > 0) {
                    break;
                }
                s[j] = i;
            }
            for (j = j; j < n; j++) {
                s[j] = j;
            }
        }
    }
    x = a[0].v1 + a[0].v2 * s[0];
    for (i = 1; i < n; i++) {
        if (s[i] == s[i-1]) {
            x = x + (a[i].v1 + a[i].v2 * s[i]);
        }
        else {
            if (pref.size() > 0)
                pref.pb(pref.back() + x);
            else
                pref.pb(x);
            x = a[i].v1 + a[i].v2 * s[i];
        }
    }
    pref.pb(x);
    int ans = -1e9;
    for (auto z : pref)
        ans = max(ans, z);
    cout << ans;
    return 0;
}
