#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const long long inf = 2e18;

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<int> 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;
    long long val = 0, k = 0, prev = -1;
    for (long long t = 0; t < n; ++t)
        if (p[t] != -1)
        {
            for ( ; k < p[t]; ++k)
            {
                if (b[k] <= 0)
                    val += a[k] + (prev + 1) * b[k];
                else
                    val += a[k] + min(k, t) * b[k];
            }
            prev = t;
        }
    long long ans = val;
    for ( ; k < n; ++k)
    {
        if (b[k] <= 0)
            val += a[k] + (prev + 1) * b[k];
        else
            val += a[k] + k * b[k];
        ans = max(ans, val);
    }
    cout << ans;
    return 0;
}
