#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

const ll INF = (ll)1e18;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<ll> a(n + 1), b(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> a[i] >> b[i];
    }
    vector<int> p(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> p[i];
    }
    vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, -INF));
    for (int i = 0; i <= n; ++i) dp[i][0] = 0;
    for (int i = 1; i <= n; ++i) {
        for (int t = 0; t <= n; ++t) {
            if (t > 0) dp[t][i] = max(dp[t][i], dp[t - 1][i]);
            if (t < i && (p[t] == i || p[t] == -1)) {
                ll sm = 0;
                for (int j = i; j > 0; --j) {
                    sm += a[j] + t * b[j];
                    if (t > 0 && dp[t][j - 1] != -INF) {
                        dp[t][i] = max(dp[t][i], dp[t][j - 1] + sm);
                    }
                }
            }
        }
    }
    ll mx = -INF;
    int K = *max_element(p.begin(), p.end());
    for (int i = max(1, K); i <= n; ++i) {
        for (int t = 0; t < i; ++t) {
            mx = max(mx, dp[t][i]);
        }
    }
    cout << mx << "\n";
    return 0;
}