#include <bits/stdc++.h>

#define int long long

using ll = long long;
using ld = long double;
using namespace std;
mt19937 rnd(239);

const int inf = (int)1e18;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);

    int n, otv = 0, mx = -inf, mn = inf; cin >> n;
    vector <int> a(n), b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
        mx = max(mx, b[i]);
        mn = min(mn, b[i]);
    }
    vector <int> p(n);
    for (int i = 0; i < n; i++) cin >> p[i];
    if (n <= 5000) {
        vector<vector<int>> dp(n, vector<int>(n, -inf));
        dp[0][0] = a[0];
        for (int i = 1; i < n; i++) {
            mx = -inf;
            for (int j = 0; j <= i; j++) {
                if (j < i) mx = max(mx, dp[i - 1][j]);
                dp[i][j] = mx + a[i] + j * b[i];
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= i; j++) {
                otv = max(otv, dp[i][j]);
            }
        }
        cout << otv << "\n";
        return 0;
    }
    if (mx <= 0) {
        int cur = 0;
        for (auto i : a) {
            cur += i;
            otv = max(otv, cur);
        }
        cout << otv << "\n";
        return 0;
    }
    if (mn >= 0) {
        int cur = 0;
        for (int i = 0; i < n; i++) {
            cur += a[i] + i * b[i];
            otv = max(otv, cur);
        }
        cout << otv << "\n";
        return 0;
    }
    vector <int> otr(n + 1, 0);
    for (int i = n - 1; i >= 0; i--) {
        otr[i] = max((int)0, a[i] + i * b[i] + otr[i + 1]);
    }
    int cur = 0;
    for (int i = 0; i < n; i++) {
        cur += a[i];
        otv = max(otv, cur + otr[i + 1]);
    }
    cout << otv << "\n";
    return 0;
}
