#include<bits/stdc++.h>

using namespace std;

#define int long long

void solve() {
    int n;
    cin >> n;
    vector<pair<int, int>> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i].first >> a[i].second;
    }
    vector<int> p(n);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
    }
    int mmax = -9999999999;
    int now = 0;
    int y = 0;
    for (int i = 0; i < n; i++) {
        while ((i + 1) >= p[y] && p[y] != -1) {
            y += 1;
        }
        if (y > i) {
            cout << mmax;
            return;
        }
        now += a[i].first;
        now += a[i].second * y;
        mmax = max(mmax, now);
    }
    cout << max(mmax, 0ll);
}

signed main() {
    int t = 1;
    while(t--) {
        solve();
    }
}
