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

const int INF = 1e17;

vector<int> f(vector<int> a, int i) {
    int n = a.size();
    vector<int> b(n);
    if (i == 0) {
        return a;
    }
    if (i == 1) {
        for (int i = 0; i < n; i++) {
            b[i] = a[n - 1 - i];
        }
        return b;
    }
    if (i == 2) {
        for (int i = 0; i < n; i++) {
            b[i] = n + 1 - a[i];
        }
        return b;
    }
    for (int i = 0; i < n; i++) {
        b[a[i] - 1] = i + 1;
    }
    return b;
}

const int MOD = 998244353;

int pw(int a, int b) {
    //cout << a << " " << b << endl;
    if (b == 0) {
        return 1;
    }
    if (b % 2 == 1) {
        return (pw(a, b - 1) * a) % MOD;
    }
    int t = pw(a, b / 2);
    return (t * t) % MOD;
}

int g(vector<int>& a) {
    int ans = 0;
    for (int i = 0; i < a.size(); i++) {
        ans = (ans + pw(a[i], i + 1)) % MOD;
        //cout << ans << " " << a[i] << " " << i + 1 << endl;
    }
    /*
    for (auto e : a) {
        cout << e << " ";
    }
    cout << endl << ans << endl;*/
    return ans;
}

int ind(vector<vector<int>>& a, vector<int>& b) {
    for (int i = 0; i < a.size(); i++) {
        if (a[i] == b) {
            return i;
        }
    }
    return -1;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin >> n;
    vector<int> a(n), b(n), p(n);
    for (int i = 0; i < n; i++){
        cin >> a[i] >> b[i];
    }
    int minn = 0;
    vector<int> pp(n, n + 1);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        p[i] -= 1;
        for (int j = 0; j <= p[i]; j++) {
            minn = max(minn, j);
            pp[j] = min(pp[j], i);
    //..cout << j << " " << i << endl;
        }
    }
    vector<vector<int>> dp(n, vector<int>(n, -INF));
    for (int i =0; i < n; i++) {
        dp[0][i] = a[0];
    }
    for (int i = 1; i < n; i++) {
        for (int j = 0; j <= 0; j++) {
            dp[i][0] = dp[i - 1][0] + a[i];
        }
        for (int j = 1; j < n && j <= i; j++) {
            dp[i][j] = max(dp[i][j - 1] + b[i], dp[i - 1][j] + a[i] + b[i] * j);
        }
        for (int j = i + 1; j < n; j++) {
            dp[i][j] = dp[i][j - 1];
        }
        for (int j = 0; j < n; j++) {
            if (j > pp[i]) {
                dp[i][j] = -INF;
            }
        }
    }
/*
    for (auto e : dp) {
        for (auto j : e) {
            cout << j << " ";
        }
        cout << endl;
    }
*/
    int ans = -INF;
    for (int i = minn; i < n; i++) {
        for (int j = 0; j < n; j++) {
            ans = max(ans, dp[i][j]);
        }
    }
    cout << ans << endl;
}
