#pragma GCC optimize("O3")
#include <vector>

#pragma GCC target("avx2")
#include <algorithm>
#include <numeric>
#include <iostream>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <set>
#include <map>

using namespace std;

#define ll long long
#define ld long double
#define pll pair<ll, ll>
#define pld pair<ld, ld>

#define vll vector<ll>
#define vvll vector<vll>
#define vvvll vector<vvll>
#define vvvvll vector<vvvll>

#define vld vector<ld>
#define vvld vector<vld>
#define vvvld vector<vvld>
#define vvvvld vector<vvvld>

#define vpll vector<pll>
#define vvpll vector<vpll>
#define vvvpll vector<vvpll>
#define vvvvpll vector<vvvpll>

#define vpld vector<pld>
#define vvpld vector<vpld>
#define vvvpld vector<vvpld>
#define vvvvpld vector<vvvpld>

#define forlr(i, l, r) for(ll i = (l); i < (r); ++i)
#define forn(i, n) forlr(i, 0, n)
#define forin(x, v) for(auto& x : v)

#define all(v) v.begin(), v.end()
#define pb emplace_back

template <class T>
istream& operator>>(istream& in, vector<T>& v) {
	forin(x, v)
		in >> x;
	return in;
}

template <class T>
ostream& operator<<(ostream& out, vector<T>& v) {
	forin(x, v)
		out << x << ' ';
	return out;
}

struct DO {
	ll l, r;
	DO* lp = 0, *rp = 0;
	ll a = -1e18;

	DO(ll l, ll r) : l(l), r(r) {
		if (r - l > 1) {
			lp = new DO(l, (l + r) / 2);
			rp = new DO((l + r) / 2, r);
		}
	}

	ll get(ll lt, ll rt) {
		if (rt <= l || r <= lt)
			return -1e18;
		if (lt <= l && r <= rt)
			return a;
		return max(lp->get(lt, rt), rp->get(lt, rt));
	}

	void mod(ll i, ll x) {
		if (r - l == 1) {
			a = x;
			return;
		}
		(i < (l + r) / 2 ? lp : rp)->mod(i, x);
		a = max(lp->a, rp->a);
	}
};

struct SP {
#ifdef _LOCAL
	ll __lg(ll n) {
		return log2(n);
	}
#endif // _LOCAL

	ll n;
	ll lgn = __lg(n);
	vvll t = vvll(lgn + 1, vll(n));

	SP(vll a) : n(a.size()) {
		t[0] = a;
		forn(i, lgn) {
			forn(j, n - (2ll << i) + 1) {
				t[i + 1][j] = max(t[i][j], t[i][j + (1ll << i)]);
			}
		}
	}

	ll get(ll l, ll r) {
		ll lgd = __lg(r - l);
		return max(t[lgd][l], t[lgd][r - (1ll << lgd)]);
	}
};

void main13() {
	ll n;
	cin >> n;
	vll a(n), b(n);
	forn(i, n)
		cin >> a[i] >> b[i];

	forn(i, n - 1)
		a[i + 1] += a[i];

	//vvll dp(n + 1, vll(n, -1e18)); // j = t[i]
	SP dp(vll(n, 0));

	ll ans = 0;
	forlr(i, 1, n + 1) {
		vll ndp(n, -1e18);
		forn(j, i) {
			ll mx = dp.get(0, j + 1);
			ndp[j] = (mx + j * b[i - 1]);
		}
		dp = SP(ndp);
		ans = max(ans, dp.get(0, i) + a[i - 1]);
	}

	cout << ans;
}

void main0() {
	ll n;
	cin >> n;
	vll a(n), b(n);
	forn(i, n)
		cin >> a[i] >> b[i];

	forn(i, n - 1)
		a[i + 1] += a[i];

	//vvll dp(n + 1, vll(n, -1e18)); // j = t[i]
	SP dp(vll(n, 0));

	vll p(n);
	cin >> p;

	vll l(n), r(n);

	ll pp = 0, pt = 0;
	forn(i, n) {
		if (p[i] == -1)
			continue;

		forlr(j, pp, p[i]) {
			l[j] = min(j, pt);
			r[j] = min(j, i) + 1;
		}

		pp = p[i];
		pt = i + 1;
	}

	forlr(i, pp, n) {
		l[i] = pt;
		r[i] = i + 1;
	}

	ll ans = (pp ? -1e18 : 0ll);

	forlr(i, 1, n + 1) {
		vll ndp(n, -1e18);
		forlr(j, l[i - 1], r[i - 1]) {
			ll mx = dp.get(0, j + 1);
			ndp[j] = (mx + j * b[i - 1]);
		}
		dp = SP(ndp);
		if(i >= pp)
			ans = max(ans, dp.get(0, i) + a[i - 1]);
	}
	
	cout << ans;
}

void main5() {
	ll n;
	cin >> n;
	vll a(n), b(n);
	forn(i, n)
		cin >> a[i] >> b[i];
	vll p(n);
	cin >> p;

	ll at = 0, pp = 0, pt = 0;
	forn(i, n) {
		if (p[i] == -1)
			continue;

		forlr(j, pp, p[i]) {
			at += a[j];	//					 mn  mx
			at += b[j] * min(j, (b[j] >= 0 ? i : pt));
		}

		pp = p[i];
		pt = i + 1;
	}

	ll ans = at;
	forlr(i, pp, n) {
		ans = max(ans, at += a[i] + b[i] * (b[i] >= 0 ? i : pt));
	}
	cout << ans;
}

void main51() {
	ll n;
	cin >> n;
	vll a(n), b(n);
	forn(i, n)
		cin >> a[i] >> b[i];
	vll p(n);
	cin >> p;

	ll pp = 0, pt = 0;

	forn(i, n) {
		if (p[i] == -1)
			continue;

		pp = p[i];
		pt = i;
	}

	ll at = 0;
	forn(i, pp)
		at += a[i] + b[i] * min(i, pt);
	ll ans = at;
	forlr(i, pp, n) {
		ans = max(ans, at += a[i] + b[i] * i);
	}
	cout << ans;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

#ifdef _LOCAL
	while (1)
#endif // _LOCAL
	{
		ll t = 1;
		//cin >> t;
		while (t--) {
			main0();
			cout << endl;
		}
	};
}
