#include<bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
using ll = long long;
using ld = long double;

#define int long long
const int maxn = 1e6 + 2;
const int inf = 1e18 + 1;

int a[maxn], b[maxn];

int p[maxn], l[maxn];

int pref_a[maxn], pref_b[maxn];

vector<int> idx;

int dp[20][maxn];

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n; cin >> n;
    idx.push_back(0);
    for (int i = 1; i < n + 1; i++) {
        cin >> a[i] >> b[i];
        if (b[i] > 0) {
            idx.push_back(i);
        }
    }
    idx.push_back(n + 1);
    for (int i = 1; i < n + 1; i++) {
        pref_a[i] = pref_a[i - 1] + a[i];
        pref_b[i] = pref_b[i - 1] + b[i];
    }
    int mx = 0;
    for (int i = 1; i < n + 1; i++) {
        cin >> p[i];
    }
    int res = 0;
    dp[0][0] = 0;
    dp[1][0] = pref_a[idx[1] - 1] - pref_a[idx[0] - 1];
    int cur = 0;
    for (int i = 1; i < n + 1; i++) {
        cur += a[i];
        res = max(res, cur);
    }
    for (int i = 1; i < n + 1; i++) {
        dp[0][i] = -inf;
        dp[1][i] = -inf;
    }
    int sz = (int)idx.size();
    for (int i = 1; i + 1 < sz; i++) {
        int pref = -inf;
        int sum = 0, cur = idx[i + 1] - 1;
        for (int j = 0; j < i; j++) {
            while (cur > idx[i] && a[cur] + b[cur] * j < 0) {
                cur--;
            }
            pref = max(pref, dp[i][j]);
            dp[i][j] = pref + a[idx[i]] + b[idx[i]] * j;
            dp[i + 1][j] = pref +  pref_a[idx[i + 1] - 1] - pref_a[idx[i - 1] - 1] + (pref_b[idx[i + 1] - 1] - pref_b[idx[i] - 1]) * j;
            res = max(res, dp[i][j] + pref_a[cur] - pref_a[idx[i]] + (pref_b[cur] - pref_b[idx[i]]) * j);
        }
        for (int j = i; j < n + 1; j++) {
            dp[i][j] = -inf;
        }
    }
    cout << res << "\n";
}
