#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 1e18;
signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    vector<int> p(n);
    int lsti = -1;
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        if (p[i] != -1) lsti = i;
    }
    multiset<int> vals;
    int cur_sm = 0;
    for (int i = 0; i < n; i++) {
        cur_sm += a[i] + b[i] * i;
        vals.insert(cur_sm);
    }
    int ans = 0;
    int mns = 0;
    cur_sm = 0;
    int cur_sm2 = 0;
    for (int i = 0; i < n; i++) {
        cur_sm2 += a[i];
        cur_sm += a[i] + b[i] * i;
        vals.erase(vals.find(cur_sm));
        mns += cur_sm;
        int tmp = cur_sm2;
        if (vals.size()) {
            tmp += (*vals.rbegin()) - mns;
        }
        ans = max(ans, tmp);
    }
    cout << ans << '\n';
}
