#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
#define ll long long
#define fir first
#define sec second
#define ld long double
#define con continue
#define al(a) a.begin(), a.end()
#define size(a) a.size()
#define fastio ios_base::sync_with_stdio(0); cin.tie(0)
ll mod = 998244353;



int main() {
	int n;
	cin >> n;
	vector<ll> a(n + 1, 0), b(n + 1, 0);
	bool g56 = 1;
	for (int i = 1; i <= n; i++) {
		cin >> a[i] >> b[i];
		if (b[i] > 0) {
			g56 = 0;
		}
	}
	vector<ll> p(n+1, 0);
	bool pminus = 1;
	for (int i = 1; i <= n; i++) {
		cin >> p[i];
		if (p[i] != -1) {
			pminus = 0;
		}
	}
	ll ans = 0;
	if (pminus && g56) {																		//+7
		for (int i = 1; i <= n; i++) {
			ans += a[i];
		}
		cout << ans << endl;
		return 0;
	}
}