#include<bits/stdc++.h>

using namespace std;

using ll = long long;

#define f first
#define s second
#define pii pair<ll, ll>

ll const INF = 1e17;


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector<pii> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i].f >> a[i].s;
    }
    vector<int> p(n), t(n);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        t[i] = min(i, t[i]);
        if (p[i] != -1) {
            t[p[i] - 1] = i;
        }
    }
    for (int i = n - 2; i >= 0; --i) {
        t[i] = min(t[i], t[i + 1]);
    }
    vector<vector<ll>> dp(n, vector<ll> (n + 1, -INF));
    dp[0][0] = a[0].f;
    ll ans = dp[0][0];
    for (int i = 1; i < n; ++i) {
        for (int j = 0; j <= t[i]; ++j) {
            for (int j1 = 0; j1 <= j; ++j1) {
                if (dp[i - 1][j1] == -INF) continue;
                dp[i][j] = max(dp[i][j], dp[i - 1][j1] + a[i].f + a[i].s * j);
                ans = max(ans, dp[i][j]);
            }
        }
    }
    cout << ans;
}

