Alexandra has a paper strip with n numbers on it. Let's call them ai from left to right.Now Alexandra wants to split it into some pieces (possibly 1). For each piece of strip, it must satisfy: Each piece should contain at least l numbers. The difference between the maximal and the minimal number on the piece should be at most s.Please help Alexandra to find the minimal number of pieces meeting the condition above.

The first line contains three space-separated integers n, s, l (1 ≤ n ≤ 105, 0 ≤ s ≤ 109, 1 ≤ l ≤ 105). The second line contains n integers ai separated by spaces ( - 109 ≤ ai ≤ 109).

Output the minimal number of strip pieces. If there are no ways to split the strip, output -1.

NoteFor the first sample, we can split the strip into 3 pieces: [1, 3, 1], [2, 4], [1, 2].For the second sample, we can't let 1 and 100 be on the same piece, so no solution exists.

open Printf
open Scanf

let ( ++ ) a b = Int64.add a b
let ( -- ) a b = Int64.sub a b
let long x = Int64.of_int x
let abs64 = Int64.abs

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

let rec mylog n = if n=1 then 0 else 1+(mylog (n/2))

let build_range_mmin a n mmin =
  (* construct an array of arrays m.().()
     where m.(i).(j) is the mmin of the 2^i elements from a.(j) ... a.(j+2^i-1)
  *)
  let k = mylog n in  (* 2^k is the biggest block we use, so use 2^0...2^k *)

  let m = Array.make (k+1) [||] in
  m.(0) <- Array.copy a;
  for kk = 1 to k do
    let b = 1 lsl kk in (* block size *)
    m.(kk) <- Array.make (n-b+1) 0L;
    for j=0 to n-b do
      m.(kk).(j) <- mmin m.(kk-1).(j) m.(kk-1).(j+b/2)
    done
  done;
  (m,k,n)

let range_mmin a b (m,_,_) mmin =
  let k = mylog (b-a+1) in
  let bl = 1 lsl k in (* block size *)
    mmin m.(k).(a) m.(k).(b-bl+1)

let () =
  let n = read_int() in
  let s = read_long() in
  let l = read_int() in

  let a = Array.init n (fun _ -> read_long()) in

  let m = Array.make n 0 in

  let minrange = build_range_mmin a n min in
  let maxrange = build_range_mmin a n max in

  let rec label_loop i j = if j>=n then ()
    else if i+1 > j || j-i < l then label_loop i (j+1)
    else if i <> -1 && m.(i) = 0 then label_loop (i+1) j
    else (
      let lo = range_mmin (i+1) j minrange min in
      let hi = range_mmin (i+1) j maxrange max in
      if hi--lo > s then label_loop (i+1) j
      else (
	m.(j) <- if i = -1 then 1 else m.(i)+1;
	label_loop i (j+1)
      )
    )
  in

  label_loop (-1) (l-1);

  printf "%d\n" (if m.(n-1) = 0 then -1 else m.(n-1))


Alice has a birthday today, so she invited home her best friend Bob. Now Bob needs to find a way to commute to the Alice's home.In the city in which Alice and Bob live, the first metro line is being built. This metro line contains $$$n$$$ stations numbered from $$$1$$$ to $$$n$$$. Bob lives near the station with number $$$1$$$, while Alice lives near the station with number $$$s$$$. The metro line has two tracks. Trains on the first track go from the station $$$1$$$ to the station $$$n$$$ and trains on the second track go in reverse direction. Just after the train arrives to the end of its track, it goes to the depot immediately, so it is impossible to travel on it after that.Some stations are not yet open at all and some are only partially open — for each station and for each track it is known whether the station is closed for that track or not. If a station is closed for some track, all trains going in this track's direction pass the station without stopping on it.When the Bob got the information on opened and closed stations, he found that traveling by metro may be unexpectedly complicated. Help Bob determine whether he can travel to the Alice's home by metro or he should search for some other transport.

The first line contains two integers $$$n$$$ and $$$s$$$ ($$$2 \le s \le n \le 1000$$$) — the number of stations in the metro and the number of the station where Alice's home is located. Bob lives at station $$$1$$$. Next lines describe information about closed and open stations. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i = 0$$$ or $$$a_i = 1$$$). If $$$a_i = 1$$$, then the $$$i$$$-th station is open on the first track (that is, in the direction of increasing station numbers). Otherwise the station is closed on the first track. The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$b_i = 0$$$ or $$$b_i = 1$$$). If $$$b_i = 1$$$, then the $$$i$$$-th station is open on the second track (that is, in the direction of decreasing station numbers). Otherwise the station is closed on the second track.

Print "YES" (quotes for clarity) if Bob will be able to commute to the Alice's home by metro and "NO" (quotes for clarity) otherwise. You can print each letter in any case (upper or lower).

NoteIn the first example, all stations are opened, so Bob can simply travel to the station with number $$$3$$$.In the second example, Bob should travel to the station $$$5$$$ first, switch to the second track and travel to the station $$$4$$$ then.In the third example, Bob simply can't enter the train going in the direction of Alice's home.

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

let () =
  let n,s = get_2_i64 0 in
  let u = input_i64_array n in
  let d = input_i64_array n in
  let f11 = repm (s+1L) n false (fun m i ->
    let i = i32 i-$1 in
    if u.(i) = 1L && d.(i) = 1L then (
      `Break_m true
    ) else `Ok m
  ) in
  let f =
    if u.(0)<>1L then false
    else if u.(i32 s-$1)=1L then true
    else if d.(i32 s-$1)=1L && f11 then true
    else false
  in print_endline @@ if f then "YES" else "NO"

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

let () =
  let n,s = get_2_i64 0 in
  let u = input_i64_array n in
  let d = input_i64_array n in
  let f = repm 1L n false (fun m i ->
    let i = i32 i-$1 in
    if u.(i) = 1L && d.(i) = 1L then (
      `Break_m true
    ) else `Ok m
  ) in
  print_endline@@ if u.(0)<>1L || d.(i32 s-$1)<>1L || not f then "NO" else "YES"

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

let () =
  let n,s = get_2_i64 0 in
  let u = input_i64_array n in
  let d = input_i64_array n in
  let f11 = repm 1L n false (fun m i ->
    let i = i32 i-$1 in
    if u.(i) = 1L && d.(i) = 1L then (
      `Break_m true
    ) else `Ok m
  ) in
  (* print_endline@@ if u.(0)<>1L || d.(i32 s-$1)<>1L || not f then "NO" else "YES" *)
  let fall =
    if u.(0)=1L && u.(i32 s-$1)=1L then true
    else if d.(0)=1L && d.(i32 s-$1)=1L then true
    else if u.(0)=1L && d.(i32 s-$1)=1L && f11 then true
    else if d.(0)=1L && u.(i32 s-$1)=1L && f11 then true
    else false
  in print_endline @@ if fall then "YES" else "NO"


Polycarp likes to play with numbers. He takes some integer number $$$x$$$, writes it down on the board, and then performs with it $$$n - 1$$$ operations of the two kinds:   divide the number $$$x$$$ by $$$3$$$ ($$$x$$$ must be divisible by $$$3$$$);  multiply the number $$$x$$$ by $$$2$$$. After each operation, Polycarp writes down the result on the board and replaces $$$x$$$ by the result. So there will be $$$n$$$ numbers on the board after all.You are given a sequence of length $$$n$$$ — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.It is guaranteed that the answer exists.

The first line of the input contatins an integer number $$$n$$$ ($$$2 \le n \le 100$$$) — the number of the elements in the sequence. The second line of the input contains $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 3 \cdot 10^{18}$$$) — rearranged (reordered) sequence that Polycarp can wrote down on the board.

Print $$$n$$$ integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board. It is guaranteed that the answer exists.

NoteIn the first example the given sequence can be rearranged in the following way: $$$[9, 3, 6, 12, 4, 8]$$$. It can match possible Polycarp's game which started with $$$x = 9$$$.

let ($) f g = fun x -> g (f x)
let split_string ?(pattern="") = Str.split @@ Str.regexp pattern
let n = Scanf.sscanf (read_line ()) "%d" @@ fun n -> n
let a = read_line () |> split_string ~pattern:" " |> List.map Int64.of_string

let three = Int64.of_int 3

let deg_3 =
  let rec f i n = if n = (Int64.mul (Int64.div n three) three) then f (i + 1) (Int64. div n three) else i in
  f 0

let () =
  List.map (fun i -> (-deg_3 i, i)) a
    |> List.sort compare
    |> List.map (snd $ Int64.to_string)
    |> String.concat " "
    |> print_endline


The Little Elephant loves playing with arrays. He has array a, consisting of n positive integers, indexed from 1 to n. Let's denote the number with index i as ai. Additionally the Little Elephant has m queries to the array, each query is characterised by a pair of integers lj and rj (1 ≤ lj ≤ rj ≤ n). For each query lj, rj the Little Elephant has to count, how many numbers x exist, such that number x occurs exactly x times among numbers alj, alj + 1, ..., arj.Help the Little Elephant to count the answers to all queries.

The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 105) — the size of array a and the number of queries to it. The next line contains n space-separated positive integers a1, a2, ..., an (1 ≤ ai ≤ 109). Next m lines contain descriptions of queries, one per line. The j-th of these lines contains the description of the j-th query as two space-separated integers lj and rj (1 ≤ lj ≤ rj ≤ n).

In m lines print m integers — the answers to the queries. The j-th line should contain the answer to the j-th query.

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n 0 in
  let c = Hashtbl.create n in
  let ls = Array.make m 0 in
  let rs = Array.make m 0 in
  let res = Array.make m 0 in
  let () =
    for i = 0 to n - 1 do
      let k = Scanf.bscanf sb "%d " (fun s -> s) in
	a.(i) <- k;
	try
	  let cnt = Hashtbl.find c k in
	    Hashtbl.replace c k (cnt + 1)
	with
	  | Not_found -> Hashtbl.replace c k 1
    done;
    for i = 0 to m - 1 do
      ls.(i) <- Scanf.bscanf sb "%d " (fun s -> s);
      rs.(i) <- Scanf.bscanf sb "%d " (fun s -> s);
    done;
  in
  let ds = ref [] in
  let () =
    for i = 0 to n - 1 do
      let k = a.(i) in
	if k <= Hashtbl.find c k then (
	  ds := k :: !ds;
	  Hashtbl.replace c k (-1)
	)
    done
  in
  let b = Array.make (n + 1) 0 in
    List.iter
      (fun d ->
	 b.(0) <- 0;
	 for i = 1 to n do
	   b.(i) <- b.(i - 1) + if a.(i - 1) = d then 1 else 0;
	 done;
	 for i = 0 to m - 1 do
	   if d = b.(rs.(i)) - b.(ls.(i) - 1)
	   then res.(i) <- res.(i) + 1
	 done
      ) !ds;
    for i = 0 to m - 1 do
      Printf.printf "%d\n" res.(i)
    done


For given n, l and r find the number of distinct geometrical progression, each of which contains n distinct integers not less than l and not greater than r. In other words, for each progression the following must hold: l ≤ ai ≤ r and ai ≠ aj , where a1, a2, ..., an is the geometrical progression, 1 ≤ i, j ≤ n and i ≠ j.Geometrical progression is a sequence of numbers a1, a2, ..., an where each term after first is found by multiplying the previous one by a fixed non-zero number d called the common ratio. Note that in our task d may be non-integer. For example in progression 4, 6, 9, common ratio is .Two progressions a1, a2, ..., an and b1, b2, ..., bn are considered different, if there is such i (1 ≤ i ≤ n) that ai ≠ bi.

The first and the only line cotains three integers n, l and r (1 ≤ n ≤ 107, 1 ≤ l ≤ r ≤ 107).

Print the integer K — is the answer to the problem.

NoteThese are possible progressions for the first test of examples:   1;  2;  3;  4;  5;  6;  7;  8;  9;  10. These are possible progressions for the second test of examples:   6, 7;  6, 8;  6, 9;  7, 6;  7, 8;  7, 9;  8, 6;  8, 7;  8, 9;  9, 6;  9, 7;  9, 8. These are possible progressions for the third test of examples:   1, 2, 4;  1, 3, 9;  2, 4, 8;  4, 2, 1;  4, 6, 9;  8, 4, 2;  9, 3, 1;  9, 6, 4. These are possible progressions for the fourth test of examples:   4, 6, 9;  9, 6, 4.

let (+|) = Int64.add
let (-|) = Int64.sub
let ( *| ) = Int64.mul
let ( /| ) = Int64.div

let rec gcd a b =
  if b = 0
  then a
  else gcd b (a mod b)

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let l = Scanf.bscanf sb "%d " (fun s -> s) in
  let r = Scanf.bscanf sb "%d " (fun s -> s) in
    if n = 1
    then Printf.printf "%d\n" (r - l + 1)
    else if n = 2
    then Printf.printf "%Ld\n"
      (Int64.of_int (r - l + 1) *| Int64.of_int (r - l))
    else (
      let pow a n =
	let r = ref 1 in
	  for i = 1 to n do
	    r := !r * a
	  done;
	  !r
      in
      let m = int_of_float (float_of_int r ** (1.0 /. float_of_int (n - 1)) +. 0.000001) in
      let res = ref 0L in
	for a = 1 to m do
	  for b = a + 1 to m do
	    if gcd a b = 1 then (
	      let k1 = r / pow b (n - 1) in
	      let k2 = (l - 1) / pow a (n - 1) in
		(*Printf.printf "asd %d %d  %d %d\n" a b k2 k1;*)
		if k1 > k2
		then res := !res +| Int64.of_int (k1 - k2)
	    )
	  done
	done;
	res := !res *| 2L;
	Printf.printf "%Ld\n" !res
    )

let (+|) = Int64.add
let (-|) = Int64.sub
let ( *| ) = Int64.mul
let ( /| ) = Int64.div

let rec gcd a b =
  if b = 0
  then a
  else gcd b (a mod b)

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let l = Scanf.bscanf sb "%d " (fun s -> s) in
  let r = Scanf.bscanf sb "%d " (fun s -> s) in
    if n = 1
    then Printf.printf "%d\n" (r - l + 1)
    else if n = 2
    then Printf.printf "%Ld\n"
      (Int64.of_int (r - l + 1) *| Int64.of_int (r - l))
    else (
      let pow a n =
	let r = ref 1 in
	  for i = 1 to n do
	    r := !r * a
	  done;
	  !r
      in
      let m = int_of_float (float_of_int r ** (1.0 /. float_of_int (n - 1))) in
      let res = ref 0L in
	for a = 1 to m do
	  for b = a + 1 to m do
	    if gcd a b = 1 then (
	      let k1 = r / pow b (n - 1) in
	      let k2 = (l - 1) / pow a (n - 1) in
		(*Printf.printf "asd %d %d  %d %d\n" a b k2 k1;*)
		if k1 > k2
		then res := !res +| Int64.of_int (k1 - k2)
	    )
	  done
	done;
	res := !res *| 2L;
	Printf.printf "%Ld\n" !res
    )


In the rush of modern life, people often forget how beautiful the world is. The time to enjoy those around them is so little that some even stand in queues to several rooms at the same time in the clinic, running from one queue to another.(Cultural note: standing in huge and disorganized queues for hours is a native tradition in Russia, dating back to the Soviet period. Queues can resemble crowds rather than lines. Not to get lost in such a queue, a person should follow a strict survival technique: you approach the queue and ask who the last person is, somebody answers and you join the crowd. Now you're the last person in the queue till somebody else shows up. You keep an eye on the one who was last before you as he is your only chance to get to your destination) I'm sure many people have had the problem when a stranger asks who the last person in the queue is and even dares to hint that he will be the last in the queue and then bolts away to some unknown destination. These are the representatives of the modern world, in which the ratio of lack of time is so great that they do not even watch foreign top-rated TV series. Such people often create problems in queues, because the newcomer does not see the last person in the queue and takes a place after the "virtual" link in this chain, wondering where this legendary figure has left.The Smart Beaver has been ill and he's made an appointment with a therapist. The doctor told the Beaver the sad news in a nutshell: it is necessary to do an electrocardiogram. The next day the Smart Beaver got up early, put on the famous TV series on download (three hours till the download's complete), clenched his teeth and bravely went to join a queue to the electrocardiogram room, which is notorious for the biggest queues at the clinic.Having stood for about three hours in the queue, the Smart Beaver realized that many beavers had not seen who was supposed to stand in the queue before them and there was a huge mess. He came up to each beaver in the ECG room queue and asked who should be in front of him in the queue. If the beaver did not know his correct position in the queue, then it might be his turn to go get an ECG, or maybe he should wait for a long, long time...As you've guessed, the Smart Beaver was in a hurry home, so he gave you all the necessary information for you to help him to determine what his number in the queue can be.

The first line contains two integers n (1 ≤ n ≤ 103) and x (1 ≤ x ≤ n) — the number of beavers that stand in the queue and the Smart Beaver's number, correspondingly. All willing to get to the doctor are numbered from 1 to n. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ n) — the number of the beaver followed by the i-th beaver. If ai = 0, then the i-th beaver doesn't know who is should be in front of him. It is guaranteed that values ai are correct. That is there is no cycles in the dependencies. And any beaver is followed by at most one beaver in the queue. The input limits for scoring 30 points are (subproblem B1):    It is guaranteed that the number of zero elements ai doesn't exceed 20.  The input limits for scoring 100 points are (subproblems B1+B2):    The number of zero elements ai is arbitrary.

Print all possible positions of the Smart Beaver in the line in the increasing order.

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let x = Scanf.bscanf sb "%d " (fun s -> s) in
  let x = x - 1 in
  let a = Array.make n 0 in
  let b = Array.make n (-1) in
  let () =
    for i = 0 to n - 1 do
      let k = Scanf.bscanf sb "%d " (fun s -> s) in
	a.(i) <- k - 1;
	if k > 0
	then b.(k - 1) <- i
    done;
  in
  let used = Array.make n false in
  let c = Array.make n 1 in
  let rec dfs k =
    used.(k) <- true;
    if b.(k) >= 0 then (
      dfs b.(k);
      c.(k) <- c.(b.(k)) + 1;
    )
  in
  let used2 = Array.make n false in
  let bob =
    let c = ref 1 in
    let p = ref x in
      used2.(x) <- true;
      while a.(!p) >= 0 do
	incr c;
	p := a.(!p);
	used2.(!p) <- true;
      done;
      !c
  in
  let () =
    for i = 0 to n - 1 do
      if not used.(i)
      then dfs i
    done;
  in
  let s = ref [] in
  let () =
    for i = 0 to n - 1 do
      if not used2.(i) && a.(i) < 0
      then s := c.(i) :: !s
    done;
  in
  let s = Array.of_list !s in
  let a = Array.make (n + 1) false in
    a.(0) <- true;
    for k = 0 to Array.length s - 1 do
      let k = s.(k) in
(*Printf.printf "asd %d\n" k;*)
	for i = n downto k do
	  if a.(i - k)
	  then a.(i) <- true
	done
    done;
    (*for i = 0 to n - 1 do
      Printf.printf "c %d %d %d\n" i c.(i) x;
    done;*)
    for i = 0 to n do
      if a.(i)
      then Printf.printf "%d\n" (i + bob);
    done


Petya has got 2n cards, each card contains some integer. The numbers on the cards can be the same. Let's index all cards by consecutive integers from 1 to 2n. We'll denote the number that is written on a card with number i, as ai. In order to play one entertaining game with his friends, Petya needs to split the cards into pairs so that each pair had equal numbers on the cards. Help Petya do that.

The first line contains integer n (1 ≤ n ≤ 3·105). The second line contains the sequence of 2n positive integers a1, a2, ..., a2n (1 ≤ ai ≤ 5000) — the numbers that are written on the cards. The numbers on the line are separated by single spaces.

If it is impossible to divide the cards into pairs so that cards in each pair had the same numbers, print on a single line integer -1. But if the required partition exists, then print n pairs of integers, a pair per line — the indices of the cards that form the pairs. Separate the numbers on the lines by spaces. You can print the pairs and the numbers in the pairs in any order. If there are multiple solutions, print any of them.

let input = open_in "input.txt"
let output = open_out "output.txt"

let read_int () = Scanf.fscanf input " %d " (fun x -> x) ;;

let n = read_int () ;;

let a =
    let len = 2 * n
    in let rec loop i f =
        if i > len then f []
        else let ai = read_int () in loop (i + 1) (fun l -> f ((ai, i) :: l))
    in loop 1 (fun x -> x) ;;

let seen = Array.make 5001 0 ;;

let pairs =
    let rec loop l a = match l with
        | [] -> a
        | (v, i) :: t ->
            if seen.(v) = 0 then (seen.(v) <- i ; loop t a)
            else let j = seen.(v) in seen.(v) <- 0 ; loop t ((j, i) :: a)
    in loop a [] ;;

let possible =
    let rec loop i =
        if i > 5000 then true
        else if seen.(i) <> 0 then false else loop (i + 1)
    in loop 1 ;;

let print_pair (a, b) = Printf.fprintf output "%d %d\n" a b ;;

if not possible then Printf.fprintf output "-1\n"
else
    let rec loop = function
        | [] -> ()
        | h :: t -> print_pair h ; loop t
    in loop pairs ;;

let input = open_in "input.txt"
let output = open_out "output.txt"

let read_int () = Scanf.fscanf input " %d " (fun x -> x) ;;

let n = read_int () ;;

let a =
    let len = 2 * n
    in let rec loop i f =
        if i > len then f []
        else let ai = read_int () in loop (i + 1) (fun l -> f ((ai, i) :: l))
    in loop 1 (fun x -> x) ;;

let upper = List.fold_left (fun x (v, i) -> max x v) 0 a ;;
let seen = Array.make (upper + 1) 0 ;;

let pairs =
    let rec loop l a = match l with
        | [] -> a
        | (v, i) :: t ->
            if seen.(v) = 0 then (seen.(v) <- i ; loop t a)
            else let j = seen.(v) in seen.(v) <- 0 ; loop t ((j, i) :: a)
    in loop a [] ;;

let possible =
    let rec loop i =
        if i > upper then true
        else if seen.(i) <> 0 then false else loop (i + 1)
    in loop 1 ;;

if not possible then Printf.fprintf output "-1\n"
else
    let print_pair (a, b) = Printf.fprintf output "%d %d\n" a b
    in let rec loop = function
        | [] -> ()
        | h :: t -> print_pair h ; loop t
    in loop pairs ;;

let chan = Scanf.Scanning.from_channel (open_in "input.txt")
let read_int () = Scanf.bscanf chan " %d " (fun x -> x)

let ochan = open_out "output.txt"
let print_string s = Printf.fprintf ochan "%s" s

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let maxf i j f = fold (i+1) j (fun k a -> max (f k) a) (f i)

let () =
  let n = read_int() in
  let a = Array.init (2*n) (fun _ -> read_int()) in

  let t = Array.make 5001 (-1) in

  let rec loop i p = if i=2*n then () else (
    if t.(a.(i)) >= 0 then (
      if p then print_string (Printf.sprintf "%d %d\n" (1+t.(a.(i))) (i+1));
      t.(a.(i)) <- -1;
      loop (i+1) p
    ) else (
      t.(a.(i)) <- i;
      loop (i+1) p
    )
  )
  in

    loop 0 false;

    if (maxf 0 5000 (fun i-> t.(i))) = -1 then (
      loop 0 true;
    ) else (
      print_string "-1\n"
    )

let chan = Scanf.Scanning.from_channel (open_in "input.txt")
let read_int () = Scanf.bscanf chan " %d " (fun x -> x)

let ochan = open_out "output.txt"
let print_string s = Printf.fprintf ochan "%s" s

module Iset = Set.Make (struct type t = int let compare = compare end)

let () =
  let n = read_int() in
  let a = Array.init (2*n) (fun _ -> read_int()) in

  let rec loop i p s = if i=2*n then s else (
    if Iset.mem a.(i) s
    then (
      if p then print_string (Printf.sprintf "%d %d\n" a.(i) a.(i));
      loop (i+1) p (Iset.remove a.(i) s)
    )
    else loop (i+1) p (Iset.add a.(i) s)
  )
  in
  let s = loop 0 false Iset.empty in

    if Iset.is_empty s then (
      ignore (loop 0 true Iset.empty)
    ) else (
      print_string "-1\n"
    )


You are given n points with integer coordinates on the plane. Points are given in a way such that there is no triangle, formed by any three of these n points, which area exceeds S.Alyona tried to construct a triangle with integer coordinates, which contains all n points and which area doesn't exceed 4S, but, by obvious reason, had no success in that. Please help Alyona construct such triangle. Please note that vertices of resulting triangle are not necessarily chosen from n given points.

In the first line of the input two integers n and S (3 ≤ n ≤ 5000, 1 ≤ S ≤ 1018) are given — the number of points given and the upper bound value of any triangle's area, formed by any three of given n points. The next n lines describes given points: ith of them consists of two integers xi and yi ( - 108 ≤ xi, yi ≤ 108) — coordinates of ith point. It is guaranteed that there is at least one triple of points not lying on the same line.

Print the coordinates of three points — vertices of a triangle which contains all n points and which area doesn't exceed 4S. Coordinates of every triangle's vertex should be printed on a separate line, every coordinate pair should be separated by a single space. Coordinates should be an integers not exceeding 109 by absolute value. It is guaranteed that there is at least one desired triangle. If there is more than one answer, print any of them.

let (+|) = Int64.add
let (-|) = Int64.sub
let ( *| ) = Int64.mul
let ( /| ) = Int64.div

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let s = Scanf.bscanf sb "%Ld " (fun s -> s) in
  let ps = Array.make n (0L, 0L) in
  let _ =
    for i = 0 to n - 1 do
      let x = Scanf.bscanf sb "%Ld " (fun s -> s) in
      let y = Scanf.bscanf sb "%Ld " (fun s -> s) in
	ps.(i) <- (x, y)
    done;
  in
  let cross x1 y1 x2 y2 = x1 *| y2 -| x2 *| y1 in
  let area3 (x, y) (x1, y1) (x2, y2) =
    x *| y1 +| x1 *| y2 +| x2 *| y -| x2 *| y1 -| x1 *| y -| x *| y2
  in
  let cmp ((x1 : int64), (y1 : int64)) (x2, y2) =
    if x1 < x2
    then -1
    else if x1 > x2
    then 1
    else compare y1 y2
  in
  let _ = Array.sort cmp ps in
    (* Convex hull *)
  let p1 = ps.(0)
  and p2 = ps.(n - 1) in
  let up = Array.make n (0L, 0L) in
  let down = Array.make n (0L, 0L) in
  let pos_up = ref 0 in
  let pos_down = ref 0 in
  let hull =
    up.(!pos_up) <- p1;
    incr pos_up;
    down.(!pos_down) <- p1;
    incr pos_down;
    for i = 1 to n - 1 do
      if i = n - 1 || area3 p1 ps.(i) p2 < 0L then (
	while !pos_up >= 2 &&
	  area3 up.(!pos_up - 2) up.(!pos_up - 1) ps.(i) >= 0L do
	    decr pos_up
	done;
	up.(!pos_up) <- ps.(i);
	incr pos_up;
      );
      if i = n - 1 || area3 p1 ps.(i) p2 > 0L then (
	while !pos_down >= 2 &&
	  area3 down.(!pos_down - 2) down.(!pos_down - 1) ps.(i) <= 0L do
	    decr pos_down
	done;
	down.(!pos_down) <- ps.(i);
	incr pos_down;
      );
    done;
    let hull = Array.make (!pos_up + !pos_down - 2) ps.(0) in
      for i = 0 to !pos_up - 1 do
	hull.(i) <- up.(i)
      done;
      for i = !pos_down - 2 downto 1 do
	hull.(!pos_down - 2 - i + !pos_up) <- down.(i)
      done;
      hull
  in
  let area3 (x, y) (x1, y1) (x2, y2) =
    let x = float_of_int x
    and y = float_of_int y
    and x1 = float_of_int x1
    and y1 = float_of_int y1
    and x2 = float_of_int x2
    and y2 = float_of_int y2 in
      x *. y1 +. x1 *. y2 +. x2 *. y -. x2 *. y1 -. x1 *. y -. x *. y2
  in
  let hull =
    Array.map (fun (x, y) -> (Int64.to_int x, Int64.to_int y)) hull
  in
  let maxs = ref 0.0 in
  let b = ref (0, 0, 0) in
    (*for i = 0 to Array.length hull - 1 do
      let (x, y) = hull.(i) in
	Printf.eprintf "asd %Ld %Ld\n" x y
    done;*)
    for i = 0 to Array.length hull - 1 do
      let k = ref i in
	for j = i + 2 to Array.length hull - 1 do
	  let s = ref 0.0 in
	  let s' = ref 0.0 in
	    s := abs_float (area3 hull.(i) hull.(j) hull.(!k));
	    while !k < j - 1 &&
	      (s' := abs_float (area3 hull.(i) hull.(j) hull.(!k + 1));
	       !s' >= !s)
	    do
	      incr k;
	      s := !s';
	      (*Printf.eprintf "qaz %d %d %d %Ld\n" i j !k !s;*)
	    done;
	    (*Printf.eprintf "zxc %d %d %Ld\n" i j !s;*)
	    if !maxs < !s then (
	      maxs := !s;
	      b := (i, j, !k);
	    )
	done;
    done;
    let (i, j, k) = !b in
    let (x1, y1) = hull.(i)
    and (x2, y2) = hull.(j)
    and (x3, y3) = hull.(k) in
      (*Printf.eprintf "qwe %Ld %Ld\n" x1 y1;
      Printf.eprintf "qwe %Ld %Ld\n" x2 y2;
      Printf.eprintf "qwe %Ld %Ld\n" x3 y3;*)
      Printf.printf "%d %d\n" (x2 + x3 - x1) (y2 + y3 - y1);
      Printf.printf "%d %d\n" (x3 + x1 - x2) (y3 + y1 - y2);
      Printf.printf "%d %d\n" (x1 + x2 - x3) (y1 + y2 - y3);

open Printf
open Scanf

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let maxf i j f = fold (i+1) j (fun k a -> max (f k) a) (f i)

let ( ** ) a b = Int64.mul a b
let ( ++ ) a b = Int64.add a b
let ( -- ) a b = Int64.sub a b

let long x = Int64.of_int x
let short x = Int64.to_int x

let cross (x1,y1) (x2,y2) = (x1**y2) -- (y1**x2)
let ( --- ) (x1,y1) (x2,y2) = (x1--x2, y1--y2)
let ( +++ ) (x1,y1) (x2,y2) = (x1++x2, y1++y2)

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

let () =
  let n = read_int () in
  let _ = read_long () in (* don't need s *)
  let p = Array.init n (fun _ ->
    let x = read_long() in
    (x, read_long())
  ) in

  let area a b c = Int64.abs (cross (b --- a) (c --- a)) in

  let rec expand no_change_count ar (a,b,c) =
    if no_change_count = 2 then (a,b,c) else
      let (ar', c') = maxf 0 (n-1) (fun c -> (area p.(a) p.(b) p.(c), c)) in
      if ar' > ar then expand 0 ar' (c',a,b) else expand (no_change_count+1) ar (c,a,b)
  in

  let (a,b,c) = expand 0 0L (0,1,2) in
  let (a,b,c) = (p.(a), p.(b), p.(c)) in

  let pr (x,y) = printf "%Ld %Ld\n" x y in
  pr (a +++ b --- c);
  pr (b +++ c --- a);
  pr (c +++ a --- b)

let (+|) = Int64.add
let (-|) = Int64.sub
let ( *| ) = Int64.mul
let ( /| ) = Int64.div

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let s = Scanf.bscanf sb "%Ld " (fun s -> s) in
  let ps = Array.make n (0L, 0L) in
  let _ =
    for i = 0 to n - 1 do
      let x = Scanf.bscanf sb "%Ld " (fun s -> s) in
      let y = Scanf.bscanf sb "%Ld " (fun s -> s) in
	ps.(i) <- (x, y)
    done;
  in
  let cross x1 y1 x2 y2 = x1 *| y2 -| x2 *| y1 in
  let area3 (x, y) (x1, y1) (x2, y2) =
    x *| y1 +| x1 *| y2 +| x2 *| y -| x2 *| y1 -| x1 *| y -| x *| y2
  in
  let cmp ((x1 : int64), (y1 : int64)) (x2, y2) =
    if x1 < x2
    then -1
    else if x1 > x2
    then 1
    else compare y1 y2
  in
  let _ = Array.sort cmp ps in
    (* Convex hull *)
  let p1 = ps.(0)
  and p2 = ps.(n - 1) in
  let up = Array.make n (0L, 0L) in
  let down = Array.make n (0L, 0L) in
  let pos_up = ref 0 in
  let pos_down = ref 0 in
  let hull =
    up.(!pos_up) <- p1;
    incr pos_up;
    down.(!pos_down) <- p1;
    incr pos_down;
    for i = 1 to n - 1 do
      if i = n - 1 || area3 p1 ps.(i) p2 < 0L then (
	while !pos_up >= 2 &&
	  area3 up.(!pos_up - 2) up.(!pos_up - 1) ps.(i) >= 0L do
	    decr pos_up
	done;
	up.(!pos_up) <- ps.(i);
	incr pos_up;
      );
      if i = n - 1 || area3 p1 ps.(i) p2 > 0L then (
	while !pos_down >= 2 &&
	  area3 down.(!pos_down - 2) down.(!pos_down - 1) ps.(i) <= 0L do
	    decr pos_down
	done;
	down.(!pos_down) <- ps.(i);
	incr pos_down;
      );
    done;
    let hull = Array.make (!pos_up + !pos_down - 2) ps.(0) in
      for i = 0 to !pos_up - 1 do
	hull.(i) <- up.(i)
      done;
      for i = !pos_down - 2 downto 1 do
	hull.(!pos_down - 2 - i + !pos_up) <- down.(i)
      done;
      hull
  in
  let maxs = ref 0L in
  let b = ref (0, 0, 0) in
    (*for i = 0 to Array.length hull - 1 do
      let (x, y) = hull.(i) in
	Printf.eprintf "asd %Ld %Ld\n" x y
    done;*)
    for i = 0 to Array.length hull - 1 do
      let k = ref i in
      let s = ref 0L in
	for j = i + 2 to Array.length hull - 1 do
	  while !k < j - 1 &&
	    Int64.abs (area3 hull.(i) hull.(j) hull.(!k + 1)) > !s
	  do
	    incr k;
	    s := Int64.abs (area3 hull.(i) hull.(j) hull.(!k));
	  done;
	  if !maxs < !s then (
	    maxs := !s;
	    b := (i, j, !k);
	  )
	done;
    done;
    let (i, j, k) = !b in
    let (x1, y1) = hull.(i)
    and (x2, y2) = hull.(j)
    and (x3, y3) = hull.(k) in
      Printf.printf "%Ld %Ld\n" (x2 +| x3 -| x1) (y2 +| y3 -| y1);
      Printf.printf "%Ld %Ld\n" (x3 +| x1 -| x2) (y3 +| y1 -| y2);
      Printf.printf "%Ld %Ld\n" (x1 +| x2 -| x3) (y1 +| y2 -| y3);

let (+|) = Int64.add
let (-|) = Int64.sub
let ( *| ) = Int64.mul
let ( /| ) = Int64.div

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let s = Scanf.bscanf sb "%Ld " (fun s -> s) in
  let ps = Array.make n (0L, 0L) in
  let _ =
    for i = 0 to n - 1 do
      let x = Scanf.bscanf sb "%Ld " (fun s -> s) in
      let y = Scanf.bscanf sb "%Ld " (fun s -> s) in
	ps.(i) <- (x, y)
    done;
  in
  let cross x1 y1 x2 y2 = x1 *| y2 -| x2 *| y1 in
  let area3 (x, y) (x1, y1) (x2, y2) =
    x *| y1 +| x1 *| y2 +| x2 *| y -| x2 *| y1 -| x1 *| y -| x *| y2
  in
  let cmp ((x1 : int64), (y1 : int64)) (x2, y2) =
    if x1 < x2
    then -1
    else if x1 > x2
    then 1
    else compare y1 y2
  in
  let _ = Array.sort cmp ps in
    (* Convex hull *)
  let p1 = ps.(0)
  and p2 = ps.(n - 1) in
  let up = Array.make n (0L, 0L) in
  let down = Array.make n (0L, 0L) in
  let pos_up = ref 0 in
  let pos_down = ref 0 in
  let hull =
    up.(!pos_up) <- p1;
    incr pos_up;
    down.(!pos_down) <- p1;
    incr pos_down;
    for i = 1 to n - 1 do
      if i = n - 1 || area3 p1 ps.(i) p2 < 0L then (
	while !pos_up >= 2 &&
	  area3 up.(!pos_up - 2) up.(!pos_up - 1) ps.(i) > 0L do
	    decr pos_up
	done;
	up.(!pos_up) <- ps.(i);
	incr pos_up;
      );
      if i = n - 1 || area3 p1 ps.(i) p2 > 0L then (
	while !pos_down >= 2 &&
	  area3 down.(!pos_down - 2) down.(!pos_down - 1) ps.(i) < 0L do
	    decr pos_down
	done;
	down.(!pos_down) <- ps.(i);
	incr pos_down;
      );
    done;
    let hull = Array.make (!pos_up + !pos_down - 2) ps.(0) in
      for i = 0 to !pos_up - 1 do
	hull.(i) <- up.(i)
      done;
      for i = !pos_down - 2 downto 1 do
	hull.(!pos_down - 2 - i + !pos_up) <- down.(i)
      done;
      hull
  in
  let maxs = ref 0L in
  let b = ref (0, 0, 0) in
    (*for i = 0 to Array.length hull - 1 do
      let (x, y) = hull.(i) in
	Printf.printf "asd %Ld %Ld\n" x y
    done;*)
    for i = 0 to Array.length hull - 1 do
      let k = ref i in
      let s = ref 0L in
	for j = i + 2 to Array.length hull - 1 do
	  while !k < j - 1 &&
	    Int64.abs (area3 hull.(i) hull.(j) hull.(!k + 1)) > !s
	  do
	    incr k;
	    s := Int64.abs (area3 hull.(i) hull.(j) hull.(!k));
	  done;
	  if !maxs < !s then (
	    maxs := !s;
	    b := (i, j, !k);
	  )
	done;
    done;
    let (i, j, k) = !b in
    let (x1, y1) = hull.(i)
    and (x2, y2) = hull.(j)
    and (x3, y3) = hull.(k) in
      Printf.printf "%Ld %Ld\n" (x2 +| x3 -| x1) (y2 +| y3 -| y1);
      Printf.printf "%Ld %Ld\n" (x3 +| x1 -| x2) (y3 +| y1 -| y2);
      Printf.printf "%Ld %Ld\n" (x1 +| x2 -| x3) (y1 +| y2 -| y3);


The Bitlandians are quite weird people. They do everything differently. They have a different alphabet so they have a different definition for a string.A Bitlandish string is a string made only of characters "0" and "1".BitHaval (the mayor of Bitland) loves to play with Bitlandish strings. He takes some Bitlandish string a, and applies several (possibly zero) operations to it. In one operation the mayor may take any two adjacent characters of a string, define one of them as x and the other one as y. Then he calculates two values p and q: p = x xor y, q = x or y. Then he replaces one of the two taken characters by p and the other one by q.The xor operation means the bitwise excluding OR operation. The or operation is the bitwise OR operation.So for example one operation can transform string 11 to string 10 or to string 01. String 1 cannot be transformed into any other string.You've got two Bitlandish strings a and b. Your task is to check if it is possible for BitHaval to transform string a to string b in several (possibly zero) described operations.

The first line contains Bitlandish string a, the second line contains Bitlandish string b. The strings can have different lengths. It is guaranteed that the given strings only consist of characters "0" and "1". The strings are not empty, their length doesn't exceed 106.

Print "YES" if a can be transformed into b, otherwise print "NO". Please do not print the quotes.

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let sum i j f = fold i j (fun i a -> (f i) + a) 0
let rec forall i j f = (i>j) || ((f i) && forall (i+1) j f)
let rec exists i j f = (i<=j) && ((f i) || exists (i+1) j f)

let () =
  let a = read_line() in
  let b = read_line() in
  let na = String.length a in
  let nb = String.length b in
  let all_zeros s n = forall 0 (n-1) (fun i -> s.[i] = '0') in
  let answer =
    if na != nb then false
    else if all_zeros a na then all_zeros b nb
    else not (all_zeros b nb)
  in
    print_string (if answer then "YES\n" else "NO\n")

(* CF odd game prob 276 B *)
open Filename;;

(* you can use either gr or rdln but not both *)
let use_input_txt = false;;
let fn = (Filename.dirname Filename.current_dir_name);;
let filename = Filename.concat "." "input.txt";;
let cin = if use_input_txt then Scanf.Scanning.open_in filename
	else Scanf.Scanning.stdin;;

let cout = open_out "output.txt";;
let gr () = Scanf.bscanf cin " %d" (fun i -> i);;
let grf () = Scanf.scanf " %f" (fun i -> i);;
let grl () = Scanf.scanf " %Ld" (fun i -> i);;
let rdln() = input_line Pervasives.stdin;;
(* let spli s = Str.split (Str.regexp " ") s;; *)
let printlisti li = List.iter (fun i -> (print_int i; print_string " ")) li;;
let printlisti64 li = List.iter (fun i -> (print_string (Int64.to_string i); print_string " ")) li;;
let printarri ai = Array.iter (fun i -> (print_int i; print_string " ")) ai;;
let debug = false;;

let rec has1 i s =
	if i >= (String.length s) then false
	else if s.[i] = '1' then true
	else has1 (i+1) s;;

let can_convert a b =
	if (String.length a) <> (String.length b) then false
	else
		let ha = has1 0 a in
		let hb = has1 0 b in
		ha = hb;;

let main() =
	let sa = rdln() in
	let sb = rdln() in
	begin
		print_string (if can_convert sa sb then "YES" else "NO")
	end;;

main();;


Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.Initially, there are $$$n$$$ superheroes in avengers team having powers $$$a_1, a_2, \ldots, a_n$$$, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by $$$1$$$. They can do at most $$$m$$$ operations. Also, on a particular superhero at most $$$k$$$ operations can be done.Can you help the avengers team to maximize the average power of their crew?

The first line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 \le n \le 10^{5}$$$, $$$1 \le k \le 10^{5}$$$, $$$1 \le m \le 10^{7}$$$) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^{6}$$$) — the initial powers of the superheroes in the cast of avengers.

Output a single number — the maximum final average power. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$$$.

NoteIn the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by $$$2$$$ each.

let rv f p q = f q p
let fi = float_of_int
let (++),( ** ),fi64,i64 = Int64.(add,mul,to_float,of_int)
;;
Scanf.(Array.(scanf " %d %d %d" @@ fun n k m ->
  let a = init n (fun _ -> scanf " %Ld" @@ fun v -> v) in
  sort (rv compare) a;
  let total = ref 0L in
  init n (fun left ->
    let left = left + 1 in
    total := !total ++ a.(left-1);
    let add = i64 @@ m - (n - left) in
    let limit = (i64 left) ** (i64 k) in
    let add = min limit add in
    let r =
      if add < 0L then 0.
      else (fi64 (!total ++ add)) /. (fi left) in
    (* Printf.printf "%2d : %2Ld :: %2Ld :: %f\n" left add !total r; *)
    r
  )
  |> fold_left max (((fi64 @@ fold_left (++) 0L a)) /. (fi n))
  |> Printf.printf "%.14f"
))

let rv f p q = f q p
let fi = float_of_int
let (++),( ** ),(--),fi64,i64 = Int64.(add,mul,sub,to_float,of_int)
;;
Scanf.(Array.(scanf " %d %Ld %Ld" @@ fun n k m ->
  let a = init n (fun _ -> scanf " %Ld" @@ fun v -> v) in
  sort (rv compare) a;
  let total = ref 0L in
  init n (fun left ->
    let left = left + 1 in
    total := !total ++ a.(left-1);
    let add = m -- (i64 n -- i64 left) in
    let limit = (i64 left) ** k in
    let add = min limit add in
    let r =
      if add < 0L then 0.
      else (fi64 (!total ++ add)) /. (fi left) in
    r
  )
  |> fold_left max 0.
  |> Printf.printf "%.14f"
));;

open Printf
open Scanf

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
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 read_pair _ = bscanf Scanning.stdib " %d %d " (fun x y -> (x,y))
let () =
  let (n,k) = read_pair () in
  let m = read_int() in
  let a = Array.init n read_int in
  let s = Array.make (n+1) 0.0 in

  Array.sort compare a;

  for i=1 to n do
    s.(i) <- s.(i-1) +. (float a.(i-1))
  done;

  let answer = maxf 0 (min m (n-1)) (fun i ->
    (* i are removed *)
    let kept = s.(n) -. s.(i) in
    let j = float (n-i) in (* number left in the set *)
    let ninc = min (float (m-i)) (j *. (float k)) in
    (kept +. ninc) /. j
  ) in

  printf "%f\n" answer

let rv f p q = f q p
let fi = float_of_int;;
Scanf.(Array.(scanf " %d %d %d" @@ fun n k m ->
  let a = init n (fun _ -> scanf " %d" @@ fun v -> v) in
  sort (rv compare) a;
  let total = ref 0 in
  init n (fun left ->
    let left = left + 1 in
    total := !total + a.(left-1);
    let add = m - (n - left) in
    let limit = left * k in
    let add = min limit add in
    let r =
      if add < 0 then 0.
      else (fi (!total + add)) /. (fi left) in
    (* Printf.printf "%2d : %2d :: %2d :: %f\n" left add !total r; *)
    r
  )
  |> fold_left max 0.
  |> print_float
))

let rv f p q = f q p
let fi = float_of_int
let (++),i64f,i64 = Int64.(add,to_float,of_int)
;;
Scanf.(Array.(scanf " %d %d %d" @@ fun n k m ->
  let a = init n (fun _ -> scanf " %Ld" @@ fun v -> v) in
  sort (rv compare) a;
  let total = ref 0L in
  init n (fun left ->
    let left = left + 1 in
    total := !total ++ a.(left-1);
    let add = m - (n - left) in
    let limit = left * k in
    let add = i64 @@ min limit add in
    let r =
      if add < 0L then 0.
      else (i64f (!total ++ add)) /. (fi left) in
    (* Printf.printf "%2d : %2d :: %2d :: %f\n" left add !total r; *)
    r
  )
  |> fold_left max 0.
  |> print_float
));;

let rv f p q = f q p
let fi = float_of_int;;
Scanf.(Array.(scanf " %d %d %d" @@ fun n k m ->
  let a = init n (fun _ -> scanf " %d" @@ fun v -> v) in
  sort (rv compare) a;
  let total = ref 0 in
  init n (fun left ->
    let left = left + 1 in
    total := !total + a.(left-1);
    let add = m - (n - left) in
    let limit = left * k in
    let add = min limit add in
    let r = (fi (!total + add)) /. (fi left) in
    (* Printf.printf "%2d : %2d :: %2d :: %f\n" left add !total r; *)
    r
  )
  |> fold_left max 0.
  |> print_float
))

let rv f p q = f q p
let fi = float_of_int
let (++),i64f,i64 = Int64.(add,to_float,of_int)
;;
Scanf.(Array.(scanf " %d %d %d" @@ fun n k m ->
  let a = init n (fun _ -> scanf " %Ld" @@ fun v -> v) in
  sort (rv compare) a;
  let total = ref 0L in
  init n (fun left ->
    let left = left + 1 in
    total := !total ++ a.(left-1);
    let add = m - (n - left) in
    let limit = left * k in
    let add = i64 @@ min limit add in
    let r =
      if add < 0L then 0.
      else (i64f (!total ++ add)) /. (fi left) in
    (* Printf.printf "%2d : %2d :: %2d :: %f\n" left add !total r; *)
    r
  )
  |> fold_left max ((i64f @@ fold_left (++) 0L a) /. (fi n))
  |> print_float
));;

open Printf
open Scanf

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
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 read_pair _ = bscanf Scanning.stdib " %d %d " (fun x y -> (x,y))
let () =
  let (n,k) = read_pair () in
  let m = read_int() in
  let a = Array.init n read_int in
  let s = Array.make (n+1) 0.0 in

  Array.sort compare a;

  for i=1 to n do
    s.(i) <- s.(i-1) +. (float a.(i-1))
  done;

  let answer = maxf 0 (min m (n-1)) (fun i ->
    let kept = s.(n) -. s.(i) in
    let j = n-i in (* number left in the set *)
    let ninc = min (m-i) (j * k) in
    (kept +. (float ninc)) /. (float (n-i))
  ) in

  printf "%f" answer


Kuznecov likes art, poetry, and music. And strings consisting of lowercase English letters.Recently, Kuznecov has found two strings, $$$a$$$ and $$$b$$$, of lengths $$$n$$$ and $$$m$$$ respectively. They consist of lowercase English letters and no character is contained in both strings. Let another string $$$c$$$ be initially empty. Kuznecov can do the following two types of operations:  Choose any character from the string $$$a$$$, remove it from $$$a$$$, and add it to the end of $$$c$$$.  Choose any character from the string $$$b$$$, remove it from $$$b$$$, and add it to the end of $$$c$$$. But, he can not do more than $$$k$$$ operations of the same type in a row. He must perform operations until either $$$a$$$ or $$$b$$$ becomes empty. What is the lexicographically smallest possible value of $$$c$$$ after he finishes?A string $$$x$$$ is lexicographically smaller than a string $$$y$$$ if and only if one of the following holds: $$$x$$$ is a prefix of $$$y$$$, but $$$x \neq y$$$;  in the first position where $$$x$$$ and $$$y$$$ differ, the string $$$x$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$y$$$.

There are several test cases in the input data. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. This is followed by the test cases description. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1\leq n,m,k \leq 100$$$) — parameters from the statement. The second line of each test case contains the string $$$a$$$ of length $$$n$$$. The third line of each test case contains the string $$$b$$$ of length $$$m$$$.  The strings contain only lowercase English letters. It is guaranteed that no symbol appears in $$$a$$$ and $$$b$$$ simultaneously.

In each test case, output a single string $$$c$$$ — the answer to the problem.

3

6 4 2

aaaaaa

bbbb

5 9 3

caaca

bedededeb

7 7 1

noskill

wxhtzdy

NoteIn the first test case, it is optimal to take two 'a's from the string $$$a$$$ and add them to the string $$$c$$$. Then it is forbidden to take more characters from $$$a$$$, hence one character 'b' from the string $$$b$$$ has to be taken. Following that logic, we end up with $$$c$$$ being 'aabaabaa' when string $$$a$$$ is emptied.In the second test case it is optimal to take as many 'a's from string $$$a$$$ as possible, then take as many 'b's as possible from string $$$b$$$. In the end, we take two 'c's from the string $$$a$$$ emptying it.

(* 2 strings a and b with unique chars *)
(* can remove any char from a/b and in c *)
(* cant do more than k on same string *)
(* keep going until a or b empty *)

(* need to haave smallest string in
   alphabetical e.g abcde < abcdf*)

let read_string () = Scanf.scanf " %s" (fun s -> s);;
let read_int () = Scanf.scanf " %d" (fun x -> x);;

let rec create_char_arr data = function
| 0 -> []
| n -> (String.get data (n-1))::create_char_arr data (n-1)
;;

let rec partition pivot left right = function
| [] -> left, right
| c::unsorted_arr ->
  if ((Char.code c) < (Char.code pivot)) then partition pivot (c::left) (right) unsorted_arr
  else partition pivot left (c::right) unsorted_arr
;;

let rec quickSortFaster = function
| [], sorted_arr -> sorted_arr
| [c], sorted_arr -> c::sorted_arr
| pivot::unsorted_arr, sorted_arr ->
  let left, right = partition pivot [] [] unsorted_arr in
  quickSortFaster (left, pivot::quickSortFaster (right, sorted_arr))
;;

type a_or_b = A_CHOSE | B_CHOSE;;
let rec consume_strings k = function
| _, [], _, _ -> []
| [], _, _, _ -> []
| a_c::a_chars, b_c::b_chars, consec_k, last_choice ->
  if (consec_k == k) then begin
    if (last_choice = A_CHOSE) then b_c::(consume_strings k (a_c::a_chars, b_chars, 1, B_CHOSE))
    else a_c::(consume_strings k (a_chars, (b_c::b_chars), 1, A_CHOSE))
  end
  else begin
    if (Char.code a_c < Char.code b_c) then begin
      if (last_choice = A_CHOSE) then a_c::(consume_strings k (a_chars, (b_c::b_chars), consec_k+1, A_CHOSE))
      else a_c::(consume_strings k (a_chars, (b_c::b_chars), 1, A_CHOSE))
    end
    else if (Char.code a_c > Char.code b_c) then begin
      if (last_choice = A_CHOSE) then b_c::(consume_strings k ((a_c::a_chars), b_chars, 1, B_CHOSE))
      else b_c::(consume_strings k ((a_c::a_chars), b_chars, consec_k+1, B_CHOSE))
    end
    else begin
      if (List.length a_chars < List.length b_chars) then begin
        if (last_choice = A_CHOSE) then a_c::(consume_strings k (a_chars, (b_c::b_chars), consec_k+1, A_CHOSE))
        else a_c::(consume_strings k (a_chars, (b_c::b_chars), 1, A_CHOSE))
      end
      else begin
        if (last_choice = A_CHOSE) then b_c::(consume_strings k ((a_c::a_chars), b_chars, 1, B_CHOSE))
        else b_c::(consume_strings k ((a_c::a_chars), b_chars, consec_k+1, B_CHOSE))
      end
    end
  end

;;

let run_case = function
| () ->
  let n = read_int () in
  let m = read_int () in
  let k = read_int () in
  let a_str = read_string () in
  let a_chars = quickSortFaster ((create_char_arr a_str n), []) in
  let b_str = read_string () in
  let b_chars = quickSortFaster ((create_char_arr b_str m), []) in
  List.iter (fun x -> print_char x ) (consume_strings k (a_chars, b_chars, 0, A_CHOSE));
  print_newline ()
;;

let rec iter_cases = function
| 0 -> 0
| n ->
  run_case ();
  iter_cases (n-1)
;;

let t = read_int () in
iter_cases t;;


In Absurdistan, there are n towns (numbered 1 through n) and m bidirectional railways. There is also an absurdly simple road network — for each pair of different towns x and y, there is a bidirectional road between towns x and y if and only if there is no railway between them. Travelling to a different town using one railway or one road always takes exactly one hour.A train and a bus leave town 1 at the same time. They both have the same destination, town n, and don't make any stops on the way (but they can wait in town n). The train can move only along railways and the bus can move only along roads.You've been asked to plan out routes for the vehicles; each route can use any road/railway multiple times. One of the most important aspects to consider is safety — in order to avoid accidents at railway crossings, the train and the bus must not arrive at the same town (except town n) simultaneously.Under these constraints, what is the minimum number of hours needed for both vehicles to reach town n (the maximum of arrival times of the bus and the train)? Note, that bus and train are not required to arrive to the town n at the same moment of time, but are allowed to do so.

The first line of the input contains two integers n and m (2 ≤ n ≤ 400, 0 ≤ m ≤ n(n - 1) / 2) — the number of towns and the number of railways respectively. Each of the next m lines contains two integers u and v, denoting a railway between towns u and v (1 ≤ u, v ≤ n, u ≠ v). You may assume that there is at most one railway connecting any two towns.

Output one integer — the smallest possible time of the later vehicle's arrival in town n. If it's impossible for at least one of the vehicles to reach town n, output  - 1.

NoteIn the first sample, the train can take the route  and the bus can take the route . Note that they can arrive at town 4 at the same time.In the second sample, Absurdistan is ruled by railwaymen. There are no roads, so there's no way for the bus to reach town 4.

open Printf
open Scanf

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let inf = 1_000_000
let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)

module Vset = Set.Make (struct type t = int*int let compare = compare end)

let () =
  let shortest_path n graph (s,t) =
    let dist = Array.make n inf in
    let parent = Array.make n (-1) in
    let rec dijkstra q =
      if Vset.is_empty q then None else
	let (dv,v) = Vset.min_elt q in
	let q = Vset.remove (dv,v) q in
	if v=t then Some dv else
	  let rec loop wlist q = match wlist with [] -> q
	    | (w,l)::wlist ->
	      let dw = dist.(w) in
	      if (dw = inf || Vset.mem (dw,w) q) && dv+l < dw then
		let ndw = min (dv + l) dw in
		parent.(w) <- v;
		dist.(w) <- ndw;
		let q = Vset.add (ndw,w) (Vset.remove (dw,w) q) in
		loop wlist q
	      else loop wlist q        (* w is completed, or the edge is useless *)
	  in
	  dijkstra (loop graph.(v) q)
    in

    let q = Vset.singleton (0,s) in
    dist.(s) <- 0;
    dijkstra q
  in

  let n = read_int() in
  let m = read_int() in

  let g = Array.make n [] in

  let edges = fold 1 m (fun _ ac ->
    let a = read_int() - 1 in
    let b = read_int() - 1 in
    let (a,b) = (min a b, max a b) in
    (a,b)::ac
  ) [] in

  let add_edge (a,b) =
    g.(a) <- (b,1)::g.(a);
    g.(b) <- (a,1)::g.(b);
  in

  let build_graph edges = List.iter add_edge edges in

  let build_complement_graph edges =
    let mat = Array.make_matrix n n true in
    List.iter (fun (a,b) -> mat.(a).(b) <- false) edges;
    for i=0 to n-2 do
      for j=i+1 to n-1 do
	if mat.(i).(j) then add_edge (i,j)
      done
    done
  in

  if List.exists (fun (a,b) -> (a,b) = (0, n-1)) edges
    then build_complement_graph edges else build_graph edges;

  match shortest_path n g (0,n-1) with
    | None -> printf "-1\n"
    | Some d -> printf "%d\n" d


The rules of Sith Tournament are well known to everyone. n Sith take part in the Tournament. The Tournament starts with the random choice of two Sith who will fight in the first battle. As one of them loses, his place is taken by the next randomly chosen Sith who didn't fight before. Does it need to be said that each battle in the Sith Tournament ends with a death of one of opponents? The Tournament ends when the only Sith remains alive.Jedi Ivan accidentally appeared in the list of the participants in the Sith Tournament. However, his skills in the Light Side of the Force are so strong so he can influence the choice of participants either who start the Tournament or who take the loser's place after each battle. Of course, he won't miss his chance to take advantage of it. Help him to calculate the probability of his victory.

The first line contains a single integer n (1 ≤ n ≤ 18) — the number of participants of the Sith Tournament. Each of the next n lines contains n real numbers, which form a matrix pij (0 ≤ pij ≤ 1). Each its element pij is the probability that the i-th participant defeats the j-th in a duel. The elements on the main diagonal pii are equal to zero. For all different i, j the equality pij + pji = 1 holds. All probabilities are given with no more than six decimal places. Jedi Ivan is the number 1 in the list of the participants.

Output a real number — the probability that Jedi Ivan will stay alive after the Tournament. Absolute or relative error of the answer must not exceed 10 - 6.

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let p = Array.make_matrix n n 0.0 in
  let _ =
    for i = 0 to n - 1 do
      for j = 0 to n - 1 do
	p.(i).(j) <- Scanf.bscanf sb "%f " (fun s -> s)
      done
    done;
  in
  let a = Array.make_matrix n (1 lsl n) 0.0 in
    a.(0).(1) <- 1.0;
    for s = 1 to 1 lsl n - 1 do
      for i = 0 to n - 1 do
	if s land (1 lsl i) <> 0 then (
	  for j = 0 to n - 1 do
	    if i <> j && s land (1 lsl j) <> 0 then (
	      a.(i).(s) <-
		max a.(i).(s) (p.(i).(j) *. a.(i).(s lxor (1 lsl j)) +.
				 p.(j).(i) *. a.(j).(s lxor (1 lsl i)))
	    )
	  done
	)
      done
    done;
    (*for j = 0 to 1 lsl n - 1 do
      for i = 0 to n - 1 do
	Printf.printf "asd %d %d %f\n" j i a.(i).(j)
      done
    done;*)
    let res =
      let res = ref 0.0 in
	for i = 0 to n - 1 do
	  res := max !res a.(i).(1 lsl n - 1)
	done;
	!res
    in
      Printf.printf "%.8f\n" res


Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis!The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options.You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once.You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy.

The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 105)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·105)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer vali (1 ≤ vali ≤ 109)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in.

Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy.

NoteIn the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.

let (+|) = Int64.add
let (-|) = Int64.sub
let ( *| ) = Int64.mul
let ( /| ) = Int64.div

let _ =
  let sb = Scanf.Scanning.stdib in
  let a = Scanf.bscanf sb "%d " (fun s -> s) in
  let b = Scanf.bscanf sb "%d " (fun s -> s) in
  let c = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let us = ref [] in
  let ps = ref [] in
  let _ =
    for i = 1 to m do
      let v = Scanf.bscanf sb "%d " (fun s -> s) in
      let s = Scanf.bscanf sb "%s " (fun s -> s) in
	if s = "USB"
	then us := v :: !us
	else ps := v :: !ps
    done;
  in
  let us = Array.of_list !us in
  let ps = Array.of_list !ps in
  let _ =
    Array.sort compare us;
    Array.sort compare ps;
  in
  let nu =
    if Array.length us >= a
    then a
    else Array.length us
  in
  let np =
    if Array.length ps >= b
    then b
    else Array.length ps
  in
  let nu = ref nu
  and np = ref np in
  let c = ref c in
    while !c > 0 && (!nu < Array.length us || !np < Array.length ps) do
      let vu =
	if !nu < Array.length us
	then us.(!nu)
	else 1000000001
      in
      let vp =
	if !np < Array.length ps
	then ps.(!np)
	else 1000000001
      in
	if vu < vp
	then incr nu
	else incr np;
	decr c
    done;
    let res = ref 0L in
      for i = 0 to !nu - 1 do
	res := !res +| Int64.of_int us.(i)
      done;
      for i = 0 to !np - 1 do
	res := !res +| Int64.of_int ps.(i)
      done;
      Printf.printf "%d %Ld\n" (!nu + !np) !res


Two neighbours, Alan and Bob, live in the city, where there are three buildings only: a cinema, a shop and the house, where they live. The rest is a big asphalt square. Once they went to the cinema, and the film impressed them so deeply, that when they left the cinema, they did not want to stop discussing it.Bob wants to get home, but Alan has to go to the shop first, and only then go home. So, they agreed to cover some distance together discussing the film (their common path might pass through the shop, or they might walk circles around the cinema together), and then to part each other's company and go each his own way. After they part, they will start thinking about their daily pursuits; and even if they meet again, they won't be able to go on with the discussion. Thus, Bob's path will be a continuous curve, having the cinema and the house as its ends. Alan's path — a continuous curve, going through the shop, and having the cinema and the house as its ends.The film ended late, that's why the whole distance covered by Alan should not differ from the shortest one by more than t1, and the distance covered by Bob should not differ from the shortest one by more than t2.Find the maximum distance that Alan and Bob will cover together, discussing the film.

The first line contains two integers: t1, t2 (0 ≤ t1, t2 ≤ 100). The second line contains the cinema's coordinates, the third one — the house's, and the last line — the shop's.  All the coordinates are given in meters, are integer, and do not exceed 100 in absolute magnitude. No two given places are in the same building.

In the only line output one number — the maximum distance that Alan and Bob will cover together, discussing the film. Output the answer accurate to not less than 4 decimal places.

let read_float _ = Scanf.bscanf Scanf.Scanning.stdib " %f" (fun x -> x)
let read _ = let x = read_float 0 in let y = read_float 0 in x,y
let (|>) x f = f x

let eps = 1e-10
let cmp a b =
  if abs_float (a-.b) < eps then
    0
  else
    compare a b
let det (ax,ay) (bx,by) = ax*.by-.ay*.bx
let dot (ax,ay) (bx,by) = ax*.bx+.ay*.by
let add (ax,ay) (bx,by) = ax+.bx, ay+.by
let sub (ax,ay) (bx,by) = ax-.bx, ay-.by
let mul w (x,y) = x*.w, y*.w
let ortho (x,y) = y, -.x
let len x = dot x x |> sqrt
let dist a b = sub a b |> len

let dist (x1,y1) (x2,y2) =
  (x1-.x2)*.(x1-.x2)+.(y1-.y2)*.(y1-.y2) |> sqrt

(* a != b *)
let intersect (a,ra) (b,rb) =
  let dc = dist a b in
  if cmp dc (ra-.rb |> abs_float) <= 0 then (* internal *)
    if ra < rb then [a] else [b]
  else if cmp (ra+.rb) dc < 0 then (* external *)
    []
  else
    let proj = (ra*.ra-.rb*.rb+.dc*.dc)/.(2.0*.dc) |> min ra in
    let hh = ra*.ra-.proj*.proj in
    let h = sqrt hh in
    let foot = add a (mul (proj/.dc) (sub b a)) in
    let dlt = mul (h/.dc) (ortho (sub b a)) in
    [add foot dlt; sub foot dlt]

(* a != b && b != c && c != a *)
let inter3 aa bb cc =
  let test (a,ra) (b,rb) ((cx,cy) as c,rc) =
    let dc = dist a b in
    if cmp dc (ra-.rb |> abs_float) <= 0 then
      intersect (if ra < rb then (a,ra) else (b,rb)) (c,rc) <> []
    else
      intersect (a,ra) (b,rb) |> List.exists (fun (x,y) -> cmp (dist (x,y) (cx,cy)) rc <= 0)
  in
  test aa bb cc || test bb cc aa || test cc aa bb

let () =
  let t1 = read_float 0 in
  let t2 = read_float 0 in
  let a = read 0 in
  let b = read 0 in
  let c = read 0 in
  let lb = dist a b +. t2 in
  let lc = dist a c +. t1 in
  let rec bisect l h =
    if cmp l h >= 0 then
      l
    else
      let m = (l+.h)*.0.5 in
      if inter3 (a,m) (b,lb-.m) (c,lc-.m) then
        bisect m h
      else
        bisect l m
  in
  if cmp (dist a c +. dist c b) lb <= 0 then
    Printf.printf "%f\n" (min lb (dist c b +. lc))
    (*bisect (min (lb+.dist b c) lc) (min lb lc) |> Printf.printf "%f\n"*)
  else
    bisect 0.0 (min lb lc) |> Printf.printf "%f\n"

let read_float _ = Scanf.bscanf Scanf.Scanning.stdib " %f" (fun x -> x)
let read _ = let x = read_float 0 in let y = read_float 0 in x,y
let (|>) x f = f x

let eps = 1e-9
let cmp a b =
  if abs_float (a-.b) < eps then
    0
  else
    compare a b
let det (ax,ay) (bx,by) = ax*.by-.ay*.bx
let dot (ax,ay) (bx,by) = ax*.bx+.ay*.by
let add (ax,ay) (bx,by) = ax+.bx, ay+.by
let sub (ax,ay) (bx,by) = ax-.bx, ay-.by
let mul w (x,y) = x*.w, y*.w
let ortho (x,y) = y, -.x
let len x = dot x x |> sqrt
let dist a b = sub a b |> len

let dist (x1,y1) (x2,y2) =
  (x1-.x2)*.(x1-.x2)+.(y1-.y2)*.(y1-.y2) |> sqrt

(* a != b *)
let intersect (a,ra) (b,rb) =
  let dc = dist a b in
  if cmp dc (ra-.rb |> abs_float) <= 0 then (* internal *)
    if ra < rb then [a] else [b]
  else if cmp (ra+.rb) dc < 0 then (* external *)
    []
  else
    let proj = (ra*.ra-.rb*.rb+.dc*.dc)/.(2.0*.dc) in
    let h = ra*.ra-.proj*.proj |> sqrt in
    let foot = add a (mul (proj/.dc) (sub b a)) in
    let dlt = mul (h/.dc) (ortho (sub b a)) in
    [add foot dlt; sub foot dlt]

(* a != b && b != c && c != a *)
let inter3 aa bb cc =
  let test (a,ra) (b,rb) ((cx,cy) as c,rc) =
    let dc = dist a b in
    if cmp dc (ra-.rb |> abs_float) <= 0 then
      intersect (if ra < rb then (a,ra) else (b,rb)) (c,rc) <> []
    else
      intersect (a,ra) (b,rb) |> List.exists (fun (x,y) -> cmp (dist (x,y) (cx,cy)) rc <= 0)
  in
  test aa bb cc || test bb cc aa || test cc aa bb

let () =
  let t1 = read_float 0 in
  let t2 = read_float 0 in
  let a = read 0 in
  let b = read 0 in
  let c = read 0 in
  let lb = dist a b +. t1 in
  let lc = dist a c +. t2 in
  let rec bisect l h =
    if cmp l h >= 0 then
      l
    else
      let m = (l+.h)/.2.0 in
      if inter3 (a,m) (b,lb-.m) (c,lc-.m) then
        bisect m h
      else
        bisect l m
  in
  if cmp (dist a c +. dist c b) lb <= 0 then
    Printf.printf "%f\n" (min lb (dist c b +. lc))
  else
    bisect 0.0 (min lb lc) |> Printf.printf "%f\n"

let read_float _ = Scanf.bscanf Scanf.Scanning.stdib " %f" (fun x -> x)
let read _ = let x = read_float 0 in let y = read_float 0 in x,y
let (|>) x f = f x

let eps = 1e-8
let cmp a b =
  if abs_float (a-.b) < eps then
    0
  else
    compare a b
let det (ax,ay) (bx,by) = ax*.by-.ay*.bx
let dot (ax,ay) (bx,by) = ax*.bx+.ay*.by
let add (ax,ay) (bx,by) = ax+.bx, ay+.by
let sub (ax,ay) (bx,by) = ax-.bx, ay-.by
let mul w (x,y) = x*.w, y*.w
let ortho (x,y) = y, -.x
let len x = dot x x |> sqrt
let dist a b = sub a b |> len

let dist (x1,y1) (x2,y2) =
  (x1-.x2)*.(x1-.x2)+.(y1-.y2)*.(y1-.y2) |> sqrt

(* a != b *)
let intersect (a,ra) (b,rb) =
  let dc = dist a b in
  if cmp dc (ra-.rb |> abs_float) <= 0 then (* internal *)
    if ra < rb then [a] else [b]
  else if cmp (ra+.rb) dc < 0 then (* external *)
    []
  else
    let proj = (ra*.ra-.rb*.rb+.dc*.dc)/.(2.0*.dc) in
    let h = ra*.ra-.proj*.proj |> sqrt in
    let foot = add a (mul (proj/.dc) (sub b a)) in
    let dlt = mul (h/.dc) (ortho (sub b a)) in
    [add foot dlt; sub foot dlt]

(* a != b && b != c && c != a *)
let inter3 aa bb cc =
  let test (a,ra) (b,rb) ((cx,cy) as c,rc) =
    let dc = dist a b in
    if cmp dc (ra-.rb |> abs_float) <= 0 then
      intersect (if ra < rb then (a,ra) else (b,rb)) (c,rc) <> []
    else
      intersect (a,ra) (b,rb) |> List.exists (fun (x,y) -> cmp (dist (x,y) (cx,cy)) rc <= 0)
  in
  test aa bb cc || test bb cc aa || test cc aa bb

let () =
  (*intersect ((0.0,0.0),3.0) ((3.0,0.0),3.0) |> List.iter (fun (x,y)->*)
    (*Printf.printf "%f %f\n" x y);*)
  (*if inter3 ((0.0,0.0),3.0) ((3.0,0.0),3.0) ((1.5,2.7),0.2) then*)
    (*Printf.printf "meow";*)

  (*exit 0;*)
  let t1 = read_float 0 in
  let t2 = read_float 0 in
  let a = read 0 in
  let b = read 0 in
  let c = read 0 in
  let lb = dist a b +. t1 in
  let lc = dist a c +. t2 in
  let rec bisect l h =
    if cmp l h >= 0 then
      l
    else
      let m = (l+.h)/.2.0 in
      if inter3 (a,m) (b,lb-.m) (c,lc-.m) then
        bisect m h
      else
        bisect l m
  in
  if cmp (dist a c +. dist c b) lb <= 0 then
    Printf.printf "%f\n" (min lb (dist c b +. lc))
  else
    bisect 0.0 (min lb lc) |> Printf.printf "%f\n"

let read_float _ = Scanf.bscanf Scanf.Scanning.stdib " %f" (fun x -> x)
let read _ = let x = read_float 0 in let y = read_float 0 in x,y
let (|>) x f = f x

let eps = 1e-10
let cmp a b =
  if abs_float (a-.b) < eps then
    0
  else
    compare a b
let det (ax,ay) (bx,by) = ax*.by-.ay*.bx
let dot (ax,ay) (bx,by) = ax*.bx+.ay*.by
let add (ax,ay) (bx,by) = ax+.bx, ay+.by
let sub (ax,ay) (bx,by) = ax-.bx, ay-.by
let mul w (x,y) = x*.w, y*.w
let ortho (x,y) = y, -.x
let len x = dot x x |> sqrt
let dist a b = sub a b |> len

let dist (x1,y1) (x2,y2) =
  (x1-.x2)*.(x1-.x2)+.(y1-.y2)*.(y1-.y2) |> sqrt

(* a != b *)
let intersect (a,ra) (b,rb) =
  let dc = dist a b in
  if cmp dc (ra-.rb |> abs_float) <= 0 then (* internal *)
    if ra < rb then [a] else [b]
  else
    let proj = (ra*.ra-.rb*.rb+.dc*.dc)/.(2.0*.dc) in
    let hh = ra*.ra-.proj*.proj in
    if hh < 0.0 then (* external *)
      []
    else
      let h = sqrt hh in
      let foot = add a (mul (proj/.dc) (sub b a)) in
      let dlt = mul (h/.dc) (ortho (sub b a)) in
      [add foot dlt; sub foot dlt]

(* a != b && b != c && c != a *)
let inter3 aa bb cc =
  let test (a,ra) (b,rb) ((cx,cy) as c,rc) =
    let dc = dist a b in
    if cmp dc (ra-.rb |> abs_float) <= 0 then
      intersect (if ra < rb then (a,ra) else (b,rb)) (c,rc) <> []
    else
      intersect (a,ra) (b,rb) |> List.exists (fun (x,y) -> cmp (dist (x,y) (cx,cy)) rc <= 0)
  in
  test aa bb cc || test bb cc aa || test cc aa bb

let () =
  let t1 = read_float 0 in
  let t2 = read_float 0 in
  let a = read 0 in
  let b = read 0 in
  let c = read 0 in
  let lb = dist a b +. t2 in
  let lc = dist a c +. t1 in
  let rec bisect l h =
    if cmp l h >= 0 then
      l
    else
      let m = (l+.h)*.0.5 in
      if inter3 (a,m) (b,lb-.m) (c,lc-.m) then
        bisect m h
      else
        bisect l m
  in
  if cmp (dist a c +. dist c b) lb <= 0 then
    Printf.printf "%f\n" (min lb (dist c b +. lc))
    (*bisect (min (lb+.dist b c) lc) (min lb lc) |> Printf.printf "%f\n"*)
  else
    bisect 0.0 (min lb lc) |> Printf.printf "%f\n"

let read_float _ = Scanf.bscanf Scanf.Scanning.stdib " %f" (fun x -> x)
let read _ = let x = read_float 0 in let y = read_float 0 in x,y
let (|>) x f = f x

let eps = 1e-9
let cmp a b =
  if abs_float (a-.b) < eps then
    0
  else
    compare a b
let det (ax,ay) (bx,by) = ax*.by-.ay*.bx
let dot (ax,ay) (bx,by) = ax*.bx+.ay*.by
let add (ax,ay) (bx,by) = ax+.bx, ay+.by
let sub (ax,ay) (bx,by) = ax-.bx, ay-.by
let mul w (x,y) = x*.w, y*.w
let ortho (x,y) = y, -.x
let len x = dot x x |> sqrt
let dist a b = sub a b |> len

let dist (x1,y1) (x2,y2) =
  (x1-.x2)*.(x1-.x2)+.(y1-.y2)*.(y1-.y2) |> sqrt

(* a != b *)
let intersect (a,ra) (b,rb) =
  let dc = dist a b in
  if cmp dc (ra-.rb |> abs_float) <= 0 then (* internal *)
    if ra < rb then [a] else [b]
  else if cmp (ra+.rb) dc < 0 then (* external *)
    []
  else
    let proj = (ra*.ra-.rb*.rb+.dc*.dc)/.(2.0*.dc) in
    let h = ra*.ra-.proj*.proj |> sqrt in
    let foot = add a (mul (proj/.dc) (sub b a)) in
    let dlt = mul (h/.dc) (ortho (sub b a)) in
    [add foot dlt; sub foot dlt]

(* a != b && b != c && c != a *)
let inter3 aa bb cc =
  let test (a,ra) (b,rb) ((cx,cy) as c,rc) =
    let dc = dist a b in
    if cmp dc (ra-.rb |> abs_float) <= 0 then
      intersect (if ra < rb then (a,ra) else (b,rb)) (c,rc) <> []
    else
      intersect (a,ra) (b,rb) |> List.exists (fun (x,y) -> cmp (dist (x,y) (cx,cy)) rc <= 0)
  in
  test aa bb cc || test bb cc aa || test cc aa bb

let () =
  let t1 = read_float 0 in
  let t2 = read_float 0 in
  let a = read 0 in
  let b = read 0 in
  let c = read 0 in
  let lb = dist a b +. t2 in
  let lc = dist a c +. t1 in
  let rec bisect l h =
    if cmp l h >= 0 then
      l
    else
      let m = (l+.h)/.2.0 in
      if inter3 (a,m) (b,lb-.m) (c,lc-.m) then
        bisect m h
      else
        bisect l m
  in
  if cmp (dist a c +. dist c b) lb <= 0 then
    Printf.printf "%f\n" (min lb (dist c b +. lc))
  else
    bisect 0.0 (min lb lc) |> Printf.printf "%f\n"


For some binary string $$$s$$$ (i.e. each character $$$s_i$$$ is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length $$$2$$$ were written. For each pair (substring of length $$$2$$$), the number of '1' (ones) in it was calculated.You are given three numbers:  $$$n_0$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$0$$$;  $$$n_1$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$1$$$;  $$$n_2$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$2$$$. For example, for the string $$$s=$$$"1110011110", the following substrings would be written: "11", "11", "10", "00", "01", "11", "11", "11", "10". Thus, $$$n_0=1$$$, $$$n_1=3$$$, $$$n_2=5$$$.Your task is to restore any suitable binary string $$$s$$$ from the given values $$$n_0, n_1, n_2$$$. It is guaranteed that at least one of the numbers $$$n_0, n_1, n_2$$$ is greater than $$$0$$$. Also, it is guaranteed that a solution exists.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the input. Then test cases follow. Each test case consists of one line which contains three integers $$$n_0, n_1, n_2$$$ ($$$0 \le n_0, n_1, n_2 \le 100$$$; $$$n_0 + n_1 + n_2 > 0$$$). It is guaranteed that the answer for given $$$n_0, n_1, n_2$$$ exists.

Print $$$t$$$ lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them.

let scan_int () = Scanf.scanf " %d" (fun x -> x) ;;

open Printf

let init (x : int) (f : int -> 'a) : 'a list =
  let rec build k =
    if k = x then []
    else (f k) :: build (k + 1) in
  build 0
;;

let run () =
  let n0 = scan_int () in
  let n1 = scan_int () in
  let n2 = scan_int () in
  let lst =
    if (n0 = 0 && n2 = 0) then begin
      let one = 10 :: (init ((n1 - 1) / 2) (fun _x -> 10)) in
      if (n1 mod 2 = 0) then 0 :: one
      else one
    end
    else if n1 = 0 && n2 = 0 then init (n0 + 1) (fun _x -> 0)
    else if (n0 = 0 && n1 = 0) then init (n2 + 1) (fun _x -> 1)
    else begin
      let zer = init (n0+1) (fun _x -> 0) in
      let two = init (n2+1) (fun _x -> 1) in
      if n0 > 0 then
        let one = init ((n1 - 1) / 2) (fun _x -> 10) in
        if n1 mod 2 = 0 then 1 :: (zer @ (one @ two))
        else (zer @ (one @ two))
      else
        let one = init (n1 / 2) (fun _x -> 10) in
        if n1 mod 2 = 1 then 0 :: (one @ two)
        else one @ two
    end in
  List.iter (printf "%d") lst;
  print_endline ""
;;

let () =
  let t = scan_int () in
  for i = 1 to t do
    run ()
  done
;;

(* Codeforces 1352 D Alice, Bob and Candies comp   *)

(* you can use either gr or rdln but not both *)
let gr () = Scanf.scanf " %d" (fun i -> i);;
let grf () = Scanf.scanf " %f" (fun i -> i);;
let grl () = Scanf.scanf " %Ld" (fun i -> i);;
let rdln() = input_line stdin;;
(* let spli s = Str.split (Str.regexp " ") s;; *)
let printlisti li = List.iter (fun i -> (print_int i; print_string " ")) li;;
let printarri ai = Array.iter (fun i -> (print_int i; print_string " ")) ai;;
let debug = false;;

(* reading reversed list *)
let rec readlist n acc = match n with
	| 0 -> acc
	| _ -> readlist (n -1) (gr() :: acc);;
let printlisti li = List.iter (fun i -> (print_int i; print_string " ")) li;;
let printlists ls = List.iter (fun s -> (print_string s; print_string " ")) ls;;

(* ============= above library below custom ========== *)

let rec bs_rec i bts n0 n1 n2 =
	let ii = (i + 1) in
	if n0 > 0 then
		let _ = (Bytes.set bts i '0') in
		bs_rec ii bts (n0-1) n1 n2
	else if n1 > 0 then
		let ch = if (n1 mod 2) == 1 then '1' else '0' in
		let _ = (Bytes.set bts i ch) in
		bs_rec ii bts 0 (n1-1) n2
	else if n2 > 0 then
		let _ = (Bytes.set bts i '1') in
		bs_rec ii bts 0 0 (n2 - 1)
	else
		();;

let binary_string bts n0 n1 n2 n =
	if n0==0 && n1==0 then Bytes.fill bts 0 n '1'
	else if n1==0 && n2==0 then Bytes.fill bts 0 n '0'
	else
  	let _ = if (n1 mod 2) == 0
		  then
				begin
					Bytes.set bts 0 '1';
					Bytes.set bts 1 '0'
				end
			else
				(Bytes.set bts 0 '0') in
  	let i0,nn1 = if (n1 mod 2) == 0 then 2,(n1 - 1) else 1,n1 in
		bs_rec i0 bts n0 nn1 n2;;

let do_one () =
    let n0 = gr () in
    let n1 = gr () in
    let n2 = gr () in
		let n = n0 + n1 + n2 + 1 in
		let bts = Bytes.create n in
    let _ = binary_string bts n0 n1 n2 n in
		let bs = Bytes.to_string bts in
  	Printf.printf "%s\n" bs;;

let do_all t =
    for i = 1 to t do
        do_one ()
    done;;

let main() =
	let t = gr () in
	do_all t;;

main();;

let scan_int () = Scanf.scanf " %d" (fun x -> x) ;;

open Printf

let init (x : int) (f : int -> 'a) : 'a list =
  let rec build k =
    if k = x then []
    else (f k) :: build (k + 1) in
  build 0
;;

let run () =
  let n0 = scan_int () in
  let n1 = scan_int () in
  let n2 = scan_int () in
  let lst =
    if (n0 = 0 && n2 = 0) then begin
      let one = 10 :: (init ((n1 - 1) / 2) (fun _x -> 10)) in
      if (n1 mod 2 = 0) then 0 :: one
      else one
    end
    else if n1 = 0 && n2 = 0 then init (n0 + 1) (fun _x -> 0)
    else if (n0 = 0 && n1 = 0) then init (n2 + 1) (fun _x -> 1)
    else begin
      let zer = init (n0+1) (fun _x -> 0) in
      let two = init (n2+1) (fun _x -> 1) in
      if n0 > 0 then
        let one = init ((n1 - 1) / 2) (fun _x -> 10) in
        if n1 mod 2 = 0 then 1 :: (zer @ (one @ two))
        else (zer @ (one @ two))
      else
        let one = init (n1 / 2) (fun _x -> 10) in
        if n1 mod 2 = 0 then 0 :: (one @ two)
        else one @ two
    end in
  List.iter (printf "%d") lst;
  print_endline ""
;;

let () =
  let t = scan_int () in
  for i = 1 to t do
    run ()
  done
;;


You are given a string s and should process m queries. Each query is described by two 1-based indices li, ri and integer ki. It means that you should cyclically shift the substring s[li... ri] ki times. The queries should be processed one after another in the order they are given.One operation of a cyclic shift (rotation) is equivalent to moving the last character to the position of the first character and shifting all other characters one position to the right.For example, if the string s is abacaba and the query is l1 = 3, r1 = 6, k1 = 1 then the answer is abbacaa. If after that we would process the query l2 = 1, r2 = 4, k2 = 2 then we would get the string baabcaa.

The first line of the input contains the string s (1 ≤ |s| ≤ 10 000) in its initial state, where |s| stands for the length of s. It contains only lowercase English letters. Second line contains a single integer m (1 ≤ m ≤ 300) — the number of queries. The i-th of the next m lines contains three integers li, ri and ki (1 ≤ li ≤ ri ≤ |s|, 1 ≤ ki ≤ 1 000 000) — the description of the i-th query.

Print the resulting string s after processing all m queries.

NoteThe sample is described in problem statement.

open Array;;

let string_to_list s =
  let rec explode i l =
    if ( i < 0 ) then l
    else explode (i-1) (s.[i] :: l)
  in explode ( String.length s - 1) [];;

let licz x a b k =
  let y = init ( Array.length x ) (fun i -> x.(i) )
  in
    for i = a-1 to b-1 do
      set y i x.( ( ( (i-a+1-k) mod (b-a+1) ) + (b-a+1) ) mod (b-a+1) + a-1 )
    done;
    y;;

let case x =
  Scanf.bscanf Scanf.Scanning.stdin "%d %d %d\n" ( licz x );;

let x = Array.of_list ( string_to_list ( read_line() ) );;

let rec repeat x f y =
  if x > 0 then
  begin
    let z = f y in
    repeat (x-1) f z
  end
  else y;;

iter print_char ( repeat ( read_int() ) case x );;

let _ =
  let sb = Scanf.Scanning.stdib in
  let s = Scanf.bscanf sb "%s " (fun s -> s) in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
    for i = 1 to n do
      let l = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let r = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let k = Scanf.bscanf sb "%d " (fun s -> s) in
      let len = r - l + 1 in
      let s2 = String.copy s in
	for j = l to r do
	  let p = ((j - l - k) mod len + len) mod len + l in
	    s.[j] <- s2.[p]
	done;
    done;
    Printf.printf "%s\n" s