// #define _GLIBCXX_DEBUG
// #pragma GCC optimize("O3,unroll-loops")
#include<iostream>
#include<vector>
using namespace std;
using ll = long long;
using ld = long double;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    ll n;
    cin>>n;
    vector<ll> a(n), b(n);
    vector<pair<ll, ll>> p;
    for (ll i = 0; i<n; i++){
        cin>>a[i]>>b[i];
    }
    for (ll i = 0; i<n; i++){
        ll c;
        cin>>c;c--;
        if (c != -2){p.push_back({c, i});}
    }
    vector<ll> mi(n), ma(n);
    ll ind = 0;
    ll t = -1;
    for (ll i = 0; i<n; i++){
        while (ind != p.size()){
            if (p[ind].first > i-1){break;}
            t = p[ind].second;
            ind++;
        }
        mi[i] = t+1;
    }
    ind = p.size()-1;
    t = 1e18;
    for (ll i = n-1; i>=0; i--){
        while (ind >= 0){
            if (p[ind].first < i){break;}
            t = p[ind].second;
            ind--;
        }
        ma[i] = min(i, t);
    }
    ll ans = 0;
    for (ll i = 0; i<n; i++){
        ans += a[i] + b[i]*(ma[i]+1);
    }
    cout<<ans<<endl;
    return 0;
}