#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const ll INF = 1e18;
vector<ll> prefb;

ll getb(ll l, ll r) {
    if (l == 0) {
        return prefb[r];
    }
    return prefb[r] - prefb[l - 1];
}

ll n;
vector<ll> a;
vector<ll> b;
vector<ll> plan;
vector<ll> toplimit;

ll solveforlast(ll last) {
    ll curans = 0;
    ll curid = 0;
    ll timer = 0;
    ll bestans = -INF;
    int cntused = 0;

    while (cntused != last) {
        if (toplimit[timer] == INF) {
            bestans = max(bestans, curans);
        }
        while (cntused < toplimit[timer] && b[curid] < 0 && cntused < last) {
            curans += a[curid] + timer * b[curid];
            cntused++;
            curid++;
            if (toplimit[timer] == INF) {
                bestans = max(bestans, curans);
            }
        }
        while (cntused < plan[timer]) {
            curans += a[curid] + timer * b[curid];
            cntused++;
            curid++;
        }
        if (curid == timer) {
            curans += a[curid] + timer * b[curid];
            cntused++;
            curid++;
        }
        timer++;
    }
    bestans = max(bestans, curans);
    return bestans;
}

void maxupd(ll &a, const ll &b) {
    a = max(a, b);
}

void solve() {
    cin >> n;
    a.resize(n);
    b.resize(n);
    plan.resize(n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    ll maxplan = 0;
    for (ll i = 0; i < n; i++) {
        cin >> plan[i];
        maxplan = max(maxplan, plan[i]);
    }
    toplimit.assign(n + 1, INF);
    if (plan[n - 1] != -1) {
        toplimit[n - 1] = plan[n - 1];
    }
    for (ll i = n - 2; i >= 0; i--) {
        if (plan[i] == -1) {
            toplimit[i] = toplimit[i + 1];
        } else {
            toplimit[i] = plan[i];
        }
    }
    prefb.resize(n);
    prefb[0] = b[0];
    for (ll i = 1; i < n; i++) {
        prefb[i] = b[i] + prefb[i - 1];
    }

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

    dp[0][0] = a[0];
    ll bestans = -INF;
    if (toplimit[0] == INF) bestans = 0;

    for (int i = 0; i < n - 1; i++) {
        ll bestprev = -INF;
        for (int time = 0; time <= i + 1; time++) {
            if (time != 0 && plan[time - 1] > i + 1) break;
            if (toplimit[i + 1] == INF) {
                maxupd(bestans, dp[i][time]);
            }
            maxupd(bestprev, dp[i][time]);
            maxupd(dp[i + 1][time], bestprev + a[i + 1] + b[i + 1] * time);
        }
    }
    for (int time = 0; time <= n; time++) {
        maxupd(bestans, dp[n - 1][time]);
    }

    // for (int last = maxplan; last <= n; last++) {
    //     bestans = max(bestans, solveforlast(last));
    // }

    cout << bestans << "\n";
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    solve();
}
