#include <bits/stdc++.h>
using namespace std;
#define int long long

signed main() {
#ifdef LOCAL
    freopen("test", "r", stdin);
    freopen("res", "w", stdout);
#endif

    int n;
    cin >> n;

    vector<int> a(n), b(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
    }

    vector<int> p(n);
    for (int i = 0; i < n; ++i) {
        cin >> p[i]; p[i]--;
    }

    vector<int> l(n + 1, 0), r(n + 1, n);
    iota(r.begin(), r.end(), 0);
    for (int i = 0; i < n; ++i) {
        if (p[i] < 0) continue;
        r[p[i]] = min(r[p[i]], i);
        l[p[i] + 1] = max(l[p[i]], i + 1);
    }

    for (int i = n - 1; i >= 0; --i) {
        r[i] = min(r[i + 1], r[i]);
    }
    
    int from = *max_element(p.begin(), p.end());

    int ans = -2e18;
    if (from < 0) ans = 0;
    int cur = 0;
    for (int i = 0; i < n; ++i) {
        cur += a[i] + b[i] * r[i];
        if (i >= from) ans = max(ans, cur);
    }

    cout << ans << '\n';
}