#include <bits/stdc++.h>
using namespace  std;
#define int long long
const int INF = 1e18;
const int mod = 998244353;
vector <int> give_obr(vector <int> vec) {
    vector <int> ans(vec.size());
    for (int i = 0;i < vec.size();i++) {
        ans[vec[i]] = i;
    }
    return ans;
}
int binpow(int a,int b) {
    if (!b)return 1;
    if (b%2==0) {
        int mid = binpow(a,b/2);
        return (mid * mid) % mod;
    }
    return (a * binpow(a,b-1) )% mod;
}
int give_sum(vector <int> &vec) {
    int n = vec.size();
    int ans = 0;
    for (int i = 0;i < n;i++) {
        ans += binpow(vec[i]+1,i+1);
        if (ans >= mod) {
            ans-=mod;
        }
    }
    return ans;
}
void sub_solve(int n,int q) {
    vector <int> vec(n);
    for (int i =0;i < n;i++) {
        cin >> vec[i];
        vec[i]--;
    }
    int mask = (1<<3);
    vector <int> var_ans(mask);
    vector <int> all_zap(q);
    for (int i = 0;i < q;i++) {
        cin >> all_zap[i];
    }
    for (auto el : all_zap) {
        vector <int> tmp = vec;
        if (el == 2) {
            for (int i = 0;i < n;i++) {
                tmp[i] = n - vec[i] - 1;
            }
        }
        if (el == 1) {
            for (int i = 0;i < n;i++) {
                tmp[i] = vec[n-i-1];
            }
        }
        if (el == 3) {
            for (int i = 0;i < n;i++) {
                tmp[vec[i]] = i;
            }
        }
        vec = tmp;
        cout << give_sum(vec) << " ";

    }
}
signed main() {
    int n;
    cin >> n;
    vector <pair<int,int>> vec(n);
    for (int i = 0;i < n;i++) {
        cin >> vec[i].first >> vec[i].second;
    }
    vector <int> pi(n);
    int min_sz = 0;
    for (int i  =0;i < n;i++) {
        cin >> pi[i];
        min_sz = max(min_sz,pi[i]);
    }
    vector <int> l(n),r(n,n-1);
    int f_free = 0;
    int last_pi = 0;
    for (int i = 0;i < n;i++) {
        r[i] = min(r[i],i);
        if (pi[i] != -1) {
            for (int l_now = pi[i];l_now < n;l_now++) {
                l[l_now] = max(l[l_now],i + 1);
            }
            for (int r_now = pi[i] - 1;r_now >= 0;r_now--) {
                r[r_now] = min(r[r_now],i);
            }
        }
    }
    vector <vector <int>> dp(n,vector <int> (n,-INF));
    dp[0][0] = vec[0].first;
    for (int i = 1;i < n;i++) {
        dp[0][i] = dp[0][i-1];
    }
    for (int i = 1;i < n;i++) {
        for (int t_now = l[i];t_now <= r[i];t_now++) {
            int val_now = vec[i].first + t_now * vec[i].second;
            dp[i][t_now] = max(dp[i][t_now],val_now + dp[i-1][t_now]);
        }
        for (int t_now = 1;t_now < n;t_now++) {
            dp[i][t_now] = max(dp[i][t_now],dp[i][t_now-1]);
        }
    }
    if (min_sz == 0) {
        int best_ans = 0;
        for (int i = 0;i < n;i++) {
            best_ans = max(best_ans,dp[i].back());
        }
        cout << best_ans;
    }else {
        int best_ans = -INF;
        for (int i = min_sz - 1;i < n;i++) {
            best_ans = max(best_ans,dp[i].back());
        }
        cout << best_ans;
    }

}