You are given a tree (a connected undirected graph without cycles) of $$$n$$$ vertices. Each of the $$$n - 1$$$ edges of the tree is colored in either black or red.You are also given an integer $$$k$$$. Consider sequences of $$$k$$$ vertices. Let's call a sequence $$$[a_1, a_2, \ldots, a_k]$$$ good if it satisfies the following criterion:  We will walk a path (possibly visiting same edge/vertex multiple times) on the tree, starting from $$$a_1$$$ and ending at $$$a_k$$$.  Start at $$$a_1$$$, then go to $$$a_2$$$ using the shortest path between $$$a_1$$$ and $$$a_2$$$, then go to $$$a_3$$$ in a similar way, and so on, until you travel the shortest path between $$$a_{k-1}$$$ and $$$a_k$$$. If you walked over at least one black edge during this process, then the sequence is good.   Consider the tree on the picture. If $$$k=3$$$ then the following sequences are good: $$$[1, 4, 7]$$$, $$$[5, 5, 3]$$$ and $$$[2, 3, 7]$$$. The following sequences are not good: $$$[1, 4, 6]$$$, $$$[5, 5, 5]$$$, $$$[3, 7, 3]$$$.There are $$$n^k$$$ sequences of vertices, count how many of them are good. Since this number can be quite large, print it modulo $$$10^9+7$$$.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$2 \le k \le 100$$$), the size of the tree and the length of the vertex sequence. Each of the next $$$n - 1$$$ lines contains three integers $$$u_i$$$, $$$v_i$$$ and $$$x_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$x_i \in \{0, 1\}$$$), where $$$u_i$$$ and $$$v_i$$$ denote the endpoints of the corresponding edge and $$$x_i$$$ is the color of this edge ($$$0$$$ denotes red edge and $$$1$$$ denotes black edge).

Print the number of good sequences modulo $$$10^9 + 7$$$.

NoteIn the first example, all sequences ($$$4^4$$$) of length $$$4$$$ except the following are good:   $$$[1, 1, 1, 1]$$$ $$$[2, 2, 2, 2]$$$ $$$[3, 3, 3, 3]$$$ $$$[4, 4, 4, 4]$$$ In the second example, all edges are red, hence there aren't any good sequences.

open Printf
open Scanf

let (++) = Int64.add
let (--) = Int64.sub
let ( ** ) = Int64.mul
let (//) = Int64.div
let (%%) = Int64.rem
let landl = Int64.logand
let lorl = Int64.logor
let lnotl = Int64.lognot
let lxorl = Int64.logxor
let lsll = Int64.shift_left
let lsrl = Int64.shift_right_logical
let long = Int64.of_int
let succl = Int64.succ
let predl = Int64.pred

let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let read_long _ = bscanf Scanning.stdin " %Ld " (fun x -> x)

let m = 1_000_000_007L

let rec mod_pow a = function
  | 0L -> 1L
  | b -> (if b %% 2L = 0L then 1L else a) ** mod_pow (a ** a %% m) (b//2L) %% m

module UnionFind : sig
  type t
  val create: int -> t
  val find: t -> int -> int
  val unite: t -> int -> int -> unit
  val same: t -> int -> int -> bool
  val size: t -> int -> int
end = struct
  type t = {
    par: int array;
    rank: int array;
    size: int array;
  }

  let create n = {
    par = Array.init n (fun x -> x);
    rank = Array.make n 1;
    size = Array.make n 1;
  }

  let rec find uf a =
    if a = uf.par.(a)
    then a
    else
      let () = uf.par.(a) <- find uf uf.par.(a) in
      uf.par.(a)

  let unite uf a b =
    let a = find uf a in
    let b = find uf b in
    let (a,b) = if a <= b then (a,b) else (b,a) in
    if a != b
    then
      let () = uf.par.(a) <- b in
      let () = uf.size.(b) <- uf.size.(b) + uf.size.(a) in
      if uf.rank.(a) = uf.rank.(b)
      then uf.rank.(b) <- uf.rank.(b) + 1

  let same uf a b = find uf a = find uf b

  let size uf a = uf.size.(find uf a)

end

let read_edge _ = bscanf Scanning.stdin " %d %d %d " (fun u v x -> u-1,v-1,x)

let rec foldi i n accum f =
  if i >= n
  then accum
  else foldi (i+1) n (f accum i) f

let () =
  let n,k = bscanf Scanning.stdin " %d %d " (fun x y -> x,y) in
  let edges = Array.init (n-1) read_edge in
  let open UnionFind in
  let uf = create n in
  Array.iter (fun (u,v,x) -> if x = 0 then unite uf u v) edges;
  let seen = Array.make n false in
  let res = foldi 0 n 0L (fun accum i ->
      let v = find uf i in
      let accum' = if not seen.(v)
        then mod_pow (long @@ size uf v) (long k) ++ accum
        else accum in
      seen.(v) <- true;
      accum' %% m) in
  let n_k = mod_pow (long n) (long k) in
  printf "%Ld\n" @@ (n_k -- res ++ m) %% m

open Printf
open Scanf

let (++) = Int64.add
let (--) = Int64.sub
let ( ** ) = Int64.mul
let (//) = Int64.div
let (%%) = Int64.rem
let landl = Int64.logand
let lorl = Int64.logor
let lnotl = Int64.lognot
let lxorl = Int64.logxor
let lsll = Int64.shift_left
let lsrl = Int64.shift_right_logical
let long = Int64.of_int
let succl = Int64.succ
let predl = Int64.pred

let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let read_long _ = bscanf Scanning.stdin " %Ld " (fun x -> x)

let m = 1_000_000_007L

let rec mod_pow a = function
  | 0L -> 1L
  | b -> (if b %% 2L = 0L then 1L else a) ** mod_pow (a ** a %% m) (b//2L)

module UnionFind : sig
  type t
  val create: int -> t
  val find: t -> int -> int
  val unite: t -> int -> int -> unit
  val same: t -> int -> int -> bool
  val size: t -> int -> int
end = struct
  type t = {
    par: int array;
    rank: int array;
    size: int array;
  }

  let create n = {
    par = Array.init n (fun x -> x);
    rank = Array.make n 1;
    size = Array.make n 1;
  }

  let rec find uf a =
    if a = uf.par.(a)
    then a
    else
      let () = uf.par.(a) <- find uf uf.par.(a) in
      uf.par.(a)

  let unite uf a b =
    let a = find uf a in
    let b = find uf b in
    let (a,b) = if a <= b then (a,b) else (b,a) in
    if a != b
    then
      let () = uf.par.(a) <- b in
      let () = uf.size.(b) <- uf.size.(b) + uf.size.(a) in
      if uf.rank.(a) = uf.rank.(b)
      then uf.rank.(b) <- uf.rank.(b) + 1

  let same uf a b = find uf a = find uf b

  let size uf a = uf.size.(find uf a)

end

let read_edge _ = bscanf Scanning.stdin " %d %d %d " (fun u v x -> u-1,v-1,x)

let rec foldi i n accum f =
  if i >= n
  then accum
  else foldi (i+1) n (f accum i) f

let () =
  let n,k = bscanf Scanning.stdin " %d %d " (fun x y -> x,y) in
  let edges = Array.init (n-1) read_edge in
  let open UnionFind in
  let uf = create n in
  Array.iter (fun (u,v,x) -> if x = 0 then unite uf u v) edges;
  let seen = Array.make n false in
  let res = foldi 0 n 0L (fun accum i ->
      let v = find uf i in
      let accum' = if not seen.(v)
        then mod_pow (long @@ size uf v) (long k) ++ accum
        else accum in
      seen.(v) <- true;
      accum' %% m) in
  let n_k = mod_pow (long n) (long k) in
  printf "%Ld\n" @@ (n_k -- res ++ m) %% m


You are given an array $$$s$$$ consisting of $$$n$$$ integers.You have to find any array $$$t$$$ of length $$$k$$$ such that you can cut out maximum number of copies of array $$$t$$$ from array $$$s$$$.Cutting out the copy of $$$t$$$ means that for each element $$$t_i$$$ of array $$$t$$$ you have to find $$$t_i$$$ in $$$s$$$ and remove it from $$$s$$$. If for some $$$t_i$$$ you cannot find such element in $$$s$$$, then you cannot cut out one more copy of $$$t$$$. The both arrays can contain duplicate elements.For example, if $$$s = [1, 2, 3, 2, 4, 3, 1]$$$ and $$$k = 3$$$ then one of the possible answers is $$$t = [1, 2, 3]$$$. This array $$$t$$$ can be cut out $$$2$$$ times.   To cut out the first copy of $$$t$$$ you can use the elements $$$[1, \underline{\textbf{2}}, 3, 2, 4, \underline{\textbf{3}}, \underline{\textbf{1}}]$$$ (use the highlighted elements). After cutting out the first copy of $$$t$$$ the array $$$s$$$ can look like $$$[1, 3, 2, 4]$$$.  To cut out the second copy of $$$t$$$ you can use the elements $$$[\underline{\textbf{1}}, \underline{\textbf{3}}, \underline{\textbf{2}}, 4]$$$. After cutting out the second copy of $$$t$$$ the array $$$s$$$ will be $$$[4]$$$. Your task is to find such array $$$t$$$ that you can cut out the copy of $$$t$$$ from $$$s$$$ maximum number of times. If there are multiple answers, you may choose any of them.

The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$s$$$ and the desired number of elements in $$$t$$$, respectively. The second line of the input contains exactly $$$n$$$ integers $$$s_1, s_2, \dots, s_n$$$ ($$$1 \le s_i \le 2 \cdot 10^5$$$).

Print $$$k$$$ integers — the elements of array $$$t$$$ such that you can cut out maximum possible number of copies of this array from $$$s$$$. If there are multiple answers, print any of them. The required array $$$t$$$ can contain duplicate elements. All the elements of $$$t$$$ ($$$t_1, t_2, \dots, t_k$$$) should satisfy the following condition: $$$1 \le t_i \le 2 \cdot 10^5$$$.

NoteThe first example is described in the problem statement.In the second example the only answer is $$$[7, 3, 1, 3]$$$ and any its permutations. It can be shown that you cannot choose any other array such that the maximum number of copies you can cut out would be equal to $$$2$$$.In the third example the array $$$t$$$ can be cut out $$$5$$$ times.

(* Problem 2 of round 521*)
(* Comments:

   This is one type of binary_search problem. You will encounter many such
   problems. General binary_search which you might have studied(or will study)
   in the university is done over an array of numbers. In this kind of problems,
   you need to perform the binary_search over the entire solution space.

   For e.g.: In this problem, you can consider the entire solution space as all
   numbers ranging from 1 to n (as you cannot have more than "n" copies).

   So, step 1 of this problem is to find the maximum number of copies which can be
   cut out from the the given array. Let me call this value as val. How we are
   going to find val, I will come back to that later. Let us move to Step 2

*)
open Scanf;;
open Num;;
open Printf;;

(*These are some utility functions*)
module U = struct
  let mprint z =
    print_endline (Num.string_of_num z);;

  let read_one format = Scanf.scanf format (fun x -> x)

  let read_one_number () =
    let rslt= read_one "%d " in
    rslt

  let read_array n a =
    for i = 0 to n-1 do
      let r = read_one_number () in
      a.(i) <- r
    done

end

(*The solutions*)
let nn = 200002;;

let n = U.read_one_number ()
let k = U.read_one_number ()
let a = Array.make n 0
let b = Array.make nn 0
let () = U.read_array n a;;

let () = Array.iter (fun x-> b.(x) <- b.(x) + 1 ) a;;
let c = Array.mapi (fun i x -> (i,x) ) b;;

let my_compare h1 h2 =
  let (_,v1) = h1 in
  let (_,v2) = h2 in
  compare v2 v1;;

let d = c
        |> Array.to_list
        |> List.filter (fun h -> let (_,v) = h in v > 0 )
        |> List.sort (fun h1 h2 -> my_compare h1 h2)
        |> Array.of_list
;;

let dl = Array.length d;;

(* This function determines if cc copies is possible*)
let is_possible cc =
  let flag = true in
  let total = ref 0 in
    for i = 0 to dl -1 do
      let (_,v1) = d.(i) in
      total := !total + v1 / cc
    done;
    if !total < k then
      flag = false
    else
      true
;;

let rec binary_search s e =
  if s == e then
    s
  else if e - s == 1 then
    if is_possible e then
      e
    else
      s
  else
    begin
      let mid = (s+e) / 2 in
      if is_possible mid then
        binary_search mid  e
      else
        binary_search s (mid - 1)
    end
;;

let rslt = binary_search 1 n;;

(*
let () =
  Printf.printf "%B %d\n" (is_possible 2) rslt

let () =
  Array.iter (fun (k1,v1) -> Printf.printf "%d %d\n" k1 v1) d
   *)

let () =
  let nx = ref k in
  for i = 0 to dl -1 do
    let (k1,v1) = d.(i) in
    let m = v1 / rslt in
    if m > 0 && !nx >0 then
      let test = !nx - m in
      if test >=0 then
        begin
          for i = 0 to m-1 do
            Printf.printf "%d " k1
          done;
          nx := test
        end
      else
        begin
          for i = 0 to !nx-1 do
            Printf.printf "%d " k1
          done;
          nx := 0
        end
  done

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  let (+=) r v = r := !r + v
  let (-=) r v = r := !r - v
  let ( *= ) r v = r := !r * v
  let (/=) r v = r := !r / v
  let (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module I64_infix = struct
    let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
      let open Int64 in
      (logand),(logor),(logxor),lognot,
      (fun u v -> shift_left u (i32 v)),
      (fun u v -> shift_right u (i32 v)),
      (fun u v -> shift_right_logical u (i32 v))
  end
  (* input *)
  let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v))
  let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v)
  let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v)
  let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w)
  let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x)
  let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y)
  let i32_get_int _ = Scanf.scanf " %d" (fun v -> v) let i32_get_2_ints _ = Scanf.scanf " %d %d" (fun u v -> u,v) let i32_get_3_ints _ = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w) let i32_get_4_ints _ = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x) let i32_get_5_ints _ = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y)
  (* utils *)
  external id : 'a -> 'a = "%identity"
  let ( *< ) f g x = f (g x)
  let ( *> ) f g x = g (f x)
  let rv f p q = f q p (* ex. 24 |> rv (/) 6 *)
  let alen a = i64 @@ Array.length a
  let llen l = i64 @@  List.length l
  let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
  let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
  let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) []
  let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
  let fail _ = failwith "fail"
  let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
  let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
  let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
  let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
  let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
    let rec f0 ng ok =
      if labs (ok - ng) <= 1L then ok
      else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
    in f0 (ng-1L*d) (ok+1L*d)
  let lower_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >= v)
  let upper_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >  v)
  let equal_range a v = lower_bound a v, upper_bound a v (* #=snd-fst *)
  let rec fix f x = f (fix f) x
  let fix_memo ?(size=10000) f x =
    let tb = Hashtbl.create ~random:true size in
    let rec f0 x =
      try Hashtbl.find tb x with
      | Not_found -> (let v = f f0 x in Hashtbl.add tb x v; v)
    in f0 x
  (* imperative *)
  let rep f t ?(stride=1L) fbod = let i = ref f in while !i <= t do fbod !i; i := !i + stride done
  let repb f t ?(stride=1L) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i + stride done
  let repm f t ?(stride=1L) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + stride done; !m
  let repi f t ?(stride=1) fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ stride done
  let repib f t ?(stride=1) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ stride done
  let repim f t ?(stride=1) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ stride done; !m
  let rep_rev f t ?(stride=1L) fbod = rep t f ~stride @@ fun v -> fbod @@ f + t - v
  let repb_rev f t ?(stride=1L) fbod = repb t f ~stride @@ fun v -> fbod @@ f + t - v
  let repm_rev f t ?(stride=1L) m0 fbod = repm f t ~stride m0 @@ fun u v -> fbod u @@ f + t - v
  let repi_rev f t ?(stride=1) fbod = repi t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repib_rev f t ?(stride=1) fbod = repib t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repim_rev f t ?(stride=1) m0 fbod = repim f t ~stride m0 @@ fun u v -> fbod u @@ f +$ t -$ v
  (* output *)
  let print_list_mle f ls = string_of_list f ls |> print_endline
  let print_array_mle f a = string_of_array f a |> print_endline
  let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
  let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
  (* debug *)
  let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
  let dump_i64_list ls = print_list ist ls
  let dump_i64_array a = print_array ist a
end open Lib open Array
module IMap = Map.Make(Int64)
let appearance a = (* (value,count) *)
  Array.fold_left (fun u v -> IMap.add v (1L + try IMap.find v u with Not_found -> 0L) u) IMap.empty a
let () =
  let n,k = get_2_i64 0 in
  let a = input_i64_array n in
  let ls =
    let mp = appearance a in
    IMap.bindings mp |> List.map (fun (k,v) -> (v,k))
    |> List.sort (fun u v -> compare v u)
  in
  (* print_list (fun (v,k) -> sprintf "%Ld(%Ld) " v k) ls; *)
  let constr d =
    let ans = ref [] in
    List.iter (fun (c,v) ->
      let p = c / d in
      rep 1L p (fun _ -> ans := v::!ans)
    ) ls;
    (* printf "%Ld : %s\n" d (string_of_list ist !ans); *)
    !ans in
  let check d =
    if llen @@ constr d >= k then true
    else false
  in
  let d = binary_search n 0L (fun d -> check d) in
  (* constr d |> List.iter (fun v -> printf "%Ld " v) *)
  constr d |> of_list |> iteri (fun i v ->
    if i=0 then printf "%Ld" v
    else if i<i32 k then printf " %Ld" v
  )

(*This file is to test the environment used by the judge*)

print_int max_int; print_endline;;

(* Problem 2 of round 521*)
(* Comments:
   To make this program is even faster, I need to have in the function 'cut'
   l1 to be an array, use another integer (starting from k) to indicate stop.

   But this is so boring, I better use C.

*)
open Scanf;;
open String;;
open Num;;

(*These are some utility functions*)
module U = struct
  let mprint z =
    print_endline (Num.string_of_num z);;

  let read_one format =
    let n = Scanf.scanf format (fun x -> x) in
    n;;

  let read_one_number () =
    let n = read_one "%d " in
    n

  let read_array n a =
    for i = 0 to n-1 do
      let n = read_one_number () in
      a.(i) <- n
    done

end

(*The solutions*)

let n = U.read_one_number ()
let k = U.read_one_number ()
let a = Array.make n 0
let () = U.read_array n a

module H = Hashtbl
let t = H.create n

(*
let () =
  Array.iter (fun x-> print_int x; print_endline "") a
*)

let my_find s x =
  try Some (H.find s x)
  with
    Not_found -> None
;;

let () = Array.iter (fun x->
        match my_find t x with
          None -> H.add t x 1
        | Some z -> H.replace t x (1+z)
  ) a
;;

let my_compare h1 h2 =
  let (_,v1) = h1 in
  let (_,v2) = h2 in
  if v1 > v2 then 1
  else if v1 == v2 then 0
  else -1;;

let ll = H.fold (fun k v x -> (k,v) :: x) t [];;

if n==k then
  print_int (List.length ll); print_endline "";;

let lr = List.stable_sort my_compare ll
         |> List.rev;;
(*
let () =
  List.iter (fun h ->
      let (k,v) = h in Printf.printf "%d %d\n" k v
    ) lr;;
   *)

let rec cut l1 l2 =
  let n1 = List.length l1 in
  if List.length l2 == k then
    l2
  else
    begin
      if n1 == 1 then
        (List.hd l1) :: l2
      else
        begin
          let h1 :: _ = l1 in
          let h2 :: _ = List.tl l1 in
          let (k1,a1) = h1 in
          let (_,a2) = h2 in
          if a1 < 2*a2 then
            cut (List.tl l1) (h1 :: l2)
          else
            cut ((k1,a1-a1/2) :: (List.tl l1)) ((k1,a1/2) :: l2)
        end
    end
;;

if n==k then
  begin
    print_int (List.length lr); print_endline "";
    List.iter (fun h ->
        let (i,_) = h in Printf.printf "%d " i
      ) lr
  end
else
  begin
    let rslt = cut lr [] in
    if List.length rslt < k then
      begin
        print_int 0; print_endline ""
      end
    else
      List.iter (fun h ->
          let (i,_) = h in Printf.printf "%d " i
        ) rslt;
  end
;;

(* Problem 2 of round 521*)
(* Comments:
   To make this program is even faster, I need to have in the function 'cut'
   l1 to be an array, use another integer (starting from k) to indicate stop.

   But this is so boring, I better use C.

*)
open Scanf;;
open String;;
open Num;;

(*These are some utility functions*)
module U = struct
  let mprint z =
    print_endline (Num.string_of_num z);;

  let read_one format =
    let n = Scanf.scanf format (fun x -> x) in
    n;;

  let read_one_number () =
    let n = read_one "%d " in
    n

  let read_array n a =
    for i = 0 to n-1 do
      let n = read_one_number () in
      a.(i) <- n
    done

end

(*The solutions*)

let n = U.read_one_number ()
let k = U.read_one_number ()
let a = Array.make n 0
let () = U.read_array n a

module H = Hashtbl
let t = H.create n

(*
let () =
  Array.iter (fun x-> print_int x; print_endline "") a
*)

let my_find s x =
  try Some (H.find s x)
  with
    Not_found -> None

let total = ref 0

let () = Array.iter (fun x->
        let y = my_find t x in
        match y with
          None -> H.add t x 1
        | Some z -> H.replace t x (1+z)
  ) a
;;

let my_compare h1 h2 =
  let (_,v1) = h1 in
  let (_,v2) = h2 in
  if v1 > v2 then 1
  else if v1 == v2 then 0
  else -1;;

let ll = H.fold (fun k v x -> (k,v) :: x) t [];;

let lr = List.stable_sort my_compare ll
         |> List.rev;;
(*
let () =
  List.iter (fun h ->
      let (k,v) = h in Printf.printf "%d %d\n" k v
    ) lr;;
   *)

let rec cut l1 l2 =
  let n1 = List.length l1 in
  if List.length l2 == k then
    l2
  else
    begin
      if n1 == 1 then
        (List.hd l1) :: l2
      else
        begin
          let h1 :: _ = l1 in
          let h2 :: _ = List.tl l1 in
          let (k1,a1) = h1 in
          let (_,a2) = h2 in
          if a1 < 2*a2 then
            cut (List.tl l1) (h1 :: l2)
          else
            cut ((k1,a1-a1/2) :: (List.tl l1)) ((k1,a1/2) :: l2)
        end
    end
;;

if n==k then
  List.iter (fun h ->
      let (k,_) = h in Printf.printf "%d " k
    ) lr
else
  begin
    let rslt = cut lr [] in
    if List.length rslt < k then
      begin
        print_int 0; print_endline ""
      end
    else
      List.iter (fun h ->
          let (k,_) = h in Printf.printf "%d " k
        ) rslt;
  end
;;

(* Problem 2 of round 521*)
(* Comments:
   To make this program is even faster, I need to have in the function 'cut'
   l1 to be an array, use another integer (starting from k) to indicate stop.

   But this is so boring, I better use C.

*)
open Scanf;;
open String;;
open Num;;

(*These are some utility functions*)
module U = struct
  let mprint z =
    print_endline (Num.string_of_num z);;

  let read_one format =
    let n = Scanf.scanf format (fun x -> x) in
    n;;

  let read_one_number () =
    let n = read_one "%d " in
    n

  let read_array n a =
    for i = 0 to n-1 do
      let n = read_one_number () in
      a.(i) <- n
    done

end

(*The solutions*)

let n = U.read_one_number ()
let k = U.read_one_number ()
let a = Array.make n 0
let () = U.read_array n a

module H = Hashtbl
let t = H.create n

(*
let () =
  Array.iter (fun x-> print_int x; print_endline "") a
*)

let my_find s x =
  try Some (H.find s x)
  with
    Not_found -> None

let total = ref 0

let () = Array.iter (fun x->
        let y = my_find t x in
        match y with
          None -> H.add t x 1
        | Some z -> H.replace t x (1+z)
  ) a
;;

let my_compare h1 h2 =
  let (_,v1) = h1 in
  let (_,v2) = h2 in
  if v1 > v2 then 1
  else if v1 == v2 then 0
  else -1;;

let ll = H.fold (fun k v x -> (k,v) :: x) t [];;

let lr = List.stable_sort my_compare ll
         |> List.rev;;
(*
let () =
  List.iter (fun h ->
      let (k,v) = h in Printf.printf "%d %d\n" k v
    ) lr;;
   *)

let rec cut l1 l2 =
  let n1 = List.length l1 in
  if List.length l2 == k then
    l2
  else
    begin
      if n1 == 1 then
        (List.hd l1) :: l2
      else
        begin
          let h1 :: _ = l1 in
          let h2 :: _ = List.tl l1 in
          let (k1,a1) = h1 in
          let (_,a2) = h2 in
          if a1 < 2*a2 then
            cut (List.tl l1) (h1 :: l2)
          else
            cut ((k1,a1-a1/2) :: (List.tl l1)) ((k1,a1/2) :: l2)
        end
    end
;;

if n==k then
  begin
    print_int (List.length lr); print_endline "";
    List.iter (fun h ->
        let (i,_) = h in Printf.printf "%d " i
      ) lr
  end
else
  begin
    let rslt = cut lr [] in
    if List.length rslt < k then
      begin
        print_int 0; print_endline ""
      end
    else
      List.iter (fun h ->
          let (i,_) = h in Printf.printf "%d " i
        ) rslt;
  end
;;

(* Problem 2 of round 521*)
(* Comments:
   To make this program is even faster, I need to have in the function 'cut'
   l1 to be an array, use another integer (starting from k) to indicate stop.

   But this is so boring, I better use C.

*)
open Scanf;;
open String;;
open Num;;

(*These are some utility functions*)
module U = struct
  let mprint z =
    print_endline (Num.string_of_num z);;

  let read_one format =
    let n = Scanf.scanf format (fun x -> x) in
    n;;

  let read_one_number () =
    let n = read_one "%d " in
    n

  let read_array n a =
    for i = 0 to n-1 do
      let n = read_one_number () in
      a.(i) <- n
    done

end

(*The solutions*)

let n = U.read_one_number ()
let k = U.read_one_number ()
let a = Array.make n 0
let () = U.read_array n a

module H = Hashtbl
let t = H.create n

(*
let () =
  Array.iter (fun x-> print_int x; print_endline "") a
*)

let my_find s x =
  try Some (H.find s x)
  with
    Not_found -> None

let total = ref 0

let () = Array.iter (fun x->
        let y = my_find t x in
        match y with
          None -> H.add t x 1
        | Some z -> H.replace t x (1+z)
  ) a
;;

let my_compare h1 h2 =
  let (_,v1) = h1 in
  let (_,v2) = h2 in
  if v1 > v2 then 1
  else if v1 == v2 then 0
  else -1;;

let ll = H.fold (fun k v x -> (k,v) :: x) t [];;

let lr = List.stable_sort my_compare ll
         |> List.rev;;
(*
let () =
  List.iter (fun h ->
      let (k,v) = h in Printf.printf "%d %d\n" k v
    ) lr;;
   *)

let rec cut l1 l2 =
  let n1 = List.length l1 in
  if List.length l2 == k then
    l2
  else
    begin
      if n1 == 1 then
        (List.hd l1) :: l2
      else
        begin
          let h1 :: _ = l1 in
          let h2 :: _ = List.tl l1 in
          let (k1,a1) = h1 in
          let (_,a2) = h2 in
          if a1 < 2*a2 then
            cut (List.tl l1) (h1 :: l2)
          else
            cut ((k1,a1-a1/2) :: (List.tl l1)) ((k1,a1/2) :: l2)
        end
    end
;;

if n==k then
  List.iter (fun h ->
      let (i,_) = h in Printf.printf "%d " i
    ) lr
else
  begin
    let rslt = cut lr [] in
    if List.length rslt < k then
      begin
        print_int 0; print_endline ""
      end
    else
      List.iter (fun h ->
          let (i,_) = h in Printf.printf "%d " i
        ) rslt;
  end
;;

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  let (+=) r v = r := !r + v
  let (-=) r v = r := !r - v
  let ( *= ) r v = r := !r * v
  let (/=) r v = r := !r / v
  let (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module I64_infix = struct
    let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
      let open Int64 in
      (logand),(logor),(logxor),lognot,
      (fun u v -> shift_left u (i32 v)),
      (fun u v -> shift_right u (i32 v)),
      (fun u v -> shift_right_logical u (i32 v))
  end
  (* input *)
  let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v))
  let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v)
  let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v)
  let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w)
  let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x)
  let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y)
  let i32_get_int _ = Scanf.scanf " %d" (fun v -> v) let i32_get_2_ints _ = Scanf.scanf " %d %d" (fun u v -> u,v) let i32_get_3_ints _ = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w) let i32_get_4_ints _ = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x) let i32_get_5_ints _ = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y)
  (* utils *)
  external id : 'a -> 'a = "%identity"
  let ( *< ) f g x = f (g x)
  let ( *> ) f g x = g (f x)
  let rv f p q = f q p (* ex. 24 |> rv (/) 6 *)
  let alen a = i64 @@ Array.length a
  let llen l = i64 @@  List.length l
  let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
  let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
  let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) []
  let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
  let fail _ = failwith "fail"
  let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
  let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
  let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
  let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
  let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
    let rec f0 ng ok =
      if labs (ok - ng) <= 1L then ok
      else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
    in f0 (ng-1L*d) (ok+1L*d)
  let lower_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >= v)
  let upper_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >  v)
  let equal_range a v = lower_bound a v, upper_bound a v (* #=snd-fst *)
  let rec fix f x = f (fix f) x
  let fix_memo ?(size=10000) f x =
    let tb = Hashtbl.create ~random:true size in
    let rec f0 x =
      try Hashtbl.find tb x with
      | Not_found -> (let v = f f0 x in Hashtbl.add tb x v; v)
    in f0 x
  (* imperative *)
  let rep f t ?(stride=1L) fbod = let i = ref f in while !i <= t do fbod !i; i := !i + stride done
  let repb f t ?(stride=1L) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i + stride done
  let repm f t ?(stride=1L) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + stride done; !m
  let repi f t ?(stride=1) fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ stride done
  let repib f t ?(stride=1) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ stride done
  let repim f t ?(stride=1) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ stride done; !m
  let rep_rev f t ?(stride=1L) fbod = rep t f ~stride @@ fun v -> fbod @@ f + t - v
  let repb_rev f t ?(stride=1L) fbod = repb t f ~stride @@ fun v -> fbod @@ f + t - v
  let repm_rev f t ?(stride=1L) m0 fbod = repm f t ~stride m0 @@ fun u v -> fbod u @@ f + t - v
  let repi_rev f t ?(stride=1) fbod = repi t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repib_rev f t ?(stride=1) fbod = repib t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repim_rev f t ?(stride=1) m0 fbod = repim f t ~stride m0 @@ fun u v -> fbod u @@ f +$ t -$ v
  (* output *)
  let print_list_mle f ls = string_of_list f ls |> print_endline
  let print_array_mle f a = string_of_array f a |> print_endline
  let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
  let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
  (* debug *)
  let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
  let dump_i64_list ls = print_list ist ls
  let dump_i64_array a = print_array ist a
end open Lib open Array
module IMap = Map.Make(Int64)
let appearance a = (* (value,count) *)
  Array.fold_left (fun u v -> IMap.add v (1L + try IMap.find v u with Not_found -> 0L) u) IMap.empty a
let () =
  let n,k = get_2_i64 0 in
  let a = input_i64_array n in
  let ls =
    let mp = appearance a in
    IMap.bindings mp |> List.map (fun (k,v) -> (v,k))
    |> List.sort (fun u v -> compare v u)
  in
  (* print_list (fun (v,k) -> sprintf "%Ld(%Ld) " v k) ls; *)
  let constr d =
    let ans = ref [] in
    List.iter (fun (c,v) ->
      let p = c / d in
      rep 1L p (fun _ -> ans := v::!ans)
    ) ls;
    (* printf "%Ld : %s\n" d (string_of_list ist !ans); *)
    !ans in
  let check d =
    if llen @@ constr d >= k then true
    else false
  in
  let d = binary_search n 0L (fun d -> check d) in
  constr d |> print_list ist

  (* let r = make 20010 [] in
  IMap.iter (fun p q ->
    (* if q=k then r := (p+1L)::!r *)
    (* arr.(i32 q) <- p::arr.(i32 q) *)
    r.(i32 q) <- p::r.(i32 q)
  ) mp;
  let ls = ref [] in
  iteri (fun c v -> if List.length v<>0 then ls:=(c,v)::!ls) r;
  (* let r = List.sort (fun u v -> -compare u v) !r in *)
  let ls = !ls |> List.sort (fun u v -> -compare u v) in
  let d = binary_search n 0L (fun d ->
    (* printf "%Ld\n" d; *)
    let l = List.map (fun (c,v) -> ((i64 c)/d,v)) ls |> List.sort (fun u v -> compare v u) in
    let _,f = List.fold_left (fun (sum,f) (v,ls) ->
      let ne = sum + v * (llen ls) in
      (* printf "%Ld %Ld\n" v ne; *)
      if ne >= k then (ne,true) else (ne,f)
    ) (0L,false) l
    in f
  ) in
  let l = List.map (fun (c,v) -> ((i64 c)/d,v)) ls |> List.sort (fun u v -> compare v u) in
  let rest = ref k in
  let _,f = List.fold_left (fun (sum,f) (v,ls) ->
    let ne = (v * llen ls) in
    if ne <= !rest then (
      rep 0L (ne-1L) (fun i ->
        let b = init (i32 v) (fun _ -> ls) |> to_list |> List.flatten |> of_list
        in printf "%Ld " b.(i32 i)
      );
      rest -= ne; (sum,f)
    ) else (
      let b = init (i32 v) (fun _ -> ls) |> to_list |> List.flatten |> of_list in
      rep 0L (!rest) (fun i ->
        printf "%Ld " b.(i32 i)
      ); (sum,f)
    )
    (* if  >= k then (ne,true) else (ne,f) *)
  ) (0L,false) l
  in
  () *)

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  let (+=) r v = r := !r + v
  let (-=) r v = r := !r - v
  let ( *= ) r v = r := !r * v
  let (/=) r v = r := !r / v
  let (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module I64_infix = struct
    let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
      let open Int64 in
      (logand),(logor),(logxor),lognot,
      (fun u v -> shift_left u (i32 v)),
      (fun u v -> shift_right u (i32 v)),
      (fun u v -> shift_right_logical u (i32 v))
  end
  (* input *)
  let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v))
  let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v)
  let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v)
  let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w)
  let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x)
  let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y)
  let i32_get_int _ = Scanf.scanf " %d" (fun v -> v) let i32_get_2_ints _ = Scanf.scanf " %d %d" (fun u v -> u,v) let i32_get_3_ints _ = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w) let i32_get_4_ints _ = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x) let i32_get_5_ints _ = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y)
  (* utils *)
  external id : 'a -> 'a = "%identity"
  let ( *< ) f g x = f (g x)
  let ( *> ) f g x = g (f x)
  let rv f p q = f q p (* ex. 24 |> rv (/) 6 *)
  let alen a = i64 @@ Array.length a
  let llen l = i64 @@  List.length l
  let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
  let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
  let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) []
  let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
  let fail _ = failwith "fail"
  let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
  let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
  let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
  let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
  let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
    let rec f0 ng ok =
      if labs (ok - ng) <= 1L then ok
      else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
    in f0 (ng-1L*d) (ok+1L*d)
  let lower_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >= v)
  let upper_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >  v)
  let equal_range a v = lower_bound a v, upper_bound a v (* #=snd-fst *)
  let rec fix f x = f (fix f) x
  let fix_memo ?(size=10000) f x =
    let tb = Hashtbl.create ~random:true size in
    let rec f0 x =
      try Hashtbl.find tb x with
      | Not_found -> (let v = f f0 x in Hashtbl.add tb x v; v)
    in f0 x
  (* imperative *)
  let rep f t ?(stride=1L) fbod = let i = ref f in while !i <= t do fbod !i; i := !i + stride done
  let repb f t ?(stride=1L) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i + stride done
  let repm f t ?(stride=1L) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + stride done; !m
  let repi f t ?(stride=1) fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ stride done
  let repib f t ?(stride=1) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ stride done
  let repim f t ?(stride=1) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ stride done; !m
  let rep_rev f t ?(stride=1L) fbod = rep t f ~stride @@ fun v -> fbod @@ f + t - v
  let repb_rev f t ?(stride=1L) fbod = repb t f ~stride @@ fun v -> fbod @@ f + t - v
  let repm_rev f t ?(stride=1L) m0 fbod = repm f t ~stride m0 @@ fun u v -> fbod u @@ f + t - v
  let repi_rev f t ?(stride=1) fbod = repi t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repib_rev f t ?(stride=1) fbod = repib t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repim_rev f t ?(stride=1) m0 fbod = repim f t ~stride m0 @@ fun u v -> fbod u @@ f +$ t -$ v
  (* output *)
  let print_list_mle f ls = string_of_list f ls |> print_endline
  let print_array_mle f a = string_of_array f a |> print_endline
  let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
  let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
  (* debug *)
  let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
  let dump_i64_list ls = print_list ist ls
  let dump_i64_array a = print_array ist a
end open Lib open Array
module IMap = Map.Make(Int64)
let appearance a = (* (value,count) *)
  Array.fold_left (fun u v -> IMap.add v (1L + try IMap.find v u with Not_found -> 0L) u) IMap.empty a
let () =
  let n,k = get_2_i64 0 in
  let a = input_i64_array n in
  let ls =
    let mp = appearance a in
    IMap.bindings mp |> List.map (fun (k,v) -> (v,k))
    |> List.sort (fun u v -> compare v u)
  in
  (* print_list (fun (v,k) -> sprintf "%Ld(%Ld) " v k) ls; *)
  let constr d =
    let ans = ref [] in
    List.iter (fun (c,v) ->
      let p = c / d in
      rep 1L p (fun _ -> ans := v::!ans)
    ) ls;
    (* printf "%Ld : %s\n" d (string_of_list ist !ans); *)
    !ans in
  let check d =
    if llen @@ constr d >= k then true
    else false
  in
  let d = binary_search n 0L (fun d -> check d) in
  constr d |> List.iter (fun v -> printf "%Ld " v)

  (* let r = make 20010 [] in
  IMap.iter (fun p q ->
    (* if q=k then r := (p+1L)::!r *)
    (* arr.(i32 q) <- p::arr.(i32 q) *)
    r.(i32 q) <- p::r.(i32 q)
  ) mp;
  let ls = ref [] in
  iteri (fun c v -> if List.length v<>0 then ls:=(c,v)::!ls) r;
  (* let r = List.sort (fun u v -> -compare u v) !r in *)
  let ls = !ls |> List.sort (fun u v -> -compare u v) in
  let d = binary_search n 0L (fun d ->
    (* printf "%Ld\n" d; *)
    let l = List.map (fun (c,v) -> ((i64 c)/d,v)) ls |> List.sort (fun u v -> compare v u) in
    let _,f = List.fold_left (fun (sum,f) (v,ls) ->
      let ne = sum + v * (llen ls) in
      (* printf "%Ld %Ld\n" v ne; *)
      if ne >= k then (ne,true) else (ne,f)
    ) (0L,false) l
    in f
  ) in
  let l = List.map (fun (c,v) -> ((i64 c)/d,v)) ls |> List.sort (fun u v -> compare v u) in
  let rest = ref k in
  let _,f = List.fold_left (fun (sum,f) (v,ls) ->
    let ne = (v * llen ls) in
    if ne <= !rest then (
      rep 0L (ne-1L) (fun i ->
        let b = init (i32 v) (fun _ -> ls) |> to_list |> List.flatten |> of_list
        in printf "%Ld " b.(i32 i)
      );
      rest -= ne; (sum,f)
    ) else (
      let b = init (i32 v) (fun _ -> ls) |> to_list |> List.flatten |> of_list in
      rep 0L (!rest) (fun i ->
        printf "%Ld " b.(i32 i)
      ); (sum,f)
    )
    (* if  >= k then (ne,true) else (ne,f) *)
  ) (0L,false) l
  in
  () *)

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  let (+=) r v = r := !r + v
  let (-=) r v = r := !r - v
  let ( *= ) r v = r := !r * v
  let (/=) r v = r := !r / v
  let (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module I64_infix = struct
    let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
      let open Int64 in
      (logand),(logor),(logxor),lognot,
      (fun u v -> shift_left u (i32 v)),
      (fun u v -> shift_right u (i32 v)),
      (fun u v -> shift_right_logical u (i32 v))
  end
  (* input *)
  let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v))
  let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v)
  let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v)
  let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w)
  let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x)
  let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y)
  let i32_get_int _ = Scanf.scanf " %d" (fun v -> v) let i32_get_2_ints _ = Scanf.scanf " %d %d" (fun u v -> u,v) let i32_get_3_ints _ = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w) let i32_get_4_ints _ = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x) let i32_get_5_ints _ = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y)
  (* utils *)
  external id : 'a -> 'a = "%identity"
  let ( *< ) f g x = f (g x)
  let ( *> ) f g x = g (f x)
  let rv f p q = f q p (* ex. 24 |> rv (/) 6 *)
  let alen a = i64 @@ Array.length a
  let llen l = i64 @@  List.length l
  let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
  let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
  let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) []
  let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
  let fail _ = failwith "fail"
  let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
  let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
  let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
  let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
  let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
    let rec f0 ng ok =
      if labs (ok - ng) <= 1L then ok
      else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
    in f0 (ng-1L*d) (ok+1L*d)
  let lower_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >= v)
  let upper_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >  v)
  let equal_range a v = lower_bound a v, upper_bound a v (* #=snd-fst *)
  let rec fix f x = f (fix f) x
  let fix_memo ?(size=10000) f x =
    let tb = Hashtbl.create ~random:true size in
    let rec f0 x =
      try Hashtbl.find tb x with
      | Not_found -> (let v = f f0 x in Hashtbl.add tb x v; v)
    in f0 x
  (* imperative *)
  let rep f t ?(stride=1L) fbod = let i = ref f in while !i <= t do fbod !i; i := !i + stride done
  let repb f t ?(stride=1L) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i + stride done
  let repm f t ?(stride=1L) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + stride done; !m
  let repi f t ?(stride=1) fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ stride done
  let repib f t ?(stride=1) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ stride done
  let repim f t ?(stride=1) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ stride done; !m
  let rep_rev f t ?(stride=1L) fbod = rep t f ~stride @@ fun v -> fbod @@ f + t - v
  let repb_rev f t ?(stride=1L) fbod = repb t f ~stride @@ fun v -> fbod @@ f + t - v
  let repm_rev f t ?(stride=1L) m0 fbod = repm f t ~stride m0 @@ fun u v -> fbod u @@ f + t - v
  let repi_rev f t ?(stride=1) fbod = repi t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repib_rev f t ?(stride=1) fbod = repib t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repim_rev f t ?(stride=1) m0 fbod = repim f t ~stride m0 @@ fun u v -> fbod u @@ f +$ t -$ v
  (* output *)
  let print_list_mle f ls = string_of_list f ls |> print_endline
  let print_array_mle f a = string_of_array f a |> print_endline
  let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
  let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
  (* debug *)
  let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
  let dump_i64_list ls = print_list ist ls
  let dump_i64_array a = print_array ist a
end open Lib open Array
module IMap = Map.Make(Int64)
let appearance a = (* (value,count) *)
  Array.fold_left (fun u v -> IMap.add v (1L + try IMap.find v u with Not_found -> 0L) u) IMap.empty a
let () =
  let n,k = get_2_i64 0 in
  let a = input_i64_array n in
  let ls =
    let mp = appearance a in
    IMap.bindings mp |> List.map (fun (k,v) -> (v,k))
    |> List.sort (fun u v -> compare v u)
  in
  (* print_list (fun (v,k) -> sprintf "%Ld(%Ld) " v k) ls; *)
  let constr d =
    let ans = ref [] in
    List.iter (fun (c,v) ->
      let p = c / d in
      rep 1L p (fun _ -> ans := v::!ans)
    ) ls;
    (* printf "%Ld : %s\n" d (string_of_list ist !ans); *)
    !ans in
  let check d =
    if llen @@ constr d >= k then true
    else false
  in
  let d = binary_search n 0L (fun d -> check d) in
  (* constr d |> List.iter (fun v -> printf "%Ld " v) *)
  constr d |> of_list |> iteri (fun i v ->
    (if i=0 then printf "%Ld"
    else printf " %Ld") v
  )

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  let (+=) r v = r := !r + v
  let (-=) r v = r := !r - v
  let ( *= ) r v = r := !r * v
  let (/=) r v = r := !r / v
  let (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module I64_infix = struct
    let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
      let open Int64 in
      (logand),(logor),(logxor),lognot,
      (fun u v -> shift_left u (i32 v)),
      (fun u v -> shift_right u (i32 v)),
      (fun u v -> shift_right_logical u (i32 v))
  end
  (* input *)
  let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v))
  let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v)
  let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v)
  let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w)
  let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x)
  let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y)
  let i32_get_int _ = Scanf.scanf " %d" (fun v -> v) let i32_get_2_ints _ = Scanf.scanf " %d %d" (fun u v -> u,v) let i32_get_3_ints _ = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w) let i32_get_4_ints _ = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x) let i32_get_5_ints _ = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y)
  (* utils *)
  external id : 'a -> 'a = "%identity"
  let ( *< ) f g x = f (g x)
  let ( *> ) f g x = g (f x)
  let rv f p q = f q p (* ex. 24 |> rv (/) 6 *)
  let alen a = i64 @@ Array.length a
  let llen l = i64 @@  List.length l
  let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
  let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
  let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) []
  let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
  let fail _ = failwith "fail"
  let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
  let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
  let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
  let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
  let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
    let rec f0 ng ok =
      if labs (ok - ng) <= 1L then ok
      else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
    in f0 (ng-1L*d) (ok+1L*d)
  let lower_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >= v)
  let upper_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >  v)
  let equal_range a v = lower_bound a v, upper_bound a v (* #=snd-fst *)
  let rec fix f x = f (fix f) x
  let fix_memo ?(size=10000) f x =
    let tb = Hashtbl.create ~random:true size in
    let rec f0 x =
      try Hashtbl.find tb x with
      | Not_found -> (let v = f f0 x in Hashtbl.add tb x v; v)
    in f0 x
  (* imperative *)
  let rep f t ?(stride=1L) fbod = let i = ref f in while !i <= t do fbod !i; i := !i + stride done
  let repb f t ?(stride=1L) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i + stride done
  let repm f t ?(stride=1L) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + stride done; !m
  let repi f t ?(stride=1) fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ stride done
  let repib f t ?(stride=1) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ stride done
  let repim f t ?(stride=1) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ stride done; !m
  let rep_rev f t ?(stride=1L) fbod = rep t f ~stride @@ fun v -> fbod @@ f + t - v
  let repb_rev f t ?(stride=1L) fbod = repb t f ~stride @@ fun v -> fbod @@ f + t - v
  let repm_rev f t ?(stride=1L) m0 fbod = repm f t ~stride m0 @@ fun u v -> fbod u @@ f + t - v
  let repi_rev f t ?(stride=1) fbod = repi t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repib_rev f t ?(stride=1) fbod = repib t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repim_rev f t ?(stride=1) m0 fbod = repim f t ~stride m0 @@ fun u v -> fbod u @@ f +$ t -$ v
  (* output *)
  let print_list_mle f ls = string_of_list f ls |> print_endline
  let print_array_mle f a = string_of_array f a |> print_endline
  let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
  let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
  (* debug *)
  let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
  let dump_i64_list ls = print_list ist ls
  let dump_i64_array a = print_array ist a
end open Lib open Array
module IMap = Map.Make(Int64)
let arr = make 50 []
let appearance a = (* (value,count) *)
  let open IMap in Array.fold_left (fun u v -> IMap.add v (1L + try IMap.find v u with Not_found -> 0L) u) IMap.empty a
let () =
  let n,k = get_2_i64 0 in
  let a = input_i64_array n in
  let mp = appearance a in
  let r = ref [] in
  IMap.iter (fun p q ->
    (* if q=k then r := (p+1L)::!r *)
    arr.(i32 q) <- p::arr.(i32 q)
  ) mp;
  let n_max = IMap.fold (fun _ v u -> max v u) mp 0L in
  (* iteri (fun i r ->
    if List.length r<>0 then
    printf "%d : %s\n" i (string_of_list ist r);
  ) arr; *)
  let amari = ref 0 in
  (* let r_amari = ref [] in *)
  rep_rev n_max 1L (fun p ->
    let p = i32 p in
    if !amari +$ List.length arr.(p) >= i32 k then (
      (* print_list ist arr.(i32 p); *)
      if List.length arr.(p) >= i32 k then (
        let a = arr.(p) |> of_list in
        repi 0 (p) (fun i -> printf "%Ld " a.(i));
        exit 0;
      ) else (
        let rest = ref @@ i32 k -$ (List.length arr.(p)) in
        let a = arr.(p) |> of_list in
        repi 0 (length a-$1) (fun i -> printf "%Ld " a.(i));
        let q,res = ref @@ p+$1,ref [] in
        while !rest > 0 do
          (* printf "%d:\n  " !rest;
          print_list ist !res; *)
          match arr.(!q) with
          | [] -> (q := !q +$ 1)
          | ls -> (
            (* let b = arr.(!q) |> of_list in *)
            let v = List.length arr.(!q) in
            let lls = make !q 0L |> to_list |> List.map (fun _ -> ls) |> List.flatten
            |> of_list
            in
            (* print_array ist lls; *)
            let r = ref 0 in
            while !rest > 0 && !r < length lls do
              res := lls.(!r) :: !res;
              r := !r +$ 1;
              rest := !rest -$ 1
            done;
            if !rest = 0 then (
              print_list ist !res; exit 0
            ) else (q := !q +$ 1)
          )
        done

      )
    ) else (
      let q = List.length arr.(p) in
      amari := !amari +$ q *$ p;
      (* r_amari := ()::!r_amari *)
    )
  );
  (* print_array (string_of_list ist) arr; *)
  (* print_list ist !r *)


Polycarp is sad — New Year is coming in few days but there is still no snow in his city. To bring himself New Year mood, he decided to decorate his house with some garlands.The local store introduced a new service this year, called "Build your own garland". So you can buy some red, green and blue lamps, provide them and the store workers will solder a single garland of them. The resulting garland will have all the lamps you provided put in a line. Moreover, no pair of lamps of the same color will be adjacent to each other in this garland!For example, if you provide $$$3$$$ red, $$$3$$$ green and $$$3$$$ blue lamps, the resulting garland can look like this: "RGBRBGBGR" ("RGB" being the red, green and blue color, respectively). Note that it's ok to have lamps of the same color on the ends of the garland.However, if you provide, say, $$$1$$$ red, $$$10$$$ green and $$$2$$$ blue lamps then the store workers won't be able to build any garland of them. Any garland consisting of these lamps will have at least one pair of lamps of the same color adjacent to each other. Note that the store workers should use all the lamps you provided.So Polycarp has bought some sets of lamps and now he wants to know if the store workers can build a garland from each of them.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of sets of lamps Polycarp has bought. Each of the next $$$t$$$ lines contains three integers $$$r$$$, $$$g$$$ and $$$b$$$ ($$$1 \le r, g, b \le 10^9$$$) — the number of red, green and blue lamps in the set, respectively.

Print $$$t$$$ lines — for each set of lamps print "Yes" if the store workers can build a garland from them and "No" otherwise.

NoteThe first two sets are desribed in the statement.The third set produces garland "RBRG", for example.

let rec read_lines n =
  match n with
  | 0 -> []
  | _ -> let res = read_line () in
    res :: read_lines (n - 1)

let fuck str b ch =
  try
    Some (String.index_from str b ch)
  with _ ->
    None

let split str ch =
  let rec split' b e =
    if b >= e
    then
      []
    else
      match (fuck str b ch) with
      | None -> let res = String.sub str b (e - b) |> int_of_string in [res]
      | Some(dest) ->
        let res = String.sub str b (dest - b) |> int_of_string in
        res :: split' (dest + 1) e
  in
  let res = split' 0 (String.length str) in
  res
;;

let () =
  let n = read_int () in
  let rec loop num =
    if num = 0
    then
      ()
    else
      let line = read_line () in
      Scanf.sscanf line "%Ld %Ld %Ld" (fun a b c ->
          let open Int64 in
          let ls' = List.sort (fun a b -> compare a b) [a;b;c] in
          let n1 = List.nth ls' 0 in
          let n2 = List.nth ls' 1 in
          let n3 = List.nth ls' 2 in
          if (compare (add (add n2 n1) 1L) n3) >= 0 then Printf.printf "Yes\n" else Printf.printf "No\n"
        )
      ;
      num - 1 |> loop
  in
  loop n
;;

let rec read_lines n =
  match n with
  | 0 -> []
  | _ -> let res = read_line () in
    res :: read_lines (n - 1)

let fuck str b ch =
  try
    Some (String.index_from str b ch)
  with _ ->
    None

let split str ch =
  let rec split' b e =
    if b >= e
    then
      []
    else
      match (fuck str b ch) with
      | None -> let res = String.sub str b (e - b) |> int_of_string in [res]
      | Some(dest) ->
        let res = String.sub str b (dest - b) |> int_of_string in
        res :: split' (dest + 1) e
  in
  let res = split' 0 (String.length str) in
  res
;;

let () =
  let n = read_int () in
  let rec loop num =
    if num = 0
    then
      ()
    else
      let line = read_line () in
      Scanf.sscanf line "%Ld %Ld %Ld" (fun a b c ->
          let open Int64 in
          let ls' = List.sort (fun a b -> compare a b) [a;b;c] in
          let n1 = List.nth ls' 0 in
          let n2 = List.nth ls' 1 in
          let n3 = List.nth ls' 2 in
          if (compare (add (add n3 n1) 1L) n2) > 0 then Printf.printf "Yes\n" else Printf.printf "No\n"
        );
in
loop n
;;

let rec read_lines n =
  match n with
  | 0 -> []
  | _ -> let res = read_line () in
    res :: read_lines (n - 1)

let fuck str b ch =
  try
    Some (String.index_from str b ch)
  with _ ->
    None

let split str ch =
  let rec split' b e =
    if b >= e
    then
      []
    else
      match (fuck str b ch) with
      | None -> let res = String.sub str b (e - b) |> int_of_string in [res]
      | Some(dest) ->
        let res = String.sub str b (dest - b) |> int_of_string in
        res :: split' (dest + 1) e
  in
  let res = split' 0 (String.length str) in
  res
;;

let () =
  let n = read_int () in
  let rec loop num =
    if num = 0
    then
      ()
    else
      let line = read_line () in
      Scanf.sscanf line "%Ld %Ld %Ld" (fun a b c ->
          let open Int64 in
          let comp n1 n2 n3 =
            (compare (add n1 n2) n3) < 0
          in let eq n1 n2 =
               (compare n1 n2) = 0
          in let relate n1 n2 =
               (compare (div n1 n2) 1L) = 0 || (compare (div n2 n1) 1L) = 0 ||
               (eq n1 1L && eq n2 2L) || (eq n1 2L && eq n2 1L)
          in
          if comp a b c || comp a c b || comp b c a || not (relate a b && relate b c)
          then
            Printf.printf "No\n"
          else
            Printf.printf "Yes\n"
        );
    loop (num - 1)
  in
  loop n
;;

let rec read_lines n =
  match n with
  | 0 -> []
  | _ -> let res = read_line () in
    res :: read_lines (n - 1)

let fuck str b ch =
  try
    Some (String.index_from str b ch)
  with _ ->
    None

let split str ch =
  let rec split' b e =
    if b >= e
    then
      []
    else
      match (fuck str b ch) with
      | None -> let res = String.sub str b (e - b) |> int_of_string in [res]
      | Some(dest) ->
        let res = String.sub str b (dest - b) |> int_of_string in
        res :: split' (dest + 1) e
  in
  let res = split' 0 (String.length str) in
  res
;;

let () =
  let n = read_int () in
  let rec loop num =
    if num = 0
    then
      ()
    else
      let line = read_line () in
      Scanf.sscanf line "%Ld %Ld %Ld" (fun a b c ->
          let open Int64 in
          let ls' = List.sort (fun a b -> compare a b) [a;b;c] in
          let n1 = List.nth ls' 0 in
          let n2 = List.nth ls' 1 in
          let n3 = List.nth ls' 2 in
          if (compare (add (add n3 n1) 1L) n2) >= 0 then Printf.printf "Yes\n" else Printf.printf "No\n"
        );
in
loop n
;;

let rec read_lines n =
  match n with
  | 0 -> []
  | _ -> let res = read_line () in
    res :: read_lines (n - 1)

let fuck str b ch =
  try
    Some (String.index_from str b ch)
  with _ ->
    None

let split str ch =
  let rec split' b e =
    if b >= e
    then
      []
    else
      match (fuck str b ch) with
      | None -> let res = String.sub str b (e - b) |> int_of_string in [res]
      | Some(dest) ->
        let res = String.sub str b (dest - b) |> int_of_string in
        res :: split' (dest + 1) e
  in
  let res = split' 0 (String.length str) in
  res
;;

let () =
  let n = read_int () in
  let rec loop num =
    if num = 0
    then
      ()
    else
      Scanf.scanf "%Ld %Ld %Ld" (fun a b c ->
          let open Int64 in
          let comp n1 n2 n3 =
            (compare (add n1 n2) n3) < 0
          in
          if comp a b c || comp a c b || comp b c a
          then
            Printf.printf "No\n"
          else
            Printf.printf "Yes\n"
        )
  in
  loop n
;;

let rec read_lines n =
  match n with
  | 0 -> []
  | _ -> let res = read_line () in
    res :: read_lines (n - 1)

let fuck str b ch =
  try
    Some (String.index_from str b ch)
  with _ ->
    None

let split str ch =
  let rec split' b e =
    if b >= e
    then
      []
    else
      match (fuck str b ch) with
      | None -> let res = String.sub str b (e - b) |> int_of_string in [res]
      | Some(dest) ->
        let res = String.sub str b (dest - b) |> int_of_string in
        res :: split' (dest + 1) e
  in
  let res = split' 0 (String.length str) in
  res
;;

let () =
  try
  let n = read_int () in
  let rec loop num =
    if num = 0
    then
      ()
    else
      let line = read_line () in
      Scanf.sscanf line "%Ld %Ld %Ld" (fun a b c ->
          let open Int64 in
          let comp n1 n2 n3 =
            (compare (add n1 n2) n3) < 0
          in
          if comp a b c || comp a c b || comp b c a
          then
            Printf.printf "No\n"
          else
            Printf.printf "Yes\n"
        );
    loop (n - 1)
  in
  loop n
  with End_of_file ->
    ()

;;

let rec read_lines n =
  match n with
  | 0 -> []
  | _ -> let res = read_line () in
    res :: read_lines (n - 1)

let fuck str b ch =
  try
    Some (String.index_from str b ch)
  with _ ->
    None

let split str ch =
  let rec split' b e =
    if b >= e
    then
      []
    else
      match (fuck str b ch) with
      | None -> let res = String.sub str b (e - b) |> int_of_string in [res]
      | Some(dest) ->
        let res = String.sub str b (dest - b) |> int_of_string in
        res :: split' (dest + 1) e
  in
  let res = split' 0 (String.length str) in
  res
;;

let () =
  let n = read_int () in
  let rec loop num =
    if num = 0
    then
      ()
    else
      let line = read_line () in
      Scanf.sscanf line "%Ld %Ld %Ld" (fun a b c ->
          let open Int64 in
          let comp n1 n2 n3 =
            (compare (add n1 n2) n3) < 0
          in let equal n1 n2 =
            (compare n1 n2) = 0
          in
          if comp a b c || comp a c b || comp b c a || equal a 0L || equal b 0L || equal c 0L
          then
            Printf.printf "No\n"
          else
            Printf.printf "Yes\n"
        );
    loop (num - 1)
  in
  loop n
;;

let rec read_lines n =
  match n with
  | 0 -> []
  | _ -> let res = read_line () in
    res :: read_lines (n - 1)

let fuck str b ch =
  try
    Some (String.index_from str b ch)
  with _ ->
    None

let split str ch =
  let rec split' b e =
    if b >= e
    then
      []
    else
      match (fuck str b ch) with
      | None -> let res = String.sub str b (e - b) |> int_of_string in [res]
      | Some(dest) ->
        let res = String.sub str b (dest - b) |> int_of_string in
        res :: split' (dest + 1) e
  in
  let res = split' 0 (String.length str) in
  res
;;

let () =
  let n = read_int () in
  let rec loop num =
    if num = 0
    then
      ()
    else
      let line = read_line () in
      Scanf.sscanf line "%Ld %Ld %Ld" (fun a b c ->
          let open Int64 in
          let comp n1 n2 =
            let res = sub n1 n2 in
            res = 0L || res = -1L || res = 1L
          in
          if comp a b && comp a c && comp b c
          then
            Printf.printf "Yes\n"
          else
            Printf.printf "No\n"
        );
    loop (num - 1)
  in
  loop n
;;

let rec read_lines n =
  match n with
  | 0 -> []
  | _ -> let res = read_line () in
    res :: read_lines (n - 1)

let fuck str b ch =
  try
    Some (String.index_from str b ch)
  with _ ->
    None

let split str ch =
  let rec split' b e =
    if b >= e
    then
      []
    else
      match (fuck str b ch) with
      | None -> let res = String.sub str b (e - b) |> int_of_string in [res]
      | Some(dest) ->
        let res = String.sub str b (dest - b) |> int_of_string in
        res :: split' (dest + 1) e
  in
  let res = split' 0 (String.length str) in
  res
;;

let () =
  let n = read_int () in
  let rec loop num =
    if num = 0
    then
      ()
    else
      let line = read_line () in
      Scanf.sscanf line "%Ld %Ld %Ld" (fun a b c ->
          let open Int64 in
          let comp n1 n2 n3 =
            (compare (add n1 n2) n3) < 0
          in
          if comp a b c || comp a c b || comp b c a
          then
            Printf.printf "No\n"
          else
            Printf.printf "Yes\n"
        );
    loop (num - 1)
  in
  loop n
;;


Vasya has recently found out what a digital root of a number is and he decided to share his knowledge with you.Let's assume that S(n) is the sum of digits of number n, for example, S(4098) = 4 + 0 + 9 + 8 = 21. Then the digital root of number n equals to:   dr(n) = S(n), if S(n) < 10;  dr(n) = dr( S(n) ), if S(n) ≥ 10. For example, dr(4098)  =  dr(21)  =  3.Vasya is afraid of large numbers, so the numbers he works with are at most 101000. For all such numbers, he has proved that dr(n)  =  S( S( S( S(n) ) ) ) (n ≤ 101000).Now Vasya wants to quickly find numbers with the given digital root. The problem is, he hasn't learned how to do that and he asked you to help him. You task is, given numbers k and d, find the number consisting of exactly k digits (the leading zeroes are not allowed), with digital root equal to d, or else state that such number does not exist.

The first line contains two integers k and d (1 ≤ k ≤ 1000; 0 ≤ d ≤ 9).

In a single line print either any number that meets the requirements (without the leading zeroes) or "No solution" (without the quotes), if the corresponding number does not exist. The chosen number must consist of exactly k digits. We assume that number 0 doesn't contain any leading zeroes.

NoteFor the first test sample dr(5881)  =  dr(22)  =  4.For the second test sample dr(36172)  =  dr(19)  =  dr(10)  =  1.

let (|>) x f = f x ;;
let (@@) f x = f x ;;

let ident x = x ;;

let char_to_int x = int_of_char x - int_of_char '0' ;;
let int_to_char x = char_of_int (x + int_of_char '0') ;;

module List = struct
  include ListLabels ;;

  let rec iteri i f = function
    | [] -> ()
    | a::l -> f i a; iteri (i + 1) f l

  let iteri ~f l = iteri 0 f l
end
;;

module Array = struct
  include ArrayLabels ;;

  let reduce ~f xs =
    let acc = ref xs.(0) in
    for i = 1 to length xs - 1 do
      acc := f !acc xs.(i)
    done;
    !acc
  ;;
end
;;

module String = struct
  include StringLabels ;;

  let map_to_list ~f ss =
    let res = ref [] in
    iter ss ~f:(fun c -> res := f c :: !res);
    List.rev !res
  ;;

  let of_char_list ss =
    let res = String.create @@ List.length ss in
    List.iteri ss ~f:(fun i c -> res.[i] <- c);
    res
  ;;

  let iteri ~f a =
    for i = 0 to length a - 1 do f i (unsafe_get a i) done
  ;;
end
;;

module Float = struct
  let (+) = (+.)
  let (-) = (-.)
  let ( * ) = ( *. )
  let (/) = (/.)
end
;;

module Int64 = struct
  include Int64
  let (+) = add
  let (-) = sub
  let ( * ) = mul
  let (/) = div
end
;;

let fold_for ?(skip=1) min max ~init ~f =
  let acc = ref init in
  let cur = ref min in
  while !cur < max do
    acc := f !acc !cur;
    cur := !cur + skip;
  done;
  !acc
;;

let iter_for ?(skip=1) min max ~f =
  let cur = ref min in
  while !cur < max do
    f !cur;
    cur := !cur + skip;
  done
;;

let solve k d =
  if d <> 0 then begin
    let s = String.create k in
    s.[0] <- char_of_int (d + int_of_char '0');
    String.fill s 1 (k - 1) '0';
    print_endline s
  end else if k = 1 then begin
    print_endline "0"
  end else begin
    print_endline "No solution";
  end
;;

let () =
  let k, d = Scanf.scanf "%d %d " (fun k d -> k, d) in
  solve k d
;;

let (|>) x f = f x ;;
let (@@) f x = f x ;;

let ident x = x ;;

let char_to_int x = int_of_char x - int_of_char '0' ;;
let int_to_char x = char_of_int (x + int_of_char '0') ;;

module List = struct
  include ListLabels ;;

  let rec iteri i f = function
    | [] -> ()
    | a::l -> f i a; iteri (i + 1) f l

  let iteri ~f l = iteri 0 f l
end
;;

module Array = struct
  include ArrayLabels ;;

  let reduce ~f xs =
    let acc = ref xs.(0) in
    for i = 1 to length xs - 1 do
      acc := f !acc xs.(i)
    done;
    !acc
  ;;
end
;;

module String = struct
  include StringLabels ;;

  let map_to_list ~f ss =
    let res = ref [] in
    iter ss ~f:(fun c -> res := f c :: !res);
    List.rev !res
  ;;

  let of_char_list ss =
    let res = String.create @@ List.length ss in
    List.iteri ss ~f:(fun i c -> res.[i] <- c);
    res
  ;;

  let iteri ~f a =
    for i = 0 to length a - 1 do f i (unsafe_get a i) done
  ;;
end
;;

module Float = struct
  let (+) = (+.)
  let (-) = (-.)
  let ( * ) = ( *. )
  let (/) = (/.)
end
;;

module Int64 = struct
  include Int64
  let (+) = add
  let (-) = sub
  let ( * ) = mul
  let (/) = div
end
;;

let fold_for ?(skip=1) min max ~init ~f =
  let acc = ref init in
  let cur = ref min in
  while !cur < max do
    acc := f !acc !cur;
    cur := !cur + skip;
  done;
  !acc
;;

let iter_for ?(skip=1) min max ~f =
  let cur = ref min in
  while !cur < max do
    f !cur;
    cur := !cur + skip;
  done
;;

let solve k d =
  if d <> 0 then begin
    let s = String.create k in
    s.[0] <- char_of_int (d + int_of_char '0');
    String.fill s 1 (k - 1) '0';
    print_endline s
  end else if k = 1 then begin
    print_endline "0"
  end else begin
    let s = String.create k in
    s.[0] <- char_of_int (1 + int_of_char '0');
    s.[1] <- char_of_int (9 + int_of_char '0');
    String.fill s 2 (k - 2) '0';
    print_endline s
  end
;;

let () =
  let k, d = Scanf.scanf "%d %d " (fun k d -> k, d) in
  solve k d
;;

let (|>) x f = f x ;;
let (@@) f x = f x ;;

let ident x = x ;;

let char_to_int x = int_of_char x - int_of_char '0' ;;
let int_to_char x = char_of_int (x + int_of_char '0') ;;

module List = struct
  include ListLabels ;;

  let rec iteri i f = function
    | [] -> ()
    | a::l -> f i a; iteri (i + 1) f l

  let iteri ~f l = iteri 0 f l
end
;;

module Array = struct
  include ArrayLabels ;;

  let reduce ~f xs =
    let acc = ref xs.(0) in
    for i = 1 to length xs - 1 do
      acc := f !acc xs.(i)
    done;
    !acc
  ;;
end
;;

module String = struct
  include StringLabels ;;

  let map_to_list ~f ss =
    let res = ref [] in
    iter ss ~f:(fun c -> res := f c :: !res);
    List.rev !res
  ;;

  let of_char_list ss =
    let res = String.create @@ List.length ss in
    List.iteri ss ~f:(fun i c -> res.[i] <- c);
    res
  ;;

  let iteri ~f a =
    for i = 0 to length a - 1 do f i (unsafe_get a i) done
  ;;
end
;;

module Float = struct
  let (+) = (+.)
  let (-) = (-.)
  let ( * ) = ( *. )
  let (/) = (/.)
end
;;

module Int64 = struct
  include Int64
  let (+) = add
  let (-) = sub
  let ( * ) = mul
  let (/) = div
end
;;

let fold_for ?(skip=1) min max ~init ~f =
  let acc = ref init in
  let cur = ref min in
  while !cur < max do
    acc := f !acc !cur;
    cur := !cur + skip;
  done;
  !acc
;;

let iter_for ?(skip=1) min max ~f =
  let cur = ref min in
  while !cur < max do
    f !cur;
    cur := !cur + skip;
  done
;;

let solve k d =
  let s = String.create k in
  s.[0] <- char_of_int (d + int_of_char '0');
  String.fill s 1 (k - 1) '0';
  print_endline s
;;

let () =
  let k, d = Scanf.scanf "%d %d " (fun k d -> k, d) in
  solve k d
;;


Boboniu likes playing chess with his employees. As we know, no employee can beat the boss in the chess game, so Boboniu has never lost in any round.You are a new applicant for his company. Boboniu will test you with the following chess question:Consider a $$$n\times m$$$ grid (rows are numbered from $$$1$$$ to $$$n$$$, and columns are numbered from $$$1$$$ to $$$m$$$). You have a chess piece, and it stands at some cell $$$(S_x,S_y)$$$ which is not on the border (i.e. $$$2 \le S_x \le n-1$$$ and $$$2 \le S_y \le m-1$$$).From the cell $$$(x,y)$$$, you can move your chess piece to $$$(x,y')$$$ ($$$1\le y'\le m, y' \neq y$$$) or $$$(x',y)$$$ ($$$1\le x'\le n, x'\neq x$$$). In other words, the chess piece moves as a rook. From the cell, you can move to any cell on the same row or column.Your goal is to visit each cell exactly once. Can you find a solution?Note that cells on the path between two adjacent cells in your route are not counted as visited, and it is not required to return to the starting point.

The only line of the input contains four integers $$$n$$$, $$$m$$$, $$$S_x$$$ and $$$S_y$$$ ($$$3\le n,m\le 100$$$, $$$2 \le S_x \le n-1$$$, $$$2 \le S_y \le m-1$$$) — the number of rows, the number of columns, and the initial position of your chess piece, respectively.

You should print $$$n\cdot m$$$ lines. The $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \leq x_i \leq n$$$, $$$1 \leq y_i \leq m$$$), denoting the $$$i$$$-th cell that you visited. You should print exactly $$$nm$$$ pairs $$$(x_i, y_i)$$$, they should cover all possible pairs $$$(x_i, y_i)$$$, such that $$$1 \leq x_i \leq n$$$, $$$1 \leq y_i \leq m$$$. We can show that under these constraints there always exists a solution. If there are multiple answers, print any.

open Printf
open Scanf

let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let read_4 _ = bscanf Scanning.stdin " %d %d %d %d " (fun a b c d -> (a,b,c,d))

let () =
  let (n,m,row,col) = read_4() in

  let trans (i,j) =
    ((if i=row then 1 else if i=1 then row else i),
      (if j=col then 1 else if j=1 then col else j))
  in

  for i=1 to n do
    for j=1 to m do
      let j = if i mod 2 = 1 then j else m+1-j in
      let (i,j) = trans (i,j) in
      printf "%d %d\n" i j
    done
  done


Mishka is a little polar bear. As known, little bears loves spending their free time playing dice for chocolates. Once in a wonderful sunny morning, walking around blocks of ice, Mishka met her friend Chris, and they started playing the game.Rules of the game are very simple: at first number of rounds n is defined. In every round each of the players throws a cubical dice with distinct numbers from 1 to 6 written on its faces. Player, whose value after throwing the dice is greater, wins the round. In case if player dice values are equal, no one of them is a winner.In average, player, who won most of the rounds, is the winner of the game. In case if two players won the same number of rounds, the result of the game is draw.Mishka is still very little and can't count wins and losses, so she asked you to watch their game and determine its result. Please help her!

The first line of the input contains single integer n n (1 ≤ n ≤ 100) — the number of game rounds. The next n lines contains rounds description. i-th of them contains pair of integers mi and ci (1 ≤ mi,  ci ≤ 6) — values on dice upper face after Mishka's and Chris' throws in i-th round respectively.

If Mishka is the winner of the game, print "Mishka" (without quotes) in the only line. If Chris is the winner of the game, print "Chris" (without quotes) in the only line. If the result of the game is draw, print "Friendship is magic!^^" (without quotes) in the only line.

NoteIn the first sample case Mishka loses the first round, but wins second and third rounds and thus she is the winner of the game.In the second sample case Mishka wins the first round, Chris wins the second round, and the game ends with draw with score 1:1.In the third sample case Chris wins the first round, but there is no winner of the next two rounds. The winner of the game is Chris.

let read () = Scanf.scanf "%d " (fun n -> n)
let print result = Printf.printf "%s\n" result

let count () =
    let n = read () in
    let rec loop i count_a count_b =
        if i < n
        then
            let a = read () in
            let b = read () in
            if a > b
            then
                loop (i + 1) (count_a + 1) count_b
            else
                if a < b
                then
                    loop (i + 1) count_a (count_b + 1)
                else
                    loop (i + 1) count_a count_b
        else
            (count_a, count_b)
    in loop 0 0 0

let solve (count_a, count_b) =
    if count_a > count_b
    then
        print "Mishka"
    else
        if count_a < count_b
        then
            print "Chris"
        else
            print "Friendship is magic!^^"

let () = solve (count ())

let read_pair () =
    let s = read_line () in
    let nums = List.map int_of_string (Str.split (Str.regexp " ") s) in
    match nums with
    | [a;b] -> (a, b)
    | _ -> (0, 0)

;;

let rec get_pair n (a, b)=
    if n > 0 then
        match read_pair () with
        | (x, y) -> if x > y then
                        get_pair (n-1) (a+1, b)
                     else if x < y then
                        get_pair (n-1) (a, b+1)
                    else
                        get_pair (n-1) (a, b)

    else
        (a, b)

;;

let read_nums () =
    let n = read_int () in
    let (a, b) = get_pair n (0, 0) in
    if a = b then
        print_string "Friendship is magic!^^\n"
    else if a > b then
        print_string "Mishka\n"
    else
        print_string "Chris\n"
;;

read_nums () ;;

let n = read_line () |> int_of_string in
let scores = Array.init n (fun x -> read_line())
  |> Array.map (fun x -> let b = Str.split (Str.regexp " *") x |> List.map int_of_string in (List.nth b 0, List.nth b 1)) |> Array.to_list in
let (m,c) = List.split scores in
let sum = List.fold_left ( + ) 0 in
print_string (match List.map2 compare m c |> sum with
  | 0 -> "Friendship is magic!^^"
  | e when e > 0 -> "Mishka"
  | _ -> "Chris"
)

let read_pair () =
    let s = read_line () in
    let nums = List.map int_of_string (Str.split (Str.regexp " ") s) in
    match nums with
    | [a;b] -> (a, b)
    | _ -> (0, 0)

;;

let rec get_pair n (a, b)=
    if n > 0 then
        match read_pair () with
        | (a, b) -> if a > b then
                        get_pair (n-1) (a+1, b)
                     else if a < b then
                        get_pair (n-1) (a, b+1)
                    else
                        get_pair (n-1) (a, b)

    else
        (a, b)

;;

let read_nums () =
    let n = read_int () in
    let (a, b) = get_pair n (0, 0) in
    if a = b then
        print_string "Friendship is magic!^^\n"
    else if a > b then
        print_string "Mishka\n"
    else
        print_string "Chris\n"
;;

read_nums () ;;

let n = read_line () |> int_of_string in
let scores = Array.init n (fun x -> read_line())
  |> Array.map (fun x -> let b = Str.split (Str.regexp " *") x |> List.map int_of_string in (List.nth b 0, List.nth b 1)) |> Array.to_list in
let (m,c) = List.split scores in
let sum = List.fold_left ( + ) 0 in
print_string (match List.map2 compare m c |> sum with
  | 0 -> "Friendship is magic!^^"
  | e when e > 0 -> "Minska"
  | _ -> "Chris"
)


DZY loves Fast Fourier Transformation, and he enjoys using it.Fast Fourier Transformation is an algorithm used to calculate convolution. Specifically, if a, b and c are sequences with length n, which are indexed from 0 to n - 1, andWe can calculate c fast using Fast Fourier Transformation.DZY made a little change on this formula. NowTo make things easier, a is a permutation of integers from 1 to n, and b is a sequence only containing 0 and 1. Given a and b, DZY needs your help to calculate c.Because he is naughty, DZY provides a special way to get a and b. What you need is only three integers n, d, x. After getting them, use the code below to generate a and b.//x is 64-bit variable;function getNextX() {    x = (x * 37 + 10007) % 1000000007;    return x;}function initAB() {    for(i = 0; i < n; i = i + 1){        a[i] = i + 1;    }    for(i = 0; i < n; i = i + 1){        swap(a[i], a[getNextX() % (i + 1)]);    }    for(i = 0; i < n; i = i + 1){        if (i < d)            b[i] = 1;        else            b[i] = 0;    }    for(i = 0; i < n; i = i + 1){        swap(b[i], b[getNextX() % (i + 1)]);    }}Operation x % y denotes remainder after division x by y. Function swap(x, y) swaps two values x and y.

The only line of input contains three space-separated integers n, d, x (1 ≤ d ≤ n ≤ 100000; 0 ≤ x ≤ 1000000006). Because DZY is naughty, x can't be equal to 27777500.

Output n lines, the i-th line should contain an integer ci - 1.

NoteIn the first sample, a is [1 3 2], b is [1 0 0], so c0 = max(1·1) = 1, c1 = max(1·0, 3·1) = 3, c2 = max(1·0, 3·0, 2·1) = 2.In the second sample, a is [2 1 4 5 3], b is [1 1 1 0 1].In the third sample, a is [5 2 1 4 3], b is [1 1 1 1 0].

open Printf
open Scanf

let ( ** ) a b = Int64.mul a b
let ( ++ ) a b = Int64.add a b
let ( -- ) a b = Int64.sub a b
let ( // ) a b = Int64.div a b
let ( %% ) a b = Int64.rem a b

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
(* The following two functions require the range to be of length at least 1 *)
let maxf i j f = fold (i+1) j (fun k a -> max (f k) a) (f i)

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let long x = Int64.of_int x
let short x = Int64.to_int x

let x = ref 0L

let getNextX () =
  x := (!x ** 37L ++ 10007L) %% 1000000007L;
  !x

let genab n d =
  let a = Array.init n (fun i -> i+1) in
  let b = Array.init n (fun i -> if i<d then 1 else 0) in
  let permute a =
    let swap i j =
      let (ai,aj) = (a.(i),a.(j)) in
      a.(i) <- aj;
      a.(j) <- ai
    in
    for i=0 to n-1 do
      swap i (short (getNextX() %% (long (i + 1))))
    done
  in
  permute a;
  permute b;
  (a,b)

let () =
  let n = read_int () in
  let d = read_int () in
  x := long (read_int ());

  let (a,b) = genab n d in

  let index = Array.init n (fun i -> i) in

  Array.sort (fun i j -> compare a.(j) a.(i)) index;

  let ones = Array.make n 0 in

  let rec fill_ones c i = if c = d then () else
      if b.(i) = 1 then (ones.(c) <- i; fill_ones (c+1) (i+1))
      else fill_ones c (i+1)
  in
  fill_ones 0 0;

  let answer = Array.make n 0 in

  let stopping_value = if d<=1000 then n else (999*n)/1000 in

  let rec loop n_finished i = if n_finished >= stopping_value || i = n then () else
      let j = index.(i) in
      let rec do_ones k ac = if k=d then ac else
	  let l = ones.(k) in
	  let i = l+j in
	  if i >= n then ac else
	    if answer.(i) = 0 then (answer.(i) <- a.(j); do_ones (k+1) (ac+1))
	    else do_ones (k+1) ac
      in
      let more_finished = do_ones 0 0 in
      loop (n_finished + more_finished) (i+1)
  in

  loop 0 0;

  if stopping_value < n then
    for i=0 to n-1 do
      if answer.(i) = 0 then (
	answer.(i) <- maxf 0 i (fun j -> a.(j) * b.(i-j))
      )
    done;

  for i=0 to n-1 do
    printf "%d\n" answer.(i)
  done


The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold:  the first letter of $$$a$$$ is less than the first letter of $$$b$$$;  the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that:  Word $$$1$$$: ab  Word $$$2$$$: ac  ...  Word $$$25$$$: az  Word $$$26$$$: ba  Word $$$27$$$: bc  ...  Word $$$649$$$: zx  Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).

For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.

let readInt () = Scanf.scanf " %d" (fun x -> x);;
let readBerland () = Scanf.scanf " %c%c" (fun a b -> a, b);;

let convertCharsToAlphabetPos = function
  | a, b -> (Char.code a) - 97, (Char.code b) - 97
;;
let getBerlandPos = function
  | a, b ->
    if b<a then a*25+b
    else if b>a then a*25+b-1
    else raise (Failure "Value Err!")
;;

let rec iterator = function
  | 0 -> 0
  | n ->
    let a, b = convertCharsToAlphabetPos (readBerland ()) in
    (* Add 1 as we aren't zero indexing *)
    print_int ((getBerlandPos (a, b))+1);
    print_newline ();
    iterator (n-1)
;;

let test_cases = readInt () in iterator test_cases;;

let n = int_of_string (read_line ())

let solve () =
  let s = read_line () in
  let first = (Char.code s.[0]) - 97 in
  let second = (Char.code s.[1]) - 97 in
  if second < first then
    Printf.printf "%d\n" ((26 * first) + second - (first - 1))
  else
    Printf.printf "%d\n" ((26 * first) + second - first)
;;
for i = 0 to n - 1 do
  solve ()
done;;

let readInt () = Scanf.scanf " %d" (fun x -> x);;
let readBerland () = Scanf.scanf " %c%c" (fun a b -> a, b);;

let convertCharsToAlphabetPos = function
  | a, b -> (Char.code a) - 97, (Char.code b) - 97
;;
let getBerlandPos = function
  | a, b -> a*26+b
;;

let rec iterator = function
  | 0 -> 0
  | n ->
    let a, b = convertCharsToAlphabetPos (readBerland ()) in
    print_int (getBerlandPos (a, b));
    print_newline ();
    iterator (n-1)
;;

let test_cases = readInt () in iterator test_cases;;


After too much playing on paper, Iahub has switched to computer games. The game he plays is called "Block Towers". It is played in a rectangular grid with n rows and m columns (it contains n × m cells). The goal of the game is to build your own city. Some cells in the grid are big holes, where Iahub can't build any building. The rest of cells are empty. In some empty cell Iahub can build exactly one tower of two following types:  Blue towers. Each has population limit equal to 100.  Red towers. Each has population limit equal to 200. However, it can be built in some cell only if in that moment at least one of the neighbouring cells has a Blue Tower. Two cells are neighbours is they share a side. Iahub is also allowed to destroy a building from any cell. He can do this operation as much as he wants. After destroying a building, the other buildings are not influenced, and the destroyed cell becomes empty (so Iahub can build a tower in this cell if needed, see the second example for such a case).Iahub can convince as many population as he wants to come into his city. So he needs to configure his city to allow maximum population possible. Therefore he should find a sequence of operations that builds the city in an optimal way, so that total population limit is as large as possible.He says he's the best at this game, but he doesn't have the optimal solution. Write a program that calculates the optimal one, to show him that he's not as good as he thinks.

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 500). Each of the next n lines contains m characters, describing the grid. The j-th character in the i-th line is '.' if you're allowed to build at the cell with coordinates (i, j) a tower (empty cell) or '#' if there is a big hole there.

Print an integer k in the first line (0 ≤ k ≤ 106) — the number of operations Iahub should perform to obtain optimal result. Each of the following k lines must contain a single operation in the following format:   «B x y» (1 ≤ x ≤ n, 1 ≤ y ≤ m) — building a blue tower at the cell (x, y);  «R x y» (1 ≤ x ≤ n, 1 ≤ y ≤ m) — building a red tower at the cell (x, y);  «D x y» (1 ≤ x ≤ n, 1 ≤ y ≤ m) — destroying a tower at the cell (x, y).  If there are multiple solutions you can output any of them. Note, that you shouldn't minimize the number of operations.

module Deque = struct
    type 'a der = { mutable l : 'a del; e : 'a; mutable r : 'a del }
    and 'a del = Null | El of 'a der ;;
    type 'a deq = { mutable len : int; mutable top : 'a del ;
                    mutable bot : 'a del } ;;

    (* create empty deque *)
    let create () = { len = 0; top = Null; bot = Null } ;;

    let length q = q.len ;;

    (* add element x to deque q *)
    let add x q =
        if q.len = 0 then
            begin
                let e = El { l = Null; e = x; r = Null } in
                q.len <- 1 ;
                q.top <- e ;
                q.bot <- e
            end
        else
            begin
                let El b = q.bot in
                let e = El { l = q.bot; e = x; r = Null } in
                b.r <- e ;
                q.len <- q.len + 1 ;
                q.bot <- e
            end ;;

    (* drop bottom element of deque *)
    let drop q = match q.bot with
        Null -> ()
      | El { l=b; e=_; r=_} -> q.len <- q.len - 1 ;
                               q.bot <- b ;;

    (* iterate over deque *)
    let iter f q =
        let rec aux = function
            Null -> ()
          | El { l=_; e=x; r=r } -> f x ; aux r
        in aux q.top ;;
end ;;

let (n, m) = Scanf.sscanf (read_line ()) "%d %d" (fun a b -> a, b) in
let grid = Array.init n (fun _ -> read_line ()) in
let moves = Deque.create () in (* : (char * (int * int)) -- queue of (char,
                                  coord) pairs *)

(* while v is not empty, choose an element of v and DFS to construct a tree of
 * connected components *)
let deltas = [(0, 1); (0, -1); (-1, 0); (1, 0)] in
for rx = 0 to n - 1 do
    for ry = 0 to m - 1 do
        let rec dfs ((x, y) as pos) =
            (* if we haven't seen this node, and it's valid *)
            try
                if grid.(x).[y] = '.' then
                    begin
                        grid.(x).[y] <- ' ' ;
                        (* build a blue building here *)
                        Deque.add ('B', pos) moves ;

                        let r = List.filter (fun (dx, dy) -> dfs (x + dx, y + dy)) deltas in
                        let c = List.length r in

                        (* if this isn't the root, destroy and replace with red *)
                        if pos <> (rx, ry) then
                            if c = 0 then
                                begin
                                    Deque.drop moves ;
                                    Deque.add ('R', pos) moves
                                end
                            else
                                begin
                                    Deque.add ('D', pos) moves ;
                                    Deque.add ('R', pos) moves
                                end ;
                        true
                    end
                else false
            with
                Invalid_argument _ -> false
        in
        ignore (dfs (rx, ry))
    done
done ;

Printf.printf "%d\n" (Deque.length moves) ;
Deque.iter (fun (c, (x, y)) ->
    Printf.printf "%c %d %d\n" c (x + 1) (y + 1)) moves ;;

module Deque = struct
    type 'a der = { mutable l : 'a del; e : 'a; mutable r : 'a del }
    and 'a del = Null | El of 'a der ;;
    type 'a deq = { mutable len : int; mutable top : 'a del ;
                    mutable bot : 'a del } ;;

    (* create empty deque *)
    let create () = { len = 0; top = Null; bot = Null } ;;

    let length q = q.len ;;

    (* add element x to deque q *)
    let add x q =
        if q.len = 0 then
            begin
                let e = El { l = Null; e = x; r = Null } in
                q.len <- 1 ;
                q.top <- e ;
                q.bot <- e
            end
        else
            begin
                let El b = q.bot in
                let e = El { l = q.bot; e = x; r = Null } in
                b.r <- e ;
                q.len <- q.len + 1 ;
                q.bot <- e
            end ;;

    (* drop bottom element of deque *)
    let drop q = match q.bot with
        Null -> ()
      | El { l=b; e=_; r=_} -> q.len <- q.len - 1 ;
                               q.bot <- b ;;

    (* iterate over deque *)
    let iter f q =
        let rec aux = function
            Null -> ()
          | El { l=_; e=x; r=r } -> f x ; aux r
        in aux q.top ;;
end ;;

let (n, m) = Scanf.sscanf (read_line ()) "%d %d" (fun a b -> a, b) in
let grid = Array.init n (fun _ -> read_line ()) in
let moves = Deque.create () in (* : (char * (int * int)) -- queue of (char,
                                  coord) pairs *)

(* while v is not empty, choose an element of v and DFS to construct a tree of
 * connected components *)
let deltas = [(0, 1); (0, -1); (-1, 0); (1, 0)] in
for rx = 0 to n - 1 do
    for ry = 0 to m - 1 do
        let rec dfs ((x, y) as pos) =
            (* if we haven't seen this node, and it's valid *)
            try
                if grid.(x).[y] = '.' then
                    begin
                        grid.(x).[y] <- ' ' ;
                        (* build a blue building here *)
                        Deque.add ('B', pos) moves ;

                        let r = List.filter (fun (dx, dy) -> dfs (x + dx, y + dy)) deltas in
                        let c = List.length r in

                        (* if this isn't the root, destroy and replace with red *)
                        if pos <> (rx, ry) then
                            if c = 0 then
                                begin
                                    Deque.drop moves ;
                                    Deque.add ('R', pos) moves
                                end
                            else
                                begin
                                    Deque.add ('D', pos) moves ;
                                    Deque.add ('R', pos) moves
                                end ;
                        true
                    end
                else false
            with
                Invalid_argument _ -> false
        in
        ignore (dfs (rx, ry))
    done
done ;

Printf.printf "%d\n" (Deque.length moves) ;
Deque.iter (fun (c, (x, y)) ->
    Printf.printf "%c %d %d\n" c x y) moves ;;