#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <numeric>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <bitset>
#include <random>
#include <queue>
using namespace std;
#pragma GCC target("avx,avx2,tune=native,sse,sse2")
#define int long long

const int mod = 998244353;
const int lg = 6;

inline bool vsuprun() {
	static int it = 0;
	static bool done = false;
	if (it++ == 100) {
		it = 0;
		if (1.0 * clock() / CLOCKS_PER_SEC > 2.0) {
			done = true;
		}
	}
	return done;
}

int binpow(int a, int b) {
	if (b == 0) {
		return 1;
	}
	int t = binpow(a, b / 2);
	if (b % 2 == 0) {
		return (t * t) % mod;
	}
	return (((t * t) % mod) * a) % mod;
}

signed main() {
	int n;
	cin >> n;
	vector<int> a(n);
	vector<int> b(n);
	bool fl_m = 1;
	bool fl_b = 1;
	for (int i = 0; i < n; ++i) {
		cin >> a[i] >> b[i];
		if (b[i] > 0) {
			fl_m = 0;
		}
		if (b[i] < 0) {
			fl_b = 0;
		}
	}
	vector<int> p(n);
	for (int i = 0; i < n; ++i) {
		cin >> p[i];
		if (p[i] != -1) {
			fl_m = 0;
			fl_b = 0;
		}
	}
	if (fl_b == 1) {
		int ans = -2e18;
		int now = 0;
		for (int i = 0; i < n; ++i) {
			now += a[i] + b[i] * i;
			ans = max(ans, now);
		}
		cout << ans << endl;
		return 0;
	}
	if (fl_m == 1) {
		return 89;
		int ans = -2e18;
		int now = 0;
		for (int i = 0; i < n; ++i) {
			now += a[i];
			ans = max(ans, now);
		}
		cout << ans << endl;
		return 0;
	}
	vector<int> l(n, 0);
	vector<int> r(n);
	int ans = -2e18;
	for (int i = 0; i < n; ++i) {
		r[i] = i;
		for (int j = i - 1; j >= 0; --j) {
			if (p[j] != -1) {
				if (p[j] >= (i + 1)) {
					r[i] = min(r[i], j);
				}
				if (p[j] <= i) {
					l[i] = max(l[i], j);
				}
			}
		}
	}
	/*for (int i = 0; i < n; ++i) {
		cout << l[i] << ' ' << r[i] << endl;
	}*/
	vector<vector<int>> dp(n, vector<int>(n, -2e18));
	vector<vector<int>> pref(n, vector<int>(n, -2e18));
	for (int i = l[0]; i <= r[0]; ++i) {
		dp[0][i] = a[0] + b[0] * i;
	}
	for (int i = 0; i < n; ++i) {
		if (i > 0) {
			pref[0][i] = max(pref[0][i - 1], dp[0][i]);
		}
		else {
			pref[0][i] = dp[0][i];
		}
	}
	ans = max(ans, pref[0][n - 1]);
	for (int j = 1; j < n; ++j) {
		for (int i = l[j]; i <= r[j]; ++i) {
			if (pref[j - 1][i] != -2e18) {
				dp[j][i] = pref[j - 1][i] + a[j] + b[j] * i;
			}
		}
		for (int i = 0; i < n; ++i) {
			if (i > 0) {
				pref[j][i] = max(pref[j][i - 1], dp[j][i]);
			}
			else {
				pref[j][i] = dp[j][i];
			}
		}
		ans = max(ans, pref[j][n - 1]);
	}
	/*for (auto i : dp) {
		for (auto j : i) {
			cout << j << ' ';
		}
		cout << endl;
	}
	for (auto i : pref) {
		for (auto j : i) {
			cout << j << ' ';
		}
		cout << endl;
	}*/
	cout << ans << endl;
	return 0;
}