#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2")
#pragma GCC optimize("unroll-loops")

using namespace std;
using namespace __gnu_pbds;

using ll = long long;
using ld = long double;

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

// gp_hash_table<ll, ll> ht;

const ll INF = 1e16;

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    ll n;
    cin >> n;

    vector<ll> a(n), b(n);

    for (ll i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
    }

    vector<ll> p(n);
    ll k = 0;
    vector<pair<ll, ll>> temp;
    for (ll i = 0; i < n; ++i) {
        cin >> p[i];
        if (p[i] != -1) {
            temp.push_back({p[i], i});
        }
        k = max(k, p[i]);
    }

    vector<pair<ll, ll>> otr(n, {0, n});
    for (ll i = 0; i < n; ++i) {
        ll l, r;
        if (temp.empty()) {
            l = 0;
            r = i;
        } else {
            pair<ll, ll> t = {i + 1, 0};
            auto it = lower_bound(temp.begin(), temp.end(), t);
            if (it == temp.begin()) {
                l = 0;
            } else {
                it = prev(it);
                l = it->second + 1;
            }

            it = lower_bound(temp.begin(), temp.end(), t);
            if (it == temp.end()) {
                r = i;
            } else {
                r = it->second;
            }
        }
        otr[i] = {l, min(r, i)};
    }

    // for (int i = 0; i < n; ++i) {
    //     cout << otr[i].first << ' ' << otr[i].second << endl;
    // }
    
    vector<vector<ll>> dp(n, vector<ll>(n + 1, -INF));
    for (ll j = otr[0].first; j <= otr[0].second; ++j) {
        dp[0][j] = a[0] + b[0] * j;
    }
    for (ll i = 1; i < n; ++i) {
        for (ll j = otr[i].first; j <= otr[i].second; ++j) {
            for (ll w = otr[i - 1].first; w <= min(j, otr[i - 1].second); ++w) {
                if (dp[i - 1][w] == -INF) {
                    continue;
                }
                dp[i][j] = max(dp[i][j], dp[i - 1][w] + a[i] + b[i] * j);
            }
        }
    }

    ll res = -INF;
    for (int d = max(1ll, k); d <= n; ++d) {
        for (ll i = 0; i <= n; ++i) {
            // cout << d - 1 << ' ' << i << ": " << dp.size() << endl;
            res = max(res, dp[d - 1][i]);
        }
    }

    cout << res << '\n';

    return 0;
}
