#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

void solve() {
    int n;
    cin >> n;
    vector <pair <ll, ll>> a(n);
    for (auto &[x, y] : a) {
        cin >> x >> y;
    }
    vector <int> p(n);
    for (auto &e : p) {
        cin >> e;
    }
    vector <int> nxt(n + 1, -1);
    for (int i = n - 2; i >= 0; i--) {
        if (p[i] == -1) {
            nxt[i] = nxt[i + 1];
        } else {
            nxt[i] = i;
        }
    }
    if (nxt[0] == -1) {
        ll mx = 0;
        ll sum = 0;
        for (int i = 0; i < n; i++) {
            sum += a[i].first;
            mx = max(mx, sum);
        }
        cout << mx;
        return;
    }
    ll time = 0;
    int i = 0;
    ll ans = 0;
    while (nxt[i] != -1) {
        for (int k = i; k < p[nxt[i]]; k++) {
            ans += a[k].first + time * a[k].second;
        }
        time = nxt[i] + 2;
        i = p[nxt[i]] + 1;
    }
    ll mx = 0, sum = 0;
    for (int k = i; k < n; k++) {
        sum += a[k].first + time * a[k].second;
        mx = max(mx, sum);
    }
    cout << ans + mx;
}

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

// Halal code
