#include <bits/stdc++.h>

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

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int N = 5e3 + 11;
const int mod = 998244353;
#define int long long
signed main () {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif // LOCAL
    int n;
    cin >> n;
    int ord = -1;
    vector<int> p(n + 1);
    bool f2= 0;
    vector<int> a(n + 1), b(n + 1);
    bool f3 = 0;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i] >> b[i];
        if (b[i] >= 0 && ord == -1) ord = i;
        if (b[i] > 0) f2 = 1;
        if (b[i] < 0) f3 = 1;
    }
    vector<int> mxval (n + 1), mnval (n + 2);
    bool fl = 0;
    for (int i = 1; i <= n; ++i) mxval[i] = i - 1;
    int len = 0;
    for (int i = 1; i <= n; ++i) {
        cin >> p[i];
        len = max (len, p[i]);
        if (p[i] != -1) fl = 1;
        if (p[i] != -1) {
            mxval[p[i]] = min (mxval[p[i]], i - 1);
            mnval[p[i] + 1] = max(mnval[p[i] + 1], i);
        }
    }
    for (int i = n - 1; i >= 1; --i) {
        mxval[i] = min (mxval[i + 1], mxval[i]);
    }
    for (int i = 2; i<= n; ++i) {
        mnval[i] = max (mnval[i - 1], mnval[i]);
    }
    long long ans = -1e18;
    if (!fl) ans = 0;
        long long cur_ans = 0;
    for (int i = 1; i <= n; ++i) {
        if (i < ord) cur_ans += a[i] + b[i] * mnval[i];
        else cur_ans += a[i] + b[i] * mxval[i];
        if (i >= len) {
            ans = max (ans, cur_ans);
        }
    }
    cout << ans << '\n';
    return 0;

}

