#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
#define int long long
using ll = long long;
using namespace std;
// #pragma GCC optimize("O3, unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
//  using ordered_set = tree<int, null_type, less<int>, rb_tree_tag,
//  tree_order_statistics_node_update>;

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    // freopen("in", "r", stdin);
    // freopen("out", "w", stdout);

    int n;
    cin >> n;
    vector<int> a(n);
    vector<int> b(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
    }
    vector<int> p(n);
    for (auto& x : p) cin >> x;
    vector<vector<int>> dp(n + 1, vector<int>(n + 1, -1e18));
    vector<int> rest(n + 1, -1);
    for (int i = 0; i < n; ++i) {
        if (p[i] != -1) {
            rest[p[i]] = i + 1;
        }
    }
    dp[0][0] = 0;
    int maxn = -1e15;
    for (int i = 1; i <= n; ++i) {
        int curmax = dp[i - 1][0];
        int tmp = i;
        if (rest[i] != -1)
            tmp = rest[i];
        for (int k = 0; k < tmp; ++k) {
            curmax = max(curmax, dp[i - 1][k]);
            dp[i][k] = max(dp[i][k], curmax + a[i - 1] + b[i - 1] * k);
            maxn = max(maxn, dp[i][k]);
        }
    }
    cout << maxn << '\n';
    return 0;
}
