#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 1e18;
signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    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 lsti = -1;
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        if (p[i] != -1) lsti = i;
    }
    int ans = -inf;
    vector<vector<int>> dp(2, vector<int>(n + 1, -inf));
    int cur_sm = 0;
    dp[0][0] = cur_sm;
    for (int i = 0; i < n; i++) {
        cur_sm += a[i];
        dp[0][i + 1] = cur_sm;
        if (lsti == -1 or (i >= lsti and i + 1 >= p[lsti])) {
            ans = max(ans, cur_sm);
        }
    }
    for (int i = 1; i < n; i++) {
        for (int j = 0; j <= n; j++) {
            if (p[i - 1] == -1 or p[i - 1] == j) {
                dp[i % 2][j] = dp[(i + 1) % 2][j];
            }else {
                dp[i % 2][j] = -inf;
            }
        }
        vector<int> prefs(n + 1);
        for (int j = 1; j <= n; j++) {
            prefs[j] = prefs[j - 1] + a[j - 1] + b[j - 1] * i;
        }
        int tmp = -inf;
        for (int j = i + 1; j <= n; j++) {
            int add = dp[(i - 1) % 2][j - 1];
            int mins = -prefs[j- 1];
            if (p[i - 1] == -1 or p[i - 1] == j - 1) {
                tmp = max(tmp, add + mins);
            }
            int mxv = prefs[j] + tmp;
            dp[i % 2][j] = max(dp[i % 2][j], mxv);
            if (lsti == -1 or (i >= lsti and j >= p[lsti])) {
                ans = max(ans, dp[i % 2][j]);
            }
        }
    }
    if (lsti == -1) ans = max(ans, 0ll);
    cout << ans << '\n';
}