#include <vector>
#include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <iomanip>
#include <string>
#include <cmath>
#include <random>
#include <unordered_map>
#include <deque>
typedef long long ll;
typedef long double ld;
using namespace std;
void solve() {
	ll n; cin >> n;
	vector<pair<ll, ll>>k;
	for (int i = 0; i < n; i++) {
		ll a, b; cin >> a >> b;
		k.push_back({ a, b });
	}
	vector<ll>p(n);
	vector<ll>d(n, n);
	vector<ll>R(n, 0);
	for (int i = 0; i < n; i++)
		R[i] = i;
	ll ind = n, mx = -1, last = 0;
	for (int i = 0; i < n; i++) {
		cin >> p[i];
		if (p[i] == -1)
			continue;
		d[p[i]-1] = min((ll)i, d[p[i]-1]);
		mx = max(mx, p[i] - 1);
	}
	vector<ll>time(n, 1e18);
	for (int i = n - 1; i >= 0; i--) {
		ind = min(ind, d[i]);
		time[i] = min(R[i], ind);
	}
	ll res = 0;
	ll ans = -1e18;
	if (mx == -1) {
		ans = 0; mx = 0;
	}
	for (int i = 0; i < n; i++) {
		ll a = k[i].first, b = k[i].second;
		res += (a + b * time[i]);
		if (i >= mx)
			ans = max(ans, res);
	}
	cout << ans << endl;

}
int main()
{
	cin.tie(nullptr)->sync_with_stdio(false);
	cout.tie(nullptr)->sync_with_stdio(false);

	ll _T = 1;//cin>>_T;
	while (_T--)
		solve();
}