#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 3, mod = 998244353, inf = 1e18 + 7;
int a[N], b[N], p[N];
int dp[N][20];
signed  main(){
     ios::sync_with_stdio(false);
     cin.tie(nullptr);
     int n;
     cin >> n;
     int ans = 0, cur = 0;
     vector<int> rem;
     rem.push_back(1);
     for (int i = 1; i<= n; i++){
        cin >> a[i] >> b[i];
        if (b[i] > 0){
            rem.push_back(i);
        }
     }
     for (int i = 0; i <= n; i++){
        for (int j = 0 ;j < rem.size(); j++){
            dp[i][j] = -inf;
        }
     }
     dp[0][0] = 0;
     for (int i = 1; i <= n; i++){
        for (int j = 0 ;j < rem.size(); j++){
            if (i >= rem[j]){
                dp[i][j] = max(dp[i - 1][j] + a[i] + b[i] * (rem[j] - 1), dp[i][j]);
            }
            if (j + 1 < rem.size() && i >= rem[j + 1]){
                dp[i][j + 1] = max(dp[i - 1][j] + a[i] + b[i] * (rem[j + 1] - 1), dp[i][j + 1]);
            }
        }
        for (int j = 0; j < rem.size(); j++){
            ans = max(ans, dp[i][j]);
        }
     }
     cout << ans;
     for (int i = 0 ;i < n; i++){
        cin >> p[i];
     }

     /*int sm = 0;
     for (int i = 0; i<= n; i++){
        for (int j = 0; j < 2; j++){
            dp[j][i] = -inf;
        }
     }
     for (int  i= 0; i <= n; i++){
        sm +=a[i];
        if (p[0] == -1){
            dp[0][i] = sm;
        } else {
            if (i == p[0]){
                dp[0][i] = sm;
            }
        }
        //cout << dp[0][i] << " ";
     }
     //cout << "\n";
     //cout << dp[0][3] << "\n";
     for (int i = 1; i < n; i++){
        if (p[i] != -1){
            int sm = 0;
            for (int j = p[i]; j >= i; j--){
                dp[1][p[i]] = max(dp[1][p[i]], dp[0][j] + sm);
                sm += a[j] + b[j] * i;
            }
        } else {
            int best = dp[0][i];
            dp[1][i] = dp[0][i];
            for (int t = i + 1; t <= n; t++){
                best = max(best + a[t] + b[t] * i, dp[0][t]);
                dp[1][t] = best;
            }
        }
     }
     int ans = -inf;
     for (int i = n - 1; i >= 0; i--){
        for (int j = 0; j <= n; j++){
            ans = max(ans, dp[i][j]);
        }
        if (p[i] != -1){
            break;
        }
     }

     cout << ans;*/
 }

