#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <map>
#include <random>
#include <ext/pb_ds/assoc_container.hpp>

#define all(a) a.begin(), a.end()

using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

mt19937_64 rnd(42);

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

constexpr int mod = 998244353;

struct mint {
    int val = 0;

    mint() = default;

    mint(int const &x) {
        if (x >= mod) val = x - mod;
        else if (x < 0) val = x + mod;
        else val = x;
    }

    mint(ll const &x) : val(x % mod) {}

    mint operator+(mint const &n) const {
        return val + n.val;
    }

    mint operator-(mint const &n) const {
        return val - n.val;
    }

    mint operator*(mint const &n) const {
        return (ll) val * n.val;
    }

    bool operator<(mint const &n) const {
        return val < n.val;
    }

    bool operator==(mint const &n) const {
        return val == n.val;
    }

    bool operator!=(mint const &n) const {
        return val != n.val;
    }
};

mint fpow(mint a, ll n) {
    mint res = 1;
    while (n) {
        if (n & 1) {
            res = res * a;
        }
        a = a * a;
        n >>= 1;
    }
    return res;
}

mint operator/(mint const &a, mint const &b) {
    return a * fpow(b, mod - 2);
}

void solve() {
    int n;
    cin >> n;
    vector<pll> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i].first >> a[i].second;
    }
    vector<int> p(n), t(n, -1);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
    }

    for (int i = 0; i < n; ++i) {
        if (p[i] != -1) {
            for (int j = p[i] - 1; t[j] == -1; --j) {
                t[j] = i;
            }
        }
    }

    int r = 0;
    for (int i = 0; i < n; ++i) {
        if (t[i] == -1) {
            if (r <= i) r = i + 1;
            while (r < n and t[r] == -1) ++r;
            if (r != n) {
                t[i] = t[r];
            } else {
                t[i] = i;
            }
        }
    }

    ll ans = 0;
    ll cur = 0;
    int i = n - 1;
    while (i > 0 and p[i] == -1) --i;
    if (i == -1) i = 0;
    else i = p[i];
    for (int j = 0; j < n; ++j) {
        cur += a[j].first + a[j].second * t[j];
    }
    ans = cur;
    for (; i < n; ++i) {
        cur += a[i].first + a[i].second * t[i];
        ans = max(ans, cur);
    }
    cout << ans;
}

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

    solve();
}