#include <bits/stdc++.h>
#include <immintrin.h>

using namespace std;
using ll = long long;

void solve();

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    cout << fixed << setprecision(20);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
}

const int mod = 998'244'353;

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

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

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

int bin_pow(int a, int x) {
    int res = 1;
    while (x) {
        if (x & 1) {
            res = mul(res, a);
        }
        a = mul(a, a);
        x >>= 1;
    }
    return res;
}

int inv(int a) {
    return bin_pow(a, mod - 2);
}

struct node {
    node *l, *r;
    ll k, b;
    ll get(ll x) {
        return k * x + b;
    }
    node(ll _k, ll _b) : k(_k), b(_b) {
        l = nullptr;
        r = nullptr;
    }
};

const ll MPRB = (1ll << 40);

struct li_chao {
    node *root = nullptr;
    li_chao() = default;
    node *_insert(ll k, ll b, ll l1, ll r1, node *cur) {
        if (cur == nullptr) {
            node *res = new node(k, b);
            return res;
        }
        if (cur->get((l1 + r1) / 2) < k * ((l1 + r1) / 2) + b) {
            swap(cur->k, k);
            swap(cur->b, b);
        }
        if (l1 == r1) return cur;
        if (cur->get(l1) < k * l1 + b) {
            cur->l = _insert(k, b, l1, (l1 + r1) / 2, cur->l);
        } else {
            cur->r = _insert(k, b, (l1 + r1) / 2 + 1, r1, cur->r);
        }
        return cur;
    }
    void insert(ll k, ll b) {
        root = _insert(k, b, 0, MPRB - 1, root);
    }
    ll _get(ll x, ll l1, ll r1, node *cur) {
        if (cur == nullptr) {
            return INT64_MIN / 2;
        }
        ll res = cur->get(x);
        if (l1 == r1) return res;
        if (x > (l1 + r1) / 2) {
            res = max(res, _get(x, (l1 + r1) / 2 + 1, r1, cur->r));
        } else {
            res = max(res, _get(x, l1, (l1 + r1) / 2, cur->l));
        }
        return res;
    }
    ll get(ll x) {
        return _get(x, 0, MPRB - 1, root);
    }
};

void solve() {
    int n;
    cin >> n;
    vector<ll> a(n), b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    vector<int> p(n);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
    }
    vector<ll> pra(n + 1), prb(n + 1), dp(n + 1);
    pra[0] = 0;
    prb[0] = 0;
    dp[0] = 0;
    for (int i = 0; i < n; i++) {
        pra[i + 1] = pra[i] + a[i];
        prb[i + 1] = prb[i] + b[i];
    }
    int st = 0, stv = 0;
    for (int ptr = 0; ptr < n; ptr++) {
        if (p[ptr] != -1) {
            int en = p[ptr];
            for (int i = st; i < en; i++) {
                dp[i + 1] = dp[i] + a[i] + b[i] * stv;
            }
            li_chao lc;
            for (int i = st + 1; i <= en; i++) {
                ll val = lc.get(prb[i]) + pra[i];
                dp[i] = max(dp[i], val);
                if (i <= ptr) {
                    lc.insert(i, dp[i] - pra[i] - prb[i] * i);
                }
            }
            for (int i = st; i < en; i++) {
                if (ptr > i) continue;
                dp[en] = max(dp[en], dp[i] + pra[en] - pra[i] + (prb[en] - prb[i]) * ptr);
            }
            st = en;
            stv = ptr + 1;
        }
    }
    int en = n, ptr = n - 1;
    for (int i = st; i < en; i++) {
        dp[i + 1] = dp[i] + a[i] + b[i] * stv;
    }
    li_chao lc;
    for (int i = st + 1; i <= en; i++) {
        ll val = lc.get(prb[i]) + pra[i];
        dp[i] = max(dp[i], val);
        if (i <= ptr) {
            lc.insert(i, dp[i] - pra[i] - prb[i] * i);
        }
    }
    ll ans = dp[en];
    for (int i = st; i < en; i++) {
        ans = max(ans, dp[i]);
    }
    cout << ans << '\n';
}
