#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

using namespace std;
using ll = long long;

template<typename T>
using ordered_set = tree<T, null_type, less<T>,
    rb_tree_tag, tree_order_statistics_node_update>;

template<typename T, typename Y>
istream &operator>>(istream &in, pair<T, Y> &a) {
    return in >> a.first >> a.second;
}

template<typename T, typename Y>
ostream &operator<<(ostream &out, const pair<T, Y> &a) {
    return out << a.first << ' ' << a.second;
}

template<typename T>
istream &operator>>(istream &in, vector<T> &a) {
    for (auto &i: a) {
        in >> i;
    }
    return in;
}

template<typename T>
ostream &operator<<(ostream &out, const vector<T> &a) {
    for (const auto &i: a) {
        out << i << ' ';
    }
    return out;
}

template<typename T>
void print(const vector<T> &a, string c = "\n") {
    for (const auto &i: a) {
        cout << i << c;
    }
}

const ll INF = 1e18;

void solve() {
    ll n;
    cin >> n;
    vector<ll> a(n + 1), b(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> a[i] >> b[i];
    }
    vector<ll> p(n + 1);
    ll lst = -1;
    vector<int> l(n + 1, 0), r(n + 1, n);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        if (p[i] != -1) {
            lst = max(lst, p[i]);
            l[p[i] + 1] = i + 1;
            r[p[i]] = min(r[p[i]], i);
        }
    }
    for (int i = 1; i <= n; ++i) {
        l[i] = max(l[i], l[i - 1]);
    }
    for (int i = n - 1; i >= 1; --i) {
        r[i] = min(r[i], r[i + 1]);
    }
    // ll mx = lst == -1 ? 0 : -INF, now = 0;
    // for (int i = 1; i <= n; ++i) {
    //     now += a[i] + b[i] * l[i];
    //     if (i >= lst) {
    //         mx = max(mx, now);
    //     }
    // }
    ll sum = 0, mx = lst == -1 ? 0 : -INF;
    for (int i = 1; i <= n; ++i) {
        sum += a[i] + b[i] * min((i - 1), r[i]);
        if (i >= lst) {
            mx = max(mx, sum);
        }
    }
    cout << mx;
}

int main() {
#ifdef LOCAL
    freopen("inp.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    freopen("err.txt", "w", stderr);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int times = 1;
    // cin >> times;
    while (times--) {
        solve();
        cout << '\n';
    }
}
