#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define cin(x) for (auto &i : (x)) cin >> i;
#define cout(x) for (auto &i : (x)) cout << i << " "; cout << "\n"
#define fori(x) for (int i = 0; i<(x); i++)
#define forj(x) for (int j = 0; j<(x); j++)
#define fork(x) for (int k = 0; k<(x); k++)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(), (x).rend()

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
template<class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

long long calc(int st, vector<long long> a, vector<long long> b) {
    int n = a.size();
    long long an = 0;
    vector<pair<long long, int> > f;
    fori(n) {
        an += a[i] + b[i] * (i + st);
        f.emplace_back(b[i], i + st);
        while (f.back().second != st && f.back().first <= 0) {
            auto [bb, t] = f.back();
            f.pop_back();
            an -= (t - f.back().second) * bb;
            f.back().first += bb;
        }
    }
    return an;
}

void solve() {
    int n;
    cin >> n;
    vector<long long> a(n), b(n);
    fori(n) {
        cin >> a[i] >> b[i];
    }
    vector<int> p(n);
    cin(p);


    long long ans = 0;

    int now = 0;

    int i = 0;
    while (i < n) {
        int j = i;
        while (j < n && p[j] == -1) j++;
        int now1 = n;
        if (j != n) now1 = p[j] - now;
        vector<long long> a1, b1;
        for (int k = now; k < now1; k++) {
            a1.push_back(a[k]);
            b1.push_back(b[k]);
        }
        long long an = calc(i, a1, b1);
        if (j != n) ans += an;
        else ans += max(0ll, an);


        now = now1;
        i = j + 1;
    }


    cout << ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int tt = 1;
    // cin >> tt;
    while (tt--) {
        solve();
    }
    return 0;
}
