#include <bits/stdc++.h>

// #pragma GCC optimize("Ofast")

using namespace std;

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

constexpr long long INF = 1e18, MOD = 1e9 + 7;


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    ll n;
    cin >> n;
    vector<ll> a(n), b(n);
    vector<ll> p(n);
    for (ll i = 0; i < n; ++i) {
        cin >> a[i];
        cin >> b[i];
    }
    vector<ll> ind(n, -1);
    ll last = -1;
    ll cnt = 0;
    for (ll i = 0; i < n; ++i) {
        cin >> p[i];
        if (p[i] != -1) {
            if (ind[p[i] - 1] == -1) {
                ind[p[i] - 1] = i;
            }
            last = p[i] - 1;
        }
        cnt = max(cnt, p[i]);
    }

    ll ans = -INF;
    vector<vector<ll>> dp(n, vector<ll> (n, -INF));
    dp[0][0] = a[0];
    if (last < 0) {
        ans = 0;
    }
    ll now = 0;
    if (last <= 0) {
        ans = max(ans, a[0]);
    }
    for (ll i = 1; i < n; ++i) {
        if (ind[i] != -1) {
            ll mx = dp[i - 1][0];
            for (ll j = 0; j < i + 1; ++j) {
                mx = max(mx, dp[i - 1][j]);
                if (j > now) {
                    if (j <= ind[i]) {
                        dp[i][j] = mx + a[i] + j * b[i];
                        if (last <= i) {
                            ans = max(ans, dp[i][j]);
                        }
                    }
                }
            }
            now = max(now, ind[i] + 1);
        }
        else {
            ll mx = dp[i - 1][0];
            for (ll j = 0; j < i + 1; ++j) {
                mx = max(mx, dp[i - 1][j]);
                if (j > now) {
                    dp[i][j] = mx + a[i] + j * b[i];
                    if (last <= i) {
                        ans = max(ans, dp[i][j]);
                    }
                }
            }
        }


    }
    cout << ans;
}