#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #pragma GCC optimize("O3,Ofast,unroll-loops")
// #pragma GCC target("avx2,bmi")

using namespace std;
// using namespace __gnu_pbds;
// template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

void solve();
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif
    int t = 1;
    while (t--) {
        solve();
    }
}

#define int long long
const int MAXN =2e5, MOD = 998244353, INF = 2e18;




void solve() {
    int n;
    cin >> n;
    vector<int> a(n + 1), b(n + 1);
    vector<int> pos;
    pos.push_back(0);
    for (int i = 1; i <=n; i++) {
        cin >> a[i] >> b[i];
        if (b[i] > 0) {
            pos.push_back(i);
        }
    }
    vector<int>p(n);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
    }
    int ans = 0;
    int now = 0;
    vector<vector<int>> dp(pos.size() + 1, vector<int>(n + 1, -INF));
    vector<int> prefa(n + 1), prefb(n + 1);
    for (int i = 0; i < n; i++) {
        prefa[i + 1] = prefa[i] + a[i + 1];
        prefb[i + 1] = prefb[i] + prefb[i + 1];
    }

    dp[0][0] = 0;
    for (int i = 1; i <= pos.size(); i++) {
        for (int j = 0; j < pos[i]; j++) {
            dp[i-1][j] += prefa[pos[i]-1] - prefa[pos[i-1]] + (prefb[pos[i]-1] - prefb[pos[i-1]]) * j;
        }
        for (int j= 0; j < pos[i]; j++) {
            dp[i][j] = max(dp[i][j], dp[i-1][j] + a[i] + b[i] * j);
        }
        for (int j = 1; j <= n; j++) {
            dp[i][j]  = max(dp[i][j-1], dp[i][j]);
        }
        ans = max(ans, dp[i][n]);
    }
    cout << ans;
}