#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <array>
using namespace std;
using ll = long long;
#define all(x) x.begin(),x.end()
#define int ll
const ll INF = 1e18;

vector<ll> a, b;
vector<vector<ll>> dp;

void solve() {
	int n;
	cin >> n;
	vector<ll> t(n + 1), e(n + 1, n - 1);
	a.resize(n + 1);
	b.resize(n + 1);
	//dp.resize(n + 1, vector<ll>(n, -INF));
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i] >> b[i];
	}
	vector<int> p(n + 1);
	int mx = 0;
	for (int i = 1; i <= n; i++)
	{
		cin >> p[i];
		mx = max(mx, p[i]);
		if (p[i] != -1) {
			t[p[i] + 1] = i;
			e[p[i]] = i - 1;
		}
	}
	for (int i = 1; i <= n; i++)
	{
		t[i] = max(t[i], t[i - 1]);
	}
	for (int i = n - 1; i >= 1; i--)
	{
		e[i] = min(e[i], i - 1);
		e[i] = min(e[i], e[i + 1]);
	}
	ll ans = -INF, sum = 0;
	bool f = 0;
	for (int i = 0; i <= n; i++)
	{
		if (i > 0) {
			if (b[i] <= 0) {
				sum += a[i] + b[i] * t[i];
			}
			else {
				sum += a[i] + b[i] * e[i];
			}
		}
		if (i >= mx) {
			if (!f) {
				f = 1;
				ans = sum;
			}
			ans = max(ans, sum);
		}
	}


	cout << ans;

}

signed main()
{
#ifdef _DEBUG
	freopen("in.txt", "r", stdin);
#endif // _DEBUG
	ios::sync_with_stdio(0);
	cin.tie(0);
	solve();
}

