#include <bits/stdc++.h>
#define int long long
using namespace std;

const int inf = 1e18;

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    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 &x : p) cin >> x;
    vector<int> start(n), wall(n + 1, -1);
    for(int i = 0; i < n; ++i) start[i] = i;
    int m = 0;
    for(int i = 0; i < n; ++i){
        if(p[i] != -1){
            start[p[i] - 1] = min(start[p[i] - 1], i);
            wall[p[i]] = i + 1;
            m = p[i];
        }
    }
    wall[0] = 0;
    for(int i = n - 2; i >= 0; --i){
        start[i] = min(start[i], start[i + 1]);
    }
    int ans = -inf, sum = 0;
    vector<int> x, c;
    for(int i = 0; ; ++i){
        if(wall[i] != -1){
            x.push_back(wall[i]);
            c.push_back(-inf);
        }
        if(i >= m) ans = max(ans, sum);
        if(i == n) break;
        x.push_back(start[i]);
        c.push_back(-b[i]);
        sum += a[i] + b[i]*start[i];
        while(c.back() > 0){
            int y = x.back(), d = c.back();
            x.pop_back(); c.pop_back();
            sum += (y - x.back()) * d;
            c.back() += d;
        }
    }
    cout << ans << endl;
}