#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;
const ll inf = 1e18;
void solve();

template<typename T, typename T1> T remax(T& x, T1 y) { if (x < y) x = y; return x; }
template<typename T, typename T1> T remin(T& x, T1 y) { if (y < x) x = y; return x; }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
//    cin >> t;
    while (t--) solve();
}
#define int int64_t

const int mod = 998244353;

int binpow(int n, int k) {
    int rs = 1;
    while (k) {
        if (k & 1) rs = rs * n % mod;
        n = n * n % mod;
        k >>= 1;
    }
    return rs;
}

int solve(int n, vector<int> a, vector<int> b, vector<int> p) {
    //vector<vector<int>> dp(n, vector<int>(n, -inf));

    int lst = -1;
    for (int i = 0; i < n; ++i) {
        if (p[i] > -1) lst = i;
    }

    if (lst == -1) {
        vector<vector<int>> dp(n, vector<int>(n, -inf));
        dp[0][0] = a[0];
        int ans = max<int>(0, a[0]);
        for (int i = 1; i < n; ++i) {
            int bst = -inf;
            for (int j = 0; j <= i; ++j) {
                if (j < i) remax(bst, dp[i - 1][j]);
                dp[i][j] = bst + a[i] + b[i] * j;
                remax(ans, dp[i][j]);
            }
        }
        return ans;
    }
    vector<vector<int>> dp(n, vector<int>(n, -inf));
    dp[0][0] = a[0];
    int ans = -inf;
    vector<int> l(n), r(n);
    for (int i = 0; i < n; ++i) {
        l[i] = 0, r[i] = i;
    }
    int unt = 0;
    for (int i = 0; i < n; ++i) {
        if (p[i] == -1) continue;
        int j = p[i] - 1;
        remin(r[j], i);
        remax(unt, j);
        if (j + 1 < n) {
            remax(l[j + 1], i + 1);
        }
    }
    for (int i = 0; i < n; ++i) {
        int bst = -inf;
        for (int j = 0; j < l[i]; ++j) remax(bst, dp[i - 1][j]);
        for (int j = l[i]; j <= r[i]; ++j) {
//            for (int k = 0; k <= min(i - 1, j); ++k) {
//                remax(dp[i][j], dp[i - 1][k] + a[i] + b[i] * j);
//            }
            if (j < i) remax(bst, dp[i - 1][j]);
            remax(dp[i][j], bst + a[i] + b[i] * j);
            if (i >= unt) remax(ans, dp[i][j]);
        }
    }
    return ans;
}

int stupid(int n, vector<int> a, vector<int> b, vector<int> p) {
    vector<int> t(n);
    int best = *max_element(p.begin(), p.end()) == -1 ? 0 : -inf;
    auto rec = [&](auto& self, int i, int sum) -> void {
        if (i == n) return;
        for (int j = (i ? t[i - 1] : 0); j <= i; ++j) {
            t[i] = j;
            self(self, i + 1, sum + a[i] + b[i] * j);
            bool ok = 1;
            for (int k = 0; k < n; ++k) {
                int cnt = 0;
                for (int z = 0; z <= i; ++z) {
                    cnt += t[z] <= k;
                }
                if (p[k] == -1) continue;
                ok &= cnt == p[k];
            }
            if (ok) remax(best, sum + a[i] + b[i] * j);
        }
    };
    rec(rec, 0, 0);
    return best;
}

mt19937_64 mt(1109);

int gen(int l, int r) {
    return uniform_int_distribution<int>(l, r)(mt);
}

void solve() {
    /*#ifdef LOCAL
    int id = 0;
    while (true) {
        ++id;
        int n = gen(1, 10);
        vector<int> a(n), b(n);
        for (auto& x : a) x = gen(-10, 10);
        for (auto& x : b) x = gen(-10, 10);
        vector<int> p(n, -1);
        int sl = stupid(n, a, b, p);
        int sm = solve(n, a, b, p);
        if (sl == sm) {
            cout << "OK " << id << '\n';
            continue;
        }
        cout << "WA\n";
        for (auto& x : a) cout << x << ' ';
        for (auto& x : b) cout << x << ' ';
        cout << string(50, '=') << '\n';
        cout << sl << ' ' << sm << '\n';
    }
    #else*/
    int n; cin >> n;
    vector<int> a(n), b(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i] >> b[i];
    vector<int> p(n);
    for (auto& x : p) cin >> x;
    cout << solve(n, a, b, p);
    //cout << stupid(n, a, b, p);
    //#endif
}
