#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>


using namespace std;
using ll = long long;

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(0);

	int n;
	cin >> n;
	vector<ll> a(n), b(n);
	for (int i = 0; i < n; ++i) {
		cin >> a[i] >> b[i];
	}
	vector<ll> p(n);
	for (int i = 0; i < n; ++i) {
		cin >> p[i];
	}
	vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, -1e18));
	dp[0][0] = 0;
	ll rs = -1e18;
	for (int i = 0; i < n; ++i) {
		for (int j = i; j < n; ++j) {
			dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + (a[j] + b[j] * i));
		}
		for (int j = 0; j <= n; ++j) {
			if (p[i] == -1 || p[i] == j) {
				dp[i + 1][j] = dp[i][j];
			}
		}
	}
	cout << *max_element(dp[n].begin(), dp[n].end());

	return 0;
}
