#include <bits/stdc++.h>

using namespace std;

#define int long long
#define pb push_back
#define ff first
#define ss second

void solve() {
    int n;
    cin >> n;
    vector<pair<int, int>> a(n);
    for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second;
    vector<int> b(n);
    for (int i = 0; i < n; i++) cin >> b[i];
    int sm = 0;
    int pr = 0;
    int ind = 0;
    for (int i = 0; i < n; i++) {
        if (b[i] != -1) {
            for (int j = ind; j < b[i]; j++) {
                sm += a[j].ff;
                sm += a[j].ss * pr;
            }
            ind = b[i];
            pr = i + 1;
        }
    }
    for (int j = ind; j < n; j++) {
        if (a[j].ff + a[j].ss * pr >= 0) sm += (a[j].ff + a[j].ss * pr);
        else break;
    }
    cout << sm;
}

signed main() {
//    freopen("input.txt", "rb", stdin);
//    freopen("output.txt", "wb", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
//    cin >> t;
    while (t--) solve();
}
