#include <bits/stdc++.h>

using namespace std;

#define int long long

signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
    int la = 0, lat = 0;
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        if (x == -1) continue;
        vector<pair<int, pair<int, int>>> st;
        int cut = lat;
        while (la < x) {
            long long sub = 0, su = 0;
            sub += b[la];
            su += b[la] * cut;
            if (sub >= 0) {
                st.push_back({min(la, i - 1), {b[la] * min(la, i - 1), sub}});
                cut = min(la, i - 1);
                la++;
                continue;
            }
            while (!st.empty() and su <= 0) {
                su -= sub * (cut - st.back().first);
                cut = st.back().first;
                su += st.back().second.first;
                sub += st.back().second.second;
                st.pop_back();
            }
            st.push_back({cut, {su, sub}});
            la++;
        }
        for (auto [x, y] : st) {
            ans += y.first;
        }
        lat = i;
    }
    for (int i = 0; i < la; i++) ans += a[i];
    vector<pair<int, pair<int, int>>> st;
    int cut = lat;
    int res = 0, mares = 0, sua = 0;
    while (la < n) {
        long long sub = 0, su = 0;
        sua += a[la];
        sub += b[la];
        su += b[la] * cut;
        if (sub >= 0) {
            st.push_back({la, {b[la] * la, sub}});
            res += b[la] * la;
            mares = max(mares, res + sua);
            cut = la;
            la++;
            continue;
        }
        while (!st.empty() and su <= 0) {
            su -= sub * (cut - st.back().first);
            res -= st.back().second.first;
            cut = st.back().first;
            su += st.back().second.first;
            sub += st.back().second.second;
            st.pop_back();
        }
        st.push_back({cut, {su, sub}});
        res += su;
        mares = max(mares, res + sua);
        la++;
    }
    ans += mares;
    cout << ans;
}