#include<bits/stdc++.h>

using namespace std;

using ll = long long;

#define f first
#define s second
#define pii pair<ll, ll>

ll const INF = 1e16;


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n;
    cin >> n;
    vector<pii> a(n);
    for (ll i = 0; i < n; ++i) {
        cin >> a[i].f >> a[i].s;
    }
    vector<ll> p(n), t(n);
    iota(t.begin(), t.end(), 0);
    for (ll i = 0; i < n; ++i) {
        cin >> p[i];
        if (p[i] != -1) {
            t[p[i] - 1] = min(t[p[i] - 1], i);
        }
    }
    for (ll i = n - 2; i >= 0; --i) {
        t[i] = min(t[i + 1], t[i]);
    }
    ll cur = 0, ans = -INF;
    for (int i = 0; i < n; ++i) {
        cur += a[i].f + t[i] * a[i].s;
        ans = max(ans, cur);
    }
    cout << ans;
////    for (auto el : t) {
////        cout << el << ' ';
////    }
////    cout << endl;
//    vector<vector<ll>> dp(n, vector<ll> (n, -INF));
//    vector<vector<ll>> mx(n, vector<ll> (n, -INF));
//    dp[0][0] = a[0].f;
//    //mx[0][0] = dp[0][0];
//    for (ll i = 1; i < n; ++i) {
//        for (ll j = 0; j <= t[i]; ++j) {
//            //ll m = mx[i - 1][j];
//            //dp[i][j] = m + a[i].f + j * a[i].s;
////            mx[i][j] = dp[i][j];
////            if (j != 0) {
////                mx[i][j] = max(mx[i][j], mx[i][j - 1]);
////            }
//            for (ll j1 = 0; j1 <= j; ++j1) {
//                if (dp[i - 1][j1] == -INF) continue;
//                dp[i][j] = max(dp[i][j], dp[i - 1][j1] + a[i].f + j * a[i].s);
//            }
//        }
//    }
//    ll ans = a[0].f;
//    for (auto el : dp) {
//        for (auto e : el) {
//            ans = max(ans, e);
//        }
//    }
//    cout << ans;

}

