#include <bits/stdc++.h>

using namespace std;

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

//@formatter:off
template <typename T> istream& operator>>(istream& in, vector<T> &vec) { for (auto &i : vec) in >> i; return in; }
template <typename T> ostream& operator<<(ostream& out, vector<T> &vec) { for (auto &i : vec)  out << i << ' '; out << '\n'; return out; }
//@formatter:on
#define int ll


void solve() {
    int n;
    cin >> n;
    vector<ll> a(n + 1), b(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> a[i] >> b[i];
    }
    vector<int> p(n), rp(n + 1);
    cin >> p;

    const ll INF = 1e18;
    vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, -INF));

    dp[0][0] = 0;
    ll ans = (p[0] == -1 ? 0 : -INF);
    vector<int> bl(n + 1, 0), br(n + 1, n + 1);
    p.push_back(n);
    for (int ind = 1, i = 0, lst = 0; i <= n; i++) {
        while (ind <= p[i]) {
            bl[ind] = lst;
            br[ind] = i + 1;
            ind++;
        }
        if (p[i] != -1) lst = i + 1;
    }

    for (int j = 1; j <= n; j++) {
        ll res = -INF;
        for (int i = 0; i < j; i++) {
            res = max(res, dp[i][j - 1]);
            if (bl[j] <= i && i < min(j, br[j]))
                dp[i][j] = max(dp[i][j], res + a[j] + b[j] * i);
        }
    }
    int mx = *max_element(p.begin(), p.end());
    for (int i = 0; i <= n; i++) {
        for (int j = max(0ll, mx); j <= n; ++j) {
            if (bl[j] <= i && i < min(j, br[j]))
                ans = max(ans, dp[i][j]);
        }
    }
    cout << ans << '\n';
}

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    // cin >> t;
    while (t--) solve();
}