#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <cmath>
#include <algorithm>
#include <bitset>
#include <random>
using namespace std;

#define int long long

const int inf = 1e9 + 10;
const int Mx = 1e6 + 10;
int A[Mx], B[Mx], P[Mx];
int P2[Mx];
int N;

signed main() {
    cin >> N;
    for (int i = 1; i <= N; i++) {
        cin >> A[i] >> B[i];
    }
    for (int i = 1; i <= N; i++) {
        cin >> P[i];
    }
    P[0] = P[1];
    P2[N + 1] = N + 1;
    for (int i = N; i >= 0; i--) {
        P2[i] = P[i];
        if (P[i] == -1) {
            P2[i] = P2[i+1];
        }
    }
    int j = 0, total = 0, mx = -inf;
    for (int i = 0; i <= N; i++) {
        //cout << P[i] << ' ' << P2[i] << ' ' << mx << ' ' << total << endl;
        while (j < N && j < max(P[i], P2[i])) {
            j++;
            int app = A[j] + i * B[j];
            //cout << "app: " << app << endl;
            if (P2[i] == N + 1) {
                mx = max(mx, total);
            }
            total += app;
        }
    }
    cout << max(total, mx);
}

/*
3
1
-2
1
-2
-1
-2
-1 -1 -1
*/

