#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
using ll = long long;
mt19937 rnd(52);
ll mod = 998244353;
int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    cin.tie(nullptr);
    ios::sync_with_stdio(0);
#endif
    int n;
    cin >> n;
    vector<ll> a(n);
    vector<ll> b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    vector<ll> prefsa(n+1);
    vector<ll> prefsb(n+1);
    for (int i = 0; i < n; i++) {
        prefsa[i+1] = prefsa[i] + a[i];
        prefsb[i+1] = prefsb[i] + b[i];
    }
    vector<int> p(n);
    vector<ll> t;
    int cur = 0;
    ll ans = 0;
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        if (p[i] == -1) {
            continue;
        }
        ans = -1e18;
        while (t.size()!=p[i]) {
            t.push_back(cur);
        }
        cur = i+1;
    }
    while (t.size()!=n) {
        t.push_back(cur);
    }
    ll answer = 0;
    for (int i = 0; i < n; i++) {
        ans+=a[i]+b[i]*t[i];
        if (p[i] == -1) {
            answer = max(answer, ans);
        } else {
            answer = ans;
        }
    }
    cout << answer << endl;
    return 0;
}