#include <bits/stdc++.h>
using namespace std;
#define int int64_t

vector<int> tree;
vector<int> p;

void build(int v, int l, int r) {
    if (l + 1 == r) {
        tree[v] = l - 1;
        return;
    }
    int m = (l + r) / 2;
    build(2 * v + 1, l, m);
    build(2 * v + 2, m, r);
    tree[v] = max(tree[2 * v + 1], tree[2 * v + 2]);
}

void push(int v) {
    int x = p[v];
    if (x == -1) {
        return;
    }
    if (tree[2 * v + 1] > x) {
        tree[2 * v + 1] = x;
        p[2 * v + 1] = x;
    }
    if (tree[2 * v + 2] > x) {
        tree[2 * v + 2] = x;
        p[2 * v + 2] = x;
    }
    p[v] = -1;
}

void update(int v, int l, int r, int ql, int qr, int x) {
    if (ql >= r || l >= qr) {
        return;
    }
    if (ql <= l && r <= qr) {
        if (tree[v] > x) {
            tree[v] = x;
            p[v] = x;
        }
        return;
    }
    push(v);
    int m = (l + r) / 2;
    update(2 * v + 1, l, m, ql, qr, x);
    update(2 * v + 2, m, r, ql, qr, x);
    tree[v] = max(tree[2 * v + 1], tree[2 * v + 2]);
}

int get(int v, int l, int r, int ql, int qr) {
    if (ql >= r || l >= qr) {
        return -1;
    }
    if (ql <= l && r <= qr) {
        return tree[v];
    }
    push(v);
    int m = (l + r) / 2;
    return max(get(2 * v + 1, l, m, ql, qr), get(2 * v + 2,m, r, ql, qr));
}

void solve() {
    int n;
    cin >> n;
    vector<int> a(n + 1);
    vector<int> b(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> a[i] >> b[i];
    }
    tree = vector<int> (4 * n + 100, -1);
    p = vector<int> (4 * n + 100, -1);
    build(0, 1, n + 1);
    int y = 0;
    vector<int> dp(n + 1, 0);
    vector<int> down(n + 1, 0);
    int in1 = 0, ink = 0;
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        if (x != -1) {
            for (int j = ink + 1; j <= x; j++) {
                down[j] = in1;
            }
            ink = x;
            in1 = i;
            y = max(y, x);
            update(0, 1, n + 1, 1, x + 1, i - 1);
        }
    }
    vector<int> d(n + 1, 0);
    for (int i = 1; i <= n; i++) {
        d[i] = get(0, 1, n + 1, i, i + 1);
    }
    int ans = 0;
    vector<int> kol(n + 1, 0);
    set<int> st;
    for (int i = 1; i <= n; i++) {
        dp[d[i]] += b[i];
        kol[d[i]]++;
        st.insert(d[i]);
        ans += a[i];
        ans += (b[i] * d[i]);
        int u = d[i];
        while (u > down[i] && dp[u] < 0) {
            auto it = st.end();
            it--;
            st.erase(it);
            it = st.end();
            it--;
            int h = max((*it), down[i]);
            kol[h] += kol[u];
            kol[u] = 0;
            ans -= (dp[u] * (u - h));
            dp[h] += dp[u];
            dp[u] = 0;
            st.insert(h);
            u = h;
        }
    }
    vector<int> h(n + 1, 0);
    int f = 0;
    for (int i = 0; i < n; i++) {
        for (int j = f + 1; j <= f + kol[i]; j++) {
            h[j] = i;
        }
        f = f + kol[i];
    }
    int w = 0, u = 0;
    for (int i = n; i > y; i--) {
        u += (a[i] + h[i] * b[i]);
        w = min(w, u);
    }
    cout << ans - w;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}