#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(a) a.begin(), a.end()
const int MAXN = 1e6 + 1, MOD = 998244353;

void solve() {
    int n;
    cin >> n;
    vector<ll> a(n), b(n);
    vector<int> p(n), t(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
    }
    int k = 0;
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        k = max(k, p[i]);
        if (p[i] != -1 && p[i] < n) {
            t[p[i]] = i;
        }
    }
    ll ans = -1e18, cur = 0;
    for (int i = 0; i < n; ++i) {
        t[i] = max(t[i], (i == 0 ? 0 : t[i - 1]));
        cur += a[i] + b[i] * t[i];
        if (i >= k - 1) {
            ans = max(ans, cur);
        }
    }
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }
}
