#include <bits/stdc++.h>

#define int long long
// #pragma GCC optimize("O3")
// // #pragma GCC optimize("Ofast")
// // #pragma GCC target("unroll_loops")

using ll = long long;
using ld = long double;
using namespace std;

// #include <ext/pb_ds/assoc_container.hpp>

//  using namespace __gnu_pbds;
// using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;

const int INF = 1e18, MAXN = 5010, mod = 998244353;

int binpow(int a, int b) {
    if (b == 0) {
        return 1;
    }
    int aa = binpow(a, b / 2);
    if (b % 2 == 0) {
        return (aa * aa) % mod;
    }
    return ((aa * aa) % mod * a) % mod;
}

void solve();

signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int tt = 1;
    //cin >> tt;
    while (tt--) {
        solve();
        cout << "\n";
    }
}


void solve() {
    int n;
    cin >> n;
    vector<int> p(n);
    vector<pair<int,int> > arr(n);\
    vector<int> pref_a(n + 1);
    for (int i = 0; i < n; i++) {
        cin >> arr[i].first >> arr[i].second;
        pref_a[i + 1] = pref_a[i] + arr[i].first;
    }
    vector<int> ogr1(n, 0);
    int mxp = 0;
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        if (p[i] != -1 && p[i] != n) {
            ogr1[p[i]] = max(i + 1, ogr1[p[i]]);
            mxp = max(mxp, p[i]);
        }
    }
    for (int i = 1; i < n; i++) {
        ogr1[i] = max(ogr1[i - 1], ogr1[i]);
    }
    
    int ans = -INF, sm = 0;
    for (int i = 0; i < n; i++) {
        if (i >= mxp) {
            ans = max(ans, sm);
        }
        sm += arr[i].first + arr[i].second * ogr1[i];
    }


    ans = max(ans, sm);


    cout << ans;
}
