#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;
    vector<ll> p2 = p;
    for (auto &x : p) {
        if (x == -1) x = inf;
    }
    for (auto &x : p2) {
        if (x == -1) x = -inf;
    }
    for (ll i = n - 2; i >= 0; --i) {
        p[i] = min(p[i], p[i + 1]);
    }
    for (ll i = 1; i < n; ++i) {
        p2[i] = max(p2[i], p2[i - 1]);
    }
    ll lst = -1;
    for (ll i = 0; i < n; ++i) {
        if (p[i] != inf) lst = i;
    }
    ll ptr = 0, ans = 0;
    for (ll i = 0; i <= lst; ++i) {
        while (ptr < n && a[ptr].ss <= 0 && ptr < p[i]) {
            ans += a[i].first + a[i].ss * i;
            ptr++;
        }
        while (ptr < n && a[ptr].ss >= 0 && ptr < p2[i]) {
            ans += a[i].first + a[i].ss * i;
            ptr++;
        }
    }
    ll pref = 0, mx = 0;
    for (ll i = ptr; i < n; ++i) {
        if (a[i].ss < 0) {
            pref += a[i].ff + a[i].ss * (lst + 1);
        } else {
            pref += a[i].ff + a[i].ss * i;
        }
        mx = max(pref, mx);
    }
    cout << ans + mx << '\n';
}