#include <bits/stdc++.h>

using namespace std;

const long long MOD = 998244353;

int main()
{
    int n;
    cin >> n;
    vector <pair <long long, long long>> v(n);
    for (int i = 0; i < n; ++i) {
        cin >> v[i].first >> v[i].second;
    }
    vector <int> p(n);
    int j = 0;
    long long ans = 0;
    int prev = 0;
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        if (p[i] != -1) {
            while (j < p[i]) {
                ans += v[j].first + v[j].second * prev;
                j++;
            }
            prev = i + 1;
        }
    }
    long long k = 0;
    long long maxx = 0;
    for (int i = j; i < n; ++i) {
        k += v[i].first + v[i].second * prev;
        if (k > maxx) {
            maxx = k;
        }
    }
    cout << ans + maxx;
    return 0;
}
