#include <bits/stdc++.h>

using namespace std;

#define f(i, n) for(int i=0;i<n;i++)
#define f1(i, n) for(int i=1;i<n;i++)
#define int long long
#define ll long long
// #define ll __int128_
#define MOD (ll)998244353
#define vi vector<int>
#define vvi vector<vi>
#define vll vector<ll>
#define vvll vector<vll>

vi as, bs;
ll gt(int in, ll t)
{
    return as[in] + t * bs[in];
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin >> n;
    as = vi(n);
    bs = vi(n);
    f(i, n)
        cin >> as[i] >> bs[i];
    
    vi ps(n);
    int map = -1;
    f(i, n)
    {
        cin >> ps[i];
        if (ps[i] != -1)
            ps[i]--;
        map = max(map, ps[i]);
    }
    vi mat(n, 1e9);
    vi mit(n);
    int st = 0;
    int lsp = 0;
    f(i, n)
    {
        if (ps[i] == -1)
            continue;
        while (st <= ps[i])
        {
            mit[st] = lsp;
            mat[st] = i;
            st++;
        }
        lsp = i;
    }
    mat.push_back(1e9);

    vvll dp(n, vi(n, -9e18));
    dp[0][0] = gt(0, 0);
    dp[0][1] = dp[0][0];
    ll res = -9e18;
    if (mat[1] >= 1e9)
        res = max(dp[0][0], (ll)0);
    f1(i, n)
    {
        // ll prma = -9e18;
        int na = min(i, mat[i]);
        for (int t = mit[i]; t <= na; t++)
        {
            // prma = max(prma, dp[i-1][t]);
            dp[i][t] = dp[i-1][t] + gt(i, t);
            if (t > 0)
                dp[i][t] = max(dp[i][t], dp[i][t-1]);
            // res = max(res, dp[i][t]);
        }
        for (int t = na; t <= i+1; t++)
            dp[i][t+1] = dp[i][t];
        // dp[i][0] = dp[i-1][0] + gt(i, i);
        if (mat[i+1] >= 1e9)
            res = max(res, dp[i][i]);
    }
    cout << res << '\n';
    return 0;
}