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