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

using namespace std;
using ll = long long;

struct Segtree {
    vector<pair<ll, ll> > tree;
    ll sz;

    void build(ll n) {
        sz = 1;
        while (sz < n) {
            sz *= 2;
        }
        tree.resize(sz * 2 - 1);
    }

    void update(ll v, ll l, ll r, ll q, ll x) {
        if (l > q || r <= q) {
            return;
        }
        if (r - l == 1) {
            tree[v] = {x, l};
            return;
        }
        ll m = (l + r) / 2;
        update(v * 2 + 1, l, m, q, x);
        update(v * 2 + 2, m, r, q, x);
        tree[v] = tree[v * 2 + 2];
        if (tree[v * 2 + 1].first < tree[v * 2 + 2].first) {
            tree[v] = tree[v * 2 + 1];
        }
    }

    pair<ll, ll> get(ll v, ll l, ll r, ll lq, ll rq) {
        if (l >= rq || r <= lq) {
            return {1e18, -1};
        }
        if (l >= lq && r <= rq) {
            return tree[v];
        }
        ll m = (l + r) / 2;
        auto gl = get(v * 2 + 1, l, m, lq, rq), gr = get(v * 2 + 2, m, r, lq, rq);
        if (gl.first < gr.first) {
            return gl;
        }
        return gr;
    }
};

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    ll n;
    cin >> n;
    vector<ll> a(n), b(n), prefb(n);
    for (ll i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
        prefb[i] = b[i];
        if (i > 0) {
            prefb[i] += prefb[i - 1];
        }
    }
    Segtree tree;
    tree.build(n);
    for (ll i = 0; i < n; ++i) {
        tree.update(0, 0, tree.sz, i, prefb[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);
        }
        auto g = tree.get(0, 0, tree.sz, last, pref[last]);
        if (last > 0) {
            g.first -= prefb[last - 1];
        }
        ll sum = 0;
        if (g.first < 0);
        while (last <= g.second) {
            if (last >= i) {
                cur += a[last] + i * b[last];
                sum += b[last];
                if (sum == g.first) {
                    last++;
                    if (last >= mx) {
                        ans = max(ans, cur);
                    }
                    break;
                }
                last++;
                if (last >= mx) {
                    ans = max(ans, cur);
                }
            }
        }
        if (last == i) {
            cur += a[i] + i * b[i];
            last++;
        }
        if (last >= mx) {
            ans = max(ans, cur);
        }
        last--;
    }

    cout
            <<
            ans;
}
