#define _GLIBCXX_FILESYSTEM
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 5e3 + 10;
long long a[MAXN], b[MAXN];
int l[MAXN], r[MAXN], p[MAXN];

long long dp[MAXN][MAXN];
bool v[MAXN][MAXN];

long long f(int n, int t) {
    if (n == 0) return 0;

    if (v[n][t]) return dp[n][t];
    v[n][t] = 1;

    dp[n][t] = -1e18;

    if (t) dp[n][t] = max(dp[n][t], f(n, t - 1));
    if (l[n] <= t && t <= r[n]) dp[n][t] = max(dp[n][t], f(n - 1, t) + a[n] * t + b[n]);

    return dp[n][t];
}

int main() {

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

    int n;
    cin >> n;

    for (int i = 1; i <= n; i++) {
        cin >> b[i] >> a[i];
    }

    
    for (int i = 1; i <= n; i++) {
        cin >> p[i];
    }
    
    int last = 0;
    
    for (int i = 1; i <= n; i++) {
        l[i] = 0;
        r[i] = i - 1;
    }

    for (int i = 1; i <= n; i++) {
        if (p[i] == -1) continue;

        for (int idx = p[last] + 1; idx <= p[i]; idx++) {
            l[idx] = last;
            r[idx] = min(r[idx], i - 1);
        }

        last = i;
    }

    long long ans = -1e18, current = 0;

    for (int i = 1; i <= n; i++) {
        current += a[i] * l[i] + b[i];
        if (i >= p[last]) ans = max(ans, current);
    }

    cout << ans << '\n';
    return 0;

}