#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()
#define debout(x) std::cout << #x << " : " << x << '\n';
#define debvout(x) std::cout << #x << " : "; for (auto i: x) std::cout << i << ' '; std::cout << '\n';
using ll = long long;

const ll inf = 1e18;

void s1nm() {
  ll n;
  std::cin >> n;
  std::vector<ll> a(n), b(n), p(n);
  for (ll i = 0; i < n; i++) std::cin >> a[i] >> b[i];
  for (ll &i: p) std::cin >> i;
  std::vector<ll> mxt(n), mnt(n);
  for (ll i = 1; i < n; i++) mxt[i] = i;
  ll temp = 0, temp2 = 0;
  for (ll i = 0; i < n; i++) {
    if (p[i] == -1) continue;
    while (temp < p[i]) mxt[temp] = std::min(mxt[temp], i), mnt[temp] = temp2, temp++;
    temp2 = i + 1;
  }
  while (temp < n) mnt[temp] = temp2, temp++;
  std::vector<ll> prefa(n), prefb(n);
  std::partial_sum(all(a), prefa.begin()), std::partial_sum(all(b), prefb.begin());
  std::vector<ll> dp(n, -inf);
  dp[0] = a[0];
  for (ll i = 1; i < n; i++) {
    for (ll j = 0; j <= i; j++) {
      if (mxt[j] < mnt[i]) continue;
      dp[i] = std::max({dp[i], (j != 0 ? dp[j - 1] - prefa[j - 1] : 0) + prefa[i] +  (prefb[i] - (j != 0 ? prefb[j - 1] : prefb[j])) * mnt[i], 
       (j != 0 ? dp[j - 1] - prefa[j - 1] : 0) + prefa[i] +  (prefb[i] - (j != 0 ? prefb[j - 1] : prefb[j])) * mxt[i]});
    }
  }
  ll e = *std::max_element(all(p));
  std::cout << (e == -1 ? std::max(0ll, *std::max_element(all(dp))) : *std::max_element(dp.begin() + *std::max_element(all(p)) - 1, dp.end()));
}

signed main() {
  std::ios::sync_with_stdio(false), std::cin.tie(nullptr);
  ll TTT = 1;
  // std::cin >> TTT;
  while (TTT--) {
    s1nm();
    std::cout << '\n';
  }
}