#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <cstdio>
#include <bitset>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <cmath>

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    long long n;
    std::cin >> n;
    std::vector<long long> a(n), b(n);
    for (long long i = 0; i < n; ++i) {
        std::cin >> a[i] >> b[i];
    }
    std::vector<long long> p(n + 1, -1);
    for (long long i = 1; i <= n; ++i) {
        std::cin >> p[i];
    }
    int ind = *max_element(p.begin(), p.end());
    if (n <= 5000) {
        std::vector<long long> last_res(n + 1, 0);
        long long res = -3e18;
        if (ind == -1) res = 0;
        for (long long i = 0; i < n; ++i) {
            std::vector<long long> cur_res(n + 1, -3e18);
            long long tfix = 0;
            for (int t = 0; t <= n; ++t) {
                if (p[t] <= i && p[t] != -1) {
                    tfix = t;
                }
            }
            for (long long t = tfix; t <= i; ++t) {
                if (p[t] >= i + 1ll) {
                    break;
                }
                cur_res[t] = a[i] + b[i] * t;
                cur_res[t] += *std::max_element(last_res.begin(), last_res.begin() + t + 1);
                if (i >= ind - 1) res = std::max(res, cur_res[t]);
            }
            last_res = cur_res;
        }
        std::cout << res << '\n';
    } else {
        long long res = -3e18;
        if (ind == -1) {
            res = 0;
        }
        long long cur = 0;
        ind = 0;
        long long t = 0;
        for (int i = 0; i <= n; ++i) {
            if (ind == n) break;
            if (p[i] != -1) {
                while (ind < p[i] && ind < n) {
                    cur += a[ind] + b[ind] * t;
                    ++ind;
                }
                t = i;
            }
        }
        ind = *max_element(p.begin(), p.end());
        for (int i = ind; i < n; ++i) {
            cur += a[i] + b[i] * t;
            if (ind - 1 <= i) {
                res = std::max(res, cur);
            }
        }
        std::cout << res << '\n';
    }
    return 0;
}