#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

using ll = long long;
using ld = long double;

const int MAXN = 1e6+50;
const int MOD = 998244353;
const int INF = 1e9;
const ll LLINF = 1e16;
const ld EPS = 1e-9;
const int LOGN = 25;

ll binpow(ll x, ll k) {
    if (k == 0) return 1;
    if (k % 2 == 0) {
        ll d = binpow(x, k/2) % MOD;
        return d*d%MOD;
    }
    return x*(binpow(x, k-1))%MOD;
}
int n, q;

ll dp[5005][5005]; // dp[i][j] = максимальный ответ на префиксе i, если i чела закончили обслуживать на j сек
bool was[5005]; // посещен ли ученик

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n;
    vector<ll> a(n+1), b(n+1);
    for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];
    int max_p = 0;
    vector<int> p(n+1); for (int i = 1; i <= n; i++) {
        cin >> p[i];
        if (p[i] != -1) max_p = max(max_p, p[i]);
    }

    for (int i = 0; i < 5005; i++)
        for (int j = 0; j < 5005; j++)
            dp[i][j] = -LLINF;

    ll ans = (max_p != 0 ? -LLINF : 0LL);

    dp[1][0] = a[1]; was[1] = true;
    if (p[1] != -1) {
        for (int i = 2; i <= p[1]; i++) {
            dp[i][0] = dp[i-1][0]+a[i];
            was[i] = true;
        }
        ans = max(ans, dp[p[1]][0]);
    } else ans = max(ans, dp[1][0]);

    for (int i = 2; i <= n; i++) {
        if (p[i] != -1 && !was[i]) {
            was[i] = true;

            dp[i][0] = dp[i-1][0]+a[i]; ans = max(ans, dp[i][0]);
            for (int j = 1; j < i; j++) {
                dp[i][j] = max(dp[i][j-1]+b[i], dp[i-1][j]+a[i]+j*b[i]);
                ans = max(ans, dp[i][j]);
            }
        }

        for (int k = i; k <= max(i, p[i]); k++) {
            if (was[k]) continue;
            was[k] = true;

            dp[k][0] = dp[k-1][0]+a[k]; ans = max(ans, dp[i][0]);
            for (int j = 1; j < i; j++) {
                dp[k][j] = max(dp[k][j-1]+b[k], dp[k-1][j]+a[k]+j*b[k]); ans = max(ans, dp[k][j]);
            }
        }


    }

    /*
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < i; j++) {
            if (dp[i][j] == -LLINF) cout << 'd' << ' ';
            else cout << (dp[i][j]) << ' ';
        }
        cout << '\n';
    }
    */

    /*
    for (int i = max_p; i <= n; i++) {
        for (int j = 0; j < i; j++) ans = max(ans, dp[i][j]);
    }
    */

    cout << ans;
}
