#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif

using namespace std;

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ff first
#define ss second
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = long double;

constexpr int MOD = (int)1e9 + 7;

struct mint {
    int x;
    mint(int y) {
        if (y >= MOD) y -= MOD;
        if (y < 0) y += MOD;
        x = y;
    }
    mint(ll y) {
        if (y >= MOD) y %= MOD;
        if (y < 0) y += MOD;
        x = y;
    }
    mint operator+(const mint& b) const {
        return x + b.x;
    }
    mint operator-(const mint& b) const {
        return x - b.x;
    }
    mint operator*(const mint& b) const {
        return x * (ll) b.x;
    }
    mint& operator+=(const mint& b) {
        return *this = *this + b;
    }
    mint& operator-=(const mint& b) {
        return *this = *this - b;
    }
    mint& operator*=(const mint& b) {
        return *this = *this * b;
    }
};

struct point {
    ll x, y;

    point operator+(const point& b) const {
        return {x + b.x, y + b.y};
    }
    point operator-(const point& b) const {
        return {x - b.x, y - b.y};
    }
    ll operator*(const point& b) const {
        return x * b.x + y * b.y;
    }
    ll operator%(const point& b) const {
        return x * b.y - y * b.x;
    }
    ll len_sqr() const {
        return x * x + y * y;
    }
    ld len() const {
        return sqrtl(len_sqr());
    }
};

signed main() {
#ifdef LOCAL
    freopen("in", "r", stdin);
    freopen("out", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vector<ll> a(n);
    vector<ll> b(n);
    for (int i = 0; i < n; i++) {
        int x, y;
        cin >> x >> y;
        a[i] = x;
        b[i] = y;
    }
    vector<int> p(n);
    for (int i = 0; i < n; i++) cin >> p[i];
    // int next_limit = 0;
    // while (next_limit < n && p[next_limit] == -1) {
    //     next_limit++;
    // }
    // ll ans = -1e18;
    // ll now_ans = 0;
    // if (next_limit == n) ans = 0;
    // for (int i = 0; i < n; i++) {
    //     int nowb = 0;
    //     if (next_limit < n) {
    //         nowb = max(nowb, next_limit);
    //     }
    //     now_ans += a[i] + b[i] * nowb;
    //     while (next_limit < n && (p[next_limit] == i + 1 || p[next_limit] == -1)) {
    //         next_limit++;
    //     }
    //     if (next_limit == n) {
    //         ans = max(ans, now_ans);
    //     }
    // }
    // cout << ans << '\n';

    vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, -1e18));
    dp[0][0] = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < i; j++) {
            if (p[j] != -1 && p[j] < i) continue;
            ll x = a[i - 1] + b[i - 1] * j;
            dp[i][j] = max(dp[i][j], dp[i - 1][j] + x);
            if (p[j] != -1 && p[j] >= i) {
                if (p[j] == i) {
                    for (int z = 0; z < j; z++) {
                        dp[i][z] = -1e18;
                    }
                }
                break;
            }
        }
        for (int j = 1; j < i; j++) {
            dp[i][j] = max(dp[i][j], dp[i][j - 1]);
        }
    }
    int min_i = *max_element(all(p));
    if (min_i < 0) min_i++;
    ll ans = -1e18;
    for (int i = min_i; i <= n; i++) {
        for (int j = 0; j <= i; j++) {
            ans = max(ans, dp[i][j]);
        }
    }
    cout << ans << '\n';
}