#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>

using namespace std;
using T = long long;
using D = long double;
using L = __int128_t;
const int MAXN = 5e3 + 10;
const T INF = -1e16;

T tmp[MAXN], dp[MAXN];
T a[MAXN], b[MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
    }

    fill(dp, dp + n + 1, INF);
    fill(tmp, tmp + n + 1, INF);
    tmp[0] = 0;
    for (int j = 1; j <= n; ++j) {
        tmp[j] = tmp[j - 1] + a[j - 1];
    }
    T currStart = 0, ans = INF;
    for (T t = 0; t < n; ++t) {
        T currP;
        cin >> currP;
        currP = max(currP, currStart);
        for (int i = currStart; i < currP; ++i) {
            dp[i] = INF;
        }

        for (int i = currP; i <= t; ++i) {
            dp[i] = tmp[i];
        }

        for (int j = max(t + 1, currP); j <= n; ++j) {
            dp[j] = max(dp[j - 1] + a[j - 1] + t * b[j - 1], max(tmp[j], tmp[j - 1] + a[j - 1] + t * b[j - 1]));
        }

        swap(tmp, dp);
    }

    for (int i = 0; i <= n; ++i) {
        ans = max(ans, tmp[i]);
    }

    cout << ans << '\n';
    return 0;
}