#include <experimental/random>
#include <bits/stdc++.h>

using namespace std;
//experimental::randint(1, 10);
#define ll long long

ll MOD = 998244353;
ll INF = 1e18;


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ll n;
    cin >> n;
    vector<ll> a(n);
    vector<ll> b(n);
    vector<ll> pref_a(n + 1);
    vector<ll> pref_b(n + 1);
    for (ll i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
        pref_a[i + 1] = pref_a[i] + a[i];
        pref_b[i + 1] = pref_b[i] + b[i];
    }
    vector<ll> p(n);
    ll ls = -1;
    for (ll i = 0; i < n; i++) {
        cin >> p[i];
        if (p[i] != -1) {
            ls = i;
        }
    }
    ll ans = -INF;
    for (ll i = 0; i <= n; i++) {
        ans = max(ans, pref_a[i]);
    }
    cout << ans << endl;
}
