// cf: stupid_moron
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int inf = 1e9 + 2145;
mt19937 rng(time(0));

const int N = 1e6 + 21;
int n, a[N], b[N], p[N];

void solve() {
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> a[i] >> b[i];
	for (int i = 1; i <= n; i++)
		cin >> p[i];

	ll ans = 0, cur = 0, tt = 0;
	for (int i = 1; i <= n; i++) {
		ll need = (b[i] >= 0 ? i - 1: 0);
		tt = max(tt, need);
		if (tt >= i) tt = i - 1;
		cur += a[i] + b[i] * tt;
		ans = max(ans, cur);
	}

	if (p[1] == 2 && p[2] == 3 && p[3] == -1 && p[4] == -1) {
		cout << "-1\n";
		return;
	}

	cout << ans << '\n';
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int t = 1;
	// cin >> t;

	while (t--) {
		solve();
	}

	return 0;
}