#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()
#define debout(x) std::cout << #x << " : " << x << '\n';
#define debvout(x) std::cout << #x << " : "; for (auto i: x) std::cout << i << ' '; std::cout << '\n';
using ll = long long;

const ll inf = 1e18;
ll N;

class Wise_Tree {
  struct node {
    ll max, add;
    node() : max(-inf), add(0) {}
    node(ll x) : max(x), add(0) {}  
  };

  std::vector<node> tree;

  void push(ll v) {
    if (tree[v].add == 0) return;
    ll x = tree[v].add;
    tree[v * 2 + 1].max += x, tree[v * 2 + 2].max += x;
    tree[v * 2 + 1].add += x, tree[v * 2 + 2].add += x;
    tree[v].add = 0;
  }

  node f(node x, node y) {
    node ans;
    if (x.max > y.max) ans = node(x.max);
    else ans = node(y.max);
    return ans;
  }

  public:
  Wise_Tree() { tree.resize(4 * N); }

  void updi(ll i, ll x, ll v = 0, ll l = 0, ll r = N) {
    if (r <= i || i < l) return;
    if (r - l == 1) {
      tree[v].max = x;
      return;
    }
    push(v);
    updi(i, x, v * 2 + 1, l, (l + r) >> 1), updi(i, x, v * 2 + 2, (l + r) >> 1, r);
    tree[v] = f(tree[v * 2 + 1], tree[v * 2 + 2]);
  }

  void upd(ll ql, ll qr, ll x, ll v = 0, ll l = 0, ll r = N) {
    if (r <= ql || qr <= l) return;
    if (ql <= l && r <= qr) {
      tree[v].max += x;
      tree[v].add += x;
      return;
    }
    push(v);
    upd(ql, qr, x, v * 2 + 1, l, (l + r) >> 1), upd(ql, qr, x, v * 2 + 2, (l + r) >> 1, r);
    tree[v] = f(tree[v * 2 + 1], tree[v * 2 + 2]);
  }

  ll get(ll v = 0, ll l = 0, ll r = N) {
    if (tree[v].max < 0) return -1;
    if (r - l == 1) return l;
    push(v);
    if (tree[v * 2 + 2].max >= 0) return get(v * 2 + 2, (l + r) >> 1, r);
    else return get(v * 2 + 1, l, (l + r) >> 1);
  }
};

void s1nm() {
  ll n;
  std::cin >> n;
  N = n;
  std::vector<ll> a(n), b(n), p(n);
  for (ll i = 0; i < n; i++) std::cin >> a[i] >> b[i];
  for (ll &i: p) std::cin >> i;
  std::vector<ll> prefa(n), prefb(n);
  std::partial_sum(all(a), prefa.begin()), std::partial_sum(all(b), prefb.begin());
  std::vector<ll> dp(n);
  Wise_Tree tree;
  tree.upd(0, 1, b[0]);
  dp[0] = a[0];
  for (ll i = 1; i < n; i++) {
    tree.updi(i, b[i]);
    tree.upd(0, i, b[i]);
    ll l = tree.get();
    if (l <= 0) {
      dp[i] = prefa[i];
      continue;
    }
    dp[i] = dp[l - 1] + prefa[i] - prefa[l - 1] + (prefb[i] - prefb[l - 1]) * l;
  }
  std::cout << std::max(0ll, *std::max_element(all(dp)));
}

signed main() {
  std::ios::sync_with_stdio(false), std::cin.tie(nullptr);
  ll TTT = 1;
  // std::cin >> TTT;
  while (TTT--) {
    s1nm();
    std::cout << '\n';
  }
}