#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <iomanip>
#include <deque>
#include <queue>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <random>
using namespace std;

#define ll long long
#define ld long double
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
mt19937 rnd(time(0));
const ll inf = 1e18;
vector<ll> a, b;


ll get_ans(int i, int t) {
	return a[i] + t * b[i];
}

void solve() {
	
	int n;
	cin >> n;
	a.resize(n);
	b.resize(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i] >> b[i];
	}
	vector<int> times(n + 1,-1);
	for (int i = 0; i < n; i++) {
		int p;
		cin >> p;
		if (p == -1) continue;
		times[i + 1] = p;
	}
	vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, -inf));
	dp[0][0] = 0;

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (j >= i)
				dp[i][j] = max(dp[i - 1][j - 1], dp[i][j - 1]) + get_ans(j - 1, i - 1);
			dp[i][j] = max(dp[i][j], dp[i - 1][j]);
		}
		
		if (times[i] != -1) {
			for (int v = 0; v <= n; v++) {
				if (v != times[i]) dp[i][v] = -inf;
			}
		}
	}
	ll mx = -inf;
	for (int k = 0; k <= n; k++) {
		mx = max(mx, dp[n][k]);
	}
	
	cout << mx;
}

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	solve();
}