#ifndef LOCAL
#pragma GCC optimize("O3,unroll-loops")
#endif

#include <bits/extc++.h>

using namespace std;

#define int long long
#define ll long long
#define ull unsigned long long
#define all(x) x.begin(), x.end()

int32_t main() {
#ifndef LOCAL
    cin.tie(nullptr), ios_base::sync_with_stdio(false);
#endif
    int n;
    cin >> n;
    vector<int> a(n + 1), b(n + 1), p(n + 1);
    for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];
    for (int i = 1; i <= n; i++) cin >> p[i];
    vector<vector<int> > dp(n + 1, vector<int>(n, -1e15));
    int ans = LLONG_MIN;
    if (*max_element(all(p)) == -1) ans = 0;
    vector<int> max_t(n + 1, INT_MAX), min_t(n + 2, 0);
    for (int i = 1; i <= n; i++) {
        if (p[i] != -1) {
            max_t[p[i]] = i;
            min_t[p[i] + 1] = i;
        }
    }
    for (int i = 2; i <= n; i++) min_t[i] = max(min_t[i - 1], min_t[i]);
    for (int i = n - 1; i >= 1; i--) max_t[i] = min(max_t[i + 1], max_t[i]);
    dp[0][0] = 0;
    for (int i = 1; i <= n; i++) {
        int mx = dp[i - 1][0];
        for (int j = min_t[i]; j < min(i, max_t[i]); j++) {
            if (j != i - 1) mx = max(mx, dp[i - 1][j]);
            dp[i][j] = a[i] + b[i] * j + mx;
        }
    }
    for (int j = 0; j < n; j++) {
        if (p[j + 1] == -1) {
            for (int i = 1; i <= n; i++) ans = max(ans, dp[i][j]);
        }else {
            ans = max(ans, dp[p[j + 1]][j]);
        }
    }
    cout << ans;
    return 0;
}
