#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define ld long double
#define pb push_back
#define sz(x) (int) x.size()
#define all(x) x.begin(), x.end()
#define int long long

typedef tree<int, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    vector<pair<int, int>> a(n + 1), b(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> a[i].first >> a[i].second;
    }
    int last = 0, was = 0;
    vector<int> p(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> p[i];
        if (p[i] + 1) {
            for (int j = was + 1; j <= p[i]; ++j) {
                b[j].first = last;
                b[j].second = min(i - 1, j - 1);
            }
            was = p[i];
            last = i;
        }
    }
    for (int j = was + 1; j <= n; ++j) {
        b[j].first = last;
        b[j].second = j - 1;
    }
    vector<vector<int>> dp(n + 1, vector<int>(n + 1, -1e18));
    vector<int> pref(n + 2, -1e18);
    int ans = -1e18;
    if (was == 0) {
        ans = 0;
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = b[i].first; j <= b[i].second; ++j) {
            dp[i][j] = a[i].first + a[i].second * j + (i == 1 ? 0 : pref[j]);
            if (i >= was) {
                ans = max(ans, dp[i][j]);
            }
        }
        pref[0] = dp[i][0];
        for (int j = 1; j < n; ++j) {
            pref[j] = max(pref[j - 1], dp[i][j]);
        }
    }
    cout << ans;
    return 0;
}