#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <map>
#include <iomanip>
using namespace std;
using ll = long long;
using ld = long double;
#define endl '\n'
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n; cin >> n; vector<int> a(n), b(n);
	for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
	vector<int> p(n);
	for (auto& i : p) cin >> i;
	vector<int> t1(n, 0), t2(n, n);
	for (int i = 0; i < n; i++) t2[i] = i;
	for (int i = 0; i < n; i++) {
		if (p[i] != -1) {
			t2[p[i] - 1] = min(t2[p[i] - 1], i);
			t1[p[i]] = max(t1[p[i]], i + 1);
		}
	}
	for (int i = n - 2; i >= 0; i--) {
		t2[i] = min(t2[i], t2[i + 1]);
	}
	for (int i = 1; i < n; i++) {
		t1[i] = max(t1[i], t1[i - 1]);
	}
	//for (int i = 0; i < n; i++) cout << t1[i] << " " << t2[i] << "      ";
	//cout << endl;
	int gr = *max_element(p.begin(), p.end()) - 1;
	ll ans = -3e18;
	if (gr == -2) ans = 0;
	ll val = 0;
	for (int i = 0; i < n; i++) {
		val += a[i] + b[i] * 1ll * t2[i];
		if (gr <= i) ans = max(ans, val);
	}
	cout << ans;
	return 0;
}
