
#include <iostream>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <map>
#include <unordered_map>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using pll = pair<ll, ll>;
ll mod = 998244353;
ll pow(ll n, ll k) {
    ll ans = 1;
    while (k) {
        if (k & 1) ans = ans * n % mod;
        n = n * n % mod;
        k >>= 1;
    }
    return ans;
}
struct LiChao {
    struct Line {
        ll a = -1e18, b = 0;
        ll operator()(ll x) {
            return a + b * x;
        }
    };
    struct Node {
        Line li;
        ll l = 0, r = 0;
    };
    ll n;
    vector<Node> tree{Node(), Node()};
    LiChao(ll n) : n(n) {
    }
    void update(Line line, ll l, ll r, ll v) {
        if (l == r) {
            tree[v] = {line};
            return;
        };
        ll m = (l + r) >> 1;
        if (tree[v].li(m) < line(m)) {
            swap(line, tree[v].li);
        }
        if (tree[v].r == 0) {
            tree[v].r = tree.size();
            tree.push_back(Node());
            update(line, m + 1, r, tree[v].r);
            return;
        }
        if (tree[v].l == 0) {
            tree[v].l = tree.size();
            tree.push_back(Node());
            update(line, l, m, tree[v].l);
            return;
        }
        if (tree[tree[v].l].li(l) < line(l)) {
            update(line, l, m, tree[v].l);
        } else if (tree[tree[v].r].li(r) < line(r)) {
            update(line, m + 1, r, tree[v].r);
        }
    }
    ll get(ll x, ll l, ll r, ll v) {
        if (v == 0) return -1e18;
        if (l == r) {
            return tree[v].li(x);
        }
        ll m = (l + r) >> 1;
        if (x <= m) return max(tree[v].li(x), get(x, l, m, tree[v].l));
        else return max(tree[v].li(x), get(x, m + 1, r, tree[v].r));
    }
    void update(Line l) {
        update(l, -n, n, 1);
    }
    ll get(ll x) {
        return get(x, -n, n, 1);
    }
};
pll solve2(vll a, vll b, ll t0, ll t_mx) {
    ll n = a.size();
    for (ll i = 0; i < n; ++i) a[i] += b[i] * t0;
    ll ans = 0;
    ll b_sum = 0;
    ll res = -1e18;
    ll pr = 0;
    vll pref(n + 1, 0);
    for (ll i = 0; i < n; ++i) pref[i + 1] = pref[i] + b[i];
    vll dp(n, -1e18);
    for (ll i = 0; i < n; ++i) {
        ans += a[i];
        if (b[i] >= 0) {
            b_sum += b[i] * min(t_mx, i);
        } else {
            b_sum = 0;
            for (ll j = 0; j < i; ++j) {
                ll cur = dp[j];
                b_sum = max(b_sum, (dp[j] - pref[j + 1] * min(t_mx, j)) + pref[i + 1] * min(t_mx, j));
            }
        }
        dp[i] = b_sum;
        res = max(res, ans + b_sum);
    }
    return pll(ans + b_sum, res);
}
void solve() {
    ll n;
    cin >> n;
    vll a(n), b(n);
    for (ll i = 0; i < n; ++i) cin >> a[i] >> b[i];
    vll p(n);
    for (ll i = 0; i < n; ++i) cin >> p[i];
    vll cura, curb;
    ll t0 = 0;
    ll ans = 0, res = -1e18;
    p[n - 1] = n;
    for (ll i = 0; i < n; ++i) {
        if (p[i] == -1) {
        } else {
            cura.clear();
            curb.clear();
            for (ll j = t0; j < p[i]; ++j) cura.push_back(a[j]), curb.push_back(b[j]);
            reverse(cura.begin(), cura.end());
            reverse(curb.begin(), curb.end());
            for (ll i = 0; i < t0; ++i) {
                cura.push_back(0);
                curb.push_back(0);
            }
            reverse(cura.begin(), cura.end());
            reverse(curb.begin(), curb.end());
            auto [ans0, res0] = solve2(cura, curb, 0, i);
            t0 = p[i];
            res = max(res, res0 + ans);
            ans += ans0;
        }   
    }
    cout << res << endl;
}

int main() {
  //  freopen("in.txt", "r", stdin);
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int t = 1;
    for (int i = 0; i < t; ++i) {
        solve();
    }
    cout << endl;
    return 0;
}
