#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long

using namespace __gnu_pbds;
using namespace std;

template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;



signed main() {
    int n;
    cin >> n;
    vector<pair<int, int>> a(n);
    vector<pair<int, int>> og(n);
    for (int i = 0; i < n; ++i) {
        og[i] = {0, i};
    }
    for (int i = 0; i < n; ++i) {
        cin >> a[i].first >> a[i].second;
    }
    vector<int> p(n);
    int mn = 0;
    int nw = 0;
    int toch = -1;
    for (int i = 0; i  < n; ++i) {
        cin >> p[i];
        if (p[i] == -1) continue;
        toch = p[i] - 1;
        for (int j = nw; j < p[i]; ++j) {
            og[j].second = min(og[j].second, i);
            og[j].first = max(og[j].first, mn);
        }
        mn = i + 1;
        nw = p[i];
    }
    while (nw < n) {
        og[nw].first = max(og[nw].first, mn);
        nw++;
    }
    vector<vector<int>> dp(n, vector<int> (n, -1e17));
    dp[0][0] = a[0].first;
    for (int i = 1; i < n; ++i) {
        int mx = -1e17;
        for (int j = 0; j < n; ++j) {
            mx = max(mx, dp[i - 1][j]);
            if (j >= og[i].first && j <= og[i].second) {
                dp[i][j] = mx + a[i].first + j * (a[i].second);
            }
        }
    }
    int mx = -1e17;
    for (int i = n - 1; i >= toch; --i) {
        if (i == -1) {
            mx = max(mx, (int)0);
            continue;
        }
        for (int j = 0; j < n; ++j) {
            mx = max(mx, dp[i][j]);
        }
    }
    cout << mx << '\n';
}