#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define asc(x) x.begin(), x.end()
#define des(x) x.rbegin(), x.rend()
#define pb push_back
#define eb emplace_back
#define ll long long
#define db long double
#define fori(a, b) for (int i = (a); i < (b); ++i)
#define forj(a, b) for (int j = (a); j < (b); ++j)

using namespace std;
using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;

mt19937 gen(228);

const ll inf = 1e18;

void solve() {
    int n; cin >> n;

    vector<ll> pra(n + 1, 0), prb(n + 1, 0), a(n), b(n);

    fori(0, n) {
        int x, y; cin >> x >> y;
        a[i] = x, b[i] = y;

        pra[i + 1] = pra[i] + x;
        prb[i + 1] = prb[i] + y;
    }

    vector<int> p(n);

    fori(0, n) {
        cin >> p[i];
    }

    vector<pair<int, int>> pos;
    fori(0, n) {
        if (p[i] != -1) {
            pos.eb(p[i], i);
        }
    }

    pos.eb(n, n - 1);

    auto solve = [&](int ql, int qr, int mn, int mx, bool stop) {
        if (ql > qr || mn > mx) return 0ll;

        ll ans = 0, best = 0;

        vector<int> ps;

        fori(ql, qr + 1) {
            if (b[i] > 0) {
                ps.pb(i);
            }
        }

        int idx = 0;
        vector<ll> dp(mx - mn + 2, 0);
        for (int i : ps) {
            vector<ll> ndp(mx - mn + 2, LLONG_MIN);

            for (int t = mn; t <= min(mx, i); ++t) {
                ndp[t - mn] = dp[t - mn] + a[i] + b[i] * t;

                best = max(best, ndp[t - mn]);

                if (idx + 1 == ps.size()) {
                    ndp[t - mn] += pra[qr + 1] - pra[i + 1] + (prb[qr + 1] - prb[i + 1]) * t;
                } else {
                    ndp[t - mn] += pra[ps[idx + 1]] - pra[i + 1] + (prb[ps[idx + 1]] - prb[i + 1]) * t;
                }

                ++idx;

                if (t > mn) {
                    ndp[t - mn] = max(ndp[t - mn], ndp[t - mn - 1]);
                }
            }

            swap(dp, ndp);
        }

        if (stop) return best;

        return dp.back();
    };

    ll ans = 0;

    int last = 0;

    int mn = 0, ql = 0;
    for (auto[qr, mx] : pos) {
        ++last;

        if (last == pos.size()) {
            ans += solve(ql, qr - 1, mn, mx, 1);
        } else {
            ans += solve(ql, qr - 1, mn, mx, 0);
        }

        mn = mx + 1;
        ql = qr;
    }

    cout << ans;
}

signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int tt = 1;
    while (tt--) {
        solve();
        cout << "\n";
    }
}