#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target ("avx2,bmi")
using namespace std;
// using namespace __gnu_pbds;
#define int long long
// typedef tree<int,null_type,less<>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
const int N = 1e6 + 10, mod = 998244353, mod1 = 1e9 + 9, inf = 1e18;
int d[N];

void solve() {
    int n, m;
    cin >> n;
    vector<int> a(n), 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];
    int j = 0;
    int q = -1;
    int ans = 0;
    for (int i = 0; i < n; i++) {
        ans += b[i] * (n - 1) + a[i];
    }
    cout << ans;
    return;
    for (int i = 0; i < n; i++) {
        if (p[i] != -1) {
            int w = 0, e = 0, w1 = 0, e1 = 0;
            for (; j < p[i]; j++) {
                if (b[j] < 0) {
                    w += b[j];
                    e += a[j];
                } else {
                    w1 += b[j];
                    e1 += a[j];
                }
            }
            ans += (q + 1) * w + e;
            ans += i * w1 + e1;
            q = i;
        }
    }
    int ans1 = ans;
    for (; j < n; j++) {
        if (b[j] < 0)ans += (q + 1) * b[j] + a[j];
        else ans += (n - 1) * b[j] + a[j];
        ans1 = max(ans1, ans);
    }
    cout << ans1;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
