#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <set>
#include <stack>

using namespace std;
using ll = long long;

template <class T>
istream& operator>>(istream& in, vector<T>& a) {
	for (T& i : a) {
		in >> i;
	}
	return in;
}

template <class T>
ostream& operator<<(ostream& out, vector<T>& a) {
	for (T& i : a) {
		out << i;
	}
	return out;
}

const ll MOD = 998'244'353;

void solve() {
	ll n; cin >> n;
	vector<ll> a(n);
	vector<ll> b(n);
	for (ll i = 0; i < n; i++) {
		ll a_, b_; cin >> a_ >> b_;
		a[i] = a_;
		b[i] = b_;
	}
	
	vector<ll> pref_a(n + 1, 0);
	for (ll i = 0; i < n; i++) {
		pref_a[i + 1] = pref_a[i] + a[i];
	}
	vector<ll> pref_a_max(n + 1, 0);

	for (ll i = 0; i < n; i++) {
		pref_a_max[i + 1] = max(pref_a_max[i], pref_a[i + 1]);
	}

	ll ans = -1E18;
	ll first;
	ll p;
	for (ll i = 0; i < n; i++) {
		cin >> p;
		if (i == 0) first = p;
	}

	if (first == -1) {
		for (ll i = 1; i <= n; i++) {
			ans = max(ans, pref_a[i]);
		}
	}
	else if (p == -1) {
		for (ll i = first; i <= n; i++) {
			ans = max(ans, pref_a[i]);
		}
	}
	else {
		ans = pref_a[p];
	}
	cout << ans;
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	
	ll t = 1;
	//cin >> t;
	while (t--) {
		solve();
		cout << '\n';
	}
}
