#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <experimental/random>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ordered_set = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>;
using str = string;
using ld = long double;
const ll INF = 1e18, MOD = 998244353;
#define int long long
int binp(int v,int x) {
    if (x == 0) {
        return 1;
    }
    auto w = binp(v, x / 2);
    if (x % 2) {
        return w * w % MOD * v % MOD;
    }
    return w * w % MOD;
}

void solve();

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int _ = 1;
    //cin >> _;
    while (_--) {
        solve();
        //cout << "\n";
    }
}

void solve() {
    int n;
    cin >> n;
    vector<pair<int,int> > a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i].first >> a[i].second;
    }
    vector<int> p(n);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
    }
    vector dp(n + 1, -INF);
    vector<pair<int,int> > ogr(n + 1);
    for (int i = 0; i <= n; ++i) {
        ogr[i].second = i;
    }
    int mx = -1;
    for (int i = 0; i < n; i++) {
        int v = p[i];
        mx = max(mx, v);
        if (v != -1) {
            for (int j = 0; j < v; ++j) {
                ogr[j].second = min(ogr[j].second, i);
            }
            for (int j = v; j < n; ++j) {
                ogr[j].first = max(ogr[j].first, i + 1);
            }
        }
    }
    dp[0] = 0;
    int ans = -INF;
    if (mx == -1) {
        ans = 0;
    }
    for (int i = 0; i < n; ++i) {
        for (int j = ogr[i].second; j >= ogr[i].first; --j) {
            dp[j] += a[i].first + a[i].second * j;
        }
        for (int j = ogr[i].first - 1; j >= 0; --j) {
            dp[j] = -INF;
        }
        for (int j = 1; j <= ogr[i + 1].second; j++) {
            dp[j] = max(dp[j], dp[j - 1]);
        }
        if (i + 1 >= mx) {
            for (int j = 0; j <= n; j++) {
                ans = max(ans, dp[j]);
            }
        }
    }
    cout << ans;
}
