
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
#define int int64_t
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using namespace std;


const int INF = 1e15;

int32_t main() {
    cin.tie(0)->sync_with_stdio(0);
    int n; cin >> n;
    vector<int> a(n), b(n);
    
    for (int i = 0; i < n; i++)
        cin >> a[i] >> b[i];

    vector<int> p(n);
    for (int &i : p)
        cin >> i;

    vector<int> mx(n + 1, INF);

    for (int i = 0; i < n; i++) {
        if (p[i] != -1)
            mx[p[i] - 1] = min(i, mx[p[i] - 1]);
    }

    for (int i = n - 2; i >= 0; i--) {
        mx[i] = min(mx[i], mx[i + 1]);
    }

    vector<int> mx1(n, 1);

    for (int i = 1; i <= n; i++)
        mx1[i - 1] = i;
    
    for (int i = 0; i < n; i++)
        if (p[i] != -1)
            mx1[i] = p[i];

    for (int i = 1; i < n; i++)
        mx1[i] = max(mx1[i - 1], mx1[i]);

    vector<int> dp(n, -INF);



    int res = -INF;

    dp[0] = a[0];
    if (mx1[0] == 1)
        res = max(res, a[0]);


    for (int i = 1; i < n; i++) {
        int cur_mx = -INF;
        vector<int> dp1(n, -INF);

        for (int j = 0; j <= min(i, mx[i]); j++) {
            cur_mx = max(cur_mx, dp[j]);

            if (mx1[j] >= i+1) {
                dp1[j] = a[i] + b[i]*j + cur_mx;   
            }
        }
        
        swap(dp1, dp);

        for (int j = 0; j < n; j++)
            if (mx1[j] == i+1)
                res = max(res, dp[j]);
    }

    cout << res;    
}