#include <bits/stdc++.h>

#define int long long

using ll = long long;
using ld = long double;
using namespace std;
mt19937 rnd(239);

const int inf = (int)1e18;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);

    int n, otv = -inf, x, y, ok = 1; cin >> n;
    vector <int> a(n), b(n);
    for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
    vector <int> p(n);
    vector <vector <int>> w(n, vector <int> (n, 1));
    for (int i = 0; i < n; i++) {
        cin >> p[i];
    }
    for (int i = 0; i < n; i++) {
        if (p[i] != -1) {
            p[i]--;
            ok = 0;
            for (int ii = 0; ii <= p[i]; ii++) {
                for (int jj = i + 1; jj < n; jj++) {
                    w[ii][jj] = 0;
                }
            }
            for (int ii = p[i] + 1; ii < n; ii++) {
                for (int jj = 0; jj <= i; jj++) {
                    w[ii][jj] = 0;
                }
            }
        }
    }
    if (ok) otv = 0;
    vector <vector <int>> dp(n, vector <int> (n, -inf));
    dp[0][0] = a[0];
    for (int i = 1; i < n; i++) {
        for (int j = 0; j <= i; j++) {
            if (!w[i][j]) continue;
            for (int k = 0; k <= min(j, i - 1); k++) {
                dp[i][j] = max(dp[i][j], dp[i - 1][k] + a[i] + j * b[i]);
            }
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j <= i; j++) {
            if (!w[i][j]) continue;
            otv = max(otv, dp[i][j]);
        }
    }
    cout << otv << "\n";
    return 0;
}
