#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long

using namespace __gnu_pbds;
using namespace std;

template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;



signed main() {
    int n;
    cin >> n;
    vector<pair<int, int>> a(n);
    vector<pair<int, int>> og(n);
    for (int i = 0; i < n; ++i) {
        og[i] = {0, i};
    }
    for (int i = 0; i < n; ++i) {
        cin >> a[i].first >> a[i].second;
    }
    vector<int> p(n);
    int mn = 0;
    int nw = 0;
    int toch = -1;
    for (int i = 0; i  < n; ++i) {
        cin >> p[i];
        if (p[i] == -1) continue;
        toch = p[i] - 1;
        for (int j = nw; j < p[i]; ++j) {
            og[j].second = min(og[j].second, i);
            og[j].first = max(og[j].first, mn);
        }
        mn = i + 1;
        nw = p[i];
    }
    while (nw < n) {
        og[nw].first = max(og[nw].first, mn);
        nw++;
    }
    vector<int> cl(n);
    int ans = -1e17;
    if (toch == -1) ans = 0;
    int sm = 0;
    for (int i = 0; i < n; ++i) {
        cl[i] = a[i].first + a[i].second * og[i].second;
        sm += cl[i];
        if (i >= toch) {
            ans = max(ans, sm);
        }
    }
    cout << ans;
}