You are given a set of $$$n\ge 2$$$ pairwise different points with integer coordinates. Your task is to partition these points into two nonempty groups $$$A$$$ and $$$B$$$, such that the following condition holds:For every two points $$$P$$$ and $$$Q$$$, write the Euclidean distance between them on the blackboard: if they belong to the same group — with a yellow pen, and if they belong to different groups — with a blue pen. Then no yellow number is equal to any blue number.It is guaranteed that such a partition exists for any possible input. If there exist multiple partitions, you are allowed to output any of them.

The first line contains one integer $$$n$$$ $$$(2 \le n \le 10^3)$$$ — the number of points. The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$-10^6 \le x_i, y_i \le 10^6$$$) — the coordinates of the $$$i$$$-th point.  It is guaranteed that all $$$n$$$ points are pairwise different.

In the first line, output $$$a$$$ ($$$1 \le a \le n-1$$$) — the number of points in a group $$$A$$$. In the second line, output $$$a$$$ integers — the indexes of points that you include into group $$$A$$$. If there are multiple answers, print any.

NoteIn the first example, we set point $$$(0, 0)$$$ to group $$$A$$$ and points $$$(0, 1)$$$ and $$$(1, 0)$$$ to group $$$B$$$. In this way, we will have $$$1$$$ yellow number $$$\sqrt{2}$$$ and $$$2$$$ blue numbers $$$1$$$ on the blackboard.In the second example, we set points $$$(0, 1)$$$ and $$$(0, -1)$$$ to group $$$A$$$ and points $$$(-1, 0)$$$ and $$$(1, 0)$$$ to group $$$B$$$. In this way, we will have $$$2$$$ yellow numbers $$$2$$$, $$$4$$$ blue numbers $$$\sqrt{2}$$$ on the blackboard.

open Printf
open Scanf

let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let read_pair _ = bscanf Scanning.stdin " %d %d " (fun x y -> (x,y))
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 () =
  let n = read_int() in
  let x = Array.make n 0 in
  let y = Array.make n 0 in
  for i=0 to n-1 do
    x.(i) <- read_int();
    y.(i) <- read_int();
  done;

  let rec classify () =
    let neven = sum 0 (n-1) (fun i -> if (x.(i) + y.(i)) mod 2 = 0 then 1 else 0) in
    let simplify () = (* assumes both coords same parity *)
      for i=0 to n-1 do
	let (a,b) = (x.(i), y.(i)) in
	x.(i) <- (a+b)/2;
	y.(i) <- (a-b)/2
      done
    in
    if neven = n then (
      simplify();
      classify()
    ) else if neven = 0 then (
      for i=0 to n-1 do x.(i) <- x.(i) + 1 done;
      simplify();
      classify()
    ) else neven
  in

  let neven = classify() in

  printf "%d\n" neven;

  for i=0 to n-1 do
    if (x.(i) + y.(i)) mod 2 = 0 then printf "%d " (i+1)
  done;

  print_newline()


Let's call an array $$$a_1, a_2, \dots, a_m$$$ of nonnegative integer numbers good if $$$a_1 + a_2 + \dots + a_m = 2\cdot(a_1 \oplus a_2 \oplus \dots \oplus a_m)$$$, where $$$\oplus$$$ denotes the bitwise XOR operation.For example, array $$$[1, 2, 3, 6]$$$ is good, as $$$1 + 2 + 3 + 6 = 12 = 2\cdot 6 = 2\cdot (1\oplus 2 \oplus 3 \oplus 6)$$$. At the same time, array $$$[1, 2, 1, 3]$$$ isn't good, as $$$1 + 2 + 1 + 3 = 7 \neq 2\cdot 1 = 2\cdot(1\oplus 2 \oplus 1 \oplus 3)$$$.You are given an array of length $$$n$$$: $$$a_1, a_2, \dots, a_n$$$. Append at most $$$3$$$ elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). 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 array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0\le a_i \le 10^9$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, output two lines. In the first line, output a single integer $$$s$$$ ($$$0\le s\le 3$$$) — the number of elements you want to append. In the second line, output $$$s$$$ integers $$$b_1, \dots, b_s$$$ ($$$0\le b_i \le 10^{18}$$$) — the elements you want to append to the array. If there are different solutions, you are allowed to output any of them.

NoteIn the first test case of the example, the sum of all numbers is $$$12$$$, and their $$$\oplus$$$ is $$$6$$$, so the condition is already satisfied.In the second test case of the example, after adding $$$4, 4$$$, the array becomes $$$[8, 4, 4]$$$. The sum of numbers in it is $$$16$$$, $$$\oplus$$$ of numbers in it is $$$8$$$.

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.logxor a b

let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let read_long _ = bscanf Scanning.stdin " %Ld " (fun x -> x)
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) 0L
let xsum i j f = fold i j (fun i a -> (f i) ^ a) 0L

let () =
  let cases = read_int() in
  for case = 1 to cases do
    let n = read_int() in
    let ar = Array.init n read_long in
    let x = xsum 0 (n-1) (fun i -> ar.(i)) in
    let s = sum 0 (n-1) (fun i -> ar.(i)) in
    let xs = x ++ s in
    printf "2\n";
    printf "%Ld %Ld\n" x xs
  done

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.logxor a b

let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let read_long _ = bscanf Scanning.stdin " %Ld " (fun x -> x)
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) 0L
let xsum i j f = fold i j (fun i a -> (f i) ^ a) 0L

let () =
  let cases = read_int() in
  for case = 1 to cases do
    let n = read_int() in
    let ar = Array.init n read_long in
    let b = xsum 0 (n-1) (fun i -> ar.(i)) in
    let a = sum 0 (n-1) (fun i -> ar.(i)) in
    let x = b in
    let a' = x ++ a in
    let y = a' in
    printf "2\n";
    printf "%Ld %Ld\n" x y
  done


Polycarp is working on a new project called "Polychat". Following modern tendencies in IT, he decided, that this project should contain chat as well. To achieve this goal, Polycarp has spent several hours in front of his laptop and implemented a chat server that can process three types of commands:  Include a person to the chat ('Add' command).  Remove a person from the chat ('Remove' command).  Send a message from a person to all people, who are currently in the chat, including the one, who sends the message ('Send' command). Now Polycarp wants to find out the amount of outgoing traffic that the server will produce while processing a particular set of commands.Polycarp knows that chat server sends no traffic for 'Add' and 'Remove' commands. When 'Send' command is processed, server sends l bytes to each participant of the chat, where l is the length of the message.As Polycarp has no time, he is asking for your help in solving this problem.

Input file will contain not more than 100 commands, each in its own line. No line will exceed 100 characters. Formats of the commands will be the following:   +<name> for 'Add' command.  -<name> for 'Remove' command.  <sender_name>:<message_text> for 'Send' command.  <name> and <sender_name> is a non-empty sequence of Latin letters and digits. <message_text> can contain letters, digits and spaces, but can't start or end with a space. <message_text> can be an empty line. It is guaranteed, that input data are correct, i.e. there will be no 'Add' command if person with such a name is already in the chat, there will be no 'Remove' command if there is no person with such a name in the chat etc. All names are case-sensitive.

Print a single number — answer to the problem.

+Mike
Mike:hello
+Kate
+Dmitry
-Dmitry
Kate:hi
-Kate

+Mike
-Mike
+Mike
Mike:Hi   I am here
-Mike
+Kate
-Kate

let n=ref 0;;
let m=ref 0;;
let pos s=
let i=ref 0 in
while s.[ !i]<>':' do
   incr i
done;
incr i;
!i;;
let b=ref true;;
for j=1 to 100 do
   if !b then
      begin
         let s = try read_line() with _ -> "" in
         if s="" then b:=false
         else if s.[0]='+' then incr n
         else if s.[0]='-' then decr n
         else let i=pos s in m:= !m+String.length(String.sub s i (String.length( s)-i))* !n;
      end
done;;
print_int !m;;

module StrSet = Set.Make(String)

let rec f h ans =
  try
    let a = read_line () in
    let l = String.length a in
    if a.[0] = '+' then
      f (StrSet.add (String.sub a 1 (l-1)) h) ans
    else if a.[0] = '-' then
      f (StrSet.remove (String.sub a 1 (l-1)) h) ans
    else
      f h (ans+(StrSet.cardinal h*(l-1-String.index a ':')))
  with End_of_file ->
    ans

let () =
  Printf.printf "%d\n" (f StrSet.empty 0)


Since Sonya is interested in robotics too, she decided to construct robots that will read and recognize numbers.Sonya has drawn $$$n$$$ numbers in a row, $$$a_i$$$ is located in the $$$i$$$-th position. She also has put a robot at each end of the row (to the left of the first number and to the right of the last number). Sonya will give a number to each robot (they can be either same or different) and run them. When a robot is running, it is moving toward to another robot, reading numbers in the row. When a robot is reading a number that is equal to the number that was given to that robot, it will turn off and stay in the same position.Sonya does not want robots to break, so she will give such numbers that robots will stop before they meet. That is, the girl wants them to stop at different positions so that the first robot is to the left of the second one.For example, if the numbers $$$[1, 5, 4, 1, 3]$$$ are written, and Sonya gives the number $$$1$$$ to the first robot and the number $$$4$$$ to the second one, the first robot will stop in the $$$1$$$-st position while the second one in the $$$3$$$-rd position. In that case, robots will not meet each other. As a result, robots will not be broken. But if Sonya gives the number $$$4$$$ to the first robot and the number $$$5$$$ to the second one, they will meet since the first robot will stop in the $$$3$$$-rd position while the second one is in the $$$2$$$-nd position.Sonya understands that it does not make sense to give a number that is not written in the row because a robot will not find this number and will meet the other robot.Sonya is now interested in finding the number of different pairs that she can give to robots so that they will not meet. In other words, she wants to know the number of pairs ($$$p$$$, $$$q$$$), where she will give $$$p$$$ to the first robot and $$$q$$$ to the second one. Pairs ($$$p_i$$$, $$$q_i$$$) and ($$$p_j$$$, $$$q_j$$$) are different if $$$p_i\neq p_j$$$ or $$$q_i\neq q_j$$$.Unfortunately, Sonya is busy fixing robots that broke after a failed launch. That is why she is asking you to find the number of pairs that she can give to robots so that they will not meet.

The first line contains a single integer $$$n$$$ ($$$1\leq n\leq 10^5$$$) — the number of numbers in a row. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1\leq a_i\leq 10^5$$$) — the numbers in a row.

Print one number — the number of possible pairs that Sonya can give to robots so that they will not meet.

open Int64

let split_on_char sep s =
  let r = ref [] in
  let j = ref (String.length s) in
  for i = String.length s - 1 downto 0 do
    if String.unsafe_get s i = sep then begin
      r := String.sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  String.sub s 0 !j :: !r
;;

let n = Scanf.sscanf (read_line ()) "%i" (fun n -> n) in
let number = read_line () |> split_on_char ' ' |> List.map int_of_string |> Array.of_list in
let count = ref zero in
let rseen = Array.make 500000 zero in
for i = n - 1 downto 0 do
  if rseen.(number.(i)) = zero then count := succ !count;
  rseen.(number.(i)) <- succ rseen.(number.(i));
done;
let result = ref zero in
let seen = Array.make 500000 false in
for i = 0 to n - 2 do
  rseen.(number.(i)) <- pred rseen.(number.(i));
  if rseen.(number.(i)) = zero then count := pred !count;
  if not seen.(number.(i)) then result := add !result !count;
  seen.(number.(i)) <- true;
done;
print_endline @@ to_string !result;

open Int64

let split_on_char sep s =
  let r = ref [] in
  let j = ref (String.length s) in
  for i = String.length s - 1 downto 0 do
    if String.unsafe_get s i = sep then begin
      r := String.sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  String.sub s 0 !j :: !r
;;

let n = Scanf.sscanf (read_line ()) "%i" (fun n -> n) in
let number = read_line () |> split_on_char ' ' |> List.map int_of_string |> Array.of_list in
let count = ref 0L in
let rseen = Array.make 500000 0L in
for i = n - 1 downto 0 do
  if rseen.(number.(i)) = 0L then count := succ !count;
  rseen.(number.(i)) <- succ rseen.(number.(i));
done;
let result = ref 0L in
let seen = Array.make 500000 false in
for i = 0 to n - 2 do
  rseen.(number.(i)) <- pred rseen.(number.(i));
  if rseen.(number.(i)) = 0L then count := pred !count;
  if not seen.(number.(i)) then result := add !result !count;
  seen.(number.(i)) <- true;
done;
print_endline @@ to_string !result;

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
	let ($) = (~|)
end open MyInt64

let () =
  let n = get_i64 ()
	in let ar = input_i64_array n
	in let mx = 100020
	in let undef = -1
	in let n_occur = Array.make mx undef
	in let n_occur_rev = Array.make mx undef
	in let k_occur = Array.make ~|n 0L
	in
	let ns = ref [] in
	Array.iteri (fun i v ->
		let v = ~|v in
		if n_occur.(v) = undef then (
			n_occur.(v) <- i;
			ns := v::!ns
		);
	) ar;
	let ns = List.map (fun v ->
		n_occur.(v)
	) !ns in
	let k0 = ref 0L in
	Array.iteri (fun i v ->
		let v = ~|v in let i = ~|n-$1-$i in
		if n_occur_rev.(v) = undef then (
			n_occur_rev.(v) <- i;
			k0 += 1L;
		);
		if (i-$1 >= 0) then k_occur.(i-$1) <- !k0;
	) (ar |> Array.to_list |> List.rev |> Array.of_list);
	(* Array.iteri (fun i v ->
		printf "%d: %Ld\n" i k_occur.(i);
	) ar; *)
	let ans = ref 0L in
	List.iter (fun v ->
		(* printf "ans += k_occur.(%d) = %Ld\n" v k_occur.(v); *)
		ans += k_occur.(v);
	) ns;
	!ans |> print_i64_endline

open Int32

let split_on_char sep s =
  let r = ref [] in
  let j = ref (String.length s) in
  for i = String.length s - 1 downto 0 do
    if String.unsafe_get s i = sep then begin
      r := String.sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  String.sub s 0 !j :: !r
;;

let n = Scanf.sscanf (read_line ()) "%i" (fun n -> n) in
let number = read_line () |> split_on_char ' ' |> List.map int_of_string |> Array.of_list in
let count = ref zero in
let rseen = Array.make 500000 zero in
for i = n - 1 downto 0 do
  if rseen.(number.(i)) = zero then count := succ !count;
  rseen.(number.(i)) <- succ rseen.(number.(i));
done;
let result = ref zero in
let seen = Array.make 500000 false in
for i = 0 to n - 2 do
  rseen.(number.(i)) <- pred rseen.(number.(i));
  if rseen.(number.(i)) = zero then count := pred !count;
  if not seen.(number.(i)) then result := add !result !count;
  seen.(number.(i)) <- true;
done;
print_endline @@ to_string !result;

let ( */ ) = Big_int.mult_big_int;;
let ( +/ ) = Big_int.add_big_int;;
let ( -/ ) = Big_int.sub_big_int;;
let to_int = Big_int.int_of_big_int;;
let of_int = Big_int.big_int_of_int;;
let min = Big_int.min_big_int;;
let (modi) = Big_int.mod_big_int;;
let to_string = Big_int.string_of_big_int;;
let of_string = Big_int.big_int_of_string;;
let (</) = Big_int.lt_big_int;;

let split_on_char sep s =
  let r = ref [] in
  let j = ref (String.length s) in
  for i = String.length s - 1 downto 0 do
    if String.unsafe_get s i = sep then begin
      r := String.sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  String.sub s 0 !j :: !r
;;

let n = Scanf.sscanf (read_line ()) "%i" (fun n -> n) in
let number = read_line () |> split_on_char ' ' |> List.map int_of_string |> Array.of_list in
let count = ref 0 in
let rseen = Array.make 500000 0 in
for i = n - 1 downto 0 do
  if rseen.(number.(i)) = 0 then incr count;
  rseen.(number.(i)) <- rseen.(number.(i))  + 1;
done;
let result = ref 0 in
let seen = Array.make 500000 false in
for i = 0 to n - 2 do
  rseen.(number.(i)) <- rseen.(number.(i))  - 1;
  if rseen.(number.(i)) = 0 then decr count;
  if not seen.(number.(i)) then result := !result + !count;
  seen.(number.(i)) <- true;
done;
print_int !result;
print_newline ();


Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions:  Walk up or down one unit on a tree.  Eat a nut on the top of the current tree.  Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > hi + 1. Compute the minimal time (in seconds) required to eat all nuts.

The first line contains an integer n (1  ≤  n ≤ 105) — the number of trees. Next n lines contains the height of trees: i-th line contains an integer hi (1 ≤ hi ≤ 104) — the height of the tree with the number i.

Print a single integer — the minimal time required to eat all nuts in seconds.

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

let () =
  let n = read_int() in
  let h = Array.init n (fun _ -> read_int()) in

  let rec loop i ac = if i=n-1 then ac else (
    let ac = ac + (abs (h.(i) - h.(i+1))) + 2 in
      loop (i+1) ac
  ) in

  let ans = loop 0 (h.(0)+1) in
    Printf.printf "%d\n" ans


Everyone knows that 2010 FIFA World Cup is being held in South Africa now. By the decision of BFA (Berland's Football Association) next World Cup will be held in Berland. BFA took the decision to change some World Cup regulations:  the final tournament features n teams (n is always even)  the first n / 2 teams (according to the standings) come through to the knockout stage  the standings are made on the following principle: for a victory a team gets 3 points, for a draw — 1 point, for a defeat — 0 points. In the first place, teams are ordered in the standings in decreasing order of their points; in the second place — in decreasing order of the difference between scored and missed goals; in the third place — in the decreasing order of scored goals  it's written in Berland's Constitution that the previous regulation helps to order the teams without ambiguity. You are asked to write a program that, by the given list of the competing teams and the results of all the matches, will find the list of teams that managed to get through to the knockout stage.

The first input line contains the only integer n (1 ≤ n ≤ 50) — amount of the teams, taking part in the final tournament of World Cup. The following n lines contain the names of these teams, a name is a string of lower-case and upper-case Latin letters, its length doesn't exceed 30 characters. The following n·(n - 1) / 2 lines describe the held matches in the format name1-name2 num1:num2, where name1, name2 — names of the teams; num1, num2 (0 ≤ num1, num2 ≤ 100) — amount of the goals, scored by the corresponding teams. Accuracy of the descriptions is guaranteed: there are no two team names coinciding accurate to the letters' case; there is no match, where a team plays with itself; each match is met in the descriptions only once.

Output n / 2 lines — names of the teams, which managed to get through to the knockout stage in lexicographical order. Output each name in a separate line. No odd characters (including spaces) are allowed. It's guaranteed that the described regulations help to order the teams without ambiguity.

let (|>) x f = f x

let () =
  let n = read_int () in
  let s2i = Hashtbl.create n in
  let i2s = Array.make n "" in
  for i = 0 to n-1 do
    let s = read_line () in
    i2s.(i) <- s;
    Hashtbl.add s2i s i
  done;

  let score = Array.make n 0 in
  let goal = Array.make n 0 in
  let gd = Array.make n 0 in
  let re = Str.regexp "\\([A-Za-z]+\\)-\\([A-Za-z]+\\) \\([0-9]+\\):\\([0-9]+\\)" in
  let add i dlt = score.(i) <- score.(i) + dlt in
  for i = 1 to n*(n-1)/2 do
    let s = read_line () in
    let flag = Str.string_match re s 0 in
    let u = Str.matched_group 1 s |> Hashtbl.find s2i
    and v = Str.matched_group 2 s |> Hashtbl.find s2i
    and uu = Str.matched_group 3 s |> int_of_string
    and vv = Str.matched_group 4 s |> int_of_string in
    if uu > vv then
      add u 3
    else if uu < vv then
      add v 3
    else (
      add u 1;
      add v 1
    );
    gd.(u) <- gd.(u) + uu - vv;
    gd.(v) <- gd.(v) + vv - uu;
    goal.(u) <- goal.(u) + uu;
    goal.(v) <- goal.(v) + uu;
  done;

  let idx = Array.init n (fun x -> x) in
  Array.sort (fun x y -> let r = compare score.(y) score.(x) in
    if r <> 0 then r
    else
      let r = compare gd.(y) gd.(x) in
      if r <> 0 then r
      else compare goal.(y) goal.(x)) idx;
  let ans = Array.init (n/2) (fun i -> i2s.(idx.(i))) in
  Array.sort compare ans;
  for i = 0 to n/2-1 do
    print_endline ans.(i)
  done

let (|>) x f = f x

let () =
  let n = read_int () in
  let s2i = Hashtbl.create n in
  let i2s = Array.make n "" in
  for i = 0 to n-1 do
    let s = read_line () in
    i2s.(i) <- s;
    Hashtbl.add s2i s i
  done;

  let score = Array.make n 0 in
  let re = Str.regexp "\\([A-Za-z]+\\)-\\([A-Za-z]+\\) \\([0-9]+\\):\\([0-9]+\\)" in
  let add i dlt = score.(i) <- score.(i) + dlt in
  for i = 1 to n*(n-1)/2 do
    let s = read_line () in
    let flag = Str.string_match re s 0 in
    let u = Str.matched_group 1 s |> Hashtbl.find s2i
    and v = Str.matched_group 2 s |> Hashtbl.find s2i
    and uu = Str.matched_group 3 s |> int_of_string
    and vv = Str.matched_group 4 s |> int_of_string in
    if uu > vv then
      add u 3
    else if uu < vv then
      add v 3
    else (
      add u 1;
      add v 1
    )
  done;

  let idx = Array.init n (fun x -> x) in
  Array.sort (fun x y -> compare score.(y) score.(x)) idx;
  let ans = Array.init (n/2) (fun i -> i2s.(idx.(i))) in
  Array.sort compare ans;
  for i = 0 to n/2-1 do
    print_endline ans.(i)
  done


Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she decided that only these two kinds of flowers should be in this exhibition.There are $$$n$$$ flowers in a row in the exhibition. Sonya can put either a rose or a lily in the $$$i$$$-th position. Thus each of $$$n$$$ positions should contain exactly one flower: a rose or a lily.She knows that exactly $$$m$$$ people will visit this exhibition. The $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive. The girl knows that each segment has its own beauty that is equal to the product of the number of roses and the number of lilies.Sonya wants her exhibition to be liked by a lot of people. That is why she wants to put the flowers in such way that the sum of beauties of all segments would be maximum possible.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1\leq n, m\leq 10^3$$$) — the number of flowers and visitors respectively. Each of the next $$$m$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1\leq l_i\leq r_i\leq n$$$), meaning that $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive.

Print the string of $$$n$$$ characters. The $$$i$$$-th symbol should be «0» if you want to put a rose in the $$$i$$$-th position, otherwise «1» if you want to put a lily. If there are multiple answers, print any.

NoteIn the first example, Sonya can put roses in the first, fourth, and fifth positions, and lilies in the second and third positions;  in the segment $$$[1\ldots3]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$;  in the segment $$$[2\ldots4]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$;  in the segment $$$[2\ldots5]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$. The total beauty is equal to $$$2+2+4=8$$$.In the second example, Sonya can put roses in the third, fourth, and sixth positions, and lilies in the first, second, and fifth positions;  in the segment $$$[5\ldots6]$$$, there are one rose and one lily, so the beauty is equal to $$$1\cdot 1=1$$$;  in the segment $$$[1\ldots4]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$;  in the segment $$$[4\ldots6]$$$, there are two roses and one lily, so the beauty is equal to $$$2\cdot 1=2$$$. The total beauty is equal to $$$1+4+2=7$$$.

let ( */ ) = Big_int.mult_big_int;;
let ( +/ ) = Big_int.add_big_int;;
let ( -/ ) = Big_int.sub_big_int;;
let to_int = Big_int.int_of_big_int;;
let of_int = Big_int.big_int_of_int;;
let min = Big_int.min_big_int;;
let (modi) = Big_int.mod_big_int;;
let to_string = Big_int.string_of_big_int;;
let of_string = Big_int.big_int_of_string;;
let (</) = Big_int.lt_big_int;;

let split_on_char sep s =
  let r = ref [] in
  let j = ref (String.length s) in
  for i = String.length s - 1 downto 0 do
    if String.unsafe_get s i = sep then begin
      r := String.sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  String.sub s 0 !j :: !r
;;

let n,m = Scanf.sscanf (read_line ()) "%i %i" (fun n m -> (n, m)) in
for i = 1 to m do
  ignore (read_line ());
done;
for i = 1 to n do
  print_int (i mod 2);
done;
print_newline ();

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,m = get_2_i64 ()
  in let ar = Array.make (~|m) (0L,0L)
  in
  for i=0 to ~|m-$1 do
    let u,v = get_2_i64 ()
    in ar.(i) <- u,v
  done;
  let res = Array.make ~|n 0 in
  Array.iteri (fun i _ -> print_int @@  if i mod 2 = 0 then 1 else 0) res;
  print_newline ()


Today at the lesson of mathematics, Petya learns about the digital root.The digital root of a non-negative integer is the single digit value obtained by an iterative process of summing digits, on each iteration using the result from the previous iteration to compute a digit sum. The process continues until a single-digit number is reached. Let's denote the digital root of $$$x$$$ as $$$S(x)$$$. Then $$$S(5)=5$$$, $$$S(38)=S(3+8=11)=S(1+1=2)=2$$$, $$$S(10)=S(1+0=1)=1$$$.As a homework Petya got $$$n$$$ tasks of the form: find $$$k$$$-th positive number whose digital root is $$$x$$$.Petya has already solved all the problems, but he doesn't know if it's right. Your task is to solve all $$$n$$$ tasks from Petya's homework.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^3$$$) — the number of tasks in Petya's homework. The next $$$n$$$ lines contain two integers $$$k_i$$$ ($$$1 \le k_i \le 10^{12}$$$) and $$$x_i$$$ ($$$1 \le x_i \le 9$$$) — $$$i$$$-th Petya's task in which you need to find a $$$k_i$$$-th positive number, the digital root of which is $$$x_i$$$.

Output $$$n$$$ lines, $$$i$$$-th line should contain a single integer — the answer to the $$$i$$$-th problem.

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

let () =
  let n = gi 0 in
  rep 1L n (fun _ ->
    let k,x = g2 0 in
    printf "%Ld\n" @@ x + 9L * (k - 1L)
  )

let (++),( ** ),(--) = Int64.(add,mul,sub);;
Scanf.(Array.(scanf " %d" @@ fun q ->
  init q (fun _ -> scanf " %Ld %Ld" @@ fun k x ->
    Printf.printf "%Ld\n" @@ x ++ 9L ** (k--1L))))

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

let () =
  let n = gi 0 in
  rep 1L n (fun _ ->
    let k,x = g2 0 in
    printf "%Ld" @@ x + 9L * (k - 1L)
  )


I, Fischl, Prinzessin der Verurteilung, descend upon this land by the call of fate an — Oh, you are also a traveler from another world? Very well, I grant you permission to travel with me.It is no surprise Fischl speaks with a strange choice of words. However, this time, not even Oz, her raven friend, can interpret her expressions! Maybe you can help us understand what this young princess is saying?You are given a string of $$$n$$$ lowercase Latin letters, the word that Fischl just spoke. You think that the MEX of this string may help you find the meaning behind this message. The MEX of the string is defined as the shortest string that doesn't appear as a contiguous substring in the input. If multiple strings exist, the lexicographically smallest one is considered the MEX. Note that the empty substring does NOT count as a valid MEX.A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds:   $$$a$$$ is a prefix of $$$b$$$, but $$$a \ne b$$$;  in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$. A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.Find out what the MEX of the string is!

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the length of the word. The second line for each test case contains a single string of $$$n$$$ lowercase Latin letters. The sum of $$$n$$$ over all test cases will not exceed $$$1000$$$.

For each test case, output the MEX of the string on a new line.

3
28
qaabzwsxedcrfvtgbyhnujmiklop
13
cleanairactbd
10
aannttoonn

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 n2l n = char_of_int ((int_of_char 'a') + n)

let mex h len =
  let a = Array.make len 0 in
  let rec increment i = if i = len then false
    else if a.(i) = 25 then (
      a.(i) <- 0;
      increment (i+1)
    ) else (
      a.(i) <- a.(i) + 1;
      true;
    )
  in
  let rec loop () =
    let s = String.init len (fun i -> n2l a.(len-i-1)) in
    if not (Hashtbl.mem h s) then Some s else (
      if increment 0 then loop () else None
    )
  in
  loop ()

let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let read_string _ = bscanf Scanning.stdin " %s " (fun x -> x)
let () =
  let cases = read_int () in
  for case = 1 to cases do
    let n = read_int () in
    let s = read_string () in

    let rec loop len =
      let h = Hashtbl.create 10 in
      if len > 3 then failwith "len too big" else (
	for i = 0 to n-len do
	  let x = String.sub s i len in
	  Hashtbl.replace h x true;
	done;
	match mex h len with
	  | None -> loop (len+1)
	  | Some s -> printf "%s\n" s
      )
    in
    loop 1
  done


In late autumn evening n robots gathered in the cheerful company of friends. Each robot has a unique identifier — an integer from 1 to 109.At some moment, robots decided to play the game "Snowball". Below there are the rules of this game. First, all robots stand in a row. Then the first robot says his identifier. After that the second robot says the identifier of the first robot and then says his own identifier. Then the third robot says the identifier of the first robot, then says the identifier of the second robot and after that says his own. This process continues from left to right until the n-th robot says his identifier.Your task is to determine the k-th identifier to be pronounced.

The first line contains two positive integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ min(2·109, n·(n + 1) / 2). The second line contains the sequence id1, id2, ..., idn (1 ≤ idi ≤ 109) — identifiers of roborts. It is guaranteed that all identifiers are different.

Print the k-th pronounced identifier (assume that the numeration starts from 1).

NoteIn the first sample identifiers of robots will be pronounced in the following order: 1, 1, 2. As k = 2, the answer equals to 1.In the second test case identifiers of robots will be pronounced in the following order: 10, 10, 4, 10, 4, 18, 10, 4, 18, 3. As k = 5, the answer equals to 4.

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 long x = Int64.of_int x
let short x = Int64.to_int x

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

let () =
  let n = read_int () in
  let k = read_long () in
  let id = Array.init n (fun _ -> read_int()) in

  let tri j = (j ** (j++1L)) // 2L in

  let rec loop i = if tri i >= k then i--1L else loop (i++1L) in

  let index = -1 + short (k -- (tri (loop 1L))) in

  printf "%d\n" id.(index)


In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) — the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) — the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$.

Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it.

open Printf
open Scanf

(*------ union find code *)
let makeset n = Array.make n (-1) (* negatives of the sizes at the roots *)
let rec find i p = if p.(i) < 0 then i else (
  p.(i) <- find p.(i) p;
  p.(i);
)
let union i j p =
  let i = find i p in
  let j = find j p in
  if i<>j then (
    let sz = p.(i) + p.(j) in
    p.(j) <- i;
    p.(i) <- sz
  )

let size i p = -p.(i)
(*------ end union find code *)

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

  let p = makeset n in

  for i = 0 to m-1 do
    let k = read_int() in
    if k>0 then (
      let first = -1 + read_int () in
      for j = 1 to k-1 do
	let next = -1 + read_int() in
	union first next p
      done
    )
  done;

  for i=0 to n-1 do
    printf "%d " (size (find i p) p)
  done;

  print_newline()


You are given an array $$$a_1, a_2, \dots, a_n$$$. You can perform operations on the array. In each operation you can choose an integer $$$i$$$ ($$$1 \le i < n$$$), and swap elements $$$a_i$$$ and $$$a_{i+1}$$$ of the array, if $$$a_i + a_{i+1}$$$ is odd.Determine whether it can be sorted in non-decreasing order using this operation any number of times.

Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases. 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 length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, print "Yes" or "No" depending on whether you can or can not sort the given array. You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as positive answer).

NoteIn the first test case, we can simply swap $$$31$$$ and $$$14$$$ ($$$31 + 14 = 45$$$ which is odd) and obtain the non-decreasing array $$$[1,6,14,31]$$$.In the second test case, the only way we could sort the array is by swapping $$$4$$$ and $$$2$$$, but this is impossible, since their sum $$$4 + 2 = 6$$$ is even.In the third test case, there is no way to make the array non-decreasing.In the fourth test case, the array is already non-decreasing.

(* Codeforces 1614 A Divan and a Store 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 rec is_sorted x = match x with
  | [] -> true
  | h::[] -> true
  | h::h2::t -> if h <= h2 then is_sorted (h2::t) else false;;

let rec sep_even_odd leven lodd l = match l with
    | [] -> ((List.rev leven), (List.rev lodd))
    | h :: t ->
    let nle = if (h mod 2) == 0 then h :: leven else leven in
    let nlo = if (h mod 2) == 0 then lodd else h :: lodd in
    sep_even_odd nle nlo t;;

let do_one () =
    let n = gr () in
    let arev = readlist n [] in
    let a = List.rev arev in
    let el,ol = sep_even_odd [] [] a in
    let esorted = is_sorted el in
    let osorted = is_sorted ol in
    let ok = esorted && osorted in
    let sans = if ok then "Yes" else "No" in
    Printf.printf "%s\n" sans;;

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

let main() =
	do_all ();;

main();;


Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special. Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri].Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible. You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.The mex of a set S is a minimum possible non-negative integer that is not in S.

The first line contains two integers n and m (1 ≤ n, m ≤ 105). The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].

In the first line print single integer — the maximum possible minimum mex. In the second line print n integers — the array a. All the elements in a should be between 0 and 109. It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109. If there are multiple solutions, print any of them.

NoteThe first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2.

let read_pair = fun a b -> (a, b) in

let (n, m) = Scanf.scanf "%d %d\n" read_pair and ans = ref 99999999 in
for i = 1 to m do
        let (tmp1, tmp2) = Scanf.scanf "%d %d\n" read_pair in
        ans := min (!ans) (tmp2 - tmp1 + 1)
done;
print_int !ans;
print_newline ();
for i = 0 to n - 1 do
        print_int (i mod !ans);
        print_string " "
done;;

open Printf
open Scanf

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

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

  let len = minf 0 (m-1) (fun _ ->
    let (l,r) = read_pair() in
    r-l+1
  ) in

  printf "%d\n" len;

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

  print_newline()

let () =
  Scanf.scanf "%d %d " @@ fun n m ->
  let mex = ref n in
  for i = 1 to m do
    Scanf.scanf "%d %d " @@ fun l r ->
    mex := min !mex (r - l + 1)
  done;
  Printf.printf "%d\n" !mex;
  for i = 0 to n - 2 do
    Printf.printf "%d " (i mod !mex)
  done;
  Printf.printf "%d\n" ((n - 1) mod !mex)


You are given a garland consisting of $$$n$$$ lamps. States of the lamps are represented by the string $$$s$$$ of length $$$n$$$. The $$$i$$$-th character of the string $$$s_i$$$ equals '0' if the $$$i$$$-th lamp is turned off or '1' if the $$$i$$$-th lamp is turned on. You are also given a positive integer $$$k$$$.In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa).The garland is called $$$k$$$-periodic if the distance between each pair of adjacent turned on lamps is exactly $$$k$$$. Consider the case $$$k=3$$$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa.Your task is to find the minimum number of moves you need to make to obtain $$$k$$$-periodic garland from the given one.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 25~ 000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10^6; 1 \le k \le n$$$) — the length of $$$s$$$ and the required period. The second line of the test case contains the string $$$s$$$ consisting of $$$n$$$ characters '0' and '1'. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$ ($$$\sum n \le 10^6$$$).

For each test case, print the answer — the minimum number of moves you need to make to obtain $$$k$$$-periodic garland from the given one.

let r_int () = Scanf.scanf " %d" (fun x -> x) in
let r_char () = Scanf.scanf " %c" (fun x -> x) in

let rec r_list = function
| 0 -> []
| n -> (if r_char () = '0' then 0 else 1) :: r_list (n - 1)
in

let rec calcule_cout etat =
   let (a, b, c) = etat in
   function
   | [] -> c
   | x :: r ->
      let a = a + x in
      let b = min a (b + 1 - x) in
      let c = min b (c + x) in
      calcule_cout (a, b, c) r
in

let nbTests = r_int() in
for iTest = 1 to nbTests do
   let nbBits = r_int () in
   let dist = r_int () in
   let bits = r_list nbBits in
   let mini = ref 1000000 in

   let nbUns = List.fold_left (+) 0 bits in

   let listes = Array.make dist [] in
   let rec separe i = function
   | [] -> ()
   | x :: r -> listes.(i) <- x :: listes.(i); separe ((i + 1) mod dist) r
   in separe 0 bits;

   for iMod = 0 to (dist - 1) do
      let curUns = List.fold_left (+) 0 listes.(iMod) in
      let curCout = nbUns - curUns + (calcule_cout (0,0,0) listes.(iMod)) in
      mini := min !mini curCout
   done;

   print_int !mini;
   print_newline ()
done


Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones.Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any two adjacent positions in the string, and if one them contains 0, and the other contains 1, then we are allowed to remove these two digits from the string, obtaining a string of length n - 2 as a result.Now Andreid thinks about what is the minimum length of the string that can remain after applying the described operation several times (possibly, zero)? Help him to calculate this number.

First line of the input contains a single integer n (1 ≤ n ≤ 2·105), the length of the string that Andreid has. The second line contains the string of length n consisting only from zeros and ones.

Output the minimum length of the string that may remain after applying the described operations several times.

NoteIn the first sample test it is possible to change the string like the following: .In the second sample test it is possible to change the string like the following: .In the third sample test it is possible to change the string like the following: .

let read_int () = Scanf.scanf "%d " (fun n -> n)
let read_char () = Scanf.scanf "%c " (fun c -> c)

let solve () =
    let n = read_int () in
    let rec loop i zeros ones =
        if i < n
        then
            let number = read_char () in
                if number = '0'
                then
                    loop (i + 1) (zeros + 1) ones
                else
                    loop (i + 1) zeros (ones + 1)
        else
            Pervasives.abs (zeros - ones)
    in loop 0 0 0

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

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

let input () =
    let n = Pervasives.read_int () in
    let list = str_to_list (Pervasives.read_line ()) in
    (list, n)

let solve (list, n) =
    let zeros = List.length (List.filter (fun c -> c = '0') list) in
    let ones = n - zeros in
    let result = Pervasives.abs (zeros - ones) in
    result

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

let rec count s c i =
    if i = String.length s
    then 0
    else (if s.[i]=c then 1 else 0) + count s c (i+1)

in let n = read_int()
in let s = read_line()
in let reml = n - 2 * min (count s '0' 0) (count s '1' 0)
in print_int(reml)


Caisa solved the problem with the sugar and now he is on the way back to home. Caisa is playing a mobile game during his path. There are (n + 1) pylons numbered from 0 to n in this game. The pylon with number 0 has zero height, the pylon with number i (i > 0) has height hi. The goal of the game is to reach n-th pylon, and the only move the player can do is to jump from the current pylon (let's denote its number as k) to the next one (its number will be k + 1). When the player have made such a move, its energy increases by hk - hk + 1 (if this value is negative the player loses energy). The player must have non-negative amount of energy at any moment of the time. Initially Caisa stand at 0 pylon and has 0 energy. The game provides a special opportunity: one can pay a single dollar and increase the height of anyone pylon by one. Caisa may use that opportunity several times, but he doesn't want to spend too much money. What is the minimal amount of money he must paid to reach the goal of the game?

The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers h1, h2, ..., hn (1  ≤  hi  ≤  105) representing the heights of the pylons.

Print a single number representing the minimum number of dollars paid by Caisa.

NoteIn the first sample he can pay 4 dollars and increase the height of pylon with number 0 by 4 units. Then he can safely pass to the last pylon.

let pf = Printf.printf ;;
let epf = Printf.eprintf ;;
let sf = Scanf.scanf ;;

let (|>) x f = f x ;;
let (@@) f x = f x ;;
module Array = struct
  include ArrayLabels
  let fold_lefti ~f ~init arr =
    let acc = ref init in
    for i = 0 to Array.length arr - 1 do
      acc := f i !acc arr.(i)
    done;
    !acc
  ;;
end
module String = StringLabels ;;
module List  = struct
  include ListLabels ;;
  let rec repeat n a = if n = 0 then [] else a :: repeat (n - 1) a ;;
  let rec drop n a =
    if n = 0 then a
    else match a with
    | [] -> failwith "cannot take"
    | x :: xs -> drop (n - 1) xs ;;

  let init ~f n =
    let res = ref [] in
    for i = 0 to n - 1 do
      res := f i :: !res
    done;
    List.rev !res
  ;;
end ;;
module H = Hashtbl ;;

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

let () =
  sf "%d " (fun n ->
    let arr = Array.init n ~f:(fun i -> sf "%d " (fun i -> i)) in
    pf "%d\n" @@ Array.fold_left ~init:0 arr ~f:max)
;;

open Scanf
open Printf

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

let a = Array.make (n+1) 0

for i = 1 to n do
  let j = Scanf.scanf "%d " ( fun x -> x ) in
  a.(i)<-j
done

let rslt = Array.fold_left (fun x y -> if x > y then x else y ) (-1000) a
in printf "%d\n" rslt

open Scanf
open Printf

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

let a = Array.make (n+1) 0

for i = 1 to n do
  let j = Scanf.scanf "%d " ( fun x -> x ) in
  a.(i)<-j
done

let cnt = ref 0

for i = 1 to n do
  let m = a.(i) - a.(i-1) in
  cnt := m + !cnt
done

let () = printf "%d\n" !cnt


You are given two polynomials:  P(x) = a0·xn + a1·xn - 1 + ... + an - 1·x + an and  Q(x) = b0·xm + b1·xm - 1 + ... + bm - 1·x + bm.  Calculate limit .

The first line contains two space-separated integers n and m (0 ≤ n, m ≤ 100) — degrees of polynomials P(x) and Q(x) correspondingly. The second line contains n + 1 space-separated integers — the factors of polynomial P(x): a0, a1, ..., an - 1, an ( - 100 ≤ ai ≤ 100, a0 ≠ 0). The third line contains m + 1 space-separated integers — the factors of polynomial Q(x): b0, b1, ..., bm - 1, bm ( - 100 ≤ bi ≤ 100, b0 ≠ 0).

If the limit equals  + ∞, print "Infinity" (without quotes). If the limit equals  - ∞, print "-Infinity" (without the quotes). If the value of the limit equals zero, print "0/1" (without the quotes). Otherwise, print an irreducible fraction — the value of limit , in the format "p/q" (without the quotes), where p is the — numerator, q (q > 0) is the denominator of the fraction.

NoteLet's consider all samples:           You can learn more about the definition and properties of limits if you follow the link: http://en.wikipedia.org/wiki/Limit_of_a_function

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

let next_int: unit -> int = fun _ ->
  Scanf.scanf " %d" (fun x -> x)
;;

let next_polynomial n =
  let ret = next_int () in
  for i = 1 to n do
    next_int ()
  done;
  ret
;;

let rec print_gcd a b =
  let g = gcd (abs a) (abs b) in
  if b < 0
  then print_gcd (-a) (-b)
  else Printf.printf "%d/%d" (a / g) (b / g)
;;

let _ =
  let n = next_int () in
  let m = next_int () in
  let a = next_polynomial n in
  let b = next_polynomial m in
  if n < m then print_string "0/1"
  else if n = m then print_gcd a b
  else print_string ((if a * b > 0 then "" else "-") ^ "Infinity")
;;

let rec gcd x y =
  if y == 0 then x else gcd y (x mod y)

let solve n m p q =
  let a = List.hd p in
  let b = List.hd q in
  if n > m  then if  a * b >= 0 then "Infinity" else "-Infinity"
  else if n < m then "0/1"
  else
    let gcd = gcd a b in
    let a = a / gcd in
    let b = b / gcd in
    if a > 0 && b < 0
    then (string_of_int  (- a)) ^ "/" ^ (string_of_int (- b))
    else (string_of_int a) ^ "/" ^ (string_of_int b)

let id x = x

let rec input_polynomial dim =
  if dim < 0 then []
  else (Scanf.scanf " %d" id) :: input_polynomial (dim - 1)

let _ =
  let n = Scanf.scanf " %d" id in
  let m = Scanf.scanf " %d" id in
  let p = List.rev (input_polynomial n) in
  let q = List.rev (input_polynomial m) in
  Printf.printf "%s\n" (solve n m p q)

let fraction n d =
  let str = (string_of_int n) ^ "/" ^  (string_of_int d) in
  Ratio.string_of_ratio (Num.ratio_of_num (Num.num_of_string str))

let solve p q =
  if List.length p > List.length q
  then if  (List.hd p) * (List.hd q) >= 0 then "Infinity"
    else "-Infinity"
  else if List.length p < List.length q then "0/1"
  else fraction (List.hd p) (List.hd q)

let id x = x

let rec input_polynomial dim =
  if dim < 0 then []
  else (Scanf.scanf " %d" id) :: input_polynomial (dim - 1)

let _ =
  let n = Scanf.scanf " %d" id in
  let m = Scanf.scanf " %d" id in
  let p = List.rev (input_polynomial n) in
  let q = List.rev (input_polynomial m) in
  Printf.printf "%s\n" (solve p q)

let sb = Scanf.Scanning.stdib;;

let fraction n d =
  let str = (string_of_int n) ^ "/" ^  (string_of_int d) in
  Ratio.string_of_ratio (Num.ratio_of_num (Num.num_of_string str))

let solve p q =
  if List.length p > List.length q
  then if  (List.hd p) * (List.hd q) >= 0 then "Infinity"
    else "-Infinity"
  else if List.length p < List.length q then "0/1"
  else fraction (List.hd p) (List.hd q)

let id x = x

let rec input_polynomial dim =
  if dim < 0 then []
  else (Scanf.bscanf sb " %d" id) :: input_polynomial (dim - 1)

let _ =
  let n = Scanf.bscanf sb " %d" id in
  let m = Scanf.bscanf sb " %d" id in
  let p = List.rev (input_polynomial n) in
  let q = List.rev (input_polynomial m) in
  Printf.printf "%s\n" (solve p q)

module Std = struct

  module List = struct
    include List
    module L = List
    let map f lst = L.rev (L.rev_map f lst)
    let map2 f l1 l2 = L.rev (L.rev_map2 f l1 l2)
    let each_cons f lst = L.rev_map2 f (L.tl (L.rev lst)) (L.rev (L.tl lst))
    let append x y = L.rev (L.rev_append x y)
    let fold = L.fold_left
    let permute_with lst p = L.map (Array.get (Array.of_list lst)) p
  end

  let ( |> ) x f = f x
  let ( <| ) f x = f x
  let ( ~. ) = float_of_int
  let ( @ ) = List.append

  let rec repeat n f x = if n = 0 then x else repeat (n-1) f (f x)

  let di x = prerr_endline (string_of_int x); x
  let df f = prerr_endline (string_of_float f); f
  let ds s = prerr_endline s; s

  let int () = Scanf.scanf " %d" (fun x -> x)
  let read_int = int
  let float () = Scanf.scanf " %f" (fun x -> x)
  let read_float = float
  let rec n_int n () = List.rev <| repeat n (fun lst -> int () :: lst) []
  let rec n_float n () = List.rev <| repeat n (fun lst -> float () :: lst) []

end

open Std
module L = List

let rec gcd x y =
  if y = 0 then x
  else gcd y (x mod y)

(* entry point *)
let n = int ()
let m = int ()

let a = n_int (n+1) ()
let b = n_int (m+1) ()

let () =
  if n > m then
    Printf.printf "%s\n" (if (L.hd a) * (L.hd b) > 0 then "Infinity" else "-Infinity")
  else if n < m then
    Printf.printf "%s\n" "0/1"
  else
    let a = (L.hd a) in
    let b = (L.hd b) in
    let g = gcd a b in
    let a = a / g in
    let b = b / g in
    if a > 0 && b < 0 then
      Printf.printf "%d/%d" (-a) (-b)
    else
      Printf.printf "%d/%d" a b

let rec gcd x y =
  if y == 0 then x else gcd y (x mod y)

let solve n m p q =
  let a = List.hd p in
  let b = List.hd q in
  if n > m  then if  a * b >= 0 then "Infinity" else "-Infinity"
  else if n < m then "0/1"
  else
    let gcd = gcd a b in
    let a = a / gcd in
    let b = b / gcd in
    (string_of_int a) ^ "/" ^ (string_of_int b)

let id x = x

let rec input_polynomial dim =
  if dim < 0 then []
  else (Scanf.scanf " %d" id) :: input_polynomial (dim - 1)

let _ =
  let n = Scanf.scanf " %d" id in
  let m = Scanf.scanf " %d" id in
  let p = List.rev (input_polynomial n) in
  let q = List.rev (input_polynomial m) in
  Printf.printf "%s\n" (solve n m p q)

let sb = Scanf.Scanning.stdib;;

let fraction n d =
  let str = (string_of_int n) ^ "/" ^  (string_of_int d) in
  Ratio.string_of_ratio (Num.ratio_of_num (Num.num_of_string str))

let solve p q =
  if List.length p > List.length q
  then if  (List.hd p) > 0 then "Infinity"
    else "-Infinity"
  else if List.length p < List.length q then "0/1"
  else fraction (List.hd p) (List.hd q)

let id x = x

let rec input_polynomial dim =
  if dim < 0 then []
  else (Scanf.bscanf sb " %d" id) :: input_polynomial (dim - 1)

let _ =
  let n = Scanf.bscanf sb " %d" id in
  let m = Scanf.bscanf sb " %d" id in
  let p = List.rev (input_polynomial n) in
  let q = List.rev (input_polynomial m) in
  Printf.printf "%s\n" (solve p q)

module Std = struct

  module List = struct
    include List
    module L = List
    let map f lst = L.rev (L.rev_map f lst)
    let map2 f l1 l2 = L.rev (L.rev_map2 f l1 l2)
    let each_cons f lst = L.rev_map2 f (L.tl (L.rev lst)) (L.rev (L.tl lst))
    let append x y = L.rev (L.rev_append x y)
    let fold = L.fold_left
    let permute_with lst p = L.map (Array.get (Array.of_list lst)) p
  end

  let ( |> ) x f = f x
  let ( <| ) f x = f x
  let ( ~. ) = float_of_int
  let ( @ ) = List.append

  let rec repeat n f x = if n = 0 then x else repeat (n-1) f (f x)

  let di x = prerr_endline (string_of_int x); x
  let df f = prerr_endline (string_of_float f); f
  let ds s = prerr_endline s; s

  let int () = Scanf.scanf " %d" (fun x -> x)
  let read_int = int
  let float () = Scanf.scanf " %f" (fun x -> x)
  let read_float = float
  let rec n_int n () = List.rev <| repeat n (fun lst -> int () :: lst) []
  let rec n_float n () = List.rev <| repeat n (fun lst -> float () :: lst) []

end

open Std
module L = List

let rec gcd x y =
  if y = 0 then x
  else gcd y (x mod y)

(* entry point *)
let n = int ()
let m = int ()

let a = n_int (n+1) ()
let b = n_int (m+1) ()

let () =
  if n > m then
    Printf.printf "%s\n" (if (L.hd a > 0) then "Infinity" else "-Infinity")
  else if n < m then
    Printf.printf "%s\n" "0/1"
  else
    let a = (L.hd a) in
    let b = (L.hd b) in
    let g = gcd a b in
    let a = a / g in
    let b = b / g in
    if a > 0 && b < 0 then
      Printf.printf "%d/%d" (-a) (-b)
    else
      Printf.printf "%d/%d" a b

module Std = struct

  module List = struct
    include List
    module L = List
    let map f lst = L.rev (L.rev_map f lst)
    let map2 f l1 l2 = L.rev (L.rev_map2 f l1 l2)
    let each_cons f lst = L.rev_map2 f (L.tl (L.rev lst)) (L.rev (L.tl lst))
    let append x y = L.rev (L.rev_append x y)
    let fold = L.fold_left
    let permute_with lst p = L.map (Array.get (Array.of_list lst)) p
  end

  let ( |> ) x f = f x
  let ( <| ) f x = f x
  let ( ~. ) = float_of_int
  let ( @ ) = List.append

  let rec repeat n f x = if n = 0 then x else repeat (n-1) f (f x)

  let di x = prerr_endline (string_of_int x); x
  let df f = prerr_endline (string_of_float f); f
  let ds s = prerr_endline s; s

  let int () = Scanf.scanf " %d" (fun x -> x)
  let read_int = int
  let float () = Scanf.scanf " %f" (fun x -> x)
  let read_float = float
  let rec n_int n () = List.rev <| repeat n (fun lst -> int () :: lst) []
  let rec n_float n () = List.rev <| repeat n (fun lst -> float () :: lst) []

end

open Std
module L = List

let rec gcd x y =
  if y = 0 then x
  else gcd y (x mod y)

(* entry point *)
let n = int ()
let m = int ()

let a = n_int (n+1) ()
let b = n_int (m+1) ()

let () =
  if n > m then
    Printf.printf "%s\n" (if (L.hd a > 0) then "Infinity" else "-Infinity")
  else if n < m then
    Printf.printf "%s\n" "0/1"
  else
    let a = (L.hd a) in
    let b = (L.hd b) in
    let g = gcd a b in
    Printf.printf "%d/%d" (a / g) (b / g)


PolandBall has such a convex polygon with n veritces that no three of its diagonals intersect at the same point. PolandBall decided to improve it and draw some red segments. He chose a number k such that gcd(n, k) = 1. Vertices of the polygon are numbered from 1 to n in a clockwise way. PolandBall repeats the following process n times, starting from the vertex 1: Assume you've ended last operation in vertex x (consider x = 1 if it is the first operation). Draw a new segment from vertex x to k-th next vertex in clockwise direction. This is a vertex x + k or x + k - n depending on which of these is a valid index of polygon's vertex.Your task is to calculate number of polygon's sections after each drawing. A section is a clear area inside the polygon bounded with drawn diagonals or the polygon's sides.

There are only two numbers in the input: n and k (5 ≤ n ≤ 106, 2 ≤ k ≤ n - 2, gcd(n, k) = 1).

You should print n values separated by spaces. The i-th value should represent number of polygon's sections after drawing first i lines.

NoteThe greatest common divisor (gcd) of two integers a and b is the largest positive integer that divides both a and b without a remainder.For the first sample testcase, you should output "2 3 5 8 11". Pictures below correspond to situations after drawing lines.

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

let g2d n = n land (-n)

let fenwick_new n =
  Array.make n 0

let fenwick_modify ft n x =
  let i = ref (n + 1) in
    while !i <= Array.length ft do
      ft.(!i - 1) <- ft.(!i - 1) + x;
      i := !i + g2d !i;
    done

let fenwick_count ft n =
  let i = ref (n + 1) in
  let res = ref 0 in
    while !i > 0 do
      res := !res + ft.(!i - 1);
      i := !i - g2d !i;
    done;
    !res

let fenwick_count2 ft m n =
  fenwick_count ft n - fenwick_count ft (m - 1)

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let k =
    if 2 * k > n
    then n - k
    else k
  in
  let ft = fenwick_new n in
  let res = ref 1L in
  let p = ref 0 in
    for _t = 1 to n do
      res := !res +| 1L;
      let q = !p + k in
	if q <= n
	then (
	  res := !res +| Int64.of_int (fenwick_count2 ft (!p + 1) (q - 1));
	  fenwick_modify ft q 1;
	) else (
	  res := !res +| Int64.of_int (fenwick_count2 ft (!p + 1) (n - 1));
	  res := !res +| Int64.of_int (fenwick_count2 ft 0 (q - n - 1));
	  fenwick_modify ft (q - n) 1;
	);
	fenwick_modify ft !p 1;
	p := q mod n;
	Printf.printf "%Ld " !res
    done;
    Printf.printf "\n"

let g2d n = n land (-n)

let fenwick_new n =
  Array.make n 0

let fenwick_modify ft n x =
  let i = ref (n + 1) in
    while !i <= Array.length ft do
      ft.(!i - 1) <- ft.(!i - 1) + x;
      i := !i + g2d !i;
    done

let fenwick_count ft n =
  let i = ref (n + 1) in
  let res = ref 0 in
    while !i > 0 do
      res := !res + ft.(!i - 1);
      i := !i - g2d !i;
    done;
    !res

let fenwick_count2 ft m n =
  fenwick_count ft n - fenwick_count ft (m - 1)

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let k =
    if 2 * k > n
    then n - k
    else k
  in
  let ft = fenwick_new n in
  let res = ref 1 in
  let p = ref 0 in
    for _t = 1 to n do
      incr res;
      let q = !p + k in
	if q <= n
	then (
	  res := !res + fenwick_count2 ft (!p + 1) (q - 1);
	  fenwick_modify ft q 1;
	) else (
	  res := !res + fenwick_count2 ft (!p + 1) (n - 1);
	  res := !res + fenwick_count2 ft 0 (q - n - 1);
	  fenwick_modify ft (q - n) 1;
	);
	fenwick_modify ft !p 1;
	p := q mod n;
	Printf.printf "%d " !res
    done;
    Printf.printf "\n"


There are n parliamentarians in Berland. They are numbered with integers from 1 to n. It happened that all parliamentarians with odd indices are Democrats and all parliamentarians with even indices are Republicans.New parliament assembly hall is a rectangle consisting of a × b chairs — a rows of b chairs each. Two chairs are considered neighbouring if they share as side. For example, chair number 5 in row number 2 is neighbouring to chairs number 4 and 6 in this row and chairs with number 5 in rows 1 and 3. Thus, chairs have four neighbours in general, except for the chairs on the border of the hallWe know that if two parliamentarians from one political party (that is two Democrats or two Republicans) seat nearby they spent all time discussing internal party issues.Write the program that given the number of parliamentarians and the sizes of the hall determine if there is a way to find a seat for any parliamentarian, such that no two members of the same party share neighbouring seats.

The first line of the input contains three integers n, a and b (1 ≤ n ≤ 10 000, 1 ≤ a, b ≤ 100) — the number of parliamentarians, the number of rows in the assembly hall and the number of seats in each row, respectively.

If there is no way to assigns seats to parliamentarians in a proper way print -1. Otherwise print the solution in a lines, each containing b integers. The j-th integer of the i-th line should be equal to the index of parliamentarian occupying this seat, or 0 if this seat should remain empty. If there are multiple possible solution, you may print any of them.

NoteIn the first sample there are many other possible solutions. For example, 3 20 1and 2 13 0The following assignment 3 21 0is incorrect, because parliamentarians 1 and 3 are both from Democrats party but will occupy neighbouring seats.

open Printf
open Scanf

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

let () =
  let n = read_int () in
  let a = read_int () in
  let b = read_int () in

  if n > a*b then (
    printf "-1\n"
  ) else (
    let inc = if b land 1 = 0 then 1 else 0 in
    for i=0 to a-1 do
      for j=0 to b-1 do
	let ind = b*i + j + 1 in
	let x = match (i land 1, j land 1) with
	  | (0,_) -> ind
	  | (1,0) -> ind + inc
	  | (1,1) -> ind - inc
	  | _ -> failwith "bad"
	in
	printf "%d " (if x > n then 0 else x);
      done;
      print_newline()
    done
  )


Another Codeforces Round has just finished! It has gathered $$$n$$$ participants, and according to the results, the expected rating change of participant $$$i$$$ is $$$a_i$$$. These rating changes are perfectly balanced — their sum is equal to $$$0$$$.Unfortunately, due to minor technical glitches, the round is declared semi-rated. It means that all rating changes must be divided by two.There are two conditions though:   For each participant $$$i$$$, their modified rating change $$$b_i$$$ must be integer, and as close to $$$\frac{a_i}{2}$$$ as possible. It means that either $$$b_i = \lfloor \frac{a_i}{2} \rfloor$$$ or $$$b_i = \lceil \frac{a_i}{2} \rceil$$$. In particular, if $$$a_i$$$ is even, $$$b_i = \frac{a_i}{2}$$$. Here $$$\lfloor x \rfloor$$$ denotes rounding down to the largest integer not greater than $$$x$$$, and $$$\lceil x \rceil$$$ denotes rounding up to the smallest integer not smaller than $$$x$$$.  The modified rating changes must be perfectly balanced — their sum must be equal to $$$0$$$. Can you help with that?

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 13\,845$$$), denoting the number of participants. Each of the next $$$n$$$ lines contains a single integer $$$a_i$$$ ($$$-336 \le a_i \le 1164$$$), denoting the rating change of the $$$i$$$-th participant. The sum of all $$$a_i$$$ is equal to $$$0$$$.

Output $$$n$$$ integers $$$b_i$$$, each denoting the modified rating change of the $$$i$$$-th participant in order of input. For any $$$i$$$, it must be true that either $$$b_i = \lfloor \frac{a_i}{2} \rfloor$$$ or $$$b_i = \lceil \frac{a_i}{2} \rceil$$$. The sum of all $$$b_i$$$ must be equal to $$$0$$$. If there are multiple solutions, print any. We can show that a solution exists for any valid input.

NoteIn the first example, $$$b_1 = 5$$$, $$$b_2 = -3$$$ and $$$b_3 = -2$$$ is another correct solution.In the second example there are $$$6$$$ possible solutions, one of them is shown in the example output.

let n = read_int() in
let rec go n bal = match n with
    | 0 -> ()
    | _ -> begin
        let x = read_int() in
        if (x mod 2 = 0) then (
            Printf.printf "%d\n" (x/2);
            go (n-1) bal
        ) else
            let delta = if bal = 0 then 1 else -bal in
            let x = (x+delta)/2 in (
                Printf.printf "%d\n" x;
                go (n-1) (bal+delta)
            )
    end
in go n 0


The term of this problem is the same as the previous one, the only exception — increased restrictions.

The first line contains two positive integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ 109) — the number of ingredients and the number of grams of the magic powder. The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 109), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie. The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 109), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.

Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.

open Printf
open Scanf

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

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 ( ^^ ) a b = Int64.shift_left a b
let rec sum (i : int) (j : int) f (ac : int64) =
  if i > j then
    ac
  else
    sum (i + 1) j f (ac ++ (f i));;

let () =
  try
    while true do
      let n = read_int () in
      let k = read_long () in
      let a = Array.init n (fun _ -> read_long()) in
      let b = Array.init n (fun _ -> read_long()) in

      let possible m =
        let x = sum 0 (n - 1) (fun i -> min 4000000000L (max 0L ((m ** a.(i)) -- b.(i)))) 0L in
        x <= k
      in

      let rec binary_search small large =
        if (small ++ 1L) < large then
          let (mid : int64) = (small ++ large) // 2L in
          if possible mid then binary_search mid large else binary_search small mid
        else
          small
      in

      printf "%Ld\n" (binary_search 0L 2000000001L)
    done
  with End_of_file -> ()

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.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 rec sum i j f ac = if i>j then ac else sum (i+1) j f (ac ++ (f i))
let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_long () = bscanf Scanning.stdib " %Ld " (fun x -> x)
let () =
  let n = read_int () in
  let k = read_long () in
  let a = Array.init n (fun _ -> read_long()) in
  let b = Array.init n (fun _ -> read_long()) in

  let can_bake m =
    let mp = sum 0 (n-1) (fun i -> max 0L (m ** a.(i) -- b.(i))) 0L in
    mp <= k
  in

  let rec bsearch lo hi =
    (* lo can be made, hi cannot *)
    if lo++1L = hi then lo else
      let m = (lo ++ hi) // 2L in
      if can_bake m then bsearch m hi else bsearch lo m
  in

  let rec findhi hi = if can_bake hi then findhi (2L ** hi) else hi in

  printf "%Ld\n" (bsearch 0L (findhi 1L))

open Printf
open Scanf

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

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 ( ^^ ) a b = Int64.shift_left a b
let rec sum (i : int) (j : int) f (ac : int64) =
  if i > j then
    ac
  else
    sum (i + 1) j f (ac ++ (f i));;

let () =
  try
    while true do
      let n = read_int () in
      let k = read_long () in
      let a = Array.init n (fun _ -> read_long()) in
      let b = Array.init n (fun _ -> read_long()) in

      let possible m =
        let x = sum 0 (n - 1) (fun i -> max 0L (m ** a.(i) -- b.(i))) 0L in
        x <= k
      in

      let rec binary_search small large =
        if (small ++ 1L) < large then
          let mid = (small ++ large) // 2L in
          if possible mid then binary_search mid large else binary_search small mid
        else
          small
      in

      printf "%Ld\n" (binary_search 0L 1000000001L)
    done
  with End_of_file -> ()

open Printf
open Scanf

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

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 ( ^^ ) a b = Int64.shift_left a b
let rec sum (i : int) (j : int) f (ac : int64) =
  if i > j then
    ac
  else
    sum (i + 1) j f (ac ++ (f i));;

let () =
  try
    while true do
      let n = read_int () in
      let k = read_long () in
      let a = Array.init n (fun _ -> read_long()) in
      let b = Array.init n (fun _ -> read_long()) in

      let possible m =
        let x = sum 0 (n - 1) (fun i -> max 0L (m ** a.(i) -- b.(i))) 0L in
        x <= k
      in

      let rec binary_search small large =
        if (small ++ 1L) < large then
          let mid = (small ++ large) // 2L in
          if possible mid then binary_search mid large else binary_search small mid
        else
          small
      in

      printf "%Ld\n" (binary_search 0L 2000000001L)
    done
  with End_of_file -> ()


Vasiliy likes to rest after a hard work, so you may often meet him in some bar nearby. As all programmers do, he loves the famous drink "Beecola", which can be bought in n different shops in the city. It's known that the price of one bottle in the shop i is equal to xi coins.Vasiliy plans to buy his favorite drink for q consecutive days. He knows, that on the i-th day he will be able to spent mi coins. Now, for each of the days he want to know in how many different shops he can buy a bottle of "Beecola".

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of shops in the city that sell Vasiliy's favourite drink. The second line contains n integers xi (1 ≤ xi ≤ 100 000) — prices of the bottles of the drink in the i-th shop. The third line contains a single integer q (1 ≤ q ≤ 100 000) — the number of days Vasiliy plans to buy the drink. Then follow q lines each containing one integer mi (1 ≤ mi ≤ 109) — the number of coins Vasiliy can spent on the i-th day.

Print q integers. The i-th of them should be equal to the number of shops where Vasiliy will be able to buy a bottle of the drink on the i-th day.

NoteOn the first day, Vasiliy won't be able to buy a drink in any of the shops.On the second day, Vasiliy can buy a drink in the shops 1, 2, 3 and 4.On the third day, Vasiliy can buy a drink only in the shop number 1.Finally, on the last day Vasiliy can buy a drink in any shop.

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

let binary_search arr n value =
    let result =
        (let rec search low high =
            if high = low || high - low = 1
            then
                low
            else
                let mid = (low + high) / 2 in
                if arr.(mid - 1) > value
                then
                    search low mid
                else
                    search mid high
        in search 0 (n + 1))
    in
    print result

let input () =
    let n = read () in
    let price = Array.init n (fun n -> read ()) in
    let m = read () in
    let cash = Array.init m (fun n -> read ()) in
    (n, price, cash)

let solve (n, price, cash) =
    (Array.fast_sort Pervasives.compare price;
    Array.iter (binary_search price n) cash)

let () = solve (input ())

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

let make_array n =
    let arr = Array.make n 0 in
    let rec loop i =
        if i < n
        then
            (let int = read () in
            Array.set arr i int;
            loop (i + 1))
        else
            arr
    in loop 0

let binary_search arr n value =
    let result =
        (let rec search low high =
            if high = low || high - low = 1
            then
                low
            else
                let mid = (low + high) / 2 in
                if arr.(mid - 1) > value
                then
                    search low mid
                else
                    search mid high
        in search 0 (n + 1))
    in
    print result

let input () =
    let n = read () in
    let price = make_array n in
    let m = read () in
    let cash = make_array m in
    (n, price, cash)

let solve (n, price, cash) =
    (Array.fast_sort Pervasives.compare price;
    Array.iter (binary_search price n) cash)

let () = solve (input ())

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

let make_array n =
    let arr = Array.make n 0 in
    let rec loop i =
        if i < n
        then
            (let int = read () in
            Array.set arr i int;
            loop (i + 1))
        else
            arr
    in loop 0

let binary_search arr n value =
    let result =
        (let rec search low high contender =
            assert (high >= low);
            if high = low
            then
                if arr.(low) <= value
                then
                    low
                else
                    contender
            else
                let mid = (low + high) / 2 in
                if arr.(mid) > value
                then
                    if mid = low
                    then
                        contender
                    else
                        search low (mid - 1) contender
                else
                    search (mid + 1) high mid

        in search 0 (n - 1) (-1))
    in
    print (result + 1)

let input () =
    let n = read () in
    let price = make_array n in
    let m = read () in
    let cash = make_array m in
    (n, price, cash)

let solve (n, price, cash) =
    (Array.fast_sort Pervasives.compare price;
    Array.iter (binary_search price n) cash)

let () = solve (input ())

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

let count_smaller ele shop_arr n =
  let print n = Printf.printf "%d\n" n in
  if ele < shop_arr.(0) then print 0
  else if ele >= shop_arr.(n-1) then print n
  else
    let rec bsearch low high =
      if low + 1 = high then print high
      else
        let mid = (low + high) / 2 in
        if ele < shop_arr.(mid) then bsearch low mid
        else if ele >= shop_arr.(mid) then bsearch mid high
    in bsearch 0 (n - 1)

let () =
  let n = read_int () in
  let shop_arr = Array.init n (fun _ ->
      read_int ();
    )
  in
  let q = read_int () in
  let coin_arr = Array.init q (fun _ ->
      read_int ();
    )
  in
  Array.sort compare shop_arr;
  Array.iter (fun x -> count_smaller x shop_arr n) coin_arr

open Printf
open Scanf

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

  Array.sort compare x;

  let find_index m =
  (* return the index of the biggest shop whose cost is <= m *)
  (* -1 if there is no such one *)
    let rec bsearch lo hi =
      (* x.(lo) <= m   x.(hi) > m *)
      if lo + 1 = hi then lo else
	let mid = (lo+hi)/2 in
	if x.(mid) <= m then bsearch mid hi else bsearch lo mid
    in
    if x.(n-1) <= m then n-1
    else if x.(0) > m then -1
    else bsearch 0 (n-1)
  in

  for j=0 to q-1 do
    let i = find_index m.(j) in
    printf "%d\n" (i+1)
  done

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

let count_smaller ele shop_arr n =
  let print n = Printf.printf "%d\n" n in
  if ele < shop_arr.(0) then print 0
  else if ele >= shop_arr.(n-1) then print n
  else
    let rec bsearch low high =
      if low + 1 = high then print high
      else
        let mid = (low + high) / 2 in
        if ele < shop_arr.(mid) then bsearch low mid
        else if ele > shop_arr.(mid) then bsearch mid high
        else print (mid + 1)
    in bsearch 0 (n - 1)

let () =
  let n = read_int () in
  let shop_arr = Array.init n (fun _ ->
      read_int ();
    )
  in
  let q = read_int () in
  let coin_arr = Array.init q (fun _ ->
      read_int ();
    )
  in
  Array.sort compare shop_arr;
  Array.iter (fun x -> count_smaller x shop_arr n) coin_arr


Polycarpus has a computer with n processors. Also, his computer has n memory cells. We'll consider the processors numbered by integers from 1 to n and that the memory cells are consecutively numbered by integers from 1 to n.Polycarpus needs to come up with a parallel program model. For each memory cell number i this program must record the value n - i to this cell. In other words, for each cell you've got to find the distance to cell n.Let's denote the value that is written in the i-th cell as ai. Initially, ai = 1 (1 ≤ i < n) and an = 0. We will consider that only processor i can write values in the memory cell number i. All processors can read an information from some cell (several processors can read an information from some cell simultaneously).The parallel program is executed in several steps. During each step we execute the parallel version of the increment operation. Executing the parallel version of the increment operation goes as follows:  Each processor independently of the other ones chooses some memory cell. Let's say that processor i has chosen a cell with number ci (1 ≤ ci ≤ n).  All processors simultaneously execute operation ai = ai + aci. Help Polycarpus come up with the parallel program model that is executed in exactly k steps. Calculate the operations that need to be executed. Note that after k steps for all i's value ai must be equal n - i.

The first line contains two space-separated integers n and k (1 ≤ n ≤ 104, 1 ≤ k ≤ 20). It is guaranteed that at the given n and k the required sequence of operations exists.

Print exactly n·k integers in k lines. In the first line print numbers c1, c2, ..., cn (1 ≤ ci ≤ n) for the first increment operation. In the second line print the numbers for the second increment operation. In the k-th line print the numbers for the k-th increment operation. As a result of the printed operations for any i value ai must equal n - i.

(* CF ParallelProg 291D *)
open Filename;;
open Str;;

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

let cout = open_out "output.txt";;
let gr () = Scanf.bscanf cin " %d" (fun i -> i);;
let grf () = Scanf.scanf " %f" (fun i -> i);;
let grl () = Scanf.scanf " %Ld" (fun i -> i);;
let rdln() = input_line Pervasives.stdin;;
(* let spli s = Str.split (Str.regexp " ") s;; *)
let printlisti li = List.iter (fun i -> (print_int i; print_string " ")) li;;
let printlists ls = List.iter (fun s -> (print_string (s ^ " "))) ls;;
let printlisti64 li = List.iter (fun i -> (print_string (Int64.to_string i); print_string " ")) li;;
let printarri ai = Array.iter (fun i -> (print_int i; print_string " ")) ai;;
let rec readlist n acc = match n with
	| 0 -> List.rev acc
	| _ -> readlist (n -1) (gr() :: acc);;
let debug = false;;

let n = gr();;
let k = gr();;

let a = Array.make (n+1) 1;;
let anxt = Array.make (n+1) 0;;
let c = Array.make (n+1) 0;;

let init = ( a.(n) <- 0 );;

let rec findgood i ii =
	if ii > n then i
	else if a.(i) = n-i then n
	else if a.(i)*2 > n-i then n - (n-i - a.(i))
	else i;;

let step () =
	begin
		for i=1 to n do
			let k = findgood i i in
			(
				c.(i) <- k;
				anxt.(i) <- a.(i) + a.(k);
				print_int k;
				print_string " "
			)
		done;
		for i=1 to n do
			a.(i) <- anxt.(i)
		done;
		print_string "\n"
	end;;

let main() =
	for i = 1 to k do
		step()
	done;;

main ();;