#include <bits/stdc++.h>
#define int long long

using namespace std;
const int MOD = 998'244'353;

ostream& operator<<(ostream& out, vector<int> v) {
    for (auto el : v) {
        out << el << " ";
    }
    return out << "\n";
}
istream& operator>>(istream& in, vector<int>& v) {
    for (auto& el : v) {
        in >> el;
    }
    return in;
}

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> p(n);
    cin >> p;
    for (int i = n - 2; i >= 0; i--) {
        if (p[i] == -1 && b[p[i + 1] - 1] >= 0) {
            p[i] = min(p[i + 1], i + 1);
        }
    }
    int mx = 0;
    for (int i = 0; i < n; i++) {
        mx = max(p[i], mx);
        if (p[i] != -1) {
            p[i] = max(mx, p[i]);
        }
    }
    for (int i = n - 2; i >= 0; i--) {
        if (p[i] == -1) {
            p[i] = p[i + 1];
        }
    }
    int i = 0;
    int sum = 0;
    for (; i < n && p[i] != -1; i++) {
        for (int j = (i != 0 ? p[i - 1] : 0); j < p[i]; j++) {
            sum += a[j] + b[j] * i;
        }
    }
    int ans = sum;
    for (int j = (i != 0 ? p[i - 1] : 0); j < n; j++) {
        sum += a[j] + b[j] * (b[j] > 0 ? j : i);
        ans = max(ans, sum);
    }
    cout << ans;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t = 1;
//    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
