#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

const int MOD = 1e9 + 7;
const int Inf = 2e9;
const ll INF = 3e18;

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  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);
  vector <int> pmx(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    pmx[i] = max((i > 0 ? pmx[i - 1] : -2), p[i]);
  }
  vector <int> nxt(n, -1);
  for (int i = n-1; i >= 0; i--) {
    if (i < n-1) nxt[i] = nxt[i+1];
    if (p[i] != -1) nxt[i] = p[i];
  }
  vector <int> mn(n, n), mx(n, 0);
  for (int i = 0; i < n; i++) {
    int L = -1;
    int R = i;
    while (R - L > 1) {
      int M = (L + R) / 2;
      if ((pmx[M] < i && pmx[M] != -1) || (pmx[M] == i && nxt[M] == i)) {
        L = M;
      } else {
        R = M;
      }
    }
    mn[i] = R;
    L = -1;
    R = i;
    while (R - L > 1) {
      int M = (L + R) / 2;
      if (pmx[M] < i + 1) {
        L = M;
      } else {
        R = M;
      }
    }
    mx[i] = R;
  }
  // vector <vector <ll>> dp(n + 1, vector <ll> (n + 1));
  // for (int i = 0; i < n; i++) {
  //   fill(dp[i+1].begin(), dp[i+1].end(), -INF);
  //   for (int j = 0; j <= i; j++) {
  //     for (int k = 0; k <= j; k++) {
  //       dp[i+1][j] = max(dp[i+1][j], dp[i][k] + a[i] + (ll)b[i] * j);
  //     }
  //   }
  // }
  ll ans = -INF;
  ll cur = 0;
  for (int k = 1; k <= n; k++) {
    cur += a[k-1];
    ans = max(ans, cur);
    // for (int i = 0; i <= n; i++) {
    //   ans = max(ans, dp[k][i]);
    // }
  }
  cout << ans << '\n';
  return 0;
}