// #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, m;
vector<int> a, b;
vector<int> lb, rb;
vector<int> dp;
vector<vector<int>> start_cope;
vector<int> vs;
vector<int> pref;
vector<int> pref2;
vector<vector<int>> t;

void build_pref() {
  pref.resize(n);
  pref[0] = b[0];
  for (int i = 1; i < n; ++i) {
    pref[i] = b[i] + pref[i - 1];
  }
  pref2.resize(n);
  pref2[0] = a[0];
  for (int i = 1; i < n; ++i) {
    pref2[i] = a[i] + pref2[i - 1];
  }
}

int get_pref(int l, int r) {
  if (r < l) return 0;
  assert(l <= r);
  int res = pref[r];
  if (l > 0) {
    res -= pref[l - 1];
  }
  return res;
}

int get_pref2(int l, int r) {
  if (r < l) return 0;
  assert(l <= r);
  int res = pref2[r];
  if (l > 0) {
    res -= pref2[l - 1];
  }
  return res;
}

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;
  }
  build_pref();
  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 ans = 0;
  for (int i = 0; i < n; ++i) {
    if (b[i] > 0) {
      vs.push_back(i);
    }
  }
  m = vs.size();
  t.resize(m);
  for (int i = 0; i < m; ++i) {
    int l = i, r = m - 1;
    while (l < r) {
      int m = (l + r + 1) / 2;
      int su = get_pref(vs[i], vs[m] - 1);
      if (su > 0) {
        l = m;
      } else {
        r = m - 1;
      }
    }
    t[l].push_back(i);
  }
  set<int> coping;
  int val = 0;
  for (int i = m - 1; i >= 0; --i) {
    reverse(all(t[i]));
    for (const auto& j : t[i]) {
      // j now copes
      // seg bounds
      int l = vs[j];
      int r;
      auto it = coping.upper_bound(j);
      if (it == coping.end()) {
        assert(j == i);
        r = vs[i];
      } else {
        int k = *it;
        r = vs[k] - 1;
      }
      int new_h = rb[vs[j]];
      int old_h;
      if (it == coping.begin()) {
        old_h = 0;
      } else {
        --it;
        int k = *it;
        old_h = rb[vs[k]];
      }
      int delta = get_pref(l, r) * (new_h - old_h);
      val += delta;
      coping.insert(j);
    }
    int var = get_pref2(0, vs[i]) + val;
    ans = max(ans, var);
    val -= rb[vs[i]] * b[vs[i]];
    if (i != 0) {
      coping.erase(i);
      if (coping.size() > 0) {
        auto it = coping.end();
        --it;
        int x = vs[*it];
        int h = rb[x];

        int j = i - 1;
        int l = vs[j] + 1;
        int r = vs[i] - 1;
        int w = get_pref(l, r);
        val -= w * h;
      }
    }
  }
  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;
}