#pragma GCC optimize("O3,unroll-loops")
#include <string>
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

const ll INF = 1e18;

struct Pupil {
    ll x;
    ll y;
};

void Solve () {
    int n;
    cin >> n;
    vector<Pupil>a(n);
    for (auto& [x, y] : a) {
        cin >> x >> y;
    }
    vector<int>p(n);
    for (int& i : p) {
        cin >> i;
    }
    vector<pair<int, int>>tp(n);
    for (int i = 0; i < n; ++i) {
        tp[i] = {0, i + 1};
    }
    for (int i = 0; i < n; ++i) {
        if (p[i] == -1) {
            continue;
        }
        for (int j = 0; j < p[i]; ++j) {
            tp[j].second = min(tp[j].second, i + 1);
        }
        for (int j = p[i]; j < n; ++j) {
            tp[j].first = max(tp[j].first, i + 1);
        }
    }
    int ogr = 0;
    for (int i = 0; i < n; ++i) {
        ogr = max(ogr, p[i]);
    }
    vector<ll>dp(n + 1);
    for (int i = 1; i <= n; ++i) {
        dp[i] = dp[i - 1] + a[i - 1].x + tp[i - 1].first * a[i - 1].y;
    }
    ll ans = -INF;
    if (p[n - 1] != -1) {
        cout << dp[ogr];
        return;
    }
    for (int i = ogr; i <= n; ++i) {
        ans = max(ans, dp[i]);
    }
    cout << ans;
}

signed main () {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    //cin >> t;
    while (t--) {
        Solve();
    }
}