#include <bits/stdc++.h>

using namespace std;
#define int long long

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (int i = 0; i < n; i++)
        cin >> a[i] >> b[i];
    vector<int> h(n), mn(n);
    for (int i = 0; i < n; i++) h[i] = i, mn[i] = 0;
    for (int i = 0; i < n; i++) {
        int p;
        cin >> p;
        if (p != -1) {
            p--;
            for (int j = p; j >= 0; j--)
                h[j] = min(h[j], i);
            for (int j = p + 1; j < n; j++)
                mn[j] = max(mn[j], i + 1);
        }
    }
    vector<vector<int> > dp(n, vector<int>(n + 1, -(int) 1e17));
    int ans = -(int) 1e17;
    for (int j = mn[0]; j <= h[0]; j++) {
        dp[0][j] = max((j > 0 ? dp[0][j - 1] : -(int) 1e17), a[0] + j * b[0]);
    }
    for (int x = 1; x <= n; x++)
        dp[0][x] = max(dp[0][x], dp[0][x - 1]);
    for (int i = 1; i < n; i++) {
        for (int x = mn[i]; x <= h[i]; x++) {
            dp[i][x] = (a[i] + x * b[i]) + dp[i - 1][x];
        }
        for (int x = 1; x <= n; x++)
            dp[i][x] = max(dp[i][x], dp[i][x - 1]);
    }
    for (int i = 0; i < n; i++)
        for (int k = 0; k <= n; k++)
            ans = max(ans, dp[i][k]);
    cout << ans;
    return 0;
}
