#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define asc(x) x.begin(), x.end()
#define des(x) x.rbegin(), x.rend()
#define pb push_back
#define eb emplace_back
#define ll long long
#define db long double
#define fori(a, b) for (int i = (a); i < (b); ++i)
#define forj(a, b) for (int j = (a); j < (b); ++j)

using namespace std;
using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;

mt19937 gen(228);

const ll inf = 1e18;

void solve() {
    int n; cin >> n;

    vector<ll> pra(n + 1, 0), prb(n + 1, 0);
    fori(0, n) {
        int x, y; cin >> x >> y;

        pra[i + 1] = pra[i] + x;
        prb[i + 1] = prb[i] + y;
    }

    vector<int> p(n);

    int must = 0;

    fori(0, n) {
        cin >> p[i];
        must = max(must, p[i]);
    }

    vector<ll> dp(n + 1, -inf);

    int mn = p[0];
    dp[0] = 0;

    vector<int> pos = {-1};
    fori(0, n) {
        if (p[i] != -1) {
            pos.pb(i);
        }
    }

    if (mn == -1) {
        fori(0, n) {
            dp[i + 1] = pra[i + 1];
        }
    } else {
        dp[0] = -inf;
        dp[mn] = pra[mn];
    }

    ll ans = -inf;

    if (pos.back() <= 0) {
        fori(0, n + 1) {
            ans = max(ans, dp[i]);
        }
    }

    for (int t = 1; t < n; ++t) {
        fori(0, n + 1) {
            dp[i] += -pra[i] - prb[i] * t;

            if (i) {
                dp[i] = max(dp[i - 1], dp[i]);
            }
        }

        vector<ll> ndp(n + 1, -inf);

        int mx = n;

        if (p[t] != -1) {
            mn = p[t], mx = p[t];
        }

        mn = max(mn, t + 1);

        fori(mn, mx + 1) {
            ndp[i] = pra[i] + prb[i] * t + dp[i];

            if (t >= pos.back()) {
                ans = max(ans, ndp[i]);
            }
        }

        swap(dp, ndp);
    }

    cout << ans;
}

signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int tt = 1;
    while (tt--) {
        solve();
        cout << "\n";
    }
}