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

using namespace std;
#define int long long

const int inf = 1e18;
struct  node
{
	int a, b;
};

signed main()
{

	int n;
	cin >> n;
	vector<node> a(n);

	for (int i = 0; i < n; i++)
		cin >> a[i].a >> a[i].b;

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

	vector<pair<int, int>> pr;
	for (int i = 0; i < n; i++)
	{
		if (p[i] != -1)
		{
			pr.push_back({ p[i], i });
		}
	}

	vector<int> dp(n, -inf);
	int ans = 0;
	for (int i = 0; i < n; i++)
	{
		int l =  -1 ;
		int r = pr.size() * 1ll -1;
		int jx = 0;
		while (r - l > 1)
		{
			int m = (l + r) / 2;
			if (pr[m].first >= i + 1)
			{
				r = m;
			}
			else
				l = m;
		}
		jx = r;
		if (pr.size() != 0 && pr[jx].first >= i + 1)
		{
			int lx = 0;
			if (jx != 0)
				lx = pr[jx - 1].second + 1;
			else
				lx = 0;
			int rx = min(i, pr[jx].second);
			if (i != 0)
				dp[i] = dp[i - 1] + a[i].a + a[i].b * rx;
			else
				dp[i] = a[i].a  + a[i].b * rx;
			ans = dp[i];
		}
		else
		{
			jx = pr.size();
			int lx = 0;
			if (jx != 0)
				lx = pr[jx - 1].second + 1;
			else
				lx = 0;
			if (i != 0)
				dp[i] = dp[i - 1] + a[i].a + a[i].b * i;
			else
				dp[i] = a[i].a + a[i].b * i;
			ans = max(ans, dp[i]);

		}
	}
	cout << ans << "\n";
}

