#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int N = 5e3 + 11;
const int mod = 998244353;
signed main () {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif // LOCAL
    int n;
    cin >> n;
    vector<int> p(n + 1);
    vector<int> a(n + 1), b(n + 1);
    for (int i = 1; i <= n; ++i) cin >> a[i] >> b[i];
    vector<int> mxval (n + 1), mnval (n + 2);
    bool fl = 0;
    for (int i = 1; i <= n; ++i) mxval[i] = i - 1;
    for (int i = 1; i <= n; ++i) {
        cin >> p[i];
        if (p[i] != -1) fl = 1;
        if (p[i] != -1) mxval[p[i]] = min (mxval[p[i]], i - 1), mnval[p[i] + 1] = min (mnval[p[i] + 1], i);
    }
    for (int i = n - 1; i >= 1; --i) {
        mxval[i] = min (mxval[i + 1], mxval[i]);
    }
    for (int i = 2; i<= n; ++i) {
        mnval[i] = max (mnval[i - 1], mnval[i]);
    }
//    return 0;

    long long ans = -1e18;
    if (!fl) ans = 0;
    vector<long long> dp (n + 1, -1e18);
    dp[0] = 0;
    for (int i = 1; i <= n; ++i) {
        vector<long long> ndp (n + 1, -1e18);
        for (int was = mnval[i - 1]; was <= mxval[i - 1]; ++was) {
            for (int nw = mnval[i]; nw <= mxval[i]; ++ nw) {
                if (was <= nw)
                    ndp[nw] = max (dp[was] + a[i] + 1LL * b[i] * nw, ndp[nw]);
            }
        }

        bool bad = 0;
        for (int j = 1; j <= n; ++j) {
            if (p[j] > i) bad = 1;
        }
        for (int last = 0; last <= n; ++last) dp[last] = ndp[last];
        if (!bad) {
            for (int last = mnval[i]; last <= mxval[i]; ++ last) {
                ans = max (ans, dp[last]);
            }
        }
    }
    cout << ans << '\n';
}

