#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define all(a) a.begin(), a.end()

void solve();

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll t = 1;
#ifdef LOCAL
    cin >> t;
#else
#endif
    while (t--) solve();
}

const ll INF = 1e18;

void solve() {
    ll n;
    cin >> n;
    vector<ll> a(n), b(n), p(n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    for (ll i = 0; i < n; i++) cin >> p[i];
    vector<ll> dp(n + 1, -INF);
    dp[0] = 0;
    for (ll i = 0; i < n; i++) {
        for (ll j = i; j < n; j++) {
            if (dp[j + 1] < dp[j] + (a[j] + b[j] * i))
                dp[j + 1] = dp[j] + (a[j] + b[j] * i);
        }
        if (p[i] != -1) {
            ll r = dp[p[i]];
            fill(all(dp), -INF);
            dp[p[i]] = r;
        }
    }
    cout << *max_element(all(dp)) << "\n";
}
