// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define int long long
using ld = long double;
using ll = long long;
using pii = pair<int, int>;

struct Ent {
  int a, b;
};

int n;
vector<int> a, b;
vector<int> lb, rb;
vector<int> dp;

void solve() {
  cin >> n;
  a.resize(n);
  b.resize(n);
  lb.resize(n);
  rb.resize(n);
  dp.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i] >> b[i];
    lb[i] = 0;
    rb[i] = i;
  }
  int max_p = -1;
  for (int i = 0; i < n; ++i) {
    int p;
    cin >> p;
    if (p == -1) continue;
    --p;
    max_p = max(max_p, p);
    rb[p] = min(rb[p], i);
    if (p == n - 1) continue;
    lb[p + 1] = max(lb[p + 1], i + 1);
  }
  for (int i = 1; i < n; ++i) {
    lb[i] = max(lb[i], lb[i - 1]);
    assert(lb[i] <= rb[i]);
  }
  for (int i = max_p - 1; i >= 0; --i) {
    rb[i] = min(rb[i], rb[i + 1]);
  }
  int pref = 0;
  int ans = max_p == -1 ? 0 : LLONG_MIN;
  for (int i = 0; i < n; ++i) {
    if (b[i] < 0) {
      pref += a[i] + lb[i] * b[i];
    } else {
      pref += a[i] + rb[i] * b[i];
    }
    if (i >= max_p) {
      ans = max(ans, pref);
    }
  }
  cout << ans << '\n';
}

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

#ifdef ROMALOCAL
  freopen("in.txt", "r", stdin);
#endif

  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}