#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using i128 = __int128_t;

const ll inf = 1e18 + 1e16;
const ll inf_t = 1e9 + 7;
const ll mod = 998244353;

#define int ll

void solve() {
    int k;
    cin >> k;

    vector <int> a(k);
    vector <int> b(k);

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

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

    int last_time = 1;
    int idx = 0;
    for (int i = 0; i < k; ++i) {
        if (p[i] == -1) continue;
        idx = p[i];
        last_time = max(i, 1ll);
    }

    int ans_over = -inf;
    for (int n = last_time; n <= k; ++n) {
        int nxt[n + 1];
        nxt[n] = n;
        for (int i = n - 1; i >= 0; --i) {
            if (p[i + 1] != -1) {
                nxt[i] = i + 1;
            } else {
                nxt[i] = nxt[i + 1];
            }
        }

        vector <int> pref(n + 1);
        pref[0] = 0;
        for (int i = 1; i <= n; ++i) pref[i] = pref[i - 1] + b[i - 1];

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

        int time = 0;
        int ans = 0;
        int pos = 0;
        int answer = -inf;

        for (int i = 0; i < ind.size(); ++i) {
            while (time < ind[i]) {
                if (pos == p[ind[i]]) {
                    ++time;
                    continue;
                }
                if (pos == time) {
                    ans += a[pos] + time * b[pos];
                    if (time >= last_time) answer = max(ans, answer);
                    ++pos;
                    continue;
                }
                if (pref[p[ind[i]]] - pref[pos] < 0) {
                    ans += a[pos] + time * b[pos];
                    if (time >= last_time) answer = max(ans, answer);
                    ++pos;
                } else {
                    ++time;
                }
            }
            while (pos < p[ind[i]]) {
                ans += a[pos] + time * b[pos];
                if (time >= last_time) answer = max(ans, answer);
                ++pos;
            }
        }

        ans_over = max(answer, ans_over);
    }
    cout << ans_over;
}

signed main() {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

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