#include <iostream>
#include<vector>
#include<set>
#include<map>
#include <cmath>
#include <algorithm>

using namespace std;
long long typedef ll;
ll INF = 1e15;


int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	ll n,a , b, last = -1;
	cin >> n;
	vector<ll> data, add, p, raise(n, -1);
	bool checker = true, testb = true, testantib = true;
	for (ll i = 0; i < n; ++i) {
		cin >> a >> b;
		data.push_back(a);
		add.push_back(b);
		if (b > 0) {
			testb = false;
		}
		else if (b < 0) {
			testantib = false;
		}
	}
	for (ll i = 0; i < n; ++i) {
		cin >> a;
		if (a != -1) {
			checker = false;
			raise[a - 1] = max(raise[a - 1], i);
			last = a - 1;
		}
		
		p.push_back(a);
	}
	vector<vector<ll>> options(n);
	if (testb && n > 5000) {
		ll best = 0, s = 0, pos = 0,c = -1;
		if (checker) { best = 0; }
		else { best = -INF; }
		for (ll i = 0; i < n; ++i) {
			while (pos < n && p[pos] - 1 < i) {
				pos += 1;
			}
			s += (data[i] + (c + 1) * add[i]);
			if (i >= last) {
				best = max(best, s);
			}
			c = max(c, raise[i]);
		}
		cout << best;
	}
	else if (testantib && checker) {
		ll best = 0, s = 0;
		for (ll i = 0; i < n; ++i) {
			s += (data[i] + add[i] * i);
			best = max(best, s);
		}
		cout << best;
	}
	else {
		for (ll i = 0; i < n; ++i) {
			for (ll j = 0; j <= i; ++j) {
				options[i].push_back(data[i] + add[i] * j);
			}
		}
		ll pos = 0;
		ll c = -1;
		vector<ll> prev = { 0 }, cur = {};
		ll best = 0;
		ll gbest;
		if (checker) { gbest = 0; }
		else { gbest = -INF; }
		for (ll i = 0; i < n; ++i) {
			best = prev[0];
			while (pos < n && p[pos] - 1 < i) {
				pos += 1;
			}
			for (ll j = 0; j <= i; ++j) {
				if (j > c && j <= pos) {
					cur.push_back(options[i][j] + best);
					if (i >= last) {
						gbest = max(gbest, cur.back());
					}
				}
				else {
					cur.push_back(-INF);
				}
				if (j < i - 1) {
					best = max(best, prev[j + 1]);
				}
			}
			prev = cur;
			cur.clear();
			c = max(c, raise[i]);
		}
		cout << gbest;
	}
}
