#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx,avx2,bmi2,fma")
// mt19937 rnd(24523);


unordered_map<int, int> Fw;
void update(int ind, int val) {
    ind += 10;
    for (int i = ind; i < 1e17; i = (i | (i - 1)) + 1) {
        Fw[i] += val;
    }
}
int get(int ind) {
    ind += 10;
    int ans = 0;
    for (int i = ind; i > 0; i = (i & (i - 1))) {
        ans += Fw[i];
    }
    return ans;
}

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n, sum0 = 0;
    cin >> n;
    vector<array<int, 2>> arr(n);
    for (auto& [a, b] : arr) {
        cin >> a >> b;
        sum0 += a;
    }

    int ans = LLONG_MIN;
    for (int k = 1; k <= n; ++k) {
        int now = 0;
        for (int i = 0; i < k; ++i) {
            now += arr[i][0];
        }
        vector<int> pref(k + 1);
        for (int i = k - 1; i >= 0; --i) {
            pref[i] = pref[i + 1] + arr[i][1];
        }
        vector<array<int, 2>> order;
        for (int i = 1; i < k; ++i) {
            if (pref[i] > 0) {
                order.push_back({pref[i], i});
            }
        }
        Fw.clear();
        sort(order.rbegin(), order.rend());
        for (auto& [val, i] : order) {
            int mx = max(0ll, i - get(i));
            now += val * mx;
            update(i, mx);
        }
        ans = max(ans, now);
    }
    if (ans == LLONG_MIN) {
        exit(1);
    }
    cout << ans << endl;
}   

/*
4
-6 -1
-5 14
0 10
-30 2
-1 -1 -1 -1

23

*/