#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

ostream& operator << (ostream& out, __int128 x)
{
    if (!x)
        out << 0;
    else
    {
        if (x < 0)
        {
            out << '-';
            x *= -1;
        }
        string s;
        while (x)
        {
            s += x % 10 + '0';
            x /= 10;
        }
        reverse(s.begin(), s.end());
        out << s;
    }
    return out;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<long long> a(n), b(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i] >> b[i];
    vector<long long> p(n);
    for (int i = 0; i < n; ++i)
        cin >> p[i];
    int last = -1;
    for (int i = 0; i < n; ++i)
        if (p[i] != -1)
            last = i;
    __int128 val = 0, prev = -1, k = 0;
    for (__int128 t = 0; t <= last; ++t)
        if (p[t] != -1)
        {
            for ( ; k < p[t]; ++k)
            {
                if (b[k] <= 0)
                    val += (__int128) a[k] + (prev + 1) * b[k];
                else
                    val += (__int128) a[k] + t * b[k];
            }
            prev = t;
        }
    __int128 ans = val;
    for ( ; k < n; ++k)
    {
        if (b[k] <= 0)
            val += (__int128) a[k] + (prev + 1) * b[k];
        else
            val += (__int128) a[k] + k * b[k];
        ans = max(ans, val);
    }
    cout << ans;
    return 0;
}
