#include <iostream>
#include <vector>
#include <algorithm>
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,lzcnt,popcnt,bmi,bmi2")
#define int long long
using namespace std;


int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.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);
    for (int i = 0; i < n; i++) cin >> p[i];
    int tl = 0, tr = -1;
    int lp = 0, rp = -1;
    int lastpref = 0;

    int ans = 0;
    for (int pind = 0; pind < n; pind++) {
        if (p[pind] == -1) {
            continue;
        }
        rp = p[pind] - lastpref + lp - 1;
        tr = pind;

        if (lp > rp) {
            lp = rp + 1;
            tl = tr + 1;
            lastpref = p[pind];
            continue;
        }
        // 
        for (int i = lp; i <= rp; i++) {
            if (b[i] <= 0) {
                ans += a[i] + b[i] * tl;
            }
            else {
                ans += a[i] + b[i] * min(tr, i);
            }
        }
        // cout << pind << " "  << p[pind] << "    |: " << tl << " " << tr << " " << lp << " " << rp << endl;
        
        lp = rp + 1;
        tl = tr + 1;
        lastpref = p[pind];
    }
    // ost:
    rp = n - 1;
    tr = n;
    int cans = 0;
    int bans = 0;
    for (int i = lp; i <= rp; i++) {
        if (b[i] <= 0) {
            cans += a[i] + b[i] * tl;
        }
        else {
            cans += a[i] + b[i] * min(tr, i);
        }
        bans = max(bans, cans);
    }
    // cout << "       |: " << tl << " " << tr << " " << lp << " " << rp << endl;
    //
    cout << ans + bans << endl;
    return 0;
}