#include <bits/stdc++.h>

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")

#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < n; i++)
#define cin(v) for (auto& vi : v) { cin >> vi; }
#define cout(v) for (auto& vi : v) { cout << vi << ' '; } cout << '\n';
#define int long long

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;

const int INF = 1e9 + 7;
const int MOD = 998244353;
const int MOD2 = 1e9 + 737373;
const ll LINF = 1e18 + 7;
const double eps = 1e-8;
const double PI = acos(-1);

int n, m;

ll binpow(ll x, ll y, ll mod) {
    if (y == 0) return 1;
    if (y % 2 == 0) return binpow(x * x % mod, y / 2, mod);
    return binpow(x, y - 1, mod) * x % mod;
}

vector <int> g[1000073];
char used[1000073];
int p[1000073];
int sz[1000073];

void dfs(int v) {
    if (used[v]) return;
    used[v] = 1;
    sz[v] = 1;
    for (int to : g[v]) {
        if (!used[to]) {
            dfs(to);
            sz[v] += sz[to];
        }
    }
}

int upchange[1000073];
int downchange[1000073];

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout << fixed << setprecision(10);
    bool pminus = 1, g56 = 1, g78 = 1;
    cin >> n;
    vector <pair <int, int> > line(n);
    vector <ll> A(n), B(n);
    vector <pair <int, int> > border(n);
    vector <int> plan(n);
    rep(i, n) {
        cin >> A[i] >> B[i];
        line[i] = {A[i], B[i]};
        if (B[i] > 0) {
            g56 = 0;
        }
        if (B[i] < 0) {
            g78 = 0;
        }
    }
    rep(i, n) {
        cin >> plan[i];
        if (plan[i] != -1) {
            pminus = 0;
        }
    }
    rep(i, n) {
        border[i] = {0, i};
    }
    fill(upchange, upchange + n, INF);
    int down = 0, up = n - 1;
    int minpep = 0;
    for (int i = 0; i < n; i++) {
        if (plan[i] != -1) {
            downchange[plan[i]] = i + 1;
            upchange[plan[i] - 1] = min(upchange[plan[i] - 1], i);
            minpep = max(minpep, plan[i]);
        }
    }
    for (int i = n - 2; i >= 0; i--) {
        upchange[i] = min(upchange[i], upchange[i + 1]);
    }
    for (int i = 0; i < n; i++) {
        border[i].first = max(border[i].first, downchange[i]);
        border[i].second = min(border[i].second, upchange[i]);
    }
    // rep(i, n) {
    //     cout << border[i].first << ' ' << border[i].second << '\n';
    // }
    vector <ll> pref(n + 1, 0);
    for (int i = 0; i < n; i++) {
        pref[i + 1] = pref[i] + A[i];
    }
    //cout(pref);

    if (g56) {
        ll ans = -LINF;
        vector <ll> ans_pref(n + 1, -LINF);
        ans_pref[0] = 0;
        ll cur_ans = 0;
        for (int i = 0; i < n; i++) {
            cur_ans += B[i] * border[i].first;
            ans_pref[i + 1] = cur_ans + pref[i + 1];
        }
        for (int i = minpep; i <= n; i++) {
            ans = max(ans, ans_pref[i]);
        }
        cout << ans << '\n';
    }
    else if (g78) {
        ll ans = -LINF;
        vector <ll> ans_pref(n + 1, -LINF);
        ans_pref[0] = 0;
        ll cur_ans = 0;
        for (int i = 0; i < n; i++) {
            cur_ans += B[i] * border[i].second;
            ans_pref[i + 1] = cur_ans + pref[i + 1];
        }
        for (int i = minpep; i <= n; i++) {
            ans = max(ans, ans_pref[i]);
        }
        cout << ans << '\n';
    }
    else if (n <= 5000) {
        ll ans = -LINF;
        for (int k = 1; k <= n; k++) {
            vector <ll> suff(k + 1, 0);
            for (int i = k - 1; i >= 0; i--) {
                suff[i] = suff[i + 1] + B[i];
            }
            ll cur_ans = 0;
            int pos = 0;
            for (int i = 0; i < k; i++) {
                if (B[i] >= 0) {
                    cur_ans += B[i] * border[i].second;
                }
                else {
                    cur_ans += B[i] * min(pos, border[i].first);
                }
            }
            cur_ans += pref[k];
            //cout << k << " : " << cur_ans << '\n';
            ans = max(ans, cur_ans);
        }
        cout << ans << '\n';
    }
    return 0;
}