#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int64_t n;
    cin >> n;
    vector<int64_t> a(n);
    vector<int64_t> b(n);
    for(int64_t i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
    }
    int64_t mx = 0;
    vector<int64_t> p(n);
    for(int64_t i = 0; i < n; ++i) {
        cin >> p[i];
        mx = max(mx, p[i]);
    }
    int64_t cur = 0;
    int64_t res = -1e18;
    for(int64_t i = 0; i < n; ++i) {
        cur += a[i];
        if(i >= mx) {
            res = max(res,cur);
        }
    }
    cout << res << '\n';
    return 0;
}