#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;

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

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#define o_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define int long long

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> a(n);
    vector<int> b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    vector<int> p(n, -1);
    int lst = 0;
    vector<int> mn(n);
    vector<int> mx(n);
    for (int i = 0; i < n; i++) {
        mx[i] = i;
    }
    int pos = -1;
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        if (p[i] != -1) {
            for (int y = lst; y <= p[i] - 1; y++) {
                mn[y] = max(mn[y], pos + 1);
                mx[y] = min(mx[y], i);
            }
            lst = p[i];
        }
    }
    for (int i = 1; i < n; i++) {
        mn[i] = max(mn[i], mn[i - 1]);
    }
    for (int i = n - 2; i > -1; i--) {
        mx[i] = min(mx[i], mx[i + 1]);
    }
    int mxm = -1e15;
    if (lst == 0) {
        mxm = 0;
    }
    int answer = 0;
    for (int i = 0; i < n; i++) {
        answer += max(a[i] + b[i] * mn[i], a[i] + b[i] * mx[i]);
        mxm = max(mxm, answer);
    }
    cout << mxm;
}
