#include <iostream>
#include <cmath>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <numeric>
#include <iomanip>
#include <random>
#include <chrono>

using namespace std;

using ll = long long;
using lll = __int128;
using db = double;
using ld = long double;

const int INF = 1'000'000'000;
const ll LINF = 1'000'000'000'000'000'000;
const ll MOD = 1'000'000'007;

int rnd() {
    return (rand() / RAND_MAX);
}

int main() {
    ios_base::sync_with_stdio();
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector <ll> a(n), b(n);
    ll sumA = 0, sumB = 0;
    vector <ll> prefB(n + 1, 0);
    vector <ll> prefA(n + 1, 0);
    for (int i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
        prefA[i + 1] = prefA[i] + a[i];
        prefB[i + 1] = prefB[i] + (b[i] * i);
        sumA += a[i];
    }
    vector <int> p(n);
    int minK = 0;
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        minK = max(minK, p[i]);
    }
    ll ans = -LINF;
    for (int k = minK; k <= n; ++k) {
        ans = max(ans, prefA[k]);
    }
    cout << ans;
}