Alice's hair is growing by leaps and bounds. Maybe the cause of it is the excess of vitamins, or maybe it is some black magic...To prevent this, Alice decided to go to the hairdresser. She wants for her hair length to be at most $$$l$$$ centimeters after haircut, where $$$l$$$ is her favorite number. Suppose, that the Alice's head is a straight line on which $$$n$$$ hairlines grow. Let's number them from $$$1$$$ to $$$n$$$. With one swing of the scissors the hairdresser can shorten all hairlines on any segment to the length $$$l$$$, given that all hairlines on that segment had length strictly greater than $$$l$$$. The hairdresser wants to complete his job as fast as possible, so he will make the least possible number of swings of scissors, since each swing of scissors takes one second.Alice hasn't decided yet when she would go to the hairdresser, so she asked you to calculate how much time the haircut would take depending on the time she would go to the hairdresser. In particular, you need to process queries of two types:  $$$0$$$ — Alice asks how much time the haircut would take if she would go to the hairdresser now.  $$$1$$$ $$$p$$$ $$$d$$$ — $$$p$$$-th hairline grows by $$$d$$$ centimeters. Note, that in the request $$$0$$$ Alice is interested in hypothetical scenario of taking a haircut now, so no hairlines change their length.

The first line contains three integers $$$n$$$, $$$m$$$ and $$$l$$$ ($$$1 \le n, m \le 100\,000$$$, $$$1 \le l \le 10^9$$$) — the number of hairlines, the number of requests and the favorite number of Alice. The second line contains $$$n$$$ integers $$$a_i$$$ ($$$1 \le a_i \le 10^9$$$) — the initial lengths of all hairlines of Alice. Each of the following $$$m$$$ lines contains a request in the format described in the statement. The request description starts with an integer $$$t_i$$$. If $$$t_i = 0$$$, then you need to find the time the haircut would take. Otherwise, $$$t_i = 1$$$ and in this moment one hairline grows. The rest of the line than contains two more integers: $$$p_i$$$ and $$$d_i$$$ ($$$1 \le p_i \le n$$$, $$$1 \le d_i \le 10^9$$$) — the number of the hairline and the length it grows by.

For each query of type $$$0$$$ print the time the haircut would take.

NoteConsider the first example:  Initially lengths of hairlines are equal to $$$1, 2, 3, 4$$$ and only $$$4$$$-th hairline is longer $$$l=3$$$, and hairdresser can cut it in $$$1$$$ second.  Then Alice's second hairline grows, the lengths of hairlines are now equal to $$$1, 5, 3, 4$$$  Now haircut takes two seonds: two swings are required: for the $$$4$$$-th hairline and for the $$$2$$$-nd.  Then Alice's first hairline grows, the lengths of hairlines are now equal to $$$4, 5, 3, 4$$$  The haircut still takes two seconds: with one swing hairdresser can cut $$$4$$$-th hairline and with one more swing cut the segment from $$$1$$$-st to $$$2$$$-nd hairline.  Then Alice's third hairline grows, the lengths of hairlines are now equal to $$$4, 5, 4, 4$$$  Now haircut takes only one second: with one swing it is possible to cut the segment from $$$1$$$-st hairline to the $$$4$$$-th.

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,m,l = get_3_i64 0 in
  let a = input_i64_array n in
  let c = ref 0L in
  fold_left (fun u v ->
    if u<=l && v>l then (
      c += 1L;
    ); v
  ) (-1L) a |> ignore;
  rep 1L m (fun _ ->
    match get_i64 0 with
    | 0L -> (
      printf "%Ld\n" !c
    )
    | 1L -> (
      let i,w = get_2_i64 0 in let i = i - 1L in
      let p,q = a.(i32 i),a.(i32 i)+w in
      if p<=l && q>l then (
        if (i>=1L && a.(i32 i-$1) > l) && (i<=n-2L && a.(i32 i+$1) > l) then (
          c -= 1L;
        ) else if (i>=1L && a.(i32 i-$1) > l) || (i<=n-2L && a.(i32 i+$1) > l) then (
          c += 0L;
        ) else c += 1L;
      );
      a.(i32 i) <- q;
    )
    | _ -> fail 0
  )

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,m,l = get_3_i64 0 in
  let a = input_i64_array n in
  let c = ref 0L in
  iter (fun v -> if v > l then c += 1L) a;
  rep 1L m (fun _ ->
    match get_i64 0 with
    | 0L -> (
      printf "%Ld\n" !c
    )
    | 1L -> (
      let i,w = get_2_i64 0 in let i = i - 1L in
      let p,q = a.(i32 i),a.(i32 i)+w in
      if p<=l && q>l then (
        if (i>=1L && a.(i32 i-$1) > l) && (i<=n-2L && a.(i32 i+$1) > l) then (
          c -= 1L;
        ) else if (i>=1L && a.(i32 i-$1) > l) || (i<=n-2L && a.(i32 i+$1) > l) then (
          c += 0L;
        ) else c += 1L;
      );
      a.(i32 i) <- q;
    )
    | _ -> fail 0
  )


You are given two positive integers $$$n$$$ and $$$k$$$. Print the $$$k$$$-th positive integer that is not divisible by $$$n$$$.For example, if $$$n=3$$$, and $$$k=7$$$, then all numbers that are not divisible by $$$3$$$ are: $$$1, 2, 4, 5, 7, 8, 10, 11, 13 \dots$$$. The $$$7$$$-th number among them is $$$10$$$.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the input. Next, $$$t$$$ test cases are given, one per line. Each test case is two positive integers $$$n$$$ ($$$2 \le n \le 10^9$$$) and $$$k$$$ ($$$1 \le k \le 10^9$$$).

For each test case print the $$$k$$$-th positive integer that is not divisible by $$$n$$$.

(* Codeforces 1352 C K-th Not Divisible by n 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;;

let rec kth_not_div n k =
	let n1 = Int64.pred n in
	let o = Int64.rem k n1 in
	let d = Int64.div k n1 in
	let oo = if (Int64.compare o Int64.zero) == 0 then Int64.minus_one else o in
	let ret = Int64.add (Int64.mul n d) oo in
	ret;;

let do_one () =
    let n = grl () in
    let k = grl () in
		let kth = kth_not_div n k in
  	Printf.printf "%Ld\n" kth;;

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 open Int64 in
  let n = scan_int () |> of_int in
  let k = scan_int () |> of_int in
  let check x =
    sub x (div x n) >= k in
  let rec f lo hi ans =
    if lo > hi then ans
    else begin
      let md = div (add lo hi) 2L in
      ifprintf stdout "%Ld %Ld %Ld %B\n" lo hi md (check md);
      if check md then f lo (sub md 1L) md
      else f (add md 1L) hi ans
    end in
  printf "%Ld\n" (f 1L 3000000000L max_int)
;;

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

(* Codeforces 1352 C K-th Not Divisible by n 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;;

let rec kth_not_div n k =
	let o = k mod (n - 1) in
	let d = k / (n - 1) in
	let ret = (n * d) + (if o==0 then (-1) else o) in
	(* let _ = Printf.printf "n,k,o,d,ret = %d %d %d %d %d\n" n k o d ret in *)
	ret;;

let do_one () =
    let n = gr () in
    let k = gr () in
		let kth = kth_not_div n k in
  	Printf.printf "%d\n" kth;;

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

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

main();;


n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.

The first line contains two integers: n and k (2 ≤ n ≤ 500, 2 ≤ k ≤ 1012) — the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) — powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.

Output a single integer — power of the winner.

NoteGames in the second sample:3 plays with 1. 3 wins. 1 goes to the end of the line.3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.

open Printf
open Scanf

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

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_long _ = bscanf Scanning.stdib " %Ld " (fun x -> x)

let () =
  let n = read_int () in
  let k = read_long () in
  let k = if k >= long n then n-1 else short k in
  let power = Array.init n read_int in

  let run_length i =
    let rec run_starting_at pow j ac =
      if j = n || pow < power.(j) then ac
      else run_starting_at pow (j+1) (ac+1)
    in

    let best_before = maxf 0 i (fun j -> power.(j)) in
    if best_before > power.(i) then 0 else
      (if i > 0 then 1 else 0) + run_starting_at power.(i) (i+1) 0
  in

  let rec scan i =
    let rl = run_length i in
    if rl >= k || rl >= n-i then i else scan (i+1)
  in

  printf "%d\n" (power.(scan 0))


Scrooge McDuck keeps his most treasured savings in a home safe with a combination lock. Each time he wants to put there the treasures that he's earned fair and square, he has to open the lock.  The combination lock is represented by n rotating disks with digits from 0 to 9 written on them. Scrooge McDuck has to turn some disks so that the combination of digits on the disks forms a secret combination. In one move, he can rotate one disk one digit forwards or backwards. In particular, in one move he can go from digit 0 to digit 9 and vice versa. What minimum number of actions does he need for that?

The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of disks on the combination lock. The second line contains a string of n digits — the original state of the disks. The third line contains a string of n digits — Scrooge McDuck's combination that opens the lock.

Print a single integer — the minimum number of moves Scrooge McDuck needs to open the lock.

NoteIn the sample he needs 13 moves:  1 disk:   2 disk:   3 disk:   4 disk:   5 disk:

let input () =
    let n = Scanf.scanf "%d " (fun n -> n) in
    let start = Array.init n (fun i -> Scanf.scanf "%c " (fun i -> i)) in
    let goal = Array.init n (fun i -> Scanf.scanf "%c " (fun i -> i)) in
    (n, start, goal)

let solve (n, start, goal) =
    let rec loop i count =
        if i < n
        then
            let s = Pervasives.int_of_char (start.(i)) in
            let g = Pervasives.int_of_char (goal.(i)) in
            let min = Pervasives.min s g in
            let max = Pervasives.max s g in
            let try_one = max - min in
            let try_two = min + 10 - max in
            let r = Pervasives.min try_one try_two in
            loop (i + 1) (count + r)
        else
            count
    in loop 0 0

let print result = Printf.printf "%d\n" result
let () = print (solve (input ()))

let split x = x |> Str.split (Str.regexp "") |> List.map int_of_string and
    minimum = List.fold_left min 10 and
    sum = List.fold_left (+) 0 in
let  _ = read_line () and
    fromComb = read_line () |> split and
    toComb = read_line () |> split
in
List.map2 (fun x y -> List.map abs [x-y; y-x; 10+x-y; 10+y-x] |> minimum) fromComb toComb |> sum |> print_int

let dist a b =
	min (10+b-a) (min (abs (10+a-b)) (abs (a-b)))

let _ =

	let (n,ini,fin) = Scanf.scanf "%d %s %s" (fun i j k -> (i,j,k))
	and res = ref 0 and f mot i = int_of_char (mot.[i]) in
		for i=0 to n-1 do
			res := !res + dist (f ini i) (f fin i);
		done;
	Printf.printf "%d\n" (!res)

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

let n = read_int();;
let a = read_line();;
let b = read_line();;
let rec solve index =
    if index >= String.length a || index >= String.length b then
        0
    else (
        let d = int_of_char a.[index] - (int_of_char b.[index]) |> abs in
        let dis = min d (10 - d) in
        dis + (solve (index + 1))
    );;
solve 0 |> print_int;;


Berland has n cities, some of them are connected by bidirectional roads. For each road we know whether it is asphalted or not.The King of Berland Valera II wants to asphalt all roads of Berland, for that he gathered a group of workers. Every day Valera chooses exactly one city and orders the crew to asphalt all roads that come from the city. The valiant crew fulfilled the King's order in a day, then workers went home.Unfortunately, not everything is as great as Valera II would like. The main part of the group were gastarbeiters — illegal immigrants who are enthusiastic but not exactly good at understanding orders in Berlandian. Therefore, having received orders to asphalt the roads coming from some of the city, the group asphalted all non-asphalted roads coming from the city, and vice versa, took the asphalt from the roads that had it.Upon learning of this progress, Valera II was very upset, but since it was too late to change anything, he asked you to make a program that determines whether you can in some way asphalt Berlandian roads in at most n days. Help the king.

The first line contains two space-separated integers n, m  — the number of cities and roads in Berland, correspondingly. Next m lines contain the descriptions of roads in Berland: the i-th line contains three space-separated integers ai, bi, ci (1 ≤ ai, bi ≤ n; ai ≠ bi; 0 ≤ ci ≤ 1). The first two integers (ai, bi) are indexes of the cities that are connected by the i-th road, the third integer (ci) equals 1, if the road was initially asphalted, and 0 otherwise.  Consider the cities in Berland indexed from 1 to n, and the roads indexed from 1 to m. It is guaranteed that between two Berlandian cities there is not more than one road.

In the first line print a single integer x (0 ≤ x ≤ n) — the number of days needed to asphalt all roads. In the second line print x space-separated integers — the indexes of the cities to send the workers to. Print the cities in the order, in which Valera send the workers to asphalt roads. If there are multiple solutions, print any of them.  If there's no way to asphalt all roads, print "Impossible" (without the quotes).

let (@@) f x = f x
let (|>) x f = f x
let (%) f g x = f @@ g x

let () =
  Scanf.scanf "%d %d" @@ fun n m ->
  let g = Array.make (n + n * n) [] in
  let add_edges = List.iter @@ fun (a, b) -> g.(a) <- b :: g.(a); g.(b) <- a :: g.(b) in
  let idx a b = n + n * (min a b) + (max a b) in
  for i = 1 to m do
    ignore i;
    Scanf.scanf " %d %d %d" @@ fun a b even ->
      let a = a - 1 and b = b - 1 in
      add_edges @@ if even = 1 then [(a, idx a b); (idx a b, b)] else [(a, b)]
  done;
  let colors = Array.make (n + n * n) None in
  let rec dfs c v = match colors.(v) with
    | Some c' when c <> c' -> raise Not_found
    | Some _ -> ()
    | None -> colors.(v) <- Some c; List.iter (dfs @@ not c) g.(v)
  in
  let get o = match o with Some c -> c | None -> assert false in
  try
    ArrayLabels.iteri colors ~f:(fun v c -> if c = None then dfs false v);
    List.(
      Array.sub colors 0 n |> Array.to_list
      |> mapi (fun i b -> i, b) |> filter (get % snd) |> map (string_of_int % succ % fst) |> fun l ->
        Printf.printf "%d\n%s\n" (length l) @@ String.concat " " l)
  with Not_found -> Printf.printf "Impossible\n"

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

let (%) f g x = f @@ g x

module Option = struct
  let get = function
    | Some x -> x
    | None -> assert false
end

let () =
  let dsf n = object(self)
    val parent = Array.init n (fun x -> x)
    val rank = Array.make n 1
    method get v =
      if v = parent.(v) then v
      else begin
        parent.(v) <- self#get parent.(v);
        parent.(v)
      end
    method merge a b =
      let a, b = (fun a b -> max a b, min a b) (self#get a) (self#get b) in
      if a <> b then begin
        parent.(b) <- a;
        if rank.(a) = rank.(b) then rank.(a) <- rank.(a) + 1
      end
  end in
  let bicolor g =
    let n = Array.length g in
    let colors = Array.make n None in
    let rec dfs color v =
      match colors.(v) with
        | Some c when c <> color -> raise Not_found
        | Some _ -> ()
        | None ->
          colors.(v) <- Some color;
          List.iter (dfs @@ not color) g.(v)
    in
    try
      for v = 0 to n - 1 do
        if colors.(v) = None then dfs false v
      done;
      Some (Array.map Option.get colors)
    with Not_found -> None
  in
  let add_edges g = List.iter @@ fun (a, b) -> g.(a) <- b :: g.(a) in
  let condense g : int list array * (int -> int) =
    let n = Array.length g in
    let dsf = dsf n in
    let iter_g f = ArrayLabels.iteri g ~f:(fun a -> List.iter @@ f a) in
    iter_g (fun a (b, asphalted) -> if asphalted then dsf#merge a b);
    let index = Array.make n None in
    let size = ref 0 in
    for i = 0 to n - 1 do
      let root = dsf#get i in
      begin match index.(root) with
        | Some _ -> ()
        | None -> incr size; index.(root) <- Some (!size - 1)
      end;
      index.(i) <- index.(root)
    done;
    let g' = Array.make n [] in
    let get v = Option.get index.(v) in
    iter_g (fun a (b, asphalted) ->
      let a, b = get a, get b in
      if not asphalted then add_edges g' [a, b; b, a]);
    g', get
  in
  let solve g : (int -> bool) option =
    let g', get = condense g in
    match bicolor g' with
      | Some colors -> Some (Array.get colors % get)
      | None -> None
  in
  let output n switched =
    let answer = ref [] in
    for i = 0 to n - 1 do
      if switched i then answer := (i + 1) :: !answer
    done;
    List.(Printf.sprintf "%d\n%s"
      (length !answer)
      (String.concat " " @@ map string_of_int @@ rev !answer))
  in
  let input () =
    Scanf.scanf "%d %d" @@ fun n m ->
    let g = Array.make n [] in
    let add_road a b asphalted = add_edges g [a, (b, asphalted); b, (a, asphalted)] in
    for i = 1 to m do
      ignore i; (* to get rid of unused variable warning *)
      Scanf.scanf " %d %d %d" @@ fun a b asphalted ->
        add_road (a - 1) (b - 1) (asphalted = 1)
    done;
    g
  in
  input ()
  |> fun g ->
    Printf.printf "%s\n" @@
      match solve g with
        | None -> "Impossible"
        | Some f -> output (Array.length g) f

let (@@) f x = f x
let (|>) x f = f x
let (%) f g x = f @@ g x

let () =
  Scanf.scanf "%d %d" @@ fun n m ->
  let g = Array.make n [] in
  let add_edge a b c = g.(a) <- (b, c) :: g.(a); g.(b) <- (a, c) :: g.(b) in
  for i = 1 to m do
    ignore i;
    Scanf.scanf " %d %d %d" @@ fun a b c ->
      add_edge (a - 1) (b - 1) c
  done;
  let colors = Array.make n ~-1 in
  let rec dfs c v = match colors.(v) with
    | -1 -> colors.(v) <- c; List.iter (fun (v, c') -> dfs (1 land (lnot @@ c lxor c')) v) g.(v)
    | c' when c = c' -> ()
    | _ -> raise Not_found
  in
  try
    Array.iteri (fun v _ -> try dfs 1 v with Not_found -> dfs 0 v) colors;
    Array.fold_left (fun (acc, i) c -> (if c = 1 then (string_of_int i) :: acc else acc), i + 1) ([], 1) colors
    |> fun (l, _) ->
    Printf.printf "%d\n%s\n" (List.length l) @@ String.concat " " l
  with Not_found -> Printf.printf "Impossible\n"

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

type road_graph = (int * bool) list array
type dep_graph = int list array
type tout_comp = int -> int

let (%) f g x = f @@ g x

let () =
  let make_dfs g =
    let marked, mark =
      let marked = Array.(make (length g) false) in
      (fun v -> marked.(v)),
      (fun v -> marked.(v) <- true)
    in
    fun fout v ->
    let rec dfs v =
      if not (marked v) then begin
        mark v;
        List.iter dfs g.(v);
        fout v
      end
    in
    dfs v
  in
  let add_edge g a b = g.(a) <- b :: g.(a) in
  let transpose g =
    let g' = Array.(make (length g) []) in
    ArrayLabels.iteri g ~f:(fun a -> List.iter @@ fun b -> add_edge g' b a);
    g'
  in
  let condensation (g : dep_graph) : tout_comp =
    let n = Array.length g in
    let add, iter, get_tout =
      let order = Array.make n ~-1 in
      let index = Array.copy order in
      let size = ref 0 in
      (fun v -> incr size; order.(!size - 1) <- v; index.(v) <- !size -1),
      (fun f -> Array.iter f order),
      (fun v -> index.(v))
    in
    let dfs = make_dfs g in
    for i = 0 to n - 1 do dfs add i done;
    let set_parent, get_parent =
      let parent = Array.(make (length g) ~-1) in
      (fun p v -> parent.(v) <- p),
      (fun v -> parent.(v))
    in
    let dfs = make_dfs @@ transpose g in
    iter (fun i -> dfs (set_parent i) i);
    get_tout % get_parent
  in
  let opposite n i = n / 2 - (i - n / 2) - 1 in
  let _2sat (g : dep_graph) =
    let n = Array.length g in
    let toc = condensation g in
    try
      Some (Array.init (n / 2) (fun v ->
          let tocv = toc v and toc'v = toc @@ opposite n v in
          if tocv <> toc'v then tocv > toc'v
                           else raise Not_found))
    with Not_found -> None
  in
  let dep_graph_of_road_graph (g : road_graph) : dep_graph =
    let n = Array.length g * 2 in
    let (!!) = opposite n in
    let add_disj, get =
      let g = Array.make n [] in
      (fun (a, b) -> add_edge g (!! a) b; add_edge g (!! b) a),
      (fun () -> g)
    in
    let add_disjs = List.iter add_disj in
    ArrayLabels.iteri g ~f:(fun i ->
      List.iter @@ fun (j, asphalted) ->
        add_disjs @@ (* literals correspond to enevenness of switches *)
          if asphalted then [(i, !! j); (j, !!i)] else [(i, j); (!!i, !!j)]);
    get ()
  in
  let solution = function
    | None -> "Impossible"
    | Some a ->
      let to_switch, _ =
        ArrayLabels.fold_left a
          ~init:([], 1)
          ~f:(fun (acc, i) even -> (if not even then i :: acc else acc), i + 1)
      in
      List.(Printf.sprintf "%d\n%s"
              (length to_switch)
              (String.concat " " @@ map string_of_int @@ rev to_switch))
  in
  let input () =
    Scanf.scanf "%d %d" @@ fun n m ->
    let add_road, get =
      let g = Array.make n [] in
      (fun a b asphalted -> add_edge g a (b, asphalted); add_edge g b (a, asphalted)),
      (fun () -> g)
    in
    for i = 1 to m do
      ignore i; (* to get rid of unused variable warning *)
      Scanf.scanf " %d %d %d" @@ fun a b asphalted ->
        add_road (a - 1) (b - 1) (asphalted = 1)
    done;
    get ()
  in
  input ()
  |> dep_graph_of_road_graph
  |> _2sat
  |> solution
  |> Printf.printf "%s\n"

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

type road_graph = (int * bool) list array
type dep_graph = int list array
type tout_comp = int -> int

let (%) f g x = f @@ g x

let () =
  let make_dfs g =
    let marked, mark =
      let marked = Array.(make (length g) false) in
      Array.get marked,
      (fun v -> marked.(v) <- true)
    in
    fun fout v ->
    let rec dfs v =
      if not (marked v) then begin
        mark v;
        List.iter dfs g.(v);
        fout v
      end
    in
    dfs v
  in
  let add_edge g a b = g.(a) <- b :: g.(a) in
  let transpose g =
    let g' = Array.(make (length g) []) in
    ArrayLabels.iteri g ~f:(fun a -> List.iter @@ fun b -> add_edge g' b a);
    g'
  in
  let condensation g : tout_comp =
    let n = Array.length g in
    let add, iter, get_tout =
      let order = Array.make n ~-1 in
      let index = Array.copy order in
      let size = ref 0 in
      (fun v -> incr size; order.(!size - 1) <- v; index.(v) <- !size -1),
      (fun f -> Array.iter f order),
      Array.get index
    in
    let set_parent, get_parent =
      let parent = Array.(make (length g) ~-1) in
      (fun p v -> parent.(v) <- p),
      Array.get parent
    in
    let dfs = make_dfs g in
    for i = 0 to n - 1 do dfs add i done;
    let dfs = make_dfs @@ transpose g in
    iter (fun i -> dfs (set_parent i) i);
    get_tout % get_parent
  in
  let opposite n i = n / 2 - (i - n / 2) - 1 in
  let _2sat (g : dep_graph) =
    let n = Array.length g in
    let toc = condensation g in
    try
      Some (Array.init (n / 2) (fun v ->
          let tocv = toc v and tocv' = toc @@ opposite n v in
          if tocv <> tocv' then tocv > tocv'
                           else raise Not_found))
    with Not_found -> None
  in
  let dep_graph_of_road_graph (g : road_graph) : dep_graph =
    let n = Array.length g * 2 in
    let (!!) = opposite n in
    let add_disj, get =
      let g = Array.make n [] in
      (fun (a, b) -> add_edge g (!! a) b; add_edge g (!! b) a),
      (fun () -> g)
    in
    let add_disjs = List.iter add_disj in
    ArrayLabels.iteri g ~f:(fun i ->
      List.iter @@ fun (j, asphalted) ->
        add_disjs @@ (* literals correspond to enevenness of switches *)
          if asphalted then [(i, !!j); (j, !!i)] else [(i, j); (!!i, !!j)]);
    get ()
  in
  let solution = function
    | None -> "Impossible"
    | Some a ->
      let to_switch, _ =
        ArrayLabels.fold_left a
          ~init:([], 1)
          ~f:(fun (acc, i) even -> (if not even then i :: acc else acc), i + 1)
      in
      List.(Printf.sprintf "%d\n%s"
              (length to_switch)
              (String.concat " " @@ map string_of_int @@ rev to_switch))
  in
  let input () =
    Scanf.scanf "%d %d" @@ fun n m ->
    let add_road, get =
      let g = Array.make n [] in
      (fun a b asphalted -> add_edge g a (b, asphalted); add_edge g b (a, asphalted)),
      (fun () -> g)
    in
    for i = 1 to m do
      ignore i; (* to get rid of unused variable warning *)
      Scanf.scanf " %d %d %d" @@ fun a b asphalted ->
        add_road (a - 1) (b - 1) (asphalted = 1)
    done;
    get ()
  in
  input ()
  |> dep_graph_of_road_graph
  |> _2sat
  |> solution
  |> Printf.printf "%s\n"

let () =
  Scanf.scanf "%d %d" (fun n m ->
  let g = Array.make n [] in
  for i = 1 to m do
    Scanf.scanf " %d %d %d" (fun a b c ->
      let a = a - 1 and b = b - 1 in
      g.(a) <- (b, c) :: g.(a); g.(b) <- (a, c) :: g.(b))
  done;
  let cc = Array.make n 2 in
  let rec dfs c v = match cc.(v) with
    | 2 -> cc.(v) <- c; List.iter (fun (v, c') -> dfs (1 land (lnot (c lxor c'))) v) g.(v)
    | c' when c = c' -> ()
    | _ -> raise Not_found
  in
  try
    Array.iteri (fun v _ -> try dfs 1 v with Not_found -> dfs 0 v) g;
    let l, _ = Array.fold_left (fun (acc, i) c -> (if c = 1 then string_of_int i :: acc else acc), i + 1) ([], 1) cc in
    Printf.printf "%d\n%s\n" (List.length l) (String.concat " " l)
  with Not_found -> Printf.printf "Impossible\n")

let (@@) f x = f x
let (|>) x f = f x
let (%) f g x = f @@ g x

let () =
  Scanf.scanf "%d %d" @@ fun n m ->
  let g = Array.make (n + n * n) [] in
  let add_edges = List.iter @@ fun (a, b) -> g.(a) <- b :: g.(a); g.(b) <- a :: g.(b) in
  let idx a b = n + n * (min a b) + (max a b) in
  for i = 1 to m do
    ignore i;
    Scanf.scanf " %d %d %d" @@ fun a b even ->
      let a = a - 1 and b = b - 1 in
      add_edges @@ if even = 1 then [(a, idx a b); (idx a b, b)] else [(a, b)]
  done;
  let colors = Array.make (n + n * n) None in
  let rec dfs c v = match colors.(v) with
    | Some c' when c <> c' -> raise Not_found
    | Some _ -> ()
    | None -> colors.(v) <- Some c; List.iter (dfs @@ not c) g.(v)
  in
  let get o = match o with Some c -> c | None -> assert false in
  try
    ArrayLabels.iteri colors ~f:(fun v c -> if c = None then dfs false v);
    List.(
      Array.sub colors 0 n |> Array.to_list
      |> mapi (fun i b -> i, b) |> filter (get % snd) |> map (string_of_int % fst) |> fun l ->
        Printf.printf "%d\n%s\n" (length l) @@ String.concat " " l)
  with Not_found -> Printf.printf "Impossible\n"


Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.

The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.

If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let s = Scanf.bscanf sb "%s " (fun s -> s) in
  let k = ref k in
  let c2i c = Char.code c - Char.code 'a' in
  let i2c x = Char.chr (x + Char.code 'a') in
    for i = 0 to n - 1 do
      let x = c2i s.[i] in
	if !k <= x then (
	  s.[i] <- i2c (x - !k);
	  k := 0;
	) else if !k <= 25 - x then (
	  s.[i] <- i2c (x + !k);
	  k := 0;
	) else if x <= 25 - x then (
	  s.[i] <- 'z';
	  k := !k - (25 - x);
	) else (
	  s.[i] <- 'a';
	  k := !k - x;
	)
    done;
    if !k > 0
    then Printf.printf "-1\n"
    else Printf.printf "%s\n" s


Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence.

The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105).

Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1.

let n = read_int () ;;
let vals = Str.split (Str.regexp " ") (read_line () ) ;;
let valsa = Array.of_list (List.map int_of_string vals) ;;

let min_vals = Array.fold_left min valsa.(0) valsa ;;
let rec find a x nd =
        if nd < 0 then (-1)
        else if a.(nd) == x then nd
        else find a x (nd-1) ;;
let min_index = find valsa min_vals 8 ;;
let b = Bytes.make (n/valsa.(min_index)) (Char.chr (48+ min_index+1)) ;;
let left = n - ((n/valsa.(min_index)) * valsa.(min_index)) ;;
let rec findg a g nd =
        if nd < 0 then (-1)
        else if a.(nd) <= g then nd
        else findg a g (nd-1) ;;
let rec subs bs p pr =
        let ni = findg valsa (pr + valsa.(min_index)) 8 in
        (*let _ = print_int(ni) in
        let _3 = print_int(valsa.(7)) in
        let _2 = print_int(pr + valsa.(min_index)) in
        let _4 = print_newline () in *)
        if (ni == (-1)) || (pr <= 0) || (p >= (Bytes.length bs)) then bs
        else begin
                Bytes.set bs p (Char.chr (48+ni+1));
                subs bs (p+1) (pr-valsa.(ni) + valsa.(min_index))
        end;;
if (Bytes.length b > 0) then print_string(Bytes.to_string (subs b 0 left))
else print_int((-1));;

let n = read_int () ;;
let vals = Str.split (Str.regexp " ") (read_line () ) ;;
let valsa = Array.of_list (List.map int_of_string vals) ;;

let min_vals = Array.fold_left min valsa.(0) valsa ;;
let rec find a x nd =
        if nd < 0 then (-1)
        else if a.(nd) == x then nd
        else find a x (nd-1) ;;
let min_index = find valsa min_vals 8 ;;
let b = Bytes.make (n/valsa.(min_index)) (Char.chr (48+ min_index+1)) ;;
let left = n - ((n/valsa.(min_index)) * valsa.(min_index)) ;;
let rec findg a g nd =
        if nd < 0 then (-1)
        else if a.(nd) >= g then nd
        else find a g (nd-1) ;;
let rec subs bs p pr =
        let ni = findg valsa pr 8 in
        if (ni == (-1)) || (pr == 0) || (p >= (Bytes.length bs)) then bs
        else begin
                Bytes.set bs p (Char.chr (48+ni+1));
                subs bs (p+1) (pr-valsa.(ni))
        end;;
if (Bytes.length b > 0) then print_string(Bytes.to_string b)
else print_int((-1));;


You are given a string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters.You have to remove at most one (i.e. zero or one) character of this string in such a way that the string you obtain will be lexicographically smallest among all strings that can be obtained using this operation.String $$$s = s_1 s_2 \dots s_n$$$ is lexicographically smaller than string $$$t = t_1 t_2 \dots t_m$$$ if $$$n < m$$$ and $$$s_1 = t_1, s_2 = t_2, \dots, s_n = t_n$$$ or there exists a number $$$p$$$ such that $$$p \le min(n, m)$$$ and $$$s_1 = t_1, s_2 = t_2, \dots, s_{p-1} = t_{p-1}$$$ and $$$s_p < t_p$$$.For example, "aaa" is smaller than "aaaa", "abb" is smaller than "abc", "pqr" is smaller than "z".

The first line of the input contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of $$$s$$$. The second line of the input contains exactly $$$n$$$ lowercase Latin letters — the string $$$s$$$.

Print one string — the smallest possible lexicographically string that can be obtained by removing at most one character from the string $$$s$$$.

NoteIn the first example you can remove any character of $$$s$$$ to obtain the string "aa".In the second example "abca" < "abcd" < "abcda" < "abda" < "acda" < "bcda".

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 = get_i64 0 in
  let s = scanf " %s" @@ id in
  repi 0 (i32 n -$ 2) (fun i ->
    if s.[i] > s.[i+$1] then (
      repi 0 (i-$1) (fun j -> print_char s.[j]);
      (* print_endline "==="; *)
      repi (i+$1) (i32 n-$1) (fun j -> print_char s.[j]);
      exit 0
    )
  );
  repi 0 (i32 n -$ 2) (fun i ->
    print_char s.[i]
  )

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 = get_i64 0 in
  let s = scanf " %s" @@ id in
  repi 0 (i32 n -$ 2) (fun i ->
    printf "%c %c\n" s.[i] s.[i+$1];
    if s.[i] > s.[i+$1] then (
      repi 0 (i-$1) (fun j -> print_char s.[j]);
      (* print_endline "==="; *)
      repi (i+$1) (i32 n-$1) (fun j -> print_char s.[j]);
      exit 0
    )
  );
  repi 0 (i32 n -$ 2) (fun i ->
    print_char s.[i]
  )


Dima got into number sequences. Now he's got sequence a1, a2, ..., an, consisting of n positive integers. Also, Dima has got a function f(x), which can be defined with the following recurrence:  f(0) = 0;  f(2·x) = f(x);  f(2·x + 1) = f(x) + 1. Dima wonders, how many pairs of indexes (i, j) (1 ≤ i < j ≤ n) are there, such that f(ai) = f(aj). Help him, count the number of such pairs.

The first line contains integer n (1 ≤ n ≤ 105). The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 109). The numbers in the lines are separated by single spaces.

In a single line print the answer to the problem. Please, don't use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.

NoteIn the first sample any pair (i, j) will do, so the answer is 3.In the second sample only pair (1, 2) will do.

(* Codeforces 272B - Dima seq *)

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 printarri ai = Array.iter (fun i -> (print_int i; print_string " ")) ai;;
let debug = false;;

let rec readlist n acc = match n with
	| 0 -> acc
	| _ -> readlist (n -1) (gr() :: acc);;

let rec countbits n = match n with
	| 0 -> 0
	| 1 -> 1
	| _ ->
			let (n2, o ) = (n / 2, n mod 2) in
			o + countbits n2;;

let rec setcounts l a =
	match l with
	| [] -> ()
	| h :: t ->
			begin
				a.(h) <- a.(h) + 1;
				setcounts t a
			end;;

let rec npairs n =
	let nn1 = (Int64.mul (Int64.of_int n) (Int64.of_int (n - 1))) in
	Int64.div nn1 (Int64.of_int 2);;

let rec sumArr64 a n = match n with
| -1 -> Int64.zero
| _ -> (Int64.add a.(n) (sumArr64 a (n - 1)));;

let main () =
	let na = gr() in
	let a = readlist na [] in
	let bta = List.map countbits a in
	let cnts = Array.make 33 0 in
	begin
		(* printlisti a; *)
		setcounts bta cnts;
		let aa = Array.map npairs cnts in
		let sum_pairs = sumArr64 aa 32 in
		print_string (Int64.to_string sum_pairs)
	end;;

main();;


A kindergarten teacher Natalia Pavlovna has invented a new ball game. This game not only develops the children's physique, but also teaches them how to count. The game goes as follows. Kids stand in circle. Let's agree to think of the children as numbered with numbers from 1 to n clockwise and the child number 1 is holding the ball. First the first child throws the ball to the next one clockwise, i.e. to the child number 2. Then the child number 2 throws the ball to the next but one child, i.e. to the child number 4, then the fourth child throws the ball to the child that stands two children away from him, i.e. to the child number 7, then the ball is thrown to the child who stands 3 children away from the child number 7, then the ball is thrown to the child who stands 4 children away from the last one, and so on. It should be mentioned that when a ball is thrown it may pass the beginning of the circle. For example, if n = 5, then after the third throw the child number 2 has the ball again. Overall, n - 1 throws are made, and the game ends.The problem is that not all the children get the ball during the game. If a child doesn't get the ball, he gets very upset and cries until Natalia Pavlovna gives him a candy. That's why Natalia Pavlovna asks you to help her to identify the numbers of the children who will get the ball after each throw.

The first line contains integer n (2 ≤ n ≤ 100) which indicates the number of kids in the circle.

In the single line print n - 1 numbers which are the numbers of children who will get the ball after each throw. Separate the numbers by spaces.

let n = Scanf.scanf "%d" abs;;

let m = ref 0;;

for i = 1 to n-1 do (
	m := ((!m + i) mod n);
	print_int (!m + 1);
	if i <> n-1 then print_string " ";
) done

let rec pass k = function
  | 0 -> 1
  | x -> (x + pass k (x - 1)) mod k;;
let k = read_int ();;
for i = 1 to k - 1 do
  print_int ((fun x -> if x == 0 then k else x) (pass k i));
  print_string " "
done;;
print_newline ();;


Consider the infinite sequence $$$s$$$ of positive integers, created by repeating the following steps:  Find the lexicographically smallest triple of positive integers $$$(a, b, c)$$$ such that   $$$a \oplus b \oplus c = 0$$$, where $$$\oplus$$$ denotes the bitwise XOR operation.  $$$a$$$, $$$b$$$, $$$c$$$ are not in $$$s$$$.  Here triple of integers $$$(a_1, b_1, c_1)$$$ is considered to be lexicographically smaller than triple $$$(a_2, b_2, c_2)$$$ if sequence $$$[a_1, b_1, c_1]$$$ is lexicographically smaller than sequence $$$[a_2, b_2, c_2]$$$.  Append $$$a$$$, $$$b$$$, $$$c$$$ to $$$s$$$ in this order.  Go back to the first step. You have integer $$$n$$$. Find the $$$n$$$-th element of $$$s$$$.You have to answer $$$t$$$ independent test cases.A sequence $$$a$$$ is lexicographically smaller than a sequence $$$b$$$ if in the first position where $$$a$$$ and $$$b$$$ differ, the sequence $$$a$$$ has a smaller element than the corresponding element in $$$b$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases. Each of the next $$$t$$$ lines contains a single integer $$$n$$$ ($$$1\le n \le 10^{16}$$$) — the position of the element you want to know.

In each of the $$$t$$$ lines, output the answer to the corresponding test case.

open Printf
open Scanf

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

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

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

let rec dig4 n = if n < 4L then 1 else 1 + (dig4 (n//4L));; (* # base 4 digits *)
let rec power4 x = if x=0 then 1L else 4L ** (power4 (x-1))

let base10 b4 d =
  let rec loop ac i = if i<0 then ac else (
    loop ((4L ** ac) ++ long (b4.(i))) (i-1)
  ) in
  loop 0L (d-1)

let a = [| [|0;0;0|]; [|1;2;3|]; [|2;3;1|]; [|3;1;2|]  |]
let h = [|1;2;3|]

let () =
  let cases = read_int() in
  for case = 1 to cases do
    let n = read_long() in
    let d = dig4 n in
    let n' = n -- (power4 (d-1)) in
    let b4 = Array.make d 0 in
    b4.(d-1) <- h.(short (n' %% 3L));
    for i=0 to d-2 do
      b4.(i) <- a.( short (((n'//3L) // (power4 i)) %% 4L)).( short (n' %% 3L)  )
    done;
    printf "%Ld\n" (base10 b4 d)
  done


They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so.Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right.Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid.Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle?

The first line of the input contains two integers h and w (1 ≤ h, w ≤ 500) – the number of rows and the number of columns, respectively. The next h lines describe a grid. Each line contains a string of the length w. Each character is either '.' or '#' — denoting an empty or forbidden cell, respectively. The next line contains a single integer q (1 ≤ q ≤ 100 000) — the number of queries. Each of the next q lines contains four integers r1i, c1i, r2i, c2i (1 ≤ r1i ≤ r2i ≤ h, 1 ≤ c1i ≤ c2i ≤ w) — the i-th query. Numbers r1i and c1i denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2i and c2i denote the row and the column (respectively) of the bottom right cell of the rectangle.

Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle.

NoteA red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways.

open Int64;;
let [n; m]= List.map (int_of_string) (Str.split (Str.regexp " ") (read_line ()));;
let mx= Array.make (n + 1) "" and x= ref 0L and y = ref 0L;;
let ma= Array.make_matrix (n * 2) (m * 2) 0L and mb= Array.make_matrix (n * 2) (m * 2) 0L;;
let pma= Array.make_matrix (n * 2 + 1) (m * 2 + 1) 0L and pmb= Array.make_matrix (n * 2 + 1) (m * 2 + 1) 0L;;
for i= 0 to n-1 do Array.set mx i (read_line ()) done;;
for i= 0 to n-1 do for j= 0 to m - 1 do if String.get mx.(i) j = '.' then begin
	if j <> m-1 then if String.get mx.(i) (j + 1) = '.' then Array.set ma.(i) j 1L;
	if i <> n-1 then if String.get mx.(i + 1) j = '.' then Array.set mb.(i) j 1L;
end done done;;
for i= 1 to n do for j= 1 to m do
  Array.set pma.(i) j	(sub (add (add ma.(i - 1).(j - 1) pma.(i).(j - 1)) pma.(i - 1).(j)) pma.(i-1).(j-1));
	Array.set pmb.(i) j (sub (add (add mb.(i - 1).(j - 1) pmb.(i).(j - 1)) pmb.(i - 1).(j)) pmb.(i-1).(j-1));
done done;;
let q= read_int ();;
for i= 1 to q do let [y1; x1; y2; x2]= List.map (int_of_string) (Str.split (Str.regexp " ") (read_line ())) in begin
	x := sub (add pma.(y2).(x2 - 1) pma.(y1 - 1).(x1 - 1)) (add pma.(y2).(x1 - 1) pma.(y1 - 1).(x2 - 1));
	if !x < 0L then x := 0L;
	y := sub (add pmb.(y2 - 1).(x2) pmb.(y1 - 1).(x1 - 1)) (add pmb.(y2 - 1).(x1 - 1) pmb.(y1 - 1).(x2));
	if !y < 0L then y := 0L;
	print_endline (to_string (add !x !y))
end done;;

(*#load "str.cma";;*)
open Printf;;
let [n; m]= List.map (int_of_string) (Str.split (Str.regexp " ") (read_line ()));;
let mx= Array.make (n + 1) "";;
let ma= Array.make_matrix (n * 2) (m * 2) 0 and mb= Array.make_matrix (n * 2) (m * 2) 0;;
let pma= Array.make_matrix (n * 2 + 1) (m * 2 + 1) 0 and pmb= Array.make_matrix (n * 2 + 1) (m * 2 + 1) 0;;
for i= 0 to n - 1 do Array.set mx i (read_line ()) done;;
for i= 0 to n - 1 do for j= 0 to m - 1 do if String.get mx.(i) j = '.' then
begin
	if j <> m - 1 then if String.get mx.(i) (j + 1) = '.' then Array.set ma.(i) j 1;
	if i <> n - 1 then if String.get mx.(i + 1) j = '.' then Array.set mb.(i) j 1;
end done done;;
for i= 1 to n do for j= 1 to m do
	Array.set pma.(i) j (ma.(i - 1).(j - 1) + pma.(i).(j - 1) + pma.(i - 1).(j) - pma.(i-1).(j-1));
	Array.set pmb.(i) j (mb.(i - 1).(j - 1) + pmb.(i).(j - 1) + pmb.(i - 1).(j) - pmb.(i-1).(j-1));
done done;;
let q= read_int ();;
for i= 1 to q do
  let [y1; x1; y2; x2]= List.map (int_of_string) (Str.split (Str.regexp " ") (read_line ())) in
  begin
		print_int
				((max 0 (pma.(y2).(x2 - 1) - pma.(y2).(x1 - 1) - pma.(y1 - 1).(x2 - 1) + pma.(y1 - 1).(x1 - 1))) +
				(max 0 (pmb.(y2 - 1).(x2) - pmb.(y2 - 1).(x1 - 1) - pmb.(y1 - 1).(x2) + pmb.(y1 - 1).(x1 - 1))));
		print_endline ""
	end
done;;

open Int64 open Str;;
let aset = Array.set;;
let [n; m]= List.map (int_of_string) (split (regexp " ") (read_line ()));;
let mx= Array.make (n + 1) "" and x= ref 0L and y = ref 0L;;
let ma= Array.make_matrix (n * 2) (m * 2) 0L and mb= Array.make_matrix (n * 2) (m * 2) 0L;;
let pma= Array.make_matrix (n * 2 + 1) (m * 2 + 1) 0L and pmb= Array.make_matrix (n * 2 + 1) (m * 2 + 1) 0L;;
for i= 0 to n-1 do Array.set mx i (read_line ()) done;;
for i= 0 to n-1 do for j= 0 to m - 1 do if String.get mx.(i) j = '.' then begin
  if j <> m-1 then if String.get mx.(i) (j+1) = '.' then aset ma.(i) j 1L;
  if i <> n-1 then if String.get mx.(i+1) j = '.' then aset mb.(i) j 1L;
end done done;;
for i= 1 to n do for j= 1 to m do
  aset pma.(i) j (sub (add (add pma.(i-1).(j) pma.(i).(j-1)) ma.(i-1).(j-1)) pma.(i-1).(j-1));
  aset pmb.(i) j (sub (add (add pmb.(i-1).(j) pmb.(i).(j-1)) mb.(i-1).(j-1)) pmb.(i-1).(j-1));
done done;;
let q= read_int ();;
for i= 1 to q do let [y1; x1; y2; x2]= List.map (int_of_string) (split (regexp " ") (read_line ())) in begin
  x := sub (add pma.(y2).(x2-1) pma.(y1-1).(x1-1)) (add pma.(y2).(x1-1) pma.(y1-1).(x2-1));
  if !x < 0L then x:= 0L;
  y := sub (add pmb.(y2-1).(x2) pmb.(y1-1).(x1-1)) (add pmb.(y2-1).(x1-1) pmb.(y1-1).(x2));
  if !y < 0L then y:= 0L;
  print_endline (to_string (add !x !y))
end done;;

open Printf
open Scanf

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_pair () = bscanf Scanning.stdib " %d %d " (fun x y -> (x,y))
let read_string () = bscanf Scanning.stdib " %s " (fun x -> x)

let () =
  let h = read_int () in
  let w = read_int () in
  let grid = Array.init h (fun _ -> read_string()) in
  let q = read_int () in
  let query = Array.init q (fun _ ->
    let a = read_pair() in
    let b = read_pair() in
    (a,b)
  ) in

  let geth i j =
    if i<0 || j<0 then '#' else grid.(i).[j]
  in

  let getv i j =
    if i<0 || j<0 then '#' else grid.(j).[i]
  in

  let getcount count i j = if i<0 || j<0 then 0 else count.(i).(j) in

  let make_count h w get =
    let c = Array.make_matrix h w 0 in
    for i=0 to h-1 do
      for j=0 to w-1 do
	let here = if (get i j) = '.' && (get i (j-1)) = '.' then 1 else 0 in
	c.(i).(j) <- here + (getcount c (i-1) j) + (getcount c i (j-2)) - (getcount c (i-1) (j-2));
      done
    done;
    c
  in

  let counth = make_count h w geth in
  let countv = make_count w h getv in

  for k = 0 to q-1 do
    let ((r1,c1),(r2,c2)) = query.(k) in
    let ((r1,c1),(r2,c2)) = ((r1-2,c1-2),(r2-1,c2-1)) in

    let heven =
      let c2 = c2 - (c2 land 1) in
      let c1 = c1 + (c1 land 1) in
      (getcount counth r2 c2)
      - (getcount counth r1 c2) - (getcount counth r2 c1)
      + (getcount counth r1 c1)
    in

    let hodd =
      let c2 = c2 - (1 - (c2 land 1)) in
      let c1 = c1 + (1 - (c1 land 1)) in
      (getcount counth r2 c2)
      - (getcount counth r1 c2) - (getcount counth r2 c1)
      + (getcount counth r1 c1)
    in

    let veven =
      let r2 = r2 - (r2 land 1) in
      let r1 = r1 + (r1 land 1) in
      (getcount countv c2 r2)
      - (getcount countv c1 r2) - (getcount countv c2 r1)
      + (getcount countv c1 r1)
    in

    let vodd =
      let r2 = r2 - (1 - (r2 land 1)) in
      let r1 = r1 + (1 - (r1 land 1)) in
      (getcount countv c2 r2)
      - (getcount countv c1 r2) - (getcount countv c2 r1)
      + (getcount countv c1 r1)
    in

    printf "%d\n" (heven + hodd + veven + vodd)
  done

open Printf;;
let [n; m]= List.map (int_of_string) (Str.split (Str.regexp " ") (read_line ()));;
let mx= Array.make (n + 1) "";;
let ma= Array.make_matrix (n * 2) (m * 2) 0 and mb= Array.make_matrix (n * 2) (m * 2) 0;;
let pma= Array.make_matrix (n * 2 + 1) (m * 2 + 1) 0 and pmb= Array.make_matrix (n * 2 + 1) (m * 2 + 1) 0;;
for i= 0 to n - 1 do Array.set mx i (read_line ()) done;;
for i= 0 to n - 1 do for j= 0 to m - 1 do if String.get mx.(i) j = '.' then
begin
	if j <> m - 1 then if String.get mx.(i) (j + 1) = '.' then Array.set ma.(i) j 1;
	if i <> n - 1 then if String.get mx.(i + 1) j = '.' then Array.set mb.(i) j 1;
end done done;;
for i= 1 to n do for j= 1 to m do
	Array.set pma.(i) j (ma.(i - 1).(j - 1) + pma.(i).(j - 1) + pma.(i - 1).(j) - pma.(i-1).(j-1));
	Array.set pmb.(i) j (mb.(i - 1).(j - 1) + pmb.(i).(j - 1) + pmb.(i - 1).(j) - pmb.(i-1).(j-1));
done done;;
let q= read_int ();;
for i= 1 to q do
  let [y1; x1; y2; x2]= List.map (int_of_string) (Str.split (Str.regexp " ") (read_line ())) in
  begin
		if y2 <> 1 && x2 <> 1 then
			print_int
					((max 0 (pma.(y2).(x2 - 1) - pma.(y2).(x1 - 1) - pma.(y1 - 1).(x2 - 1) + pma.(y1 - 1).(x1 - 1))) +
					(max 0 (pmb.(y2 - 1).(x2) - pmb.(y2 - 1).(x1 - 1) - pmb.(y1 - 1).(x2) + pmb.(y1 - 1).(x1 - 1))))
		else if y2 = 1 && x2 <> 1 then
			print_int
					(max 0 (pma.(y2 - 1).(x2) - pma.(y2 - 1).(x1 - 1) - pma.(y1 - 1).(x2) + pma.(y1 - 1).(x1 - 1)))
		else if x2 = 1 && y2 <> 1 then
			print_int
					(max 0 (pmb.(y2).(x2 - 1) - pmb.(y2).(x1 - 1) - pmb.(y1 - 1).(x2 - 1) + pmb.(y1 - 1).(x1 - 1)))
		else print_int 0;
		print_endline ""
	end
done;;

open Int64 open Str;;
let aset = Array.set;;
let [n; m]= List.map (int_of_string) (split (regexp " ") (read_line ()));;
let mx= Array.make (n + 1) "" and x= ref 0L and y = ref 0L;;
let ma= Array.make_matrix (n * 2) (m * 2) 0L and mb= Array.make_matrix (n * 2) (m * 2) 0L;;
let pma= Array.make_matrix (n * 2 + 1) (m * 2 + 1) 0L and pmb= Array.make_matrix (n * 2 + 1) (m * 2 + 1) 0L;;
for i= 0 to n-1 do Array.set mx i (read_line ()) done;;
for i= 0 to n-1 do for j= 0 to m - 1 do if String.get mx.(i) j = '.' then begin
  if j <> m-1 then if String.get mx.(i) (j+1) = '.' then aset ma.(i) j 1L;
  if i <> n-1 then if String.get mx.(i+1) j = '.' then aset mb.(i) j 1L;
end done done;;
for i= 1 to n do for j= 1 to m do
  aset pma.(i) j (sub (add (add ma.(i-1).(j) pma.(i).(j-1)) pma.(i-1).(j-1)) pma.(i-1).(j-1));
  aset pmb.(i) j (sub (add (add mb.(i-1).(j-1) pmb.(i).(j-1)) pmb.(i-1).(j)) pmb.(i-1).(j-1));
done done;;
let q= read_int ();;
for i= 1 to q do let [y1; x1; y2; x2]= List.map (int_of_string) (split (regexp " ") (read_line ())) in begin
  x := sub (add pma.(y2).(x2-1) pma.(y1-1).(x1-1)) (add pma.(y2).(x1-1) pma.(y1-1).(x2-1));
  if !x < 0L then x:= 0L;
  y := sub (add pmb.(y2-1).(x2) pmb.(y1-1).(x1-1)) (add pmb.(y2-1).(x1-1) pmb.(y1-1).(x2));
  if !y < 0L then y:= 0L;
  print_endline (to_string (add !x !y))
end done;;


The Little Elephant loves Ukraine very much. Most of all he loves town Rozdol (ukr. "Rozdil").However, Rozdil is dangerous to settle, so the Little Elephant wants to go to some other town. The Little Elephant doesn't like to spend much time on travelling, so for his journey he will choose a town that needs minimum time to travel to. If there are multiple such cities, then the Little Elephant won't go anywhere.For each town except for Rozdil you know the time needed to travel to this town. Find the town the Little Elephant will go to or print "Still Rozdil", if he stays in Rozdil.

The first line contains a single integer n (1 ≤ n ≤ 105) — the number of cities. The next line contains n integers, separated by single spaces: the i-th integer represents the time needed to go from town Rozdil to the i-th town. The time values are positive integers, not exceeding 109. You can consider the cities numbered from 1 to n, inclusive. Rozdil is not among the numbered cities.

Print the answer on a single line — the number of the town the Little Elephant will go to. If there are multiple cities with minimum travel time, print "Still Rozdil" (without the quotes).

NoteIn the first sample there are only two cities where the Little Elephant can go. The travel time for the first town equals 7, to the second one — 4. The town which is closest to Rodzil (the only one) is the second one, so the answer is 2.In the second sample the closest cities are cities two and five, the travelling time to both of them equals 4, so the answer is "Still Rozdil".

let _ =
  let n = Scanf.scanf " %d" (fun x -> x) in
  let a = Array.make n 0 in
  for i = 0 to n - 1 do
    Scanf.scanf " %d" (fun x -> a.(i) <- x)
  done;
  let k = ref 0 in
  let m = ref a.(0) in
  for i = 1 to n - 1 do
    if a.(i) < !m then (m := a.(i); k := i)
    else if a.(i) = !m then k := -1
  done;
  if !k = -1
  then print_string "Still Rozdil"
  else print_int (!k + 1)
;;

#load "str.cma";;

let rec get_input () =
  let n = int_of_string (read_line ()) and
	  ls = Str.split (Str.regexp "[ \t]+") (read_line ()) in
  (n, Array.mapi (fun idx x -> (idx, x))
	(Array.of_list (List.map int_of_string ls)));;

let print_int' n =
  begin
	print_int (n + 1);
	print_string "\n";
  end;;

let solve n sls =
  if n == 1 then print_int' (fst sls.(0))
  else
	begin
	  Array.fast_sort (fun (idxx, x) (idxy, y) -> x - y) sls;
	  (* Array.iter (fun (x, y) -> print_int y) sls;*)
	  if snd (sls.(0)) == snd (sls.(1)) then
		print_string "Still Rozdil\n"
	  else
		print_int' (fst (sls.(0)))
	end;;

let (n, ls) = get_input () in
solve n ls;;

#load "str.cma";;

let rec get_input () =
  let n = read_line () and
	  ls = Str.split (Str.regexp "[ \t]") (read_line ()) in
  (n, List.map int_of_string ls);;

let solve n ls =
  let rec iter l cur flag =
	match l with
		[] -> if flag then -1 else cur
	  | (x::xs) ->
		if x == cur then
		  iter xs x true
		else if x < cur then
		  iter xs x false
		else
		  iter xs cur flag
  in iter ls 1000000001 false;;

let (n, ls) = get_input () in
begin
  (*
  List.iter (fun x -> print_int x; print_string" ") ls;
  print_string "\n";
  *)
  if solve n ls == -1 then
	print_string "Still Rozdil"
  else print_int (solve n ls); print_string "\n";
end


You are given array a with n elements and the number m. Consider some subsequence of a and the value of least common multiple (LCM) of its elements. Denote LCM as l. Find any longest subsequence of a with the value l ≤ m.A subsequence of a is an array we can get by erasing some elements of a. It is allowed to erase zero or all elements.The LCM of an empty array equals 1.

The first line contains two integers n and m (1 ≤ n, m ≤ 106) — the size of the array a and the parameter from the problem statement. The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of a.

In the first line print two integers l and kmax (1 ≤ l ≤ m, 0 ≤ kmax ≤ n) — the value of LCM and the number of elements in optimal subsequence. In the second line print kmax integers — the positions of the elements from the optimal subsequence in the ascending order. Note that you can find and print any subsequence with the maximum length.

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 _ =
    for i = 0 to n - 1 do
      a.(i) <- Scanf.bscanf sb "%d " (fun s -> s)
    done;
  in
  let b = Array.make (m + 1) 0 in
  let c = Array.make (m + 1) 0 in
    for i = 0 to n - 1 do
      if a.(i) <= m
      then b.(a.(i)) <- b.(a.(i)) + 1
    done;
    for i = 1 to m do
      if b.(i) > 0 then (
	let j = ref i in
	  while !j <= m do
	    c.(!j) <- c.(!j) + b.(i);
	    j := !j + i;
	  done;
      )
    done;
    let mc = ref 1 in
    let mv = ref c.(1) in
      for i = 2 to m do
	if c.(i) > !mv then (
	  mv := c.(i);
	  mc := i;
	)
      done;
      Printf.printf "%d %d\n" !mc !mv;
      for i = 0 to n - 1 do
	if !mc mod a.(i) = 0
	then Printf.printf "%d " (i + 1)
      done;
      Printf.printf "\n"

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 _ =
    for i = 0 to n - 1 do
      a.(i) <- Scanf.bscanf sb "%d " (fun s -> s)
    done;
  in
  let b = Array.make (m + 1) 0 in
  let c = Array.make (m + 1) 0 in
    for i = 0 to n - 1 do
      if a.(i) <= m
      then b.(a.(i)) <- b.(a.(i)) + 1
    done;
    for i = 1 to m do
      if b.(i) > 0 then (
	let j = ref i in
	  while !j <= m do
	    c.(!j) <- c.(!j) + b.(i);
	    j := !j + i;
	  done;
      )
    done;
    let mc = ref 1 in
    let mv = ref 0 in
      for i = 2 to m do
	if c.(i) > !mv then (
	  mv := c.(i);
	  mc := i;
	)
      done;
      Printf.printf "%d %d\n" !mc !mv;
      for i = 0 to n - 1 do
	if !mc mod a.(i) = 0
	then Printf.printf "%d " (i + 1)
      done;
      Printf.printf "\n"


You are given four integers $$$a$$$, $$$b$$$, $$$x$$$ and $$$y$$$. Initially, $$$a \ge x$$$ and $$$b \ge y$$$. You can do the following operation no more than $$$n$$$ times:  Choose either $$$a$$$ or $$$b$$$ and decrease it by one. However, as a result of this operation, value of $$$a$$$ cannot become less than $$$x$$$, and value of $$$b$$$ cannot become less than $$$y$$$. Your task is to find the minimum possible product of $$$a$$$ and $$$b$$$ ($$$a \cdot b$$$) you can achieve by applying the given operation no more than $$$n$$$ times.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains five integers $$$a$$$, $$$b$$$, $$$x$$$, $$$y$$$ and $$$n$$$ ($$$1 \le a, b, x, y, n \le 10^9$$$). Additional constraint on the input: $$$a \ge x$$$ and $$$b \ge y$$$ always holds.

For each test case, print one integer: the minimum possible product of $$$a$$$ and $$$b$$$ ($$$a \cdot b$$$) you can achieve by applying the given operation no more than $$$n$$$ times.

NoteIn the first test case of the example, you need to decrease $$$b$$$ three times and obtain $$$10 \cdot 7 = 70$$$.In the second test case of the example, you need to decrease $$$a$$$ one time, $$$b$$$ one time and obtain $$$11 \cdot 7 = 77$$$.In the sixth test case of the example, you need to decrease $$$a$$$ five times and obtain $$$5 \cdot 11 = 55$$$.In the seventh test case of the example, you need to decrease $$$b$$$ ten times and obtain $$$10 \cdot 1 = 10$$$.

let () =
  let t = () |> read_line |> int_of_string in
  for i = 1 to t do
    let [a; b; x; y; n] =
      ()
      |> read_line
      |> Str.split (Str.regexp " ")
      |> List.map (fun s -> s |> String.trim |> Int64.of_string)
    in
    let check a b x y =
      let a' = min (Int64.sub a x) n in
      let b' = min (Int64.sub b y) (Int64.sub n a') in
      Int64.mul (Int64.sub a a') (Int64.sub b b')
    in
    min (check a b x y) (check b a y x)
    |> Int64.to_string
    |> print_endline
  done

let () =
  let t = () |> read_line |> int_of_string in
  for i = 1 to t do
    let [a; b; x; y; n] =
      ()
      |> read_line
      |> Str.split (Str.regexp " ")
      |> List.map (fun s -> s |> String.trim |> int_of_string)
    in
    let check a b x y =
      let a' = min (a - x) n in
      let b' = min (b - y) (n - a') in
      (a - a') * (b - b')
    in
    min (check a b x y) (check b a y x)
    |> string_of_int
    |> print_endline
  done


A wise man told Kerem "Different is good" once, so Kerem wants all things in his life to be different. Kerem recently got a string s consisting of lowercase English letters. Since Kerem likes it when things are different, he wants all substrings of his string s to be distinct. Substring is a string formed by some number of consecutive characters of the string. For example, string "aba" has substrings "" (empty substring), "a", "b", "a", "ab", "ba", "aba".If string s has at least two equal substrings then Kerem will change characters at some positions to some other lowercase English letters. Changing characters is a very tiring job, so Kerem want to perform as few changes as possible.Your task is to find the minimum number of changes needed to make all the substrings of the given string distinct, or determine that it is impossible.

The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the length of the string s. The second line contains the string s of length n consisting of only lowercase English letters.

If it's impossible to change the string s such that all its substring are distinct print -1. Otherwise print the minimum required number of changes.

NoteIn the first sample one of the possible solutions is to change the first character to 'b'.In the second sample, one may change the first character to 'a' and second character to 'b', so the string becomes "abko".

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 sum i j f = fold i j (fun i a -> (f i) + a) 0

let l2n c = (int_of_char c) - (int_of_char 'a')
let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_string () = bscanf Scanning.stdib " %s " (fun x -> x)

let () =
  let n = read_int () in
  let s = read_string () in

  let hist = Array.make 26 0 in

  for i=0 to n-1 do
    let j = l2n s.[i] in
    hist.(j) <- hist.(j) + 1
  done;

  let answer = sum 0 25 (fun i -> max 0 (hist.(i) - 1)) in

  let answer = if n > 26 then -1 else answer in

  printf "%d\n" answer


Valera is a collector. Once he wanted to expand his collection with exactly one antique item.Valera knows n sellers of antiques, the i-th of them auctioned ki items. Currently the auction price of the j-th object of the i-th seller is sij. Valera gets on well with each of the n sellers. He is perfectly sure that if he outbids the current price of one of the items in the auction (in other words, offers the seller the money that is strictly greater than the current price of the item at the auction), the seller of the object will immediately sign a contract with him.Unfortunately, Valera has only v units of money. Help him to determine which of the n sellers he can make a deal with.

The first line contains two space-separated integers n, v (1 ≤ n ≤ 50; 104 ≤ v ≤ 106) — the number of sellers and the units of money the Valera has. Then n lines follow. The i-th line first contains integer ki (1 ≤ ki ≤ 50) the number of items of the i-th seller. Then go ki space-separated integers si1, si2, ..., siki (104 ≤ sij ≤ 106) — the current prices of the items of the i-th seller.

In the first line, print integer p — the number of sellers with who Valera can make a deal. In the second line print p space-separated integers q1, q2, ..., qp (1 ≤ qi ≤ n) — the numbers of the sellers with who Valera can make a deal. Print the numbers of the sellers in the increasing order.

NoteIn the first sample Valera can bargain with each of the sellers. He can outbid the following items: a 40000 item from the first seller, a 20000 item from the second seller, and a 10000 item from the third seller.In the second sample Valera can not make a deal with any of the sellers, as the prices of all items in the auction too big for him.

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 minf i j f = fold (i+1) j (fun k a -> min (f k) a) (f i)

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_pair () = let u = read_int() in (u,read_int())
let () =
  let n = read_int () in
  let v = read_int () in
  let s = Array.make n [||] in
  for i=0 to n-1 do
    let k = read_int () in
    s.(i) <- Array.init k (fun i-> read_int())
  done;

  let rec loop i ac = if i=n then ac else
      let m = minf 0 ((Array.length s.(i)) -1) (fun j -> s.(i).(j)) in
      if m < v then loop (i+1) (i::ac) else loop (i+1) ac
  in

  let ans = loop 0 [] in
  let ans = List.sort compare ans in

  printf "%d\n" (List.length ans);

  List.iter (fun i -> printf "%d " (i+1)) ans;

  print_newline();

let read_int()=Scanf.scanf "%d " (fun x->x);;
let n=read_int();;
let v=read_int();;
let tab=Array.make (n+1) 0;;
let vec=Array.make (n+1) false;;
let nb=ref 0;;
for i=1 to n do
   let x=read_int() in
   for j=1 to x do
      let a=read_int() in
      if a<v then (vec.(i)<-true;tab.(i)<-a)
   done;
   if vec.(i) then incr nb
done;;
Printf.printf "%d\n" !nb;;
for i=1 to n do
   if vec.(i) then Printf.printf "%d " i
done;;


The sequence of $$$m$$$ integers is called the permutation if it contains all integers from $$$1$$$ to $$$m$$$ exactly once. The number $$$m$$$ is called the length of the permutation.Dreamoon has two permutations $$$p_1$$$ and $$$p_2$$$ of non-zero lengths $$$l_1$$$ and $$$l_2$$$.Now Dreamoon concatenates these two permutations into another sequence $$$a$$$ of length $$$l_1 + l_2$$$. First $$$l_1$$$ elements of $$$a$$$ is the permutation $$$p_1$$$ and next $$$l_2$$$ elements of $$$a$$$ is the permutation $$$p_2$$$. You are given the sequence $$$a$$$, and you need to find two permutations $$$p_1$$$ and $$$p_2$$$. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.)

The first line contains an integer $$$t$$$ ($$$1 \le t \le 10\,000$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains one integer $$$n$$$ ($$$2 \leq n \leq 200\,000$$$): the length of $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n-1$$$). The total sum of $$$n$$$ is less than $$$200\,000$$$.

For each test case, the first line of output should contain one integer $$$k$$$: the number of ways to divide $$$a$$$ into permutations $$$p_1$$$ and $$$p_2$$$. Each of the next $$$k$$$ lines should contain two integers $$$l_1$$$ and $$$l_2$$$ ($$$1 \leq l_1, l_2 \leq n, l_1 + l_2 = n$$$), denoting, that it is possible to divide $$$a$$$ into two permutations of length $$$l_1$$$ and $$$l_2$$$ ($$$p_1$$$ is the first $$$l_1$$$ elements of $$$a$$$, and $$$p_2$$$ is the last $$$l_2$$$ elements of $$$a$$$). You can print solutions in any order.

NoteIn the first example, two possible ways to divide $$$a$$$ into permutations are $$$\{1\} + \{4, 3, 2, 1\}$$$ and $$$\{1,4,3,2\} + \{1\}$$$.In the second example, the only way to divide $$$a$$$ into permutations is $$$\{2,4,1,3\} + \{2,1\}$$$.In the third example, there are no possible ways.

let scan_int () = Scanf.scanf " %d" (fun x -> x);;
let scan_int_array_of_size n = Array.init n (fun _ -> scan_int() -1);;
let show_int n = Printf.printf "%d\n" n;;
let show_int_pair (x,y) = Printf.printf "%d %d\n" x y;;

let maximum tab =
	let n = Array.length tab in
	let m = ref 0 in
	for i = 0 to n-1 do if tab.(i) <> 0 then m := i done;
	!m;;

let max_value tab =
	let n = Array.length tab in
	let m = ref 0 in
	for i = 0 to n-1 do if tab.(i) > !m then m := tab.(i) done;
	!m;;

let test_permut tab deb fin =
	let n = Array.length tab in
	let vu = Array.make n false in
	for i = deb to fin - 1 do vu.(tab.(i)) <- true done;
	let test = ref true in
	for i = 0 to (fin - deb)-1 do test := !test && vu.(i) done;
	!test;;

let t = scan_int ();;
for x = 1 to t do
	let n = scan_int () in
	let a = scan_int_array_of_size n in
	let deja_vu = Array.make n 0 in
	if max_value a >= n then show_int 0 else
	begin
		for i = 0 to n-1 do deja_vu.(a.(i)) <- succ deja_vu.(a.(i)) done;
		let m = maximum deja_vu in
		let t1 = test_permut a 0 (m+1) && test_permut a (m+1) n and t2 = test_permut a (n-m-1) n && test_permut a 0 (n-m-1) in
		match t1,t2 with
			| true,true -> if (m+1)<>(n-m-1) then (show_int 2; show_int_pair ((m+1),(n-m-1)); show_int_pair ((n-m-1),(m+1)))
											else (show_int 1; show_int_pair ((m+1),(n-m-1)))
			| true,false -> show_int 1; show_int_pair ((m+1),(n-m-1))
			| false,true -> show_int 1; show_int_pair ((n-m-1),(m+1))
			| false,false -> show_int 0
	end
done;;

let scan_int () = Scanf.scanf " %d" (fun x -> x);;
let scan_int_array_of_size n = Array.init n (fun _ -> scan_int() -1);;
let show_int n = Printf.printf "%d\n" n;;
let show_int_pair (x,y) = Printf.printf "%d %d\n" x y;;

let maximum tab =
	let n = Array.length tab in
	let m = ref 0 in
	for i = 0 to n-1 do if tab.(i) <> 0 then m := i done;
	!m;;

let max_value tab =
	let n = Array.length tab in
	let m = ref 0 in
	for i = 0 to n-1 do if tab.(i) > !m then m := tab.(i) done;
	!m;;

let test_permut tab deb fin =
	let n = Array.length tab in
	let vu = Array.make n false in
	for i = deb to fin - 1 do vu.(tab.(i)) <- true done;
	let test = ref true in
	for i = 0 to (fin - deb)-1 do test := !test && vu.(i) done;
	!test;;

let t = scan_int ();;
for x = 1 to t do
	let n = scan_int () in
	let a = scan_int_array_of_size n in
	let deja_vu = Array.make n 0 in
	if max_value a >= n then show_int 0 else
	begin
		for i = 0 to n-1 do deja_vu.(a.(i)) <- succ deja_vu.(a.(i)) done;
		let m = maximum deja_vu in
		let t1 = test_permut a 0 (m+1) && test_permut a (m+1) n and t2 = test_permut a (n-m-1) n && test_permut a 0 (n-m-1) in
		match t1,t2 with
			| true,true -> show_int 2; show_int_pair ((m+1),(n-m-1)); show_int_pair ((n-m-1),(m+1))
			| true,false -> show_int 1; show_int_pair ((m+1),(n-m-1))
			| false,true -> show_int 1; show_int_pair ((n-m-1),(m+1))
			| false,false -> show_int 0
	end
done;;


Dr. Bruce Banner hates his enemies (like others don't). As we all know, he can barely talk when he turns into the incredible Hulk. That's why he asked you to help him to express his feelings.Hulk likes the Inception so much, and like that his feelings are complicated. They have n layers. The first layer is hate, second one is love, third one is hate and so on...For example if n = 1, then his feeling is "I hate it" or if n = 2 it's "I hate that I love it", and if n = 3 it's "I hate that I love that I hate it" and so on.Please help Dr. Banner.

The only line of the input contains a single integer n (1 ≤ n ≤ 100) — the number of layers of love and hate.

I hate it
I hate that I love it
I hate that I love that I hate it

let input () = Scanf.scanf "%d" (fun n -> n)

let solve input =
    let rec loop i =
        if i <= input
        then
            ((if i mod 2 <> 0
            then
                Printf.printf "I hate "
            else
                Printf.printf "I love ");
            (if i <> input
            then
                Printf.printf "that "
            else
                Printf.printf "it\n");
            loop (i + 1))
        else
            ()
    in loop 1

let () = solve (input ())

let rec get_result n p =
    if n > 1 then
        if p = 1 then
            "I hate that " ^ get_result (n-1) (1-p)
        else
            "I love that " ^ get_result (n-1) (1-p)
    else
        if p = 1 then
            "I hate it\n"
        else
            "I love it\n"

;;

let () =
let n = read_int () in
let () = print_string (get_result n 1) in ();;

let main () =
  let gr () = Scanf.scanf " %d" (fun i -> i) in
  let n = gr () in
  let ph flag =
      match flag with
      | 0 -> "I hate"
      | 1 -> "I love"
  in
  let rec f cnt flag =
      match cnt with
      | 1 -> ph flag
      | cnt -> String.concat " that " [ph flag; f (cnt - 1) (1 - flag)]
  in
  Printf.printf "%s it\n"  (f n 0)
  ;;
let _ = main();;

let rec f t b x =
  if t = x then
    if (b = 1) then Printf.printf "I hate it\n"
    else Printf.printf "I love it\n"
  else
    (if (b = 1) then Printf.printf "I hate that "
    else Printf.printf "I love that ";
    f (t + 1) (1 - b) x)
;;
let _ =
  let x = read_int() in
  f 1 1 x
;;

let text_gen n =
    let rec helper k acc =
        if k == n
        then acc ^ " it"
        else
            if k mod 2 == 0
            then helper (k+1) (acc ^ " that I hate")
            else helper (k+1) (acc ^ " that I love")
    in helper 1 "I hate"
in

let n = read_int() in
Printf.printf "%s\n" (text_gen n)

open Printf
open Scanf

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

  for i = 1 to n do
    if i=1 then printf "I hate "
    else if i land 1 = 1 then printf "that I hate "
    else printf "that I love "
  done;

  printf "it\n"

let input () = Scanf.scanf "%d" (fun n -> n)

let solve input =
    let rec loop i =
        if i <= input
        then
            ((if i mod 2 <> 0
            then
                Printf.printf "I hate "
            else
                Printf.printf "I love ");
            (if i <> input
            then
                Printf.printf "that "
            else
                Printf.printf "it.\n");
            loop (i + 1))
        else
            ()
    in loop 1

let () = solve (input ())