Problem:
Score : 100 points

Problem Statement

There are N flowers arranged in a row.
For each i (1 \leq i \leq N), the height and the beauty of the i-th flower from the left is h_i and a_i, respectively.
Here, h_1, h_2, \ldots, h_N are all distinct.

Taro is pulling out some flowers so that the following condition is met:

The heights of the remaining flowers are monotonically increasing from left to right.

Find the maximum possible sum of the beauties of the remaining flowers.

Constraints

All values in input are integers.

1 \leq N \leq 2 × 10^5

1 \leq h_i \leq N

h_1, h_2, \ldots, h_N are all distinct.

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 \ldots h_N
a_1 a_2 \ldots a_N

Output

Print the maximum possible sum of the beauties of the remaining flowers.

Sample Input 1

4
3 1 4 2
10 20 30 40

Sample Output 1

60

We should keep the second and fourth flowers from the left.
Then, the heights would be 1, 2 from left to right, which is monotonically increasing, and the sum of the beauties would be 20 + 40 = 60.

Sample Input 2

1
1
10

Sample Output 2

10

The condition is met already at the beginning.

Sample Input 3

5
1 2 3 4 5
1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

5000000000

The answer may not fit into a 32-bit integer type.

Sample Input 4

9
4 2 5 8 3 6 1 7 9
6 8 8 4 6 3 5 7 5

Sample Output 4

31

We should keep the second, third, sixth, eighth and ninth flowers from the left.

Solution in C++14 (Clang 3.8.0) (judge verdict: Accepted)

//
//  main.cpp
//
 
#include <algorithm>
#include <array>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <limits>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
 
using namespace std;
 
using ll = int64_t;
using ull = uint64_t;
 
template <typename T> vector<T> make_vec_nd(T init, size_t size) {
  return vector<T>(size, init);
}
template <typename T, typename... Args>
auto make_vec_nd(T init, size_t size, Args... rest) {
  auto inner = make_vec_nd(init, rest...);
  return vector<decltype(inner)>(size, inner);
}
 
template <typename Monoid> struct SegmentTree {
  using Op = function<Monoid(const Monoid &lhs, const Monoid &rhs)>;
  size_t width;
  vector<Monoid> node;
  Op op;
  Monoid unit;
 
  SegmentTree(const vector<Monoid> &elems, Op op, Monoid unit)
      : op(op), unit(unit) {
    width = 1;
    while (width < elems.size()) {
      width *= 2;
    }
    node.resize(2 * width - 1, unit);
    for (size_t i = 0; i < elems.size(); i++) {
      node[i + width - 1] = elems[i];
    }
    for (size_t i = width - 2; i != numeric_limits<size_t>::max(); i--) {
      node[i] = op(node[2 * i + 1], node[2 * i + 2]);
    }
  }
  void update(size_t idx, Monoid elem) {
    idx += width - 1;
    node[idx] = elem;
    while (idx >= 1) {
      idx = (idx - 1) / 2;
      node[idx] = op(node[2 * idx + 1], node[2 * idx + 2]);
    }
  }
  Monoid query(size_t a, size_t b, size_t root) const {
    assert(0 <= a && b <= width);
    size_t left = root, right = root;
    while (left < width - 1) {
      left = 2 * left + 1;
    }
    left -= (width - 1);
    while (right < width - 1) {
      right = 2 * right + 2;
    }
    right -= (width - 1);
    right++;
    if (right <= a || b <= left) {
      return unit;
    }
    if (a <= left && right <= b) {
      return node[root];
    }
    Monoid ans_l = query(a, b, 2 * root + 1);
    Monoid ans_r = query(a, b, 2 * root + 2);
    return op(ans_l, ans_r);
  }
};
 
int main() {
    ll N;
    cin >> N;
    struct HeightInfo {
        ll idx;
        ll height;
    };
    vector<HeightInfo> his;
    for (ll i = 0; i < N; i++) {
        ll h;
        cin >> h;
        his.push_back(HeightInfo{i, h});
    }
    vector<ll> ais(N);
    for (ll i = 0; i < N; i++) {
        cin >> ais[i];
    }
    sort(his.begin(), his.end(),
         [](HeightInfo lhs, HeightInfo rhs) { return lhs.height < rhs.height; });
    unordered_map<ll, ll> height_map; // i番目の花が高さが何番めか
    for (ll i = 0; i < N; i++) {
        height_map[his[i].idx] = i;
    }
    vector<ll> dp(N, numeric_limits<ll>::min());
    SegmentTree<ll> tree(dp, [](ll lhs, ll rhs) { return max(lhs, rhs); },
                         numeric_limits<ll>::min());
    for (ll i = 0; i < N; i++) {
        ll max_upto = tree.query(0, height_map[i], 0);
        if (max_upto == numeric_limits<ll>::min()) {
            dp[i] = ais[i];
        } else {
            dp[i] = ais[i] + max_upto;
        }
        tree.update(height_map[i], dp[i]);
    }
    cout << tree.query(0, N, 0) << endl;
}