// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2")
#include <bits/stdc++.h>

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

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll INF = 1e17;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector <int> a(n), b(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
    }
    vector <int> p(n);
    int maxi = 0;
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        maxi = max(maxi, p[i]);
    }
    ll ans = (maxi ? -INF : 0);
    vector <vector <ll>> dp(n + 1, vector <ll> (n, -INF));
    dp[0][0] = 0;
    for (int i = 1; i <= n; ++i) {
        ll last = -INF;
        for (int T = 0; T < i && (p[i - 1] == -1 || T < p[i - 1]); ++T) {
            last = max(last, dp[i - 1][T]);
            dp[i][T] = last + b[i - 1] * (ll) T + a[i - 1];
            if (maxi <= i) ans = max(ans, dp[i][T]);
        }
    }
    cout << ans << "\n";
    return 0;
}