#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif

#include <vector>

#pragma GCC optimize("O3, Ofast")
#pragma GCC target("avx2")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;


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

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

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

void solve();

template<class T>
using oset = __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag,
    __gnu_pbds::tree_order_statistics_node_update>;

signed main() {
#ifdef LOCAL
    freopen(R"(C:\work\untitled\input.txt)", "r", stdin);
    freopen(R"(C:\work\untitled\output.txt)", "w", stdout);

    auto begin_ = chrono::steady_clock::now();
#else
    cin.tie(0);
    cout.tie(0);
    std::ios_base::sync_with_stdio(0);
#endif
    int t_ = 1;
    // cin >> t_;
    while (t_--) {
        solve();
    }
#ifdef LOCAL
    auto end_ = chrono::steady_clock::now();

    cout << "\n\n===========\n";
    cout << chrono::duration_cast<chrono::milliseconds>(end_ - begin_).count() << " ms";
#endif
}

using ll = long long;
#define int ll

using pii = pair<int, int>;
using vi = vector<int>;
using vp = vector<pii>;
using vv = vector<vi>;
ll inf = 1e18;

void solve() {
    int n;
    cin >> n;
    vi a(n), b(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
    }
    vi p(n, -1);
    cin >> p;
    p.push_back(-1);
    vi dp(vi(n + 4, -inf));
    dp[0] = 0;
    for (int time = 0; time <= n; ++time) {
        for (int take = time + 1; take <= n; take += 4) {
            for (int j = 0; j < 4; ++j) {
                dp[take + j] = max(dp[take + j], dp[take + j - 1] + a[take + j - 1] + (time) * b[take + j - 1]);
            }
        }
    }
    // vi x(n + 1);
    // for (int i = n - 1; i >= 0; --i) {
    //     x[i] = x[i + 1] + (i + 1) * b[i] + a[i];
    // }
    ll mn = -inf;
    for (int i = 0; i <= n; ++i) {
        mn = max(mn, dp[i]);
    }
    cout << mn << '\n';
}
