#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <set>
#include <stack>

using namespace std;
using ll = long long;

template <class T>
istream& operator>>(istream& in, vector<T>& a) {
	for (T& i : a) {
		in >> i;
	}
	return in;
}

template <class T>
ostream& operator<<(ostream& out, vector<T>& a) {
	for (T& i : a) {
		out << i;
	}
	return out;
}

const ll MOD = 998'244'353;

void solve() {
	ll n; cin >> n;
	vector<ll> a(n);
	vector<ll> b(n);
	for (ll i = 0; i < n; i++) {
		ll a_, b_; cin >> a_ >> b_;
		a[i] = a_;
		b[i] = b_;
	}

	ll ans = 0;
	ll ans_best;
	ll cur = 0;
	vector<ll> p(n); cin >> p;
	vector<ll> next(n, 0);
	ll last = -1;
	for (ll i = n - 1; i >= 0; i--) {
		if (p[i] == -1) {
			if (last != -1) {
				next[i] = last;
			}
		}
		else {
			last = i;
		}
	}
	for (ll i = 0; i < n; i++) {
		if (p[i] == -1 && next[i] == 0) {
			ans_best = ans;
			for (; cur < n; cur++) {
				ans += a[cur] + next[i] * b[cur];
				ans_best = max(ans_best, ans);
			}
			break;
		}
		else if (p[i] == -1){
			ans += a[cur] + i * b[cur];
			continue;
		}
		for (; cur < p[i]; cur++) {
			ans += a[cur] + i * b[cur];
		}
	}
	if (p.back() != -1) {
		ans_best = ans;
	}
	cout << ans_best;
}

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

	ll t = 1;
	//cin >> t;
	while (t--) {
		solve();
		cout << '\n';
	}
}
