#include<bits/stdc++.h>
#define all(p) p.begin(),p.end()
using namespace std;
#define int long long
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    int a[n + 1], b[n + 1];
    int ans = -LLONG_MAX;
    int sum = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i] >> b[i];
    }
    int p[n + 1];
    int l[n + 1];
    int r[n + 1];
    iota(r + 1, r + n + 1, 0);
    fill(l + 0, l + n + 1, 0);
    int mxx = 0;
    for (int i = 1; i <= n; i++) {
        cin >> p[i];
        if (p[i] != -1) {
            mxx = max(mxx, p[i]);
            r[p[i]] = min(r[p[i]], i - 1);
            if (p[i] != n) {
                l[p[i] + 1] = max(l[p[i] + 1], i);
            }
        }
    }
    if (mxx == 0) {
        ans = 0;
    }
    for (int i = n - 1; i >= 1; i--) {
        r[i] = min(r[i], r[i + 1]);
    }
    for (int i = 1; i <= n; i++) {
        sum += a[i] + b[i] * r[i];
        if (i >= mxx) {
            ans = max(ans, sum);
        }
    } 
    cout << ans;
}