#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

using ll = long long;
#define int long long
const int MOD = 998244353;
const int INF = 5e18;

using ordered_set = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>;

void solve1();

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int qwerty = 1;
    // cin >> qwerty;
    while (qwerty--) {
        solve1();
    }
}

void solve1() {
    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);
    for (int i = 1; i <= n; i++) {
        cin >> p[i];
    }
    vector<int> mn_time(n + 1), mx_time(n + 1);
    for (int i = 1; i <= n; i++) {
        mx_time[i] = i - 1;
    }
    bool flag = true;
    int mxx = 0;
    for (int i = 1; i <= n; i++) {
        if (p[i] == -1) continue;
        mxx = max(mxx, p[i]);
        flag = false;
        mx_time[p[i]] = min(mx_time[p[i]], i - 1);
        if (p[i] == n) continue;
        mn_time[p[i] + 1] = max(mn_time[p[i] + 1], i);
    }
    for (int i = 1; i <= n; i++) {
        mn_time[i] = max(mn_time[i], mn_time[i - 1]);
    }
    for (int i = n - 1; i >= 1; i--) {
        mx_time[i] = min(mx_time[i], mx_time[i + 1]);
    }
    vector<int> dp(n + 1);
    int ans = -INF;
    if (flag) ans = 0;
    for (int i = 1; i <= n; i++) {
        vector<int> dp1(n, -INF);
        int mx = -INF;
        for (int j = 0; j < mn_time[i]; j++) {
            mx = max(mx, dp[j]);
        }
        for (int j = mn_time[i]; j <= mx_time[i]; j++) {
            mx = max(mx, dp[j]);
            dp1[j] = mx + a[i] + j * b[i];
            if (i >= mxx) ans = max(ans, dp1[j]);
        }
        swap(dp1, dp);
    }
    cout << ans;
}
