#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) {
			for (int j = 0; j <= p[i] - 1; j++) {
				t2[j] = min(t2[j], i);
			}
			for (int j = p[i]; j < n; j++) t1[j] = max(t1[j], 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;
	vector<ll> dp(n + 1, -3e18);
	ll ans = -3e18;
	if (gr == -1) ans = 0;
	for (int i = t1[0]; i <= t2[0]; i++) {
		dp[i] = a[0] + b[0] * 1ll * i; 
		if (gr <= 0) ans = max(ans, dp[i]);
	}
	for (int i = 1; i <= n; i++) dp[i] = max(dp[i], dp[i - 1]);
	for (int j = 1; j < n; j++) {
		vector<ll> dp1(n + 1, -3e18);
		for (int i = t1[j]; i <= t2[j]; i++) {
			dp1[i] = a[j] + b[j] * 1ll * i + dp[i];
			if (gr <= j) ans = max(ans, dp1[i]);
		}
		dp = dp1;
		for (int i = 1; i <= n; i++) dp[i] = max(dp[i], dp[i - 1]);
	}
	cout << ans;
	return 0;
}
