#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, inf);
    for (auto &x : p) cin >> x;
    for (auto &x : p) {
        if (x == -1) x = -inf;
    }
    ll lst = -1;
    for (ll i = 0; i < n; ++i) {
        if (p[i] != -inf) lst = i;
    }
    for (ll i = 1; i < lst; ++i) {
        p[i] = max(p[i], p[i - 1]);
    }
    ll ans = 0, ptr = 0;
    for (ll i = 0; i <= lst; ++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 + 1);
        mx = max(mx, pref);
    }
    // for (auto &x : p) {
    //     cout << x << ' ';
    // }
    // cout << '\n';
    cout << ans + mx << '\n';
}