#include <bits/stdc++.h>

using namespace std;
#define int long long

void bad() {
    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);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
    }
    vector <int> prev(n + 1, 0), prev1(n + 1, 0), mx(n + 1, -2e9);

    for (int i = 0; i < n; ++i) {
        prev[i + 1] = prev[i] + b[i];
        prev1[i + 1] = prev1[i] + a[i] + b[i] * i;
    }
    for (int i = n - 1; i >= 0; --i) {
        mx[i] = max(mx[i + 1], prev1[i]);
    }
    int ind = 0;
    int ans = -2e18, res = 0, counter = 0;
    int last = 0;
    vector <int> bad{};
    for (int i = 0; i < n; ++i) {
        if (p[i]!= - 1) {
            bad.push_back(i);
        }
    }
    vector <int> cool{};
    for (int i = 0; i < bad.size(); ++i) {
        int cur = p[bad[i]];
        int tm = bad[i] + 1;
        cool = {cur};
        for (int j = cur; j >= counter; --j) {
            if (prev[cur] - prev[j] > prev[cur] - prev[cool.back()]) {
                cool.push_back(j);
            }
        }
        for (int j = counter; j < cool.back(); ++j) {
            res += a[j] + last * b[j];
            ++counter;
        }
        for (int t = last; t < tm; ++t) {
            if (cool.back() == t) {
                cool.pop_back();
            }
            if (!cool.empty()) {
                while (counter < cool.back()) {
                    res += a[counter] + t * b[counter];
                    ++counter;
                }
            }
        }
        while (counter < cur) {
            res += a[counter] + (tm - 1) * b[counter];
            ++counter;
        }
        last = tm;
        counter = cur;
    }
    ans = res + mx[counter] - prev1[counter];
    cool = {n};
    for (int j = n - 1; j >= counter; --j) {
        if (prev[n] - prev[j] > prev[n] - prev[cool.back()]) {
            cool.push_back(j);
        }
    }
    for (int j = counter; j < cool.back(); ++j) {
        res += a[j] + last * b[j];
        ++counter;
        ans = max(ans, res + mx[counter] - prev1[counter]);
    }
    for (int t = last; t < n; ++t) {
        if (cool.back() == t) {
            cool.pop_back();
        }
        if (!cool.empty()) {
            while (counter < cool.back()) {
                res += a[counter] + t * b[counter];
                ++counter;
                ans = max(ans, res + mx[counter] - prev1[counter]);
            }
        }
    }
    cout << ans << '\n';
}
signed main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    bad();
}
