#include <bits/stdc++.h>

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2,popcnt")
#pragma GCC target("tune=native")

using namespace std;

#define int long long
#define sz(x) (int)x.size()
#define eb emplace_back
#define vi vector<int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define vvi vector<vi>
#define vpi vector<pair<int, int> >
#define pi pair<int, int>
#define vvvi vector<vvi>

void solve_a() {
    int n, q; cin >> n >> q;
    vi a(n);
    set<int> st;
    for (int &x : a) {
        cin >> x;
        st.insert(x);
    }
    vi b(all(st));
    for (int &x : a) x = lower_bound(all(b), x) - b.begin();
    vi ans(n, 0);
    ans[0] = 1;
    vi v(n, 1);
    v[0] = 0;
    int l = a[0];
    for (int i = 1; i < n; ++i) {
        bool f = 0;
        for (int j = 0; j < n; ++j) {
            if (v[j] && a[j] != l) {
                ans[j] = i + 1;
                l = a[j];
                v[j] = 0;
                f = 1;
                break;
            }
        }
        if (!f) {
            int x = i + 1;
            for (int j = 0; j < n; ++j) {
                if (v[j]) ans[j] = x++;
            }
            break;
        }
    }
    while (q--) {
        int t; cin >> t;
        if (t == 1) {
            int x; cin >> x;
            if (a[x] == a[x - 1]) {
                swap(a[x], a[x - 1]);
                continue;
            }
            swap(a[x], a[x - 1]);
        } else {
            int x; cin >> x;
            --x;
            cout << ans[x] << endl;
        }
    }
}

void solve_d() {
    int n; cin >> n;
    vpi v(n);
    int s = 0;
    for (auto &[x, y] : v) {
        cin >> x >> y;
        s += x;
    }
    vi p(n);
    for (int &x : p) cin >> x;
    cout << max(0LL, s) << endl;
}

signed main() {
   //freopen("C:/work/untitled1/input.txt", "r", stdin);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    //cin >> t;
    while (t--) solve_d();
}