#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#define int long long
const ll INF = 2e18;
const int N = 1e2 + 100;
int a[N];
int b[N];
int c[N];
int d[N];
int p[N];
ll dp[N][N];

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i] >> b[i];
    }
    int mx = -1;
    for (int i = 1; i <= n; ++i) {
        cin >> p[i];
        if (p[i] != -1) {
            d[p[i] + 1] = i;
        }
        mx = max(mx, p[i]);
        c[i] = i - 1;
    }
    int last = 1;
    for (int i = 1; i <= n; ++i) {
        for (; last <= p[i]; last++) {
            c[last] = i - 1;
        }
        d[i] = max(d[i], d[i - 1]);
    }
    for (int i = 1; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            dp[i][j] = -INF;
        }
    }
    ll ans = 0;
    for (int i = 1; i <= n; ++i) {
        for (int j = 0; j <= i - 1; ++j) {
            dp[i][j] = dp[i - 1][j] + a[i] + j * b[i];
            ans = max(ans, dp[i][j]);
        }
        for (int j = 1; j < N; ++j) {
            dp[i][j] = max(dp[i][j], dp[i][j - 1]);
        }
    }
    cout << ans;
    return 0;
}
