#pragma GCC optimize("O3,unroll-loops")
#include <string>
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

const ll INF = 1e18;

struct Pupil {
    ll x;
    ll y;
};

void Solve () {
    int n;
    cin >> n;
    vector<Pupil>a(n);
    for (auto& [x, y] : a) {
        cin >> x >> y;
    }
    vector<int>p(n);
    for (int& i : p) {
        cin >> i;
    }
    int left = 0;
    vector<int>tp(n);
    for (int i = 0; i < n; ++i) {
        for (; left < p[i]; ++left) {
            tp[left] = i;
        }
    }
    int ogr = 0;
    for (int i = 0; i < n; ++i) {
        ogr = max(ogr, p[i]);
    }
    vector<ll>ans(n + 1);
    for (int i = 1; i <= n; ++i) {
        ans[i] = ans[i - 1] + a[i - 1].x + max(tp[i], i) * a[i - 1].y;
    }
    ll ansi = -INF;
    for (int i = ogr; i <= n; ++i) {
        ansi = max(ansi, ans[ogr]);
    }
    cout << ansi;
}

signed main () {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    //cin >> t;
    while (t--) {
        Solve();
    }
}