#include <bits/stdc++.h>
#define int long long
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define em emplace_back

using namespace std;
template<class C>
using vec = vector<C>;
using vi = vector<int>;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

vi buildL(vi a) {
    int n = a.size();
    vi st;
    vi f(n, -1);
    for (int i = 0; i < n; i++) {
        while (!st.empty() && a[st.back()] > a[i]) {
            st.pop_back();
        }
        if (!st.empty())
            f[i] = st.back();
        st.pb(i);
    }
    return f;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
#ifdef LOCAL
    freopen("input", "r", stdin);
#endif
    int n;
    cin >> n;
    vi a(n), b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    vi p(n);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        assert(p[i] == -1);
    }
    vi pref = b;
    for (int i = 1; i < n; i++) {
        pref[i] += pref[i - 1];
    }
    auto L = buildL(pref);
    vi dp(n);
    for (int i = 0; i < n; i++) {
        if (pref[i] >= 0 || L[i] != -1) {
            int left = -1;
            if (L[i] != -1)
                left = L[i] + 1;
            else
                left = 0;
            int sm = pref[i] - (left ? pref[left - 1] : 0);
            dp[i] = (left ? dp[left - 1] : 0) + sm * left;
        } else {
            dp[i] = 0;
        }
    }
    int ans = 0;
    int suma = 0;
    for (int i = 0; i < n; i++) {
        suma += a[i];
        ans = max(ans, suma + dp[i]);
    }
    cout << ans << '\n';
}