#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using ll = long long;
#define int long long
template<typename T>
using ordered_set = tree<T, null_type, std::less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using namespace std;

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

template<typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &a) {
    return out << a.first << ' ' << a.second << '\n';
}

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 (auto &i: a)out << i << ' ';
    return out;
}

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

constexpr int mod = 998244353;

template<typename T>
T fast_pow(T a, int n) {
    if (n == 0)return 1;
    if (n % 2 == 1)return a * fast_pow(a, n - 1);
    T t = fast_pow(a, n / 2);
    return t * t;
}

struct Zm {
    int k;

    Zm(int k = {}) : k(k % mod) {
        if (this->k < 0)this->k += mod;
    }

    Zm operator+(const Zm &other) const {
        return {k + other.k};
    }

    Zm operator-(const Zm &other) const {
        return {k - other.k};
    }

    Zm operator*(const Zm &other) const {
        return {k * other.k};
    }
};

constexpr int inf = 1e18 + 7;

struct Test {
    int n;
    vector<int> a;
    vector<int> b;
    vector<pair<int,int> > c;
    vector<int> p;

    void ans_neg() {
        vector<int> dp(n + 1, -inf);
        dp[0] = 0;
        int last = 0;
        queue<pair<int,int> > st;
        for (int i = 0; i < n; i++) {
            if (p[i] != -1) {
                st.emplace(p[i], i);
            }
            if (st.size() == 0)dp[i + 1] = dp[i] + a[i] + b[i] * last;
            else {
                auto &[x,y] = st.front();
                dp[i + 1] = dp[i] + a[i] + b[i] * last;
                if (x == i + 1) {
                    st.pop();
                    last = y + 1;
                }
            }
        }
        int lst = 0;
        // for (int i=0;i<n;i++) {
        //     if (p[i]!=-1)lst=max(p[i],lst);
        // }
        //cout<<dp;
        cout << *max_element(dp.begin() + lst, dp.end()) << '\n';
    }
    void ans_pos() {
        vector<int> dp(n + 1, -inf);
        dp[0] = 0;
        for (int i = 0; i < n; i++) {
            dp[i + 1] = dp[i] + a[i] + b[i] * i;
        }
        int lst = 0;
        // for (int i=0;i<n;i++) {
        //     if (p[i]!=-1)lst=max(p[i],lst);
        // }
        //cout<<dp;
        cout << *max_element(dp.begin() + lst, dp.end()) << '\n';
    }
    void ans_hiu() {
        vector<int>dp(n+1,-inf);
        for (int i = 0; i < n; i++) {
            dp[i + 1] = dp[i] + a[i] + max(b[i] * i,0ll);
        }
        cout << *max_element(dp.begin(), dp.end()) << '\n';

    }
    Test() {
        cin >> n;
        c.resize(n);
        cin >> c;
        for (auto [x,y]: c) {
            a.push_back(x);
            b.push_back(y);
        }
        p.resize(n);
        cin >> p;
        ans_hiu();
    }
};

signed main() {
#ifdef home
    freopen(".in", "r",stdin);
    freopen(".out", "w",stdout);
#else
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#endif
    int t = 1;
    //cin >> t;
    while (t--) {
        Test();
    }
}
