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

using namespace std;
using namespace __gnu_pbds;

// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx2,bmi,popcnt")


// #define double long double
#define int long long
#define endl "\n"
#define ordered_set  tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

constexpr int MOD = 998244353;
constexpr int INF = 2e9;
constexpr int K = 3;
constexpr int P = 31;
constexpr double eps = 1e-9;
constexpr int MOD1 = (1ll << 61) - 1;

inline int pls(int a, int b) {
    if (a + b >= MOD1) return a + b - MOD1;
    return a + b;
}

inline int mns(int a, int b) {
    return pls(a, MOD1 - b);
}

inline int umn(int a, int b) {
    __int128 c = __int128(a) * b;
    return pls(c & MOD1, c >> 61);
}

int binpow(int n, int k, int mod) {
    if (!k) return 1;
    if (k & 1) return n * binpow(n, k ^ 1, mod) % mod;
    int c = binpow(n, k >> 1, mod);

    return c * c % mod;
}

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

    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 (int i = 0; i < n; i++) {
        cin >> p[i];
    }
    vector<vector<int>> dp(n + 1, vector<int>(n + 1, -INF));
    dp[0][0] = 0;
    int ans=  -INF;
    for (int i = 1; i <= n; i++) {
        for (int last = 0; last <= n; last++) {
            for (int t = last; t < i; t++) {
                bool ok = true;
                for (int k = 0; k < n; k++) {
                    if (p[k] != -1 && p[k] < i && t < (k + 1)) {
                        ok = false;
                        break;
                    }
                    if ((p[k] == i && t < k + 1)) {

                    } else if (p[k] == i) {
                        ok = false;
                        break;
                    }
                }
                if (ok) {
                    dp[i][t] = max(dp[i][t], dp[i - 1][last] + a[i - 1]  + b[i - 1] * t);
                    ans = max(ans, dp[i][t]);
                }
            }
        }
    }
    cout << ans << endl;
}
