#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define int int64_t
#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvi>
#define pii pair<int, int>
#define vpi vector<pii>
#define vb vector<bool>
#define umap unordered_map
using namespace std;
//using namespace __gnu_pbds;
mt19937 rng(333);
//template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//template<class T> using omset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int inf = 1e18;
void solve5(int n, vi& A, vi& B, vi& p) {
	int ind=-1;
	for (int i=n-1; i>=0; --i) {
		if (p[i] != -1) {
			ind = i;
			break;
		}
	}
	if (ind == -1) {
		int m=0;
		int s=0;
		for (int e : A) {
			s += e;
			m = max(s, m);
		}
		cout << m << '\n';
		return;
	}
	vi T;
	for (int i=0; i<n; ++i) if (p[i] != -1) T.push_back(i);
	int prev=0;
	int s=0;
	int had=0;
	if (T[0] != 1) {
		for (int i=0; i<p[T[0]]; ++i) {
			s += A[i];
		}
		
	}
	for (int i=0; i<T.size()-1; ++i) {
		for (int j=p[T[i]]; j<p[T[i+1]]; ++j) {
			s += A[j]+B[j]*T[i];
		}
	}
	int m = s;
	for (int i=p[T.back()]; i<n; ++i) {
		s += A[i]+B[i]*T.back();
		m = max(m, s);
	}
	cout << m << '\n';
	
	
}
void solve() {
	int n; cin >> n;
	vi A(n, 0); vi B(n, 0);
	for (int i=0; i<n; ++i) cin >> A[i] >> B[i];
	vi p(n, 0); for (int i=0; i<n; ++i) cin >> p[i];
	solve5(n, A, B, p); return;
	vi dp(n+1, 0);
	vi N(n+1, 0);
	//for (int i=0; i<=n; ++i) dp[i]=0; N[i]=0;
	for (int j=1; j<=n; ++j) dp[j] = dp[j-1]+A[j-1];
	if (p[0] != -1) {
		for (int i=0; i<n; ++i) {
			if (p[0] != i) dp[i] = -inf;
		}
	}
	//for (int e : dp) cout << e << ' '; cout << endl;
	for (int i=1; i<n; ++i) {
		N[0] = dp[0];
		for (int j=1; j<=n; ++j) {
			N[j] = dp[j];
			if (j > i) {
				N[j] = max(N[j], N[j-1]+A[j-1]+B[j-1]*i);
			}
		}
		if (p[i] != -1) {
			for (int j=0; j<=n; ++j) {
				if (j != p[i]) N[j] = -inf;
			}
		}
		swap(N, dp);
		//for (int e : dp) cout << e << ' '; cout << endl;
	}
	int m = -inf;
	for (int i=0; i<=n; ++i) m = max(m, dp[i]);
	cout << m << '\n';

}
signed main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	int t=1; //cin >> t;
	while (t--) solve();
	return 0;
}
