There are two rival donut shops.The first shop sells donuts at retail: each donut costs $$$a$$$ dollars.The second shop sells donuts only in bulk: box of $$$b$$$ donuts costs $$$c$$$ dollars. So if you want to buy $$$x$$$ donuts from this shop, then you have to buy the smallest number of boxes such that the total number of donuts in them is greater or equal to $$$x$$$.You want to determine two positive integer values:   how many donuts can you buy so that they are strictly cheaper in the first shop than in the second shop?  how many donuts can you buy so that they are strictly cheaper in the second shop than in the first shop? If any of these values doesn't exist then that value should be equal to $$$-1$$$. If there are multiple possible answers, then print any of them.The printed values should be less or equal to $$$10^9$$$. It can be shown that under the given constraints such values always exist if any values exist at all.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of testcases. Each of the next $$$t$$$ lines contains three integers $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \le a \le 10^9$$$, $$$2 \le b \le 10^9$$$, $$$1 \le c \le 10^9$$$).

For each testcase print two positive integers. For both shops print such $$$x$$$ that buying $$$x$$$ donuts in this shop is strictly cheaper than buying $$$x$$$ donuts in the other shop. $$$x$$$ should be greater than $$$0$$$ and less or equal to $$$10^9$$$. If there is no such $$$x$$$, then print $$$-1$$$. If there are multiple answers, then print any of them.

NoteIn the first testcase buying any number of donuts will be cheaper in the second shop. For example, for $$$3$$$ or $$$5$$$ donuts you'll have to buy a box of $$$10$$$ donuts for $$$4$$$ dollars. $$$3$$$ or $$$5$$$ donuts in the first shop would cost you $$$15$$$ or $$$25$$$ dollars, respectively, however. For $$$20$$$ donuts you'll have to buy two boxes for $$$8$$$ dollars total. Note that $$$3$$$ and $$$5$$$ are also valid answers for the second shop, along with many other answers.In the second testcase buying any number of donuts will be either cheaper in the first shop or the same price. $$$8$$$ donuts cost $$$32$$$ dollars in the first shop and $$$40$$$ dollars in the second shop (because you have to buy two boxes). $$$10$$$ donuts will cost $$$40$$$ dollars in both shops, so $$$10$$$ is not a valid answer for any of the shops.In the third testcase $$$1$$$ donut costs $$$2$$$ and $$$3$$$ dollars, respectively. $$$2$$$ donuts cost $$$4$$$ and $$$3$$$ dollars. Thus, $$$1$$$ is a valid answer for the first shop and $$$2$$$ is a valid answer for the second shop.In the fourth testcase $$$10^9$$$ donuts cost $$$10^{18}$$$ dollars in the first shop and $$$10^9$$$ dollars in the second shop.

let read_ints () =
  let str = read_line () in
  List.map (fun x -> int_of_string x) (Str.split (Str.regexp " +") str)
;;

let rec main (t : int) =
  if t = 0 then
    ()
  else
    let [a;b;c] = read_ints () in
    (if a < c then print_int 1
     else print_int (-1);
     print_string " ";
     let a2 = Int64.of_int a in
     let b2 = Int64.of_int b in
     let c2 = Int64.of_int c in
     if (Int64.mul a2 b2) > c2 then
       print_int b
    else
      print_int (-1);
    print_endline "";
    main (t-1))

;;

let n = read_int () in
main n

(* Codeforces 1373 A Donut Shops train  *)

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

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

let shops a b c  =
    let n1 = if a < c then Int64.one else Int64.minus_one in
		let n2 = if c < (Int64.mul b a) then b else Int64.minus_one in
		(n1, n2);;

let do_one () =
    let a = grl () in
    let b = grl () in
    let c = grl () in
    let n1,n2 = shops a b c in
    Printf.printf "%Ld %Ld\n" n1 n2;;

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

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

main();;

(* Codeforces 1373 A Donut Shops train  *)

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

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

let shops a b c  =
    let n1 = if a < c then 1 else -1 in
		let n2 = if c < (b * a) then b else -1 in
		(n1, n2);;

let do_one () =
    let a = gr () in
    let b = gr () in
    let c = gr () in
    let n1,n2 = shops a b c in
    Printf.printf "%d %d\n" n1 n2;;

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

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

main();;


Consider a sequence [a1, a2, ... , an]. Define its prefix product sequence .Now given n, find a permutation of [1, 2, ..., n], such that its prefix product sequence is a permutation of [0, 1, ..., n - 1].

The only input line contains an integer n (1 ≤ n ≤ 105).

In the first output line, print "YES" if such sequence exists, or print "NO" if no such sequence exists. If any solution exists, you should output n more lines. i-th line contains only an integer ai. The elements of the sequence should be different positive integers no larger than n. If there are multiple solutions, you are allowed to print any of them.

NoteFor the second sample, there are no valid sequences.

open Printf
open Scanf

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

let ( ** ) a b = Int64.mul a b
let ( %% ) a b = Int64.rem a b
let long x = Int64.of_int x

let inverse a p =
  let rec egcd a b =
    (* given a and b this returns (x, y, g) where g = gcd(a,b) and ax+by=g  *)
    if a = 0 then (0,1,b) else
      let (x,y,g) = egcd (b mod a) a in
      (y-(b/a)*x, x, g)
  in
  let (x,_,_) = egcd a p in
  if x >= 0 then x else x+p

let isprime n =
  let rec loop i = (i*i > n) || (n mod i <> 0 && loop (i+1)) in
  n >= 2 && loop 2

let () =
  let n = read_int() in

  if n=1 then printf "YES\n1\n"
  else if n=4 then printf "YES\n1 3 2 4\n"
  else if not (isprime n) then printf "NO\n"
  else (
    printf "YES\n";
    printf "1 ";
    for i=1 to n-2 do
      let a = long (inverse i n) in
      let b = long (i+1) in
      printf "%Ld " ((a**b) %% (long n))
    done;
    printf "%d\n" n;
  )

open Printf
open Scanf

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

let inverse a p =
  let rec egcd a b =
    (* given a and b this returns (x, y, g) where g = gcd(a,b) and ax+by=g  *)
    if a = 0 then (0,1,b) else
      let (x,y,g) = egcd (b mod a) a in
      (y-(b/a)*x, x, g)
  in
  let (x,_,_) = egcd a p in
  if x >= 0 then x else x+p

let isprime n =
  let rec loop i = (i*i > n) || (n mod i <> 0 && loop (i+1)) in
  n >= 2 && loop 2

let () =
  let n = read_int() in

  if n=1 then printf "YES\n1\n"
  else if not (isprime n) then printf "NO\n"
  else (
    printf "YES\n";
    printf "1 ";
    for i=1 to n-2 do
      printf "%d " (((inverse i n) * (i+1)) mod n)
    done;
    printf "%d\n" n;
  )

open Printf
open Scanf

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

let ( ** ) a b = Int64.mul a b
let ( %% ) a b = Int64.rem a b
let long x = Int64.of_int x

let inverse a p =
  let rec egcd a b =
    (* given a and b this returns (x, y, g) where g = gcd(a,b) and ax+by=g  *)
    if a = 0 then (0,1,b) else
      let (x,y,g) = egcd (b mod a) a in
      (y-(b/a)*x, x, g)
  in
  let (x,_,_) = egcd a p in
  if x >= 0 then x else x+p

let isprime n =
  let rec loop i = (i*i > n) || (n mod i <> 0 && loop (i+1)) in
  n >= 2 && loop 2

let () =
  let n = read_int() in

  if n=1 then printf "YES\n1\n"
  else if not (isprime n) then printf "NO\n"
  else (
    printf "YES\n";
    printf "1 ";
    for i=1 to n-2 do
      let a = long (inverse i n) in
      let b = long (i+1) in
      printf "%Ld " ((a**b) %% (long n))
    done;
    printf "%d\n" n;
  )

open Printf
open Scanf

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

let inverse a p =
  let rec egcd a b =
    (* given a and b this returns (x, y, g) where g = gcd(a,b) and ax+by=g  *)
    if a = 0 then (0,1,b) else
      let (x,y,g) = egcd (b mod a) a in
      (y-(b/a)*x, x, g)
  in
  let (x,_,_) = egcd a p in
  if x >= 0 then x else x+p

let isprime n =
  let rec loop i = (i*i > n) || (n mod i <> 0 && loop (i+1)) in
  n >= 2 && loop 2

let () =
  let n = read_int() in

  if not (isprime n) then (
    printf "NO\n"
  ) else (
    printf "YES\n";
    printf "1 ";
    for i=1 to n-2 do
      printf "%d " (((inverse i n) * (i+1)) mod n)
    done;
    printf "%d\n" n;
  )


A smile house is created to raise the mood. It has n rooms. Some of the rooms are connected by doors. For each two rooms (number i and j), which are connected by a door, Petya knows their value cij — the value which is being added to his mood when he moves from room i to room j.Petya wondered whether he can raise his mood infinitely, moving along some cycle? And if he can, then what minimum number of rooms he will need to visit during one period of a cycle?

The first line contains two positive integers n and m (), where n is the number of rooms, and m is the number of doors in the Smile House. Then follows the description of the doors: m lines each containing four integers i, j, cij и cji (1 ≤ i, j ≤ n, i ≠ j,  - 104 ≤ cij, cji ≤ 104). It is guaranteed that no more than one door connects any two rooms. No door connects the room with itself.

Print the minimum number of rooms that one needs to visit during one traverse of the cycle that can raise mood infinitely. If such cycle does not exist, print number 0.

NoteCycle is such a sequence of rooms a1, a2, ..., ak, that a1 is connected with a2, a2 is connected with a3, ..., ak - 1 is connected with ak, ak is connected with a1. Some elements of the sequence can coincide, that is, the cycle should not necessarily be simple. The number of rooms in the cycle is considered as k, the sequence's length. Note that the minimum possible length equals two.

let getnum =
  let l = 512000 in
  let buf = String.make l ' ' in
  let s = ref 0 in
  let pos = ref 0 in
  let getc () =
    if !pos >= !s then (
      let new_s = input stdin buf 0 l in
	(*if new_s = 0 then raise End_of_file;*)
	s := new_s;
	if new_s = 0 then (
	  buf.[0] <- '\000';
	  s := 1;
	);
	pos := 0;
    );
    let c = buf.[!pos] in
      incr pos;
      c
  in
  let aux () =
    let n = ref 0 in
    let k = ref 1 in
      if !s - !pos < 50 then (
	let c = ref (getc ()) in
	  while !c <= ' ' do
	    c := getc ()
	  done;
	  if !c = '-' then (
	    k := -1;
	    c := getc ();
	  );
	  while !c > ' ' do
	    n := !n * 10 + (Char.code !c - Char.code '0');
	    c := getc ()
	  done;
	  !n * !k
      ) else (
	let c = ref (buf.[!pos]) in
	  incr pos;
	  while !c <= ' ' do
	    c := buf.[!pos];
	    incr pos;
	  done;
	  if !c = '-' then (
	    k := -1;
	    c := buf.[!pos];
	    incr pos;
	  );
	  while !c > ' ' do
	    n := !n * 10 + (Char.code !c - Char.code '0');
	    c := buf.[!pos];
	    incr pos;
	  done;
	  !n * !k
      )
  in
    aux

let _ =
  let n = getnum () in
  let m = getnum () in
  let es = Array.make_matrix n n 100000000 in
  let _ =
    for i = 0 to n - 1 do
      es.(i).(i) <- 0;
    done;
    for k = 1 to m do
      let i = getnum () - 1 in
      let j = getnum () - 1 in
      let cij = getnum () in
      let cji = getnum () in
	es.(i).(j) <- -cij;
	es.(j).(i) <- -cji;
    done
  in
  let mmult a b =
    let n = Array.length a in
    let c = Array.make_matrix n n 100000000 in
      for i = 0 to n - 1 do
	let ai = a.(i) in
	  for k = 0 to n - 1 do
	    let bk = Array.unsafe_get b k in
	      for j = n - 1 downto 0 do
		let d = Array.unsafe_get ai k + Array.unsafe_get bk j in
		let ci = Array.unsafe_get c i in
		  if Array.unsafe_get ci j > d
		  then Array.unsafe_set ci j d
	      done
	  done
      done;
      c
  in
  let b = Array.make 10 [| |] in
  let _ =
    b.(1) <- es;
    for i = 2 to 9 do
      b.(i) <- mmult b.(i - 1) b.(i - 1)
    done
  in
  let power n =
    let m = Array.length es in
    let n = ref n in
    let r = ref (Array.make_matrix m m 100000000) in
    let _ =
      for i = 0 to m - 1 do
	!r.(i).(i) <- 0
      done
    in
    let bi = ref 1 in
      while !n > 0 do
	if !n mod 2 = 1 then (
	  r := mmult !r b.(!bi)
	);
	incr bi;
	n := !n / 2;
      done;
      !r
  in
  let l = ref (-1) in
  let r = ref (n + 1) in
    while !r - !l > 1 do
      let m = (!l + !r) / 2 in
      let es = power m in
	(*Printf.printf "asd %d\n" m;
	for i = 0 to n - 1 do
	  for j = 0 to n - 1 do
	    Printf.printf "%d " es.(i).(j)
	  done;
	  Printf.printf "\n"
	done;*)
      let b = ref false in
	for i = 0 to n - 1 do
	  if es.(i).(i) < 0
	  then b := true
	done;
	if !b
	then r := m
	else l := m
    done;
    if !r <= n
    then Printf.printf "%d\n" !r
    else Printf.printf "0\n"

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let es = Array.make_matrix n n 100000000 in
  let _ =
    for k = 1 to m do
      let i = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let j = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let cij = Scanf.bscanf sb "%d " (fun s -> s) in
      let cji = Scanf.bscanf sb "%d " (fun s -> s) in
	es.(i).(j) <- -cij;
	es.(j).(i) <- -cji;
	es.(k - 1).(k - 1) <- 0;
    done
  in
  let min (x : int) y = if x <= y then x else y in
  let mmult a b =
    let n = Array.length a in
    let c = Array.make_matrix n n 100000000 in
      for i = 0 to n - 1 do
	for j = 0 to n - 1 do
	  for k = 0 to n - 1 do
	    c.(i).(j) <- min c.(i).(j) (a.(i).(k) + b.(k).(j))
	  done
	done
      done;
      c
  in
  let power b n =
    let m = Array.length b in
    let n = ref n in
    let r = ref (Array.make_matrix m m 100000000) in
    let _ =
      for i = 0 to m - 1 do
	!r.(i).(i) <- 0
      done
    in
    let b = ref b in
      while !n > 0 do
	if !n mod 2 = 1 then (
	  r := mmult !r !b
	);
	b := mmult !b !b;
	n := !n / 2;
      done;
      !r
  in
  let l = ref (-1) in
  let r = ref (n + 1) in
    while !r - !l > 1 do
      let m = (!l + !r) / 2 in
      let es = power es m in
	(*Printf.printf "asd %d\n" m;
	for i = 0 to n - 1 do
	  for j = 0 to n - 1 do
	    Printf.printf "%d " es.(i).(j)
	  done;
	  Printf.printf "\n"
	done;*)
      let b = ref false in
	for i = 0 to n - 1 do
	  if es.(i).(i) < 0
	  then b := true
	done;
	if !b
	then r := m
	else l := m
    done;
    if !r <= n
    then Printf.printf "%d\n" !r
    else Printf.printf "0\n"

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let es = Array.make n [] in
  let _ =
    for k = 1 to m do
      let i = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let j = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let cij = Scanf.bscanf sb "%d " (fun s -> s) in
      let cji = Scanf.bscanf sb "%d " (fun s -> s) in
	es.(i) <- (j, -cij) :: es.(i);
	es.(j) <- (i, -cji) :: es.(j);
    done
  in
  let es = Array.map Array.of_list es in
  let bellman_ford nv =
    let p = Array.make nv (-1) in
    let l = Array.make nv true in
    let w = Array.make nv 0 in
      for k = 1 to nv + 1 do
	for u = 0 to nv - 1 do
	  if l.(u) then (
	    for i = 0 to Array.length es.(u) - 1 do
	      let (v, w') = es.(u).(i) in
		if w.(u) + w' < w.(v)
		then (
		  w.(v) <- w.(u) + w';
		  p.(v) <- u;
		  l.(v) <- true;
		)
	    done;
	    l.(u) <- false;
	  )
	done
      done;
      p
  in
  let p = bellman_ford n in
  let res = ref (n + 1) in
  let v = Array.make n false in
    for i = 0 to n - 1 do
      if not v.(i) then (
	v.(i) <- true;
	let c = ref 1 in
	let u = ref p.(i) in
	  while !u >= 0 && not v.(!u) do
	    v.(!u) <- true;
	    u := p.(!u);
	    incr c;
	  done;
	  if !u = i
	  then res := min !res !c
      )
    done;
    if !res <= n
    then Printf.printf "%d\n" !res
    else Printf.printf "0\n"

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let es = Array.make_matrix n n 1000000000 in
  let _ =
    for k = 1 to m do
      let i = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let j = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let cij = Scanf.bscanf sb "%d " (fun s -> s) in
      let cji = Scanf.bscanf sb "%d " (fun s -> s) in
	es.(i).(j) <- -cij;
	es.(j).(i) <- -cji;
	es.(k - 1).(k - 1) <- 0;
    done
  in
  let min (x : int) y = if x <= y then x else y in
  let mmult a b =
    let n = Array.length a in
    let c = Array.make_matrix n n 1000000000 in
      for i = 0 to n - 1 do
	for j = 0 to n - 1 do
	  for k = 0 to n - 1 do
	    c.(i).(j) <- min c.(i).(j) (a.(i).(k) + b.(k).(j))
	  done
	done
      done;
      c
  in
  let power b n =
    let m = Array.length b in
    let n = ref n in
    let r = ref (Array.make_matrix m m 1000000000) in
    let _ =
      for i = 0 to m - 1 do
	!r.(i).(i) <- 0
      done
    in
    let b = ref b in
      while !n > 0 do
	if !n mod 2 = 1 then (
	  r := mmult !r !b
	);
	b := mmult !b !b;
	n := !n / 2;
      done;
      !r
  in
  let l = ref (-1) in
  let r = ref (n + 1) in
    while !r - !l > 1 do
      let m = (!l + !r) / 2 in
      let es = power es m in
	(*Printf.printf "asd %d\n" m;
	for i = 0 to n - 1 do
	  for j = 0 to n - 1 do
	    Printf.printf "%d " es.(i).(j)
	  done;
	  Printf.printf "\n"
	done;*)
      let b = ref false in
	for i = 0 to n - 1 do
	  if es.(i).(i) < 0
	  then b := true
	done;
	if !b
	then r := m
	else l := m
    done;
    if !r <= n
    then Printf.printf "%d\n" !r
    else Printf.printf "0\n"

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let es = Array.make_matrix n n 1000000000 in
  let _ =
    for k = 1 to m do
      let i = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let j = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let cij = Scanf.bscanf sb "%d " (fun s -> s) in
      let cji = Scanf.bscanf sb "%d " (fun s -> s) in
	es.(i).(j) <- -cij;
	es.(j).(i) <- -cji;
	es.(k - 1).(k - 1) <- 0;
    done
  in
  let min (x : int) y = if x <= y then x else y in
  let mmult a b =
    let n = Array.length a in
    let c = Array.make_matrix n n 1000000000 in
      for i = 0 to n - 1 do
	for j = 0 to n - 1 do
	  for k = 0 to n - 1 do
	    c.(i).(j) <- min c.(i).(j) (a.(i).(k) + b.(k).(j))
	  done
	done
      done;
      c
  in
  let power b n =
    let m = Array.length b in
    let n = ref n in
    let r = ref (Array.make_matrix m m 100000000) in
    let _ =
      for i = 0 to m - 1 do
	!r.(i).(i) <- 0
      done
    in
    let b = ref b in
      while !n > 0 do
	if !n mod 2 = 1 then (
	  r := mmult !r !b
	);
	b := mmult !b !b;
	n := !n / 2;
      done;
      !r
  in
  let l = ref (-1) in
  let r = ref (n + 1) in
    while !r - !l > 1 do
      let m = (!l + !r) / 2 in
      let es = power es m in
	(*Printf.printf "asd %d\n" m;
	for i = 0 to n - 1 do
	  for j = 0 to n - 1 do
	    Printf.printf "%d " es.(i).(j)
	  done;
	  Printf.printf "\n"
	done;*)
      let b = ref false in
	for i = 0 to n - 1 do
	  if es.(i).(i) < 0
	  then b := true
	done;
	if !b
	then r := m
	else l := m
    done;
    if !r <= n
    then Printf.printf "%d\n" !r
    else Printf.printf "0\n"

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let es = Array.make n [] in
  let _ =
    for k = 1 to m do
      let i = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let j = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let cij = Scanf.bscanf sb "%d " (fun s -> s) in
      let cji = Scanf.bscanf sb "%d " (fun s -> s) in
	es.(i) <- (j, -cij) :: es.(i);
	es.(j) <- (i, -cji) :: es.(j);
    done
  in
  let es = Array.map Array.of_list es in
  let bellman_ford nv =
    let p = Array.make nv (-1) in
    let l = Array.make nv true in
    let w = Array.make nv 0 in
      for k = 1 to nv do
	for u = 0 to nv - 1 do
	  if l.(u) then (
	    for i = 0 to Array.length es.(u) - 1 do
	      let (v, w') = es.(u).(i) in
		if w.(u) + w' < w.(v)
		then (
		  w.(v) <- w.(u) + w';
		  p.(v) <- u;
		  l.(v) <- true;
		)
	    done;
	    l.(u) <- false;
	  )
	done
      done;
      p
  in
  let p = bellman_ford n in
  let res = ref (n + 1) in
  let v = Array.make n (-1) in
  let d = Array.make n 0 in
    for i = 0 to n - 1 do
      if v.(i) < 0 then (
	v.(i) <- i;
	d.(i) <- 0;
	let c = ref 1 in
	let u = ref p.(i) in
	  while !u >= 0 && v.(!u) < 0 do
	    v.(!u) <- i;
	    d.(!u) <- !c;
	    u := p.(!u);
	    incr c;
	  done;
	  if !u >= 0 && v.(!u) = i
	  then res := min !res (!c - d.(!u))
      )
    done;
    if !res <= n
    then Printf.printf "%d\n" !res
    else Printf.printf "0\n"

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let es = Array.make n [] in
  let _ =
    for k = 1 to m do
      let i = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let j = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let cij = Scanf.bscanf sb "%d " (fun s -> s) in
      let cji = Scanf.bscanf sb "%d " (fun s -> s) in
	es.(i) <- (j, -cij) :: es.(i);
	es.(j) <- (i, -cji) :: es.(j);
    done
  in
  let es = Array.map Array.of_list es in
  let bellman_ford nv =
    let p = Array.make nv (-1) in
    let l = Array.make nv true in
    let w = Array.make nv 0 in
      for k = 1 to nv do
	for u = 0 to nv - 1 do
	  if l.(u) then (
	    for i = 0 to Array.length es.(u) - 1 do
	      let (v, w') = es.(u).(i) in
		if w.(u) + w' < w.(v)
		then (
		  w.(v) <- w.(u) + w';
		  p.(v) <- u;
		  l.(v) <- true;
		)
	    done;
	    l.(u) <- false;
	  )
	done
      done;
      p
  in
  let p = bellman_ford n in
  let res = ref (n + 1) in
  let v = Array.make n false in
    for i = 0 to n - 1 do
      if not v.(i) then (
	v.(i) <- true;
	let c = ref 1 in
	let u = ref p.(i) in
	  while !u >= 0 && not v.(!u) do
	    v.(!u) <- true;
	    u := p.(!u);
	    incr c;
	  done;
	  if !u = i
	  then res := min !res !c
      )
    done;
    if !res <= n
    then Printf.printf "%d\n" !res
    else Printf.printf "0\n"


Note that girls in Arpa’s land are really attractive.Arpa loves overnight parties. In the middle of one of these parties Mehrdad suddenly appeared. He saw n pairs of friends sitting around a table. i-th pair consisted of a boy, sitting on the ai-th chair, and his girlfriend, sitting on the bi-th chair. The chairs were numbered 1 through 2n in clockwise direction. There was exactly one person sitting on each chair.  There were two types of food: Kooft and Zahre-mar. Now Mehrdad wonders, was there any way to serve food for the guests such that:   Each person had exactly one type of food,  No boy had the same type of food as his girlfriend,  Among any three guests sitting on consecutive chairs, there was two of them who had different type of food. Note that chairs 2n and 1 are considered consecutive. Find the answer for the Mehrdad question. If it was possible, find some arrangement of food types that satisfies the conditions.

The first line contains an integer n (1  ≤  n  ≤  105) — the number of pairs of guests. The i-th of the next n lines contains a pair of integers ai and bi (1  ≤ ai, bi ≤  2n) — the number of chair on which the boy in the i-th pair was sitting and the number of chair on which his girlfriend was sitting. It's guaranteed that there was exactly one person sitting on each chair.

If there is no solution, print -1. Otherwise print n lines, the i-th of them should contain two integers which represent the type of food for the i-th pair. The first integer in the line is the type of food the boy had, and the second integer is the type of food the girl had. If someone had Kooft, print 1, otherwise print 2. If there are multiple solutions, print any of them.

open Printf
open Scanf

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

let () =
  let n = read_int () in
  let pair = Array.make (2*n) 0 in
  let pair_index = Array.make n 0 in
  for i=0 to n-1 do
    let (a,b) = read_pair() in
    let (a,b) = (a-1,b-1) in
    pair.(a) <- b;
    pair.(b) <- a;
    pair_index.(i) <- a
  done;

  let color = Array.make (2*n) (-1) in

  let rec dfs0 v = if color.(v) < 0 then (
    color.(v) <- 0;
    dfs1 pair.(v)
  )
  and dfs1 v =
    color.(v) <- 1;
    dfs0 (v lxor 1)
  in

  for v=0 to 2*n-1 do dfs0 v done;

  for i=0 to n-1 do
    let a = pair_index.(i) in
    printf "%d %d\n" (1+color.(a)) (1+color.(pair.(a)))
  done

let n = read_int () in
let nn = 2 * n in
let io2chair = Array.make nn 0 and chair2io = Array.make nn 0 in
for i = 0 to nn - 1 do
    let chair = Scanf.scanf "%d " (fun x -> x - 1) in
    io2chair.(i) <- chair ; chair2io.(chair) <- i
done;
let out = Array.make nn 0 in
let rec fill_cycle i =
    if out.(i) == 0 then (
        out.(i) <- 1;
        let neighbor = chair2io.(io2chair.(i) lxor 1) in
            out.(neighbor) <- 2;
            fill_cycle (neighbor lxor 1)
    ) in
for i = 0 to nn - 1 do
    fill_cycle i
done;
Array.iter (Printf.printf "%d ") out

open Printf
open Scanf

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

let () =
  let n = read_int () in
  let pair = Array.make (2*n) 0 in
  let pair_index = Array.make n 0 in
  for i=0 to n-1 do
    let (a,b) = read_pair() in
    let (a,b) = (a-1,b-1) in
    pair.(a) <- b;
    pair.(b) <- a;
    pair_index.(i) <- a
  done;

  let color = Array.make (2*n) 0 in

  let rec dfs1 v = if color.(v) = 0 then (
    color.(v) <- 1;
    dfs2 pair.(v)
  )
  and dfs2 v =
    color.(v) <- 2;
    dfs1 (v lxor 1)
  in

  for v=0 to 2*n-1 do dfs1 v done;

  for i=0 to n-1 do
    let a = pair_index.(i) in
    printf "%d %d\n" color.(a) color.(pair.(a))
  done

open Printf
open Scanf

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

let () =
  let n = read_int () in
  let pair = Array.make (2*n) 0 in
  let pair_index = Array.make n 0 in
  for i=0 to n-1 do
    let (a,b) = read_pair() in
    let (a,b) = (a-1,b-1) in
    pair.(a) <- b;
    pair.(b) <- a;
    pair_index.(i) <- a
  done;

  let color = Array.make (2*n) (-1) in

  let rec dfs0 v = if color.(v) < 0 then (
    color.(v) <- 0;
    dfs1 pair.(v)
  )
  and dfs1 v =
    color.(v) <- 1;
    dfs0 (v lxor 1)
  in

  let rec dfs_loop v =
    if v < 2*n then if color.(v) < 0 then dfs0 v else dfs_loop (v+1)
  in

  dfs_loop 0;

  for i=0 to n-1 do
    let a = pair_index.(i) in
    printf "%d %d\n" (1+color.(a)) (1+color.(pair.(a)))
  done


Once Bob saw a string. It contained so many different letters, that the letters were marked by numbers, but at the same time each letter could be met in the string at most 10 times. Bob didn't like that string, because it contained repeats: a repeat of length x is such a substring of length 2x, that its first half coincides character by character with its second half. Bob started deleting all the repeats from the string. He does it as follows: while it's possible, Bob takes the shortest repeat, if it is not unique, he takes the leftmost one, and deletes its left half and everything that is to the left of this repeat.You're given the string seen by Bob. Find out, what it will look like after Bob deletes all the repeats in the way described above.

The first input line contains integer n (1 ≤ n ≤ 105) — length of the string. The following line contains n space-separated integer numbers from 0 to 109 inclusive — numbers that stand for the letters of the string. It's guaranteed that each letter can be met in the string at most 10 times.

In the first line output the length of the string's part, left after Bob's deletions. In the second line output all the letters (separated by a space) of the string, left after Bob deleted all the repeats in the described way.

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

let base = 37

let () =
  let n = read_int 0 in
  let last = Hashtbl.create n in
  let nxt = Array.make n 0 in
  let a = Array.init n read_int in
  for i = n-1 downto 0 do
    let x = try Hashtbl.find last a.(i)
      with Not_found -> n in
    nxt.(i) <- x;
    Hashtbl.replace last a.(i) i
  done;

  let h = Array.make (n+1) 0 in
  let p = Array.make (n+1) 1 in
  for i = 0 to n-1 do
    p.(i+1) <- p.(i) * base;
    h.(i+1) <- h.(i) * base + a.(i)
  done;

  let signature i j =
    h.(j) - h.(i) * p.(j-i) in
  let cand = ref [] in
  for i = 0 to n-1 do
    let j = ref i in
    while j := nxt.(!j); !j < n do
      let k = !j*2-i in
      if k <= n && signature i !j = signature !j k then
        cand := (!j-i,i) :: !cand
    done
  done;

  let lb = ref 0 in
  cand := List.sort compare !cand;
  List.iter (fun (len,i) ->
    if !lb <= i then
      lb := i+len
  ) !cand;
  Printf.printf "%d\n" (n - !lb);
  for i = !lb to n-1 do
    Printf.printf "%d%c" a.(i) (if i = n-1 then '\n' else ' ')
  done


You are given a line of $$$n$$$ colored squares in a row, numbered from $$$1$$$ to $$$n$$$ from left to right. The $$$i$$$-th square initially has the color $$$c_i$$$.Let's say, that two squares $$$i$$$ and $$$j$$$ belong to the same connected component if $$$c_i = c_j$$$, and $$$c_i = c_k$$$ for all $$$k$$$ satisfying $$$i < k < j$$$. In other words, all squares on the segment from $$$i$$$ to $$$j$$$ should have the same color.For example, the line $$$[3, 3, 3]$$$ has $$$1$$$ connected component, while the line $$$[5, 2, 4, 4]$$$ has $$$3$$$ connected components.The game "flood fill" is played on the given line as follows:   At the start of the game you pick any starting square (this is not counted as a turn).  Then, in each game turn, change the color of the connected component containing the starting square to any other color. Find the minimum number of turns needed for the entire line to be changed into a single color.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 5000$$$) — the number of squares. The second line contains integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \le c_i \le 5000$$$) — the initial colors of the squares.

Print a single integer — the minimum number of the turns needed.

NoteIn the first example, a possible way to achieve an optimal answer is to pick square with index $$$2$$$ as the starting square and then play as follows:  $$$[5, 2, 2, 1]$$$  $$$[5, 5, 5, 1]$$$  $$$[1, 1, 1, 1]$$$ In the second example, a possible way to achieve an optimal answer is to pick square with index $$$5$$$ as the starting square and then perform recoloring into colors $$$2, 3, 5, 4$$$ in that order.In the third example, the line already consists of one color only.

open Printf
open Scanf

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

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

  let c = Array.make n 0 in

  c.(0) <- x.(0);

  let rec compress i j = if j = n then i else (
    if c.(i-1) = x.(j) then compress i (j+1)
    else (
      c.(i) <- x.(j);
      compress (i+1) (j+1)
    )
  ) in

  let n = compress 1 1 in

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

  print_newline()
  *)

  let dp = Array.make_matrix (n+1) (n+1) (-1) in

  for j=1 to n do
    dp.(0).(j) <- n-j
  done;

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

  let rec count i j =
    if dp.(i).(j) >= 0 then dp.(i).(j) else
      let answer =
	if c.(i-1) = c.(j) then 1 + (count (i-1) (j+1))
	else 1 + min (count (i-1) j) (count i (j+1))
      in
      dp.(i).(j) <- answer;
      answer
  in

  let answer = minf 0 (n-1) (fun i -> count i (i+1)) in

  printf "%d\n" answer


Ilya lives in a beautiful city of Chordalsk.There are $$$n$$$ houses on the street Ilya lives, they are numerated from $$$1$$$ to $$$n$$$ from left to right; the distance between every two neighboring houses is equal to $$$1$$$ unit. The neighboring houses are $$$1$$$ and $$$2$$$, $$$2$$$ and $$$3$$$, ..., $$$n-1$$$ and $$$n$$$. The houses $$$n$$$ and $$$1$$$ are not neighboring.The houses are colored in colors $$$c_1, c_2, \ldots, c_n$$$ so that the $$$i$$$-th house is colored in the color $$$c_i$$$. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.Ilya wants to select two houses $$$i$$$ and $$$j$$$ so that $$$1 \leq i < j \leq n$$$, and they have different colors: $$$c_i \neq c_j$$$. He will then walk from the house $$$i$$$ to the house $$$j$$$ the distance of $$$(j-i)$$$ units.Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.Help Ilya, find this maximum possible distance.

The first line contains a single integer $$$n$$$ ($$$3 \leq n \leq 300\,000$$$) — the number of cities on the street. The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq n$$$) — the colors of the houses. It is guaranteed that there is at least one pair of indices $$$i$$$ and $$$j$$$ so that $$$1 \leq i < j \leq n$$$ and $$$c_i \neq c_j$$$.

Print a single integer — the maximum possible distance Ilya can walk.

NoteIn the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of $$$5-1 = 4$$$ units.In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of $$$1$$$ unit.In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of $$$7-3 = 4$$$ units.

let () =
  let n = Scanf.scanf "%d\n" (fun x -> x) in
  let a = Array.init n (fun _i ->
      Scanf.scanf "%d " (fun x -> x))
  in
  if a.(0) <> a.(n-1) then Printf.printf "%d" (n-1) else begin
    let l = ref 0 in
    while (!l < n-1 && a.(!l) = a.(0)) do incr l; done;
    let r = ref (n-1) in
    while (!r > 0 && a.(!r) = a.(n-1)) do decr r; done;
    let max = ref 0 in
    if !l < n-1 then max := n - 1 - !l;
    if !r > 0 && (!max < !r) then max := !r;
    Printf.printf "%d" (!max)
  end;;

let () =
  let n = Scanf.scanf "%d\n" (fun x -> x) in
  let a = Array.init n (fun _i ->
      Scanf.scanf "%d " (fun x -> x))
  in
  if a.(0) <> a.(n-1) then Printf.printf "%d" (n-1) else begin
    let l = ref 0 in
    while (!l < n-1 && a.(!l) = a.(0)) do incr l; done;
    let r = ref (n-1) in
    while (!r > 0 && a.(!r) = a.(n-1)) do decr r; done;
    let max = ref 0 in
    if !l < n-1 then max := !l;
    if !r > 0 && (!max < n-1 - !r) then max := n-1 - !r;
    Printf.printf "%d" (!max)
  end;;


Moscow is hosting a major international conference, which is attended by n scientists from different countries. Each of the scientists knows exactly one language. For convenience, we enumerate all languages of the world with integers from 1 to 109.In the evening after the conference, all n scientists decided to go to the cinema. There are m movies in the cinema they came to. Each of the movies is characterized by two distinct numbers — the index of audio language and the index of subtitles language. The scientist, who came to the movie, will be very pleased if he knows the audio language of the movie, will be almost satisfied if he knows the language of subtitles and will be not satisfied if he does not know neither one nor the other (note that the audio language and the subtitles language for each movie are always different). Scientists decided to go together to the same movie. You have to help them choose the movie, such that the number of very pleased scientists is maximum possible. If there are several such movies, select among them one that will maximize the number of almost satisfied scientists.

The first line of the input contains a positive integer n (1 ≤ n ≤ 200 000) — the number of scientists. The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the index of a language, which the i-th scientist knows. The third line contains a positive integer m (1 ≤ m ≤ 200 000) — the number of movies in the cinema.  The fourth line contains m positive integers b1, b2, ..., bm (1 ≤ bj ≤ 109), where bj is the index of the audio language of the j-th movie. The fifth line contains m positive integers c1, c2, ..., cm (1 ≤ cj ≤ 109), where cj is the index of subtitles language of the j-th movie. It is guaranteed that audio languages and subtitles language are different for each movie, that is bj ≠ cj.

Print the single integer — the index of a movie to which scientists should go. After viewing this movie the number of very pleased scientists should be maximum possible. If in the cinema there are several such movies, you need to choose among them one, after viewing which there will be the maximum possible number of almost satisfied scientists.  If there are several possible answers print any of them.

NoteIn the first sample, scientists must go to the movie with the index 2, as in such case the 1-th and the 3-rd scientists will be very pleased and the 2-nd scientist will be almost satisfied.In the second test case scientists can go either to the movie with the index 1 or the index 3. After viewing any of these movies exactly two scientists will be very pleased and all the others will be not satisfied.

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 a = Array.init n (fun _ -> read_int()) in
  let m = read_int () in
  let b = Array.init m (fun _ -> read_int()) in
  let c = Array.init m (fun _ -> read_int()) in

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

  let get t = try Hashtbl.find h t with Not_found -> 0 in

  for i=0 to n-1 do
    increment a.(i)
  done;

  let eval_movie i = (get b.(i), get c.(i),i) in

  let (_,_, besti) = maxf 0 (m-1) eval_movie in

  printf "%d\n" (besti+1)


There are $$$n$$$ rectangles in a row. You can either turn each rectangle by $$$90$$$ degrees or leave it as it is. If you turn a rectangle, its width will be height, and its height will be width. Notice that you can turn any number of rectangles, you also can turn all or none of them. You can not change the order of the rectangles.Find out if there is a way to make the rectangles go in order of non-ascending height. In other words, after all the turns, a height of every rectangle has to be not greater than the height of the previous rectangle (if it is such).

The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of rectangles. Each of the next $$$n$$$ lines contains two integers $$$w_i$$$ and $$$h_i$$$ ($$$1 \leq w_i, h_i \leq 10^9$$$) — the width and the height of the $$$i$$$-th rectangle.

Print "YES" (without quotes) if there is a way to make the rectangles go in order of non-ascending height, otherwise print "NO". You can print each letter in any case (upper or lower).

NoteIn the first test, you can rotate the second and the third rectangles so that the heights will be [4, 4, 3].In the second test, there is no way the second rectangle will be not higher than the first one.

open Printf open Scanf
module MyInt64 = struct
	(* include Int64 *)
	(* int *)
	let (+$) = (+)
	let (-$) = (-)
	let (/$) = (/)
	let ( *$) = ( * )
	(* int64 *)
	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 labs p = if p < 0L then -1L*p else p
	(* let (~|) p = Int64.of_int p *)
	let (~|) p = Int64.to_int p

	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 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
end open MyInt64

let () =
	let n = get_i64 ()
	in
	let a = Array.make ~|n (0L,0L)
	in
	for i=0 to ~|n-$1 do
		let w,h = get_2_i64 ()
		in a.(i) <- (w,h)
	done;
	Array.fold_left (fun (f,u) (w,h) ->
		let m = max w h
		in
		if u>=m then (f,m) else
		let m = min w h
		in if u>=m then (f,m) else (false,m)
	) (true,Int64.max_int) a
	|> fst
	|> (fun v -> if v then "YES" else "NO")
	|> print_endline


Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $$$2 \cdot n$$$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $$$n$$$ people in each row). Students are numbered from $$$1$$$ to $$$n$$$ in each row in order from left to right.  Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $$$2n$$$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose.

The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of students in each row. The second line of the input contains $$$n$$$ integers $$$h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$$$ ($$$1 \le h_{1, i} \le 10^9$$$), where $$$h_{1, i}$$$ is the height of the $$$i$$$-th student in the first row. The third line of the input contains $$$n$$$ integers $$$h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$$$ ($$$1 \le h_{2, i} \le 10^9$$$), where $$$h_{2, i}$$$ is the height of the $$$i$$$-th student in the second row.

Print a single integer — the maximum possible total height of players in a team Demid can choose.

NoteIn the first example Demid can choose the following team as follows:   In the second example Demid can choose the following team as follows:

let read_int_list() = read_line() |> Str.(split (regexp " ")) |> List.map (fun x -> int_of_string x);;
let (+!) = Int64.add;;

let n_student = read_int();;
let height_list1 = read_int_list();;
let height_list2 = read_int_list();;

let rec calculate n hl1 hl2 =
  if n == 0
    then (0L, 0L, 0L)
    else
      let (f11, f12, f2) = calculate (n - 1) (List.tl hl1) (List.tl hl2)
      in
        let f01 = (List.hd hl1 |> Int64.of_int) +! (max f12 f2)
        and f02 = (List.hd hl2  |> Int64.of_int) +! (max f11 f2)
        and f1 = max f11 f12
        in
          (f01, f02, f1)
;;

let (f01, f02, f1) = calculate n_student height_list1 height_list2;;
Printf.printf "%Ld\n" (max f01 f02);


If you have ever interacted with a cat, you have probably noticed that they are quite particular about how to pet them. Here is an approximate map of a normal cat.  However, some cats won't tolerate this nonsense from the humans. Here is a map of a grumpy cat.  You have met a cat. Can you figure out whether it's normal or grumpy?

NotePlease make sure to use the stream flushing operation after each query in order not to leave part of your output in some buffer.

let str: string list ref = ref []

let cool = "cool"

let mem s =
  s = cool

let result l =
  List.exists (fun s -> mem s) l

let n = ["cool";"not bad";"great";"don't think so"; "don't touch me!";]

let g = ["don't even"; "are you serious?" ; "worse"; "terrible"; "go die in a hole"; "no way"]
let _ =
  let i = ref 0 in
  while !i < 10
  do
    Format.printf "%d@." !i;
    let s = read_line () in
    flush_all ();
    if List.mem s n then
      begin
        Format.printf "normal@.";
        exit 0
      end
    else if List.mem s g then
      begin
        Format.printf "grumpy@.";
        exit 0
      end
    else
      incr i
  done

module StringSet = Set.Make(String)

type cat =
  | Normal
  | Grumpy

let cats = [
  Normal, StringSet.of_list [
    "great!";
    "don't think so";
    "nod bad";
    "don't touch me";
    "cool"
  ]
  ; Grumpy, StringSet.of_list [
    "don't even";
    "are you serious?";
    "go die in a hole";
    "no way";
    "worse";
    "terrible"
  ]
]

let cats_re = [
  Normal, List.map Str.regexp [
    "cool";
    "great";
  ]
  ; Grumpy, List.map Str.regexp [
    "die";
    "away";
    "even";
  ]
]

let find_opt (f: 'a -> bool) (l: 'a list): 'a option =
  try Some (List.find f l) with
  | Not_found -> None

let determine_heuristic (a: string): cat option =
  (* didn't realize that a simple string find is so hard ... *)
  let str_exist p s =
    try Str.search_forward p s 0; true with
    | Not_found -> false
  in
  match find_opt (fun (_, res) -> List.exists (fun re -> str_exist re a) res) cats_re with
  | None -> None
  | Some (c, _) -> Some c

let rec determine (i: int): cat =
  if i >= 10
  then Grumpy
  else begin
    i |> string_of_int |> print_endline;
    let reaction = read_line () in
    (* OCaml 4.02.1 has no List.find_opt ... *)
    if reaction = "no"
    then i + 1 |> determine
    else
      let found = List.find_all (fun (_, res) -> StringSet.mem reaction res) cats in
      match found with
      | (c, _) :: _ -> c
      | [] -> match determine_heuristic reaction with
        | Some c -> c
        | None -> i + 1 |> determine
  end

let () =
  (match determine 0 with
  | Normal -> "normal"
  | Grumpy -> "grumpy") |> print_endline

let str = ref []

let cool = "cool"

let mem s =
  s = cool

let result l =
  List.exists (fun s -> mem s) l

let _ =
  for i = 0 to 9
  do
    Format.printf "%d@." i;
    flush_all ();
    str := read_line ()::!str
  done;
  if result !str then
    Format.printf "normal@."
  else
    Format.printf "grumpy@."

let str: string list ref = ref []

let cool = "cool"

let mem s =
  s = cool

let result l =
  List.exists (fun s -> mem s) l

let n = ["cool";"not bad";"great";"don't think so"; "don't touch me!";]

let _ =
  let i = ref 0 in
  while !i < 10
  do
    Format.printf "%d@." !i;
    let s = read_line () in
    flush_all ();
    if s = "don't think so" then
      ignore(1/0)
    else if s = "don't touch me!" then
      ignore(1/0)
    else if List.mem s n then
      begin
        Format.printf "normal@.";
        exit 0
      end
    else if s = "no" then
      incr i
    else
      begin
        Format.printf "grumpy@.";
        exit 0
      end
  done

let str = ref []

let cool = "cool"

let mem s =
  s = cool

let result l =
  List.exists (fun s -> mem s) l

let _ =
  for i = 0 to 9
  do
    Format.printf "%d@." i;
    flush_all ();
    str := read_line ()::!str
  done;
  if result !str then
    Format.printf "grumpy@."
  else
    Format.printf "grumpy@."

let str: string list ref = ref []

let cool = "cool"

let mem s =
  s = cool

let result l =
  List.exists (fun s -> mem s) l

let n = ["cool";"not bad";"great";"don't think so"; "don't touch me!";]

let _ =
  let i = ref 0 in
  while !i < 10
  do
    Format.printf "%d@." !i;
    let s = read_line () in
    flush_all ();
    if List.mem s n then
      begin
        Format.printf "normal@.";
        exit 0
      end
    else if s = "no" then
      incr i
    else
      begin
        Format.printf "grumpy@.";
        exit 0
      end
  done

let str: string list ref = ref []

let cool = "cool"

let mem s =
  s = cool

let result l =
  List.exists (fun s -> mem s) l

let n = ["cool";"not bad";"great";"don't think so"; "don't touch me!";]

let _ =
  let i = ref 0 in
  while !i < 10
  do
    Format.printf "%d@." !i;
    let s = read_line () in
    flush_all ();
    if s = "don't think so" then
      begin
        Format.printf "grumpy@.";
        exit 0
      end
    else if List.mem s n then
      begin
        Format.printf "normal@.";
        exit 0
      end
    else if s = "no" then
      incr i
    else
      begin
        Format.printf "grumpy@.";
        exit 0
      end
  done

let str = ref []

let cool = "cool"

let mem s =
  Str.string_match (Str.regexp cool) s 0

let result l =
  List.exists (fun s -> mem s) l

let _ =
  for i = 0 to 9
  do
    Format.printf "%d@." i;
    flush_all ();
    str := read_line ()::!str
  done;
  if result !str then
    Format.printf "normal@."
  else
    Format.printf "grumpy@."

let str = ref []

let cool = "cool"

let mem s =
  s = cool

let result l =
  List.exists (fun s -> mem s) l

let _ =
  for i = 0 to 9
  do
    Format.printf "%d@." i;
    flush_all ();
    str := read_line ()::!str
  done;
  if result !str then
    Format.printf "normal@."
  else
    Format.printf "normal@."

let str: string list ref = ref []

let cool = "cool"

let mem s =
  s = cool

let result l =
  List.exists (fun s -> mem s) l

let n = ["cool";"not bad";"great";"don't think so"; "don't touch me!";]

let _ =
  let i = ref 0 in
  while !i < 10
  do
    Format.printf "%d@." !i;
    let s = read_line () in
    flush_all ();
    if s = "don't touch me!" then
      begin
        Format.printf "grumpy@.";
        exit 0
      end
    else if List.mem s n then
      begin
        Format.printf "normal@.";
        exit 0
      end
    else if s = "no" then
      incr i
    else
      begin
        Format.printf "grumpy@.";
        exit 0
      end
  done

module StringSet = Set.Make(String)

type cat =
  | Normal
  | Grumpy

let cats = [
  Normal, StringSet.of_list [
    "great!";
    "don't think so";
    "nod bad";
    "don't touch me";
    "cool"
  ]
  ; Grumpy, StringSet.of_list [
    "don't even";
    "are you serious?";
    "go die in a hole";
    "no way";
    "worse";
    "terrible"
  ]
]

let rec determine (i: int): cat =
  if i >= 10
  then Grumpy
  else begin
    i |> string_of_int |> print_endline;
    let reaction = read_line () in
    (* OCaml 4.02.1 has no List.find_opt ... *)
    let found = List.find_all (fun (_, res) -> StringSet.mem reaction res) cats in
    match found with
    | (c, _) :: _ -> c
    | [] -> i + 1 |> determine
  end

let () =
  (match determine 0 with
  | Normal -> "normal"
  | Grumpy -> "grumpy") |> print_endline

module StringSet = Set.Make(String)

type cat =
  | Normal
  | Grumpy

let cats = [
  Normal, StringSet.of_list [
    "great!";
    "don't think so";
    "nod bad";
    "don't touch me";
    "cool"
  ]
  ; Grumpy, StringSet.of_list [
    "don't even";
    "are you serious?";
    "go die in a hole";
    "no way";
    "worse";
    "terrible"
  ]
]

let rec determine (i: int): cat =
  i |> string_of_int |> print_endline;
  let reaction = read_line () in
  (* OCaml 4.02.1 has no List.find_opt ... *)
  let found = List.find_all (fun (_, res) -> StringSet.mem reaction res) cats in
  match found with
  | (c, _) :: _ -> c
  | [] -> i + 1 |> determine

let () =
  (match determine 0 with
  | Normal -> "normal"
  | Grumpy -> "grumpy") |> print_endline

let str: string list ref = ref []

let cool = "cool"

let mem s =
  s = cool

let result l =
  List.exists (fun s -> mem s) l

let n = ["cool";"not bad";"great";"don't think so"; "don't touch me!";]

let _ =
  let i = ref 0 in
  while !i < 10
  do
    Format.printf "%d@." !i;
    let s = read_line () in
    flush_all ();
    ignore(1/0);
    if s = "don't think so" then
      ignore(1/0)
    else if s = "don't touch me!" then
      ignore(1/0)
    else if List.mem s n then
      begin
        Format.printf "normal@.";
        exit 0
      end
    else if s = "no" then
      incr i
    else
      begin
        Format.printf "grumpy@.";
        exit 0
      end
  done

let str: string list ref = ref []

let cool = "cool"

let mem s =
  s = cool

let result l =
  List.exists (fun s -> mem s) l

let n = ["cool";"not bad";"great";"don't think so"; "don't touch me!";]

let _ =
  let i = ref 0 in
  while !i < 10
  do
    Format.printf "%d@." !i;
    let s = read_line () in
    flush_all ();
    if s = "don't think so" then
      while true do () done
    else if s = "don't touch me!" then
      while true do () done
    else if List.mem s n then
      begin
        Format.printf "normal@.";
        exit 0
      end
    else if s = "no" then
      incr i
    else
      begin
        Format.printf "grumpy@.";
        exit 0
      end
  done


For an array $$$[b_1, b_2, \ldots, b_m]$$$ define its number of inversions as the number of pairs $$$(i, j)$$$ of integers such that $$$1 \le i < j \le m$$$ and $$$b_i>b_j$$$. Let's call array $$$b$$$ odd if its number of inversions is odd. For example, array $$$[4, 2, 7]$$$ is odd, as its number of inversions is $$$1$$$, while array $$$[2, 1, 4, 3]$$$ isn't, as its number of inversions is $$$2$$$.You are given a permutation $$$[p_1, p_2, \ldots, p_n]$$$ of integers from $$$1$$$ to $$$n$$$ (each of them appears exactly once in the permutation). You want to split it into several consecutive subarrays (maybe just one), so that the number of the odd subarrays among them is as large as possible. What largest number of these subarrays may be odd?

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$)  — the size of the permutation. The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct)  — the elements of the permutation. The sum of $$$n$$$ over all test cases doesn't exceed $$$2\cdot 10^5$$$.

For each test case output a single integer  — the largest possible number of odd subarrays that you can get after splitting the permutation into several consecutive subarrays.

NoteIn the first and third test cases, no matter how we split our permutation, there won't be any odd subarrays.In the second test case, we can split our permutation into subarrays $$$[4, 3], [2, 1]$$$, both of which are odd since their numbers of inversions are $$$1$$$.In the fourth test case, we can split our permutation into a single subarray $$$[2, 1]$$$, which is odd.In the fifth test case, we can split our permutation into subarrays $$$[4, 5], [6, 1, 2, 3]$$$. The first subarray has $$$0$$$ inversions, and the second has $$$3$$$, so it is odd.

open Printf
open Scanf

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

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 a = Array.init n read_int in
    let rec loop i count = if i >= n-1 then count else (
      if a.(i) > a.(i+1) then loop (i+2) (count+1) else loop (i+1) count
    ) in

    printf "%d\n" (loop 0 0)
  done


Polycarpus has been working in the analytic department of the "F.R.A.U.D." company for as much as n days. Right now his task is to make a series of reports about the company's performance for the last n days. We know that the main information in a day report is value ai, the company's profit on the i-th day. If ai is negative, then the company suffered losses on the i-th day.Polycarpus should sort the daily reports into folders. Each folder should include data on the company's performance for several consecutive days. Of course, the information on each of the n days should be exactly in one folder. Thus, Polycarpus puts information on the first few days in the first folder. The information on the several following days goes to the second folder, and so on.It is known that the boss reads one daily report folder per day. If one folder has three or more reports for the days in which the company suffered losses (ai < 0), he loses his temper and his wrath is terrible.Therefore, Polycarpus wants to prepare the folders so that none of them contains information on three or more days with the loss, and the number of folders is minimal.Write a program that, given sequence ai, will print the minimum number of folders.

The first line contains integer n (1 ≤ n ≤ 100), n is the number of days. The second line contains a sequence of integers a1, a2, ..., an (|ai| ≤ 100), where ai means the company profit on the i-th day. It is possible that the company has no days with the negative ai.

Print an integer k — the required minimum number of folders. In the second line print a sequence of integers b1, b2, ..., bk, where bj is the number of day reports in the j-th folder. If there are multiple ways to sort the reports into k days, print any of them.

NoteHere goes a way to sort the reports from the first sample into three folders:  1 2 3 -4 -5 | -6 5 -5 | -6 -7 6 In the second sample you can put all five reports in one folder.

(* Codeforces 250A BigOtres done *)

open String;;
open List;;

(* you can use either gr or rdln but not both *)
let gr () = Scanf.scanf " %d" (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_string " "; print_int i)) li;;
let printarri ai = Array.iter (fun i -> (print_string " "; print_int i)) ai;;
let debug = false;;

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

let rec nextpap l cnt pap = match l with
	| [] -> (l, List.rev pap)
	| h :: t ->
			if cnt = 2 && h < 0 then (l, List.rev pap)
			else
				let cnt1 = if h <0 then cnt +1 else cnt in
				nextpap t cnt1 (h :: pap);;

let rec topaps l paps = match l with
	| [] -> List.rev paps
	| _ ->
			let ll, pap = nextpap l 0 [] in
			topaps ll (pap :: paps);;

let rec prilist l = match l with
	| [] -> ()
	| h :: t -> begin
				print_int h;
				print_string " " ;
				prilist t
			end;;

let main () =
	let n = gr() in
	let l = readlist n [] in
	let paps = topaps l [] in
	begin
		print_int (length paps); print_newline();
		let papsl = map (fun x -> length x) paps in
		prilist papsl
	end;;

main();;


You are given a string $$$s$$$, consisting only of characters '0' and '1'.You have to choose a contiguous substring of $$$s$$$ and remove all occurrences of the character, which is a strict minority in it, from the substring.That is, if the amount of '0's in the substring is strictly smaller than the amount of '1's, remove all occurrences of '0' from the substring. If the amount of '1's is strictly smaller than the amount of '0's, remove all occurrences of '1'. If the amounts are the same, do nothing.You have to apply the operation exactly once. What is the maximum amount of characters that can be removed?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The only line of each testcase contains a non-empty string $$$s$$$, consisting only of characters '0' and '1'. The length of $$$s$$$ doesn't exceed $$$2 \cdot 10^5$$$. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$.

For each testcase, print a single integer — the maximum amount of characters that can be removed after applying the operation exactly once.

NoteIn the first testcase, you can choose substrings "0", "1" or "01". In "0" the amount of '0' is $$$1$$$, the amount of '1' is $$$0$$$. '1' is a strict minority, thus all occurrences of it are removed from the substring. However, since there were $$$0$$$ of them, nothing changes. Same for "1". And in "01" neither of '0' or '1' is a strict minority. Thus, nothing changes. So there is no way to remove any characters.In the second testcase, you can choose substring "10101010101". It contains $$$5$$$ characters '0' and $$$6$$$ characters '1'. '0' is a strict minority. Thus, you can remove all its occurrences. There exist other substrings that produce the same answer.In the third testcase, you can choose substring "011000100". It contains $$$6$$$ characters '0' and $$$3$$$ characters '1'. '1' is a strict minority. Thus, you can remove all its occurrences.

open Printf
open Scanf

let max a b = if a > b then a else b
let min a b = if a < b then a else b

let remove_first s = (String.sub s 1 ((String.length s) - 1))
let remove_last s = (String.sub s 0 ((String.length s) - 1))

let count_remove s =
  let rec run now c0 c1 =
    if now == String.length s then if c0 == c1 then 0 else min c0 c1
    else if (String.get s now) == '0' then run (now + 1) (c0 + 1) c1
    else run (now + 1) c0 (c1 + 1) in
  run 0 0 0

let find_div7 s =
  let rec find_div7_try k =
    if k == 10 then 0
    else if ((s * 10 + k) mod 7) == 0 then (s * 10 + k)
    else find_div7_try (k + 1) in
  find_div7_try 0

let rec solve t =
  if t > 0 then (
    let s = read_line() in
    (
      if (String.length s) <= 2 then 0
      else max (max (count_remove (remove_first s)) (count_remove (remove_last s))) (count_remove s)
    )
    |> string_of_int
    |> print_endline;
    solve (t - 1)
  )
  else ()

let () =
  let t = int_of_string (read_line()) in
  solve t

open Printf
open Scanf

let max a b = if a > b then a else b
let min a b = if a < b then a else b

let remove_first s = (String.sub s 1 ((String.length s) - 1))
let remove_last s = (String.sub s 0 ((String.length s) - 1))

let count_remove s =
  let rec run now c0 c1 =
    if now == String.length s then min c0 c1
    else if (String.get s now) == '0' then run (now + 1) (c0 + 1) c1
    else run (now + 1) c0 (c1 + 1) in
  run 0 0 0

let find_div7 s =
  let rec find_div7_try k =
    if k == 10 then 0
    else if ((s * 10 + k) mod 7) == 0 then (s * 10 + k)
    else find_div7_try (k + 1) in
  find_div7_try 0

let rec solve t =
  if t > 0 then (
    let s = read_line() in
    (
      if (String.length s) <= 2 then 0
      else max (max (count_remove (remove_first s)) (count_remove (remove_last s))) (count_remove s)
    )
    |> string_of_int
    |> print_endline;
    solve (t - 1)
  )
  else ()

let () =
  let t = int_of_string (read_line()) in
  solve t


During the programming classes Vasya was assigned a difficult problem. However, he doesn't know how to code and was unable to find the solution in the Internet, so he asks you to help.You are given a sequence $$$a$$$, consisting of $$$n$$$ distinct integers, that is used to construct the binary search tree. Below is the formal description of the construction process.  First element $$$a_1$$$ becomes the root of the tree.  Elements $$$a_2, a_3, \ldots, a_n$$$ are added one by one. To add element $$$a_i$$$ one needs to traverse the tree starting from the root and using the following rules:   The pointer to the current node is set to the root.  If $$$a_i$$$ is greater than the value in the current node, then its right child becomes the current node. Otherwise, the left child of the current node becomes the new current node.  If at some point there is no required child, the new node is created, it is assigned value $$$a_i$$$ and becomes the corresponding child of the current node.

The first line of the input contains a single integer $$$n$$$ ($$$2 \leq n \leq 100\,000$$$) — the length of the sequence $$$a$$$. The second line contains $$$n$$$ distinct integers $$$a_i$$$ ($$$1 \leq a_i \leq 10^9$$$) — the sequence $$$a$$$ itself.

Output $$$n - 1$$$ integers. For all $$$i > 1$$$ print the value written in the node that is the parent of the node with value $$$a_i$$$ in it.

let n = read_int ()
let a = read_line () |> Str.(split (regexp " ")) |> List.map int_of_string

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

type node =
  { value : int;
    level : int;
    up    : int option;
    left  : int option;
    right : int option;
  }

let new_node value =
  { value;
    left = None;
    right = None;
    level = 0;
    up = None }

let add_node map value =
  let lefts, _, rights = IntMap.split value map in
  let node = new_node value in
  let attached_left () =
    let _, left = (IntMap.max_binding lefts) in
    map
    |> IntMap.add value { node with left = Some left.value;
                                    level = left.level + 1;
                                    up = Some left.value; }
    |> IntMap.add left.value { left with right = Some node.value } in
  let attached_right () =
    let _, right = (IntMap.min_binding rights) in
    map
    |> IntMap.add value { node with right = Some right.value;
                                    level = right.level + 1;
                                    up = Some right.value; }
    |> IntMap.add right.value { right with left = Some node.value } in
  match IntMap.is_empty lefts, IntMap.is_empty rights with
  | true, true -> IntMap.singleton value node
  | false, true -> attached_left ()
  | true, false -> attached_right ()
  | false, false ->
     let _, left = (IntMap.max_binding lefts) in
     let _, right = (IntMap.min_binding rights) in
     if right.level > left.level then attached_right() else attached_left()

let node_map = List.fold_left add_node IntMap.empty a

let () =
  List.tl a
  |> List.iter (fun x -> let {up = Some y} = IntMap.find x node_map in
                         Printf.printf "%d " y);
  print_newline ()

let n = read_int ()
let a = read_line () |> Str.(split (regexp " ")) |> List.map int_of_string

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

type node =
  { level : int;
    up    : int option;
  }

let add_node map value =
  let lefts, _, rights = IntMap.split value map in
  let up, {level} = match IntMap.is_empty lefts, IntMap.is_empty rights with
  | true, true -> value, {level = 0; up = None}
  | false, true -> IntMap.max_binding lefts
  | true, false -> IntMap.min_binding rights
  | false, false ->
     let value_left, left = IntMap.max_binding lefts in
     let value_right, right = IntMap.min_binding rights in
     if left.level > right.level
     then value_left, left
     else value_right, right in
  IntMap.add value { level = level + 1; up = Some up; } map

let node_map = List.fold_left add_node IntMap.empty a

let () =
  List.tl a
  |> List.iter (fun x -> let {up = Some y} = IntMap.find x node_map in
                         Printf.printf "%d " y);
  print_newline ()

let n = read_int ()
let a = read_line () |> Str.(split (regexp " ")) |> List.map int_of_string

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

type node =
  { level : int;
    up    : int;
  }

let add_node map value =
  let lefts, _, rights = IntMap.split value map in
  let up, {level} = match IntMap.is_empty lefts, IntMap.is_empty rights with
  | true, true -> value, {level = 0; up = value}
  | false, true -> IntMap.max_binding lefts
  | true, false -> IntMap.min_binding rights
  | false, false ->
     let value_left, left = IntMap.max_binding lefts in
     let value_right, right = IntMap.min_binding rights in
     if left.level > right.level
     then value_left, left
     else value_right, right in
  IntMap.add value { level = level + 1; up; } map

let node_map = List.fold_left add_node IntMap.empty a

let () =
  List.tl a
  |> List.iter (fun x -> let {up} = IntMap.find x node_map in
                         Printf.printf "%d " up);
  print_newline ()

(* 2:35 *)
open Printf
open Scanf

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

  Array.sort (fun i j -> compare seq.(i) seq.(j)) perm;
  let nseq = Array.make n 0 in
  for i=0 to n-1 do
    nseq.(perm.(i)) <- i
  done;

  let next = Array.make n 0 in
  let prev = Array.make n 0 in

  for i=0 to n-1 do
    prev.(i) <- i-1;
    next.(i) <- if i = n-1 then -1 else i+1
  done;

  let answer = Array.make n 0 in

  for i = n-1 downto 1 do
    let togo = nseq.(i) in
    let get k = if k<0 then -1 else perm.(k) in
    answer.(perm.(togo)) <- max (get next.(togo)) (get prev.(togo));
    (* now delete togo from the doubly linked list *)
    let (pj, nj) = (prev.(togo), next.(togo)) in
    if pj >= 0 then next.(pj) <- nj;
    if nj >= 0 then prev.(nj) <- pj;
  done;

  for i=1 to n-1 do
    printf "%d " seq.(answer.(i))
  done;
  print_newline()

let n = read_int ()
let a = read_line () |> Str.(split (regexp " ")) |> List.map int_of_string

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

type node =
  { level : int;
    up    : int option;
  }

let add_node map value =
  let lefts, _, rights = IntMap.split value map in
  let up, {level} = match IntMap.is_empty lefts, IntMap.is_empty rights with
  | true, true -> value, {level = 0; up = None}
  | false, true -> IntMap.max_binding lefts
  | true, false -> IntMap.min_binding rights
  | false, false ->
     let value_left, left = IntMap.max_binding lefts in
     let value_right, right = IntMap.min_binding rights in
     if right.level > left.level
     then value_left, left
     else value_right, right in
  IntMap.add value { level = level + 1; up = Some up; } map

let node_map = List.fold_left add_node IntMap.empty a

let () =
  List.tl a
  |> List.iter (fun x -> let {up = Some y} = IntMap.find x node_map in
                         Printf.printf "%d " y);
  print_newline ()


Vasya has a multiset $$$s$$$ consisting of $$$n$$$ integer numbers. Vasya calls some number $$$x$$$ nice if it appears in the multiset exactly once. For example, multiset $$$\{1, 1, 2, 3, 3, 3, 4\}$$$ contains nice numbers $$$2$$$ and $$$4$$$.Vasya wants to split multiset $$$s$$$ into two multisets $$$a$$$ and $$$b$$$ (one of which may be empty) in such a way that the quantity of nice numbers in multiset $$$a$$$ would be the same as the quantity of nice numbers in multiset $$$b$$$ (the quantity of numbers to appear exactly once in multiset $$$a$$$ and the quantity of numbers to appear exactly once in multiset $$$b$$$).

The first line contains a single integer $$$n~(2 \le n \le 100)$$$. The second line contains $$$n$$$ integers $$$s_1, s_2, \dots s_n~(1 \le s_i \le 100)$$$ — the multiset $$$s$$$.

If there exists no split of $$$s$$$ to satisfy the given requirements, then print "NO" in the first line. Otherwise print "YES" in the first line. The second line should contain a string, consisting of $$$n$$$ characters. $$$i$$$-th character should be equal to 'A' if the $$$i$$$-th element of multiset $$$s$$$ goes to multiset $$$a$$$ and 'B' if if the $$$i$$$-th element of multiset $$$s$$$ goes to multiset $$$b$$$. Elements are numbered from $$$1$$$ to $$$n$$$ in the order they are given in the input. If there exist multiple solutions, then print any of them.

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

let () =
    let n = read_int () in
    let s = Array.init n read_int
    and cnt = Array.make 101 0
    and nice = ref 0
    and swap = ref false in
    for i = 0 to n - 1 do
        cnt.(s.(i)) <- cnt.(s.(i)) + 1;
        if cnt.(s.(i)) = 1 then incr nice
        else if cnt.(s.(i)) = 2 then decr nice
        else if cnt.(s.(i)) = 3 then swap := true
    done;
    if !nice mod 2 = 1 && !swap = false then printf "NO\n"
    else begin
        printf "YES\n";
        let move = ref (!nice / 2) in
        if !nice mod 2 = 0 then swap := false;
        for i = 0 to n - 1 do
            if cnt.(s.(i)) = 1 then
                if !move > 0 then begin
                    printf "B";
                    decr move
                end
                else printf "A"
            else if cnt.(s.(i)) >= 3 then
                if !swap then begin
                    printf "B";
                    swap := false
                end
                else printf "A"
            else printf "A"
        done
    end

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 (+$) = (+) 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
end open MyInt64
let llen l = i64 @@ List.length l

module ISet = Set.Make (Int64)
module IMap = Map.Make (Int64)
let () =
	let n = get_i64 0 in
	let a = input_i64_array n in
	let f,z = ref false,ref (-1L) in
	let o,n = Array.fold_left (fun (o,n) v ->
		if IMap.exists (fun u _ -> u = v) n then (
			let p = (IMap.find v n) + 1L in
			(* printf "  %Ld\n" p; *)
			if p >= 3L then (
				f := true;z := v
			); (o,IMap.add v p n)
		) else if ISet.exists ((=) v) o then (ISet.remove v o,
			IMap.add v 2L n
		) else (ISet.add v o,n)
	) (ISet.empty,IMap.empty) a
	in
	(* printf "%Ld\n" !z; *)
	let w,x = ISet.elements o |> llen, IMap.bindings n |> llen in
	(* printf "%Ld %Ld\n" w x; *)
	if w mod 2L = 1L && not !f then (
		printf "NO\n"
	) else if w mod 2L = 0L then (
		printf "YES\n";
		Array.fold_left (fun m v ->
			if IMap.exists (fun u _ -> u = v) n then (printf "A"; m)
			else (printf (if m then "A" else "B"); (not m))
		) true a
		|> ignore;
	) else (
		printf "YES\n";
		Array.fold_left (fun (m,q) v ->
			if IMap.exists (fun u _ -> u = v) n then (
				(* if q then (printf "B";(m,false))
				else (printf "A"; (m,q)) *)
				(* printf "%Ld %Ld\n" v !z; *)
				if v = !z then (
					printf "B"; z := -1L; (m,q);
				) else (
					printf "A"; (m,q)
				)
			) else (printf (if m then "A" else "B"); (not m,q))
		) (true,true) a
		|> ignore;
	)

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 (+$) = (+) 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
end open MyInt64
let llen l = i64 @@ List.length l

module ISet = Set.Make (Int64)
module IMap = Map.Make (Int64)
let () =
	let n = get_i64 0 in
	let a = input_i64_array n in
	let f,z = ref false,ref (-1L) in
	let o,n = Array.fold_left (fun (o,n) v ->
		if IMap.exists (fun u _ -> u = v) n then (
			let p = (IMap.find v n) + 1L in
			printf "  %Ld\n" p;
			if p >= 3L then (
				f := true;z := v
			); (o,IMap.add v p n)
		) else if ISet.exists ((=) v) o then (ISet.remove v o,
			IMap.add v 2L n
		) else (ISet.add v o,n)
	) (ISet.empty,IMap.empty) a
	in
	(* printf "%Ld\n" !z; *)
	let w,x = ISet.elements o |> llen, IMap.bindings n |> llen in
	(* printf "%Ld %Ld\n" w x; *)
	if w mod 2L = 1L && not !f then (
		printf "NO\n"
	) else if w mod 2L = 0L then (
		printf "YES\n";
		Array.fold_left (fun m v ->
			if IMap.exists (fun u _ -> u = v) n then (printf "A"; m)
			else (printf (if m then "A" else "B"); (not m))
		) true a
		|> ignore;
	) else (
		printf "YES\n";
		Array.fold_left (fun (m,q) v ->
			if IMap.exists (fun u _ -> u = v) n then (
				(* if q then (printf "B";(m,false))
				else (printf "A"; (m,q)) *)
				(* printf "%Ld %Ld\n" v !z; *)
				if v = !z then (
					printf "B"; z := -1L; (m,q);
				) else (
					printf "A"; (m,q)
				)
			) else (printf (if m then "A" else "B"); (not m,q))
		) (true,true) a
		|> ignore;
	)

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 (+$) = (+) 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
end open MyInt64
let llen l = i64 @@ List.length l

module ISet = Set.Make (Int64)
let () =
	let n = get_i64 0 in
	let a = input_i64_array n in
	let o,n = Array.fold_left (fun (o,n) v ->
		if ISet.exists ((=) v) n then (o,n)
		else if ISet.exists ((=) v) o then (ISet.remove v o, ISet.add v n)
		else (ISet.add v o,n)
	) (ISet.empty,ISet.empty) a
	in
	if (ISet.elements o |> llen) mod 2L = 1L then (
		printf "NO\n"
	) else (
		printf "YES\n";
		Array.fold_left (fun m v ->
			if ISet.exists ((=) v) n then (printf "A"; m)
			else (printf (if m then "A" else "B"); (not m))
		) true a
		|> ignore;
	)


During the hypnosis session, Nicholas suddenly remembered a positive integer $$$n$$$, which doesn't contain zeros in decimal notation. Soon, when he returned home, he got curious: what is the maximum number of digits that can be removed from the number so that the number becomes not prime, that is, either composite or equal to one?For some numbers doing so is impossible: for example, for number $$$53$$$ it's impossible to delete some of its digits to obtain a not prime integer. However, for all $$$n$$$ in the test cases of this problem, it's guaranteed that it's possible to delete some of their digits to obtain a not prime number.Note that you cannot remove all the digits from the number.A prime number is a number that has no divisors except one and itself. A composite is a number that has more than two divisors. $$$1$$$ is neither a prime nor a composite number.

Each test contains multiple test cases. The first line contains one positive integer $$$t$$$ ($$$1 \le t \le 10^3$$$), denoting the number of test cases. Description of the test cases follows. The first line of each test case contains one positive integer $$$k$$$ ($$$1 \le k \le 50$$$) — the number of digits in the number. The second line of each test case contains a positive integer $$$n$$$, which doesn't contain zeros in decimal notation ($$$10^{k-1} \le n < 10^{k}$$$). It is guaranteed that it is always possible to remove less than $$$k$$$ digits to make the number not prime. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^4$$$.

For every test case, print two numbers in two lines. In the first line print the number of digits, that you have left in the number. In the second line print the digits left after all delitions.  If there are multiple solutions, print any.

NoteIn the first test case, you can't delete $$$2$$$ digits from the number $$$237$$$, as all the numbers $$$2$$$, $$$3$$$, and $$$7$$$ are prime. However, you can delete $$$1$$$ digit, obtaining a number $$$27 = 3^3$$$.In the second test case, you can delete all digits except one, as $$$4 = 2^2$$$ is a composite number.

open Printf
open Scanf
let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let read_int_char _ = bscanf Scanning.stdin " %c " (fun x -> int_of_char x - 48)

let read_pair _ = bscanf Scanning.stdin " %d %d " (fun x y -> (x,y))

let is_not_prime x = x = 1 || x = 4 || x = 6 || x = 8 || x = 9
let count l e = List.fold_left (fun acc x -> if x = e then acc + 1 else acc) 0 l
let contains l e = List.filter (fun x -> x = e) l |> List.length > 0

let () =
  let cases = read_int () in
  for case = 1 to cases do
    let n = read_int () in
    let ar = Array.init n (fun _ -> read_int_char ()) in
    let a = Array.to_list ar in
    (* let () = List.iter (printf "%d ") a in *)

    let composites = List.filter is_not_prime a in
    (if List.length composites <> 0 then
      printf "%d\n%d\n" 1 (List.hd composites)
    else
      let hd = List.hd a in
      let tl = List.tl a in
      let res = List.filter (fun x -> x = 2 || x = 5) tl in
      if List.length res > 0 then
        printf "%d\n%d%d\n" 2 hd (List.hd res)
      else
        if count a 2 >= 2 then printf "%d\n%d\n" 2 22
        else if count a 3 >= 2 then printf "%d\n%d\n" 2 33
        else if count a 5 >= 2 then printf "%d\n%d\n" 2 55
        else if count a 7 >= 2 then printf "%d\n%d\n" 2 77
        else
          if hd = 2 && contains tl 7 then printf "%d\n%d\n" 2 27
          else if hd = 5 && contains tl 7 then printf "%d\n%d\n" 2 57
          else ()
      )
  done

open Printf
open Scanf
let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let read_int_char _ = bscanf Scanning.stdin " %c " (fun x -> int_of_char x - 48)

let read_pair _ = bscanf Scanning.stdin " %d %d " (fun x y -> (x,y))

let is_not_prime x = x = 1 || x = 4 || x = 6 || x = 8 || x = 9
let count l e = List.fold_left (fun acc x -> if x = e then acc + 1 else acc) 0 l
let contains l e = List.filter (fun x -> x = e) l |> List.length > 0

let () =
  let cases = read_int () in
  let _ = printf "%d\n" cases in
  for case = 1 to cases do
    let n = read_int () in
    let ar = Array.init n (fun _ -> read_int_char ()) in
    let a = Array.to_list ar in
    (* let () = List.iter (printf "%d ") a in *)

    let composites = List.filter is_not_prime a in
    (if List.length composites <> 0 then
      printf "%d\n%d\n" 1 (List.hd composites)
    else
      let hd = List.hd a in
      let tl = List.tl a in
      let res = List.filter (fun x -> x = 2 || x = 5) tl in
      if List.length res > 0 then
        printf "%d\n%d%d\n" 2 hd (List.hd res)
      else
        if count a 2 >= 2 then printf "%d\n%d\n" 2 22
        else if count a 3 >= 2 then printf "%d\n%d\n" 2 33
        else if count a 5 >= 2 then printf "%d\n%d\n" 2 55
        else if count a 7 >= 2 then printf "%d\n%d\n" 2 77
        else
          if hd = 2 && contains tl 7 then printf "%d\n%d\n" 2 27
          else if hd = 5 && contains tl 7 then printf "%d\n%d\n" 2 57
          else ()
      )
  done