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 OCaml (4.02.3) (judge verdict: Accepted)

module SegTree = struct
  module type Monoid = sig
    type t
    val e : t
    val op : t -> t -> t
  end

  module Make (S : Monoid) : sig
    type t
    type elt
    (* n要素の単位元からなるセグ木を作る *)
    val make : int -> t
    (* 
     * update i f t
     * i番目の要素にfを適用したセグ木を作る
     *)
    val update : int -> (elt -> elt) -> t -> t
    (*
     * update_range l r f t
     * [l, r)の要素に自己準同型写像fを適用したセグ木を作る
     *)
    val update_range : int -> int -> (elt -> elt) -> t -> t
    (*
     * query l r t
     * [l, r)の要素の積を求める
     *)
    val query : int -> int -> t -> elt
  end with type elt = S.t = struct
    type elt = S.t
    (* sizeがセグ木の要素数，dataは要素すべてをモノイドの演算で畳み込んだもの *)
    type t = { size : int; data : elt; body : body lazy_t }
    and body = Leaf | Node of t * t

    let make_node left right =
      { size = left.size + right.size;
        data = S.op left.data right.data;
        body = lazy (Node (left, right)) }

    let rec make n = 
      assert (1 <= n);
      { size = n;
        data = S.e;
        body = lazy
          begin match n with
          | 1 -> Leaf
          | n -> Node (make (n / 2), make ((n + 1) / 2))
          end }

    let rec update i f t =
      assert (0 <= i && i < t.size);
      match t with
      | { body = lazy Leaf } -> { t with data = f t.data }
      | { body = lazy (Node (left, right)) } ->
          if i < left.size then make_node (update i f left) right
          else make_node left (update (i - left.size) f right)

    let rec update_range l r f t =
      assert (0 <= l && l < r && r <= t.size);
      match t with
      | { body = lazy Leaf } -> { t with data = f t.data }
      | { body = lazy (Node (left, right)) } ->
          if l <= 0 && t.size <= r then
            { t with
              data = f t.data;
              body = lazy
                (Node
                  (update_range 0 left.size f left,
                   update_range 0 right.size f right)) }
          else if r <= left.size then
            make_node (update_range l r f left) right
          else if left.size <= l then
            make_node left (update_range (l - left.size) (r - left.size) f right)
          else
            make_node
              (update_range l left.size f left)
              (update_range 0 (r - left.size) f right)

    let rec query l r t =
      assert (0 <= l && l < r && r <= t.size);
      match t with
      | { body = lazy Leaf } -> t.data
      | { body = lazy (Node (left, right)) } ->
          if l <= 0 && t.size <= r then t.data
          else if r <= left.size then query l r left
          else if left.size <= l then query (l - left.size) (r - left.size) right
          else S.op (query l left.size left) (query 0 (r - left.size) right)
  end
end

module MaxSegTree = SegTree.Make (struct
  type t = int
  let e = min_int
  let op = max
end)

let () = Scanf.scanf "%d\n" @@ fun n ->
  let hs = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun h -> h in
  let as_ = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun a -> a in
  Printf.printf "%d\n" @@
  MaxSegTree.query 0 (n + 1) @@
  Array.fold_left (fun st (h, a) ->
    MaxSegTree.update h (fun _ ->
      a + MaxSegTree.query 0 h st) st) (MaxSegTree.update 0 (fun _ -> 0) @@ MaxSegTree.make (n + 1)) @@
  Array.init n @@ fun i -> hs.(i), as_.(i)