// #pragma GCC optimize("O3,fast-math,unroll-loops")
#include <bits/stdc++.h>

#define i64 int64_t
#define vi vector<int>
#define vvi vector<vi>
#define vc vector
#define vb vc<bool>
#define pb push_back
#define pii pair<int, int>
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

using namespace std;

template <typename T>

istream& operator>>(istream& in, vector<T> &a) {
    for (T &el : a) in >> el;
    return in;
}

template <typename T>

ostream& operator<<(ostream& out, vector<T> a) {
    for (T &el : a) out << el << " ";
    return out;
}
#define int i64

void solve() {
    int n;
    cin >> n;
    vc<pii> p(n);
    for (int i = 0; i < n; ++i) cin >> p[i].fi >> p[i].se;
    vi t(n);
    cin >> t;
    vi t2(n, -1);
    for (int i = 0; i < n; ++i) {
        if (t[i] != -1) {
            t2[t[i] - 1] = i;
        }
    }
    vi l(n);
    vi r(n);
    iota(all(r), 0);
    int lst = -1;
    for (int i = 0; i < n; ++i) {
        l[i] = max(l[i], lst + 1);
        lst = t2[i];
    }
    lst = n;
    for (int i = n - 1; i >= 0; --i) {
        if (t2[i] != -1) lst = t2[i];
        r[i] = min(r[i], lst);
    }

    int cur = 0;
    int ans = -1e17;
    for (int i = 0; i < n; ++i) {
        if (l[i] > r[i]) break;
        if (i > lst) {
            ans = max(ans, cur);
        }
        cur += p[i].fi + p[i].se * l[i];
    }
    ans = max(ans, cur);
    cout << ans;
}

int32_t main() {
    // freopen("inputd.txt", "r", stdin);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    solve();
}