// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")

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

typedef long long ll;
typedef long double ld;
typedef __int128 lll;

const ll inf = 1e18 + 1, mod = 998244353;
const ld eps = 1e-9, pi = acos(-1);
#define all(a) a.begin(), a.end()
#define mpp make_pair
#define F first
#define S second

mt19937_64 rng(234);

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ll n;
    cin >> n;
    vector<ll> a(n + 1), b(n + 1);
    for (ll i = 1; i <= n; i++)
        cin >> a[i] >> b[i];
    vector<ll> w(1), h(1);
    for (ll i = 1; i <= n; i++) {
        ll x;
        cin >> x;
        if (x != -1) {
            w.push_back(i);
            h.push_back(x);
        }
    }
    ll ans = 0;
    for (ll i = 1; i < w.size(); i++) {
        for (ll j = h[i - 1] + 1; j <= h[i]; j++)
            if (b[j] < 0)
                ans += w[i - 1] * b[j] + a[j];
            else
                ans += min(j - 1, w[i] - 1) * b[j] + a[j];
    }
    ll sm = 0, mx = 0;
    vector<ll> dp(n + 1, -inf);
    for (ll i=w.back();i<=n;i++)
        dp[i]=0;
    for (ll i = h.back() + 1; i <= n; i++) {
        vector<ll> dp2(n + 1, -inf);
        for (ll j = 0; j < i; j++)
            if (dp[j] != -inf) {
                dp2[j] = dp[j] + a[i] + b[i] * j;
                mx = max(mx, dp2[j]);
                if (j)
                    dp2[j] = max(dp2[j], dp2[j - 1]);
            }
        dp = dp2;
        for (ll j=i;j<=n;j++)
            dp[j]=dp[j-1];
        if (mx < sm)
            mx = sm;
    }
    cout << ans + mx;
}
