#include <bits/stdc++.h>
using namespace std;

template<typename T>
istream& operator>>(istream& stream, vector<T>& a) {
    for (auto& x : a) stream >> x;
    return stream;
}
template<typename T>
ostream& operator<<(ostream& stream, vector<T>& a) {
    for (auto& x : a) stream << x << ' ';
    cout << '\n';
    return stream;
}
template<typename T1, typename T2>
istream& operator>>(istream& stream, vector<pair<T1, T2>>& a) {
    for (auto& [x, y] : a) stream >> x >>  y;
    return stream;
}
template<typename T1, typename T2>
ostream& operator<<(ostream& stream, vector<pair<T1, T2>>& a) {
    for (auto& [x, y] : a) stream << x << ' ' << y << '\n';
    return stream;
}
template<typename T1, typename T2>
inline void amax(T1& a, T2 b) {
    a = max(a, b);
}
template<typename T1, typename T2>
inline void amin(T1& a, T2 b) {
    a = min(a, b);
}

#define int int64_t
#define all(x) x.begin(), x.end()
#define pb emplace_back
#define ff first
#define ss second
#define sz(x) (int)(x.size())
#define deb cout << "----------------------"
#define MIN(x) *min_element(all(x))
#define MINID(x) min_element(all(x)) - x.begin()
#define MAX(x) *max_element(all(x))
#define MAXID(x) max_element(all(x)) - x.begin()
#define push emplace
#define SUM(x) accumulate(all(x), 0ll)
#define sj(x) sort(all(x)), x.erase(unique(all(x)), x.end())

mt19937 rnd(221);

using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
using vvi = vector<vi>;
using vpi = vector<pi>;
using vvpi = vector<vpi>;
using ld = long double;

const int64_t INF = 1e18;
const int INF2 = 1e9;
const int mod = 998244353;
const ld eps = 1e-6;

int bpow(int x, int st) {
    if (st == 0) return 1;
    int res = bpow(x, st/ 2);
    res = res * res; res %= mod;
    if (st % 2 == 1) res *= x;
    res %= mod;
    return res;
}

const int N = 1e5 + 2323;
const int D = 500;

void solve() {
    int n; cin >> n;
    vpi v(n); cin >> v;
    vi p(n); cin >> p;
    vvi dp(n + 1, vi(n + 1, -INF));
    dp[0][0] = 0;
    int mx = 0;
    if (MAX(p) != -1) mx = -INF;
    vector<vector<int>> go(n + 1);
    int need = 0;
    vector<int> ma_go(n + 1);
    iota(all(ma_go), 0);
   // cout << ma_go;
    for (int i = 0; i < n; i++) {
        if (p[i] != -1) {
            for (int j = 1; j <= p[i]; j++) {
                amin(ma_go[j], i + 1);
            }
        }
    }
   // cout << ma_go;
    int ma22 = MAX(p);
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        if (i - 2 >=0) {
            dp[i - 2].shrink_to_fit();
        }
        auto [a, b] = v[i - 1];
        int ma = dp[i - 1][0];
      //  cout << ma_go[i] << endl;
        for (int j = 0; j < ma_go[i]; j++) {
            amax(ma, dp[i - 1][j]);
            dp[i][j] = ma + (a + j * b);//mx на префиксе?
            if (i >= ma22) {
                amax(mx, dp[i][j]);
            }
        }
        //cout << dp[i];
    }
    cout << mx;
}

int32_t main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    int tt = 1;
    // cin >> tt;
    while (tt--) {
        solve();
    }
}
