#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;

struct pair1 {
    ll c;
    int i1;
    int i2;
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    ll n; cin >> n;
    vector<ll> arr1(n);
    vector<ll> arr2(n);
    for (int i = 0; i < n; i++) {
        cin >> arr1[i];
        cin >> arr2[i];
    }
    vector<ll> time(n);
    for (int i = 0; i < n; i++) {
        cin >> time[i];
    }
    int i1 = 0;
    ll ans = 0;
    int tn = 0;
    for (int i = 0; i < n; i++) {
        if (time[i] != -1) {
            // cout << i1 << " " << time[i] - 1 << " " << i << " " << tn << endl;
            int l = i1;
            int r = time[i] - 1;
            //ot tn do i;
            if (r < l) {
                continue;
            }
            ll maxsuf = 0;
            ll ned = 0;
            ll sum = 0;
            for (int j = l; j <= r; j++) {
                sum += arr1[j];
            }
            vector<int> sufsum;
            vector<pair1> sufmax;
            ll sum1 = 0;
            for (int j = r; j >= l; j--) {
                sufsum.push_back(sum1);
                sum1 += arr2[j];
            }
            sufsum.push_back(sum1);
            sum1 = 0;
            int i10 = r + 1;
            for (int j = 0; j < sufsum.size(); j++) {
                if (sum1 < sufsum[i]) {
                    sum1 = sufsum[i];
                    i10 = i;
                }
                sufmax.push_back({sum1, min(i10, i)});
            }
            sum += sufsum[sufsum.size() - 1] * tn;
            int i3 = sufmax.size() - 1;
            while (tn < i) {
                // cout << tn << " " << r;
                sum += sufmax[min((int)sufmax.size() - 1, r - tn)].c * (sufmax[min((int)sufmax.size() - 1, r - tn)].i1 - tn);
                tn = sufmax[min((int)sufmax.size() - 1, r - tn)].i1;
            }
            i1 = r + 1;
            ans += sum;
        }
    }
    i1 = max(tn, i1);
    ll sumnow = 0;
    ll summ = 0;
    ll sum3 = 0;
    ll maxsumm = 0;
    int s = 0;
    vector<pair1> ned;
    vector<int> sums;
    // sums.push_back(0);
    for (int i = i1; i < n; i++) {
        summ += arr1[i];
        sum3 += arr2[i];
        while (!ned.empty()) {
            if (ned[ned.size() - 1].c + arr2[i] > 0) {
                break;
            } else {
                sumnow -= ned[ned.size() - 1].c * (ned[ned.size() - 1].i1);
                if (ned.size() > 1) {
                    ned[ned.size() - 2].c += ned[ned.size() - 1].c;
                }
                ned.pop_back();
            }
        }
        if (arr2[i] > 0) {
            if (ned.size() == 0) {
                ned.push_back({0, i - tn, i});
            } else {
                ned.push_back({0, i - ned[ned.size() - 1].i2, i});
            }
        }
        if (ned.size() > 0) {
            sumnow += (ned[ned.size() - 1].i2 - tn) * arr2[i];
            ned[ned.size() - 1].c += arr2[i];
        } else {
            sumnow = 0;
        }
        // sumnow = 0;
        // for (auto& el : ned) {
        //     el.c = el.c + arr2[i];
        //     sumnow += el.c * (el.i1 - tk);
        //     tk = el.i1;
        // }
        maxsumm = max(maxsumm, summ + sumnow + sum3 * tn);
    }
    cout << ans + maxsumm;
}