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


using namespace std;
using ll = long long;

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(0);

	int n;
	cin >> n;
	vector<ll> a(n), b(n);
	for (int i = 0; i < n; ++i) {
		cin >> a[i] >> b[i];
	}
	vector<int> p(n), mx(n, -1);
	for (int i = 0; i < n; ++i) {
		cin >> p[i];
	}
	for (int i = n - 1; i >= 0; --i) {
		if (p[i] == -1) {
			if (i + 1 < n) {
				mx[i] = mx[i + 1];
			}
		}
		else {
			mx[i] = p[i];
		}
	}
	int cr = 0;
	ll rs = 0;
	int ti = -1, tt = 0;
	for (int i = 0; i < n; ++i) {
		while ((mx[i] != -1 && cr < i) || (mx[i] != -1 && cr < mx[i])) {
			rs += a[cr] + b[cr] * i;
			++cr;
		}
		if (p[i] != -1) {
			ti = i;
			tt = p[i];
		}
	}
	ll an = rs;
	for (int i = tt + 1; i <= n; ++i) {
		rs += a[i - 1] + b[i - 1] * (ti + 1);
		an = max(an, rs);
	}
	cout << an;

	return 0;
}
