#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")

#define int long long
#define ff first
#define ss second
#define endl "\n"
#define all(x) x.begin(), x.end()
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update<>>

const int MOD = 998244353;
const int INF = 1e18;


void solve() {
    int n;
    cin >> n;

    vector<int> a(n, 0);
    vector<int> b(n, 0);

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

    vector<int> p(n, 0);

    vector<int> gp;
    vector<int> gi;

    for (int i = 0; i < n; i++) {
        cin >> p[i];
        if (p[i] != -1) {
            gp.push_back(p[i] - 1);
            gi.push_back(i);
        }
    }

    int ans = -INF;
    int csm = 0;

    if (gp.size() == 0) {
        ans = 0;
    }

    int ncp = 0;

    for (int k = 0; k < n; k++) {
        int id = lower_bound(all(gp), k) - gp.begin();
        if (id == gi.size()) {
            ncp += b[k];
        } else {
            id = gi[id];
            csm += b[k] * id;
        }
        csm += a[k];
        if (gp.size() == 0 || gp.back() <= k) {
            ans = max(ans, csm + ncp * k);
        }
    }

    cout << ans << endl;
}


signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    // cin >> t;

    while (t--) {
        solve();
    }
}
