#include <iostream>
#include <map>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <stack>
#include <deque>
#include <numeric>
#include <iomanip>
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;

void solve() {
	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);
	for (int i = 0; i < n; i++) {
		cin >> p[i];
	}
	vector<ll> psum = { 0 };
	for (int i = 0; i < n; i++) {
		psum.push_back(psum.back() + b[i]);
	}
	vector<ll> nw(n + 1, -(1e17));
	nw[0] = 0;
	ll ans = -(1e17);
	int l = 0;
	int j = 0;
	int i = 0;
	while (j < n) {
		if (p[j] == -1) {
			j++;
			continue;
		}
		ll prev = nw[l];
		for (; i < p[j]; i++) {
			prev += a[i];
			if (b[i] > 0) {
				prev += min(i, j) * 1ll * b[i];
			}
			else {
				prev += l * 1ll * b[i];
			}
		}
		nw[j + 1] = prev;
		l = j + 1;
		j++;
	}
	//cout << i << " " << l << endl;
	ans = nw[l];
	ll prev = ans;
	for (; i < n; i++) {
		prev += a[i];
		if (b[i] < 0) {
			prev += l * 1ll * b[i];
		}
		else {
			prev += i * 1ll * b[i];
		}
		ans = max(ans, prev);
	}
	cout << ans << endl;
}
signed main() {
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	//cout << fixed << setprecision(6);
	int t = 1;
	//cin >> t;
	while (t--) {
		solve();
	}
}
/*

*/