#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ll long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ff first
#define ss second

void solve() {
    int n;
    cin >> n;
    vector<int> a(n + 1), b(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> a[i] >> b[i];
    }
    vector<int> p(n);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
    }

    vector<vector<int>> dp(n + 1, vector<int> (n, -1e18));
    for (int j = 0; j < n; ++j) dp[0][j] = 0;
    for (int i = 1; i <= n; ++i) {
        int mx = -1e18;
        if (i == 1) mx = 0;
        for (int j = 0; j < i; ++j) {
            if (j != i - 1) mx = max(mx, dp[i - 1][j]);
            if (mx == -1e18) continue;
            dp[i][j] = max(dp[i][j], mx + a[i] + j * b[i]);
        }
    }

    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        for (int j = 0; j < n; ++j) {
            //cout << dp[i][j] << ' ';
            ans = max(ans, dp[i][j]);
        }
        //cout << '\n';
    }
    cout << ans << '\n';

    /*
    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];
    }

    int ans = 0;
    vector<int> pref(n, 0);
    vector<int> t(n, 0);
    for (int i = 0; i < n; ++i) {
        t[i] = i;
        if (p[i] != -1) {
            int j = i + 1;
            while (j < p[i]) {
                t[j] = i;
                ++j;
            }
        }
    }
    for (int i = 0; i < n; ++i) {
        pref[i] = a[i] + t[i] * b[i];
        if (i > 0) pref[i] += pref[i - 1];
        if (i == n - 1 || t[i] != t[i + 1]) {
            ans = max(ans, pref[i]);
        }
        ans = max(ans, pref[i]);
    }
    cout << ans << '\n';
    */
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
