#include <iostream>
#include <queue>
#include <vector>


#define maxn 1000005
#define maxlog 21
#define X first
#define Y second
#define control std::cout<<"passed"<<"\n";

typedef long long ll;
typedef long double ld;
typedef std::pair <int , int> pii;
typedef std::pair <ll , ll> pll;


const ll inf = 4e18;


ll n;
ll a[maxn] , b[maxn] , p[maxn];
ll st[maxn];
ll l[maxn] , t[maxn];
ll prefa[maxn] , prefb[maxn];

void read()
{
    std::cin >> n;
    for (int i = 1; i <= n; i++)
        std::cin >> a[i] >> b[i];
    for (int i = 1; i <= n; i++)
        std::cin >> p[i];

    ll minn = 0;
    for (int i = 1; i <= n; i++)
        if (p[i] != -1)
            minn = std::max(minn , p[i]);

    for (int i = 1; i <= n; i++)
        if (p[i] != -1 && p[i] + 1 <= n)
            st[p[i] + 1] = std::max(st[i] , (ll)i);

    ll cc = 0;
    for (int i = 1; i <= n; i++)
        cc = std::max(cc , st[i]) , l[i] = cc;

    t[1] = 0;
    for (int i = 2; i <= n; i++)
        t[i] = std::max(t[i - 1] , l[i]);

    for (int i = 1; i <= n; i++)
    {
        prefa[i] = prefa[i - 1] + a[i];
        prefb[i] = prefb[i - 1] + b[i] * t[i];
    }

    ll ans = -inf;
    if (minn == 0)
        ans = 0;

    for (int i = std::max((ll)1 , minn); i <= n; i++)
    {
        ll curr = prefa[i] + prefb[i];
        ans = std::max(ans , curr);
    }

    std::cout << ans << "\n";
}






int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int tests = 1;
    //std::cin >> tests;
    while (tests--)
    {
        read();
    }

    return 0;
}