#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<pair<ll, ll>> v(n);
    vector<ll> cur(n);
    for (auto& [x, y] : v) cin >> x >> y;
    vector<ll> p(n);
    for (ll& o : p) cin >> o;
    vector<ll> mintime(n, n - 1);
    ll f = 0;
    for (ll i = 0; i < n; i++) {
        if (p[i] != -1) {
            f = max(f, p[i]);
            mintime[p[i] - 1] = min(mintime[p[i] - 1], i);
        }
    }
    for (ll i = n - 2; i >= 0; i--) {
        mintime[i] = min(min(i, mintime[i]), mintime[i + 1]);
    }
    vector<ll> dp(n + 1, -1e18); dp[0] = 0;
    for (int i = 0; i < n; i++) {
        ll s1 = 0, s2 = 0;
        for (ll j = i; j >= 0; j--) {
            s1 += v[j].first; s2 += v[j].second;
            dp[i + 1] = max(dp[i + 1], dp[j] + s1 + s2 * mintime[j]);
        }
    }
    // for (int i = 0; i < n; i++) {
        // ans += cur[i];
        // Ans = max(Ans, ans);
    // }
    // cout << Ans << '\n';
    cout << *max_element(dp.begin() + f, dp.end()) << '\n';
    return 0;
}
