#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using i128 = __int128_t;

const ll inf = 1e18 + 1e16;
const ll inf_t = 1e9 + 7;
const ll mod = 998244353;

#define int ll

void solve() {
    int n;
    cin >> n;

    vector <int> a(n);
    vector <int> b(n);

    for (int i = 0; i < n; ++i) cin >> a[i] >> b[i];

    vector <int> p(n);
    for (int i = 0; i < n; ++i) cin >> p[i];
    p.push_back(n);

    int nxt[n + 1];
    nxt[n] = n;
    for (int i = n - 1; i >= 0; --i) {
        if (p[i + 1] != -1) {
            nxt[i] = i + 1;
        } else {
            nxt[i] = nxt[i + 1];
        }
    }

    vector <int> last(n);
    iota(last.begin(), last.end(), 0);

    last[0] = max(p[0], 0ll);
    int rp[n];
    iota(rp, rp + n, 0);
    for (int i = 0; i < n; ++i) {
        if (p[i] != -1) rp[p[i]] = i;
    }
    for (int i = n - 1; i >= 0; --i) {
        rp[i] = min(rp[i], rp[i + 1]);
    }
    for (int i = 0; i < n; ++i) {
        last[i] = min({last[i], rp[i]});
    }

    int ans = 0;
    int answer = 0;
    for (int i = 0; i < n; ++i) {
        ans += a[i] + last[i] * b[i];
        answer = max(ans, answer);
    }

    cout << answer << "\n";
}

signed main() {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
}
