// #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<vector<int>> dp;
vector<vector<int>> pref;

void solve() {
  cin >> n;
  a.resize(n);
  b.resize(n);
  lb.resize(n);
  rb.resize(n);
  dp.resize(n, vector<int>(n, LLONG_MIN));
  pref.resize(n, vector<int>(n, LLONG_MIN));
  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 = 0; i < n; ++i) {
    for (int t = lb[i]; t <= rb[i]; ++t) {
      int val = a[i] + b[i] * t;
      int prev = i == 0 ? 0 : pref[i - 1][t];
      if (prev != LLONG_MIN) {
        dp[i][t] = val + prev;
      }
    }
    pref[i][0] = dp[i][0];
    for (int j = 1; j < n; ++j) {
      pref[i][j] = max(dp[i][j], pref[i][j - 1]);
    }
  }
  int ans = max_p == -1 ? 0 : LLONG_MIN;
  for (int i = max((int)0, max_p); i < n; ++i) {
    ans = max(ans, pref[i][n - 1]);
  }
  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;
}