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

#define int long long
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using pii = pair<int, int>;

const int INF = 4e18;


void solve() {
    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];
    vector<int> l(n, -1), r(n, -1);
    int prev = 0;
    int prev_p = 0;
    for (int i = 0; i < n; i++) {
        if (p[i] == -1) continue;
        for (int j = prev; j < p[i]; j++) {
            l[j] = prev_p;
            r[j] = min(j, i);
        }
        prev = p[i];
        prev_p = i + 1;
    }
    for (int i = 0; i < n; i++) {
        if (r[i] == -1){
            r[i] = i;
            l[i] = prev_p;
        }
        // cout << l[i] << " " << r[i] << "\n";
    } 
    int ans = -INF;
    int mn = max(0ll, *max_element(all(p)));
    if (mn == 0) ans = 0;
    int curr = 0;
    for (int i = 0; i < n; i++) {
        curr += a[i] + b[i] * l[i];
        if (i + 1 >= mn) {
            ans = max(ans, curr);
        }
    }
    cout << ans;
 




    // vector<vector<int>> dp(n + 1, vector<int> (n + 1, -INF));
    // dp[0][0] = 0;
    // for (int i = 0; i < n; i++) {
    //     int mx = -INF;
    //     for (int j = 0; j < l[i]; j++) {
    //         mx = max(mx, dp[i][j]);
    //     }
    //     for (int j = l[i]; j <= r[i]; j++) {
    //         mx = max(mx, dp[i][j]);
    //         dp[i + 1][j] = max(dp[i + 1][j], mx + a[i] + b[i] * j);
    //     }
    // }
    // int ans = -INF;
    // int mn = max(0ll, *max_element(all(p)));
    // for (int i = mn; i <= n; i++) {
    //     for (int j = 0; j <= n; j++)
    //         ans = max(ans, dp[i][j]);
    // }
    // cout << ans;
    



    

}


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

    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }

}