#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
    }
    vector<int> p(n);
    int lst = 0, mx = 0;
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        if (p[i] != -1) {
            for (int j = lst; j < p[i]; ++j) {
                ans += a[j] + b[j] * mx;
            }
            mx = i;
            lst = p[i];
        }
    }
    ll best = 0;
    ll cur = 0;
    for (int j = lst; j < n; ++j) {
        cur += a[j] + b[j] * mx;
        best = max(best, cur);
    }
    cout << ans + best << '\n';
    return 0;
}