#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()

vector<int> sol(vector<int>a ) {
    int n = sz(a);
    vector<int> used(n),ans(n);
    int lst = -1;
    for (int j = 0; j < n; j++) {
        bool f = false;
        for (int i = 0; i < n; i++) {
            if (a[i]!=lst&&!used[i]) {
                lst=a[i];
                ans[i] = j+1;
                used[i]=1;
                f=1;
                break;
            }
        }
        if (!f) {
            for (int i = 0; i< n;i++) {
                if (!used[i]) {
                    ans[i] = j + 1;
                    used[i]=1;break;
                }
            }
        }
    }
    return ans;
}

vector<int> kek(vector<int> a){
    vector<int>ans(sz(a));
    for (int j = 0; j < sz(a);j ++) ans[a[j] - 1] = j;
    return ans;
}

const ll inf = 1e18;

void solve() {
    int n; cin >> n;
    vector<int> a(n), b(n);
    for(int i = 0; i < n; i++) cin >> a[i] >> b[i];
    vector<int> p(n);
    for (auto&u: p) cin >> u;
    ll ans = 0;
    vector<ll> dlt;
    ll sma =0 ;
    for (int j = 0; j < n; j++) {
        sma += a[j];
        for (auto &u : dlt) u += b[j];
        dlt.pb(0);
        int ptr = sz(dlt) - 1;

        // for (auto u: dlt) cout << u << ' ';
        // cout<< endl;
        while(ptr >= 0 && dlt[ptr] < 0) {
            dlt[ptr] = 0;
            ptr--;
        }
        // cout<< endl;
        ans = max(ans, accumulate(all(dlt), 0ll) + sma);
    }
    cout << ans << '\n';
}

signed main() {
    int t = 1;
    #ifdef LOCAL
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
        // cin >> t;
    #else 
        cin.tie(0)->sync_with_stdio(false);
    #endif 

    while (t--) solve();

    return 0;
}