#include <bits/stdc++.h>

using namespace std;
#define int long long
const int INF = 1e18 + 1;
int n;
vector <int> a;
vector <int> b;
vector <int> p;
int ans = -INF;
vector <int> t, t2;

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    a.resize(n + 1);
    b.resize(n + 1);
    p.resize(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> a[i] >> b[i];
    }
    for (int i = 1; i <= n; i++) {
        cin >> p[i];
    }
    int last = -1;
    for (int i = n; i >= 1; i--) {
        if (p[i] != -1) {
            last = i;
            break;
        }
    }
    int all_ = p[n];
    int end_ = all_;
    if (all_ == -1) {
        end_ = n;
    }
    t.push_back(a[1]);
    for (int i = 2; i <= end_; i++) {
        t.push_back(-INF);
        t2 = t;
        fill(t.begin(), t.end(), -INF);
        for (int j = 0; j <= i - 1; j++) {
            if (p[j + 1] != -1 && p[j + 1] < i) {
                continue;
            }
            for (int z = j; z >= 0; z--) {
                if (z < j && p[z + 1] != -1 && p[z + 1] != i - 1) {
                    break;
                }
                //cout << j << ' ' << z << ' ' << t[j] << ' ' << t2[z] << ' ' << a[i] + b[i] * j << '\n';
                if (t[j] < t2[z] + a[i] + b[i] * j) {
                    t[j] = t2[z] + a[i] + b[i] * j;
                }
            }
        }
        for (int j = max(last - 1, 0); j <= i - 1; j++) {
            ans = max(ans, t[j]);
        }
//        for (int j = 0; j <= i - 1; j++) {
//            cout << t[j] << ' ';
//        }
//        cout << '\n';
    }
    cout << ans;
}
