#include <bits/stdc++.h>
using innt = int;
#define int long long
using namespace std;
using ll = long long;
const int T = 5000  + 10;
int dp[T][T];
signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> a(n), b(n), p(n + 1);
    for (int i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
    }
    for (int i = 1; i <= n; ++i) {
        cin >> p[i];
    }
    p[0] = -1;
    int mx = -1e16;
    for (int j = 0; j <= n; ++j) {
        int og = n;
        if (p[j] != -1) {
            og = p[j];
        }
        for (int i = 1; i <= og; ++i) {
            if (j == 0) {
                dp[j][i] = dp[j][i - 1] + a[i - 1];
            } else {
                if (j >= i) {
                    dp[j][i] = dp[j - 1][i];
                } else {
                    dp[j][i] = max(dp[j][i - 1] + a[i - 1] + b[i - 1]*j, dp[j - 1][i]);
                }
            }
        }
        if (p[j] == -1) {
            for (int i = 0; i <= n;++i) {
                mx = max(mx, dp[j][i]);
            }
        } else {
            mx = dp[j][p[j]];
        }
    }

    cout << mx;
    return 0;
}