#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;

using namespace __gnu_pbds;

template<typename T>
using ordered_set = tree<T, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>;
using ll = long long;

void solve() {
    ll n;
    cin >> n;
    vector<ll> a(n), b(n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    vector<ll> p(n);
    for (ll &i : p) cin >> i;
    vector<ll> t(n);
    for (ll i = 0; i < n; i++) {
        t[i] = i;
    }
    for (ll i = 0; i < n; i++) {
        if (p[i] == -1) break;
        t[p[i] - 1] = i;
    }
    for (ll i = n - 2; i >= 0; i--) {
        t[i] = min(t[i], t[i + 1]);
    }
    ll mx = 0;
    ll ans = 0;
    for (ll i = 0; i < n; i++) {
        if (p[i] == -1) {
            ans += a[i] + b[i] * t[i];
            mx = max(mx, ans);
        } else {
            mx += a[i] + b[i] * t[i];
            ans = mx;
        }
    }
    cout << mx;
}

signed main() {
    // #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    // #endif
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    ll t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }
}