#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <complex>
#include <stdlib.h>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <deque>
#include <queue>
#include <cstring>
#include <string.h>
#include <math.h>
#include <cmath>
#include <chrono>
#include <random>
#include <float.h>
#include <cfloat>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sq(x) (x) * (x)
#define cube(x) (x) * (x) * (x)
using ll = long long;
using db = long double;
const int INF = 1e9 + 7;
const int MOD = 998244353;
const ll LINF = 1LL << 62;
const db EPS = 1e-9;
using namespace std;

ll binpow(ll x, ll n) {
	if (!n) {
		return 1;
	}
	ll bp = binpow(x, n >> 1);
	if (n & 1)
		return (((bp * bp) % MOD) * x) % MOD;
	return (bp * bp) % MOD;
}

inline void solve() {

	int n;
	cin >> n;

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

	vector <int> p(n);
	for (int i = 0; i < n; i++) {
		cin >> p[i];
	}
	vector <int> tim(n, INF);
	// dp[p_i][t] = best eff
	vector <vector <ll>> dp(n, vector <ll>(n, -LINF));
	int mink = 0;
	for (int i = 0; i < n; i++) {
		if (p[i] != -1) {
			for (int j = p[i]; j < n; j++) {
				for (int t = 0; t <= i; t++) {
					dp[j][t] = LINF;
				}
			}
			for (int j = 0; j < p[i]; j++) {
				for (int t = i + 1; t < n; t++) {
					dp[j][t] = LINF;
				}
			}
			mink = max(mink, p[i]);
		}
	}
	mink--;
	ll best = -INF;
	dp[0][0] = vec[0].first;
	for (int i = 0; i < n - 1; i++) {
		for (int t = 0; t <= i; t++) {
			if (dp[i][t] != -LINF) {
				for (int next = t; next <= i + 1; next++) {
					if (dp[i + 1][next] != LINF) {
						dp[i + 1][next] = max(dp[i + 1][next], dp[i][t] + (vec[i + 1].first + vec[i + 1].second * next));
						if (i + 1 >= mink) {
							best = max(best, dp[i + 1][next]);
						}
					}
				}
			}
		}
	}

	cout << best;

}

signed main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cout.setf(ios_base::fixed); cout.precision(8);

	solve();
	return 0;
}