#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const long long inf = 1e18;

int 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];
    vector<long long> dp(n + 1);
    dp[0] = 0;
    for (int j = 1; j <= n; ++j)
        dp[j] = dp[j - 1] + a[j - 1];
    for (long long t = 1; t <= n; ++t)
    {
        if (p[t - 1] != -1)
            for (int j = 0; j <= n; ++j)
                if (j != p[t - 1])
                    dp[j] = -inf;
        for (int j = t + 1; j <= n; ++j)
            dp[j] = max(dp[j], dp[j - 1] + a[j - 1] + t * b[j - 1]);
    }
    long long ans = -inf;
    for (int j = 0; j <= n; ++j)
        ans = max(ans, dp[j]);
    cout << ans;
    return 0;
}
