#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using db = double;
using pll = pair<ll ,ll>;
const ll LINF = 1ll<<62;
const db PI = acos(-1.0);
const ll P = 2777;
const ll MOD2 = 1e9 + 1000 - 7;

struct segtree {
    int n;
    vector<int> t;
    vector<int> d;

    void push(int ind, int l, int r) {
        if (d[ind] == 0)
            return;
        t[ind] += d[ind] * (r - l);
        if (r - l > 1) {
            d[ind * 2 + 1] += d[ind];
            d[ind * 2 + 2] += d[ind];
        }
        d[ind] = 0;
    }

    segtree(int n) : n(n) {
        t.resize(n * 4);
        d.resize(n * 4);
    }

    void update(int ind, int l, int r, int ql, int qr, int add) {
        push(ind, l, r);
        if (r <= ql || qr <= l)
            return;
        if (ql <= l && r <= qr) {
            d[ind] += add;
            push(ind, l, r);
        }
        int m =(r + l) / 2;
        update(ind * 2 + 1, l, m, ql, qr, add);
        update(ind * 2 + 2, m, r, ql, qr, add);
        t[ind] = t[ind * 2 + 1] + t[ind * 2 + 2];
    }

    void update(int ql, int qr, int add) {
        update(0, 0, n, ql, qr, add);
    }

    ll query(int ind, int l, int r, int ql, int qr) {
        push(ind, l, r);
        if (r <= ql || qr <= l)
            return 0;
        if (ql <= l && r <= qr) {
            return t[ind];
        }
        int m =(r + l) / 2;
        return query(ind * 2 + 1, l, m, ql, qr) + query(ind * 2 + 2, m, r, ql, qr);
    }

    ll query(int ql, int qr) {
        return query(0, 0, n, ql, qr);
    }
};

void solve() {
    int n;
    cin >> n;
    vector<ll> a(n);
    vector<ll> b(n);
    vector<ll> p(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> p[i];
    }
    int la = 0;
    ll ans = 0;
    int ila = 0;
    for (int i = 0; i < n; i++) {
        if (p[i] != -1) {
            for (ll j = la; j < p[i]; j++) {
                if (b[j] <= 0)
                    ans += a[j] + ila * b[j];
                else {
                    if (j < i) {
                        ans += a[j] + j * b[j];
                    }
                    else {
                        ans += a[j] + i * b[j];
                    }
                }
            }
            la = p[i];
            ila = i + 1;
        }
    }
    if (p[n - 1] != -1) {
        cout << ans;
        return;
    }
    ll ansnow = ans;
    for (ll j = la; j < n; j++) {
        if (b[j] <= 0)
            ansnow += a[j] + ila * b[j];
        else {
            ansnow += a[j] + j * b[j];
        }
        ans = max(ansnow, ans);
    }
    cout << ans;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(10);
#ifdef HOLA
    freopen("input.txt", "r", stdin);
#endif
    int _t = 1;
    //cin >> _t;
    while (_t--) {
        solve();
    }
}