#include <bits/stdc++.h>
#define int long long
using namespace std;

#ifndef LOCAL
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2")
#endif

signed main() {
    ios_base::sync_with_stdio(NULL);
    cin.tie(NULL);
#ifdef LOCAL
    freopen("./../input.txt", "r", stdin);
#endif
    int n;
    cin >> n;
    vector<pair<int, int>> a(n);
    vector<int> p(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i].first >> a[i].second;
    }
    vector<int> ql(n, 0), qr(n, n - 1);
    int mxp = 0;
    for (int i = 0; i < n; i++) {
        qr[i] = i;
    }
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        if (p[i] != -1) {
            qr[p[i] - 1] = i;
            if (p[i] != n)
                ql[p[i]] = i + 1;

            mxp = p[i];
        }
    }
    for (int i = 1; i < n; i++) {
        ql[i] = max(ql[i - 1], ql[i]);
    }
    for (int i = n - 2; i >= 0; i--) {
        qr[i] = min(qr[i + 1], qr[i]);
    }
    int ans = -1e18;
    if (mxp == 0) {
        ans = 0;
    }
    int sm = 0;
    for (int i = 0; i < n; i++) {
        if (ql[i] > qr[i])
            break;
        if (a[i].second <= 0) {
            sm += a[i].first + a[i].second * ql[i];
        } else {
            sm += a[i].first + a[i].second * qr[i];
        }
        if (i >= mxp - 1)
            ans = max(ans, sm);
    }
    cout << ans << endl;
    return 0;
}