#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using i128 = __int128_t;

const ll inf = 1e18 + 1e16;
const ll inf_t = 1e9 + 7;
const ll mod = 998244353;

#define int ll

void solve() {
    int n;
    cin >> n;

    vector <int> a(n);
    vector <int> b(n);

    for (int i = 0; i < n; ++i) cin >> a[i] >> b[i];

    vector <int> p(n);
    for (int i = 0; i < n; ++i) cin >> p[i];
    p.push_back(n);

    int nxt[n + 1];
    nxt[n] = n;
    for (int i = n - 1; i >= 0; --i) {
        if (p[i + 1] != -1) {
            nxt[i] = i + 1;
        } else {
            nxt[i] = nxt[i + 1];
        }
    }

    vector <int> pref(n + 1);
    pref[0] = 0;
    for (int i = 1; i <= n; ++i) pref[i] = pref[i - 1] + b[i - 1];

    vector <int> ind;
    for (int i = 0; i <= n; ++i) {
        if (p[i] != -1) ind.push_back(i);
    }

    int time = 0;
    int ans = 0;
    int pos = 0;
    int answer = -inf;

    int last_time = 0;
    for (int i = 0; i < n; ++i) {
        if (p[i] == -1) continue;
        last_time = i;
    }

    for (int i = 0; i < ind.size() - 1; ++i) {
        while (time < ind[i]) {
            if (pos == p[ind[i]]) {
                ++time;
                continue;
            }
            if (pos == time) {
                ans += a[pos] + time * b[pos];
                ++pos;
                continue;
            }
            if (pref[p[ind[i]]] - pref[pos] < 0) {
                ans += a[pos] + time * b[pos];
                ++pos;
            } else {
                ++time;
            }
        }
        while (pos < p[ind[i]]) {
            ans += a[pos] + time * b[pos];
            ++pos;
        }
    }

    answer = ans;

    cout << answer;
}

signed main() {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
}
