#include <bits/extc++.h>
// #pragma GCC optimize("O3")
#pragma GCC target("avx,avx2") 
#define int long long

using namespace std;
using namespace __gnu_pbds;

const int inf = 1e16;

struct segtree {
    int n;
    vector<int> t;

    segtree(int _n, const vector<int> &a) {
        n = _n;
        t.resize(4 * n, -inf);
        build(0, 0, n, a);
    }

    void build(int v, int l, int r, const vector<int> &a) {
        if (l + 1 == r) {
            t[v] = a[l];
        } else {
            int m = (l + r) / 2;
            build(2 * v + 1, l, m, a);
            build(2 * v + 2, m, r, a);
            t[v] = max(t[2 * v + 1], t[2 * v + 2]);
        }
    }

    void update(int v, int l, int r, int i, int x) {
        if (l + 1 == r) {
            t[v] = x;
        } else {
            int m = (l + r) / 2;
            if (i < m) {
                update(2 * v + 1, l, m, i, x);
            } else {
                update(2 * v + 2, m, r, i, x);
            }
            t[v] = max(t[2 * v + 1], t[2 * v + 2]);
        }
    }

    void update(int i, int x) {
        update(0, 0, n, i, x);
    }

    int query(int v, int l, int r, int ql, int qr) {
        if (ql <= l && r <= qr) {
            return t[v];
        } else if (qr <= l || r <= ql) {
            return -inf;
        } else {
            int m = (l + r) / 2;
            return max(query(2 * v + 1, l, m, ql, qr),
                        query(2 * v + 2, m, r, ql, qr));
        }
    }

    int query(int l, int r) {
        return query(0, 0, n, l, r);
    }
}; 

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 + 1, -1);
    vector<int> cl(n, -1);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
    }
    for (int i = n - 1; i >= 0; --i) {
        if (p[i] != -1) {
            cl[i] = p[i];
        } else if (i < n - 1) {
            cl[i] = cl[i + 1];
        }
    }
    int lt = 0;
    for (int i = 0; i < n; ++i) {
        if (p[i] != -1) {
            lt = i;
        }
    }
    int ans = 0, ansg = -inf, last = -1;
    for (int t = 0; t <= lt; ++t) {
        while (last < t) {
            ++last;
            ans += a[last] + t * b[last];
        }
        while (last + 1 < cl[t]) {
            ++last;
            ans += a[last] + t * b[last];
        }
        if (t >= lt) {
            ansg = max(ansg, ans);
        }
    }
    while (last < n) {
        ++last;
        ans += a[last] + (lt + 1) * b[last];
        ansg = max(ans, ansg);
    }
    cout << ansg << '\n';
}

signed main(void) {
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    solve();
}