#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> 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;
		}
		for (; i < p[j]; i++) {
			ll mx = -(1e17);
			for (int t = l; t < min(i + 1, j + 1); t++) {
				mx = max(mx, nw[t]);
				ll cnt = a[i] + t * 1ll * b[i];
				nw[t] = mx + cnt;
			}
		}
		nw[j + 1] = -(1e17);
		for (int t = l; t < j + 1; t++) {
			nw[j + 1] = max(nw[j + 1], nw[t]);
		}
		l = j + 1;
		j++;
	}
	//cout << i << " " << l << endl;
	ans = nw[l];
	//cout << ans << endl;
	for (; i < n; i++) {
		ll mx = -(1e17);
		for (int t = l; t < i + 1; t++) {
			mx = max(mx, nw[t]);
			ll cnt = a[i] + t * 1ll * b[i];
			nw[t] = mx + cnt;
			ans = max(ans, nw[t]);
		}
	}
	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();
	}
}
/*

*/