#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <unordered_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,mp,pf[1048576],xf[1048576],used[1048576],ax,cant[1048576];
int tree[16777216],lazy[16777216];
void speed()
{
 ios_base::sync_with_stdio(false);
 cin.tie(0);
}
void push_lazy(int node,int l,int r)
{
    if(lazy[node]!=1e18)
    {
        tree[node]=min(tree[node],lazy[node]);
        if(l!=r)
        {
            lazy[node*2]=min(lazy[node*2],lazy[node]);
            lazy[node*2+1]=min(lazy[node*2+1],lazy[node]);
        }
        lazy[node]=1e18;
    }
}
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]=min(qval,lazy[node]);
        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]=min(tree[node*2],tree[node*2+1]);
}
int query(int node,int l,int r,int ql,int qr)
{
    if(ql>qr)return 1e18;
    push_lazy(node,l,r);
    if(l>=ql&&r<=qr)return tree[node];
    if(l>qr||r<ql)return 1e18;
    int mid=(l+r)/2;
    return min(query(node*2,l,mid,ql,qr),query(node*2+1,mid+1,r,ql,qr));
}
void build(int node,int l,int r)
{
    if(l==r)
    {
        tree[node]=l-1;
        return;
    }
    int mid=(l+r)/2;
    build(node*2,l,mid);
    build(node*2+1,mid+1,r);
    tree[node]=min(tree[node*2],tree[node*2+1]);
}
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];
    }
    ax=0;
    int last=1;
    mp=1;
    build(1,1,n);
    for(int i=1;i<=n;i++)
    {
        cin>>p[i];
        if(p[i]!=-1)
        {
            update(1,1,n,1,p[i],i);
            mp=max(mp,p[i]);
        }
    }
    for(int i=1;i<mp;i++)ax+=query(1,1,n,i,i)*b[i]+a[i];
    for(int k=mp;k<=n;k++)
    {
        ax+=query(1,1,n,k,k)*b[k]+a[k];
        //cout<<ax<<" "<<k<<endl;
        if(k==mp)mx=ax;
        else mx=max(mx,ax);
    }
    cout<<mx<<endl;
    return 0;
}
/*
4
3 6
4 10
-7 3
-3 6
3 3 -1 -1

4
-6 -1
-5 -14
0 -10
-30 -2
4 -1 -1 -1
*/
