#ifndef WILEYNE
#pragma GCC optimize("Ofast")
#endif
#include <bits/extc++.h>

using ll = long long;
const ll LL_INF = 2e18;
using namespace std;
using ordered_set = __gnu_pbds::tree<ll,__gnu_pbds::null_type,less<>,__gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;
const int MOD = 998244353;
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();



int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#ifdef WILEYNE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif
    int n;
    cin>>n;
    vector<pair<ll,ll>>v(n);
    for (auto &c:v) cin>>c.first>>c.second;
    vector<int>p(n,0);
    int min_pick = 0;
    int maxs_pick = 0;
    //int ind_last = 0;
    for (int i = 0;i<n;++i) {
        cin>>p[i];
        if (p[i]!=-1) {
           // ind_last = i;
        }
        min_pick = max(min_pick,p[i]);
    }
    int u = 0;
    ll cur_res = 0;
    ll maxs_res = -LL_INF;
    if (min_pick == 0) {
        maxs_res = 0;
    }
    p.push_back(n);
    for (int i = 0;i<n;++i) {
        while (i<n && p[i] == -1) {
            i++;
        }
        while (u<p[i]) {
            cur_res+=v[u].first+v[u].second*min(i,u);
            if (u+1>=min_pick) {
                maxs_res = max(maxs_res,cur_res);
            }
            ++u;
        }
    }
    cout<<maxs_res;
    return 0;
}