using ll = long long;
using namespace std;
#include <iostream>
#include <vector>
#include <algorithm>
#define all(x) (x).begin(), (x).end()
#pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
int main() {
    ll n;
    cin >> n;
    vector <ll> a(n), b(n), p(n);
    for (ll i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
    }
    for (ll i = 0; i < n; ++i)
        cin >> p[i];
    for (ll i = n - 1; i > 0; --i) {
        if (p[i - 1] == -1)
            p[i - 1] = p[i];
    }
    ll ans = -1e18, cur = 0;
    ll i = 0, j = 0;
    for (; i < n; ++i) {
        if (p[i] == -1)
            break;
        while (j < p[i]) {
            cur += a[j] + b[j] * i;
            ++j;
        }
    }
    ans = cur;
    for (; j < n; ++j) {
        cur += a[j] + b[j] * j;
        ans = max(ans, cur);
    }
    cout << ans;
}
