#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <set>
#include <stack>

using namespace std;
using ll = long long;

template <class T>
istream& operator>>(istream& in, vector<T>& a) {
	for (T& i : a) {
		in >> i;
	}
	return in;
}

template <class T>
ostream& operator<<(ostream& out, vector<T>& a) {
	for (T& i : a) {
		out << i;
	}
	return out;
}

const ll MOD = 998'244'353;

void solve() {
	ll n; cin >> n;
	vector<ll> a(n);
	vector<ll> b(n);
	for (ll i = 0; i < n; i++) {
		ll a_, b_; cin >> a_ >> b_;
		a[i] = a_;
		b[i] = b_;
	}
	
	vector<ll> pref_a(n + 1, 0);
	for (ll i = 0; i < n; i++) {
		pref_a[i + 1] = pref_a[i] + a[i];
	}
	vector<ll> pref_a_max(n + 1, 0);

	for (ll i = 0; i < n; i++) {
		pref_a_max[i + 1] = max(pref_a_max[i], pref_a[i + 1]);
	}

	ll ans = 0;
	ll ans_best = 0;
	ll cur = 0;
	for (ll i = 0; i < n; i++) {
		ll p; cin >> p;
		if (p == -1) {
			ans_best = ans;
			for (; cur < n; cur++) {
				ans += a[cur] + i * b[cur];
				ans_best = max(ans_best, ans);
			}
			break;
		}
		for (; cur < p; cur++) {
			ans += a[cur] + i * b[cur];
		}
	}
	cout << ans;
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	
	ll t = 1;
	//cin >> t;
	while (t--) {
		solve();
		cout << '\n';
	}
}
