#include <bits/stdc++.h>

using namespace std;

const int MOD = 998244353;

#define int long long

int powmod(int v, int mod) {
    int cr = 1;
    while (mod) {
        if (mod & 1) cr *= v;
        v *= v;
        cr %= MOD; v %= MOD; mod >>= 1;
    }
    return cr % MOD;
}

int32_t main() {
    int n; cin >> n;
    vector<pair<int, int>> a(n); for (auto &i : a) cin >> i.first >> i.second;

    vector<int> p(n); for (auto &i : p) cin >> i;
    vector<vector<int>> dp(n, vector<int>(n, -1e18));
    vector<int> f(n), s(n); for (int i = 0; i < n; i++) f[i] = (i ? f[i - 1] : 0) + a[i].first;
    for (int i = 0; i< n; i++) s[i] = (i ? s[i - 1] : 0) + a[i].second;
    int ps = 0;
    int prev = 0;
    int cr = 0;
    int rs = -1e18;
    for (auto &i : p) {
        if (!(i + 1)) {
            dp[prev - 1][0] = ps;
            for (int i1 = prev; i1 < n; i1++) {
                int mn = -1e18;
                for (int j = 0; j < i1; j++) mn = max(mn, dp[i1 - 1][j]), dp[i1][j] = max(dp[i1][j], mn + a[i1].first + a[i1].second * (j + 1));
                dp[i1][i1] = max(dp[i1][i1], mn + a[i1].first + a[i1].second * i1);
            }
            break;
        }
        for (int j = prev; j < i; j++) ps += a[j].first + a[j].second * cr; cr++;
        if (!(i - n)) rs = max(rs, ps);
        prev = i;
    }
    for (auto &i : dp) for (auto &j : i) rs = max(rs, j);
    // int rs = 0;
    // vector<int> dp(n); dp[0] = a[0].first; rs = max(rs, a[0].first);
    // for (int i = 1; i < n; i++) dp[i] = dp[i - 1] + a[i].first + a[i].second * i, rs = max(rs, dp[i]);
    cout << rs;
    return 0;
}