#include <bits/stdc++.h>

using namespace std;

#define int long long

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

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

int32_t 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) {
                for (int k = 0; k <= t; ++k) {
                    dp[i][t] = max(dp[i][t], dp[i - 1][k] + a[i] + t * 1ll * b[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";
    }

    return 0;
}