#include <bits/stdc++.h>
#define int long long
#define all(x) x.begin(), x.end()
#define pb push_back
#define rall(x) x.rbegin(), x.rend()
using namespace std;
using ll = long long;
using ld = long double;

void solve() {
    int n;
    cin >> n;
    vector<ll> a(n), b(n);
    for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
    vector<ll> p(n);
    for (auto &el : p) cin >> el;
    vector<pair<int, int>> g;
    for (int i = 0; i < n; i++) {
        if (p[i] != -1) g.pb({p[i], i + 1});
    }
    vector<int> og(n + 1, -1);
    for (int i = 0; i < n; i++) {
        if (p[i] != -1) og[p[i]] = i + 1;
    }
    ll ans = -1e18;
    if (g.empty()) ans = 0;
    ll cur = 0;
    for (int i = 0; i < n; i++) {
        ll ml = i;
        if (lower_bound(all(g), make_pair(i + 1, -1)) != g.end()) {
            ml = min(ml, (*lower_bound(all(g), make_pair(i + 1, -1))).second - 1);
        }
        cur += a[i] + b[i] * ml;
        if (g.empty() || i >= g.back().first - 1) ans = max(ans, cur);
    }
    cout << ans;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    //cin >> t;
    while (t--) solve();
}