#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
const ll inf = 1e18;

void solve() {
    int n;
    cin >> n;
    vector<int> a(n + 1), b(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> a[i] >> b[i];
    }
    vector<int> p(n + 1);
    vector<int> mi(n + 1), ma(n + 1,inf);
    for (int i = 1; i <= n; i++) {
        cin >> p[i];
        ma[i] = min(ma[i], i - 1);
        if (p[i] == -1) {
            continue;
        }
        ma[p[i]] = i - 1;
        mi[p[i] + 1] = i;
    }
    for (int i = 1; i <= n; i++) {
        mi[i] = max(mi[i - 1], mi[i]);
    }
    for (int i = n - 1; i >= 1; i--) {
        ma[i] = min(ma[i], ma[i + 1]);
    }
    vector<vector<int> > dp(n + 1, vector<int>(n + 1, -inf));
    dp[0][0] = 0;
    int ans = -inf;
    for (int i = 1; i <= n; i++) {
        for (int t = mi[i]; t <= ma[i]; t++) {
            int add = a[i] + b[i] * t;
            for (int pt = mi[i - 1]; pt <= t; pt++) {
                dp[i][t] = max(dp[i][t], dp[i - 1][pt] + add);
            }
            ans = max(ans, dp[i][t]);
        }
    }
    cout << ans;
}

signed main() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    int t = 1;
    // cin >> t;
    for (int ts = 1; ts <= t; ts++) {
        solve();
    }
}
