#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];
    }
    vector<int> pa(n + 1, 0);
    vector<int> pb(n + 1, 0);
    int y = 0;
    vector<int> pref(n + 1, 0);
    for (int i = 1; i <= n; i++) {
        pref[i] = pref[i - 1] + a[i];
    }
    int f = 0, in = 0;
    vector<int> d(n + 1, 0);
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        if (x != -1) {
            y = x;
            for (int h = f + 1; h <= x; h++) {
                d[h] = in;
            }
            f = x;
            in = i;
        }
    }
    for (int i = f + 1; i <= n; i++) {
        d[i] = in;
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        ans = (ans + d[i] * b[i] + a[i]);
    }
    int u = 0;
    int w = 0;
    for (int i = n; i > y; i--) {
        u = (u + d[i] * b[i] + a[i]);
        w = min(w, u);
    }
    cout << ans - w;
}

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