#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
const int inf = 0x3f3f3f3f;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(x) 67
#endif
const ll mod = 998'244'353;
const ll INF = 2e16;

void solve() {
    int n; cin >> n;
    vector<pll> c(n);
    for (int i = 0; i < n; ++i) {
        cin >> c[i].first >> c[i].second;
    }
    vector<int> p(n);
    for (int &i : p) {
        cin >> i;
    }
    vector<pii> seg(n);
    for (int i = 0; i < n; ++i) {
        seg[i] = {0, i};
    }
    int mink = -1;
    for (int i = 0; i < n; ++i) {
        if (p[i] == -1)continue;
        mink = max(mink, p[i] - 1);
        for (int j = 0; j < p[i]; ++j) {
            seg[j].second = min(seg[j].second, i);
        }
        for (int j = p[i]; j < n; ++j) {
            seg[j].first = max(seg[j].first, i + 1);
        }
    }
    if (mink == -1) {
        vector<ll> dp(n + 1, 0);
        dp[n] = 0;
        for (ll i = n - 1; i >= 0; --i) {
            ll suma = 0;
            ll sumb = 0;
            for (ll j = i; j < n; ++j) {
                suma += c[j].first;
                sumb += c[j].second;
                dp[i] = max(dp[i], dp[j + 1] + suma + sumb * i);
            }
        }
        debug(dp);
        cout << max(0LL, dp[0]);
        return;
    }
    debug(seg);
    vector<vector<ll>> dp(n, vector<ll> (n, -INF));
    dp[0][0] = c[0].first;
    for (ll i = 1; i < n; ++i) {
        for (int td = 0; td <= i; ++td) {
            for (int t = seg[i].first; t <= seg[i].second; ++t) {
                dp[i][t] = max(dp[i][t], dp[i - 1][td] + c[i].first + c[i].second * t);
            }
        }
        debug(dp[i]);
    }

    ll maxi = -INF;
    for (int i = mink; i < n; ++i) {
        for (int t = 0; t < n; ++t) {
            maxi = max(maxi, dp[i][t]);
        }
    }
    cout << maxi;
}

signed main() {
#ifndef LOCAL
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
    int tt = 1;
    // cin >> tt;
    while (tt--) {
        solve();
        if (tt) {
            cout << '\n';
        }
    }
}