#include<bits/stdc++.h>
#define int long long
using namespace std;

const int INF = 1e18;

void solve() {
    int n, ans = -INF;
    cin >> n;
    vector<int> a(n + 1), b = a, p = a;
    for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];
    for (int i = 1; i <= n; i++) cin >> p[i];
    for (int t = n; t >= 0; t--) {
        if (p[t] == -1) continue;
        int s = 0;
        for (int i = 0; i < p[t]; i++) s += a[i];
        for (int i = p[t]; i <= n; i++) {
            s += a[i];
            ans = max(ans, s);
        }
        break;
    }
    cout << ans;
}

int32_t main() {
    cin.tie(0);
    ios::sync_with_stdio(0);

    solve();
}
