#include <bits/stdc++.h>
//#include <bits/extc++.h>
using namespace std;
//using namespace __gnu_pbds;
using ll = long long;
using ld = long double;
using pii = pair<ll, ll>;
using pic = pair<ll, char>;
using ull = unsigned long long;
//using oset = tree<ll, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rnd('S' + 'E' + 'E' + 'D');
ll l_r(const ll &lb_, const ll &rb_) {return (lb_ + rnd() % (rb_ - lb_ + 1)); }
template<class T> ll szl(const T &v1) { return (ll)v1.size(); }
template<class T> bool mmin(T &v1, const T &v2) { return (v1 > v2 ? v1 = v2, 1 : 0); }
template<class T> bool mmax(T &v1, const T &v2) { return (v1 < v2 ? v1 = v2, 1 : 0); }
#ifndef MiracleFFT
    //#pragma GCC optimize("O3,unroll-loops")
    //#pragma GCC target("avx,avx2,bmi,bmi2,lzcnt,popcnt")
#endif
const ll INF = 2e9 + 10;
const ll LNF = 2e18 + 10;
const ll MAXN = 2e5 + 10;
const ll MAXX = 1e9 + 10;
const ll MAXC = 1e18 + 10;
const ll ALPH = 26;
const ll BL_SZ = 400;
const ll MAX_LOG = 20;
const ll CNT_OP = 100;
const ll MOD = 1e9 + 7;
const ll P = 179;
const ll M = 1e9 + 787;
const ld UNO = ld(1.0);
const ld PI = atan2(0, -1);
const ld BURN = ld(0.99999);


pii get(const vector<pii> &a, const vector<pii> &kaka) {
    vector<ll> bo;
    for (auto &[x, y] : kaka) {
        bo.push_back({y - x});
    }
    ll n = szl(a);
    /*vector<vector<ll>> dp(n);
    for (ll i = 0; i < n; i++) {
        dp[i].assign(bo[i] + 1, 0);

        ll mx = 0;
        for (ll j = 0; j <= bo[i]; j++) {
            if (i && j <= bo[i - 1]) mmax(mx, dp[i - 1][j]);
            ll add = j * a[i].second;
            dp[i][j] = mx + add;
        }
    }*/

    ll m1 = -LNF;
    ll add = 0;
    ll cc = -1;
    for (ll _ = 0; _ < n; _++) {
        cc++;
        add += kaka[cc].first * a[cc].second + a[cc].first;
        //for (auto &x : el) {
        mmax(m1, a[cc].second * bo[cc] + add);
        //}
    }
    ll m2 = -LNF;
    mmax(m2, a.back().second * bo.back() + add);
    return {m1, m2};
}


void solve() {
    ll n;
    cin >> n;

    //if (n > 5) throw(0);
    vector<pii> a(n);
    for (auto &[x, y] : a) {
        cin >> x >> y;
    }

    vector<ll> p(n);
    for (auto &el : p) {
        cin >> el;
    }

    vector<pii> bo(n);
    for (ll i = 0; i < n; i++) {
        bo[i].second = i;
    }

    vector<ll> np(n, -1);
    for (ll i = 0; i < n; i++) {
        if (p[i] != -1 && np[p[i] - 1] == -1) {
            np[p[i] - 1] = i + 1;
        }
    }
    p = np;
    p.back() = n;

    ll pr = -1;
    ll px = 0;

    ll mx = -LNF;
    ll add = 0;
    for (ll i = 0; i < n; i++) {
        if (p[i] == -1) continue;

        vector<pii> na;
        vector<pii> bo;
        for (ll j = pr + 1; j <= i; j++) {
            na.push_back(a[j]);
            bo.push_back({px, min(j, p[i] - 1)});
        }
        auto [f, all] = get(na, bo);
        mmax(mx, add + f);
        add += all;

        //end
        pr = i;
        px = p[i];
    }

    cout << mx << '\n';
}

int main() {
    bool IS_MULTITEST = 0; 
    #ifdef MiracleFFT
        IS_MULTITEST = 1;
        freopen("ain.txt", "r", stdin);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout << setprecision(9) << fixed;
    ll t = 1;
    if (IS_MULTITEST) {
        cin >> t;
    }
    while (t--) {
        solve();
    }
    #ifdef MiracleFFT
        cout << setprecision(2) << fixed;
        cout << "Time: " << ((ld)clock() / CLOCKS_PER_SEC) << " Sec" << endl;
    #endif
}
