#include<bits/stdc++.h>

using ll = long long;


std::mt19937 rng(2342);
int gen(int l, int r) {
    int x = rng() % (r - l + 1);
    return l + x;
}

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

    int n = 3;
    std::cin >> n;
    std::vector<ll> a(n), b(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> a[i] >> b[i];
    }
    std::vector<int> _p(n);
    for (auto& i : _p) std::cin >> i;
    auto smart = [&]() -> ll {
        auto p = _p;
        p.insert(p.begin(), 0);
        int k = 0;
        ll ans = 0;
        while (true){
            int s = k + 1;
            while (s < p.size() && p[s] == -1) ++s;
            // std::cout << 111 << ' ' << k << ' ' << s << ' ' << ans << std::endl;
            if (s == p.size()) {
                int l = p[k];
                ll inians = ans, mxans = ans;
                std::vector<std::pair<ll, ll>> sufs = {{0, l - k}};
                ll cur = 0, addall = 0;
                for (int r = l + 1; r <= n; ++r) {
                    cur += a[r - 1] + b[r - 1] * k;
                    addall += b[r - 1];
                    ll x = -addall, s = 1;
                    while (sufs.size() && sufs.back().first < x) {
                        s += sufs.back().second;
                        cur -= sufs.back().first * sufs.back().second;
                        sufs.pop_back();
                    }
                    sufs.emplace_back(x, s);
                    cur += x * s;
                    ll curans = cur + addall * (r - k);
                    mxans = std::max(mxans, curans);
                }
                ans = mxans;
                break;
            } else {
                int l = p[k], r = p[s];
                for (int i = l; i < r; ++i) ans += a[i] + b[i] * k;
                std::vector<ll> sufs;
                ll c = 0, mx = 0;
                for (int i = r - 1; i >= l; --i) {
                    sufs.push_back(mx);
                    c += b[i];
                    mx = std::max(mx, c);
                }
                for (int i = 0; i <  l - k; ++i) sufs.push_back(mx);
                std::reverse(sufs.begin(), sufs.end());
                for (int i = 0; i < s - k - 1; ++i) {
                    ans += sufs[i];
                }
                k = s;
            }
        }
        return ans;
    };
    auto stupid = [&]() -> ll {
        auto p = _p;
        std::vector<int> L(n, -1), R(n, -1);
        for (int i = n - 1; i >= 0; --i) {
            if (p[i] != -1) L[p[i] - 1] = i;
        }
        for (int i = 0; i < n; ++i) {
            if (p[i] != -1) R[p[i] - 1] = i;
        }
        int lst = n - 1;
        while (lst >= 0 && R[lst] == -1) --lst;
        std::vector<ll> dp(n + 1, -1e18);
        dp[0] = 0;
        ll ans = lst == -1 ? 0 : (ll)-1e18;
        for (int i = 0; i < n; ++i) {
            ll mx = dp[0];
            for (int j = 0; j <= i; ++j) {
                mx = std::max(mx, dp[j]);
                dp[j] = mx + a[i] + b[i] * j;
            }
            if (R[i] != -1) {
                ll mx = -1e18;
                for (int j = 0; j <= L[i]; ++j) mx = std::max(mx, dp[j]);
                std::fill(dp.begin(), dp.end(), -1e18);
                dp[R[i] + 1] = mx;
            }
            if (i >= lst) 
            ans = std::max(ans, *std::max_element(dp.begin(), dp.end()));
        }
        return ans;
        // std::cout << ans;
    };
    std::cout << smart();
    // for (int tc = 1; ; ++tc) {
    //     for (int i = 0; i < n; ++i) a[i] = gen(-3, 3), b[i] = gen(-3, 3);
    //     int lst = 0;
    //     for (int i = 0; i < n; ++i) {
    //         if (rng() & 1) {
    //             lst = std::max(lst, i + 1);
    //             _p[i] = gen(lst, n);
    //             lst = _p[i];
    //         } else {
    //             _p[i] = -1;
    //         }
    //     }
    //     if (smart() != stupid()) {
    //         std::cout << "WA: " << tc << ' ' << smart() << ' ' << stupid() << std::endl;
    //         for (int i = 0; i < n; ++i) {
    //             std::cout << a[i] << ' ' << b[i] << std::endl;
    //         }
    //         for (auto i : _p) std::cout << i << ' ';
    //         std::cout << std::endl;
    //         return 0;
    //     }
    //     std::cout << "OK " << tc << std::endl;
    // }
}