// template.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
// #define int long long

using namespace std;
using ll = long long;
using ld = long double;

const int MOD = 998244353;

int modpow(int a, int b)
{
    if (b == 0)
        return 1;
    if (b & 1)
        return 1ll * a * modpow(a, b - 1) % MOD;
    return modpow(1ll * a * a % MOD, b >> 1);
}

const int MAXN = 1e6 + 10;
ll dp[MAXN];
int tmq[MAXN];
int ctm = 0;
ll A = 0, B = 0;

int a[MAXN], b[MAXN];

ll get_raw_dp(int x)
{
    if (tmq[x] < ctm)
        return max(0ll, dp[x]);
    return dp[x];
}

ll get_dp(int x)
{
    return max(0ll, get_raw_dp(x) + A + B * x);
}

void set_dp(int x, ll y)
{
    y -= A;
    y -= B * x;
    //dp[x] = max(dp[x], y);
    dp[x] = y;
    tmq[x] = ctm;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    memset(tmq, 0, sizeof(tmq));
    int n;
    cin >> n;
    for (size_t i = 0; i < n; i++)
    {
        cin >> a[i] >> b[i];
    }
    vector <int> p(n);
    for (auto& i : p)
    {
        cin >> i;
    }
    for (size_t i = 0; i <= n; i++)
    {
        dp[i] = -1e18;
    }
    for (int i = n - 1; i >= 0; i--)
    {
        if (b[i] > 0)
        {
            ll res = 0;
            for (int j = i; j >= 0; j--)
            {
                res = max(res, a[i] + 1ll * j * b[i] + get_dp(j));
                set_dp(j, res);
                
                // dp[j] = res;
            }
        }
        else
        {
            /*for (size_t j = 0; j <= i; j++)
            {
                set_dp(j, get_dp(j));
            }*/
            ctm++;
            A += a[i];
            B += b[i];
            /*for (size_t j = 0; j <= i; j++)
            {
                set_dp(j, get_dp(j) + a[i] + 1ll * j * b[i]);
            }*/
        }
#ifdef _DEBUG
        cout << "dp = ";
        for (size_t j = 0; j <= i + 1; j++)
        {
            cout << get_dp(j) << ' ';
        }
        cout << '\n';
#endif
    }
    cout << get_dp(0) << '\n';
    return 0;
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
