#include <bits/stdc++.h>

#define int long long

using namespace std;

signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0);

    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
    vector<int> q(n);
    int mxq = 0;
    for (int i = 0; i < n; i++) {
        cin >> q[i];
        if (q[i] != -1) {
            mxq = i;
        }
    }

    vector<vector<int>> dp(n + 1, vector<int>(n, -1e18));
    dp[0][0] = 0;
    for (int t = 0; t < n; t++) {
        for (int i = t; i < n; i++) {
            dp[i + 1][t] = max(dp[i + 1][t], dp[i][t] + a[i] + b[i] * t);
        }
        if (q[t] != -1) {
            for (int i = 0; i < n; i++) {
                if (i != q[t] - 1) {
                    dp[i + 1][t] = -1e18;
                }
            }
        }
        if (t > 0 && q[t - 1] != -1) {
            dp[q[t - 1]][t] = -1e18;
        }
        if (t > 0)
        for (int i = t; i < n; i++) {
            dp[i][t] = max(dp[i][t], dp[i][t - 1]);
        }
    }

    int ans = -1e18;
    for (int i = 0; i <= n; i++) {
        for (int t = mxq; t < n; t++) {
            ans = max(ans, dp[i][t]);
        }
    }

    cout << ans << "\n";
}
