#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<int> a(n), b(n), p(n), t(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
    }
    int mx = 0;
    for (int i = 0 ;i < n; ++i) {
        cin >> p[i];
        mx = max(mx, p[i] - 1);
        if (p[i] != -1 && p[i] < n) {
            t[p[i]] = i;
        }
    }
    ll ans = 0, cur = 0;
    ll mn = 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 >= mx) {
            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();
    }
}
