#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;
using ll = long long;
using ordered_set = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>;
const ll maxn = 1e6 + 5;
const ll MOD = 998244353;
const ll INF = 3e18;

ll mmod(ll x, ll mod = MOD) {
    x = x%MOD;
    if (x < 0) {
        x += MOD;
    }
    return x;
}

ll binpow(ll a, ll n) {
    ll res = 1;
    while (n > 0) {
        if (n&1) {
            res= res*a % MOD;
        }
        a = a*a%MOD;
        n >>=1;
    }
    return res;

}


void solve() {
        ll n;
    cin >> n;
    vector<ll> a(n), b(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
    }
    vector<array<ll,3>> gr;
    gr.push_back({-1, 0, 0});
    for (int i = 0; i < n; ++i) {
        ll p;
        cin >> p;
        if (p != -1) {
            gr.back()[2] =  i + 1;
            gr.push_back({p-1, i + 2, 0});
        }
    }
    if (gr.back()[0] != n-1) {
        gr.back()[2] = n;
        gr.push_back({n-1, n+1, n+2});
    }
    vector<ll> ans(n);
    vector<ll> pref(n);
    ll res = -INF;
    for (int i = 0; i < gr.size()-1; ++i) {
        for (int j = gr[i][0] + 1; j <= gr[i+1][0]; ++j) {
            ans[j] = a[j] + b[j] * gr[i][1];
            if (j != 0) {
                pref[j] = pref[j-1] + ans[j];
            } else {
                pref[j] = ans[j];
            }
            res = max(res, pref[j]);
        }
    }
    cout << res;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    // freopen("C:\\work\\untitled\\in.txt", "r", stdin);
    // freopen("C:\\work\\untitled\\out.txt", "w", stdout);

    int tt = 1;
    //cin >> tt;
    while (tt--) {
        solve();
        cout << '\n';
    }
}