#include <bits/stdc++.h>

#define all(v) v.begin(), v.end()
#define len(v) (int)v.size()

using namespace std;
using ll = long long;
using ld = long double;

const ll INF = 1e18;

void solve()
{
    int n;
    cin >> n;
    vector<ll> a(n), b(n);
    vector<int> p(n);
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i] >> b[i];
    }
    for (int i = 0; i < n; ++i)
    {
        cin >> p[i];
    }
    vector<int> lt(n), rt(n, n);
    int ls = 0, l = 0;
    int mnx = 0;
    for (int i = 0; i < n; ++i)
    {
        mnx = max(mnx, p[i]);
        if (p[i] != -1)
        {
            for (int j = ls; j < p[i]; ++j)
            {
                rt[j] = i;
                lt[j] = l;
            }
            ls = p[i];
            l = i + 1;
        }
    }
    for (int i = ls; i < n; ++i)
    {
        lt[i] = l;
    }
    ll ans = -INF;
    if (mnx == 0)
    {
        ans = 0;
    }
    for (int msk = 0; msk < 1024; ++msk)
    {
        ll cur = 0;
        ls = 0;
        int cnt = 0;
        for (int k = 0; k < n; ++k)
        {
            if (b[k] <= 0)
            {
                ls = max(ls, lt[k]);
            } else
            {
                if ((msk >> cnt) & 1)
                {
                    ls = max(ls, min(k, rt[k]));
                } else
                {
                    ls = max(ls, lt[k]);
                }
                ++cnt;
            }
            cur += a[k] + b[k] * ls;
            if (k + 1 >= mnx)
            {
                ans = max(ans, cur);
            }
        }
    }
    cout << ans;
}

int main()
{
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

    //cin >> t;

    while (t--)
    {
        solve();
        cout << '\n';
    }
    return 0;
}
