#include <bits/stdc++.h>

using namespace std;

#define ll long long

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  ll n;
  cin >> n;

  vector <pair<ll, ll>> ve(n+1);
  for (ll i = 1; i <= n; ++i) {
    cin >> ve[i].first >> ve[i].second;
  }

  bool f = 1;
  vector <ll> p(n+1);
  ll cnt = 0;
  for (ll i = 1; i <= n; ++i) {
    cin >> p[i];
    if (p[i] > 0) f = 0;
    cnt = max(cnt, p[i]);
  }

  vector <ll> mi(n+2, -1e18), ma(n+2, 1e18);
  for (ll i = 1; i <= n; ++i) {
    if (p[i] == -1) continue;
    ma[p[i]] = min(ma[p[i]], (ll)i-1);
    mi[p[i]+1] = max(mi[p[i]+1], (ll)i);
  }

  /*for (auto e: mi) {
    cout << e << " ";
  }
  cout << "\n";

  for (auto e: ma) {
    cout << e << " ";
  }
  cout << "\n";*/

  vector <vector<ll>> dp(n+1, vector <ll> (n+1, -1e18));

  dp[1][0] = ve[1].first;
  for (ll i = 2; i <= n; ++i) {
    ll maxx = -1e18;
    for (ll j = 0; j <= i-1; ++j) {
      maxx = max(dp[i-1][j], maxx);
      if (j >= mi[i] && j <= ma[i]) {
        //cout << i << " " << j << "\n";
        dp[i][j] = maxx + ve[i].first + ve[i].second * j;
      }
    }
  }

  ll ans = -1e18;
  if (f) ans = 0;
  for (ll i = 0; i <= n; ++i) {
    for (ll j = 0; j <= n; ++j) {
      //cout << setw(20) << dp[i][j] << " ";
      if (i >= cnt) ans = max(ans, dp[i][j]);
    }
    //cout << "\n";
  }
  
  cout << ans << "\n";

  return 0;
}

