#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <map>



using namespace std;

typedef long long ll;

mt19937 gen;

int rnd(int l, int r) {
	return uniform_int_distribution<int>(l, r)(gen);
}

const int N = 1e6 + 7;

const int M = 998244353;



vector<int> dp[N];
vector<int> g[N];

bool fl[N];

void dfs(int v) {	


}

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cout.precision(30);

	int n;
	cin >> n;

	vector<int> a(n), b(n);
	for (int i = 0; i < n; ++i) {
		cin >> a[i] >> b[i];
	}

	vector<int> p(n);
	for (int i = 0; i < n; ++i) {
		cin >> p[i];
	}

//	vector<ll> dp(n + 1, -1e18);
//	dp[0] = 0;

	ll answer = 0;
	ll res = 0;

	vector<int> mx(n, 0);

	int k = 0;
	int ls = 0;
	for (int i = 0; i < n; ++i) {
		if (p[i] != -1) {
			for (int j = k; j < p[i]; ++j) {
				mx[j] = min(j, i);
			}
			k = p[i];
		}
	}

	for (int j = k; j < n; ++j) {
		mx[j] = j;
	}


	for (int i = 0; i < n; ++i) {
		res += a[i] + b[i] * 1ll * mx[i];
		answer = max(answer, res);
	}


	cout << answer << '\n';
}
