#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz(x) (int)(x.size())

constexpr int N = 1e7;
constexpr int inf = 1e18;
constexpr int mod = 1e9 + 7;

signed main() {
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    int curr = 0, t = 0, sum = 0;
    vector<int> p(n);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        if (p[i] != -1) {
            vector<int> c;
            while (curr < p[i]) {
                c.push_back(curr);
                curr++;
            }
            for (auto j : c) {
                if (j <= i) {
                    sum += a[j] + b[j] * j;
                } else {
                    sum += a[j] + b[j] * i;
                }
            }
            t = i + 1;
        }
    }
    int ans = sum;
    for (int i = curr; i < n; i++) {
        sum += a[i] + b[i] * i;
        ans = max(ans, sum);
    }
    cout << ans << endl;
    return 0;
}