#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];
}

void solve() {
    ll n;
    cin >> n;
    vector<ll> a(n);
    vector<ll> b(n);
    vector<ll> plan(n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    for (ll i = 0; i < n; i++) {
        cin >> plan[i];
    }
    vector<ll> toplimit(n, 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];
    }

    ll curans = 0;
    ll curid = 0;
    ll timer = 0;
    ll bestans = -INF;
    int cntused = 0;
    while (curid != n) {
        if (toplimit[timer] == INF) {
            bestans = max(bestans, curans);
        }
        while (cntused < toplimit[timer] && curid < n) {
            curans += a[curid] + timer * b[curid];
            cntused++;
            curid++;
        }
        timer++;
    }
    bestans = max(bestans, curans);

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

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