#include <iostream>
#include <bits/stdc++.h>
using namespace std;

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long n;
    cin >> n;
    vector<long long> a(n), b(n), p(n);
    for (long long i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    for (long long i = 0; i < n; i++) {
        cin >> p[i];
    }
    long long ans = 0, lst = 0, id = 0;
    for (long long i = 0; i < n; i++) {
        if (p[i] != -1) {
            while (lst != p[i]) {
                ans += a[lst] + max(i, id) * b[lst];
                lst++;
            }
        }
        id = lst;
    }
    long long add = 0, cnt = 0;
    while (lst != n) {
        cnt += a[lst] + id * b[lst];
        add = max(add, cnt);
        lst++;
    }
    cout << ans + add;
}