#include "iostream"
#include "algorithm"
#include "vector"
using namespace std;
#define int long long
#define all(arr) arr.begin(), arr.end()
#define _lg(x) (63 - __builtin_clzll(x))
constexpr int maxn = 100'010;
constexpr int mod = 1'000'000'007;
int dp[maxn]{};
int prev_dp[maxn]{};
int lim[maxn]{};
bool have_limit[maxn]{};
constexpr int minlim = -10'000'000'000'000'000ll;
void solve() {
    int n; cin >> n;
    int sum = 0;
    vector<pair<int, int>> input;
    for (int i = 0; i < n; i++) {
        lim[i] = i;
        int a, b; cin >> a >> b;
        input.emplace_back(a, b);
    }
    int minp = 0;
    for (int i = 0; i < n; i++) {
        int p; cin >> p;
        minp = max(minp, p);
        if (p != -1) {
            lim[p - 1] = min(lim[p - 1], i);
            have_limit[p - 1] = true;
        }
    }
    sum = minlim;
    if (!minp) sum = 0;
    for (int i = 0; i < n; i++) {
        int a = input[i].first;
        int b = input[i].second;
        if (!i) {
            prev_dp[0] = a;
            if (minp <= i + 1) sum = max(sum, prev_dp[0]);
            for (int j = 1; j < n; j++) prev_dp[j] = prev_dp[j - 1];
        } else {
            dp[0] = prev_dp[0] + a;
            prev_dp[0] = dp[0];
            for (int j = 1; j <= lim[i]; j++) {
                dp[j] = max(a + b * j + prev_dp[j], dp[j - 1]);
                prev_dp[j] = dp[j];
            }
            if (have_limit[i]) {
                for (int j = 0; j < lim[i]; j++) {
                    dp[j] = prev_dp[j] = minlim;
                }
            }
            for (int j = lim[i] + 1; j < n; j++) {
                prev_dp[j] = prev_dp[j - 1];
            }
            if (minp <= i + 1) sum = max(sum, dp[lim[i]]);
        }
    }
    cout << sum << '\n';
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t = 1; //cin >> t;
    while (t--) {
        solve();
    }
}