#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];
int a[maxn], b[maxn];
int mn[maxn], mx[maxn];

void solve() {
    int n;
    cin >> n;
    for (int i = 0; i <= n; i++) {
        fill(dp[i], dp[i] + n + 1, -inf);
    }
    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);
            }
        }
    }
    dp[0][0] = 0;
    int ans = -inf;
    for (int i = 1; i <= n; i++) {
        for (int t = mn[i]; t <= mx[i]; t++) {
            int tmp = -inf;
            for (int j = mn[i - 1]; j <= mx[i - 1]; j++) {
                tmp = max(tmp, dp[i - 1][j]);
            }
            dp[i][t] = tmp + a[i] + b[i] * t;
            ans = max(ans, dp[i][t]);
        }
    }
    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();
    }
}