#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],mp,pf[1048576],xf[1048576],used[1048576],cant[1048576],tree[6048576],lazy[7048576];
__int128 mx,ax;
void speed()
{
 ios_base::sync_with_stdio(false);
 cin.tie(0);
}
signed main()
{
    /*
    #ifdef ONLINE_JUDGE
    freopen("txt.in","r",stdin)
    freopen("txt.out","w",stdout)
    #endif
    */
    speed();
    cin>>n;
    int sum=0;
    vector <pair <int,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];
    }
    ax=0;
    int last=1;
    mp=0;
    for(int i=1;i<=n;i++)
    {
        cin>>p[i];
        if(p[i]!=-1)
        {
            v.push_back({p[i],i});
        }
        mp=max(mp,p[i]);
    }
    int j=-1;
    for(int i=1;i<=n;i++)
    {
        while(j<v.size()&&v[j+1].first==i-1)j++;
        if(j==v.size())j--;
        if(j==-1)cant[i]=0;
        else cant[i]=v[j].second;
    }
    for(int i=1;i<=mp-1;i++)ax+=cant[i]*b[i]+a[i];
    for(int k=max(mp,1LL);k<=n;k++)
    {
        ax+=cant[k]*b[k]+a[k];
        mx=max(mx,ax);
    }
    string s="";
    while(mx)
    {
        char c=(mx%10+'0');
        s=c+s;
        mx/=10;
    }
    if(s.empty())s="0";
    cout<<s<<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
*/
