#include <bits/stdc++.h>

using namespace std;
#define int long long

void bad() {
    int n;
    cin >> n;
    vector <int> a(n), b(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
    }
    vector <int> p(n);
    int t = -1;
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        if (p[i] != - 1) {
            t = i;
        }
    }
    vector <int> last(n + 1, - 2e15), now(n + 1, -2e15);
    last[0] = 0;
    int res = 0;
    int ans = -2e18;
    if (t == - 1) {
        ans = 0;
    }
    for (int i = 0; i < n; ++i) {
        for (int j = i; j <= n; ++j) {
            now[j] = last[j];
        }
        for (int j = i; j < n; ++j) {
            now[j + 1] = max(now[j + 1], now[j] + i * b[j] + a[j]);
        }
        if (p[i] != - 1) {
            for (int j = 0; j <= n; ++j) {
                if (j != p[i]) {
                    now[j] = -2e15;
                }
            }
        }
        swap(last, now);
        fill(now.begin(), now.end(), -2e15);
        if (i >= t) {
            for (int j = 0; j <= n; ++j) {
                ans = max(ans, last[j]);
            }
        }
    }
    cout << ans << '\n';
}
signed main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    bad();
}
