#define _CRT_SECURE_NO_WARNINGS

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

using namespace std;
using ll = long long;
using ld = long double;

const ll mod = 998244353;
const ll INF = 1e18 + 10;
const int SZ = 1e6 + 1;

int main()
{
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	int n;
	cin >> n;
	vector<ll> a(n + 1), b(n + 1);
	vector<int> p(n + 1);
	vector<ll> dp(n + 1, 0);
	for (int i = 1; i <= n; i++)
		cin >> a[i] >> b[i];
	for (int i = 1; i <= n; i++)
		cin >> p[i];

	vector<pair<ll, ll>> ps;
	ps.push_back({ n, n });
	for (int i = n; i >= 1; i--)
		if (p[i] != -1)
			ps.push_back({ p[i], i });
	int t = 0;
	ll var = 0, ans = 0;
	int i = 1;
	while (!ps.empty()) {
		while (i <= ps.back().first) {
			var += a[i] + b[i] * min((int)ps.back().second - 1, i - 1);
			ans = max(ans, var);
			i++;
		}
		if (ps.back().second == n) {
			break;
		}
		ans = var;
		t = ps.back().second;
		ps.pop_back();
	}
	cout << ans;
}