#include <bits/stdc++.h>

using namespace std;

int main() {
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    long long n;
    cin >> n;
    vector<long long> a(n), b(n);
    for(long long i = 0; i < n; i++){
        cin >> a[i] >> b[i];
    }
    vector<pair<long long, long long>> p;
    for(long long i = 0; i < n; i++){
        long long ch;
        cin >> ch;
        if(ch != -1){
            p.push_back({ch, i + 1});
        }
    }
    long long ans = 0;
    long long cool = 0;
    for(long long i = 0; i < n; i++){
        long long mint = 0;
        long long j = lower_bound(p.begin(), p.end(), pair<long long, long long>{i + 1, 0}) - p.begin() - 1;
        if(j >= 0){
            mint = p[j].second;
        }
        ans += mint * b[i] + a[i];
        cool = max(cool, ans);
    }
    cout << cool;
}
