#include <bits/stdc++.h>

using namespace std;

#ifndef lisie_bimbi
#define endl '\n'
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,bmi2,fma")
#endif

using ll = long long;
const ll inf = 1'000'000'000'000'000'000;

#define int long long

void solve(){
    int n;
    cin >> n;
    vector<ll> a(n), b(n);
    for(int i = 0; i < n; i++){
        cin >> a[i] >> b[i];
    }
    vector<int> p(n);
    vector<int> low(n), high(n, n + 1);
    for(int i = 0; i < n; i++){
        high[i] = i + 1;
    }
    for(int i = 0; i < n; i++){
        cin >> p[i];
        if(p[i] == -1){
            continue;
        }
        if(p[i] < n){
            low[p[i]] = max(low[p[i]], i + 1);
        }
        high[p[i] - 1] = min(high[p[i] - 1], i + 1);
    }   
    int mnind = -1;
    for(int i = 0; i < n; i++){
        if(p[i] == -1){
            
        }else{
            mnind = max(mnind, p[i] - 1);
        }
    }
    vector<ll> dp(n, 0);
    ll ans = -inf;
    if(mnind == -1){
        ans = 0;
    }
    int sum = 0;
    int now = 0;
    for(int i = 0; i < n; i++){
        now = max(now, low[i]);
        sum += a[i] + b[i] * now;
        if(i >= mnind){
            ans = max(ans, sum);
        }
    }
    cout << ans << endl;
    return;
    for(int j = 0; j < n; j++){
        ll mx = -inf;
        for(int i = 0; i < low[j]; i++){
            mx = max(mx, dp[i]);
            dp[i] = -inf;
        }
        for(int i = low[j]; i < high[j]; i++){
            mx = max(mx, dp[i]);
            if(mx != -inf){
                dp[i] = mx + a[j] + b[j] * i;
            }else{
                dp[i] = -inf;
            }
        }
        for(int i = high[j]; i < n; i++){
            dp[i] = -inf;
        }
        if(j >= mnind){
            for(auto i : dp){
                ans = max(ans, i);
            }
        }
        // cout << low[j] << ' ' << high[j] << endl;
        // for(auto i : dp){
        //     cout << i << ' ';
        // }
        // cout << endl;
    }
    for(auto i : dp){
        ans = max(ans, i);
    }
    cout << ans << endl;
}

signed main(){
#ifdef lisie_bimbi
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);   
#else
    cin.tie(nullptr)->sync_with_stdio(false);
#endif         
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
}