#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 = 1e12L;
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 = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    vector<int> p(n);
    vector<pair<int, int>> arr;
    bool f = false;
    int lst = 0;
    vector<int> bound, temp;
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        if (p[i] == -1) continue;
        f = true;
        --p[i];
        bound.emplace_back(p[i]);
        temp.emplace_back(i);
        lst = max(lst, p[i]);
        arr.emplace_back(p[i], i);
    }
    ans = (f ? -inf: 0);
    vector<int> t(n, 0);
   for (int i = 0; i < n; ++i) {
       auto lb = lower_bound(all(bound), i);
       int j = lb - bound.begin();
       if (lb == bound.begin() && j != *lb) t[i] = 0;
       else {
           --lb;
           t[i] = temp[lb - bound.begin()];
       }
   }
    for (int i = 0; i < n; i++) {
        sm += a[i] + b[i] * t[i];
        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';
    }
}
