#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;
    ll n1 = n;
    for (ll i = 0; i < n; i++) {
        if (p[i] == -1) {
            n1 = i;
            break;
        }
    }
    ll mx = 0;
    ll cur = 0;
    ll cnt = 0;
    for (ll i = 0; i < n; i++) {
        if (p[i] == -1) break;
        for (ll j = cnt; j < p[i]; j++) {
            mx += a[j] + b[j] * cur;
        }
        cnt = p[i];
        cur = i + 1;
    }
    ll ans = mx;
    for (ll i = cnt; i < n; i++) {
        ans += a[i] + b[i] * cur;
        mx = max(mx, ans);
    }
    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();
    }
}