#include <bits/stdc++.h>
#define int long long
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define em emplace_back

using namespace std;
template<class C>
using vec = vector<C>;
using vi = vector<int>;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

vi buildL(vi a) {
    int n = a.size();
    vi st;
    vi f(n, -1);
    for (int i = 0; i < n; i++) {
        while (!st.empty() && a[st.back()] > a[i]) {
            st.pop_back();
        }
        if (!st.empty())
            f[i] = st.back();
        st.pb(i);
    }
    return f;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
#ifdef LOCAL
    freopen("input", "r", stdin);
#endif
    int n;
    cin >> n;
    vi a(n), b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
        assert(b[i] >= 0);
    }
    vi p(n);
    vi dp(n);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
    }
    vi t(n, n - 1);
    int cur = 0;
    for (int i = 0; i < n; i++) {
        if (p[i] == -1) {
            continue;
        }
        int ti = i;
        while (cur < p[i]) {
            t[cur] = ti;
            cur++;
        }
    }
    int sum = 0;
    int ans = 0;
    for (int i = 0; i < n; i++) {
        sum += a[i];
        sum += t[i] * b[i];
        ans = max(ans, sum);
    }
    cout << ans << '\n';
}