// Увидеть мир в одной песчинке
// И Космос весь в лесной травинке,
// Вместить в ладони бесконечность
// И в миге мимолетном вечность

#include <iostream>
#include <numeric>
#include <string>
#include <vector>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <deque>
#include <queue>
#include <stack>
#include <chrono>
#include <random>
#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;
using ull = unsigned long long;
using ld = long double;
// using int128 = __int128_t;
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

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

ll MOD = 998244353;
struct mint {
    ll num;
    mint(ll _num) : num(_num) {}
    mint() {}
    mint operator+(mint y) {
        if (num + y.num >= MOD) return num + y.num - MOD;
        return num + y.num;
    }
    mint operator-(mint y) {
        if (num - y.num < 0) return num - y.num + MOD;
        return num - y.num;
    }
    mint operator*(mint y) {
        return num * y.num % MOD;
    }
    mint pow(int n) {
        mint res = 1;
        mint a = *this;
        for (int i = 0; (1ll << i) <= n; ++i, a = a * a) {
            if ((n >> i) & 1) res = res * a;
        }
        return res;
    }
    mint inv() {
        return (*this).pow(MOD - 2);
    }
    mint operator/(mint y) {
        return num * y.inv().num % MOD;
    }
    mint operator+=(mint y) {
        num = (*this + y).num;
    }
    mint operator-=(mint y) {
        num = (*this - y).num;
    }
    mint operator*=(mint y) {
        num = (*this * y).num;
    }
    mint operator/=(mint y) {
        num = (*this / y).num;
    }
};
istream& operator>>(istream &in, mint &x) {
    in >> x.num;
    return in;
}
ostream& operator<<(ostream &out, mint &x) {
    out << x.num;
    return out;
}


void solve() {
    int n;
    cin >> n;
    vector<pair<ll, ll>> lines(n);
    for (auto &[f, s] : lines) cin >> f >> s;
    vector<int> p(n);
    for (auto &el : p) cin >> el;

    ll ans = 0;
    int pr = 0;
    int t1 = 0, t2 = 0;
    auto calc2 = [&](int l, int r, int mid, int t1, int t2) {
        ll res = 0;
        for (int i = l; i < mid; ++i) {
            res += lines[i].first + lines[i].second * t1;
        }
        for (int i = mid; i < r; ++i) {
            res += lines[i].first + lines[i].second * min(i, t2);
        }
        return res;
    };
    auto calc = [&](int l, int r, int t1, int t2) {
        if (l == r) return 0ll;
        int L = l, R = r;
        while (R - L > 2) {
            int mid = (L + R) / 2;
            if (calc2(l, r, mid, t1, t2) > calc2(l, r, mid + 1, t1, t2)) {
                R = mid + 1;
            } else {
                L = mid;
            }
        }
        ll mx = -1e18;
        for (int i = L; i < R; ++i) {
            mx = max(mx, calc2(l, r, i, t1, t2));
        }
        return mx;
    };
    for (int i = 0; i < n; ++i) {
        t2 = i;
        if (p[i] != -1) {
            ans += calc(pr, p[i], t1, t2);
            pr = p[i];
            t1 = t2 = t1 + 1;
        }
    }
    if (pr != n) {
        ll mx = 0;
        for (int i = pr; i < n; ++i) {
            mx = max(mx, calc(pr, i + 1, t1, min(t2, i)));
        }
        ans += mx;
    }
    cout << ans;
}

#define LOCAL
int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int t = 1;
    // cin >> t;
    while (t--) solve();
    return 0;
}