#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

struct Sgt_push {
    long long add;
    Sgt_push() : add(0) {}
    Sgt_push(long long a) : add(a) {}
    Sgt_push apply(Sgt_push other) {
        return add + other.add;
    }
};

struct Sgt_Node {
    long long sm, sz;
    Sgt_Node() : sm(0), sz(0) {}
    Sgt_Node(long long a, long long b) : sm(a), sz(b) {}
    Sgt_Node merge(Sgt_Node other) {
        return Sgt_Node(other.sm + sm, sz + other.sz);
    }
    Sgt_Node apply(Sgt_push other) {
        return Sgt_Node(other.add * sz + sm, sz);
    }
};


struct Seg_tree {
    vector<Sgt_Node> tree;
    vector<Sgt_push> push;
    vector<long long> a;
    long long n;
    void build_q(vector<long long> a2) {
        a = a2;
        n = a.size();
        tree.resize(4 * n);
        push.resize(4 * n);
        build(0, 0, n);
    }

    void build(long long v, long long l, long long r) {
        if (l == r - 1) {
            tree[v] = Sgt_Node(a[l], 1);
            return;
        }
        build(2 * v + 1, l, (l + r) / 2);
        build(2 * v + 2, (l + r) / 2, r);
        tree[v] = tree[2 * v + 1].merge(tree[2 * v + 2]);
    }

    void update(long long v, long long l, long long r, long long ql, long long qr, Sgt_push add) {
        if (ql <= l && r <= qr) {
            tree[v] = tree[v].apply(add);
            push[v] = push[v].apply(add);
            return;
        }
        if (qr <= l || r <= ql) {
            return;
        }
        tree[2 * v + 1] = tree[2 * v + 1].apply(push[v]);
        tree[2 * v + 2] = tree[2 * v + 2].apply(push[v]);
        push[2 * v + 1] = push[2 * v + 1].apply(push[v]);
        push[2 * v + 2] = push[2 * v + 2].apply(push[v]);
        push[v] = Sgt_push();
        update(2 * v + 1, l, (l + r) / 2, ql, qr, add);
        update(2 * v + 2, (l + r) / 2, r, ql, qr, add);
        tree[v] = tree[2 * v + 1].merge(tree[2 * v + 2]);
    }

    void update_q(long long l, long long r, Sgt_push add) {
        update(0, 0, n, l, r, add);
    }

    Sgt_Node get(long long v, long long l, long long r, long long ql, long long qr) {
        if (ql <= l && r <= qr) {
            return tree[v];
        }
        if (qr <= l || r <= ql) {
            return Sgt_Node();
        }
        tree[2 * v + 1] = tree[2 * v + 1].apply(push[v]);
        tree[2 * v + 2] = tree[2 * v + 2].apply(push[v]);
        push[2 * v + 1] = push[2 * v + 1].apply(push[v]);
        push[2 * v + 2] = push[2 * v + 2].apply(push[v]);
        push[v] = Sgt_push();
        return get(2 * v + 1, l, (l + r) / 2, ql, qr).merge(get(2 * v + 2, (l + r) / 2, r, ql, qr));
    }

    Sgt_Node get_q(long long l, long long r) {
        return get(0, 0, n, l, r);
    }
};


struct Node_Aho {
    int go[26];
    int suf, supersuf, par, sz;
    bool term;
    char in;
    Node_Aho() {
        for (int i = 0; i < 26; i++) {
            go[i] = -1;
        }
        suf = -1;
        par = -1;
        sz = 0;
        term = false;
        supersuf = -1;
        in = '#';
    }
};

struct Aho {
    vector<Node_Aho> trie = {Node_Aho()};
    void add(string s) {
        int v = 0;
        for (auto &i : s) {
            if (trie[v].go[i - 'a'] == -1) {
                trie[v].go[i - 'a'] = trie.size();
                trie.push_back(Node_Aho());
                trie.back().par = v;
                trie.back().sz = trie[v].sz + 1;
                trie.back().in = i;
            }
            v = trie[v].go[i - 'a'];
        }
        trie[v].term = true;
    }


    void init(int v) {
        if (trie[v].suf != -1) {
            return;
        }
        //counting suf link
        if (v == 0) {
            trie[v].suf = -2;
        } else if (trie[v].par == 0) {
            trie[v].suf = 0;
        } else {
            trie[v].suf = trie[trie[trie[v].par].suf].go[trie[v].in - 'a'];
        }
        //counting supersuf link
        if (v != 0) {
            if (trie[trie[v].suf].term) {
                trie[v].supersuf = trie[v].suf;
            } else {
                trie[v].supersuf = trie[trie[v].suf].supersuf;
            }
        }
        //counting go
        for (long long i = 0; i < 26; i++) {
            if (trie[v].go[i] == -1) {
                if (v == 0) {
                    trie[v].go[i] = 0;
                } else {
                    trie[v].go[i] = trie[trie[v].suf].go[i];
                }
            }
        }
    }

    void initialize() {
        deque<long long> all;
        all.push_back(0);
        while (!all.empty()) {
            auto v = all[0];
            all.pop_front();
            for (auto &i : trie[v].go) {
                if (i != -1) {
                    all.push_back(i);
                }
            }
            init(v);
        }
    }
};

long long find(vector<long long> a, vector<long long> b, long long add) {
    vector<pair<long long, long long>> st;
    long long all = 0, now = 0, n = a.size(), sm_of_a = 0;
    vector<long long> pref(n + 1);
    for (long long i = 0; i < n; i++) {
        now += a[i];
        sm_of_a += a[i];
        pref[i + 1] = pref[i] + b[i];
        while (!st.empty() && pref[st.back().first] > pref[i + 1]) {
            now -= st.back().second;
            st.pop_back();
        }
        long long left = 0;
        if (st.size() >= 1) {
            left = st.back().first;
        }
        now += (pref[i + 1] - pref[left]) * (left + add);
        st.push_back({i + 1, (pref[i + 1] - pref[left]) * (left + add)});
        all = max(all, now);

    }

    //can optimize to O(n)
    return all;
}


int main() {
    #ifdef MAX
    freopen("input.txt", "r", stdin);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    /*
    //Sample of ordered set
    ordered_set X;
    X.insert(10);
    X.insert(101);
    cout << *X.find_by_order(10) << endl;
    cout << X.order_of_key(11) << endl;
    */
    //seg_tree is working
    //Hmmm...... HLD or centroids or Aho or grader? I choose grader
    //Hmmm...... HLD or centroids or Aho or stresstests? I choose stresstests
    ///Hmmm...... HLD or centroids or Aho or idk? I choose Aho
    //The time is up. Today we rise.
    long long n;
    cin >> n;
    vector<long long> a(n), b(n), p(n);
    for (long long i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    for (auto &i : p) {
        cin >> i;
    }
    vector<long long> pref(n + 1);
    for (long long i = 0; i < n; i++) {
        pref[i + 1] = pref[i] + b[i];
    }
    long long zxc = -1;
    vector<long long> can_more(n);
    for (long long i = n - 1; i >= 0; i--) {
        if (p[i] != -1) {
            zxc = p[i];
        }
        can_more[i] = zxc;
    }
    vector<pair<long long, long long>> new_b;
    for (long long i = 0; i <= n; i++) {
        new_b.push_back({pref[i], i - 1});
    }
    //cout << (1 << 22) << endl;
    vector<vector<pair<long long, long long>>> st(22, vector<pair<long long, long long>> (n + 1));
    st[0] = new_b;
    for (long long i = 1; i < 22; i++) {
        for (long long j = 0; j + (1 << i) <= n + 1; j++) {
            st[i][j] = min(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
        }
    }
    vector<long long> mx2(n + 3672);
    for (long long i = 2; i < mx2.size(); i++) {
        mx2[i] = mx2[i / 2] + 1;
    }
    long long last = 0, ans = 0;
        for (long long i = 0; i < n; i++) {
            if (last == n) {
                break;
            }
            if (p[i] != -1) {
                for (long long j = last; j < p[i]; j++) {
                    ans += a[j] + b[j] * i;
                    //cout << j << " " << ans << endl;
                }
                last = p[i];
                //cout << "after " << i << " second ans is " << ans << endl;
                continue;//we can do this manually
            }
            long long mx_can = can_more[i];
            if (mx_can != -1) {
                //we need mx on seg of pref on [max(i + 1, last), mx_can]
                long long l = max(i + 1, last), r = mx_can + 1;
                if (r - l >= mx2.size()) {
                    throw(0);
                }
                if (r <= l) {
                    throw(0);
                }
                long long k = mx2[r - l];
                auto mn = min(st[k][l], st[k][r - (1 << k)]); //we cant do this manually
                
                long long to = mn.second;
                for (long long j = last; j <= to; j++) {
                    ans += a[j] + b[j] * i;
                }
                last = to + 1;///we can do this manually
            } else {
                vector<long long> new_a, new_b;
                for (long long j = last; j < n; j++) {
                    new_a.push_back(a[j]);
                    new_b.push_back(b[j]);
                }
                //cout << "OK " << find(new_a, new_b, i) << " " << i << endl;
                ans = max(ans, ans + max(find(new_a, new_b, i), find(new_a, new_b, last)));
                break;
            }
            //cout << "after " << i << " second ans is " << ans << endl;
        }
        cout << ans << endl;
}