#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;

//@formatter:off
template <typename T> istream& operator>>(istream& in, vector<T> &vec) { for (auto &i : vec) in >> i; return in; }
template <typename T> ostream& operator<<(ostream& out, vector<T> &vec) { for (auto &i : vec)  out << i << ' '; out << '\n'; return out; }
//@formatter:on
#define int ll



void solve() {
    int n;
    cin >> n;
    vector<ll> a(n + 1), b(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> a[i] >> b[i];
    }
    vector<int> p(n);
    cin >> p;
    p.insert(p.begin(), 0);
    const ll INF = 1e18;
    
    ll pref = 0, ans = -INF;
    for (int i = 1; i <= n; i++) {
        pref += a[i];
        ans = max(ans, pref);
    }
    cout << ans << '\n';
}

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    // cin >> t;
    while (t--) solve();
}