#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target ("avx2,bmi")
using namespace std;
// using namespace __gnu_pbds;
#define int long long
// typedef tree<int,null_type,less<>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
const int N = 1e6 + 10, mod = 998244353, mod1 = 1e9 + 9, inf = 1e18;
int d[N];

void solve() {
    int n, m;
    cin >> n;
    vector<int> a(n), b(n);
    for (int i = 0; i < n; i++)cin >> a[i] >> b[i];
    vector<int> p(n);
    for (int i = 0; i < n; i++)cin >> p[i];
    for (int i = 0; i < n; i++)d[i] = -inf;
    int ans = -inf;
    for (int t = 0; t < n; t++) {
        if (p[t] == -1) {
            for (int i = n - 1; i >= t; i--) {
                int q = 0, w = 0;
                for (int j = i; j >= t; j--) {
                    q += b[j];
                    w += a[j];
                    if (j == 0)
                        d[i] = max(d[i], t * q + w);
                    else
                        d[i] = max(d[i], d[j - 1] + t * q + w);
                    ans = max(ans, d[i]);
                }
            }
        }
    }
    cout << ans;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
