#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// #pragma GCC optimize("O3")
// #pragma GCC target("avx2,bmi,bmi2")
#define endl "\n"
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define pb push_back
#define pop pop_back

using namespace std;
using namespace __gnu_pbds;

using ll = long long;
using lll = __int128_t;
using ld = long double;

template <typename T>
using ordered_set = tree<T, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>;

template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<>, rb_tree_tag, tree_order_statistics_node_update>;

constexpr int INF = 1e9;
constexpr ll LINF = 1e18;
constexpr ld EPS = 1e-5;
constexpr int MOD = 1e9 + 7, MOD2 = 998244353;

int add(int a, int b, int mod) {
    if (a + b >= mod) return a + b - mod;
    return a + b;
}

int sub(int a, int b, int mod) {
    if (a >= b) return a - b;
    return a - b + mod;
}

int mul(int a, int b, int mod) {
    return 1ll * a * b % mod;
}

int binpow(int a, int b, int mod) {
    if (b == 0) return 1;
    int x = binpow(a, b / 2, mod);
    if (b % 2) return mul(x, mul(x, a, mod), mod);
    return mul(x, x, mod);
}

mt19937 rnd(1337228666);

void solve() {
    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), next(n, n - 1);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        if (p[i] != -1) {
            p[i]--;
        }
    }

    for (int i = n - 2; i >= 0; i--) {
        if (p[i] != -1) {
            next[i] = p[i];
        } else {
            next[i] = next[i + 1];
        }
    }

    vector <int> c(n);
    int last = -1;
    for (int i = 0; i < n; i++) {
        if (p[i] != -1) {
            last = p[i];
            for (int j = p[i] + 1; j <= next[i + 1]; j++) {
                c[j] = i + 1;
            }
        }
    }

    ll maxans = (last == -1 ? 0 : -LINF), ans = 0;
    for (int i = 0; i < n; i++) {
        ans += a[i] + 1ll * b[i] * c[i];
        if (i >= last) {
            maxans = max(maxans, ans);
        }
    }
    maxans = max(maxans, ans);
    cout << maxans << endl;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    // cin >> t;
    while (t--) solve();
}