#include <iostream>
#include <algorithm>
#include <chrono>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#define int long long

using namespace std;
using ll = long long;

void solve() {
    int n; cin >> n;
    vector<pair<ll, ll>> a(n);
    vector<ll> p(n);
    ll last = -1;
    for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second;
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        if (p[i] != -1) last = i;
    }
    bool mm = false;
    ll answ = a[0].first, mx = -2e18 - 1000;
    ll cur = 1;
    for (int i = 0; i < n; i++) {
        if (p[i] == -1) {
            continue;
        }
        for (int j = cur; j < p[i]; j++) {
            answ += a[j].first + i*a[j].second;
            cur++;
        }
        if (i >= last) mx = max(mx, answ);
    }
    if (cur < n) {
        for (int j = cur; j < n; j++) {
            answ += a[j].first + (last+1)*a[j].second;
            cur++;
            mx = max(mx, answ);
        }
    }
    cout << mx;
}

signed main() {
    cin.tie(0);
    solve();
    return 0;
}