#include <bits/stdc++.h>
#define int long long
#define all(x) x.begin(), x.end()
#define pb push_back
#define rall(x) x.rbegin(), x.rend()
using namespace std;
using ll = long long;
using ld = long double;

struct ST {
    int n;
    int tree[20001];

    ST(int sz) {
        n = sz;
        fill(tree, tree + n * 4, -1e18);
    }

    void update(int v, int l, int r, int ind, int x) {
        if (r - l == 1) {
            tree[v] = x;
            return;
        }
        int m = (l + r) / 2;
        if (ind < m) update(v * 2, l, m, ind, x);
        else update(v * 2 + 1, m, r, ind, x);
        tree[v] = max(tree[v * 2], tree[v * 2 + 1]);
    }

    int get(int v, int l, int r, int ql, int qr) {
        if (qr <= l || r <= ql) return -1e18;
        if (ql <= l && r <= qr) return tree[v];
        int m = (l + r) / 2;
        return max(get(v * 2, l, m, ql, qr), get(v * 2 + 1, m, r, ql, qr));
    }

    void update(int ind, int x) {
        update(1, 0, n, ind, x);
    }

    int get(int ql, int qr) {
        return get(1, 0, n, ql, qr + 1);
    }
};

void solve() {
    int n;
    cin >> n;
    vector<ll> a(n), b(n);
    for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
    vector<ll> p(n);
    for (auto &el : p) cin >> el;
    vector<pair<int, int>> g;
    for (int i = 0; i < n; i++) {
        if (p[i] != -1) g.pb({p[i], i + 1});
    }
    vector<ST> dp(n + 1, ST(n + 1));
    dp[0].update(0, 0);
    for (int i = 1; i <= n; i++) {
        int rr = i;
        int lc = 0;
        if (!g.empty() && lower_bound(all(g), make_pair(i, -1)) != g.end()) {
            auto [xx, yy] = *lower_bound(all(g), make_pair(i, -1));
            rr = min(rr, yy);
        }
        if (!g.empty() && lower_bound(all(g), make_pair(i, -1)) != g.begin()) {
            auto it = lower_bound(all(g), make_pair(i, -1));
            it--;
            auto [xx, yy] = *it;
            lc = max(lc, yy);
        }
        for (int j = lc; j < rr; j++) {
            int l = 0, r = i - 2;
            r = min(r, j);
            if (!g.empty() && lower_bound(all(g), make_pair(i - 1, -1)) != g.end()) {
                auto [x, y] = *lower_bound(all(g), make_pair(i - 1, -1));
                r = min(r, y - 1);
            }
            if (!g.empty() && lower_bound(all(g), make_pair(i - 1, -1)) != g.begin()) {
                auto it = lower_bound(all(g), make_pair(i - 1, -1));
                it--;
                auto [x, y] = *it;
                l = max(l, y);
            }
            if (r < l) dp[i].update(j, a[i - 1] + (ll) b[i - 1] * j);
            else {
                dp[i].update(j, dp[i - 1].get(l, r) + a[i - 1] + (ll) b[i - 1] * j);
            }
        }
    }
    ll ans = -1e18;
    int l = 0;
    if (!g.empty()) {
        l = max(l, g.back().first);
    }
    for (int i = l; i <= n; i++) {
        ans = max(ans, dp[i].tree[1]);
    }
    cout << ans;
}

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