#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;

// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")

#define int long long
#define ff first
#define ss second
#define endl "\n"
#define all(x) x.begin(), x.end()
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update<>>

const int MOD = 998244353;
const int INF = 1e18;


void solve() {
    int n;
    cin >> n;

    vector<int> a(n, 0);
    vector<int> b(n, 0);

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

    vector<int> p(n, 0);

    for (int i = 0; i < n; i++) {
        cin >> p[i];
    }

    int ans = 0;

    vector<int> suf;

    int csa = 0;
    int csuf = 0;

    vector<array<int, 3> > posl;

    int used = 1;

    int cvl = 0;

    for (int k = 0; k < n; k++) {
        csa += a[k];
        csuf += b[k];
        suf.push_back(csuf);
        while (posl.size()) {
            if (posl.back()[0] + csuf - suf[posl.back()[2]] > 0) {
                break;
            }
            used -= posl.back()[1];
            cvl -= posl.back()[0] * posl.back()[1];
            posl.pop_back();
        }
        while (posl.size() && posl.back()[0] + csuf - suf[posl.back()[2]] <= b[k]) {
            used -= posl.back()[1];
            cvl -= posl.back()[0] * posl.back()[1];
            posl.pop_back();
        }
        cvl += (used - 1) * b[k];
        if (k - used + 1 > 0 && b[k] > 0) {
            cvl += b[k] * (k - used + 1);
            posl.push_back({b[k], k - used + 1, k});
            used = k + 1;
        }
        ans = max(ans, cvl + csa);
    }

    cout << ans << endl;
}


signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    // cin >> t;

    while (t--) {
        solve();
    }
}
