#include <bits/stdc++.h>

#define int long long

using namespace std;
using ll = long long;
using ld = long double;
using sll = __int128;

const int maxn = 5010, inf = 1e18;

int dp[maxn][maxn * 4];
int a[maxn], b[maxn];
int mn[maxn], mx[maxn];

void build(int rw, int ti, int l, int r) {
    if (l == r) {
        dp[rw][ti] = -inf;
        return;
    }
    int lt = ti * 2, rt = lt + 1, m = (l + r) / 2;
    build(rw, lt, l, r);
    build(rw,rt,  m + 1, r);
    dp[rw][ti] = max(dp[rw][lt], dp[rw][rt]);
}

void update(int rw, int ti, int l, int r, int pos, int x) {
    if (l == r) {
        dp[rw][l] = max(dp[rw][l], x);
        return;
    }
    int lt = ti * 2, rt = lt + 1, m = (l + r) / 2;
    if (pos <= m) update(rw, lt, l, m, pos, x);
    else update(rw, rt, m + 1, r, pos, x);
    dp[rw][ti] = max(dp[rw][lt], dp[rw][rt]);
}

int get(int rw, int ti, int l, int r, int ql, int qr) {
    if (qr < l || r < ql) return -inf;
    if (ql <= l && r <= qr) return dp[rw][ti];
    int lt = ti * 2, rt = lt + 1, m = (l + r) / 2;
    return max(get(rw, lt, l, m, ql, qr), get(rw, rt, m + 1, r, ql, qr));
}

void solve() {
    int n;
    cin >> n;
    for (int i = 0; i <= n; i++) build(i, 1, 0, n);
    for (int i = 1; i <= n; i++) {
        cin >> a[i] >> b[i];
    }
    for (int i = 1; i <= n; i++) {
        mx[i] = i - 1;
    }
    vector<int> p(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> p[i];
        for (int j = 1; j <= p[i]; j++) mx[j] = min(mx[j], i - 1);
        if (p[i] != -1) {
            for (int j = p[i] + 1; j <= n; j++) {
                mn[j] = max(mn[j], i);
            }
        }
    }
    update(0, 1, 0, n, 0, 0);
    for (int i = 1; i <= n; i++) {
        for (int t = mn[i]; t <= mx[i]; t++) {
            int tmp = get(i - 1, 1, 0, n, mn[i - 1], mx[i - 1]) + a[i] + b[i] * t;
            update(i, 1, 0, n, t, tmp);
        }
    }
    int ans = -inf;
    for (int i = 1; i <= n; i++) {
        ans = max(ans, get(i, 1, 0, n, 0, n));
    }
    cout << ans;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int test = 1;
    // cin >> test;
    while (test--) {
        solve();
    }
}