//#pragma GCC optimize("Ofast,unroll-loops,fast-math")
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define cin(v) for (auto& el : v) {cin >> el;}
#define cout(v) for(auto& el : v) {cout << el << " ";} cout << "\n";

using namespace std;
using ll = long long;
using db = double;
const ll MOD = 1e9 + 7;
const ll MOD2 = 1072005019;
const ll MOD3 = 998244353;

void solve() {
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    vector<int> p(n);
    cin(p);
    /*vector<int> min_p = p;
    min_p.push_back(n);
    for (int i = n - 1; i >= 0; i--) {
        min_p[i] = min_p[i + 1];
        if (p[i] !=- 1) {
            min_p[i] = p[i];
        }
    }*/
    if (*max_element(all(p)) == -1) {
        ll ans = 0, sum = 0;
        for (int i = 0; i < n; i++) {
            sum += a[i] + 1ll * i * b[i];
            ans = max(ans, sum);
        }
        cout << ans;
        return;
    }
    vector<int> t(n);
    int cnt = 0;
    int i = 0;
    for (int cur_t = 0;cur_t < n; cur_t++) {
        while (cur_t > i) {
            t[i] = i;
            i++;
            cnt++;
        }
        if (p[cur_t] == -1) {
            continue;
        }
        while (cnt != p[cur_t]) {
            t[i] = cur_t;
            i++;
            cnt++;
        }
    }
    for (int j = i; j < n; j++) {
        t[j] = j;
    }
    ll ans = 0, sum = 0;
    for (int z = 0; z < n; z++) {
        sum += a[z] + 1ll * b[z] * t[z];
        ans = max(ans, sum);
    }
    cout << ans;
    return;
}

signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int multitest = 0;
    if (multitest) {
        int t;
        cin >> t;
        while (t--) {
            solve();
        }
    } else {
        solve();
    }
}