#pragma GCC optimize("Ofast")
#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 int MOD = (int) 1e9 + 7;
constexpr lint INF = 8e18 + 17;

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

#define int lint

vector <pair<lint, lint>> a;

inline int get(int i, lint t) {
    auto [c, b] = a[i];
    return c + b * t;
}

signed main() {
    preset();
    int n; cin >> n;
    a.resize(n);
    for (auto &[c, b]: a) cin >> c >> b; // bt + c
    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;
    int L = 0;
    for (int i = 0; i < n; ++i) {
        if (p[i] != -1) {
            for (; L < p[i]; ++L) LR[L].second = min(LR[L].second, i + 1);
            for (int j = p[i]; j < n; ++j) LR[j].first = max(LR[j].first, i + 1);
        }
    }
    lint mx = -INF;
    int R = 0;
    lint s = 0;
    vector <lint> res(n + 1);
    res[0] = 0;
    for (int i = 0; i < n; ++i) {
        auto [l, r] = LR[i];
        s += get(i, l);
        res[i + 1] = s;
    }
    const int k = max((int) 0, *max_element(p.begin(), p.end()));
    cout << *max_element(res.begin() + k, res.end());
    return 0;
}