#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 << k << ' ' << s << ' ' << ans << std::endl;
            if (s == p.size()) {
                int l = p[k];
                ll inians = ans, mxans = ans;
                for (int r = l; r <= n; ++r) {
                    ans = inians;
                    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);
                    }
                    while (sufs.size() < r - k) sufs.push_back(mx);
                    std::sort(sufs.begin(), sufs.end(), std::greater<>());
                    for (int i = 0; i < r - k; ++i) {
                        ans += sufs[i];
                    }
                    mxans = std::max(mxans, ans);
                }
                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);
                }
                while (sufs.size() < s - k - 1) sufs.push_back(mx);
                std::sort(sufs.begin(), sufs.end(), std::greater<>());
                for (int i = 0; i < s - k - 1; ++i) {
                    ans += sufs[i];
                }
                // std::cout << l << ' ' << r << ' ' << ans << std::endl;
                k = s;
            }
        }
        return ans;
    };
    auto stupid = [&]() -> ll {
        auto p = _p;
        std::vector<int> bnd(n, -1);
        for (int i = n - 1; i >= 0; --i) {
            if (p[i] != -1) bnd[p[i] - 1] = i;
        }
        int lst = n - 1;
        while (lst >= 0 && bnd[lst] == -1) --lst;
        std::vector<ll> dp(n + 1, -1e18);
        dp[0] = 0;
        ll ans = -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 (bnd[i] != -1) {
                for (int j = 0; j <= i; ++j) {
                    if (j != bnd[i]) dp[j] = -1e18;
                }
            }
            if (i >= lst) 
            ans = std::max(ans, *std::max_element(dp.begin(), dp.end()));
        }
        return ans;
        // std::cout << ans;
    };
    std::cout << stupid();
    // for (int tc = 1; ; ++tc) {
    //     for (int i = 0; i < n; ++i) a[i] = gen(-3, 3), b[i] = gen(-3, 3);
    //     _p = std::vector<int>(n, -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;
    //         }
    //         return 0;
    //     }
    //     std::cout << "OK " << tc << std::endl;
    // }
}