#include <iostream>
#include <cmath>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <numeric>
#include <iomanip>
#include <random>
#include <chrono>
#define eb emplace_back

using namespace std;

using ll = long long;
using lll = __int128;
using db = double;
using ld = long double;

const int INF = 1'000'000'000;
const ll LINF = 1'000'000'000'000'000'000;
const ll MOD = 1'000'000'007;

int rnd() {
    return (rand() / RAND_MAX);
}

int main() {
    ios_base::sync_with_stdio();
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector <ll> a(n), b(n);
    ll sumA = 0, sumB = 0;
    vector <ll> prefB(n + 1, 0);
    vector <ll> prefA(n + 1, 0);
    int ptrPol = n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
        if (b[i] > 0) {
            ptrPol = min(ptrPol, i);
        }
        prefA[i + 1] = prefA[i] + a[i];
        prefB[i + 1] = prefB[i] + (b[i] * i);
        sumA += a[i];
    }
    vector <int> p(n);
    int minK = 0, firstOtr = n;
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        minK = max(minK, p[i]);
        if (p[i] == -1) {
            firstOtr = min(firstOtr, i);
        }
    }
    vector <ll> curT(n, -1);
    for (int i = 0; i < firstOtr; ++i) {
        if (i == 0) {
            for (int j = 0; j < p[i]; ++j) {
                curT[j] = 0;
            }
            continue;
        }
        for (int j = p[i - 1]; j < p[i]; ++j) {
            curT[j] = i;
        }
    }
    ll ans = -LINF;
    ll curDop = 0;
    for (int i = 0; i < n; ++i) {
        if (curT[i] == -1) {
            break;
        }
        curDop += a[i] + (curT[i] * b[i]);
    }
    ll curAns = curDop;
    ans = max(ans, curDop);
    for (int i = firstOtr; i < n; ++i) {
        curAns += a[i] + (b[i] * firstOtr);
        ans = max(ans, curAns);
    }
    cout << ans;
}