#include <iostream>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <set>
#include <map>
#include <random>
#include <deque>
#include <queue>
#include <unordered_set>
#include <unordered_map>

#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define int long long

using namespace std;

mt19937 rnd(52);

typedef long long ll;
typedef long double ld;

const ll MAXN = 5e5 + 1;
const ll MOD = 998244353;
const ll INF = 1e18;

int logpow(int num, int deg, int mod = MOD){
    int sum = 1;
    while(deg){
        if(deg % 2 == 0){
            num *= num;
            num %= mod;
            deg /= 2;
        }
        else{
            sum *= num;
            sum %= mod;
            deg--;
        }
    }
    return sum;
}

vector<int> mirror(vector<int> arr){
    int n = arr.size() - 1;
    vector<int> newarr(n + 1);
    for(int i = 1; i <= n; i++){
        newarr[i] = arr[n - i + 1];
    }
    return newarr;
}

vector<int> rev(vector<int> arr){
    int n = arr.size() - 1;
    vector<int> newarr(n + 1);
    for(int i = 1; i <= n; i++){
        newarr[i] = n - arr[i] + 1;
    }
    return newarr;
}

vector<int> obr(vector<int> arr){
    int n = arr.size() - 1;
    vector<int> newarr(n + 1);
    for(int i = 1; i <= n; i++){
        newarr[arr[i]] = i;
    }
    return newarr;
}


void solve(){
    int n;
    cin >> n;
    vector<pair<int, int>> arr(n + 1);
    vector<int> prefa(n + 1);
    vector<int> prefb(n + 1);
    for(int i = 1; i <= n; i++){
        int a, b;
        cin >> a >> b;
        arr[i] = {a, b};
        prefa[i] = prefa[i - 1] + a;
        prefb[i] = prefb[i - 1] + b;
    }
    vector<int> pr(n + 1);
    for(int i = 1; i <= n; i++){
        cin >> pr[i];
    }
    int ind = 0;
    for(int i = 1; i <= n; i++){
        if(pr[i] != -1) ind = i;
    }
    int cnt = 0;
    int ans = 0;
    for(int i = 1; i <= ind; i++){
        if(pr[i] != -1 && i > cnt){
            cnt++;
            ans += arr[i].first + arr[i].second * (i - 1);
        }
        else if(pr[i] == -1){
            ans += (prefa[pr[i]] - prefa[cnt]) + (i - 1) * (prefb[pr[i]] - prefb[cnt]);
            cnt = pr[i];
        }
    }
    int add = 0;
    int sum = 0;
    for(int i = cnt + 1; i <= n; i++){
        sum += arr[i].first + arr[i].second * (i - 1);
        add = max(add, sum);
    }
    cout << ans + add << endl;
}

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