#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define f first
#define s second
#define pb push_back
#define pf push_front
#define in insert
#define elif else if
#define nl '\n'

typedef __int128 int128;
typedef long double ld;
#define  long  int64_t
#define ulong uint64_t
#define uint  uint32_t

using namespace std;
using namespace __gnu_pbds;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());

template <class T>
using oset = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;

#define int long

const int N = 1e6 + 9, oo = 1e18;

int n, p[N], a[N], b[N], l[N], r[N];

int32_t main() {
    cin.tie(0); ios::sync_with_stdio(0);
    cin >> n;
    for (int i = 1; i <= n; ++i)
        cin >> a[i] >> b[i];
    iota(r + 1, r + n + 1, 0);
    int mn = 0;
    for (int i = 1; i <= n; ++i) {
        cin >> p[i];
        if (p[i] == -1)
            continue;
        r[p[i]] = i - 1;
        l[p[i] + 1] = i;
        mn = p[i];
    }
    for (int i = 2; i <= n; ++i)
        l[i] = max(l[i - 1], l[i]);
    for (int i = n - 1; i >= 1; --i)
        r[i] = min(r[i + 1], r[i]);
//    for (int i = 1; i <= n; ++i)
//        cout << l[i] << " " << r[i] << nl;
    int dp[n + 1][n];
    for (int i = 0; i <= n; ++i)
        for (int j = 0; j < n; ++j)
            dp[i][j] = -oo;
    dp[0][0] = 0;
    int prA = 0, ans = -oo;
    if (mn == 0)
        ans = 0;
    for (int i = 1; i <= n; ++i) {
        prA += a[i];
        int mx = -oo;
        for (int j = 0; j < l[i]; ++j)
            mx = max(mx, dp[i - 1][j]);
        for (int j = l[i]; j <= r[i]; ++j) {
            mx = max(mx, dp[i - 1][j]);
            dp[i][j] = max(dp[i][j], mx + b[i] * j);
            if (i >= mn && prA + dp[i][j] > ans) {
                ans = prA + dp[i][j];
                vector <int> t(i + 1);
                t[i] = j;
                for (int k = i - 1, jj = j, an = dp[i][j] - b[i] * j; k >= 1; --k) {
                    jj = min(jj, r[k]);
                    while (jj >= l[k] && dp[k][jj] != an)
                        --jj;
                    while (jj < l[k]) ++ans;
                    t[k] = jj;
                    an -= b[k] * jj;
                }
//                for (int ii = 1; ii <= i; ++ii)
//                    cout << t[ii] << " ";
//                cout << nl;
//                for (int ii = 2; ii <= i; ++ii)
//                    assert(t[ii - 1] <= t[ii]);
                for (int ii = 1; ii <= i; ++ii)
                    assert(t[ii] <= r[ii]);
                for (int ii = 1; ii <= i; ++ii)
                    assert(l[ii] <= t[ii]);
                for (int ii = 1; ii <= n; ++ii) {
                    if (p[ii] == -1)
                        continue;
                    if (i < p[ii]) {
//                    if (t[p[ii]] >= ii && r[p[ii]] > ii) {
                        cout << "AAAAA";
                        exit(0);
                    }
//                    assert(t[p[ii]] < ii);
//                    for (int j = p[ii] + 1; j <= i; ++j)
//                        assert(t[j] >= ii);
                }
            }
        }
    }
    cout << ans;
}
/*
4
-6 -1
-5 14
0 10
-30 2
1 -1 -1 -1
*/
