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

using namespace __gnu_pbds;

using namespace std;
using ll = long long;
using ld = long double;

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#define o_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define int long long

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> a(n);
    vector<int> b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    vector<int> p(n + 1, -1);
    int lst = 0;
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        if (p[i] != -1) {
            lst = i;
        }
    }
    int mx = -1e15;
    vector<vector<int>> dp(n + 1, vector<int> (n + 1, -1e15));
    dp[0][0] = 0;
    for (int i = 0; i < n; i++) {
        for (int y = 0; y <= n; y++) {
            if (y > i) {
                dp[i][y] = max(dp[i][y], dp[i][y - 1] + a[y - 1] + b[y - 1] * i);
            }
            if (i > 0) {
                dp[i][y] = max(dp[i][y], dp[i - 1][y]);
            }
        }
        for (int y = 0; y <= n; y++) {
            if (!(y == p[i] || p[i] == -1)) {
                dp[i][y] = -1e15;
            }
        }
        if (i >= lst) {
            for (int y = 0; y <= n; y++) {
                mx = max(mx, dp[i][y]);
            }
        }
    }
    cout << mx;
}
