#include "bits/stdc++.h"
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

// using namespace __gnu_pbds;
// template<class T> 
// using ordered_set = <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

using namespace std;

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using ll = long long;
using ld = long double;

#define int long long

const int inf = 1'000'000'000'000'000'000;

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

    vector<pair<int, int>> bords(n);
    for (int i = 0; i < n; ++i) {
        bords[i] = {0, i};
    }

    vector<int> p(n);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        if (p[i] != -1) {
            for (int j = p[i] - 1; j >= 0; --j) {
                bords[j].second = min(bords[j].second, i);
            }
            for (int j = p[i]; j < n; ++j) {
                bords[j].first = max(bords[j].first, i + 1);
            }
        }
    }
    int mxp = *max_element(all(p)) - 1;
    
    // for (int i = 0; i < n; ++i) {
    //     cout << bords[i].first << ' ' << bords[i].second << '\n';
    // }

    vector<vector<int>> dp(n, vector<int> (n, -inf));
    dp[0][0] = a[0];
    int ans = (mxp <= 0 ? dp[0][0] : -inf);
    for (int i = 1; i < n; ++i) {
        for (int j = bords[i].first; j <= bords[i].second; ++j) {
            for (int k = bords[i - 1].first; k <= min(bords[i - 1].second, k); ++k) {
                dp[i][j] = max(dp[i][j], a[i] + b[i] * j + dp[i - 1][k]);
                if (i >= mxp) {
                    ans = max(ans, dp[i][j]);
                }
            }
        }
    }
    cout << ans << '\n';
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    #ifdef LOCAL
    freopen("C:\\Users\\User\\Documents\\olymp\\input.txt", "r", stdin);
    freopen("C:\\Users\\User\\Documents\\olymp\\output.txt", "w", stdout);
    #endif

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

    return 0;
}