#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);

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

    vector<pair<int, int>> a(n);
    fori(0, n) cin >> a[i].first >> a[i].second;

    vector<int> p(n, -1), tmx(n);

    int must = 0;
    fori(0, n) {
        cin >> p[i];

        tmx[i] = i;

        must = max(must, p[i]);
    }

    fori(0, n) {
        if (p[i] != -1) {
            tmx[p[i] - 1] = i;
        }
    }

    for (int i = n - 2; i >= 0; --i) {
        tmx[i] = min(tmx[i], tmx[i + 1]);
    }

    map<int, ll> dp, ndp;
    dp[0] = 0;

    ll ans = LLONG_MIN;

    if (!must) ans = 0;

    fori(0, n) {
        ndp.clear();

        auto add = [&](int t, ll wt) {
            if (i + 1 >= must) ans = max(ans, wt);

            if (ndp.find(t) == ndp.end()) {
                ndp[t] = wt;
            } else {
                ndp[t] = max(ndp[t], wt);
            }
        };

        for (auto [t, wt] : dp) {
            for (int t1 = t; t1 <= tmx[i]; ++t1) {
                if (p[t1] != -1 && p[t1] < i + 1) continue;

                add(t1, wt + a[i].first + 1ll * t1 * a[i].second);
            }
        }

        swap(dp, ndp);
    }

    cout << ans;
}

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

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