#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

mt19937 rng(52);

ll get(ll l, ll r) {
    ll x = rng() % (r-l+1);
    return x + l;
}


void solve() {
    ll n;
    cin >> n;
    vector<pair<ll, ll>> v(n);
    for (ll i = 0; i < n; i++) {
        cin >> v[i].first >> v[i].second;
    }
    vector<ll> order(n);
    for (ll i = 0; i < n; i++) {
        cin >> order[i];
    }
    vector<ll> maxi(n);
    ll last = n;
    for (ll i = n-1; i >= 0; i--) {
        if (order[i] != -1) last = order[i];
        else maxi[i] = last;
    }
    ll sum = 0;
    ll ans = 0;
    ll prev = 0;
    for (ll i = 0; i < n; i++) {
        if (order[i] != -1) {
            for (ll j = prev; j < order[i]; j++) {
                sum += v[j].first + v[j].second * i;
            }
            ans = max(ans, sum);
            prev = order[i];
        } else {
            for (ll j = prev; j < n; j++) {
                sum += v[j].first + v[j].second * j;
                ans = max(ans, sum);
            }
            prev = n;
        }
        if (prev == n) break;
    }
    cout << ans << '\n';
}

int main() {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    //ios_base::sync_with_stdio(false); cin.tie(0);
    ll t = 1;
    //cin >> t;
    while(t--) solve();

    return 0;
}

