#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 <pair <int, int>> pIdx;
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        if (p[i] != -1) {
            pIdx.eb(i, p[i]);
        }
    }
    ll ans = 0;
    for (int k = 1; k <= n; ++k) {
        for (int j = 0; j <= k; ++j) {
            ll curAns = prefA[k] + prefB[k] - prefB[j];
            ans = max(ans, curAns);
        }
    }
    cout << ans;
}