module IntMap = Map.Make (struct
  type t = int
  let compare = compare
end)

let add a s =
  IntMap.add a (1 + try IntMap.find a s with Not_found -> 0) s
let remove a s =
  match IntMap.find a s with
  | exception Not_found -> s
  | 1 -> IntMap.remove a s
  | n -> IntMap.add a (n - 1) s

let () = Scanf.scanf "%d\n" @@ fun n ->
  let as_ = Array.init n @@ fun _ -> Scanf.scanf "%d\n" @@ fun a -> a in
  Printf.printf "%d\n" @@
  IntMap.fold (fun _ -> ( + ))
    ( Array.fold_left (fun s a ->
        let l, _, _ = IntMap.split a s in
        if IntMap.is_empty l
        then add a s
        else add a @@ remove (fst (IntMap.max_binding l)) s) IntMap.empty as_ ) 0

