#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 = 1e18 + 10;
const int Mx = 1e6 + 10;
int A[Mx], B[Mx], P[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];
    }
    int id = N;
    while (P[id] == -1) {
        id--;
    }
    for (int i = id; i <= N; i++) {
        P[i] = P[i-1];
    }
    if (id < N) {
        P[N] = N;
    }
    int j = 0, total = 0, mx = -inf;
    while (j < N) {
        j++;
        int app = A[j] + (N - 1) * B[j];
        mx = max(mx, total);
        total += app;
    }
    cout << max(total, mx);
}

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

/*
3
4 -1
-5 -1
10 -1
1 2 3
*/

/*
3
0 1
-100 1
101 1
-1 -1 -1
*/


