#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <map>



using namespace std;

typedef long long ll;

mt19937 gen;

int rnd(int l, int r) {
	return uniform_int_distribution<int>(l, r)(gen);
}

const int N = 1e6 + 7;

const int M = 998244353;



vector<int> dp[N];
vector<int> g[N];

bool fl[N];

void dfs(int v) {	


}

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cout.precision(30);

	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 (int i = 0; i < n; ++i) {
		cin >> p[i];
		--p[i];
	}

	vector<vector<ll>> dp(n + 1, vector<ll>(n, -1e18));
	dp[0][0] = 0;

	vector<int> lt(n, 0), rt(n, n);
	for (int i = 0; i < n; ++i) {
		if (p[i] >= 0) {
			for (int j = p[i] + 1; j < n; ++j) {
				lt[j] = i;
			}
			for (int j = 0; j <= p[i]; ++j) {
				rt[j] = min(rt[j], i);
			}
		}
	}

	for (int i = 0; i < n; ++i) {
		ll mx = -1e18;
		for (int j = 0; j <= i; ++j) {
			mx = max(mx, dp[i][j]);
			if (j >= lt[i] && j <= rt[i]) {
				dp[i + 1][j] = mx + a[i] + j * 1ll * b[i];
			}
		}
	}

	ll ans = -1e18;
	int mx = 0;
	for (int i = 0; i < n; ++i) {
		mx = max(p[i] + 1, mx);
	}
	for (int i = mx; i <= n; ++i) {
		for (int j = 0; j < n; ++j) {
			ans = max(ans, dp[i][j]);
		}
	}
	cout << ans << '\n';
}
