#include<bits/stdc++.h>

#define len(v) (int)v.size()
#define all(v) v.begin(), v.end()

using namespace std;
using ll = long long int;
using ld = long double;

void solve() {
    int n;
    cin >> n;
    vector<pair<ll, ll>> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i].first >> a[i].second;
    }
    vector<int> p(n);
    int ind = -1;
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        if (p[i] == -1 && ind == -1) {
            ind = i;
        }
    }
    if (ind == -1) {
        ll ans = 0, now = 0;
        for (ll i = 0; i < n; ++i) {
            now += a[i].first;
            ans = max(ans, now);
        }
        cout << ans;
    } else {
        ll ans = 0;
        ll last = 0;
        for (ll i = 0; i < ind; ++i) {
            if (p[i] != last) {
                for (int j = last; j < p[i]; ++j) {
                    ans += a[j].first + a[j].second * i;
                }
                last = p[i];
            }
        }
        ll add = 0, maxadd = 0;
        for (ll i = ind; i < n; ++i) {
            add += a[i].first + a[i].second * ind;
            maxadd = max(maxadd, 0LL);
        }
        cout << ans + maxadd;
    }
}
//
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int tt = 1;
    //cin >> tt;
    while (tt--) {
        solve();
    }
}