#include "bits/stdc++.h"

using namespace std;

template<typename T>
istream &operator>>(istream &is, vector<T> &a) {
      for (T &elem: a) is >> elem;
      return is;
}

template<typename T>
ostream &operator<<(ostream &os, const vector<T> &a) {
      for (T elem: a) os << elem << ' ';
      return (os << '\n');
}

#define int long long

signed main() {
#ifdef LOCAL
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
#else
      cin.tie(nullptr);
      ios_base::sync_with_stdio(false);
#endif
      int n;
      cin >> n;
      vector<int> a(n), b(n);
      for (int i = 0; i < n; ++i) cin >> a[i] >> b[i];

      vector<int> c(n);
      cin >> c;

      vector<int> l(n, 0), r(n);
      iota(r.begin(), r.end(), 0ll);

      vector<pair<int, int>> d;

      for (int i = 0; i < n; ++i) {
            if (c[i] != -1) {
                  d.push_back({c[i], i});
            }
      }
      d.push_back({n, n});

//      for (int i = 0; i < c[0]; ++i) l[i] = 0;
      for (int i = 0; i < n; ++i) {
            auto it = upper_bound(d.begin(),d .end(), pair<int, int> {i, 1e9});
            r[i] = it->second;
            it = upper_bound(d.begin(), d.end(), pair<int, int> {i, 1e9});
            if (it != d.begin()) {
                  it--;
                  l[i] = it->second + 1;
            } else {
                  l[i] = 0;
            }

      }

      for (int i = 0; i < n; ++i) {
            r[i] = min(r[i], i);
      }

//      int last = 0;
//      for (int i = 0; i < n; ++i) {
//            if (c[i] != -1) {
//                  for (int i = 0; i < last; ++i) {
//
//                  }
//                  for (int j = 0; j < c[i]; ++j) {
//                        r[j] = i;
//                  }
//            }
//      }
//
//      cout << l << r;
      vector dp(n, vector<int>(n, -1e18));
      vector p(n, vector<int>(n, -1e18));
      dp[0][0] = a[0];
      p[0][0] = a[0];
      for (int i = 1; i < n; ++i) {
            p[i - 1][0] = dp[i - 1][0];
            for (int val = 1; val < n; ++val) {
                  p[i - 1][val] = max(p[i - 1][val - 1], dp[i - 1][val]);
            }
            for (int val = l[i]; val <= r[i]; ++val) {
                  dp[i][val] = p[i - 1][val] + a[i] + b[i] * val;
            }
      }
//      cout << r;
//      cout << dp;
      int res = (*std::max_element(c.begin(), c.end()) == -1 ? 0 : -1e18);

      for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                  res = max(res, dp[i][j]);
            }
      }
      cout << res << '\n';

}