#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
const ll MAXN = 1e5 + 6;
const ll inf = 1e17 + 7;
const ll mod = 998244353;
const ll prod = 53;
mt19937 rnd(2);
ll p[MAXN];

int main() {
    ll n, a, b;
    cin >> n;
    vector<pair<ll, ll> > arr;
    for (int i = 0; i < n; i++) {
        cin >> a >> b;
        arr.push_back({a, b});
    }
    for (int i = 0; i < n; i++) {
        cin >> p[i];
    }
    ll ind = -1;
    ll k = 0;
    ll res = 0;
    for (int te = 0; te < n; te++) {
        if (p[te] != -1 && ind + 1 < p[te]) {
            for (int i = ind + 1; i < p[te]; i++) {
                res += arr[i].first;
            }
            ll dp[p[te] - ind - 1][te - k + 1];
            ll pref[te - k + 6];
            for (int i = 0; i < p[te] - ind - 1; i++) {
                for (int j = 0; j < te - k + 1; j++) {
                    dp[i][j] = -inf;
                    pref[j] = -inf;
                }
            }
            for (int i = 0; i < min(ind + 2, te - k + 1); i++) {
                dp[0][i] = (i + k) * arr[ind + 1].second;
                pref[i + 1] = max(pref[i], dp[0][i]);
            }
            pref[min(ind + 2, te - k + 1) + 1] = pref[min(ind + 2, te - k + 1)];
            for (int i = 1; i < p[te] - ind - 1; i++) {
                for (int j = 0; j < min(ind + i + 2, te - k + 1); j++) {
                    dp[i][j] = (j + k) * arr[i].second + pref[j + 1];
                }
                for (int j = 0; j < min(ind + i + 2, te - k + 1); j++) {
                    pref[j + 1] = max(pref[j], dp[i][j]);
                }
            }
            res += pref[min(p[te], te - k + 1)];
            k = te + 1;
            ind = p[te] - 1;
        }
    }
    ll res1 = res;
    if (p[n - 1] == -1 && ind + 1 < n) {
        ll dp[n - ind - 1][n - k];
        ll pref[n - k + 5];
        for (int i = 0; i < n - ind - 1; i++) {
            for (int j = 0; j < n - k; j++) {
                dp[i][j] = -inf;
                pref[j] = -inf;
            }
        }
        for (int i = 0; i < min(ind + 2, n - k); i++) {
            dp[0][i] = (i + k) * arr[ind + 1].second;
            pref[i + 1] = max(pref[i], dp[0][i]);
        }
        pref[min(ind + 2, n - k) + 1] = pref[min(ind + 2, n - k)];
        res += arr[ind + 1].first;
        res1 = max(res1, res + pref[min(ind + 2, n - k)]);
        for (int i = 1; i < n - ind - 1; i++) {
            for (int j = 0; j < min(ind + i + 2, n - k); j++) {
                dp[i][j] = (j + k) * arr[i].second + pref[j + 1];
            }
            for (int j = 0; j < min(ind + i + 2, n - k); j++) {
                pref[j + 1] = max(pref[j], dp[i][j]);
            }
            pref[min(ind + i + 2, n - k) + 1] = pref[min(ind + i + 2, n - k)];
            res += arr[ind + i + 1].first;
            res1 = max(res1, res + pref[min(i + ind + 2, n - k)]);
        }
    }
    cout << res1 << "\n";
}
