#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int N = 5003;
const __int128 INF = 1e30;

__int128 a[N];
__int128 b[N];
int p[N];

__int128 dp[N][N];
ll mx[N];
ll ogr[N];


int main() {
	ios_base::sync_with_stdio(false);
	cout.tie(0);
	cin.tie(0);


	//for (int i = 0; i < N; i++) {
	//	ogr[i] = INF;
	//}
	
	int n;
	cin >> n;

	for (int i = 1; i <= n; i++) {
		cin >> a[i] >> b[i];
	}

	for (int i = 1; i <= n; i++) {
		cin >> p[i];
		if (p[i] == -1) continue;
		ogr[p[i]] = min(ogr[p[i]], (ll)i);
		mx[p[i] + 1] = max(mx[p[i] + 1], (ll)(i));
	}

	for (int i = 1; i < N; i++) {
		mx[i] = max(mx[i], mx[i - 1]);
	}

	for (int i = 1; i <= n; i++) {
		for (int j = 0; j <= n; j++) {
			dp[i][j] = -INF;
		}
	}

	for (ll i = 1; i <= n; i++) {
		for (ll j = 0; j < i; j++) {
			__int128 tmp = -INF;
			for (int k = 0; k <= j; k++) {
				tmp = max(tmp, dp[i - 1][k]);
			}

			dp[i][j] = max(dp[i][j], tmp + a[i] + b[i] * j);
		}
	}

	__int128 ans = -INF;
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j < i; j++) {
			ans = max(ans, dp[i][j]);
		}
	}

	if (ans < 0) {
		cout << '-';
	}

	ans = abs(ans);
	vector<int> ch;
	while (ans > 0) {
		ch.push_back(ans % 10);
		ans /= 10;
	}
	
	reverse(ch.begin(), ch.end());
	for (auto i : ch) {
		cout << i;
	}

	return 0;
}