#include <iostream>
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long

template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using ll = long long;
using vi = vector<int>;
using pi = pair<int, int>;
#define sz(x) (int)(x.size())
#define all(x) x.begin(), x.end()
//#define endl "\n"

template<class T>
istream& operator >> (istream& in, pair<T, T>& a) {
    in >> a.first >> a.second;
    return in;
}
template<class T>
istream& operator >> (istream& in, vector<T>& a) {
    for (T& x : a){
        in >> x;
    }
    return in;
}
template<class T>
ostream& operator << (ostream& out, pair<T, T> a) {
    out << a.first << " " << a.second << " ";
    return out;
}
template<class T>
ostream& operator << (ostream& out, vector<T> a) {
    for (auto& x : a) {
        out << x << " ";
    }
    return out;
}
#define debug(x) cout << (#x) << ": " << (x) << endl

constexpr int inf = 1e9 + 1;
constexpr ll INF = 1e18 + 1;

int solve1(int n, vi& a, vi& b) {
    vector<vi> dp(n + 1, vi(n + 1, -INF));
    dp[0][0] = a[0];
    int ans = 0;
    for (int i = 1; i < n; i++) {
        for (int t0 = 0; t0 < i; t0++) {
            for (int t = t0; t <= i; t++) {
                dp[i][t] = max(dp[i][t], dp[i - 1][t0] + a[i] + t * b[i]);
                ans = max(ans, dp[i][t]);
            }
        }
    }
    return ans;
}

void solve() {
    int n; cin >> n;
    vi a(n), b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    vi p(n); cin >> p;

    if (*max_element(all(p)) == -1) {
        cout << solve1(n, a, b) << endl;
        return;
    }
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }
}
