#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
using ll = long long;
#define int ll
const int maxn = 5000+10;
ll dp[maxn][maxn];
ll a[maxn], b[maxn];
int p[maxn];
int ogr[maxn];
signed main()
{
#ifdef LOCAL
    freopen("main.in", "r", stdin);
    freopen("main.out", "w", stdout);
#endif
    for (int i = 0; i < maxn; ++i) {
        ogr[i] = 1e9;
    }

    int n;
    cin >> n;
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];

        // ans = max(ans, sum);
    }
    int maxp = -1;
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        // maxp = p[i] - 1;
        ogr[p[i] - 1] = i+1;
        maxp = max(maxp, p[i]-1);
        // cout << maxp << " " << p[i] - 1 << endl;
        // cout << p[i] << endl;
    }

    dp[0][0] = a[0];
    if (maxp <= 0) {

    ans = max(ans, a[0]);
    }

    for (int i = 1; i < n; ++i) {

        ll maxans = dp[i-1][0];
        for (int t = 0; t <= i; ++t) {
            if (t != i) {

            maxans = max(maxans, dp[i-1][t]);
            }
            dp[i][t] = maxans + a[i] + t * b[i];
            // cout << i << " " << t << " " << dp[i][t] << endl;
            // if (t < ogr[i-1]) {

            // }
            // if (i > maxp  || (i == maxp && t < ogr[i])) {



            ans = max(ans, dp[i][t]);
            // }
        }
    }
    cout << ans;

}