#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 3, mod = 998244353, inf = 1e18 + 7;
int dp[2][N];
int a[N], b[N], p[N];
signed  main(){
     ios::sync_with_stdio(false);
     cin.tie(nullptr);
     int n;
     cin >> n;
     for (int i = 1; i<= n; i++){
        cin >> a[i] >> b[i];
     }
     int mx = 0;
     for (int i = 0 ;i < n; i++){
        cin >> p[i];
        mx = max(mx, p[i]);
     }
     int cur = 0;
     for (int i = 0 ;i < n; i++){
        cur += a[i];
        if (i >= mx){
            ans = max(ans, cur);
        }
     }
     cout << ans;
     /*int sm = 0;
     for (int i = 0; i<= n; i++){
        for (int j = 0; j < 2; j++){
            dp[j][i] = -inf;
        }
     }
     for (int  i= 0; i <= n; i++){
        sm +=a[i];
        if (p[0] == -1){
            dp[0][i] = sm;
        } else {
            if (i == p[0]){
                dp[0][i] = sm;
            }
        }
        //cout << dp[0][i] << " ";
     }
     //cout << "\n";
     //cout << dp[0][3] << "\n";
     for (int i = 1; i < n; i++){
        if (p[i] != -1){
            int sm = 0;
            for (int j = p[i]; j >= i; j--){
                dp[1][p[i]] = max(dp[1][p[i]], dp[0][j] + sm);
                sm += a[j] + b[j] * i;
            }
        } else {
            int best = dp[0][i];
            dp[1][i] = dp[0][i];
            for (int t = i + 1; t <= n; t++){
                best = max(best + a[t] + b[t] * i, dp[0][t]);
                dp[1][t] = best;
            }
        }
     }
     int ans = -inf;
     for (int i = n - 1; i >= 0; i--){
        for (int j = 0; j <= n; j++){
            ans = max(ans, dp[i][j]);
        }
        if (p[i] != -1){
            break;
        }
     }

     cout << ans;*/
 }

