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

#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++)
    {
        prefa[i] = prefa[i - 1] + a[i];
        prefb[i] = prefb[i - 1] + b[i];
    }

    for (int i = 0; i <= n + 1; i++)
        st[i] = -1;

    for (int i = 1; i <= n; i++)
        if (p[i] != -1)
            st[i - 1] = p[i];

    std::vector <ll> dp0(n + 1 , -inf) , dp1(n + 1 , -inf);
    dp0[0] = 0;;

    for (int x = 0; x <= n - 1; x++)
    {
        std::fill(dp1.begin(), dp1.end(), -inf);


        if (st[x] != -1)
        {
            ll cc = -inf;

            for (int i = 0; i <= st[x]; i++)
                if (dp0[i] != -inf)
                    cc = std::max(cc , dp0[i] - x * prefb[i]);

            if (cc != -inf)
                dp1[st[x]] = x * prefb[st[x]] + cc;
        }
        else
        {
            ll maxx = -inf;
            for (int i = 0; i <= n; i++)
            {
                if(dp0[i] != -inf)
                    maxx = std::max(maxx , dp0[i] - x * prefb[i]);
                if (maxx != -inf)
                    dp1[i] = x * prefb[i] + maxx;
            }
        }

        dp0.swap(dp1);
    }

    ll ans = -inf;
    if (minn == 0)
        ans = 0;
    for (int i = std::max((ll)1 , minn); i <= n; i++)
        ans = std::max(ans , prefa[i] + dp0[i]);

    std::cout << ans << std::endl;
}






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

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

    return 0;
}