#ifndef LOCAL
#pragma GCC Optimize("O3, Ofast")
#pragma GCC Target("avx,avx2,unroll-loops")
#endif
#include <bits/stdc++.h>

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

#define int ll
#define f first
#define s second
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#ifndef LOCAL
#define endl "\n"
#endif

struct F {
    vector<int> t;
    F(int n) {
        t.assign(n, 0);
    }
    int get(int r) {
        int ans = 0;
        for (; r >= 0; r = (r&(r + 1)) - 1) {
            ans += t[r];
        }
        return ans;
    }
    void upd(int i, int x) {
        for (; i < t.size(); i = (i|(i + 1))) {
            t[i] += x;
        }
    }
};

mt19937 rnd(time(0));

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);
    int mx = -1;
    for (auto &x : p) {
        cin >> x;
        --x;
        mx = max(mx, x);
    }
    vector<int> d = {0, n};
    vector<int> l(n + 1, 0), r(n, n);
    for (int i = 0; i < n; ++i) {
        if (p[i] != -2) {
            d.pb(p[i] + 1);
            r[p[i]] = min(r[p[i]], i + 1);
            l[p[i] + 1] = i + 1;
        }
    }
    for (int i = 1; i < n; ++i) {
        l[i] = max(l[i], l[i - 1]);
    }
    for (int i = n - 2; i >= 0; --i) {
        r[i] = min(r[i], r[i + 1]);
    }
    sort(all(d));
    d.resize(unique(all(d)) - d.begin());
    int now = 0, ans = -1e18;
    auto get = [&](int left, int right) {
        int down = 0, up = n;
        for (int i = left; i < right; ++i) {
            down = max(down, l[i]);
            up = min(up, r[i]);
        }
        while (down >= up) {
            new int[1000];
        }
        vector<int> dp(up - down);
        auto getBest = [&]() {
            int mx = -1e18;
            for (auto &x : dp) {
                mx = max(mx, x);
            }
            return mx;
        };
        pii line = mp(0, 0);
        for (int i = left; i < right; ++i) {
            int nup = min(up, i + 1);
            line.s = max(line.s, 0ll);
            for (int j = down; j < nup; ++j) {
                if (j != down) {
                    dp[j - down] = max(dp[j - down], dp[j - down - 1]);
                }
            }
            line.f += a[i] + b[i] * down;
            line.s += b[i];
            if (i >= mx) {
               ans = max(ans, now + line.f + line.s * (nup - down - 1));
            }
            // for (auto &x : dp) {
            //     cout << x << ' ';
            // }
            // cout << endl;
        }
        now += line.f + line.s * (up - down - 1);
    };
    for (int i = 1; i < d.size(); ++i) {
        if (d[i - 1] > mx) {
            ans = max(ans, now);
        }
        get(d[i - 1], d[i]);
    }
    cout << ans << endl;
}

signed main() {
    #ifdef LOCAL
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    #else
        ios::sync_with_stdio(false);
        cin.tie(0);
    #endif
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
}