#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

struct line {
    __int128 k, b;
    __int128 operator()(ll x) {
        return k * x + b;
    }
};

vector<line> l;
vector<__int128> d;

__int128 intersect(line a, line b) {
    __int128 v = (b.b - a.b);
    __int128 u = (a.k - b.k);
    if (u == 0) return -1e18;
    return (v % u == 0 or v * u >= 0 ? v / u : v / u - 1);
}

void add(line ln) {
    while (!l.empty() and ln(d.back()) > l.back()(d.back())) {
        l.pop_back(); d.pop_back();
    }
    if (!l.empty()) {
        d.push_back(intersect(l.back(), ln));
    } else {
        d.push_back(-1e18);
    }
    l.push_back(ln);
}

ll get(ll x) {
    return l[upper_bound(d.begin(), d.end(), x) - d.begin() - 1](x);
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<pair<ll, ll>> v(n);
    vector<ll> cur(n);
    for (auto& [x, y] : v) cin >> x >> y;
    vector<ll> p(n);
    for (ll& o : p) cin >> o;
    vector<ll> mintime(n + 1, n - 1);
    ll f = 0;
    for (ll i = 0; i < n; i++) {
        if (p[i] != -1) {
            f = max(f, p[i]);
            mintime[p[i]] = min(mintime[p[i]], i);
        }
    }
    for (ll i = n - 1; i >= 0; i--) {
        mintime[i] = min(min(i - 1, mintime[i]), mintime[i + 1]);
    }
    mintime[0] = 0;
    vector<ll> p1(n + 1), p2(n + 1);
    for (int i = 0; i < n; i++) {
        p1[i + 1] = p1[i] + v[i].first;
    }
    for (int i = 0; i < n; i++) {
        p2[i + 1] = p2[i] + v[i].second;
    }
    vector<ll> dp(n + 1, -1e18); dp[0] = 0;
    add({0, 0});
    for (int i = 0; i < n; i++) {
        dp[i + 1] = max(dp[i + 1], p1[i + 1] + get(p2[i + 1]));
        add({mintime[i + 1], dp[i + 1] - (p2[i + 1] * (i + 1) + p1[i + 1])});
    }
    if (f != 0) {
        ll ans = 0, Ans = -1e18;
        for (int i = 0; i < n; i++) {
            ans += v[i].first + v[i].second * mintime[i];
            if (i >= f - 1) Ans = max(Ans, ans);
        }
        cout << Ans << '\n';
    } else {
        cout << *max_element(dp.begin() + f, dp.end()) << '\n';
    }
    return 0;
}
