#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
using ll = long long;
constexpr int maxn = 2e5 + 3, mod = 1e9 + 7, inf = 1e9;
constexpr ll big_inf = 1e16;
constexpr __int128_t bnf = 1e30;
mt19937 rng(122);

int rand_int(int l, int r) {
    return uniform_int_distribution<int>(l, r)(rng);
}

struct segtr {
    struct node {
        int sm;
    };

    int n{};
    vector<node> t;
    vector<int> a;

    node merge(node a, node b) {
        node c{};
        c.sm = a.sm + b.sm;
        return c;
    }

    void build(int v, int tl, int tr) {
        if (tl + 1 == tr) {
            t[v].sm = a[tl];
            return;
        }
        int tm = (tl + tr) / 2;
        build(2 * v, tl, tm);
        build(2 * v + 1, tm, tr);
        t[v] = merge(t[v * 2], t[v * 2 + 1]);
    }

    segtr(int sz, vector<int> &b) {
        a = b;
        int k = 1;
        while (k < sz) {
            k *= 2;
        }
        n = k;
        t.assign(2 * n + 1, {0});
        build(1, 1, n + 1);
    }

    node get(int v, int tl, int tr, int ql, int qr) {
        if (tl >= qr || ql >= tr) {
            return {0};
        }
        if (ql <= tl && tr <= qr) {
            return t[v];
        }
        int tm = (tl + tr) / 2;
        return merge(get(v * 2, tl, tm, ql, qr),
                     get(v * 2 + 1, tm, tr, ql, qr));
    }

    void upd(int v, int tl, int tr, int ps, int x) {
        if (ps < tl || ps >= tr)
            return;
        if (tl + 1 == tr) {
            t[v].sm = x;
            return;
        }
        int tm = (tl + tr) / 2;
        upd(v * 2, tl, tm, ps, x);
        upd(v * 2 + 1, tm, tr, ps, x);
        t[v] = merge(t[v * 2], t[v * 2 + 1]);
    }
};

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n;
    cin >> n;
    vector<ll> a(n + 1), b(n + 1);
    vector<__int128_t> p(n + 1);
    vector<ll> pra(n + 1), prb(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> a[i] >> b[i];
        pra[i] = pra[i - 1], prb[i] = prb[i - 1];
        pra[i] += a[i], prb[i] += b[i];
    }
    vector<int> mn(n + 2), mx(n + 2);
    for (int i = 1; i <= n; i++) {
        mx[i] = i - 1;
    }
    int kt = 0;
    for (int i = 1; i <= n; i++) {
        cin >> p[i];
        kt = max(p[i], kt);
        if (p[i] != -1) {
            mn[p[i] + 1] = max(mn[p[i] + 1], i);
            mx[p[i]] = min(mx[p[i]] , i - 1);
        }
    }
    for (int i = 2; i <= n; i++) {
        mn[i] = max(mn[i - 1], mn[i]);
    }
    for (int j = n - 1; j >= 1; j--) {
        mx[j] = min(mx[j + 1], mx[j]);
    }
    // for (int i = 1; i <= n; i++) {
    //     cout << mn[i] << ' ' << mx[i] << '\n';
    // }
    vector<__int128_t> dp1(n + 1, -bnf), dp2(n + 1, -bnf);
    dp1[0] = 0;
    __int128_t res = -bnf;
    if (kt == 0) {
        res = 0;
    }
    int pr = 0;
    for (int i = 1; i <= n; i++) {
        __int128_t kf = -bnf;
        if (b[i] > 0) {
            for (int j = 0; j < mn[i]; j++) {
                kf = max(kf, dp1[j] + pra[i] - pra[pr] + __int128_t(j) * (prb[i - 1] - prb[pr]));
            }
            for (int j = mn[i]; j <= mx[i]; j++) {
                kf = max(kf, dp1[j] + pra[i] - pra[pr] + __int128_t(j) * (prb[i - 1] - prb[pr]));
                dp2[j] = kf + ll(j) * b[i];
            }
            for (int j = 1; j <= n; j++) {
                dp1[j] = dp2[j];
                dp2[j] = -bnf;
            }
            if (i >= kt) {
                for (int j = mn[i]; j <= mx[i]; j++) {
                    res = max(res, dp1[j]);
                }
            }
            pr = i;
        }
    }
    if (res == 0) {
        cout << 0;
    }
    if (res < 0) {
        cout << '-';
        res = -res;
    }
    vector<int> vt;
    while (res > 0) {
        vt.push_back(res % 10);
        res /= 10;
    }
    reverse(vt.begin(), vt.end());
    for (auto c: vt) {
        cout << c;
    }
}