#include <bits/stdc++.h>
#define int long long
#define all(x) x.begin(), x.end()
#define pb push_back
#define rall(x) x.rbegin(), x.rend()
using namespace std;
using ll = long long;
using ld = long double;

void solve() {
    int n;
    cin >> n;
    vector<ll> a(n), b(n);
    for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
    vector<ll> p(n);
    for (auto &el : p) cin >> el;
    vector<pair<int, int>> g;
    for (int i = 0; i < n; i++) {
        if (p[i] != -1) g.pb({p[i], i + 1});
    }
    vector<vector<ll>> dp(n + 1, vector<ll> (n + 1, -1e18));
    vector<ll> pref{0};
    dp[0][0] = 0;
    for (int i = 1; i < n; i++) pref.pb(max(pref.back(), dp[0][i]));
    for (int i = 1; i <= n; i++) {
        int rr = i;
        int lc = 0;
        if (!g.empty() && lower_bound(all(g), make_pair(i, -1)) != g.end()) {
            auto [xx, yy] = *lower_bound(all(g), make_pair(i, -1));
            rr = min(rr, yy);
        }
        if (!g.empty() && lower_bound(all(g), make_pair(i, -1)) != g.begin()) {
            auto it = lower_bound(all(g), make_pair(i, -1));
            it--;
            auto [xx, yy] = *it;
            lc = max(lc, yy);
        }
        for (int j = lc; j < rr; j++) {
            int l = 0, r = i - 2;
            r = min(r, j);
            if (!g.empty() && lower_bound(all(g), make_pair(i - 1, -1)) != g.end()) {
                auto [x, y] = *lower_bound(all(g), make_pair(i - 1, -1));
                r = min(r, y - 1);
            }
            if (!g.empty() && lower_bound(all(g), make_pair(i - 1, -1)) != g.begin()) {
                auto it = lower_bound(all(g), make_pair(i - 1, -1));
                it--;
                auto [x, y] = *it;
                l = max(l, y);
            }
            if (r < l) dp[i][j] = a[i - 1] + (ll) b[i - 1] * j;
            else {
                for (int k = l; k <= r; k++) {
                    dp[i][j] = max(dp[i][j], dp[i - 1][k]);
                }
                dp[i][j] += a[i - 1] + (ll) b[i - 1] * j;
            }
        }
    }
    ll ans = -1e18;
    int l = 0;
    if (!g.empty()) {
        l = max(l, g.back().first);
    }
    for (int i = l; i <= n; i++) {
        ans = max(ans, *max_element(all(dp[i])));
    }
    cout << ans;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    //cin >> t;
    while (t--) solve();
}