#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pii pair<int, int>
#define ff first
#define ss second
#define ld long double
#define pb push_back
#define ins insert
#define int ll

mt19937 rnd(1488);

void solve(){
    int n;
    cin >> n;
    vector<pii> c(n);
    for (int i = 0; i < n; i++){
        cin >> c[i].ff >> c[i].ss;
    }
    vector<int> p(n);
    for (int i = 0; i < n; i++){
        cin >> p[i];
    }
    vector<int> mx(n + 1);
    mx[0] = -1;
    for (int i = 1; i <= n; i++){
        mx[i] = max(mx[i - 1], p[i - 1]);
    }
    vector<int> mx2(n + 1);
    mx2[n] = -1;
    for (int i = n - 1; i >= 0; i--){
        if (p[i] == -1){
            mx2[i] = mx2[i + 1];
        }
        else{
            mx2[i] = min(mx2[i + 1], p[i]);
        }
    }
    vector<int> dp(n, -1e18);
    dp[0] = c[0].ff;
    int ans = -1e18;
    if (mx[n] - 1 <= 0){
        ans = c[0].ff;
    }
    for (int i = 1; i < n; i++){
        dp[0] += c[i].ff;
        if (i >= mx[n] - 1){
            ans = max(ans, dp[0]);
        }
        for (int j = 1; j < n; j++){
            if (j <= i && i >= mx[j] && (mx2[j + 1] == -1 || mx2[j + 1] >= i)){
                dp[j] += c[i].ff + j * c[i].ss;
                if (j && dp[j - 1] + c[i].ss > dp[j]){
                    dp[j] = dp[j - 1] + c[i].ss;
                }
                if (i >= mx[n] - 1){
                    ans = max(ans, dp[j]);
                }
            }
        }
    }
    cout << ans << "\n";
}

int32_t main(){
    cin.tie(0)->ios_base::sync_with_stdio(0);
    #ifdef podureman
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif // podureman
    int _ = 1;
    //cin >> _;
    while (_--){
        solve();
    }
}
