#include <bits/stdc++.h>
using namespace std;
using ll = long long;

void Solve();

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    ll tt = 1;
    // cin >> tt;
    while (tt--) {
        Solve();
    }
    return 0;
}

# define ff first
# define ss second

const ll inf = 1e18;

void Solve() {
    ll n;
    cin >> n;
    vector<pair<ll, ll>> a(n);
    for (auto &x : a) cin >> x.ff >> x.ss;
    vector<ll> p(n);
    for (auto &x : p) cin >> x;
    ll lst = -1;
    for (ll i = 0; i < n; ++i) {
        if (p[i] != -1) lst = i;
    }
    ll ans = 0, ptr = 0;
    for (ll i = 0; i < n; ++i) {
        while (ptr < p[i] && ptr < n) {
            ans += a[ptr].ff + a[ptr].ss * i;
            ptr++;
        }
    }
    ll pref = 0, mx = 0;
    for (ll i = ptr; i < n; ++i) {
        pref += a[i].ff + a[i].ss * i;
        mx = max(mx, pref);
    }
    // for (auto &x : p) {
    //     cout << x << ' ';
    // }
    // cout << '\n';
    cout << ans + mx << '\n';
}