#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> invp(n, -1);
    vector<int> ql(n, 0);
    int mxp = 0;
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        if (p[i] != -1) {
            invp[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]);
    }
    int ans = (mxp == 0 ? 0 : -1e18);
    int sm = 0;
    for (int i = 0; i < n; i++) {
        sm += a[i].first + ql[i] * a[i].second;
        if (i >= mxp - 1)
            ans = max(ans, sm);
    }
    cout << ans << endl;
    return 0;
}