#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

int main(){
    int n;
    cin >> n;
    vector<ll> a(n + 1),b(n + 1),p(n + 1);
    for(int i = 1;i <= n;i++){
        cin >> a[i] >> b[i];
    }
    int aa = 0;
    vector<int> ind(n + 1,-1),t1(n + 1,-1),t2(n + 2,-1);
    for(int i = 1;i <= n;i++){
        cin >> p[i];
        if(p[i] != -1){
            ind[p[i]] = i;
            t1[p[i]] = i;
            t2[p[i] + 1] = i;
            aa = p[i];
        }
    }
    ll an = -1e18;
    vector<ll> ans(n + 1);
    int previ = 0,prevj = 0;
    vector<vector<ll>> dp(n + 1,vector<ll> (n,-1e18));
    dp[0][0] = 0;
    for(int i = 1;i <= n;i++){

        vector<ll> ma(n,-1e18);
        ma[0] = dp[i-1][0];
        for(int j = 1;j < n;j++){
            ma[j] = max(ma[j-1],dp[i-1][j]);
        }
        for(int j = 0;j < i;j++){
            dp[i][j] = a[i] + b[i]*j + ma[j];
            //ans = max(ans,dp[i][j]);
        }
        if(t1[i] != -1){
            for(int j = t1[i];j < n;j++){
                dp[i][j] = -1e18;
            }
        }
        if(t2[i] != -1){
            for(int j = 0;j < t2[i];j++){
                dp[i][j] = -1e18;
            }
        }

    }
    for(int i = aa;i <= n;i++){
        for(int j = 0;j < n;j++){
            an = max(an,dp[i][j]);
        }
    }
    cout << an;
    return 0;
}
