#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;
typedef tree<int, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

#define int long long

signed main() {
    ios::sync_with_stdio(0);
    cin.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];
    }
    for (auto & it : p) {
        cin >> it;
    }
    int ans = 0;
    vector<int>t(n + 1,-1e9);
    int last = 0;
    int lastp = 0;
    int i = 0;
    int cur = -1e9;
    for (; i  <n;++i ) {
        if (p[i] != -1) {
        }else {
            if (last != -1) {
                t[last] = -1;
            }
        }
        if (p[i] != last && p[i] != -1) {
            t[last] = i;
        }
        if (t[i] != -1e9) {
            cur = t[i];
        }
        if (cur == -1) {
            break;
        }
        ans += a[i] + b[i] * cur;
        last = p[i];
        if (p[i] != -1) {
            lastp = p[i];
        }
    }
    if (i != n) {
        int d = 0;
        vector<vector<int>>dp(n + 1,vector<int>(n + 1));
        vector<int>mx(n + 1,-1e9);
        for (int j = i + 1; j <= n; ++j) {
            for (int m = lastp; m < j; ++m) {
                dp[j][m] = mx[m] + a[j-1] + b[j-1] * m;
                if (j == i + 1) {
                    dp[j][m] = max(dp[j][m],a[j-1] + b[j-1] * m);
                }
                d = max(d,dp[j][m]);
                if (m == lastp) {
                    mx[m] = dp[j][m];
                }else {
                    mx[m] = max(dp[j][m],mx[m-1]);
                }
            }
            for (int m = j; m < n; ++m) {
                mx[m] = mx[m-1];
            }
        }
        ans += d;
    }
    cout << ans;
    return 0;
}
