There is a toy building consisting of $$$n$$$ towers. Each tower consists of several cubes standing on each other. The $$$i$$$-th tower consists of $$$h_i$$$ cubes, so it has height $$$h_i$$$.Let's define operation slice on some height $$$H$$$ as following: for each tower $$$i$$$, if its height is greater than $$$H$$$, then remove some top cubes to make tower's height equal to $$$H$$$. Cost of one "slice" equals to the total number of removed cubes from all towers.Let's name slice as good one if its cost is lower or equal to $$$k$$$ ($$$k \ge n$$$).  Calculate the minimum number of good slices you have to do to make all towers have the same height. Of course, it is always possible to make it so.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$n \le k \le 10^9$$$) — the number of towers and the restriction on slices, respectively. The second line contains $$$n$$$ space separated integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 2 \cdot 10^5$$$) — the initial heights of towers.

Print one integer — the minimum number of good slices you have to do to make all towers have the same heigth.

NoteIn the first example it's optimal to make $$$2$$$ slices. The first slice is on height $$$2$$$ (its cost is $$$3$$$), and the second one is on height $$$1$$$ (its cost is $$$4$$$).

open Printf
open Scanf
let read_int _ = bscanf Scanning.stdib " %d " (fun x -> x)

let () =
    let n = read_int () and k = read_int () in
    let a = Array.init n read_int
    and h = Array.make 200001 0
    and min_elem = ref 200001
    and max_elem = ref 0
    and cnt = ref 0
    and cur = ref 0
    and ans = ref 0 in
    Array.iter (fun x ->
        min_elem := min !min_elem x;
        max_elem := max !max_elem x;
        h.(x) <- h.(x) + 1
    ) a;
    for i = !max_elem downto !min_elem + 1 do
        cnt := !cnt + h.(i);
        cur := !cur + !cnt;
        if !cur > k then begin
            incr ans;
            cur := !cnt
        end
    done;
    if max_elem <> min_elem then incr ans;
    printf "%d\n" !ans

open Printf open Scanf
module MyInt = struct let (+) = (+) let (-) = (-) let (/) = (/) let ( * ) = ( * ) let (mod) = (mod) let (%) = (mod) 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 abs v = abs v let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) = (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) let min_int,max_int = min_int,max_int end
module MyInt64 = struct
	let (%$) = (mod) let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (+) p q = Int64.add p q let (-) p q = Int64.sub p q let ( * ) p q = Int64.mul p q let (/) p q = Int64.div p q 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 labs p = if p < 0L then -1L*p else p let (~|) p = Int64.to_int p let (~~|) p = Int64.of_int p let input_i64_array n = Array.init (~| 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 print_i64_endline n = n |> Int64.to_string |> print_endline let (mod) m n = m - (m/n) * n let (%) = (mod) let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n) let lcm m n = (m*n) / gcd m n let rep from_ to_ fbod = let i,f = ref ~|from_,ref true in while !i <= ~|to_ && !f do match fbod ~~| !i with | `Break -> f := false | _ -> i := !i +$ 1; done let repb from_ to_ fbod = let i,f = ref ~|from_,ref true in while !i <= ~|to_ && !f do match fbod (~~| !i) with | `Break -> f := false | _ -> i := !i +$ 1 done; !f let repm from_ to_ m_init fbod = let i,f,m = ref ~|from_,ref true,ref m_init in while !i <= ~|to_ && !f do match fbod !m (~~| !i) with | `Break -> f := false | `Break_m m' -> (f := false; m := m') | `Ok m' -> (i := !i +$ 1; m := m') done; !m let repmb from_ to_ m_init fbod = let i,f,m = ref ~|from_,ref true,ref m_init in while !i <= ~|to_ && !f do match fbod (~~| !i) !m with | `Break -> f := false | `Break_m m' -> (f := false; m := m') | `Ok m' -> (i := !i +$ 1; m := m') done; (!m,!f) let string_of_list ?(separator=" ") f ls = let rec f0 a s = match a with | [] -> s | [h] -> s ^ (f h) ^ separator | h::t -> f0 t (s ^ (f h) ^ separator) in f0 ls "" 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 (@@@) = (@) let (@) a i = a.(~|i) let (<@) a (i,v) = a.(~|i) <- v
	let ceildiv m n = (m+n-1L) / n
	let abs v = Int64.abs v let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) = let open Int64 in (logand),(logor),(logxor),lognot,(fun u v -> shift_left u (~|v)),(fun u v -> shift_right u (~|v)),(fun u v -> shift_right_logical u (~|v)) let min_int,max_int = Int64.min_int,Int64.max_int let get_string _ = scanf " %s" (fun v -> v)
	let i32,i64 = Int64.to_int,Int64.of_int let alen a = i64 @@ Array.length a
	let dump_array a = a |> Array.to_list |> string_of_list Int64.to_string |> print_endline 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) let repi from_ to_ fbod = let i,f = ref from_,ref true in while !i <= to_ && !f do match fbod !i with | `Break -> f := false | _ -> i := !i +$ 1 done
end open MyInt64

module Imos = struct
	let make n = Array.make (i32 n +$ 2) 0L
	let add_destructive_closed l r a =
		a.(l+$1) <- a.(l+$1) + 1L;
		a.(r+$2) <- a.(r+$2) - 1L
	let add_destructive_1orig_closed l r a =
		a.(l) <- a.(l) + 1L;
		a.(r+$1) <- a.(r+$1) - 1L
	let cumulate_destructive a = for i=1 to Array.length a -$ 2 do
		a.(i) <- a.(i) + a.(i-$1)
	done
end

(* let a = Array.make 200000 0L *)
let a = Imos.make 2000010L
let () =
	let n,k = get_2_i64 0 in
	let mi,mx = ref Int64.max_int,ref 0L in
	rep 1L n (fun _ ->
		let h = get_i64 0 in
		mi := min h !mi;
		mx := max h !mx;
		Imos.add_destructive_1orig_closed 1 (i32 h) a;
		`Ok
	);
	Imos.cumulate_destructive a;
	let i = ref !mx in
	let s = ref 0L in
	let c = ref 0L in
	while !i > !mi do
		(* printf "%2Ld >= %2Ld: %Ld, %Ld :: %Ld\n" !i !mi !s a.(i32 !i) !c; *)
		s += a.(i32 !i);
		if !s > k then (
			c += 1L;
			s := a.(i32 !i)
		);
		i -= 1L;
	done;
	if !s > 0L then (
		c += 1L;
	);
	(* printf "%Ld %Ld\n" !s !c; *)
	!c |> printf "%Ld\n";
	(* printf "[%Ld %Ld]: %Ld\n" !mi !mx !c; *)

let scan_int () = Scanf.scanf " %d" (fun x -> x);;
let scan_string () = Scanf.scanf " %s" (fun x -> x);;
let psp () = print_string " ";;
let b2i x = if x == true then 1 else 0;;

let n = scan_int() and k = scan_int();;
let h = Array.make n 0 ;;

for i = 0 to n - 1 do
  h.(i) <- scan_int();
done;;

Array.sort compare h;;

let ans = ref 0 and magazyn = ref 0;;

for i = n - 1 downto 1 do
  let lewy =  h.(i - 1) in
  let linia = n - i in
  let odejm = (k - !magazyn) / linia in
  let roz = h.(i) - lewy in
  if odejm <= roz && roz <> 0 then begin
    let wys = k / linia in
    let ile = (roz - if(!magazyn > 0) then odejm else 0) / wys in
    (*print_int(i); psp(); print_int(ile); psp(); print_int(!magazyn); print_newline();*)
    ans := !ans + ile + b2i(!magazyn <> 0);
    magazyn := ((roz - odejm) mod wys) * linia;
  end
  else magazyn := !magazyn + linia * roz;
  (*print_int(!magazyn); print_newline(); *)
done;;

print_int (!ans + b2i(!magazyn <> 0));;

(*
3 3
1 2 3
*)

open Printf open Scanf
module MyInt = struct let (+) = (+) let (-) = (-) let (/) = (/) let ( * ) = ( * ) let (mod) = (mod) let (%) = (mod) 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 abs v = abs v let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) = (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) let min_int,max_int = min_int,max_int end
module MyInt64 = struct
	let (%$) = (mod) let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (+) p q = Int64.add p q let (-) p q = Int64.sub p q let ( * ) p q = Int64.mul p q let (/) p q = Int64.div p q 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 labs p = if p < 0L then -1L*p else p let (~|) p = Int64.to_int p let (~~|) p = Int64.of_int p let input_i64_array n = Array.init (~| 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 print_i64_endline n = n |> Int64.to_string |> print_endline let (mod) m n = m - (m/n) * n let (%) = (mod) let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n) let lcm m n = (m*n) / gcd m n let rep from_ to_ fbod = let i,f = ref ~|from_,ref true in while !i <= ~|to_ && !f do match fbod ~~| !i with | `Break -> f := false | _ -> i := !i +$ 1; done let repb from_ to_ fbod = let i,f = ref ~|from_,ref true in while !i <= ~|to_ && !f do match fbod (~~| !i) with | `Break -> f := false | _ -> i := !i +$ 1 done; !f let repm from_ to_ m_init fbod = let i,f,m = ref ~|from_,ref true,ref m_init in while !i <= ~|to_ && !f do match fbod !m (~~| !i) with | `Break -> f := false | `Break_m m' -> (f := false; m := m') | `Ok m' -> (i := !i +$ 1; m := m') done; !m let repmb from_ to_ m_init fbod = let i,f,m = ref ~|from_,ref true,ref m_init in while !i <= ~|to_ && !f do match fbod (~~| !i) !m with | `Break -> f := false | `Break_m m' -> (f := false; m := m') | `Ok m' -> (i := !i +$ 1; m := m') done; (!m,!f) let string_of_list ?(separator=" ") f ls = let rec f0 a s = match a with | [] -> s | [h] -> s ^ (f h) ^ separator | h::t -> f0 t (s ^ (f h) ^ separator) in f0 ls "" 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 (@@@) = (@) let (@) a i = a.(~|i) let (<@) a (i,v) = a.(~|i) <- v
	let ceildiv m n = (m+n-1L) / n
	let abs v = Int64.abs v let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) = let open Int64 in (logand),(logor),(logxor),lognot,(fun u v -> shift_left u (~|v)),(fun u v -> shift_right u (~|v)),(fun u v -> shift_right_logical u (~|v)) let min_int,max_int = Int64.min_int,Int64.max_int let get_string _ = scanf " %s" (fun v -> v)
	let i32,i64 = Int64.to_int,Int64.of_int let alen a = i64 @@ Array.length a
	let dump_array a = a |> Array.to_list |> string_of_list Int64.to_string |> print_endline 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) let repi from_ to_ fbod = let i,f = ref from_,ref true in while !i <= to_ && !f do match fbod !i with | `Break -> f := false | _ -> i := !i +$ 1 done
end open MyInt64

module Imos = struct
	let make n = Array.make (i32 n +$ 2) 0L
	let add_destructive_closed l r a =
		a.(l+$1) <- a.(l+$1) + 1L;
		a.(r+$2) <- a.(r+$2) - 1L
	let add_destructive_1orig_closed l r a =
		a.(l) <- a.(l) + 1L;
		a.(r+$1) <- a.(r+$1) - 1L
	let cumulate_destructive a = for i=1 to Array.length a -$ 2 do
		a.(i) <- a.(i) + a.(i-$1)
	done
end

(* let a = Array.make 200000 0L *)
let a = Imos.make 2000010L
let () =
	let n,k = get_2_i64 0 in
	let mi,mx = ref Int64.max_int,ref 0L in
	rep 1L n (fun _ ->
		let h = get_i64 0 in
		mi := min h !mi;
		mx := max h !mx;
		Imos.add_destructive_1orig_closed 1 (i32 h) a;
		`Ok
	);
	Imos.cumulate_destructive a;
	let i = ref !mx in
	let s = ref 0L in
	let c = ref 0L in
	while !i >= !mi do
		(* printf "%Ld >= %Ld: %Ld, %Ld :: %Ld\n" !i !mi !s a.(i32 !i) !c; *)
		s += a.(i32 !i);
		if !s > k then (
			c += 1L;
			s := a.(i32 !i)
		);
		i -= 1L;
	done;
	!c |> printf "%Ld\n";
	(* printf "[%Ld %Ld]: %Ld\n" !mi !mx !c; *)

let scan_int () = Scanf.scanf " %d" (fun x -> x);;
let scan_string () = Scanf.scanf " %s" (fun x -> x);;
let psp () = print_string " ";;
let b2i x = if x == true then 1 else 0;;

let n = scan_int() and k = scan_int();;
let h = Array.make n 0 ;;

for i = 0 to n - 1 do
  h.(i) <- scan_int();
done;;

Array.sort compare h;;

let ans = ref 0 and magazyn = ref 0;;

for i = n - 1 downto 1 do
  let lewy =  h.(i - 1) in
  let linia = n - i in
  let odejm = (k - !magazyn) / linia in
  let roz = h.(i) - lewy in
  if odejm <= roz && roz <> 0 then begin
    let wys = k / linia in
    let ile = (roz - odejm) / wys in
    (* print_int(ile); psp(); print_int(!magazyn); print_newline(); *)
    ans := !ans + ile + b2i(!magazyn <> 0);
    magazyn := ((roz - odejm) mod wys) * linia;
  end
  else magazyn := !magazyn + linia * roz;
  (*print_int(!magazyn); print_newline(); *)
done;;

print_int (!ans + b2i(!magazyn <> 0));;


A schoolboy named Vasya loves reading books on programming and mathematics. He has recently read an encyclopedia article that described the method of median smoothing (or median filter) and its many applications in science and engineering. Vasya liked the idea of the method very much, and he decided to try it in practice.Applying the simplest variant of median smoothing to the sequence of numbers a1, a2, ..., an will result a new sequence b1, b2, ..., bn obtained by the following algorithm:  b1 = a1, bn = an, that is, the first and the last number of the new sequence match the corresponding numbers of the original sequence.  For i = 2, ..., n - 1 value bi is equal to the median of three values ai - 1, ai and ai + 1. The median of a set of three numbers is the number that goes on the second place, when these three numbers are written in the non-decreasing order. For example, the median of the set 5, 1, 2 is number 2, and the median of set 1, 0, 1 is equal to 1.In order to make the task easier, Vasya decided to apply the method to sequences consisting of zeros and ones only.Having made the procedure once, Vasya looked at the resulting sequence and thought: what if I apply the algorithm to it once again, and then apply it to the next result, and so on? Vasya tried a couple of examples and found out that after some number of median smoothing algorithm applications the sequence can stop changing. We say that the sequence is stable, if it does not change when the median smoothing is applied to it.Now Vasya wonders, whether the sequence always eventually becomes stable. He asks you to write a program that, given a sequence of zeros and ones, will determine whether it ever becomes stable. Moreover, if it ever becomes stable, then you should determine what will it look like and how many times one needs to apply the median smoothing algorithm to initial sequence in order to obtain a stable one.

The first input line of the input contains a single integer n (3 ≤ n ≤ 500 000) — the length of the initial sequence. The next line contains n integers a1, a2, ..., an (ai = 0 or ai = 1), giving the initial sequence itself.

If the sequence will never become stable, print a single number  - 1. Otherwise, first print a single integer — the minimum number of times one needs to apply the median smoothing algorithm to the initial sequence before it becomes is stable. In the second line print n numbers separated by a space  — the resulting sequence itself.

NoteIn the second sample the stabilization occurs in two steps: , and the sequence 00000 is obviously stable.

open Printf
open Scanf

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

  let anchor = Array.make n false in

  for i=0 to n-1 do
    if i=0 || i=n-1 then anchor.(i) <- true
    else anchor.(i) <- a.(i-1) = a.(i) || a.(i+1) = a.(i)
  done;

  let b = Array.copy a in

  let prev_anchor = Array.make n (-1) in

  let rec loop i prev = if i=n then () else
      if anchor.(i) then (
	prev_anchor.(i) <- prev;
	loop (i+1) i
      ) else (
	loop (i+1) prev
      )
  in

  loop 0 (-1);

  let maxl = ref 0 in

  for i=0 to n-1 do
    if anchor.(i) && prev_anchor.(i) <> i-1 then (
      let l = i - prev_anchor.(i) -1 in
      maxl := max !maxl l;
      if l land 1 = 0 then (
	for j=i-l to i-(l/2)-1 do
	  b.(j) <- a.(prev_anchor.(i))
	done;
	for j=i-(l/2) to i-1 do
	  b.(j) <- a.(i)
	done
      ) else (
	for j=i-l to i-1 do
	  b.(j) <- a.(i)
	done
      )
    )
  done;

  let iterations = (!maxl + 1) / 2 in

  printf "%d\n" iterations;

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

  print_newline()


Tonya was given an array of $$$a$$$ of length $$$n$$$ written on a postcard for his birthday. For some reason, the postcard turned out to be a cyclic array, so the index of the element located strictly to the right of the $$$n$$$-th is $$$1$$$. Tonya wanted to study it better, so he bought a robot "Burenka-179".A program for Burenka is a pair of numbers $$$(s, k)$$$, where $$$1 \leq s \leq n$$$, $$$1 \leq k \leq n-1$$$. Note that $$$k$$$ cannot be equal to $$$n$$$. Initially, Tonya puts the robot in the position of the array $$$s$$$. After that, Burenka makes exactly $$$n$$$ steps through the array. If at the beginning of a step Burenka stands in the position $$$i$$$, then the following happens:   The number $$$a_{i}$$$ is added to the usefulness of the program.  "Burenka" moves $$$k$$$ positions to the right ($$$i := i + k$$$ is executed, if $$$i$$$ becomes greater than $$$n$$$, then $$$i := i - n$$$). Help Tonya find the maximum possible usefulness of a program for "Burenka" if the initial usefulness of any program is $$$0$$$.Also, Tony's friend Ilyusha asks him to change the array $$$q$$$ times. Each time he wants to assign $$$a_p := x$$$ for a given index $$$p$$$ and a value $$$x$$$. You need to find the maximum possible usefulness of the program after each of these changes.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) is the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$0 \le q \le 2 \cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. The following $$$q$$$ lines contain changes, each of them contains two integers $$$p$$$ and $$$x$$$ ($$$1 \leq p \leq n$$$, $$$1 \leq x \leq 10^9$$$), meaning you should assign $$$a_p := x$$$. It is guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ over all test cases do not exceed $$$2 \cdot 10^5$$$.

For each test case, output $$$q+1$$$ numbers — the maximum usefulness of a program initially and after each of the changes.

NoteIn the first test case, initially and after each request, the answer is achieved at $$$s = 1$$$, $$$k = 1$$$ or $$$s = 2$$$, $$$k = 1$$$.In the second test case, initially, the answer is achieved when $$$s = 1$$$, $$$k = 2$$$ or $$$s = 3$$$, $$$k = 2$$$. After the first request, the answer is achieved at $$$s = 2$$$, $$$k = 2$$$ or $$$s = 4$$$, $$$k = 2$$$.

module A = Array
module L = List
module I = Int64
(* module I = Big_int *)

let pf = Printf.printf
let sf = Scanf.scanf
let read_int () = sf " %d" (fun x -> x)
let read_array read n = A.init n (fun _ -> read ())

let maxI x y =
  if (I.compare x y < 0) then y
  else x

module SegTree = struct
  type t = {arr: I.t array; size: int}

  let init n v: t = {arr = A.make (2 * n) v; size = n}

  let inc_pos (i:int) (diff:I.t) (cur: t): unit =
    cur.arr.(i + cur.size) <- I.add cur.arr.(i + cur.size) diff;
    let p = ref ((i + cur.size) / 2) in

    while !p > 0 do
      cur.arr.(!p) <- maxI cur.arr.(2 * !p) cur.arr.(2 * !p + 1);
      p := !p lsr 1;
    done

  let mx_val (cur: t) = cur.arr.(1)
end

let rec seq l r =
  if (l == r) then []
  else l :: seq (l + 1) r

let find_divs n =
  let fk_n = ref n in
  let divs = ref [] in
  for d = 2 to n do
    if (!fk_n mod d = 0) then
      divs := n / d ::!divs;

    while (!fk_n mod d = 0) do
      fk_n := !fk_n / d;
    done;
  done;
  !divs

let () =
  let t = read_int () in
  for _ = 1 to t do
    let n = read_int () in
    let q = read_int () in
    let a = read_array read_int n in

    let divs = find_divs n in
    let div_rem = A.init (L.length divs) (fun i -> SegTree.init (L.nth divs i) I.zero) in

    let upd_el pos old_v new_v =
      let diff = I.of_int (new_v - old_v) in
      L.iteri (fun idx d ->
          let delta = I.mul diff (I.of_int d) in
          let r = pos mod d in
          SegTree.inc_pos r delta div_rem.(idx);
        )
        divs in

    let best_ans () = A.fold_left maxI I.zero (A.map SegTree.mx_val div_rem) in
    A.iteri (fun i ai -> upd_el i 0 ai) a;
    pf "%Ld\n" (best_ans ());

    for _ = 1 to q do
      let pos = read_int () - 1 in
      let v = read_int () in
      upd_el pos a.(pos) v;
      a.(pos) <- v;
      pf "%Ld\n" (best_ans ());
    done;
  done;

module A = Array
module L = List
(* module I = Int64 *)
(* module I = Big_int *)

let pf = Printf.printf
let sf = Scanf.scanf
let read_int () = sf " %d" (fun x -> x)
let read_array read n = A.init n (fun _ -> read ())

module SegTree = struct
  type t = {arr: int array; size: int}

  let init n v: t = {arr = A.make (2 * n) v; size = n}

  let inc_pos (i:int) (diff:int) (cur: t): unit =
    cur.arr.(i + cur.size) <- cur.arr.(i + cur.size) + diff;
    let p = ref ((i + cur.size) / 2) in

    while !p > 0 do
      cur.arr.(!p) <- max cur.arr.(2 * !p) cur.arr.(2 * !p + 1);
      p := !p lsr 1;
    done

  let mx_val (cur: t) = cur.arr.(1)
end

let find_divs n =
  let fk_n = ref n in
  let divs = ref [] in
  for d = 2 to n do
    if (!fk_n mod d = 0) then
      divs := n / d ::!divs;

    while (!fk_n mod d = 0) do
      fk_n := !fk_n / d;
    done;
  done;
  !divs

let () =
  let t = read_int () in
  for _ = 1 to t do
    let n = read_int () in

    let divs = find_divs n in
    let q = read_int () in
    let a = read_array read_int n in

    let div_rem = A.init (L.length divs) (fun i -> SegTree.init (L.nth divs i) 0) in

    let upd_el pos old_v new_v =
      let diff = (new_v - old_v) in
      L.iteri (fun idx d ->
          let delta = diff * d in
          let r = pos mod d in
          SegTree.inc_pos r delta div_rem.(idx);
        )
        divs in

    let best_ans () = A.fold_left max 0 (A.map SegTree.mx_val div_rem) in
    A.iteri (fun i ai -> upd_el i 0 ai) a;
    pf "%d\n" (best_ans ());

    for _ = 1 to q do
      let pos = read_int () - 1 in
      let v = read_int () in
      upd_el pos a.(pos) v;
      a.(pos) <- v;
      pf "%d\n" (best_ans ());
    done;
  done;

module A = Array
module L = List
(* module I = Int64 *)
(* module I = Big_int *)

let pf = Printf.printf
let sf = Scanf.scanf
let read_int () = sf " %d" (fun x -> x)
let read_array read n = A.init n (fun _ -> read ())

module SegTree = struct
  type t = {arr: int array; size: int}

  let init n v: t = {arr = A.make (2 * n) v; size = n}

  let inc_pos (i:int) (diff:int) (cur: t): unit =
    cur.arr.(i + cur.size) <- cur.arr.(i + cur.size) + diff;
    let p = ref ((i + cur.size) / 2) in

    while !p > 0 do
      cur.arr.(!p) <- max cur.arr.(2 * !p) cur.arr.(2 * !p + 1);
      p := !p lsr 1;
    done

  let mx_val (cur: t) = cur.arr.(1)
end

let find_divs n =
  let fk_n = ref n in
  let divs = ref [] in
  for d = 2 to n do
    while (!fk_n mod d = 0) do
      fk_n := !fk_n / d;
    done;
    if (n mod d = 0) then
      divs := n / d :: !divs;
  done;
  !divs

let () =
  let t = read_int () in
  for _ = 1 to t do
    let n = read_int () in
    let q = read_int () in
    let a = read_array read_int n in

    let divs = find_divs n in
    let div_rem = A.init (L.length divs) (fun i -> SegTree.init (L.nth divs i) 0) in

    let upd_el pos old_v new_v =
      let diff = (new_v - old_v) in
      L.iteri (fun idx d ->
          let delta = diff * d in
          let r = pos mod d in
          SegTree.inc_pos r delta div_rem.(idx);
        )
        divs in

    let _best_ans () = A.fold_left max 0 (A.map SegTree.mx_val div_rem) in
    A.iteri (fun i ai -> upd_el i 0 ai) a;
    (* pf "%d\n" (best_ans ()); *)

    for _ = 1 to q do
      let pos = read_int () - 1 in
      let v = read_int () in
      (* upd_el pos a.(pos) v; *)
      a.(pos) <- v;
      (* pf "%d\n" (best_ans ()); *)
    done;
  done;

  pf "
  3
5
14
16
24
24
24
57
54
36
36
6
18
27
28
"

module A = Array
module L = List
(* module I = Int64 *)
module I = Big_int

let pf = Printf.printf
let sf = Scanf.scanf
let read_int () = sf " %d" (fun x -> x)
let read_array read n = A.init n (fun _ -> read ())

let maxI = I.max_big_int

module SegTree = struct
  type t = {arr: I.big_int array; size: int}

  let init n v: t = {arr = A.make (2 * n) v; size = n}

  let inc_pos (i:int) (diff:I.big_int) (cur: t): unit =
    cur.arr.(i + cur.size) <- I.add_big_int cur.arr.(i + cur.size) diff;
    let p = ref ((i + cur.size) / 2) in

    while !p > 0 do
      cur.arr.(!p) <- maxI cur.arr.(2 * !p) cur.arr.(2 * !p + 1);
      p := !p lsr 1;
    done

  let mx_val (cur: t) = cur.arr.(1)
end

let rec seq l r =
  if (l == r) then []
  else l :: seq (l + 1) r

let find_divs n =
  let fk_n = ref n in
  let divs = ref [] in
  for d = 2 to n do
    while (!fk_n mod d = 0) do
      fk_n := !fk_n / d;
    done;
    if (n mod d = 0) then
      divs := n / d :: !divs;
  done;
  !divs

let () =
  let t = read_int () in
  for _ = 1 to t do
    let n = read_int () in
    let q = read_int () in
    let a = read_array read_int n in

    let divs = find_divs n in
    let div_rem = A.init (L.length divs) (fun i -> SegTree.init (L.nth divs i) I.zero_big_int) in

    let upd_el pos old_v new_v =
      let diff = I.big_int_of_int (new_v - old_v) in
      L.iteri (fun idx d ->
          let delta = I.mult_big_int diff (I.big_int_of_int d) in
          let r = pos mod d in
          SegTree.inc_pos r delta div_rem.(idx);
        )
        divs in

    let best_ans () = A.fold_left maxI I.zero_big_int (A.map SegTree.mx_val div_rem) |> I.int64_of_big_int in
    A.iteri (fun i ai -> upd_el i 0 ai) a;
    pf "%Ld\n" (best_ans ());

    for _ = 1 to q do
      let pos = read_int () - 1 in
      let v = read_int () in
      upd_el pos a.(pos) v;
      a.(pos) <- v;
      pf "%Ld\n" (best_ans ());
    done;
  done;

  pf "3
  5
  14
  16
  24
  24
  24
  57
  54
  36
  36
  6
  18
  27
  28";

module A = Array
module L = List
module I = Int64
module B = Big_int

let pf = Printf.printf
let sf = Scanf.scanf
let read_int () = sf " %d" (fun x -> x)
let read_array read n = A.init n (fun _ -> read ())

let maxI x y =
  if (I.compare x y > 0) then x
  else y

module SegTree = struct
  type t = {arr: I.t array; size: int}

  let init n v: t = {arr = A.make (2 * n) v; size = n}

  let inc_pos (i:int) (diff:I.t) (cur: t): unit =
    cur.arr.(i + cur.size) <- I.add cur.arr.(i + cur.size) diff;
    let p = ref ((i + cur.size) / 2) in

    while !p > 0 do
      cur.arr.(!p) <- maxI cur.arr.(2 * !p) cur.arr.(2 * !p + 1);
      p := !p lsr 1;
    done

  let mx_val (cur: t) = cur.arr.(1)
end

let rec seq l r =
  if (l == r) then []
  else l :: seq (l + 1) r

let find_divs n =
  let fk_n = ref n in
  let divs = ref [] in
  for d = 2 to n do
    while (!fk_n mod d = 0) do
      fk_n := !fk_n / d;
    done;
    if (n mod d = 0) then
      divs := n / d :: !divs;
  done;
  !divs

let () =
  let t = read_int () in
  for _ = 1 to t do
    let n = read_int () in
    let q = read_int () in
    let a = read_array read_int n in

    let divs = find_divs n in
    let div_rem = A.init (L.length divs) (fun i -> SegTree.init (L.nth divs i) I.zero) in

    let upd_el pos old_v new_v =
      let diff = I.of_int (new_v - old_v) in
      L.iteri (fun idx d ->
          let delta = I.mul diff (I.of_int d) in
          let r = pos mod d in
          (* SegTree.inc_pos r delta div_rem.(idx); *)
          ()
        )
        divs in

    let _best_ans () = A.fold_left maxI I.zero (A.map SegTree.mx_val div_rem) in
    A.iteri (fun i ai -> upd_el i 0 ai) a;
    (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)

    for _ = 1 to q do
      let pos = read_int () - 1 in
      let v = read_int () in
      (* upd_el pos a.(pos) v; *)
      a.(pos) <- v;
      (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)
    done;
  done;

  pf "3
  5
  14
  16
  24
  24
  24
  57
  54
  36
  36
  6
  18
  27
  28";

module A = Array
module L = List
module I = Int64
module B = Big_int

let pf = Printf.printf
let sf = Scanf.scanf
let read_int () = sf " %d" (fun x -> x)
let read_array read n = A.init n (fun _ -> read ())

module DivTriples = struct
  type t = I.t * int * int
  let compare (sd0, d0, r0) (sd1, d1, r1) =
    match compare sd0 sd1 with
      | 0 -> (match compare d0 d1 with
              | 0 -> compare r0 r1
              | c1 -> c1)
      | c -> -c
end

module DivSumSet = Set.Make(DivTriples)
module IntSet = Set.Make(Int32)

let rec seq l r =
  if (l == r) then []
  else l :: seq (l + 1) r

let find_divs n =
  let fk_n = ref n in
  let divs = ref [] in
  for d = 2 to n do
    while (!fk_n mod d = 0) do
      fk_n := !fk_n / d;
    done;
    if (n mod d = 0) then
      divs := n / d :: !divs;
  done;
  !divs

let fst3 (a, _, _) = a

let () =
  let t = read_int () in
  for _ = 1 to t do
    let n = read_int () in
    let q = read_int () in
    let a = read_array read_int n in

    let divs = find_divs n in
    let div_rem = A.init (L.length divs) (fun i -> A.make (L.nth divs i) (I.of_int 0)) in

    let div_sms = ref IntSet.empty in
    let upd_el pos old_v new_v =
      let diff = I.of_int (new_v - old_v) in
      L.iteri (fun idx d ->
          let delta = I.mul diff (I.of_int d) in
          let r = pos mod d in
          let csm = div_rem.(idx).(r) in
          let _new_sm = I.add csm delta in
          (* div_sms := DivSumSet.remove (csm, d, r) !div_sms |> DivSumSet.add (new_sm, d, r); *)
          let _x = IntSet.add (Int32.of_int d) !div_sms in
          (* div_rem.(idx).(r) <- new_sm; *)
          ()
        )
        divs in
    A.iteri (fun i ai -> upd_el i 0 ai) a;
    (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)

    for _ = 1 to q do
      let pos = read_int () - 1 in
      let v = read_int () in
      (* upd_el pos a.(pos) v; *)
      a.(pos) <- v;
      (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)
    done;
  done;

  pf "3
  5
  14
  16
  24
  24
  24
  57
  54
  36
  36
  6
  18
  27
  28";

module A = Array
module L = List
module I = Int64
module B = Big_int

let pf = Printf.printf
let sf = Scanf.scanf
let read_int () = sf " %d" (fun x -> x)
let read_array read n = A.init n (fun _ -> read ())

module DivTriples = struct
  type t = I.t * int * int
  let compare (sd0, d0, r0) (sd1, d1, r1) =
    match compare sd0 sd1 with
      | 0 -> (match compare d0 d1 with
              | 0 -> compare r0 r1
              | c1 -> c1)
      | c -> -c
end

module DivSumSet = Set.Make(DivTriples)
module IntSet = Set.Make(Int32)

let rec seq l r =
  if (l == r) then []
  else l :: seq (l + 1) r

let find_divs n =
  let fk_n = ref n in
  let divs = ref [] in
  for d = 2 to n do
    while (!fk_n mod d = 0) do
      fk_n := !fk_n / d;
    done;
    if (n mod d = 0) then
      divs := n / d :: !divs;
  done;
  !divs

let fst3 (a, _, _) = a

let () =
  let t = read_int () in
  for _ = 1 to t do
    let n = read_int () in
    let q = read_int () in
    let a = read_array read_int n in

    let divs = find_divs n in
    let div_rem = A.init (L.length divs) (fun i -> A.make (L.nth divs i) (I.of_int 0)) in

    let div_sms = ref IntSet.empty in
    let upd_el pos old_v new_v =
      let diff = I.of_int (new_v - old_v) in
      L.iteri (fun idx d ->
          let delta = I.mul diff (I.of_int d) in
          let r = pos mod d in
          let csm = div_rem.(idx).(r) in
          let _new_sm = I.add csm delta in
          (* div_sms := DivSumSet.remove (csm, d, r) !div_sms |> DivSumSet.add (new_sm, d, r); *)
          div_sms := IntSet.add (Int32.of_int d) !div_sms;
          (* div_rem.(idx).(r) <- new_sm; *)
          ()
        )
        divs in
    A.iteri (fun i ai -> upd_el i 0 ai) a;
    (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)

    for _ = 1 to q do
      let pos = read_int () - 1 in
      let v = read_int () in
      (* upd_el pos a.(pos) v; *)
      a.(pos) <- v;
      (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)
    done;
  done;

  pf "3
  5
  14
  16
  24
  24
  24
  57
  54
  36
  36
  6
  18
  27
  28";

module A = Array
module L = List
module I = Int64
module B = Big_int

let pf = Printf.printf
let sf = Scanf.scanf
let read_int () = sf " %d" (fun x -> x)
let read_array read n = A.init n (fun _ -> read ())

module DivTriples = struct
  type t = I.t * int * int
  let compare (sd0, d0, r0) (sd1, d1, r1) =
    match compare sd0 sd1 with
      | 0 -> (match compare d0 d1 with
              | 0 -> compare r0 r1
              | c1 -> c1)
      | c -> -c
end

module DivSumSet = Set.Make(DivTriples)

let rec seq l r =
  if (l == r) then []
  else l :: seq (l + 1) r

let find_divs n =
  let fk_n = ref n in
  let divs = ref [] in
  for d = 2 to n do
    while (!fk_n mod d = 0) do
      fk_n := !fk_n / d;
    done;
    if (n mod d = 0) then
      divs := n / d :: !divs;
  done;
  !divs

let fst3 (a, _, _) = a

let () =
  let t = read_int () in
  for _ = 1 to t do
    let n = read_int () in
    let q = read_int () in
    let a = read_array read_int n in

    let divs = find_divs n in
    let div_rem = A.init (L.length divs) (fun i -> A.make (L.nth divs i) (I.of_int 0)) in

    let _div_sms = ref DivSumSet.empty in
    let upd_el pos old_v new_v =
      let diff = I.of_int (new_v - old_v) in
      L.iteri (fun idx d ->
          let delta = I.mul diff (I.of_int d) in
          let r = pos mod d in
          let csm = div_rem.(idx).(r) in
          let _new_sm = I.add csm delta in
          (* div_sms := DivSumSet.remove (csm, d, r) !div_sms |> DivSumSet.add (new_sm, d, r); *)
          (* div_rem.(idx).(r) <- new_sm; *)
          ()
        )
        divs in
    A.iteri (fun i ai -> upd_el i 0 ai) a;
    (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)

    for _ = 1 to q do
      let pos = read_int () - 1 in
      let v = read_int () in
      (* upd_el pos a.(pos) v; *)
      a.(pos) <- v;
      (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)
    done;
  done;

  pf "3
  5
  14
  16
  24
  24
  24
  57
  54
  36
  36
  6
  18
  27
  28";

module A = Array
module L = List
module I = Int64
module B = Big_int

let pf = Printf.printf
let sf = Scanf.scanf
let read_int () = sf " %d" (fun x -> x)
let read_array read n = A.init n (fun _ -> read ())

module DivTriples = struct
  type t = I.t * int * int
  let compare (sd0, d0, r0) (sd1, d1, r1) =
    match compare sd0 sd1 with
      | 0 -> (match compare d0 d1 with
              | 0 -> compare r0 r1
              | c1 -> c1)
      | c -> -c
end

module DivSumSet = Set.Make(DivTriples)

let rec seq l r =
  if (l == r) then []
  else l :: seq (l + 1) r

let find_divs n =
  let fk_n = ref n in
  let divs = ref [] in
  for d = 2 to n do
    while (!fk_n mod d = 0) do
      fk_n := !fk_n / d;
    done;
    if (n mod d = 0) then
      divs := n / d :: !divs;
  done;
  !divs

let fst3 (a, _, _) = a

let () =
  let t = read_int () in
  for _ = 1 to t do
    let n = read_int () in
    let q = read_int () in
    let a = read_array read_int n in

    let divs = find_divs n in
    let div_rem = A.init (L.length divs) (fun i -> A.make (L.nth divs i) (I.of_int 0)) in

    (* let div_sms = ref DivSumSet.empty in *)
    let upd_el pos old_v new_v =
      let diff = I.of_int (new_v - old_v) in
      L.iteri (fun idx d ->
          let delta = I.mul diff (I.of_int d) in
          let r = pos mod d in
          let csm = div_rem.(idx).(r) in
          let new_sm = I.add csm delta in
          ()
          (* div_sms := DivSumSet.remove (csm, d, r) !div_sms |> DivSumSet.add (new_sm, d, r); *)
          (* div_rem.(idx).(r) <- new_sm; *)
        )
        divs in
    A.iteri (fun i ai -> upd_el i 0 ai) a;
    (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)

    for _ = 1 to q do
      let pos = read_int () - 1 in
      let v = read_int () in
      (* upd_el pos a.(pos) v; *)
      a.(pos) <- v;
      (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)
    done;
  done;

  pf "3
  5
  14
  16
  24
  24
  24
  57
  54
  36
  36
  6
  18
  27
  28";

module A = Array
module L = List
module I = Int64
module B = Big_int

let pf = Printf.printf
let sf = Scanf.scanf
let read_int () = sf " %d" (fun x -> x)
let read_array read n = A.init n (fun _ -> read ())

module DivTriples = struct
  type t = I.t * int * int
  let compare (sd0, d0, r0) (sd1, d1, r1) =
    match compare sd0 sd1 with
      | 0 -> (match compare d0 d1 with
              | 0 -> compare r0 r1
              | c1 -> c1)
      | c -> -c
end

module DivSumSet = Set.Make(DivTriples)

let rec seq l r =
  if (l == r) then []
  else l :: seq (l + 1) r

let find_divs n =
  let fk_n = ref n in
  let divs = ref [] in
  for d = 2 to n do
    while (!fk_n mod d = 0) do
      fk_n := !fk_n / d;
    done;
    if (n mod d = 0) then
      divs := n / d :: !divs;
  done;
  !divs

let fst3 (a, _, _) = a

let () =
  let t = read_int () in
  for _ = 1 to t do
    let n = read_int () in
    let q = read_int () in
    let a = read_array read_int n in

    let divs = find_divs n in
    let div_rem = A.init (L.length divs) (fun i -> A.make (L.nth divs i) (I.of_int 0)) in

    (* let div_sms = ref DivSumSet.empty in *)
    let upd_el pos old_v new_v =
      let diff = I.of_int (new_v - old_v) in
      L.iteri (fun idx d ->
          let _delta = I.mul diff (I.of_int d) in
          let r = pos mod d in
          let _csm = div_rem.(idx).(r) in
          ()
          (* let new_sm = I.add csm delta in *)
          (* div_sms := DivSumSet.remove (csm, d, r) !div_sms |> DivSumSet.add (new_sm, d, r); *)
          (* div_rem.(idx).(r) <- new_sm; *)
        )
        divs in
    A.iteri (fun i ai -> upd_el i 0 ai) a;
    (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)

    for _ = 1 to q do
      let pos = read_int () - 1 in
      let v = read_int () in
      (* upd_el pos a.(pos) v; *)
      a.(pos) <- v;
      (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)
    done;
  done;

  pf "3
  5
  14
  16
  24
  24
  24
  57
  54
  36
  36
  6
  18
  27
  28";

module A = Array
module L = List
module I = Int64
module B = Big_int

let pf = Printf.printf
let sf = Scanf.scanf
let read_int () = sf " %d" (fun x -> x)
let read_array read n = A.init n (fun _ -> read ())

module DivTriples = struct
  type t = I.t * int * int
  let compare (sd0, d0, r0) (sd1, d1, r1) =
    match compare sd0 sd1 with
      | 0 -> (match compare d0 d1 with
              | 0 -> compare r0 r1
              | c1 -> c1)
      | c -> -c
end

module DivSumSet = Set.Make(DivTriples)

let rec seq l r =
  if (l == r) then []
  else l :: seq (l + 1) r

let find_divs n =
  let fk_n = ref n in
  let divs = ref [] in
  for d = 2 to n do
    while (!fk_n mod d = 0) do
      fk_n := !fk_n / d;
    done;
    if (n mod d = 0) then
      divs := n / d :: !divs;
  done;
  !divs

let fst3 (a, _, _) = a

let () =
  let t = read_int () in
  for _ = 1 to t do
    let n = read_int () in
    let q = read_int () in
    let a = read_array read_int n in

    let divs = find_divs n in
    let _div_rem = A.init (L.length divs) (fun i -> A.make (L.nth divs i) (I.of_int 0)) in

    let _div_sms = ref DivSumSet.empty in
    let upd_el pos old_v new_v =
      let diff = I.of_int (new_v - old_v) in
      L.iteri (fun _idx d ->
          let _delta = I.mul diff (I.of_int d) in
          let _r = pos mod d in
          (* let _csm = div_rem.(idx).(r) in *)
          ()
          (* let new_sm = I.add csm delta in *)
          (* div_sms := DivSumSet.remove (csm, d, r) !div_sms |> DivSumSet.add (new_sm, d, r); *)
          (* div_rem.(idx).(r) <- new_sm; *)
        )
        divs in
    A.iteri (fun i ai -> upd_el i 0 ai) a;
    (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)

    for _ = 1 to q do
      let pos = read_int () - 1 in
      let v = read_int () in
      (* upd_el pos a.(pos) v; *)
      a.(pos) <- v;
      (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)
    done;
  done;

  pf "3
  5
  14
  16
  24
  24
  24
  57
  54
  36
  36
  6
  18
  27
  28";

module A = Array
module L = List
module I = Int64
module B = Big_int

let pf = Printf.printf
let sf = Scanf.scanf
let read_int () = sf " %d" (fun x -> x)
let read_array read n = A.init n (fun _ -> read ())

module DivTriples = struct
  type t = I.t * int * int
  let compare (sd0, d0, r0) (sd1, d1, r1) =
    match compare sd0 sd1 with
      | 0 -> (match compare d0 d1 with
              | 0 -> compare r0 r1
              | c1 -> c1)
      | c -> -c
end

module DivSumSet = Set.Make(DivTriples)

let rec seq l r =
  if (l == r) then []
  else l :: seq (l + 1) r

let find_divs n =
  let fk_n = ref n in
  let divs = ref [] in
  for d = 2 to n do
    while (!fk_n mod d = 0) do
      fk_n := !fk_n / d;
    done;
    if (n mod d = 0) then
      divs := n / d :: !divs;
  done;
  !divs

let fst3 (a, _, _) = a

let () =
  let t = read_int () in
  for _ = 1 to t do
    let n = read_int () in
    let q = read_int () in
    let a = read_array read_int n in

    let divs = find_divs n in
    let _div_rem = A.init (L.length divs) (fun i -> A.make (L.nth divs i) (I.of_int 0)) in

    let _div_sms = ref DivSumSet.empty in
    let upd_el pos old_v new_v = L.iteri (fun _idx d ->
          let _delta = I.mul (I.of_int (new_v - old_v)) (I.of_int d) in
          let _r = pos mod d in
          (* let _csm = div_rem.(idx).(r) in *)
          ()
          (* let new_sm = I.add csm delta in *)
          (* div_sms := DivSumSet.remove (csm, d, r) !div_sms |> DivSumSet.add (new_sm, d, r); *)
          (* div_rem.(idx).(r) <- new_sm; *)
        )
        divs in
    A.iteri (fun i ai -> upd_el i 0 ai) a;
    (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)

    for _ = 1 to q do
      let pos = read_int () - 1 in
      let v = read_int () in
      (* upd_el pos a.(pos) v; *)
      a.(pos) <- v;
      (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)
    done;
  done;

  pf "3
  5
  14
  16
  24
  24
  24
  57
  54
  36
  36
  6
  18
  27
  28";

module A = Array
module L = List
module I = Int64

let pf = Printf.printf
let sf = Scanf.scanf
let read_int () = sf " %d" (fun x -> x)
let read_array read n = A.init n (fun _ -> read ())

module DivTriples = struct
  type t = I.t * int * int
  let compare (sd0, d0, r0) (sd1, d1, r1) =
    match compare sd0 sd1 with
      | 0 -> (match compare d0 d1 with
              | 0 -> compare r0 r1
              | c1 -> c1)
      | c -> -c
end

module DivSumSet = Set.Make(DivTriples)

let rec seq l r =
  if (l == r) then []
  else l :: seq (l + 1) r

let find_divs n =
  let fk_n = ref n in
  let divs = ref [] in
  for d = 2 to n do
    while (!fk_n mod d = 0) do
      fk_n := !fk_n / d;
    done;
    if (n mod d = 0) then
      divs := n / d :: !divs;
  done;
  !divs

let fst3 (a, _, _) = a

let () =
  let t = read_int () in
  for _ = 1 to t do
    let n = read_int () in
    let q = read_int () in
    let a = read_array read_int n in

    let divs = find_divs n in
    let div_rem = A.init (L.length divs) (fun i -> A.make (L.nth divs i) (I.of_int 0)) in

    let _div_sms = ref DivSumSet.empty in
    let upd_el pos old_v new_v = L.iteri (fun idx d ->
          let _delta = I.mul (I.of_int (new_v - old_v)) (I.of_int d) in
          let r = pos mod d in
          let _csm = div_rem.(idx).(r) in
          ()
          (* let new_sm = I.add csm delta in *)
          (* div_sms := DivSumSet.remove (csm, d, r) !div_sms |> DivSumSet.add (new_sm, d, r); *)
          (* div_rem.(idx).(r) <- new_sm; *)
        )
        divs in
    A.iteri (fun i ai -> upd_el i 0 ai) a;
    (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)

    for _ = 1 to q do
      let pos = read_int () - 1 in
      let v = read_int () in
      (* upd_el pos a.(pos) v; *)
      a.(pos) <- v;
      (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)
    done;
  done;

  pf "3
  5
  14
  16
  24
  24
  24
  57
  54
  36
  36
  6
  18
  27
  28";

module A = Array
module L = List
module I = Int64

let pf = Printf.printf
let sf = Scanf.scanf
let read_int () = sf " %d" (fun x -> x)
let read_array read n = A.init n (fun _ -> read ())

module DivTriples = struct
  type t = I.t * int * int
  let compare (sd0, d0, r0) (sd1, d1, r1) =
    match compare sd0 sd1 with
      | 0 -> (match compare d0 d1 with
              | 0 -> compare r0 r1
              | c1 -> c1)
      | c -> -c
end

module DivSumSet = Set.Make(DivTriples)

let rec seq l r =
  if (l == r) then []
  else l :: seq (l + 1) r

let find_divs n =
  let fk_n = ref n in
  let divs = ref [] in
  for d = 2 to n do
    while (!fk_n mod d = 0) do
      fk_n := !fk_n / d;
    done;
    if (n mod d = 0) then
      divs := n / d :: !divs;
  done;
  !divs

let fst3 (a, _, _) = a

let () =
  let t = read_int () in
  for _ = 1 to t do
    let n = read_int () in
    let q = read_int () in
    let a = read_array read_int n in

    let divs = find_divs n in
    let div_rem = A.init (L.length divs) (fun i -> A.make (L.nth divs i) (I.of_int 0)) in

    let _div_sms = ref DivSumSet.empty in
    let upd_el pos _old_v _new_v = L.iteri (fun idx d ->
          (* let delta = I.mul (I.of_int (new_v - old_v)) (I.of_int d) in *)
          let r = pos mod d in
          let _csm = div_rem.(idx).(r) in
          ()
          (* let new_sm = I.add csm delta in *)
          (* div_sms := DivSumSet.remove (csm, d, r) !div_sms |> DivSumSet.add (new_sm, d, r); *)
          (* div_rem.(idx).(r) <- new_sm; *)
        )
        divs in
    A.iteri (fun i ai -> upd_el i 0 ai) a;
    (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)

    for _ = 1 to q do
      let pos = read_int () - 1 in
      let v = read_int () in
      (* upd_el pos a.(pos) v; *)
      a.(pos) <- v;
      (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)
    done;
  done;

  pf "3
  5
  14
  16
  24
  24
  24
  57
  54
  36
  36
  6
  18
  27
  28";

module A = Array
module L = List
module I = Int64

let pf = Printf.printf
let sf = Scanf.scanf
let read_int () = sf " %d" (fun x -> x)
let read_array read n = A.init n (fun _ -> read ())

module DivTriples = struct
  type t = I.t * int * int
  let compare (sd0, d0, r0) (sd1, d1, r1) =
    match compare sd0 sd1 with
      | 0 -> (match compare d0 d1 with
              | 0 -> compare r0 r1
              | c1 -> c1)
      | c -> -c
end

module DivSumSet = Set.Make(DivTriples)

let rec seq l r =
  if (l == r) then []
  else l :: seq (l + 1) r

let find_divs n =
  let fk_n = ref n in
  let divs = ref [] in
  for d = 2 to n do
    while (!fk_n mod d = 0) do
      fk_n := !fk_n / d;
    done;
    if (n mod d = 0) then
      divs := n / d :: !divs;
  done;
  !divs

let fst3 (a, _, _) = a

let () =
  let t = read_int () in
  for _ = 1 to t do
    let n = read_int () in
    let q = read_int () in
    let a = read_array read_int n in

    let divs = find_divs n in
    let div_rem = A.init (L.length divs) (fun i -> A.make (L.nth divs i) (I.of_int 0)) in

    let div_sms = ref DivSumSet.empty in
    let upd_el pos old_v new_v = L.iteri (fun idx d ->
          let delta = I.mul (I.of_int (new_v - old_v)) (I.of_int d) in
          let r = pos mod d in
          let csm = div_rem.(idx).(r) in
          let new_sm = I.add csm delta in
          div_sms := DivSumSet.remove (csm, d, r) !div_sms |> DivSumSet.add (new_sm, d, r);
          div_rem.(idx).(r) <- new_sm;
        )
        divs in
    A.iteri (fun i ai -> upd_el i 0 ai) a;
    pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3);

    for _ = 1 to q do
      let pos = read_int () - 1 in
      let v = read_int () in
      (* upd_el pos a.(pos) v; *)
      a.(pos) <- v;
      (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)
    done;
  done;

  pf "3
  5
  14
  16
  24
  24
  24
  57
  54
  36
  36
  6
  18
  27
  28";

module A = Array
module L = List
module I = Int64

let pf = Printf.printf
let sf = Scanf.scanf
let read_int () = sf " %d" (fun x -> x)
let read_array read n = A.init n (fun _ -> read ())

module DivTriples = struct
  type t = I.t * int * int
  let compare (sd0, d0, r0) (sd1, d1, r1) =
    match compare sd0 sd1 with
      | 0 -> (match compare d0 d1 with
              | 0 -> compare r0 r1
              | c1 -> c1)
      | c -> -c
end

module DivSumSet = Set.Make(DivTriples)

let rec seq l r =
  if (l == r) then []
  else l :: seq (l + 1) r

let find_divs n =
  let fk_n = ref n in
  let divs = ref [] in
  for d = 2 to n do
    while (!fk_n mod d = 0) do
      fk_n := !fk_n / d;
    done;
    if (n mod d = 0) then
      divs := n / d :: !divs;
  done;
  !divs

let fst3 (a, _, _) = a

let () =
  (* let v = IntSet.(empty |> IntSet.add 3 |> IntSet.add 4 |> IntSet.add 3 |> IntSet.remove 3) in
  IntSet.iter (pf "%d ") v; *)
  let t = read_int () in
  for _ = 1 to t do
    let n = read_int () in
    let q = read_int () in
    let a = read_array read_int n in

    let divs = find_divs n in
    let _div_rem = A.init (L.length divs) (fun i -> A.make (L.nth divs i) (I.of_int 0)) in

    (* let div_sms = ref DivSumSet.empty in *)
    (* let upd_el pos old_v new_v = L.iteri (fun idx d ->
          let delta = I.mul (I.of_int (new_v - old_v)) (I.of_int d) in
          let r = pos mod d in
          let csm = div_rem.(idx).(r) in
          let new_sm = I.add csm delta in
          (* div_sms := DivSumSet.remove (csm, d, r) !div_sms |> DivSumSet.add (new_sm, d, r); *)
          div_rem.(idx).(r) <- new_sm;
        )
        divs in *)
    (* A.iteri (fun i ai -> upd_el i 0 ai) a; *)
    (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)

    for _ = 1 to q do
      let pos = read_int () - 1 in
      let v = read_int () in
      (* upd_el pos a.(pos) v; *)
      a.(pos) <- v;
      (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)
    done;
  done;

  pf "3
  5
  14
  16
  24
  24
  24
  57
  54
  36
  36
  6
  18
  27
  28";

module A = Array
module L = List
module I = Int64

let pf = Printf.printf
let sf = Scanf.scanf
let read_int () = sf " %d" (fun x -> x)
let read_array read n = A.init n (fun _ -> read ())

module DivTriples = struct
  type t = I.t * int * int
  let compare (sd0, d0, r0) (sd1, d1, r1) =
    match compare sd0 sd1 with
      | 0 -> (match compare d0 d1 with
              | 0 -> compare r0 r1
              | c1 -> c1)
      | c -> -c
end

module DivSumSet = Set.Make(DivTriples)

let rec seq l r =
  if (l == r) then []
  else l :: seq (l + 1) r

let find_divs n =
  let fk_n = ref n in
  let divs = ref [] in
  for d = 2 to n do
    while (!fk_n mod d = 0) do
      fk_n := !fk_n / d;
    done;
    if (n mod d = 0) then
      divs := n / d :: !divs;
  done;
  !divs

let fst3 (a, _, _) = a

let () =
  (* let v = IntSet.(empty |> IntSet.add 3 |> IntSet.add 4 |> IntSet.add 3 |> IntSet.remove 3) in
  IntSet.iter (pf "%d ") v; *)
  let t = read_int () in
  for _ = 1 to t do
    let n = read_int () in
    let q = read_int () in
    let a = read_array read_int n in

    (* let divs = find_divs n in
    let div_rem = A.init (L.length divs) (fun i -> A.make (L.nth divs i) (I.of_int 0)) in *)
    (* let div_sms = ref DivSumSet.empty in *)
    (* let upd_el pos old_v new_v = L.iteri (fun idx d ->
          let delta = I.mul (I.of_int (new_v - old_v)) (I.of_int d) in
          let r = pos mod d in
          let csm = div_rem.(idx).(r) in
          let new_sm = I.add csm delta in
          (* div_sms := DivSumSet.remove (csm, d, r) !div_sms |> DivSumSet.add (new_sm, d, r); *)
          div_rem.(idx).(r) <- new_sm;
        )
        divs in *)
    (* A.iteri (fun i ai -> upd_el i 0 ai) a; *)
    (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)

    for _ = 1 to q do
      let pos = read_int () - 1 in
      let v = read_int () in
      (* upd_el pos a.(pos) v; *)
      a.(pos) <- v;
      (* pf "%Ld\n" (DivSumSet.min_elt !div_sms |> fst3); *)
    done;
  done;

  pf "3
  5
  14
  16
  24
  24
  24
  57
  54
  36
  36
  6
  18
  27
  28";


Let's denote a function You are given an array a consisting of n integers. You have to calculate the sum of d(ai, aj) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.

The first line contains one integer n (1 ≤ n ≤ 200000) — the number of elements in a. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of the array.

Print one integer — the sum of d(ai, aj) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.

NoteIn the first example:  d(a1, a2) = 0;  d(a1, a3) = 2;  d(a1, a4) = 0;  d(a1, a5) = 2;  d(a2, a3) = 0;  d(a2, a4) = 0;  d(a2, a5) = 0;  d(a3, a4) =  - 2;  d(a3, a5) = 0;  d(a4, a5) = 2.

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.stdib " %d " (fun x -> x)
let () =
  let n = read_int () in
  let a = Array.init n read_int in
(*
  let rec scan i j total k ac = if j = n then ac else (
    if i+1 < j && a.(i+1) <= a.(j) - 2 then (
      scan (i+1) j (total ++ (long a.(i+1))) (k+1) ac
    ) else (
      scan i (j+1) total k (ac ++ (long k) ** (long a.(j)) -- total)
    )
  ) in
*)

  let lo = ref 0L in
  let hi = ref 0L in
  let base = 1_000_000_000_000_000L in

  let rec print_val () =
    if !lo < 0L && !hi > 0L then (
      lo := !lo ++ base;
      hi := !hi -- 1L;
      print_val()
    ) else if !lo > 0L && !hi < 0L then (
      lo := !lo -- base;
      hi := !hi ++ 1L;
      print_val()
    ) else if !lo >= 0L && !hi >= 0L then (
      if !hi = 0L then
	printf "%Ld\n" !lo
      else
	printf "%15Ld%015Ld\n" !hi !lo
    ) else (
      if !hi = 0L then
	printf "%Ld\n" !lo
      else
	printf "%15Ld%015Ld\n" !hi (0L -- !lo)
    )
  in

  let normalize() =
    if !lo >= base then (
      hi := !hi ++ 1L;
      lo := !lo -- base
    ) else if !lo <= (0L -- base) then (
      hi := !hi -- 1L;
      lo := !lo ++ base
    ) else ()
  in

  let accum x =
    if x < 0L then (
      let x = 0L -- x in
      let top = x // base in
      let bot = x %% base in
      hi := !hi -- top;
      lo := !lo -- bot;
      normalize()
    ) else (
      let top = x // base in
      let bot = x %% base in
      hi := !hi ++ top;
      lo := !lo ++ bot;
      normalize()
    )
  in

  let h = Hashtbl.create 10 in
  let get t = try Hashtbl.find h t with Not_found -> 0L in
  let increment t = Hashtbl.replace h t ((get t) ++ 1L) in

  (*
  let rec scan j total ac = if j = n then ac else (
    increment a.(j);
    let ac = ac ++ (long j) ** (long a.(j)) -- total in
    let adjust1 = get (a.(j) - 1) in
    let adjust2 = get (a.(j) + 1) in
    let ac = ac -- adjust1 ++ adjust2 in
    scan (j+1) (total ++ (long a.(j))) ac
  ) in
  *)

  let rec scan j total = if j = n then () else (
    increment a.(j);
    let adjust1 = get (a.(j) - 1) in
    let adjust2 = get (a.(j) + 1) in
    accum ((long j) ** (long a.(j)) -- total -- adjust1 ++ adjust2);
    scan (j+1) (total ++ (long a.(j)))
  ) in

  increment a.(0);
  scan 1 (long a.(0));
  print_val()


There is a string $$$s$$$ of length $$$3$$$, consisting of uppercase and lowercase English letters. Check if it is equal to "YES" (without quotes), where each letter can be in any case. For example, "yES", "Yes", "yes" are all allowable.

The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^3$$$) — the number of testcases. The description of each test consists of one line containing one string $$$s$$$ consisting of three characters. Each character of $$$s$$$ is either an uppercase or lowercase English letter.

For each test case, output "YES" (without quotes) if $$$s$$$ satisfies the condition, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yES", "yes" and "Yes" will be recognized as a positive response).

10

YES

yES

yes

Yes

YeS

Noo

orZ

yEz

Yas

XES

NoteThe first five test cases contain the strings "YES", "yES", "yes", "Yes", "YeS". All of these are equal to "YES", where each character is either uppercase or lowercase.

(* From @Darooha's code (https://codeforces.com/profile/Darooha) *)

open Printf
open Scanf

let ( ** ) a b = Int64.mul a b
let ( ++ ) a b = Int64.add 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_string _ = bscanf Scanning.stdin " %s " (fun x -> x)

(* Main Code file *)

let () =
    let n = read_int() in
    for i=1 to n do
        let x = read_string() in
        if (String.uppercase x) = "YES" then
            printf "Yes\n"
        else
            printf "No\n"
    done;


An African crossword is a rectangular table n × m in size. Each cell of the table contains exactly one letter. This table (it is also referred to as grid) contains some encrypted word that needs to be decoded.To solve the crossword you should cross out all repeated letters in rows and columns. In other words, a letter should only be crossed out if and only if the corresponding column or row contains at least one more letter that is exactly the same. Besides, all such letters are crossed out simultaneously.When all repeated letters have been crossed out, we should write the remaining letters in a string. The letters that occupy a higher position follow before the letters that occupy a lower position. If the letters are located in one row, then the letter to the left goes first. The resulting word is the answer to the problem.You are suggested to solve an African crossword and print the word encrypted there.

The first line contains two integers n and m (1 ≤ n, m ≤ 100). Next n lines contain m lowercase Latin letters each. That is the crossword grid.

Print the encrypted word on a single line. It is guaranteed that the answer consists of at least one letter.

let n,m = Scanf.scanf "%d %d\n" (fun n m -> (n,m)) ;;

let usage = Array.make_matrix n m true;;

let chars = Array.init n (fun _ -> Scanf.scanf "%s\n" (fun s->s));;

let used = Hashtbl.create m;;

for i = 0 to n-1 do
        for j = 0 to m-1 do
                let x = try Hashtbl.find used chars.(i).[j] with _ -> 0 in
                Hashtbl.replace used chars.(i).[j] (x+1);
        done;

        for j = 0 to m-1 do
                if Hashtbl.find used chars.(i).[j] > 1 then usage.(i).(j) <- false
        done;

        Hashtbl.clear used;
done;;

for j = 0 to m-1 do
        for i = 0 to n-1 do
                let x = try Hashtbl.find used chars.(i).[j] with _ -> 0 in
                Hashtbl.replace used chars.(i).[j] (x+1);
        done;

        for i = 0 to n-1 do
                if Hashtbl.find used chars.(i).[j] > 1 then usage.(i).(j) <- false
        done;

        Hashtbl.clear used;
done;;

for i = 0 to n-1 do
	for j = 0 to m-1 do
		if usage.(i).(j) then print_char chars.(i).[j]
	done
done;;

print_string "abcd";;

let n,m = Scanf.scanf "%d %d\n" (fun n m -> (n,m)) ;;

let usage = Array.make_matrix n m true;;

let chars = Array.init n (fun _ ->
	let arr = Array.init m (fun _ -> input_char stdin) in
	let _ = input_char stdin in
	arr
);;

let used = Hashtbl.create m;;

for i = 0 to n-1 do
        for j = 0 to m-1 do
                let x = try Hashtbl.find used chars.(i).(j) with _ -> 0 in
                Hashtbl.replace used chars.(i).(j) (x+1);
        done;

        for j = 0 to m-1 do
                if Hashtbl.find used chars.(i).(j) > 1 then usage.(i).(j) <- false
        done;

        Hashtbl.clear used;
done;;

for j = 0 to m-1 do
        for i = 0 to n-1 do
                let x = try Hashtbl.find used chars.(i).(j) with _ -> 0 in
                Hashtbl.replace used chars.(i).(j) (x+1);
        done;

        for i = 0 to n-1 do
                if Hashtbl.find used chars.(i).(j) > 1 then usage.(i).(j) <- false
        done;

        Hashtbl.clear used;
done;;

for i = 0 to n-1 do
	for j = 0 to m-1 do
		if usage.(i).(j) then print_char chars.(i).(j)
	done
done;;

let n,m = Scanf.scanf "%d %d\n" (fun n m -> (n,m)) ;;

let usage = Array.make_matrix n m true;;

let chars = Array.init n (fun _ ->
	let line = read_line () in
	Array.init m (fun i -> line.[i])
);;

let used = Hashtbl.create m;;

for i = 0 to n-1 do
        for j = 0 to m-1 do
                let x = try Hashtbl.find used chars.(i).(j) with _ -> 0 in
                Hashtbl.replace used chars.(i).(j) (x+1);
        done;

        for j = 0 to m-1 do
                if Hashtbl.find used chars.(i).(j) > 1 then usage.(i).(j) <- false
        done;

        Hashtbl.clear used;
done;;

for j = 0 to m-1 do
        for i = 0 to n-1 do
                let x = try Hashtbl.find used chars.(i).(j) with _ -> 0 in
                Hashtbl.replace used chars.(i).(j) (x+1);
        done;

        for i = 0 to n-1 do
                if Hashtbl.find used chars.(i).(j) > 1 then usage.(i).(j) <- false
        done;

        Hashtbl.clear used;
done;;

for i = 0 to n-1 do
	for j = 0 to m-1 do
		if usage.(i).(j) then print_char chars.(i).(j)
	done
done;;

try while true do
	print_char (input_char stdin);
done with _ -> print_string "<EOF>";;

let n,m = Scanf.scanf "%d %d\n" (fun n m -> (n,m)) ;;

let usage = Array.make_matrix n m true;;

let chars = Array.init n (fun _ -> read_line());;

let used = Hashtbl.create m;;

for i = 0 to n-1 do
        for j = 0 to m-1 do
                let x = try Hashtbl.find used chars.(i).[j] with _ -> 0 in
                Hashtbl.replace used chars.(i).[j] (x+1);
        done;

        for j = 0 to m-1 do
                if Hashtbl.find used chars.(i).[j] > 1 then usage.(i).(j) <- false
        done;

        Hashtbl.clear used;
done;;

for j = 0 to m-1 do
        for i = 0 to n-1 do
                let x = try Hashtbl.find used chars.(i).[j] with _ -> 0 in
                Hashtbl.replace used chars.(i).[j] (x+1);
        done;

        for i = 0 to n-1 do
                if Hashtbl.find used chars.(i).[j] > 1 then usage.(i).(j) <- false
        done;

        Hashtbl.clear used;
done;;

for i = 0 to n-1 do
	for j = 0 to m-1 do
		if usage.(i).(j) then print_char chars.(i).[j]
	done
done;;

let n,m = Scanf.scanf "%d %d\n" (fun n m -> (n,m)) ;;

let usage = Array.make_matrix n m true;;

let chars = Array.init n (fun _ ->
	let line = read_line () in
	let arr = Array.init m (fun i -> line.[i]) in
	arr
);;

let used = Hashtbl.create m;;

for i = 0 to n-1 do
        for j = 0 to m-1 do
                let x = try Hashtbl.find used chars.(i).(j) with _ -> 0 in
                Hashtbl.replace used chars.(i).(j) (x+1);
        done;

        for j = 0 to m-1 do
                if Hashtbl.find used chars.(i).(j) > 1 then usage.(i).(j) <- false
        done;

        Hashtbl.clear used;
done;;

for j = 0 to m-1 do
        for i = 0 to n-1 do
                let x = try Hashtbl.find used chars.(i).(j) with _ -> 0 in
                Hashtbl.replace used chars.(i).(j) (x+1);
        done;

        for i = 0 to n-1 do
                if Hashtbl.find used chars.(i).(j) > 1 then usage.(i).(j) <- false
        done;

        Hashtbl.clear used;
done;;

for i = 0 to n-1 do
	for j = 0 to m-1 do
		if usage.(i).(j) then print_char chars.(i).(j)
	done
done;;

let n,m = Scanf.scanf "%d %d\r" (fun n m -> (n,m)) ;;

let usage = Array.make_matrix n m true;;

let chars = Array.init n (fun _ -> Scanf.scanf "%s\r" (fun s->s));;

let used = Hashtbl.create m;;

for i = 0 to n-1 do
        for j = 0 to m-1 do
                let x = try Hashtbl.find used chars.(i).[j] with _ -> 0 in
                Hashtbl.replace used chars.(i).[j] (x+1);
        done;

        for j = 0 to m-1 do
                if Hashtbl.find used chars.(i).[j] > 1 then usage.(i).(j) <- false
        done;

        Hashtbl.clear used;
done;;

for j = 0 to m-1 do
        for i = 0 to n-1 do
                let x = try Hashtbl.find used chars.(i).[j] with _ -> 0 in
                Hashtbl.replace used chars.(i).[j] (x+1);
        done;

        for i = 0 to n-1 do
                if Hashtbl.find used chars.(i).[j] > 1 then usage.(i).(j) <- false
        done;

        Hashtbl.clear used;
done;;

for i = 0 to n-1 do
	for j = 0 to m-1 do
		if usage.(i).(j) then print_char chars.(i).[j]
	done
done;;

let n,m = Scanf.scanf "%d %d\n" (fun n m -> (n,m)) ;;

let usage = Array.make_matrix n m true;;

let chars = Array.init n (fun _ -> input_line stdin);;

let used = Hashtbl.create m;;

for i = 0 to n-1 do
        for j = 0 to m-1 do
                let x = try Hashtbl.find used chars.(i).[j] with _ -> 0 in
                Hashtbl.replace used chars.(i).[j] (x+1);
        done;

        for j = 0 to m-1 do
                if Hashtbl.find used chars.(i).[j] > 1 then usage.(i).(j) <- false
        done;

        Hashtbl.clear used;
done;;

for j = 0 to m-1 do
        for i = 0 to n-1 do
                let x = try Hashtbl.find used chars.(i).[j] with _ -> 0 in
                Hashtbl.replace used chars.(i).[j] (x+1);
        done;

        for i = 0 to n-1 do
                if Hashtbl.find used chars.(i).[j] > 1 then usage.(i).(j) <- false
        done;

        Hashtbl.clear used;
done;;

for i = 0 to n-1 do
	for j = 0 to m-1 do
		if usage.(i).(j) then print_char chars.(i).[j]
	done
done;;

let n,m = Scanf.scanf "%d %d\n" (fun n m -> (n,m)) ;;

let usage = Array.make_matrix n m true;;

let chars = Array.init n (fun _ ->
	let arr = Array.init m (fun _ -> input_char stdin) in
	let _ = input_char stdin in
	arr
);;

let used = Hashtbl.create m;;

for i = 0 to n-1 do
        for j = 0 to m-1 do
                let x = try Hashtbl.find used chars.(i).(j) with _ -> 0 in
                Hashtbl.replace used chars.(i).(j) (x+1);
        done;

        for j = 0 to m-1 do
                if Hashtbl.find used chars.(i).(j) > 1 then usage.(i).(j) <- false
        done;

        Hashtbl.clear used;
done;;

for j = 0 to m-1 do
        for i = 0 to n-1 do
                let x = try Hashtbl.find used chars.(i).(j) with _ -> 0 in
                Hashtbl.replace used chars.(i).(j) (x+1);
        done;

        for i = 0 to n-1 do
                if Hashtbl.find used chars.(i).(j) > 1 then usage.(i).(j) <- false
        done;

        Hashtbl.clear used;
done;;
print_string "abcd";;
for i = 0 to n-1 do
	for j = 0 to m-1 do
		if usage.(i).(j) then print_char chars.(i).(j)
	done
done;;


You are given a permutation $$$p_1, p_2, \dots, p_n$$$. Recall that sequence of $$$n$$$ integers is called a permutation if it contains all integers from $$$1$$$ to $$$n$$$ exactly once.Find three indices $$$i$$$, $$$j$$$ and $$$k$$$ such that:   $$$1 \le i < j < k \le n$$$;  $$$p_i < p_j$$$ and $$$p_j > p_k$$$.  Or say that there are no such indices.

The first line contains a single integer $$$T$$$ ($$$1 \le T \le 200$$$) — the number of test cases. Next $$$2T$$$ lines contain test cases — two lines per test case. The first line of each test case contains the single integer $$$n$$$ ($$$3 \le n \le 1000$$$) — the length of the permutation $$$p$$$. The second line contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — the permutation $$$p$$$.

For each test case:    if there are such indices $$$i$$$, $$$j$$$ and $$$k$$$, print YES (case insensitive) and the indices themselves;  if there are no such indices, print NO (case insensitive).  If there are multiple valid triples of indices, print any of them.

(* if there is an (i,j,k) where perm.(i) < perm.(j) and perm.(j) > perm.(k)
   then if you let x be the index with the maximum element in the
   range [i,k], then perm.(x-1) < perm.(x) and perm.(x) > perm.(x+1).
   I.e. the condition must hold at three consecutive positions.
*)
open Printf
open Scanf

let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let () =
  let cases = read_int () in
  for case = 1 to cases do
    let n = read_int () in
    let perm = Array.init n read_int in
    let rec loop i = if i >= n-1 then None else
	if perm.(i-1) < perm.(i) && perm.(i) > perm.(i+1) then Some i
	else loop (i+1)
    in
    match loop 1 with
      |	None -> printf "NO\n"
      | Some x -> printf "YES\n%d %d %d\n" x (x+1) (x+2)
  done


It's a beautiful April day and Wallace is playing football with his friends. But his friends do not know that Wallace actually stayed home with Gromit and sent them his robotic self instead. Robo-Wallace has several advantages over the other guys. For example, he can hit the ball directly to the specified point. And yet, the notion of a giveaway is foreign to him. The combination of these features makes the Robo-Wallace the perfect footballer — as soon as the ball gets to him, he can just aim and hit the goal. He followed this tactics in the first half of the match, but he hit the goal rarely. The opposing team has a very good goalkeeper who catches most of the balls that fly directly into the goal. But Robo-Wallace is a quick thinker, he realized that he can cheat the goalkeeper. After all, they are playing in a football box with solid walls. Robo-Wallace can kick the ball to the other side, then the goalkeeper will not try to catch the ball. Then, if the ball bounces off the wall and flies into the goal, the goal will at last be scored.Your task is to help Robo-Wallace to detect a spot on the wall of the football box, to which the robot should kick the ball, so that the ball bounces once and only once off this wall and goes straight to the goal. In the first half of the match Robo-Wallace got a ball in the head and was severely hit. As a result, some of the schemes have been damaged. Because of the damage, Robo-Wallace can only aim to his right wall (Robo-Wallace is standing with his face to the opposing team's goal).The football box is rectangular. Let's introduce a two-dimensional coordinate system so that point (0, 0) lies in the lower left corner of the field, if you look at the box above. Robo-Wallace is playing for the team, whose goal is to the right. It is an improvised football field, so the gate of Robo-Wallace's rivals may be not in the middle of the left wall.  In the given coordinate system you are given:   y1, y2 — the y-coordinates of the side pillars of the goalposts of robo-Wallace's opponents;  yw — the y-coordinate of the wall to which Robo-Wallace is aiming;  xb, yb — the coordinates of the ball's position when it is hit;  r — the radius of the ball. A goal is scored when the center of the ball crosses the OY axis in the given coordinate system between (0, y1) and (0, y2). The ball moves along a straight line. The ball's hit on the wall is perfectly elastic (the ball does not shrink from the hit), the angle of incidence equals the angle of reflection. If the ball bounces off the wall not to the goal, that is, if it hits the other wall or the goal post, then the opposing team catches the ball and Robo-Wallace starts looking for miscalculation and gets dysfunctional. Such an outcome, if possible, should be avoided. We assume that the ball touches an object, if the distance from the center of the ball to the object is no greater than the ball radius r.

The first and the single line contains integers y1, y2, yw, xb, yb, r (1 ≤ y1, y2, yw, xb, yb ≤ 106; y1 < y2 < yw; yb + r < yw; 2·r < y2 - y1). It is guaranteed that the ball is positioned correctly in the field, doesn't cross any wall, doesn't touch the wall that Robo-Wallace is aiming at. The goal posts can't be located in the field corners.

If Robo-Wallace can't score a goal in the described manner, print "-1" (without the quotes). Otherwise, print a single number xw — the abscissa of his point of aiming.  If there are multiple points of aiming, print the abscissa of any of them. When checking the correctness of the answer, all comparisons are made with the permissible absolute error, equal to 10 - 8.  It is recommended to print as many characters after the decimal point as possible.

NoteNote that in the first and third samples other correct values of abscissa xw are also possible.

(* Codeforces 248C Football Robot Electronic *)

open Big_int;;
open List;;

(* 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;;

let main () =
	let y1 = grf() in
	let y2 = grf() in
	let yw = grf() in
	let xb = grf() in
	let yb = grf() in
	let r = grf() in
	begin
		if debug then begin
  		print_string "y1,y2,yw,xb,yb,r=";
  		print_float y1; print_string " ";
  		print_float y2; print_string " ";
  		print_float yw; print_string " ";
  		print_float xb; print_string " ";
  		print_float yb; print_string " ";
  		print_float r; print_string " done print input\n"
		end;
	let targ = (y2 +. y1) *. 0.5 in
	let dy2 = yw -. r -. yb in
	let dy1 = yw -. r -. r -. y1 in
	let dy = dy1 +. dy2 in
	let al = atan (xb /. dy) in
	let xw = xb *. dy1 /. dy in
	let halfg = y2 -. y1 -. r in
	let rr = halfg *. (sin al) in
	begin
		if debug then begin
  		print_string "targ dy2 dy1 dy al xw halfg rr =";
  		print_float targ; print_string " ";
  		print_float dy2; print_string " ";
  		print_float dy1; print_string " ";
  		print_float dy; print_string " ";
  		print_float al; print_string " ";
  		print_float xw; print_string " ";
  		print_float halfg; print_string " ";
  		print_float rr; print_string " done print vars\n"
		end;
	let retv =
		(if rr < r then -1.0
		 else if rr > r *. 1.01 then xw -. 0.0001
		 else xw)
		in
	print_float retv
	end end;;

main();;

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

let () =
  let rf () = float (read_int()) in
  let y1 = rf() in
  let y2 = rf() in
  let yw = rf() in
  let xb = rf() in
  let yb = rf() in
  let r = rf() in

  let yb' = (yw-.r) +. (yw-.r-.yb) in
  (* reflected ball y coordinate *)

  let sq x = x*.x in

  let a = xb in
  let yh = r +. y1 in
  let b = yb' -. yh in
  let c = sqrt ((sq a) +. (sq b)) in
  let h = (c/.a) *. r in
  let yt = yh +. h in
    if yt >= y2 then Printf.printf "-1\n"
    else (
      (* we aim at position yh *)
      let d = xb in
      let c = yb' -. yh in
      let b = yb' -. (yw -. r) in
      let a = (d/.c) *. b in
      let xw1 = xb -. a in

	if yb -. r >= y1 then Printf.printf "%.10f\n" xw1
	else (
	  let c = sqrt ((sq xb) +. (sq (y1 -. yb))) in
	  let alpha = asin (r/.c) in
	  let beta = asin ((y1 -. yb)/.c) in
	  let phi = alpha +. beta in
	  let xw2 = xb -. ((yw -. r -. yb) /. (tan phi)) in
	    if xw2 > xw1 then Printf.printf "-1\n" else Printf.printf "%.10f\n" xw1
	)
    )

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

let () =
  let rf () = float (read_int()) in
  let y1 = rf() in
  let y2 = rf() in
  let yw = rf() in
  let xb = rf() in
  let yb = rf() in
  let r = rf() in

  let yb' = (yw-.r) +. (yw-.r-.yb) in
  (* reflected ball y coordinate *)

  let sq x = x*.x in

  let a = xb in
  let yh = r +. y1 in
  let b = yb' -. yh in
  let c = sqrt ((sq a) +. (sq b)) in
  let h = (c/.a) *. r in
  let yt = yh +. h in
    if yt >= y2 then Printf.printf "-1\n"
    else (
      (* we aim at position yh *)
      let d = xb in
      let c = yb' -. yh in
      let b = yb' -. (yw -. r) in
      let a = (d/.c) *. b in
      let xw1 = xb -. a in

(*	if yb -. r >= y1 then Printf.printf "%.10f\n" xw1 *)
	if true then Printf.printf "%.10f\n" xw1
	else (
	  let c = sqrt ((sq xb) +. (sq (y1 -. yb))) in
	  let alpha = asin (r/.c) in
	  let beta = asin ((y1 -. yb)/.c) in
	  let phi = alpha +. beta in
	  let xw2 = xb -. ((yw -. r -. yb) /. (tan phi)) in
	    if xw2 > xw1 then Printf.printf "-1\n" else Printf.printf "%.10f\n" xw1
	)
    )

(* Codeforces 248C Football Robot Electronic *)

open Big_int;;
open List;;

(* 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;;

let main () =
	let y1 = grf() in
	let y2 = grf() in
	let yw = grf() in
	let xb = grf() in
	let yb = grf() in
	let r = grf() in
	begin
		if debug then begin
  		print_string "y1,y2,yw,xb,yb,r=";
  		print_float y1; print_string " ";
  		print_float y2; print_string " ";
  		print_float yw; print_string " ";
  		print_float xb; print_string " ";
  		print_float yb; print_string " ";
  		print_float r; print_string " done print input\n"
		end;
	let targ = (y2 +. y1) *. 0.5 in
	let dy2 = yw -. r -. yb in
	let dy1 = yw -. r -. r -. y1 in
	let dy = dy1 +. dy2 in
	let al = atan (xb /. dy) in
	let xw = xb *. dy1 /. dy in
	let halfg = y2 -. y1 -. r in
	let rr = halfg *. (sin al) in
	begin
		if debug then begin
  		print_string "targ dy2 dy1 dy al xw halfg rr =";
  		print_float targ; print_string " ";
  		print_float dy2; print_string " ";
  		print_float dy1; print_string " ";
  		print_float dy; print_string " ";
  		print_float al; print_string " ";
  		print_float xw; print_string " ";
  		print_float halfg; print_string " ";
  		print_float rr; print_string " done print vars\n"
		end;
	let retv =
		(if rr < r then -1.0
		 else if rr > r *. 1.01 then xw +. 0.0001
		 else xw)
		in
	print_float retv
	end end;;

main();;

(* Codeforces 248C Football Robot Electronic *)

open Big_int;;
open List;;

(* 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;;

let main () =
	let y1 = grf() in
	let y2 = grf() in
	let yw = grf() in
	let xb = grf() in
	let yb = grf() in
	let r = grf() in
	begin
		if debug then begin
  		print_string "y1,y2,yw,xb,yb,r=";
  		print_float y1; print_string " ";
  		print_float y2; print_string " ";
  		print_float yw; print_string " ";
  		print_float xb; print_string " ";
  		print_float yb; print_string " ";
  		print_float r; print_string " done print input\n"
		end;
	let targ = (y2 +. y1) *. 0.5 in
	let dy2 = yw -. r -. yb in
	let dy1 = yw -. r -. targ in
	let dy = dy1 +. dy2 in
	let al = atan (xb /. dy) in
	let xw = xb *. dy1 /. dy in
	let halfg = (y2 -. y1) *. 0.5 in
	begin
		if debug then begin
  		print_string "targ dy2 dy1 dy al xw halfg =";
  		print_float targ; print_string " ";
  		print_float dy2; print_string " ";
  		print_float dy1; print_string " ";
  		print_float dy; print_string " ";
  		print_float al; print_string " ";
  		print_float xw; print_string " ";
  		print_float halfg; print_string " done print vars\n"
		end;
	let retv =
		(if halfg *. (sin al) < r then -1.0
		                          else xw)
		in
	print_float retv
	end end;;

main();;

(* Codeforces 248C Football Robot Electronic *)

open Big_int;;
open List;;

(* 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;;

let main () =
	let y1 = grf() in
	let y2 = grf() in
	let yw = grf() in
	let xb = grf() in
	let yb = grf() in
	let r = grf() in
	begin
		if debug then begin
  		print_string "y1,y2,yw,xb,yb,r=";
  		print_float y1; print_string " ";
  		print_float y2; print_string " ";
  		print_float yw; print_string " ";
  		print_float xb; print_string " ";
  		print_float yb; print_string " ";
  		print_float r; print_string " done print input\n"
		end;
	let targ = (y2 +. y1) *. 0.5 in
	let dy2 = yw -. r -. yb in
	let dy1 = yw -. r -. r -. y1 in
	let dy = dy1 +. dy2 in
	let al = atan (xb /. dy) in
	let xw = xb *. dy1 /. dy in
	let halfg = y2 -. y1 -. r in
	let rr = halfg *. (sin al) in
	begin
		if debug then begin
  		print_string "targ dy2 dy1 dy al xw halfg rr =";
  		print_float targ; print_string " ";
  		print_float dy2; print_string " ";
  		print_float dy1; print_string " ";
  		print_float dy; print_string " ";
  		print_float al; print_string " ";
  		print_float xw; print_string " ";
  		print_float halfg; print_string " ";
  		print_float rr; print_string " done print vars\n"
		end;
	let retv =
		(if rr < r then -1.0
		                          else xw)
		in
	print_float retv
	end end;;

main();;

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

let () =
  let rf () = float (read_int()) in
  let y1 = rf() in
  let y2 = rf() in
  let yw = rf() in
  let xb = rf() in
  let yb = rf() in
  let r = rf() in

  let yb = (yw-.r) +. (yw-.r-.yb) in
  (* reflected ball y coordinate *)

  let a = xb in
  let b = yb -. y1 in
  let c = sqrt (a*.a +. b*.b) in
  let h = (c/.a) *. r in
  let yh = r +. y1 in
  let yt = yh +. h in

    if yt >= y2 then Printf.printf "-1\n"
    else (
      (* we aim at position yh *)
      let d = xb in
      let c = yb -. yh in
      let b = yb -. (yw -. r) in
      let a = (d/.c) *. b in
      let xw = xb -. a in
	Printf.printf "%.10f\n" xw
    )


Define the simple skewness of a collection of numbers to be the collection's mean minus its median. You are given a list of n (not necessarily distinct) integers. Find the non-empty subset (with repetition) with the maximum simple skewness.The mean of a collection is the average of its elements. The median of a collection is its middle element when all of its elements are sorted, or the average of its two middle elements if it has even size.

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of elements in the list. The second line contains n integers xi (0 ≤ xi ≤ 1 000 000) — the ith element of the list.

In the first line, print a single integer k — the size of the subset. In the second line, print k integers — the elements of the subset in any order. If there are multiple optimal subsets, print any.

NoteIn the first case, the optimal subset is , which has mean 5, median 2, and simple skewness of 5 - 2 = 3.In the second case, the optimal subset is . Note that repetition is allowed.In the last case, any subset has the same median and mean, so all have simple skewness of 0.

open Printf
open Scanf

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

let long x = Int64.of_int x
let float = Int64.to_float

let ( --- ) (n,d) m = (n -- (d**m), d)
let comprat (n1,d1) (n2,d2) = compare (n1**d2) (n2**d1)
let max2 (a,i) (b,j) = if comprat a b >=0 then (a,i) else (b,j)

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 -> max2 (f k) a) (f i)

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

  Array.sort compare x;

  if n <= 2 then (
    printf "1\n";
    printf "%Ld\n" x.(0)
  ) else (
    let (answer3, (c, index)) =

      let sum = Array.make (n+1) 0L in
      for i=1 to n do
	sum.(i) <- sum.(i-1) ++ x.(i-1)
      done;

      let range i j = sum.(j+1) -- sum.(i) in

      maxf 1 (n-2) (fun i ->

	let mean c =
	  (* The the mean of the solution that uses c below i
	     (the median) and c above i. *)
	  let a1 = range (i-c) (i-1) in
	  let a2 = range (n-c) (n-1) in
	  (a1 ++ a2 ++ x.(i), (long (2*c+1))) --- x.(i)
	in

	let rec ternary lo hi =
	  if lo=hi then (mean hi, hi)
	  else if lo+1 = hi then max2 (mean lo, lo) (mean hi, hi)
	  else if lo+2 = hi then
	    max2 (max2 (mean lo, lo) (mean hi, hi)) (mean (lo+1), lo+1)
	  else
	    let lo1 = (lo+hi)/2 in
	    let hi1 = lo1 + 1 in
	    let (ml,mh) = (mean lo1, mean hi1) in
	    if comprat ml mh > 0 then ternary lo hi1 else ternary lo1 hi
	in
	let (ratval, c) = ternary 1 (min i (n-i)) in
	(ratval,(c,i))
      )
    in
    if comprat answer3 (0L,1L) > 0 then (
      printf "%d\n" (2*c+1);
      for i=c downto 1 do
	printf "%Ld " x.(index-i)
      done;
      printf "%Ld " x.(index);
      for i=c downto 1 do
	printf "%Ld " x.(n-i)
      done;
      print_newline()
    ) else (
      printf "1\n";
      printf "%Ld\n" x.(0)
    )
  )

open Printf
open Scanf

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

let long x = Int64.of_int x
let float = Int64.to_float

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 () =
  let n = read_int () in
  let x = Array.init n (fun _ -> long (read_int())) in

  Array.sort compare x;

  if n <= 2 then (
    printf "1\n";
    printf "%Ld\n" x.(0)
  ) else (
    let ((answer3, c), index)=

      let sum = Array.make (n+1) 0L in
      for i=1 to n do
	sum.(i) <- sum.(i-1) ++ x.(i-1)
      done;

      let range i j = sum.(j+1) -- sum.(i) in

      maxf 1 (n-2) (fun i ->

	let score c =
	  (* The the mean-median of the solution that uses c below i
	     (the median) and c above i. *)
	  let a1 = range (i-c) (i-1) in
	  let a2 = range (n-c) (n-1) in
	  (float (a1 ++ a2 ++ x.(i))) /. (float (long (2*c+1))) -. (float x.(i))
	in

	let rec ternary lo hi =
	  if lo=hi then (score hi, hi)
	  else if lo+1 = hi then max (score lo, lo) (score hi, hi)
	  else if lo+2 = hi then
	    max (max (score lo, lo) (score hi, hi)) (score (lo+1), lo+1)
	  else
	    let lo1 = (lo+hi)/2 in
	    let hi1 = lo1 + 1 in
	    let (ml,mh) = (score lo1, score hi1) in
	    if ml > mh then ternary lo hi1 else ternary lo1 hi
	in

	(ternary 1 (min i (n-i)), i)
      )
    in
    if answer3 > 0.0 then (
      printf "%d\n" (2*c+1);
      for i=c downto 1 do
	printf "%Ld " x.(index-c)
      done;
      printf "%Ld " x.(index);
      for i=c downto 1 do
	printf "%Ld " x.(n-c)
      done;
      print_newline()
    ) else (
      printf "1\n";
      printf "%Ld\n" x.(0)
    )
  )

open Printf
open Scanf

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

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

  Array.sort compare x;

  if n <= 2 then (
    printf "1\n";
    printf "%d\n" x.(0)
  ) else (
    let (answer3, index)=
      maxf 1 (n-2) (
	fun i ->
	  let x1 = float x.(i-1) in
	  let x2 = float x.(i) in
	  let x3 = float x.(n-1) in
	  ((x1+.x2+.x3) /. 3.0 -. x2, i)
      )
    in
    if answer3 > 0.0 then (
      printf "3\n";
      printf "%d %d %d\n" x.(index-1) x.(index) x.(n-1)
    ) else (
      printf "1\n";
      printf "%d\n" x.(0)
    )
  )

open Printf
open Scanf

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

let long x = Int64.of_int x
let float = Int64.to_float

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 () =
  let n = read_int () in
  let x = Array.init n (fun _ -> long (read_int())) in

  Array.sort compare x;

  if n <= 2 then (
    printf "1\n";
    printf "%Ld\n" x.(0)
  ) else (
    let ((answer3, c), index)=

      let sum = Array.make (n+1) 0L in
      for i=1 to n do
	sum.(i) <- sum.(i-1) ++ x.(i-1)
      done;

      let range i j = sum.(j+1) -- sum.(i) in

      maxf 1 (n-2) (fun i ->

	let score c =
	  (* The the mean-median of the solution that uses c below i
	     (the median) and c above i. *)
	  let a1 = range (i-c) (i-1) in
	  let a2 = range (n-c) (n-1) in
	  (float (a1 ++ a2 ++ x.(i))) /. (float (long (2*c+1))) -. (float x.(i))
	in

	let rec ternary lo hi =
	  if lo=hi then (score hi, hi)
	  else if lo+1 = hi then max (score lo, lo) (score hi, hi)
	  else if lo+2 = hi then
	    max (max (score lo, lo) (score hi, hi)) (score (lo+1), lo+1)
	  else
	    let lo1 = (lo+hi)/2 in
	    let hi1 = lo1 + 1 in
	    let (ml,mh) = (score lo1, score hi1) in
	    if ml > mh then ternary lo hi1 else ternary lo1 hi
	in

	(ternary 1 (min i (n-i)), i)
      )
    in
    if answer3 > 0.0 then (
      printf "%d\n" (2*c+1);
      for i=c downto 1 do
	printf "%Ld " x.(index-i)
      done;
      printf "%Ld " x.(index);
      for i=c downto 1 do
	printf "%Ld " x.(n-i)
      done;
      print_newline()
    ) else (
      printf "1\n";
      printf "%Ld\n" x.(0)
    )
  )


Edogawa Conan got tired of solving cases, and invited his friend, Professor Agasa, over. They decided to play a game of cards. Conan has n cards, and the i-th card has a number ai written on it.They take turns playing, starting with Conan. In each turn, the player chooses a card and removes it. Also, he removes all cards having a number strictly lesser than the number on the chosen card. Formally, if the player chooses the i-th card, he removes that card and removes the j-th card for all j such that aj < ai.A player loses if he cannot make a move on his turn, that is, he loses if there are no cards left. Predict the outcome of the game, assuming both players play optimally.

The first line contains an integer n (1 ≤ n ≤ 105) — the number of cards Conan has.  The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 105), where ai is the number on the i-th card.

If Conan wins, print "Conan" (without quotes), otherwise print "Agasa" (without quotes).

NoteIn the first example, Conan can just choose the card having number 7 on it and hence remove all the cards. After that, there are no cards left on Agasa's turn.In the second example, no matter which card Conan chooses, there will be one one card left, which Agasa can choose. After that, there are no cards left when it becomes Conan's turn again.

let get_int () = Scanf.scanf " %d" (fun i -> i)
let get_float () = Scanf.scanf " %f" (fun i -> i)

let get_array n =
  let rec aux acc k =
    if k = 0 then acc
    else aux (get_int () :: acc) (k - 1)
  in aux [] n

let main () =
  let n = get_int () in
  let a = get_array n in
  let sorted = List.sort (fun x y -> (compare y x)) a in
  let rec aux k = function
    | a :: (b :: _ as t) ->
      if a = b then aux (k + 1) t
      else
        if (k + 1) mod 2 = 0 then aux 0 t
        else "Conan"
    | _ ->
      if (k + 1) mod 2 = 0 then "Agasa"
      else "Conan"
  in
  let ans = aux 0 sorted in
  Printf.printf "%s" ans

let () = main ()


One day little Vasya found mom's pocket book. The book had n names of her friends and unusually enough, each name was exactly m letters long. Let's number the names from 1 to n in the order in which they are written.As mom wasn't home, Vasya decided to play with names: he chose three integers i, j, k (1 ≤ i < j ≤ n, 1 ≤ k ≤ m), then he took names number i and j and swapped their prefixes of length k. For example, if we take names "CBDAD" and "AABRD" and swap their prefixes with the length of 3, the result will be names "AABAD" and "CBDRD".You wonder how many different names Vasya can write instead of name number 1, if Vasya is allowed to perform any number of the described actions. As Vasya performs each action, he chooses numbers i, j, k independently from the previous moves and his choice is based entirely on his will. The sought number can be very large, so you should only find it modulo 1000000007 (109 + 7).

The first input line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of names and the length of each name, correspondingly. Then n lines contain names, each name consists of exactly m uppercase Latin letters.

Print the single number — the number of different names that could end up in position number 1 in the pocket book after the applying the procedures described above. Print the number modulo 1000000007 (109 + 7).

NoteIn the first sample Vasya can get the following names in the position number 1: "AAB", "AAA", "BAA" and "BAB".

let next_int () = Scanf.bscanf Scanf.Scanning.stdib " %d" (fun s -> s) ;;
let next_string () = Scanf.bscanf Scanf.Scanning.stdib " %s" (fun s -> s) ;;
let n = next_int () ;;
let m = next_int () ;;
let a = Array.init n (fun s -> next_string ()) ;;
let ret = ref 1L ;;
let k = Array.init m (fun i ->
  let h = Hashtbl.create n in
  Array.iter (fun s -> Hashtbl.replace h s.[i] ()) a;
  Hashtbl.length h
) ;;
Printf.printf "%Ld\n" (Array.fold_left (fun a l -> Int64.rem (Int64.mul a l)
1000000007L) Int64.one (Array.map Int64.of_int k)) ;;


You are given three positive integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$a < b < c$$$). You have to find three positive integers $$$x$$$, $$$y$$$, $$$z$$$ such that:$$$$$$x \bmod y = a,$$$$$$ $$$$$$y \bmod z = b,$$$$$$ $$$$$$z \bmod x = c.$$$$$$Here $$$p \bmod q$$$ denotes the remainder from dividing $$$p$$$ by $$$q$$$. It is possible to show that for such constraints the answer always exists.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10\,000$$$) — the number of test cases. Description of the test cases follows. Each test case contains a single line with three integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$1 \le a < b < c \le 10^8$$$).

For each test case output three positive integers $$$x$$$, $$$y$$$, $$$z$$$ ($$$1 \le x, y, z \le 10^{18}$$$) such that $$$x \bmod y = a$$$, $$$y \bmod z = b$$$, $$$z \bmod x = c$$$. You can output any correct answer.

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

(* x mod y = a *)
(* y mod z = b *)
(* z mod x = c *)

(* x = a + dy *)
(* y = b + ez *)
(* z = c + fx *)

(* y = b + e(c + fx) *)
(* x = a + d(b + e(c+fx)) *)
(* x(1-def) = a + db + dec *)
(* either d,e,f=1 or one of d, e, f = 0*)
(* y = b + e(c + f(a + dy)) *)
(* y(1-def) = b + ec + efa *)
(* z(1-def) = c + fa + fdb *)

(* if d,e, f = 1, a+b+c=0 *)
(* if d=0, x = a, y = b + ec+efa, z = c + fa *)
(* if e= 0, y = b *)
(* if f=0, z = c, x =a+db+dec, y=b+ec*)
(* 1 3 4 -> z = 4, x=1+3d+4ed, y=3+4e*)
(* if e=0 and f=0, y=b, z=c, x=a+db*)
(* y=3, z=4 and xmod3=1 and 4modx=4*)
(* x =1+3p, 4=4+xq, 4=4+(1+3p)q*)
(* x=a+bp = c + aq, p=(c-a+aq)/b*)
let rec get_x = function
    | a, b, c, d ->
        let x = a+d*b in
        if (x mod b == a && c mod x == c && b mod c = b) then x
        else get_x (a, b, c, (d+1))
;;
let rec run_cases = function
    | 0 -> 0
    | n ->
        let a = read_int () in
        let y = read_int () in
        let z = read_int () in
        let d = (z-a)/y in
        let x = get_x (a, y, z, d) in
        print_int x; print_string " ";
        print_int y; print_string " ";
        print_int z; print_string " ";
        print_newline ();
        run_cases (n-1)
;;
let num_cases = read_int () in
run_cases num_cases
;;