#include <bits/stdc++.h>

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

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

void solve() {
    int n;
    cin >> n;
    vector <pair <ll, ll>> a(n);
    for (auto &[x, y] : a) {
        cin >> x >> y;
    }
    vector <int> p(n);
    int lst = 0;
    bool have = 0;
    int time = 1;
    int _ = 1;
    for (auto &e : p) {
        cin >> e;
        if (e != -1) {
            have = true;
            time = _;
        }
        _++;
        lst = max(lst, e);
    }
    vector <int> need(n, n + 1);
    int t = n + 1;
    for (int i = 0; i < n; i++) {
        if (p[i] == -1) continue;
        for (int j = 0; j < p[i]; j++) {
            if (need[j] == n + 1) {
                need[j] = i;
            }
        }
    }
    vector <vector <ll>> dp(n + 1, vector <ll> (n + 1, -9e18));
    vector <vector <ll>> mx(n + 1, vector <ll> (n + 1, -9e18));
    fill(all(mx[0]), 0);
    fill(all(dp[0]), 0);
    for (int i = 1; i <= n; i++) {
        for (int j = i; j > 0; j--) {
            for (int time = 0; time < j; time++) {
                if (time >= need[j - 1]) {continue;}
                ll sum = 0;
                for (int k = j - 1; k < i; k++) {
                    sum += a[k].first + (ll)time * a[k].second;
                }
                if (mx[j - 1][max(0, time - 1)] != -9e18) {
                    dp[i][time] = max(dp[i][time], mx[j - 1][max(0, time - 1)] + sum);
                }
            }
        }
        for (int k = 0; k < n; k++) {
            if (p[k] != -1 && p[k] >= i) {
                for (int time = k + 1; time <= n; time++) {
                    dp[i][time] = -9e18;
                }
            }
        }
        ll mxx = -9e18;
        for (int time = 0; time <= n; time++) {
            mxx = max(mxx, dp[i][time]);
            mx[i][time] = mxx;
        }
    }
    ll ans = -9e18;
    for (int i = lst; i <= n; i++) {
        for (int t = time - 1; t <= n; t++) {
            ans = max(ans, dp[i][t]);
        }
    }
    cout << ans;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
}

// Halal code
