
#include <bits/stdc++.h>

using namespace std;
#define ll long long
const ll INF = 1e15;
const ll mod = 998244353;

struct node{
    ll a, b;
};


signed main() {
    ios_base::sync_with_stdio(false), cin.tie(0);
    ll n;
    cin >> n;
    vector<node> v (n);
    for (int i = 0; i < n; ++i) {
        cin >> v[i].a >> v[i].b;
    }
    vector<ll> b (n);
    for (int i = 0; i < n; ++i) {
        cin >> b[i];
        b[i]--;
    }
    ll last1 = -1;
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
        if (b[i] == -1) continue;
        for (int j = last1+1; j <= b[i]; ++j) {
            ans += v[j].a + v[j].b * i;
        }
        last1 = b[i];
    }
    if (b[n-1] == -1) {
        for (int j = last1 + 1; j < n; ++j) {
            ans += v[j].a + v[j].b * (n - 1);
        }
    }
    cout << ans;
}