#include <bits/stdc++.h>

using namespace std;

#define int long long
const int MN = -1e8, MX = 1e8, MOD = 998244353;


signed main(){
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    int wow = n;
    for(int i = 0; i < n; ++i){
        cin >> a[i] >> b[i];
        if(b[i] > 0 && wow == n)
            wow = i - 1;
    }
    vector<int> p(n), t_mn(n), t_mx(n);
    int last = 0, j = 0;
    for(int i = 0; i < n; ++i){
        cin >> p[i];
        if(p[i] != -1){
            while(j < p[i]){
                t_mx[j] = min(i, j);
                t_mn[j] = last;
                ++j;
            }
            last = i + 1;
        }
    }
    int kek = 0, hihi = j;
    while(j < n){
        if(b[j] < 0)
            kek += a[j] + b[j] * last;
        else
            kek += a[j] + b[j] * j;
        ++j;
        //cout << kek << '\n';
    }
    vector<int> t(n);
    int ans = 0;
    for(int i = 0; i < hihi; ++i){
        if(b[i] < 0)
            t[i] = t_mn[i];
        else
            t[i] = t_mx[i];
        //cout << t_mn[i] << ' ' << t_mx[i] << ' ' << t[i] << '\n';
        ans += a[i] + b[i] * t[i];
    }
    cout << ans + max((int)0, kek);
}











