#include <iostream>
#include <fstream>
#include <iomanip>
#include <random>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <array>
#include <bitset>
//#define int long long
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front

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

const int N = 5002;
const ll INF = 1e18;

ll dp[N][N];

void sol() {
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) dp[i][j] = -INF;
    }
    dp[0][0] = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
        dp[0][i + 1] = dp[0][i] + a[i];
    }
    vector<int> p(n);
    for (int &i : p) cin >> i;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j <= n; j++) {
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
            if (j >= i && j < n) dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + a[j] + (ll)b[j] * i);
        }
    }
    ll ans = -INF;
    for (int i = 0; i <= n; i++) {
        ans = max(ans, dp[n][i]);
    }
    cout << ans << '\n';
    return;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    sol();
    return 0;
}
