#include <bits/stdc++.h>
using namespace std;
#define int long long

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    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);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
    }
    int min_must = *max_element(p.begin(), p.end());
    int res = -1000000000000000000, ans = 0;
    vector<int> mnTm(n), mxTm(n);
    for (int i = 0; i < n; ++i) {
        mnTm[i] = 0;
        mxTm[i] = i;
    }
    int prv = n;
    for (int i = n - 1; i >= 0; --i) {
        if (p[i] != -1) {
            while (prv > p[i]) {
                mnTm[prv - 1] = i + 1;
                prv--;
            }
        }
    }
    prv = 0;
    for (int i = 0; i < n; ++i) {
        if (p[i] != -1) {
            while (prv < p[i]) {
                mxTm[prv] = min(mxTm[prv], i);
                prv++;
            }
        }
    }
    
    if (min_must <= 0) {
        res = 0;
    }

    int frPositive = n;
    for (int i = 0; i < n; ++i) {
        if (b[i] > 0) {
            frPositive = i;
            break;
        }
    }
    bool isSubt510 = true;
    for (int i = 0; i < frPositive; ++i) {
        isSubt510 = isSubt510 && b[i] <= 0;
    }
    for (int i = frPositive; i < n; ++i) {
        isSubt510 = isSubt510 && b[i] >= 0;
    }

    if (true || isSubt510) {
        int prv_tm = 0;
        // int ans2 = -1000000000000000000;
        for (int i = 0; i < n; ++i) {
            // ans2 = max(ans2, ans);
            // ans2 += a[i] + max(prv_tm, mnTm[i]) * b[i];
            if (b[i] < 0) {
                ans += a[i] + max(prv_tm, mnTm[i]) * b[i];
            } else {
                ans += a[i] + mxTm[i] * b[i];
                prv_tm = mxTm[i];
            }
            if (i + 1 >= min_must) {
                res = max(res, ans);
                // res = max(res, ans2);
            }
        }
        cout << res << "\n";
        return 0;
    }
    vector<int> dp(n + 1);
    int l = 0, r = n;
    for (int i = 0; i < n; ++i) {
        for (int j = 1; j <= n; ++j) {
            dp[j] = max(dp[j], dp[j - 1]);
        }

        for (int j = mnTm[i]; j <= mxTm[i]; ++j) {
            dp[j] += a[i] + b[i] * j;
            if (i + 1 >= min_must) {
                res = max(res, dp[j]);
            }
        }
        for (int j = 0; j < mnTm[i]; ++j) {
            dp[j] = -1000000000000000;
        }
        l = mnTm[i];
        for (int j = mxTm[i] + 1; j <= n; ++j) {
            dp[j] = -1000000000000000;
        }
        r = mxTm[i];
        
    }
    cout << res << "\n";
}