#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define fi first
#define se second
#define pb push_back
#define pob pop_back
#define endl '\n'
#define fori(s,n) for(int i=s;i<n;i++)
#define forj(s,n) for(int j=s;j<n;j++)
#define int long long
#define ull unsigned long long
#define ll long long
#define ld long double
using namespace std;
using namespace __gnu_pbds;
random_device rd;
mt19937 rnd(rd());
void solve(){
    int n;cin>>n;
    vector<pair<int,int>>ab(n);
    vector<int>p(n);
    fori(0,n) cin>>ab[i].fi>>ab[i].se;
    fori(0,n) cin>>p[i];
    int mx=-1e9,mn=1e9;
    fori(0,n){
        mx=max(mx,ab[i].se);
        mn=min(mn,ab[i].se);
    }
    if(mx<=0){
        int ax=-1e9;
        int ans=0;
        int j=0;
        int last=-1;
        fori(0,n){
            if(p[i]==-1) continue;
            else{
                while(j<p[i]){
                    ans+=ab[j].fi+ab[j].se*last;
                    j++;
                }
                last=i;
            }
        }
        ax=ans;
        while(j<n){
            ans+=ab[j].fi+ab[j].se*last;
            ax=max(ax,ans);
            j++;
        }
        cout<<ax<<endl;
        return;
    }
    if(mn>=0){
        int ans=0;
        int j=0;
        fori(0,n){
            if(p[i]==-1) continue;
            else{
                while(j<p[i]){
                    ans+=ab[j].fi+ab[j].se*min(j+1,i+1);
                    j++;
                }
            }
        }
        while(j<n){
            ans+=ab[j].fi+ab[j].se*min(j+1,n);
        }
        cout<<ans<<endl;
        return;
    }
}
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int tt=1;
    //cin>>tt;
    while(tt--) solve();
    return 0;
}
