#ifndef LOCAL
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<vector>
#pragma GCC optimize("avx2")
#endif

#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;

using i128 = __int128;
using ll = long long;
using ld = long double;

template<typename T>
using orderet_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template<typename T, typename V>
using table = gp_hash_table<T, V>;

const int inf = 2e9 + 7;
const ll INF = 4e18 + 7;
#define int ll

void solve() {
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (int i = 0; i < n; ++i) cin >> a[i] >> b[i];
    vector<int> mn(n, -inf), mx(n, inf);
    for (int i = 0; i < n; ++i) {
        mn[i] = 0;
        mx[i] = i;
    }
    int cnt = -1;
    for (int i = 1; i <= n; ++i) {
        int p;
        cin >> p;
        if (p != -1) {
            --p;
            cnt = max(cnt, p);
            mx[p] = min(mx[p], i - 1);
            if (p + 1 < n) mn[p + 1] = max(mn[p + 1], i);
            //for (int j = p + 1; j < n; ++j) mn[j] = max(mn[j], i);
        }
    }
    for (int i = 1; i < n; ++i) mn[i] = max(mn[i], mn[i - 1]);
    vector<int> v(n);
    for (int i = 0; i < n; ++i) {
        v[i] = a[i] + b[i] * mn[i];
        if (i > 0) v[i] += v[i - 1];
    }
    int ans = -INF;
    if (cnt == -1) {
        ans = 0;
        cnt = 0;
    }
    for (int i = cnt; i < n; ++i) ans = max(ans, v[i]);
    cout << ans;
}

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t = 1;
    //cin>>t;
    while (t--) solve();
}
