#include<bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
using ll = long long;
using ld = long double;

#define int long long
const int maxn = 1e6 + 2;
const int inf = 1e18 + 1;

int a[maxn], b[maxn];

int p[maxn], l[maxn];

set<int> mn, mx;

vector<int> del[maxn];

vector<int> add[maxn];

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n; cin >> n;
    for (int i = 1; i < n + 1; i++) {
        cin >> a[i] >> b[i];
    }
    int border = 0;
    for (int i = 1; i < n + 1; i++) {
        cin >> p[i];
        if (p[i] == -1) {
            continue;
        }
        add[p[i] + 1].push_back(i);
        del[p[i] + 1].push_back(i - 1);
        mx.insert(i - 1);
        border = max(border, p[i]);
    }
    int res = -inf;
    int cur = 0;
    //int border = 0;
    if (border == 0) {
        res = 0;
    }
    mn.insert(0);
    mx.insert(n + 1);
    for (int i = 1; i < n + 1; i++) {
        while (!add[i].empty()) {
            mn.insert(add[i].back());
            add[i].pop_back();
        }
        while (!del[i].empty()) {
            mx.erase(del[i].back());
            del[i].pop_back();
        }
        int l = *(--mn.end());
        int r = min(i - 1, *(mx.begin()));
        if (b[i] <= 0) {
            cur += a[i] + b[i] * l;
        } else {
            cur += a[i] + b[i] * r;
        }
        if (i >= border) {
            res = max(res, cur);
        }
    }
    cout << res << "\n";
}
