#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #pragma GCC optimize("O3,Ofast,unroll-loops")
// #pragma GCC target("avx2,bmi")

using namespace std;
// using namespace __gnu_pbds;
// template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

void solve();

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif
    int t = 1;
    while (t--) {
        solve();
    }
}

#define int long long
const int MAXN = 2e5, MOD = 998244353, INF = 2e18;


void solve() {
    int n;
    cin >> n;
    vector<int> a(n + 1), b(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> a[i] >> b[i];

    }
    vector<int> p(n);
    vector<int> pos(n + 1);
    for (int i = 1; i <= n; i++) {
        pos[i] = i;
    }

    int last = 0;
    vector<int> start(n + 2);

    for (int i = 0; i < n; i++) {
        cin >> p[i];
        last = max(last, p[i]);
        if (p[i] != -1) {
            start[p[i]+ 1] = max(start[p[i] + 1],i + 1);
            pos[p[i]] = min(pos[p[i]], i+1);
        }
    }
    for (int i = 2; i <= n; i++) {
        start[i] = max(start[i-1], start[i]);
    }
    for (int i = n-1; i >= 1; i--) {
        pos[i] = min(pos[i], pos[i+1]);
    }
    int ans = -INF;
    if (last == 0) {
        ans = 0;
    }
    int now = 0;
    int ls = 0;
    for (int i = 1; i <= n; i++) {
        if (b[i] < 0) {
            now += a[i] + b[i] * start[i];
        } else {
            now += a[i] + b[i] * (pos[i]-1);    
        }
        if (i >= last) {
            ans = max(ans, now);
        }
    }
    cout << ans;
}
