#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pb push_back
#define endl "\n"
using ll = long long;
using pii = pair<int, int>;
using ld = long double;

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

template <typename t>
ostream& operator<<(ostream &out, vector<t> &a){
    for (auto &el : a){
        out << el << " ";
    }
    return out;
}

int lg = 20;
const int maxn = 1e6;
int _lg[maxn];



class st{
private:
    vector<vector<pii>> mn;

public:
    st(int n, vector<int> a): mn(lg, vector<pii> (n)){
        for (int i = 0; i < n; ++i){
            mn[0][i] = {a[i], i};
        }
        for (int j = 1; j < lg; ++j){
            for (int i = 0; i < n; ++i){
                mn[j][i] = mn[j - 1][i];
                if (i + (1 << (j - 1)) < n) mn[j][i] = min(mn[j][i], mn[j - 1][i + (1 << (j - 1))]);
            }
        }
    }

    pii get_min(int l, int r){
        int _l = _lg[r - l];
        return min(
            mn[_l][l], 
            mn[_l][r - (1 << _l)]
        );
    }

};


void solve(){
    // cout << 0 << endl;
    // return;
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (int i = 0; i < n; ++i){
        cin >> a[i] >> b[i];
    }
    vector<int> pra(n + 1), prb(n + 1);
    for (int i = 0; i < n; ++i){
        pra[i + 1] = pra[i] + a[i];
        prb[i + 1] = prb[i] + b[i];
    }
    st _mn(n + 1, prb);
    vector<int> p(n);
    for (int i = 0; i < n; ++i){
        cin >> p[i];
    }
    vector<pii> v;
    v.pb({0, 0});
    for (int i = 0; i < n; ++i){
        if (p[i] == -1) continue;
        v.pb({i + 1, p[i]});
    }
    int inf = 1e18;
    int ans = 0;
    // int now = 0;
    // for (int i = 0; i < n; ++i){
    //     now += a[i];
    //     ans = max(ans, now);
    // }
    // cout << ans << endl;
    // return;
    for (int ind = 0; ind < (int) v.size() - 1; ++ind){
        int i = v[ind].first;
        int nowcnt = v[ind].second;
        int j = v[ind + 1].first;
        int needcnt = v[ind + 1].second;
        
        // int sm = prb[nowcnt + 1];

        // pii mn = _mn.get_min(nowcnt + 1, needcnt + 1);
        // int k = mn.second - 1;
        // // k = needcnt - 1;
        // ans += pra[k + 1] - pra[nowcnt];
        // ans += (prb[k + 1] - prb[nowcnt]) * i;
        // continue;
        for (int m = i; m < j; ++m){
            if (nowcnt == i){
                ans += a[nowcnt] + b[nowcnt] * m;
                nowcnt++;
            }
            pii kk = _mn.get_min(nowcnt, needcnt + 1);
            int k = kk.second;
            ans += pra[k] - pra[nowcnt];
            ans += (prb[k] - prb[nowcnt]) * m;
            nowcnt = k;
        }
        int m = j - 1;
        ans += pra[needcnt] - pra[nowcnt];
        ans += (prb[needcnt] - prb[nowcnt]) * m;
        nowcnt = needcnt;
    }
    int i = v.back().second;
    int cnt = v.back().second;
    // ans += pra[n] - pra[cnt];
    int now = 0;
    int res = 0;
    for (int j = cnt; j < n; ++j){
        now += a[j] + b[j] * i;
        res = max(res, now);
    }

    cout << ans + res << endl;
}


signed main(){
    _lg[1] = 0;
    for (int i = 2; i < maxn; ++i){
        _lg[i] = _lg[i / 2] + 1;
    }
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    ios_base::sync_with_stdio(0); cin.tie(0);
#endif
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
}