#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define int long long

using namespace std;
using ll = long long;
using vi = vector<int>;
using pi = pair<int, int>;
using ld = long double;

const int INF = 2e18;

void upd(int &x, int y) {
    x = max(x, y);
}

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vi a(n), b(n);

    for (int i = 0; i < n; ++i) cin >> a[i] >> b[i];
    // for (int i = 0; i < n; ++i) cin >> b[i];

    vi p(n);
    for (int i = 0; i < n; ++i) cin >> p[i];

    vi asum(n + 1), bsum(n + 1);

    for (int i = 1; i <= n; ++i) asum[i] = asum[i - 1] + a[i - 1];
    for (int i = 1; i <= n; ++i) bsum[i] = bsum[i - 1] + b[i - 1];

    int mx = -INF;

    int pref = 0;

    int sz = 0;

    for (int i = 0; i < n; ++i) sz += (p[i] != -1);

    vi num(sz);

    int itr = 0;

    for (int i = 0; i < n; ++i) {
        if (p[i] != -1) num[itr++] = i;
    }

    int it = 0;

    int prev = 0;

    int need = 0;

    int took = 0;

    vi bbsum(n + 1);
    for (int i = 0; i < n; ++i) {
        bbsum[i + 1] = bbsum[i] + b[i] * i; 
    } 

    for (int i = 0; i < sz; ++i) {
        int id = num[i];
        need += asum[p[id]] - asum[took];
        need += (bbsum[p[id]] - bbsum[took]);

        // cout << "i " << id << ' ' << p[id] << ' ' << took << ' ' << need << endl;
        took = p[id];
        prev = num[i] + 1;
    }


    int add = 0;
    int bst = 0;

    // cout << "q " << prev << endl;

    for (int i = took; i < n; ++i) {
        add += a[i];
        add += b[i] * i;
        upd(bst, add);
    }

    // cout << "d " << need << endl;

    cout << need + bst;
}