#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 = 1e9 + 1;

int a[maxn], b[maxn];

int p[maxn], l[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 mx = 0;
    for (int i = 1; i < n + 1; i++) {
        cin >> p[i];
        if (p[i] != -1) {
            l[p[i] + 1] = i;
            mx = max(mx, p[i]);
        }
    }
    int res = -inf;
    int cur = 0;
    int border = 0;
    if (mx == 0) {
        res = 0;
    }
    for (int i = 1; i < n + 1; i++) {
        border = max(border, l[i]);
        cur += a[i] + b[i] * border;
        if (i >= mx) {
            res = max(res, cur);
        }
    }
    cout << res << "\n";
}
