#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")

using namespace std;
using ll = long long;

# define len(x) ((ll) x.size())
const ll inf = 1e18;

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

    vector<pair<ll, ll>> a(n);
    for (ll i = 0; i < n; ++i) {
        cin >> a[i].first >> a[i].second;
    }

    vector<ll> p(n);
    for (ll i = 0; i < n; ++i) {
        cin >> p[i];
    }
    vector<ll> fnw(n, inf);
    for (ll i = 0; i < n; ++i) {
        if (p[i] == -1) { continue; }
        fnw[p[i] - 1] = min(i, fnw[p[i] - 1]);
    }

    ll mn = 0;
    ll prv = 0;
    for (ll i = 0; i < n; ++i) {
        if (fnw[i] == inf) {
            fnw[i] = mn;
        } else {
            mn = prv + 1;
            prv = fnw[i];
        }
    }

    ll res = 0;
    ll mx = 0;
    for (ll i = 0; i < n; ++i) {
        res += a[i].first + a[i].second * fnw[i];
        mx = max(mx, res);
    }

    cout << mx << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    solve();

    return 0;
}