#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <map>

using namespace std;
using ll = long long;

constexpr int INF = 1e9 + 7;
constexpr ll LINF = 1e18 + 7;
constexpr int MOD = 998244353;
constexpr int SZ = 1e6 + 1;
vector<int>g[SZ];
vector<int>x;



int p[SZ];
int bb[SZ];
int cntbb[SZ];
int cnt[SZ];
vector<int>et;
vector<int>fet;
int tin[SZ];
int ftin[SZ];
int tout[SZ];
void dfs(int v, int pr) {
	cntbb[v] = bb[v];
	cnt[v] = 1;
	tin[v] = et.size();
	ftin[v] = fet.size();
	et.push_back(v);
	fet.push_back(v);
	for (int u : g[v]) {
		if (u == pr) continue;
		dfs(u, v);
		cntbb[v] += cntbb[u];
		cnt[v] += cnt[u];
		fet.push_back(v);
	}
	tout[v] = et.size();

}
ll binpow(ll s, ll n) {
	if (n == 0) return 1;
	if (n & 1) return binpow(s, n - 1) * s % MOD;
	ll d = binpow(s, n / 2);
	return d * d % MOD;
}

int st[20][SZ];
int lg[SZ];
void build() {
	for (int i = 2; i < SZ; ++i) {
		lg[i] = lg[i / 2] + 1;
	}
	for (int i = 0; i < fet.size(); ++i) {
		st[0][i] = fet[i];
	}
	for (int i = 1; i < 20; ++i) {
		for (int j = 0; j + (1 << i) <= fet.size(); ++j) {
			if (tin[st[i - 1][j]] < tin[st[i - 1][j + (1 << (i - 1))]]) {
				st[i][j] = st[i - 1][j];
			}
			else {
				st[i][j] = st[i - 1][j + (1 << (i - 1))];
			}
		}
	}
}

int getlca(int u, int v) {
	int l = ftin[u], r = ftin[v];
	if (r < l) swap(l, r); ++r;
	if (tin[st[lg[r - l]][l]] < tin[st[lg[r - l]][r - (1 << lg[r - l])]]) {
		return st[lg[r - l]][l];
	}
	else {
		return st[lg[r - l]][r - (1 << lg[r - l])];
	}
}
int n;
int ans = 0;

bool check(vector<int>& cur) {
	vector<int>vert(n, -1);
	// for (auto x : cur) cout <<x<<' ';
	// cout <<'\n';
	for (int i = 0; i < n; ++i) {
		int c = cur[i];
		if (c == -1) continue;
		if (vert[c] == -1) vert[c] = i;
		vert[c] = getlca(vert[c], i);
	}
	// for (auto x : vert) cout <<x<<' ';
	// cout <<"\n";
	for (int v : x) {
		if (vert[v] != v) return false;
	}
	return true;

}

void rec(int i, vector<vector<int>>& pos, vector<int>& cur) {
	if (i == n) {
		if (check(cur))++ans;
		return;
	}
	for (int v : pos[i]) {
		cur[i] = v;
		rec(i + 1, pos, cur);
	}
}


int main() {
	cin.tie(0)->sync_with_stdio(0);
	int n;
	cin >> n;
	vector<pair<ll, ll>>a(n);
	for (int i = 0; i < n; ++i) cin >> a[i].first >> a[i].second;
	vector<int>p(n);
	for (int i = 0; i < n; ++i) cin >> p[i];
	vector<vector<ll>>dp(n + 1, vector<ll>(n + 1, -LINF));
	dp[0][0] = 0;
	ll mx = -LINF;
	int j = 0;
	vector<vector<ll>>prefs(n + 1, vector<ll>(n + 1));
	for (int i = 1; i <= n; ++i) {
		for (int t = 0; t < i; ++t) {
			dp[i][t] = max(dp[i][t], prefs[i - 1][t] + a[i - 1].first + a[i - 1].second * t);
		}
		while (j < n && p[j] < i)++j;
			for (int t = 0; t < n; ++t) {
				if (t > j) {
					dp[i][t] = -LINF;
				}
			}
			if (j < n && p[j] == i) {
				for (int t = 0; t < j; ++t) {
					dp[i][t] = -LINF;
				}
			}
		prefs[i][0] = dp[i][0];
		for (int t = 1; t < n; ++t) {
			prefs[i][t] = max(prefs[i][t - 1], dp[i][t]);
		}
	}
	int lstt = 0;
	for (int i = 0; i < n; ++i) {
		if (p[i] != -1) lstt = i;
	}

	for (int t = lstt; t <= n; ++t) {
		for (int i = max(0, p[lstt]); i <= n; ++i) {
			mx = max(mx, dp[i][t]);
		}
	}
	
	cout << mx << '\n';
}
