// #define _GLIBCXX_DEBUG
// #pragma GCC optimize("O3,unroll-loops")
#include<iostream>
#include<vector>
using namespace std;
using ll = long long;
using ld = long double;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    ll n;
    cin>>n;
    vector<ll> a(n), b(n);
    vector<pair<ll, ll>> p;
    for (ll i = 0; i<n; i++){
        cin>>a[i]>>b[i];
    }
    for (ll i = 0; i<n; i++){
        ll c;
        cin>>c;c--;
        if (c != -2){p.push_back({c, i});}
    }
    vector<ll> mi(n), ma(n);
    ll ind = 0;
    ll t = -1;
    for (ll i = 0; i<n; i++){
        while (ind != p.size()){
            if (p[ind].first > i-1){break;}
            t = p[ind].second;
            ind++;
        }
        mi[i] = t+1;
    }
    ind = p.size()-1;
    t = 1e18;
    for (ll i = n-1; i>=0; i--){
        while (ind >= 0){
            if (p[ind].first < i){break;}
            t = p[ind].second;
            ind--;
        }
        ma[i] = min(i, t);
    }
    // cout<<'@'<<endl;
    // for (auto&e:mi){cout<<e<<' ';}cout<<endl;
    // for (auto&e:ma){cout<<e<<' ';}cout<<endl;
    vector<vector<ll>> dp(n, vector<ll> (n, -1e18)), pr(n, vector<ll> (n+1, -1e18));
    for (ll i = mi[0]; i <= ma[0]; i++){
        dp[0][i] = a[i];
    }
    for (ll i = 0; i<n; i++){
        pr[0][i+1] = max(pr[0][i], dp[0][i]);
    }
    for (ll i = 1; i<n; i++){
        for (ll j = mi[i]; j<= ma[i]; j++){
            dp[i][j] = pr[i-1][j+1] + a[i] + b[i]*j;
        }
        for (ll j = 0; j<n; j++){
            pr[i][j+1] = max(pr[i][j], dp[i][j]);
        }
    }
    ll las = 0;
    if (p.size()){
        las = p[p.size()-1].first;
    }
    ll ans = -1e18;
    for (ll i = las; i<n; i++){
        for (ll j = 0; j<n; j++){
            ans = max(ans, dp[i][j]);
        }
    }
    cout<<ans<<endl;
    return 0;
}