#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);
    vector <ll> prefB(n + 1, 0);
    vector <ll> prefA(n + 1, 0);
    for (int i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
        prefA[i + 1] = prefA[i] + a[i];
        prefB[i + 1] = prefB[i] + (b[i] * i);
    }
    vector <int> p(n);
    vector <ll> minT(n, -1);
    vector <pair <int, int>> pIdx;
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        if (p[i] != -1) {
            pIdx.eb(i, p[i]);
        }
    }
    if (!pIdx.empty()) {
        for (int i = 0; i < pIdx[0].second; ++i) {
            minT[i] = 0;
        }
        for (int i = 1; i < pIdx.size(); ++i) {
            if (pIdx[i].second == pIdx[i - 1].second) {
                continue;
            }
            for (int j = pIdx[i - 1].second; j < pIdx[i].second; ++j) {
                minT[j] = pIdx[i - 1].first + 1;
            }
        }
        int nextT = pIdx.back().first + 1;
        for (int i = 0; i < n; ++i) {
            if (minT[i] == -1) {
                minT[i] = nextT;
            }
        }
    } else {
        minT.assign(n, 0);
    }
    int minK = 0;
    ll curAns = 0;
    if (!pIdx.empty()) {
        minK = pIdx.back().second;
        for (int i = 0; i < minK; ++i) {
            curAns += a[i] + (b[i] * minT[i]);
        }
    }
    ll ans = curAns;
    for (int i = minK; i < n; ++i) {
        curAns += a[i] + (b[i] * minT[i]);
        ans = max(ans, curAns);
    }
    cout << ans;
}