
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <queue>
#include <iomanip>
#define int long long
using namespace std;

int mod = 998244353;

int bn(int a, int p) {
	if (p == 0) {
		return 1;
	}
	if (p % 2 == 0) {
		int T = bn(a, p / 2) % mod;
		return T * T % mod;
	}
	return bn(a, p - 1) * a % mod;
}

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n; cin >> n;
	vector<int> a(n);
	vector<int> b(n);
	vector<int> p(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i] >> b[i];
	}
	int T = 0;
	for (int i = 0; i < n; i++) {
		cin >> p[i];
		T = max(T, p[i]);
	}
	vector<int> grr(n);
	vector<int> grl(n);
	for (int i = 0; i < n; i++) {
		grr[i] = i;
	}
	int R = 0;
	for (int i = 0; i < n; i++) {
		if (p[i] != -1) {
			for (int j = R; j < p[i]; j++) {
				grr[j] = min(grr[j], i);
				R++;
			}
		}
	}
	int L = n - 1;
	for (int i = n - 1; i >= 0; i--) {
		if (p[i] != -1) {
			for (int j = L; j >= p[i]; j--) {
				grl[j] = max(grl[j], i + 1);
				L--;
			}
		}
	}
	vector<int> dp(n + 1, -1e18);
	dp[0] = 0;
	for (int i = 1; i <= n; i++) {
		dp[i] = dp[i - 1] + a[i - 1] + grl[i - 1] * b[i - 1];
	}
	int ans = -1e18;
	for (int i = T; i <= n; i++) {
		ans = max(ans, dp[i]);
	}
	cout << ans;
}