#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 = 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;
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        if (p[i] == -1) continue;
        f = true;
        --p[i];
        arr.emplace_back(p[i], i);
    }
    ans = (f ? -inf: 0);
    vector<int> t(n, 0);
    if (f) {
        for (int j = 0; j < arr[0].first; ++j) {
            t[j] = arr[0].second;
        }
    }
    for (int i = 0; i < ll(arr.size()) - 1; i++) {
        for (int j = arr[i].first; j <= arr[i + 1].first - 1; ++j) t[j] = arr[i].second;
    }
    t[arr.back().first] = arr.back().second;
    for (int j = arr.back().first; j < n; ++j) {
        if (!t[j])
            t[j] = j;
    }
    for (int i = 0; i < n; i++) {
        sm += a[i] + b[i] * t[i];
        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';
    }
}
