#include <bits/stdc++.h>
using namespace std;
#define int int64_t

const int INF = (4 * 1e18);

void solve() {
    int n;
    cin >> n;
    vector<int> a(n + 1);
    vector<int> b(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> a[i] >> b[i];
    }
    vector<int> pa(n + 1, 0);
    for (int i = 1; i <= n; i++) {
        pa[i] = pa[i - 1] + a[i];
    }
    vector<int> pb(n + 1, 0);
    for (int i = 1; i <= n; i++) {
        pb[i] = pb[i - 1] + b[i];
    }
    vector<int> dp(n + 1, -INF);
    dp[0] = 0;
    for (int i = 1; i <= n; i++) {
        dp[i] = pa[i];
    }
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        if (x != -1) {
            for (int j = 0; j <= n; j++) {
                if (x != j) {
                    dp[j] = -INF;
                }
            }
        }
        vector<int> g = dp;
         for (int u = i + 1; u <= n; u++) {
             for (int l = i; l <= u - 1; l++) {
                 g[u] = max(g[u], dp[l] + (pb[u] - pb[l]) * i + (pa[u] - pa[l]));
             }
         }
        dp = g;
    }
    int ans = -INF;
    for (int i = 0; i <= n; i++) {
        ans = max(ans, dp[i]);
    }
    cout << ans;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}