#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif // LOCAL

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using pii = pair<int, int>;

constexpr ll INF = 2'000'000'000'000'000'000;

constexpr int N = 1'000'000, LOG = 20;
int a[N], b[N], tL[N], tR[N];
ll zS[2 << LOG], dS[2 << LOG];

void add(int v, int vl, int vr, int l, ll z, int d) {
    if (vr <= l) {
        return;
    }
    if (l <= vl) {
        zS[v] += z + d * (ll)(vl - l);
        dS[v] += d;
        return;
    }
    int vm = (vl + vr) / 2;
    add(v * 2, vl, vm, l, z, d);
    add(v * 2 + 1, vm, vr, l, z, d);
}

void del(int v, int vl, int vr, int l, ll z, ll d) {
    if (vr <= l) {
        return;
    }
    z += zS[v] + dS[v] * (l - vl);
    d += dS[v];
    if (l <= vl) {
        zS[v] -= z + d * (vl - l);
        dS[v] -= d;
        return;
    }
    int vm = (vl + vr) / 2;
    del(v * 2, vl, vm, l, z, d);
    del(v * 2 + 1, vm, vr, l, z, d);
}

ll get(int v, int vl, int vr, int i, ll z) {
    z += zS[v] + dS[v] * (i - vl);
    if (vl + 1 == vr) {
        return z;
    }
    int vm = (vl + vr) / 2;
    if (i < vm) {
        return get(v * 2, vl, vm, i, z);
    }
    return get(v * 2 + 1, vm, vr, i, z);
}

void smart_solve() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
        tR[i] = i;
    }
    int z = 0;
    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        if (x == -1) {
            continue;
        }
        if (x < n) {
            tL[x] = i + 1;
        }
        z = --x;
        tR[x] = min(tR[x], i);
    }

    for (int i = 1; i < n; ++i) {
        tL[i] = max(tL[i], tL[i - 1]);
    }
    for (int i = n - 2; i >= 0; --i) {
        tR[i] = min(tR[i], tR[i + 1]);
    }

    ll ans = -INF * (z > 0);
    for (int i = 0; i < n; ++i) {
        add(1, 0, n, tL[i], a[i] + b[i] * (ll)tL[i], b[i]);
        add(1, 0, n, tR[i] + 1, -a[i] - b[i] * (ll)(tR[i] + 1), -b[i]);
        int l = tL[i] - 1, r = tR[i];
        while (l + 1 < r) {
            int mid = (l + r) / 2;
            (get(1, 0, n, mid, 0) < get(1, 0, n, mid + 1, 0) ? l : r) = mid;
        }
        del(1, 0, n, r + 1, 0, 0);
        ll res = get(1, 0, n, r, 0);
        add(1, 0, n, r + 1, res, 0);
        if (z <= i) {
            ans = max(ans, res);
        }
    }
    cout << ans << "\n";
}

int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif // LOCAL
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int times = 1;
//    cin >> times;
    while (times--) {
        smart_solve();
    }
    return 0;
}
