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

#ifndef DEBUG
// #pragma GCC optimize("avx2")
// #pragma GCC optimize("O3")
#endif

using namespace __gnu_pbds;

using ll = long long;
using ld = long double;
template<typename T>
using oset = tree<T, null_type, std::less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using pll = std::pair<ll, ll>;

const ll MOD = 998244353;

std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());

void solve() {
    ll n;
    std::cin >> n;
    std::vector<ll> a(n), b(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> a[i] >> b[i];
    }
    ll maxx = 0;
    ll maxi = 0;
    std::vector<ll> p(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> p[i];
    }
    for (int i = n - 1; i >= 0; --i) {
        if (p[i] != -1) {
            maxx = p[i];
            maxi = i;
        }
    }
    std::vector<ll> prefa(n + 1), prefb(n + 1);
    for (int i = 0; i < n; ++i) {
        prefa[i + 1] = prefa[i] + a[i];
        prefb[i + 1] = prefb[i] + b[i];
    }
    std::vector<std::vector<ll>> dp(n, std::vector<ll>(n + 1, -1e18));
    if (p[0] == -1) {
        for (int pr = 1; pr <= n; ++pr) {
            dp[0][pr] = prefa[pr];
        }
    }
    else {
        dp[0][p[0]] = prefa[p[0]];
    }
    for (int tim = 1; tim < n; ++tim) {
        for (int l = ((p[tim] == -1) ? tim + 1 : p[tim]); l <= ((p[tim] == -1) ? n : p[tim]); ++l) {
            for (int pr = ((p[tim - 1] == -1) ? tim : p[tim - 1]); pr <= ((p[tim - 1] == -1) ? n : p[tim - 1]); ++pr) {
                dp[tim][l] = std::max(dp[tim][l], dp[tim - 1][pr] + prefa[l] - prefa[pr] + tim * (prefb[l] - prefb[pr]));
            }
        }
    }
    ll ans = -1e18;
    for (int tim = maxi; tim < n; ++tim) {
        for (int pr = maxx; pr <= n; ++pr) {
            ans = std::max(ans, dp[tim][pr]);
        }
    }
    std::cout << ans;
}

int main() {
#ifdef DEBUG
    std::freopen("input.txt", "r", stdin);
#endif
    std::cin.tie(0)->std::ios::sync_with_stdio(0);
    int tt = 1;
    // std::cin >> tt;
    while (tt--) {
        solve();
    }
}
