#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;
const ll inf = 1e18;
void solve();

template<typename T, typename T1> T remax(T& x, T1 y) { if (x < y) x = y; return x; }
template<typename T, typename T1> T remin(T& x, T1 y) { if (y < x) x = y; return x; }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
//    cin >> t;
    while (t--) solve();
}
#define int int64_t

const int mod = 998244353;

int binpow(int n, int k) {
    int rs = 1;
    while (k) {
        if (k & 1) rs = rs * n % mod;
        n = n * n % mod;
        k >>= 1;
    }
    return rs;
}

int solve(int n, vector<int> a, vector<int> b, vector<int> p) {
    int lst = -1;
    for (int i = 0; i < n; ++i) {
        if (p[i] > -1) lst = i;
    }
    int ans = lst == -1 ? 0 : -inf;
    vector<int> l(n), r(n);
    for (int i = 0; i < n; ++i) {
        l[i] = 0, r[i] = i;
    }
    int unt = -1;
    for (int i = 0; i < n; ++i) {
        if (p[i] == -1) continue;
        int j = p[i] - 1;
        remin(r[j], i);
        remax(unt, j);
        if (j + 1 < n) {
            remax(l[j + 1], i + 1);
        }
    }
    for (int i = 1; i < n; ++i) remax(l[i], l[i - 1]);
    for (int i = n - 2; i >= 0; --i) {
        remin(r[i], l[i + 1]);
    }
    int sum = 0;
    vector<int> t(n);
    t[0] = r[0];
    sum = a[0] + b[0] * t[0];
    if (unt <= 0) remax(ans, sum);
    for (int i = 1; i < n; ++i) {
        t[i] = r[i];
        assert(t[i] >= t[i - 1]);
        sum += a[i] + b[i] * t[i];
        if (i >= unt)
            remax(ans, sum);
    }
    return ans;
}

int stupid(int n, vector<int> a, vector<int> b, vector<int> p) {
    int lst = -1;
    for (int i = 0; i < n; ++i) {
        if (p[i] > -1) lst = i;
    }

    if (lst == -1) {
        vector<vector<int>> dp(n, vector<int>(n, -inf));
        dp[0][0] = a[0];
        int ans = max<int>(0, a[0]);
        for (int i = 1; i < n; ++i) {
            int bst = -inf;
            for (int j = 0; j <= i; ++j) {
                if (j < i) remax(bst, dp[i - 1][j]);
                dp[i][j] = bst + a[i] + b[i] * j;
                remax(ans, dp[i][j]);
            }
        }
        return ans;
    }
    vector<vector<int>> dp(n, vector<int>(n, -inf));
    dp[0][0] = a[0];
    int ans = -inf;
    vector<int> l(n), r(n);
    for (int i = 0; i < n; ++i) {
        l[i] = 0, r[i] = i;
    }
    int unt = 0;
    for (int i = 0; i < n; ++i) {
        if (p[i] == -1) continue;
        int j = p[i] - 1;
        remin(r[j], i);
        remax(unt, j);
        if (j + 1 < n) {
            remax(l[j + 1], i + 1);
        }
    }
    for (int i = 0; i < n; ++i) {
        int bst = -inf;
        for (int j = 0; j < l[i]; ++j) remax(bst, dp[i - 1][j]);
        for (int j = l[i]; j <= r[i]; ++j) {
            if (j < i) remax(bst, dp[i - 1][j]);
            remax(dp[i][j], bst + a[i] + b[i] * j);
            if (i >= unt) remax(ans, dp[i][j]);
        }
    }
    return ans;
}

mt19937_64 mt(1);

int gen(int l, int r) {
    return uniform_int_distribution<int>(l, r)(mt);
}

void solve() {
//    int id = 0;
//    while (true) {
//        ++id;
//        int n = gen(1, 4);
//        vector<int> a(n), b(n);
//        for (auto& x : a) x = gen(-10, 10);
//        for (auto& x : b) x = gen(-10, 0);
//        vector<int> p(n, -1);
//        for (int i = 0; i < n; ++i) {
//            if (mt() & 1) p[i] = gen(i + 1, n);
//        }
//        int sl = stupid(n, a, b, p);
//        int sm = solve(n, a, b, p);
//        if (sl == sm) {
//            cout << "OK " << id << '\n';
//            continue;
//        }
//        cout << "WA\n";
//        cout << n << '\n';
//        for (int i = 0; i < n; ++i) cout << a[i] << ' ' << b[i] << '\n';
//        for (auto& x : p) cout << x << ' ';
//        cout << '\n';
//        cout << string(50, '=') << '\n';
//        cout << sl << ' ' << sm << '\n';
//        return;
//    }
    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 (auto& x : p) cin >> x;
    cout << solve(n, a, b, p);
}
