#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <array>
#include <unordered_set>
#include <unordered_map>

using namespace std;
using ll = long long;
using ld = long double;

#define int ll

const int INF = 1e18;

struct Node {
    int a, b;
};

signed main() {
    int n;
    cin >> n;

    vector<Node> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i].a >> a[i].b;
    }

    vector<int> p(n, -1);
    for (int i = 0; i < n; ++i){
        int v;
        cin >> v;
        if (v != -1)
            p[v - 1] = i;
    }

    vector<int> dp(n);
    int now = 0, minx = 0;
    for (int i = 0; i < n; ++i) {
        now += a[i].a + a[i].b * minx;
        dp[i] = now;

        if (p[i] != -1)
            minx = i + 2;
    }

    int res = -INF;
    for (int i = n - 1; i >= 0; --i) {
        res = max(res, dp[i]);
        if (p[i] != -1) break;
        if (i == 0) {
            res = max(res, 0ll);
        }
    }

    cout << res;
}