#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define cin(x) for (auto &i : (x)) cin >> i;
#define cout(x) for (auto &i : (x)) cout << i << " "; cout << "\n"
#define fori(x) for (int i = 0; i<(x); i++)
#define forj(x) for (int j = 0; j<(x); j++)
#define fork(x) for (int k = 0; k<(x); k++)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(), (x).rend()

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
template<class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

long long calc(vector<long long> a) {
    long long re = 0;
    long long now = 0;
    fori(a.size()) {
        now += a[i];
        re = max(re, now);
    }
    return re;
}

void solve() {
    int n;
    cin >> n;
    vector<long long> a(n), b(n);
    fori(n) {
        cin >> a[i] >> b[i];
    }
    vector<int> p(n);
    cin(p);
    long long ans = 0;

    long long an = 0;
    vector<pair<long long, int> > f;
    fori(n) {
        an += a[i] + b[i] * i;
        f.emplace_back(b[i], i);
        while (f.back().second != 0 && f.back().first <= 0) {
            auto [bb, t] = f.back();
            f.pop_back();
            an -= (t - f.back().second) * bb;
            f.back().first += bb;
        }
        ans = max(ans, an);
    }
    cout << ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int tt = 1;
    // cin >> tt;
    while (tt--) {
        solve();
    }
    return 0;
}
