#include <bits/stdc++.h>

using namespace std;

const long long inf = 8e18;
const int N = 1e6 + 5;

int a[N], b[N], p[N];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);

    int n;
    cin >> n;

    for (int i = 1; i <= n; ++i) {
        cin >> a[i] >> b[i];
    }

    bool sub = 1;
    for (int i = 1; i <= n; ++i) {
        cin >> p[i];
        sub = (p[i] != -1 ? 0 : sub);
    }

    if (sub && n <= 5000) {
        vector<vector<long long>> dp(n + 5, vector<long long>(n + 5, -inf));
        dp[0][0] = 0;

        vector<long long> mx(n + 1, 0);
        for (int i = 1; i <= n; ++i) {
            for (int t = 0; t < i; ++t) {
                dp[i][t] = mx[t] + a[i] + t * 1ll * b[i];
                if (t == 0) mx[t] = dp[i][t];
                else mx[t] = max(dp[i][t], mx[t - 1]);
            }

            mx[i] = mx[i - 1];
        }
    
        long long ans = 0;
        for (int i = 1; i <= n; ++i) {
            for (int t = 0; t < i; ++t) {
                ans = max(ans, dp[i][t]);
            }
        }
    
        cout << ans << "\n";
    } else if (n <= 5000) {
        vector<vector<long long>> dp(n + 5, vector<long long>(n + 5, -inf));
        dp[0][0] = 0;

        vector<int> to(n + 1);
        for (int i = 1; i <= n; ++i) {
            to[i] = i - 1;
        }
        
        int mn_p = p[1];
        for (int i = 1; i <= n; ++i) {
            mn_p = max(mn_p, p[i]);
            if (p[i] != -1) {
                for (int j = 1; j <= p[i]; ++j) {
                    to[j] = min(to[j], i - 1);
                }
            }
        }

        vector<long long> mx(n + 1, 0);
        for (int i = 1; i <= n; ++i) {
            for (int t = 0; t <= to[i]; ++t) {
                dp[i][t] = mx[t] + a[i] + t * 1ll * b[i];
                if (t == 0) mx[t] = dp[i][t];
                else mx[t] = max(dp[i][t], mx[t - 1]);
            }

            mx[to[i] + 1] = mx[to[i]];
        }
    
        long long ans = -inf;
        for (int i = 1; i <= n; ++i) {
            for (int t = 0; t < i; ++t) {
                ans = max(ans, dp[i][t]);
            }
        }
    
        cout << ans << "\n";
    } else {
        cout << 0 << "\n";
    }

    return 0;
}