#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector <int64_t> a(n), b(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
    }

    vector <int> mn(n + 1, -1), mx(n, 2 * n), p(n);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        if (p[i] == -1) continue;
        mn[p[i]] = i + 2;
        mx[p[i] - 1] = i + 1;
        mx[i] = min(mx[i], i);
    }

    vector <vector <int64_t>> dp(n, vector <int64_t> (n, -1e18));
    dp[0][0] = a[0];


    int64_t ans = -1e18;
    for (int i = 1; i < n; ++i) {
        for (int j = 0; j <= i; ++j) {
            if (dp[i - 1][j] > -1e18) {
                dp[i][j] = a[i] + b[i] * j + dp[i - 1][j];
            }
            if (j > 0 && dp[i - 1][j - 1] > -1e18) {
                dp[i][j] = max(dp[i][j], a[i] + b[i] * j + dp[i - 1][j - 1]);
            }
            ans = max(ans, dp[i][j]);
        }
    }

    cout << ans;
    return 0;
}
