#include <bits/stdc++.h>

using namespace std;
using ll=long long;
using ld=long double;
using ull=unsigned long long;
using lll=__int128;

#ifdef LOCAL
#define deb(x) cout<<__LINE__<<": "<<(#x)<<" = "<<(x)<<endl;
#else
#define deb(x)
#endif
#define all(x) x.begin(), x.end()
#define arr(x) x.rbegin(), x.rend()
#define int ll
constexpr int INF=1e18;
signed main(){
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin>>n;
    vector<pair<int,int>> a(n);
    for (auto i=0; i<n; ++i) cin>>a[i].first>>a[i].second;
    vector<int> p(n);
    for (auto i=0; i<n; ++i) cin>>p[i];
    vector<int> d={p[0]};
    for (auto i=1; i<n; ++i) d.push_back(max(d.back(),p[i]));
    vector<int> t(n,n);
    int l=*max_element(all(p));
    for (int i=0; i<n; ++i) {
        if (p[i]!=-1) t[p[i]]=min(t[p[i]],i);
    }
    for (auto i=n-2; i>=0; --i) t[i]=min(t[i],t[i+1]);
    vector<int> s(n,-INF);
    int ans=-INF;
    s[0]=0;
    if (l==-1) ans=0;
    for (auto i=0; i<n; ++i) {
        vector<int> q(n, -INF);
        for (auto j=i; j>=0; --j) {
            if ((p[j]==-1 || i+1<=p[j]) && (j==0 || i+1>d[j-1])) {
                for (auto z=0; z<=j; ++z) {
                    q[j]=max(q[j],s[z]+a[i].first+j*a[i].second);
                }
            }
        }
        s=q;
        if (i+1>=l) for (auto j=0; j<n; ++j) {
            if (p[j]!=-1 && p[j]<i) continue;
            ans=max(ans, s[j]);
        }
    }
    cout<<ans;
}
