#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
//template<class T> using ordered_set = tree<T , null_type, less<T> , rb_tree_tag, tree_order_statistics_node_update>;
//template<class T> using ordered_multiset = tree<T , null_type, less_equal<T> , rb_tree_tag, tree_order_statistics_node_update>;

#define ll int
#define fi first
#define se second
#define pb push_back


void solve(){


}

ll C = -99999999;

signed main()
{
//    cin.tie(0);
//    cout.tie(0);
//    ios_base::sync_with_stdio(false);
    ifstream cin("input.txt");



    ll t;
    t=1;
//    cin >> t;
    for(ll o=0;o<t;o++){
    ll n;
    ll ans=C,l=0,r;
    cin >> n;
//    return;
    vector<pair<ll,ll>> v(n);
    for(ll i=0;i<n;i++){
        cin >> v[i].fi >> v[i].se;
    }
//    return;
    vector<ll> p(n);
    for(ll i=0;i<n;i++){
        cin >> p[i];
        if (i>0 and p[i]!=-1){
            p[i]=p[i]-p[i-1];
        }
    }
//    return 0;
    vector<vector<ll>> dp;
    vector<ll> mn(n+2,0);
    dp.pb(vector<ll> (1));
    dp[0][0]=0;
    if (p[0]!=-1){
//        cout << "A";
        for(ll i=1;i<=p[0];i++){
            dp.pb(vector<ll> (1));
            dp[i][0] = dp[i-1][0] + v[i-1].fi;
        }
        mn[dp.size()]=1;
    }



    for(ll i=1;i<n;i++){
        if (p[i]==-1){
            break;
        }
        for(ll j=0;j<p[i];j++){
            dp.pb(vector<ll> (0));
            ll x = dp.size()-1;
            // ot mn[x] do i;
            vector<ll> prefmax;
            ll s=C;
            for(ll k=0;k<dp[x-1].size();k++){
                s = max(s , dp[x-1][k]);
                prefmax.pb(s);
            }
            for(ll k=mn[x];k<=i;k++){
                if (j==0){
//                  dp[x][k-mn[x]] = prefmax[min(k-mn[x-1]-1, (ll)prefmax.size()-1)] + v[x-1].fi + v[x-1].se * k;
                    dp[x].pb( prefmax[min(k-mn[x-1]-1, (ll)prefmax.size()-1)] + v[x-1].fi + v[x-1].se * k );
                }else{
//                    dp[x][k-mn[x]] = prefmax[min(k-mn[x-1], (ll)prefmax.size()-1)] + v[x-1].fi + v[x-1].se * k;
                    dp[x].pb ( prefmax[min(k-mn[x-1], (ll)prefmax.size()-1)] + v[x-1].fi + v[x-1].se * k );
                }
            }
//            cout << "A";
            mn[x+1]=mn[x]+1;
        }
    }
//    return 0;
    ll z=0;
    for(ll i=0;i<n;i++){
        if (p[i]!=-1){
            z++;
        }
    }
    if (z!=n){

        for(ll i=0;i<dp[dp.size()-1].size();i++){
            ans=max(ans,dp[dp.size()-1][i]);
        }
//        return 0;
//        mn[dp.size()+1] = mn[dp.size()]+1
        while (dp.size()<n+1){
            dp.pb(vector<ll> (0));
            ll x = dp.size()-1;
//            return 0;
            mn[x+1]=mn[x];
//            return 0;
            vector<ll> prefmax;
            ll s=C;
//            return 0;

            for(ll k=0;k<dp[x-1].size();k++){
                s=max(s , dp[x-1][k]);
                prefmax.pb(s);
            }
//            cout << x  << " " << prefmax.size() << " " << mn[x] << "\n";
//            return 0;
            for(ll k=mn[x];k<x;k++){
//                    cout << prefmax.size();
                if (p[max(x-2,0)]!=-1){
//                    dp[x][k - mn[x]] = prefmax[min(prefmax.size()-1 , k-mn[x-1]-1)] + v[x-1].fi + v[x-1].se * k;
                    dp[x].pb ( prefmax[min((ll)prefmax.size()-1 , k-mn[x-1]-1)] + v[x-1].fi + v[x-1].se * k );
                }else{
//                    dp[x][k - mn[x]] = prefmax[min(prefmax.size()-1 , k-mn[x-1])] + v[i-1].fi + v[x-1].se * k;
                    dp[x].pb( prefmax[min((ll)prefmax.size()-1 , k-mn[x-1])] + v[x-1].fi + (v[x-1].se * k) );
//                    cout << x << " " << prefmax[min((ll)prefmax.size()-1 , k-mn[x-1])] << " " << k << " " << v[x-1].fi << " " << v[x-1].se << "\n";
//                    cout << dp[x][k-mn[x]] << "\n";
                }
//                cout << 1;
                ans = max(ans,dp[x][k-mn[x]]);
            }
        }
    }
//    return 0;
    cout << ans;
//    cout << "ANS: " << ans << "\n";
//    for(ll i=0;i<dp.size();i++){
//        cout << mn[i] << " | ";
//        for(ll j=0;j<dp[i].size();j++){
//            cout << dp[i][j] << " ";
//        }
//        cout << "\n";
//    }
    }
}
