#include <bits/stdc++.h>

using namespace std;

#define int long long

signed main() {
    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> a(n), b(n), p(n);
    for (int i = 0; i < n; ++i) cin >> a[i] >> b[i];
    for (int i = 0; i < n; ++i) cin >> p[i];
    vector<int> t(n);
    for (int i = 0; i < n; ++i) t[i] = i;
    for (int i = 0; i < n; ++i) {
        if (p[i] != -1) t[p[i] - 1] = i + 1;
    }
    for (int i = n - 2; i >= 0; --i) t[i] = min(t[i], t[i + 1]);
    // for (int i : t) cout << i << ' ';
    // cout << endl;
    int ans = 0;
    int sm = 0;
    for (int i = 0; i < n; ++i) {
        sm += a[i] + t[i] * b[i];
        ans = max(ans, sm);
    }
    cout << ans;
}