#include <iostream>
#include<vector>
#include<string>
#include<numeric>
#include<algorithm>
#include<functional>

using namespace std;

#define int long long
#define db double
#define ll long long
#define pii pair<int, int>
#define tiii tuple<int ,int, int >
#define tiiii tuple<int, int, int, int>
#define tiiiii tuple<int, int, int, int, int>
#define ve vector
#define vi ve<int>
#define vvi ve<vi>
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define cin(a) for(auto &now : a) cin >> now;
#define print(a, s) for(auto now : a) cout << now << s;


void solve() {
	int n; cin >> n;
	vi a(n + 1), b(n + 1);
	vi p(n);
	for (int i = 1; i <= n; ++i) {
		cin >> a[i] >> b[i];
	}
	for (int i = 0; i < n; ++i) cin >> p[i];
	int mx = 0;
	int ans = 0;
	vi nxt(n + 1, -1);
	vi pr; partial_sum(all(a), back_inserter(pr));
	for (int i = n - 1; i >= 0; --i) {
		if (p[i] != -1) nxt[i] = i;
		else nxt[i] = nxt[i + 1];
	}
	vi pp(n); pp[0] = p[0];
	for (int i = 1; i < n; ++i) pp[i] = max(pp[i - 1], p[i]);
	if (*max_element(all(p)) == -1) {
		int ans = 0;
		for (int i = 1; i <= n; ++i) chmax(ans, pr[i]);
		for (int kk = 1; kk <= n; ++kk) {
			if (b[kk] > 0) {
				int res = pr[kk];
				vi v(kk + 1);
				int tsm = 0;
				for (int i = kk; i >= 1; --i) {
					tsm += b[i];
					if (tsm > 0) {
						res += tsm * (i - 1); tsm = 0;
					}
				}
				/*for (int i = kk; i >= 1; --i) {
					v[i] = b[i];
					if (i < kk) v[i] += v[i + 1];
				}
				vi st;
				for (int i = 1; i <= kk; ++i) {
					while (st.size() && v[st.back()] < v[i]) {
						st.pop_back();
					}
					st.push_back(i);
				}
				for (int i = 0; i < st.size(); ++i) {
					if (i == 0) res += v[st[i]] * (st[i] - 1);
					else res += v[st[i]] * (st[i] - st[i - 1]);
				}*/
				chmax(ans, res);
			}
			
			
		}
		cout << ans << "\n";
		/*int res = 0;
		int sm = 0;
		for (int i = 1; i <= n; ++i) {
			if (b[i] <= 0) sm += a[i];
			else sm += a[i] + b[i] * (i - 1);
			chmax(res, sm);
		}
		cout << res;*/
	}
	else {
		for (int i = 1; i <= n; ++i) {
			if (i > pp.back()) {
				mx = i; break;
			}
			ans += a[i] + b[i] * min(i - 1,(int)( lower_bound(all(pp), i) - pp.begin()));
		}
		int res = ans;
		int sm = 0;
		for (int i = mx; i <= n; ++i) {
			sm += a[i] + b[i] * (i - 1);
			chmax(res, ans + sm);
		}
		cout << res;
	}

}
int32_t main() {
	// ios::sync_with_stdio(0);
	// cin.tie(0); cout.tie(0);
	//cout << fixed << setprecision(9);

	int t = 1;
	//cin >> t;
	while (t--) {
		solve();
	}
}


