#pragma GCC optimize("O3, unroll-loops")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

const int inf = 1e18L;
const int mod = 998244353;
const int maxn = 1e6 + 1;

void solve() {
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    ll sm = 0, ans = -inf;
    int first = -1;
    int lst = -1;
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
        if (first == -1 && b[i] >= 0) first = i;
    }
    vector<int> p(n), t(n, 0);
    vector<int> z(n);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        if (p[i] != -1) lst = i;
        else continue;
        if (p[i] != n)
            t[p[i]] = max(i, t[p[i]]);
        z[p[i] - 1] = i;
    }
    ll mx = (lst == -1 ? 0: -inf);
    for (int i = 0; i < n; i++) {
        mx = max(mx, t[i]);
        sm += a[i] + b[i] * mx;
        if (i >= lst)
            ans = max(ans, sm);
    }
    cout << ans;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
        cout << '\n';
    }
}
