#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
using namespace std;

#define ll long long
#define vc vector
#define db double
#define str string
#define its(a) (a).begin(), (a).end()
#define itsr(a) (a).rbegin(), (a).rend()
#define sz(a) (a).size()
#define tstr to_string
#define scan(a) for (auto &_i:a) cin >> _i;

const ll MOD = 998244353;

ll binpow(ll a, ll k) {
	if (k == 0) return 1;
	if (k == 1) return a;
	ll md = binpow(a, k / 2);
	if (k % 2) return md * md % MOD * a % MOD;
	return md * md % MOD;
}
pair<ll, ll> operator + (pair<ll, ll> a, pair<ll, ll> b) {
	return { a.first + b.first, a.second + b.second };
}
const ll INF = 1e18 + 18;
void solve() {
	/*vc<ll> tr;
	auto get = [&](auto get, int v, int l, int r, int lq, int rq) -> ll {
		if (lq <= l and r <= rq) return tr[v];
		if (rq <= l or r <= lq) return 0;
		int mid = (r + l) >> 1;
		return get(get, 2 * v + 1, l, mid, lq, rq) + get(get, 2 * v + 2, mid, r, lq, rq);
	};
	auto build = [&](auto build, int v, int l, int r) -> void {
		if (r - l == 1) {
			tr[v] = a[l];
			return;
		}
		int mid = (r + l) >> 1;
		build(build, 2 * v + 1, l, mid);
		build(build, 2 * v + 2, mid, r);
		tr[v] = tr[2 * v + 1] + tr[2 * v + 2];
	};
	auto st = [&](auto st, int v, int l, int r, int e, int x) -> void {
		if (r - l == 1) {
			tr[v] = x;
			return;
		}
		int mid = (r + l) >> 1;
		if (e < mid) st(st, 2 * v + 1, l, mid, e, x);
		else st(st, 2 * v + 2, mid, r, e, x);
		tr[v] = tr[2 * v + 1] + tr[2 * v + 2];
	};*/
	ll n; cin >> n;
	vc<pair<ll, ll>> a(n);
	ll ans = -INF;
	for (int i = 0; i < n; ++i) {
		cin >> a[i].first >> a[i].second;
	}
	vc<ll> p(n); scan(p);

	vc<pair<ll, ll>> bd(n, { 0,0 });
	for (int i = 0; i < n; ++i) bd[i].second = i;

	ll mxv = 0;
	set<pair<ll, ll>> st;
	for (int i = 0; i < n; ++i) {
		
		if (p[i] != -1) st.insert({ p[i], i+1 });
	}

	for (int i = 0; i < n; ++i) {
		while (sz(st) and (*st.begin()).first == i) {
			mxv = (*st.begin()).second;
			st.erase({ i, (*st.begin()).second });
		}
		bd[i].first = mxv;
	}

	for (int i = 0; i < n; ++i) {
		if (p[i] != -1) st.insert({ p[i]-1, i });
	}
	mxv = n - 1;
	for (int i = n - 1; i >= 0; --i) {
		while (sz(st) and (*st.rbegin()).first == i) {
			mxv = (*st.rbegin()).second;
			st.erase({ i, (*st.rbegin()).second });
		}
		bd[i].second = min(mxv, bd[i].second);
	}


	ll mpi = *max_element(its(p));
	ll cur = 0;
	for (ll i = 0; i < n; ++i) {
		cur += bd[i].first * a[i].second + a[i].first;
		if (i + 1 >= mpi) ans = max(cur, ans);
	}
	cout << ans;
	return;

	vc<vc<ll>> dp(n, vc<ll>(n, -INF));
	for (ll i = bd[0].first; i <= bd[0].second; ++i) dp[0][i] = a[0].second * i + a[0].first;

	for (ll i = 1; i < n; ++i) {
		ll mx = -INF;

		for (ll j = 0; j < bd[i].first; ++j) mx = max(mx, dp[i - 1][j]);
		for (ll j = bd[i].first; j <= bd[i].second; ++j) {
			mx = max(mx, dp[i - 1][j]);
			dp[i][j] = a[i].first + a[i].second * j + mx;
		}
	}
	ans = -INF;


	for (int i = max(0ll, mpi-1); i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			ans = max(ans, dp[i][j]);
		}
	}
	cout << ans << '\n';


	/*vc<ll> tr(4*n);
	vc<pair<ll, ll>> add(4*n);

	auto push = [&](int v, int l, int r) ->void {
		if (r - l == 1) {
			tr[v] += add[v].first;
			add[v] = { 0,0 };
			return;
		}
		int mid = (r + l) >> 1;
		add[2 * v + 1] = add[2 * v + 1] + add[v];
		add[2 * v + 2] = add[2 * v + 2] + pair<ll,ll>({add[v].first + add[v].second * (mid - l), add[v].second});
		tr[v] += (2 * add[v].first + add[v].second * (ll)(r - l - 1)) * n / 2;
		add[v] = { 0,0 };
	};


	auto get = [&](auto get, int v, int l, int r, int lq, int rq) -> ll {
		push(v, l, r);
		if (lq <= l and r <= rq) return tr[v];
		if (rq <= l or r <= lq) return 0;
		int mid = (r + l) >> 1;
		ll ans = get(get, 2 * v + 1, l, mid, lq, rq) + get(get, 2 * v + 2, mid, r, lq, rq);
	};
	auto build = [&](auto build, int v, int l, int r) -> void {
		if (r - l == 1) {
			tr[v] = a[l];
			return;
		}
		int mid = (r + l) >> 1;
		build(build, 2 * v + 1, l, mid);
		build(build, 2 * v + 2, mid, r);
		tr[v] = tr[2 * v + 1] + tr[2 * v + 2];
	};
	auto st = [&](auto st, int v, int l, int r, int e, int x) -> void {
		if (r - l == 1) {
			tr[v] = x;
			return;
		}
		int mid = (r + l) >> 1;
		if (e < mid) st(st, 2 * v + 1, l, mid, e, x);
		else st(st, 2 * v + 2, mid, r, e, x);
		tr[v] = tr[2 * v + 1] + tr[2 * v + 2];
	};*/

}

int main()
{
	cin.tie(0)->sync_with_stdio(0);

	ll _t = 1;
	//cin >> _t;
	while (_t--)
		solve();
}
