#pragma GCC optimize("O3")
#pragma GCC target("avx2", "bmi2", "popcnt", "lzcnt")
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#include <climits>
#include <queue>
#include <random>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <vector>
#include <string>

using namespace std;
using namespace __gnu_pbds;

typedef long long int lint;
typedef long double ld;
typedef tree<int, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ost;

constexpr lint INF = 4e18 + 17;

inline void preset() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
}

signed main() {
    preset();
    int n; cin >> n;
    vector <pair<lint, lint>> a(n);
    for (auto &[c, b]: a) cin >> c >> b; // bt + c
    // vector <vector<lint>> dp(n + 1, vector<lint>(n, -INF));
    // dp[0][0] = 0;
    // fill(dp[0].begin(), dp[0].end(), 0ll);
    vector <int> p(n);
    for (auto &el: p) cin >> el;
    vector <pair<int, int>> LR(n, make_pair(0, n));
    for (int i = 0; i < n; ++i) LR[i].second = i + 1;
    for (int i = 0; i < n; ++i) {
        if (p[i] != -1) {
            for (int j = 0; j < p[i]; ++j) LR[j].second = min(LR[j].second, i + 1);
            for (int j = p[i]; j < n; ++j) LR[j].first = max(LR[j].first, i + 1);
        }
    }
    vector <lint> sufmax(n, 0ll);
    vector <lint> dp(n, -INF);
    vector <lint> res(n + 1);
    res[0] = 0;
    for (int i = 0; i < n; ++i) {
        // fill dp[i + 1][time]
        auto [c, b] = a[i];
        auto [l, r] = LR[i];
        // cout << i << " " << l << " " << r << endl;
        for (int t = l; t < r; ++t) {
            // for (int t1 = 0; t1 <= t; ++t1) {
            //     dp[i + 1][t] = max(dp[i + 1][t], dp[i][t1] + c + b * t);
            // }
            //dp[i + 1][t] = sufmax[t];
            dp[t] = max(-INF, sufmax[t] + c + b * t);
        }
        sufmax[0] = dp[0];
        for (int j = 1; j < n; ++j) sufmax[j] = max(sufmax[j - 1], dp[j]);
        res[i + 1] = sufmax.back();
    }
    int k = max(0, *max_element(p.begin(), p.end()));
    // lint mx = -INF;
    // for (int i = k; i <= n; ++i) for (int t = 0; t < n; ++t) {
    //     mx = max(mx, dp[i][t]);
    // }
    // cout << mx << endl;
    cout << *max_element(res.begin() + k, res.end());
    return 0;
}