#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n; cin >> n;
    vector<int> a(n + 1), b(n + 1);
    for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];
    vector<int> p(n + 1, -1);
    int mx = 0;
    for (int i = 1; i <= n; i++) {
        cin >> p[i];
        p[i] = max(p[i], p[i - 1]);
        mx = max(mx, p[i]);
    }
    vector<vector<int>> dp(n + 1, vector<int>(n + 1, -INT_MAX));
    dp[0][0] = 0;
    for (int t = 0; t < n; t++) {
        for (int i = max(1, t); i <= n; i++) {
            if (i < p[t]) continue;
            if (t > 0) dp[t][i] = dp[t - 1][i];
            if (dp[t][i - 1] == -INT_MAX) continue;
            dp[t][i] = max(dp[t][i], dp[t][i - 1] + b[i] * t + a[i]);
        }
    }
    int ans = -INT_MAX;

    for (int t = 0; t < n; t++) {
        for (int i = mx; i <= n; i++) {
            ans = max(ans, dp[t][i]);
        }
    }
    cout << ans << '\n';
    return 0;
}