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

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