#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <iomanip>
#include <set>
#include <cstring>
#define control cout<<"e ne"<<endl;
#define int long long
using namespace std;
int n,a[1048576],b[1048576],p[1048576],mx=-1e18,mp,pf[1048576],xf[1048576],used[1048576],ax,cant[1048576],tree[6048576],lazy[7048576];
void speed()
{
 ios_base::sync_with_stdio(false);
 cin.tie(0);
}
void push_lazy(int node,int l,int r)
{
    if(lazy[node])
    {
        tree[node]=max(tree[node],lazy[node]);
        if(l!=r)
        {
            lazy[node*2]=max(lazy[node*2],lazy[node]);
            lazy[node*2+1]=max(lazy[node*2+1],lazy[node]);
        }
        lazy[node]=0;
    }
}
void update(int node,int l,int r,int ql,int qr,int qval)
{
    push_lazy(node,l,r);
    if(l>=ql&&r<=qr)
    {
        lazy[node]=qval;
        push_lazy(node,l,r);
        return;
    }
    if(l>qr||r<ql)return;
    int mid=(l+r)/2;
    update(node*2,l,mid,ql,qr,qval);
    update(node*2+1,mid+1,r,ql,qr,qval);
    tree[node]=max(tree[node*2],tree[node*2+1]);
}
int query(int node,int l,int r,int ql,int qr)
{
    push_lazy(node,l,r);
    if(l>=ql&&r<=qr)return tree[node];
    if(l>qr||r<ql)return 0;
    int mid=(l+r)/2;
    return max(query(node*2,l,mid,ql,qr),query(node*2+1,mid+1,r,ql,qr));
}
signed main()
{
    /*
    #ifdef ONLINE_JUDGE
    freopen("txt.in","r",stdin)
    freopen("txt.out","w",stdout)
    #endif
    */
    speed();
    cin>>n;
    int sum=0;
    vector <int> v;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i]>>b[i];
        xf[i]=xf[i-1]+a[i];
        pf[i]=pf[i-1]+b[i];
    }
    int last=1;
    for(int i=1;i<=n;i++)
    {
        cin>>p[i];
        if(p[i]!=-1)
        {
            update(1,1,n,p[i]+1,n,i);
        }
        mp=max(mp,p[i]);
    }
    for(int i=1;i<=mp-1;i++)ax+=query(1,1,n,i,i)*b[i]+a[i];
    for(int k=max(mp,1LL);k<=n;k++)
    {
        ax+=query(1,1,n,k,k)*b[k]+a[k];
        mx=max(mx,ax);
    }
    cout<<mx<<endl;
    return 0;
}
/*
4
3 -6
4 -10
-7 -3
-3 -6
3 3 -1 -1
*/
