//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define ld long double
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define coutp cout << "       "
#define cont continue
#define F first
#define S second

using namespace std;
using namespace __gnu_pbds;

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

template<typename T>
istream& operator>> (istream& in, pair<T, T>& a) {
    in >> a.F >> a.S;
    return in;
}

template<typename T>
ostream& operator<< (ostream& os, pair<T, T>& a) {
    os << a.F << " " << a.S;
    return os;
}

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

template<typename T>
ostream& operator<< (ostream& os, vector<T>& a) {
    for (auto& x : a) {
        os << x << " ";
    }
    return os;
}

constexpr int inf = 2e18, mod = 998244353;
constexpr ld eps = 1e-9;

int add(int a, int b) {
    return (a + b) % mod;
}

int mul(int a, int b) {
    return (a * b) % mod;
}

int fast_pow(int a, int k) {
    if (k == 0) {
        return 1;
    }
    int q = fast_pow(a, k / 2);
    q = mul(q, q);
    if (k % 2 == 1) {
        q = mul(q, a);
    }
    return q;
}

int mi(int a, int b) {
    return (a + mod - b) % mod;
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int multitest = 1; //cin >> multitest;
    while (multitest--) {
        int n; cin >> n;
        vector<int> a(n), b(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i] >> b[i];
        }
        vector<int> p(n); cin >> p;
        vector<vector<int>> dp(n + 1, vector<int>(n, -inf));
        dp[0] = vector<int>(n);
//        vector<int> dop(n + 1);
//        int mx = 0;
//        for (int i = 0; i < n; i++) {
//            if (p[i] == -1) {
//                cont;
//            }
//            dop[p[i]] = max(dop[p[i]], i + 1);
//            for (int j = 0; j < p[i]; j++) {
//                if (dp[j + 1].size() > i + 1) {
//                    dp[j + 1] = vector<int>(i + 1, -inf);
//                }
//            }
//        }
        int ans = -inf;
        for (int i = 0; i < n; i++) {
            //mx = max(mx, dop[i]);
            int t = dp[i].size();
            vector<int> pr(t + 1, -inf);
            for (int j = 0; j < t; j++) {
                pr[j + 1] = max(dp[i][j], pr[j]);
            }
        //coutp << i << " " << mx << endl;
            for (int j = 0; j <= i; j++) {
                dp[i + 1][j] = pr[j + 1] + a[i] + b[i] * j;
            }
            ans = max(ans, *max_element(all(dp[i + 1])));
            //cout << i << " : " << dp[i + 1] << endl;
        }
        cout << ans;
    }
    return 0;
}

/*
4
3-6
4-10
-7-3
-3 6
3 3 -1-1
*/
