#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const ll mod = 998244353;
const ll inf = 1e17;

vector<vector<ll>> G;
vector<ll> sz;
void dfs(ll v, ll p) {
    for (ll u : G[v]) {
        if (u == p) continue;
        dfs(u, v);
        sz[v] += sz[u];
    }
}

ll binpow(ll a, ll b) {
    if (!b) return 1;
    if (b % 2 == 1) {
        return a * binpow(a, b - 1) % mod;
    }
    ll x = binpow(a, b / 2);
    return x * x % mod;
}

void solve() {
   ll n; cin >> n;
    vector<ll> A(n + 1), B(n + 1);
    for (ll i = 1; i <= n; ++i) cin >> A[i] >> B[i];
    vector<ll> P(n + 1);
    vector<ll> mn(n + 1, 0);
    ll idx = -1;
    for (ll i = 1; i <= n; ++i) {
        cin >> P[i];
        if (P[i] != -1) {
            idx = P[i];
            if (P[i] != n) mn[P[i] + 1] = i;
        }
    }
    for (ll i = 1; i <= n; ++i) mn[i] = max(mn[i - 1], mn[i]);
    ll tec = 0;
    ll ans = 0;
    if (idx != -1) ans = -inf;
    for (ll i = 1; i <= n; ++i) {
        tec += A[i] + mn[i] * B[i];
        if (i >= idx) ans = max(ans, tec);
    }
    cout << ans << '\n';
    // ll n, k; cin >> n >> k;
    // vector<ll> X(k);
    // for (ll i = 0; i < k; ++i) cin >> X[i];
    // G.assign(n + 1, {});
    // vector<ll> P(n + 1, 0);
    // for (ll i = 2; i <= n; ++i) {
    //     ll p; cin >> p;
    //     P[i] = p;
    //     G[p].push_back(i);
    //     G[i].push_back(p);
    // }
    // sort(X.begin(), X.end());
    // ll ans = 1;
    // ll tec = k - 1;
    // ll cnt = k + 1;
    // for (ll i = n; i >= 1; --i) {
    //     if (tec >= 0 && X[tec] == i) {
    //         cnt--;
    //         tec--;
    //         continue;
    //     }
    //     ans = (ans * cnt) % mod;
    // }
    // cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
}