#include <iostream>
#include <vector>
#include <string>
#include <numeric>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <random>
#include <cmath>

using namespace std;

#pragma GCC optimize("O3")
#pragma GCC target("avx2")

#define int long long
#define all(a) a.begin(),a.end()
#define endl '\n'
#define pii pair<int, int>
#define pb emplace_back
typedef long double ld;

int const N = 2e6, M = 5e3 + 5, INF = 2e18;
int mod = 998244353, mod2 = 1e9 + 9;
int P = 1e9 + 7;

int add(int a, int b) {
	return (a + b) % mod2;
}

int mul(int a, int b) {
	return (a * b) % mod2;
}

int sub(int a, int b) {
	a -= b;
	if (a < 0) a += mod2;
	return a;
}

int binpow(int x, int p) {
	if (p == 0) return 1;
	int res = binpow(x, p / 2);
	if (p % 2 == 0) {
		return res * res % mod;
	}
	return (res * res % mod) * x % mod;
}


int a[N], b[N], p[N];
int dp[M][M];
int last[N];
int tree[4 * N];
int s = (1ll << 13);
pii bord[N];

int find(int lq, int rq, int l = 0, int r = s, int ind = 1) {
	if (r <= lq || rq <= l) {
		return -INF;
	}
	if (lq <= l && r <= rq) {
		return tree[ind];
	}
	int mid = (r + l) / 2;
	return max(find(lq, rq, l, mid, ind * 2), find(lq, rq, mid, r, ind * 2 + 1));
}

int32_t main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n; cin >> n;
	fill(tree, tree + 4 * N, -INF);
	for (int i = 1; i <= n; ++i) cin >> a[i] >> b[i];
	int mx = -1, mn = 1;

	for (int i = 1; i <= n; ++i) {
		mx = max(mx, b[i]);
		mn = min(mn, b[i]);
	}
	for (int i = 1; i < M; ++i) {
		last[i] = -INF;
		for (int j = 0; j < M; ++j) {
			dp[i][j] = -INF;
		}
	}

	for (int i = 0; i < n; ++i) cin >> p[i];
	for (int i = 1; i <= n; ++i) {
		bord[i] = { 0, i };
	}
	int lastp = 0; //lastcnt
	int lastt = -1;
	int cntp = 0;
	int lastneed = 0;
	for (int i = 0; i < n; ++i) {
		lastneed = max(lastneed, p[i]);
		if (p[i] == -1) {
			cntp++;
			continue;
		}
		for (int j = lastp + 1; j <= p[i]; ++j) {
			bord[j] = { lastt + 1, i + 1 };
		}
		lastt = i;
		lastp = p[i];
	}

	int ans = 0;
	//if (cntp == n) ans = 0;
	dp[1][0] = a[1];
	last[0] = 0;
	
	for (int i = 1; i <= n; ++i) {
		int mx = -INF;
		for (int t = bord[i].first; t < bord[i].second; ++t) {
			dp[i][t] = last[t] + a[i] + b[i] * t;
			mx = max(mx, dp[i][t]);
		}
		if (i <= lastneed) ans = mx;
		else ans = max(ans, mx);
		last[0] = dp[i][0];
		for (int t = 1; t <= i; ++t) {
			last[t] = max(last[t - 1], dp[i][t]);
		}
	}

	cout << ans << endl;

	return 0;
	if (mx <= 0) {
		int sum = 0;
		int cur = 0;
		int ans = 0;
		for (int i = 0; i < n; ++i) {
			cur += a[i];
			ans = max(ans, cur);
		}
		cout << ans;
	}
	else if (mn >= 0) {
		int sum = 0;
		int cur = 0;
		int ans = 0;
		for (int i = 0; i < n; ++i) {
			cur += a[i] + b[i] * (i);
			ans = max(ans, cur);
		}
		cout << ans;
	}
	else {
		int sum = 0;
		int cur = 0;
		int ans = 0;
		for (int i = 0; i < n; ++i) {
			if (b[i] >= 0) cur += a[i] + b[i] * (i);
			else cur += a[i];
			ans = max(ans, cur);
		}
		cout << ans;
	}

	return 0;
}