Polycarpus adores TV series. Right now he is ready to finish watching a season of a popular sitcom "Graph Theory". In total, the season has n episodes, numbered with integers from 1 to n.Polycarpus watches episodes not one by one but in a random order. He has already watched all the episodes except for one. Which episode has Polycaprus forgotten to watch?

The first line of the input contains integer n (2 ≤ n ≤ 100000) — the number of episodes in a season. Assume that the episodes are numbered by integers from 1 to n. The second line contains n - 1 integer a1, a2, ..., an (1 ≤ ai ≤ n) — the numbers of episodes that Polycarpus has watched. All values of ai are distinct.

Print the number of the episode that Polycarpus hasn't watched.

let read_int()=Scanf.scanf "%d " (fun x->x);;
let n=read_int();;
let tab=Array.make (n+1) true;;
for i=1 to n-1 do
   let x=read_int() in
   tab.(x)<-false
done;;
for i=1 to n do
   if tab.(i) then print_int i
done;;


In one one-dimensional world there are n platforms. Platform with index k (platforms are numbered from 1) is a segment with coordinates [(k - 1)m, (k - 1)m + l], and l < m. Grasshopper Bob starts to jump along the platforms from point 0, with each jump he moves exactly d units right. Find out the coordinate of the point, where Bob will fall down. The grasshopper falls down, if he finds himself not on the platform, but if he finds himself on the edge of the platform, he doesn't fall down.

The first input line contains 4 integer numbers n, d, m, l (1 ≤ n, d, m, l ≤ 106, l < m) — respectively: amount of platforms, length of the grasshopper Bob's jump, and numbers m and l needed to find coordinates of the k-th platform: [(k - 1)m, (k - 1)m + l].

Output the coordinates of the point, where the grosshopper will fall down. Don't forget that if Bob finds himself on the platform edge, he doesn't fall down.

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

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

let () =
  let n = read_int64 0 in
  let d = read_int64 0 in
  let m = read_int64 0 in
  let l = read_int64 0 in
  let rec go i =
    let x = ((i**m++l)//d++1L)**d in
    if i = n--1L || x < (i++1L)**m then
      Printf.printf "%Ld\n" x
    else
      go (i++1L)
  in
  go 0L


A film festival is coming up in the city N. The festival will last for exactly n days and each day will have a premiere of exactly one film. Each film has a genre — an integer from 1 to k.On the i-th day the festival will show a movie of genre ai. We know that a movie of each of k genres occurs in the festival programme at least once. In other words, each integer from 1 to k occurs in the sequence a1, a2, ..., an at least once.Valentine is a movie critic. He wants to watch some movies of the festival and then describe his impressions on his site.As any creative person, Valentine is very susceptive. After he watched the movie of a certain genre, Valentine forms the mood he preserves until he watches the next movie. If the genre of the next movie is the same, it does not change Valentine's mood. If the genres are different, Valentine's mood changes according to the new genre and Valentine has a stress.Valentine can't watch all n movies, so he decided to exclude from his to-watch list movies of one of the genres. In other words, Valentine is going to choose exactly one of the k genres and will skip all the movies of this genre. He is sure to visit other movies.Valentine wants to choose such genre x (1 ≤ x ≤ k), that the total number of after-movie stresses (after all movies of genre x are excluded) were minimum.

The first line of the input contains two integers n and k (2 ≤ k ≤ n ≤ 105), where n is the number of movies and k is the number of genres. The second line of the input contains a sequence of n positive integers a1, a2, ..., an (1 ≤ ai ≤ k), where ai is the genre of the i-th movie. It is guaranteed that each number from 1 to k occurs at least once in this sequence.

Print a single number — the number of the genre (from 1 to k) of the excluded films. If there are multiple answers, print the genre with the minimum number.

NoteIn the first sample if we exclude the movies of the 1st genre, the genres 2, 3, 2, 3, 3, 3 remain, that is 3 stresses; if we exclude the movies of the 2nd genre, the genres 1, 1, 3, 3, 3, 1, 1, 3 remain, that is 3 stresses; if we exclude the movies of the 3rd genre the genres 1, 1, 2, 2, 1, 1 remain, that is 2 stresses.In the second sample whatever genre Valentine excludes, he will have exactly 3 stresses.

(* Codeforces 250C done *)

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

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

let rec removedoubles l = match l with
	| [] -> []
	| h :: [] -> h :: []
	| a :: b :: t ->
			let tl = removedoubles (b :: t) in
			if a = b then tl
			else a :: tl;;

let rec countStress l cnts = match l with
	| [] -> ()
	| h :: [] -> ()
	| a :: b :: [] -> cnts.(b) <- cnts.(b) + 1
	| a :: b :: c :: t ->
			begin
				if a <> c then
					cnts.(b) <- cnts.(b) + 1
				else cnts.(b) <- cnts.(b) + 2;
				countStress (b :: c :: t) cnts
			end;;

let countStressF l cnts =
	let a = List.hd l in begin
		cnts.(a) <- cnts.(a) + 1;
		countStress l cnts
	end;;

let rec findBest cnts i k imx mx =
	if i > k then imx
	else if cnts.(i) > mx then findBest cnts (i +1) k i cnts.(i)
	else findBest cnts (i +1) k imx mx;;

let main () =
	let n = gr() in
	let k = gr() in
	let a = readlist n [] in
	let rva = List.rev a in
	begin
		if debug then begin
			print_int n;
			print_int k;
			printlisti rva
		end;
		let cnts = Array.make (k +1) 0 in
		let anodub = removedoubles rva in
		begin
			if debug then begin
				print_string "anodub:\n";
				printlisti anodub
			end;
			countStressF anodub cnts;
			if debug then begin
				print_string "  countStress passed\n";
				printarri cnts;
				print_string "counting and print best\n"
			end;
			let best = findBest cnts 0 k 0 0 in
			print_int best
		end end;;

main();;

(* Codeforces 250C done *)

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

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

let rec removedoubles l = match l with
	| [] -> []
	| h :: [] -> h :: []
	| a :: b :: t ->
			let tl = removedoubles (b :: t) in
			if a = b then tl
			else a :: tl;;

let rec countStress l cnts first = match l with
	| [] -> ()
	| h :: [] -> ()
	| a :: b :: [] -> cnts.(b) <- cnts.(b) + 1
	| a :: b :: c :: t ->
			begin
				if first then cnts.(a) <- cnts.(a) + 1;
				if a <> c then
					cnts.(b) <- cnts.(b) + 1
					else cnts.(b) <- cnts.(b) + 2;
				countStress (b :: c :: t) cnts false
			end;;

let rec findBest cnts i k imx mx =
	if i > k then imx
	else if cnts.(i) > mx then findBest cnts (i +1) k i cnts.(i)
			                  else findBest cnts (i +1) k imx mx;;

let main () =
	let n = gr() in
	let k = gr() in
	let a = readlist n [] in
	let rva = List.rev a in
	begin
		(* print_int n;    *)
		(* print_int k;    *)
		(* printlisti rva; *)
	let cnts = Array.make (k+1) 0 in
	let anodub = removedoubles rva in
	begin
		(* print_string "anodub:\n"; *)
		(* printlisti anodub;        *)
		countStress anodub cnts true;
		(* print_string "  countStress passed\n";    *)
		(* printarri cnts;                           *)
		(* print_string "counting and print best\n"; *)
		let best = findBest cnts 0 k 0 0 in
		print_int best
	end end;;

main();;


Alice and Bob play a game. They have a binary string $$$s$$$ (a string such that each character in it is either $$$0$$$ or $$$1$$$). Alice moves first, then Bob, then Alice again, and so on.During their move, the player can choose any number (not less than one) of consecutive equal characters in $$$s$$$ and delete them.For example, if the string is $$$10110$$$, there are $$$6$$$ possible moves (deleted characters are bold):  $$$\textbf{1}0110 \to 0110$$$;  $$$1\textbf{0}110 \to 1110$$$;  $$$10\textbf{1}10 \to 1010$$$;  $$$101\textbf{1}0 \to 1010$$$;  $$$10\textbf{11}0 \to 100$$$;  $$$1011\textbf{0} \to 1011$$$. After the characters are removed, the characters to the left and to the right of the removed block become adjacent. I. e. the following sequence of moves is valid: $$$10\textbf{11}0 \to 1\textbf{00} \to 1$$$.The game ends when the string becomes empty, and the score of each player is the number of $$$1$$$-characters deleted by them.Each player wants to maximize their score. Calculate the resulting score of Alice.

The first line contains one integer $$$T$$$ ($$$1 \le T \le 500$$$) — the number of test cases. Each test case contains exactly one line containing a binary string $$$s$$$ ($$$1 \le |s| \le 100$$$).

For each test case, print one integer — the resulting score of Alice (the number of $$$1$$$-characters deleted by her).

NoteQuestions about the optimal strategy will be ignored.

(* 给定一个 0、1 构成的串，每个玩家每次可删除任意长度的一个连续字串 *)
(* 每删除一个 1，获得一分，求先手能获得的最大分数 *)

let rec compress =
  let f accu c =
    match accu with
    | [] -> [ (c, 1) ]
    | (prev, count) :: rest ->
        if prev = c then (prev, count + 1) :: rest else (c, 1) :: accu
  in
  List.fold_left f []

let explode str =
  let len = String.length str in
  let rec f accu i = if i = len then accu else f (str.[i] :: accu) (i + 1) in
  f [] 0 |> List.rev

let take =
  let f accu = function '1', c -> c :: accu | _ -> accu in
  List.fold_left f []

let rec sum = function [] -> 0 | [ x ] -> x | x :: y :: rest -> x + sum rest

let main =
  let test_cases = Scanf.scanf " %d" (fun x -> x) in

  for i = 0 to test_cases - 1 do
    let elements = Scanf.scanf " %s" explode |> compress in

    let r = take elements |> List.sort (fun a b -> b - a) |> sum in
    print_endline (string_of_int r)
  done


Some large corporation where Polycarpus works has its own short message service center (SMSC). The center's task is to send all sorts of crucial information. Polycarpus decided to check the efficiency of the SMSC. For that, he asked to give him the statistics of the performance of the SMSC for some period of time. In the end, Polycarpus got a list of n tasks that went to the SMSC of the corporation. Each task was described by the time it was received by the SMSC and the number of text messages to send. More formally, the i-th task was described by two integers ti and ci — the receiving time (the second) and the number of the text messages, correspondingly.Polycarpus knows that the SMSC cannot send more than one text message per second. The SMSC uses a queue to organize its work. Consider a time moment x, the SMSC work at that moment as follows:  If at the time moment x the queue is not empty, then SMSC sends one message from the queue (SMSC gets the message from the head of the queue). Otherwise it doesn't send messages at the time moment x.  If at the time moment x SMSC receives a task, then it adds to the queue all the messages from this task (SMSC adds messages to the tail of the queue). Note, that the messages from the task cannot be send at time moment x. That's because the decision about sending message or not is made at point 1 before adding these messages to the queue. Given the information about all n tasks, Polycarpus wants to count two values: the time when the last text message was sent and the maximum size of the queue at some time. Help him count these two characteristics he needs to evaluate the efficiency of the SMSC.

The first line contains a single integer n (1 ≤ n ≤ 103) — the number of tasks of the SMSC. Next n lines contain the tasks' descriptions: the i-th line contains two space-separated integers ti and ci (1 ≤ ti, ci ≤ 106) — the time (the second) when the i-th task was received and the number of messages to send, correspondingly. It is guaranteed that all tasks were received at different moments of time. It is guaranteed that the tasks are sorted in the chronological order, that is, ti < ti + 1 for all integer i (1 ≤ i < n).

In a single line print two space-separated integers — the time when the last text message was sent and the maximum queue size at a certain moment of time.

NoteIn the first test sample:   second 1: the first message has appeared in the queue, the queue's size is 1;  second 2: the first message is sent, the second message has been received, the queue's size is 1;  second 3: the second message is sent, the queue's size is 0, Thus, the maximum size of the queue is 1, the last message was sent at the second 3.

(* Codeforces contest 292 A - SMS Center *)

open Filename;;

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

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

let n = gr();;  (* size of permutation *)

let rec readpairs n acc = match n with
  | 0 -> List.rev acc
  | _ ->
		let a = gr() in
		let b = gr() in
		readpairs (n -1) ((a,b) :: acc);;

let rec get_maxlen_maxt lprs maxl maxt =
	match lprs with
	| [] -> (maxl, maxt)
	| hd :: tl ->
		let (t,n) = hd in
		let inq = max 0 (maxt - t) in
		let nmaxl = max maxl (inq + n) in
		let nmaxt = t + (inq + n) in
		get_maxlen_maxt tl nmaxl nmaxt;;

let main() =
	let prs = readpairs n [] in
	let (maxl, maxt) = get_maxlen_maxt prs 0 0 in
	(
		print_int maxt;
		print_string " ";
		print_int maxl
		);;

	main();;


Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm.His farm is presented by a rectangle grid with $$$r$$$ rows and $$$c$$$ columns. Some of these cells contain rice, others are empty. $$$k$$$ chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm.Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements:  Each cell of the farm is assigned to exactly one chicken.  Each chicken is assigned at least one cell.  The set of cells assigned to every chicken forms a connected area. More precisely, if two cells $$$(x, y)$$$ and $$$(u, v)$$$ are assigned to the same chicken, this chicken is able to walk from $$$(x, y)$$$ to $$$(u, v)$$$ by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him.

Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 2 \cdot 10^4$$$). Description of the test cases follows. The first line of each test case contains three integers $$$r$$$, $$$c$$$ and $$$k$$$ ($$$1 \leq r, c \leq 100, 1 \leq k \leq 62$$$), representing the size of Long's farm and the number of chickens Long has.  Each of the next $$$r$$$ lines contains $$$c$$$ characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of $$$r \cdot c$$$ over all test cases does not exceed $$$2 \cdot 10^4$$$.

For each test case, print $$$r$$$ lines with $$$c$$$ characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any.

4
3 5 3
..R..
...R.
....R
6 4 6
R..R
R..R
RRRR
RRRR
R..R
R..R
5 5 4
RRR..
R.R..
RRR..
R..R.
R...R
2 31 62
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR

11122
22223
33333
aacc
aBBc
aBBc
CbbA
CbbA
CCAA
11114
22244
32444
33344
33334
abcdefghijklmnopqrstuvwxyzABCDE
FGHIJKLMNOPQRSTUVWXYZ0123456789

NoteThese pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice.In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is $$$0$$$.In the second test case, there are $$$4$$$ chickens with $$$3$$$ cells of rice, and $$$2$$$ chickens with $$$2$$$ cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is $$$3 - 2 = 1$$$.In the third test case, each chicken has $$$3$$$ cells with rice. In the last test case, since there are $$$62$$$ chicken with exactly $$$62$$$ cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.

(* Feeding Chicken
 * https://codeforces.com/contest/1254/problem/A *)

(* I shouldn't need explicitly named record fields. But I wanted to
 * practice the notation. *)
type cell = {coords: int * int; has_rice: bool}
type assignment = {coords: int * int; chicken: int}

(* Converts a board into a list of cells, in snake order. *)
let cells_of_board n m (board: string array) =
  let next (i, j) = match (i mod 2, j) with
    | (0, k) when k = m - 1 -> (i + 1, j)
    | (0, _)                -> (i, j + 1)
    | (_, 0)                -> (i + 1, j)
    | _                     -> (i, j - 1) in
  let rec iter i j acc =
    if i >= n then
      acc
    else
      let (ni, nj) = next (i, j) in
      iter ni nj ({coords =  (i, j); has_rice = String.get board.(i) j = 'R'} :: acc)
  in iter 0 0 []

(* Converts a bunch of chicken assignments to an array of 'bytes' arrays representing
 * the result. *)
let result_of_assns n m (assns : assignment list) =
  let chicken_labels =
    "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM" in
  (* The following didn't work! It created n references to the same array.
     It's like doing "arr = [[0] * m] * n" in Python.
     let res = Array.make n (Bytes.make m '0') in *)
     let res = Array.init n (fun _ -> Bytes.make m '0') in
  (* Ugh, imperative updates... but this is probably more efficient. *)
  let rec iter ls = match ls with
    | [] -> ()
    | {coords=(i, j); chicken = c} :: lss ->
      (Bytes.set res.(i) j (String.get chicken_labels c);
       iter lss)
  in (iter assns; res)

(* Takes a 'board' (2D array showing where the rice is) and
 * converts it to a board of chicken assignments. *)
let solve n m chicks (board: string array) =
  let cells = cells_of_board n m board in
  (* There's probably a function to do this in one go. *)
  let rice_total = List.fold_left
      (fun acc cl -> if cl.has_rice then acc + 1 else acc)
      0 cells
  in
  (* The amount of rice each chicken should have. *)
  let amt = rice_total / chicks in
  (* These are hungry chickens! They will eat amt+1 rice. *)
  let hungry = rice_total - amt * chicks in
  (* Iterate through cells and assign them chickens in order.
   * Assign the hungry chickens first, then the rest. *)
  let rec iter cls prev_chick k acc =
    match cls with
    | [] -> acc
    | {coords=crd; has_rice=hr} :: clss ->
      let mx = if prev_chick < hungry then amt + 1 else amt in
      let nk' = if hr then k + 1 else k in
      let (cur_chick, nk) = if nk' > mx then
          (prev_chick + 1, 1)
        else
          (prev_chick, nk') in
      iter clss cur_chick nk ({coords=crd; chicken=cur_chick} :: acc)
  in result_of_assns n m (iter cells 0 0 [])

let rec rep n f = if n = 0 then () else (f (); rep (n-1) f)

let main () =
  let tt = read_int () in
  rep tt (fun () ->
      (* Scanf.scanf caused Codeforces to give a runtime error for some reason *)
      let [n; m; k] =
        read_line ()
        |> Str.split (Str.regexp " ")
        |> List.map int_of_string in
      let board = Array.init n (fun _ -> read_line ()) in
      Array.iter (fun s -> (print_bytes s; print_newline ())) (solve n m k board)
    )

let _ = main ()

(* Feeding Chicken
 * https://codeforces.com/contest/1254/problem/A *)

(* I shouldn't need explicitly named record fields. But I wanted to
 * practice the notation. *)
type cell = {coords: int * int; has_rice: bool}
type assignment = {coords: int * int; chicken: int}

(* Converts a board into a list of cells, in snake order. *)
let cells_of_board n m (board: string array) =
  let next (i, j) = match (i mod 2, j) with
    | (0, k) when k = m - 1 -> (i + 1, j)
    | (0, _)                -> (i, j + 1)
    | (_, 0)                -> (i + 1, j)
    | _                     -> (i, j - 1) in
  let rec iter i j acc =
    if i >= n then
      acc
    else
      let (ni, nj) = next (i, j) in
      iter ni nj ({coords =  (i, j); has_rice = String.get board.(i) j = 'R'} :: acc)
  in iter 0 0 []

(* Converts a bunch of chicken assignments to an array of 'bytes' arrays representing
 * the result. *)
let result_of_assns n m (assns : assignment list) =
  let chicken_labels =
    "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM" in
  (* The following didn't work! It created n references to the same array.
     It's like doing "arr = [[0] * m] * n" in Python.
     let res = Array.make n (Bytes.make m '0') in *)
     let res = Array.init n (fun _ -> Bytes.make m '0') in
  (* Ugh, imperative updates... but this is probably more efficient. *)
  let rec iter ls = match ls with
    | [] -> ()
    | {coords=(i, j); chicken = c} :: lss ->
      (Bytes.set res.(i) j (String.get chicken_labels c);
       iter lss)
  in (iter assns; res)

(* Takes a 'board' (2D array showing where the rice is) and
 * converts it to a board of chicken assignments. *)
let solve n m chicks (board: string array) =
  let cells = cells_of_board n m board in
  (* There's probably a function to do this in one go. *)
  let rice_total = List.fold_left
      (fun acc cl -> if cl.has_rice then acc + 1 else acc)
      0 cells
  in
  (* The amount of rice each chicken should have. *)
  let amt = rice_total / chicks in
  (* These are hungry chickens! They will eat amt+1 rice. *)
  let hungry = rice_total - amt * chicks in
  (* Iterate through cells and assign them chickens in order.
   * Assign the hungry chickens first, then the rest. *)
  let rec iter cls prev_chick k acc =
    match cls with
    | [] -> acc
    | {coords=crd; has_rice=hr} :: clss ->
      let mx = if prev_chick < hungry then amt + 1 else amt in
      let nk' = if hr then k + 1 else k in
      let (cur_chick, nk) = if nk' > mx then
          (prev_chick + 1, 1)
        else
          (prev_chick, nk') in
      iter clss cur_chick nk ({coords=crd; chicken=cur_chick} :: acc)
  in result_of_assns n m (iter cells 0 0 [])

let rec rep n f = if n = 0 then () else (f (); rep (n-1) f)

let main () =
  let tt = read_int () in
  rep tt (fun () ->
      (* Scanf.scanf caused Codeforces to give a runtime error for some reason *)
      let (n, m, k) = Scanf.sscanf (read_line ()) "%d %d %d" (fun x y z -> (x, y, z)) in
      let board = Array.init n (fun _ -> read_line ()) in
      Array.iter (fun s -> (print_bytes s; print_newline ())) (solve n m k board)
    )

let _ = main ()

(* Feeding Chicken
 * https://codeforces.com/contest/1254/problem/A *)

(* I shouldn't need explicitly named record fields. But I wanted to
 * practice the notation. *)
type cell = {coords: int * int; has_rice: bool}
type assignment = {coords: int * int; chicken: int}

(* Converts a board into a list of cells, in snake order. *)
let cells_of_board n m (board: string array) =
  let next (i, j) = match (i mod 2, j) with
    | (0, k) when k = m - 1 -> (i + 1, j)
    | (0, _)                -> (i, j + 1)
    | (_, 0)                -> (i + 1, j)
    | _                     -> (i, j - 1) in
  let rec iter i j acc =
    if i >= n then
      acc
    else
      let (ni, nj) = next (i, j) in
      iter ni nj ({coords =  (i, j); has_rice = String.get board.(i) j = 'R'} :: acc)
  in iter 0 0 []

(* Converts a bunch of chicken assignments to an array of 'bytes' arrays representing
 * the result. *)
let result_of_assns n m (assns : assignment list) =
  let chicken_labels =
    "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKKLZXCVBNM" in
  (* The following didn't work! It created n references to the same array.
     It's like doing "arr = [[0] * m] * n" in Python.
     let res = Array.make n (Bytes.make m '0') in *)
     let res = Array.init n (fun _ -> Bytes.make m '0') in
  (* Ugh, imperative updates... but this is probably more efficient. *)
  let rec iter ls = match ls with
    | [] -> ()
    | {coords=(i, j); chicken = c} :: lss ->
      (Bytes.set res.(i) j (String.get chicken_labels c);
       iter lss)
  in (iter assns; res)

(* Takes a 'board' (2D array showing where the rice is) and
 * converts it to a board of chicken assignments. *)
let solve n m chicks (board: string array) =
  let cells = cells_of_board n m board in
  (* There's probably a function to do this in one go. *)
  let rice_total = List.fold_left
      (fun acc cl -> if cl.has_rice then acc + 1 else acc)
      0 cells
  in
  (* The amount of rice each chicken should have. *)
  let amt = rice_total / chicks in
  (* These are hungry chickens! They will eat amt+1 rice. *)
  let hungry = rice_total - amt * chicks in
  (* Iterate through cells and assign them chickens in order.
   * Assign the hungry chickens first, then the rest. *)
  let rec iter cls prev_chick k acc =
    match cls with
    | [] -> acc
    | {coords=crd; has_rice=hr} :: clss ->
      let mx = if prev_chick < hungry then amt + 1 else amt in
      let nk' = if hr then k + 1 else k in
      let (cur_chick, nk) = if nk' > mx then
          (prev_chick + 1, 1)
        else
          (prev_chick, nk') in
      iter clss cur_chick nk ({coords=crd; chicken=cur_chick} :: acc)
  in result_of_assns n m (iter cells 0 0 [])

let rec rep n f = if n = 0 then () else (f (); rep (n-1) f)

let main () =
  let tt = read_int () in
  rep tt (fun () ->
      (* Scanf.scanf caused Codeforces to give a runtime error for some reason *)
      let (n, m, k) = Scanf.sscanf (read_line ()) "%d %d %d" (fun x y z -> (x, y, z)) in
      let board = Array.init n (fun _ -> read_line ()) in
      Array.iter (fun s -> (print_bytes s; print_newline ())) (solve n m k board)
    )

let _ = main ()

(* Feeding Chicken
 * https://codeforces.com/contest/1254/problem/A *)

(* I shouldn't need explicitly named record fields. But I wanted to
 * practice the notation. *)
type cell = {coords: int * int; has_rice: bool}
type assignment = {coords: int * int; chicken: int}

(* Converts a board into a list of cells, in snake order. *)
let cells_of_board n m (board: string array) =
  let next (i, j) = match (i mod 2, j) with
    | (0, k) when k = m - 1 -> (i + 1, j)
    | (0, _)                -> (i, j + 1)
    | (_, 0)                -> (i + 1, j)
    | _                     -> (i, j - 1) in
  let rec iter i j acc =
    if i >= n then
      acc
    else
      let (ni, nj) = next (i, j) in
      iter ni nj ({coords =  (i, j); has_rice = String.get board.(i) j = 'R'} :: acc)
  in iter 0 0 []

(* Converts a bunch of chicken assignments to an array of 'bytes' arrays representing
 * the result. *)
let result_of_assns n m (assns : assignment list) =
  let chicken_labels =
    "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKKLZXCVBNM" in
  (* The following didn't work! It created n references to the same array.
     It's like doing "arr = [[0] * m] * n" in Python.
     let res = Array.make n (Bytes.make m '0') in *)
     let res = Array.init n (fun _ -> Bytes.make m '0') in
  (* Ugh, imperative updates... but this is probably more efficient. *)
  let rec iter ls = match ls with
    | [] -> ()
    | {coords=(i, j); chicken = c} :: lss ->
      (Bytes.set res.(i) j (String.get chicken_labels c);
       iter lss)
  in (iter assns; res)

(* Takes a 'board' (2D array showing where the rice is) and
 * converts it to a board of chicken assignments. *)
let solve n m chicks (board: string array) =
  let cells = cells_of_board n m board in
  (* There's probably a function to do this in one go. *)
  let rice_total = List.fold_left
      (fun acc cl -> if cl.has_rice then acc + 1 else acc)
      0 cells
  in
  (* The amount of rice each chicken should have. *)
  let amt = rice_total / chicks in
  (* These are hungry chickens! They will eat amt+1 rice. *)
  let hungry = rice_total - amt * chicks in
  (* Iterate through cells and assign them chickens in order.
   * Assign the hungry chickens first, then the rest. *)
  let rec iter cls prev_chick k acc =
    match cls with
    | [] -> acc
    | {coords=crd; has_rice=hr} :: clss ->
      let mx = if prev_chick < hungry then amt + 1 else amt in
      let nk' = if hr then k + 1 else k in
      let (cur_chick, nk) = if nk' > mx then
          (prev_chick + 1, 1)
        else
          (prev_chick, nk') in
      iter clss cur_chick nk ({coords=crd; chicken=cur_chick} :: acc)
  in result_of_assns n m (iter cells 0 0 [])

let rec rep n f = if n = 0 then () else (f (); rep (n-1) f)

let main () =
  print_string "asdf"

let _ = main ()


$$$n$$$ people live on the coordinate line, the $$$i$$$-th one lives at the point $$$x_i$$$ ($$$1 \le i \le n$$$). They want to choose a position $$$x_0$$$ to meet. The $$$i$$$-th person will spend $$$|x_i - x_0|$$$ minutes to get to the meeting place. Also, the $$$i$$$-th person needs $$$t_i$$$ minutes to get dressed, so in total he or she needs $$$t_i + |x_i - x_0|$$$ minutes. Here $$$|y|$$$ denotes the absolute value of $$$y$$$.These people ask you to find a position $$$x_0$$$ that minimizes the time in which all $$$n$$$ people can gather at the meeting place.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of test cases. Then the test cases follow. Each test case consists of three lines. The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of people.  The second line contains $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$0 \le x_i \le 10^{8}$$$) — the positions of the people. The third line contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$0 \le t_i \le 10^{8}$$$), where $$$t_i$$$ is the time $$$i$$$-th person needs to get dressed. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, print a single real number — the optimum position $$$x_0$$$. It can be shown that the optimal position $$$x_0$$$ is unique. Your answer will be considered correct if its absolute or relative error does not exceed $$$10^{−6}$$$. Formally, let your answer be $$$a$$$, the jury's answer be $$$b$$$. Your answer will be considered correct if $$$\frac{|a−b|}{max(1,|b|)} \le 10^{−6}$$$.

Note  In the $$$1$$$-st test case there is one person, so it is efficient to choose his or her position for the meeting place. Then he or she will get to it in $$$3$$$ minutes, that he or she need to get dressed.  In the $$$2$$$-nd test case there are $$$2$$$ people who don't need time to get dressed. Each of them needs one minute to get to position $$$2$$$.  In the $$$5$$$-th test case the $$$1$$$-st person needs $$$4$$$ minutes to get to position $$$1$$$ ($$$4$$$ minutes to get dressed and $$$0$$$ minutes on the way); the $$$2$$$-nd person needs $$$2$$$ minutes to get to position $$$1$$$ ($$$1$$$ minute to get dressed and $$$1$$$ minute on the way); the $$$3$$$-rd person needs $$$4$$$ minutes to get to position $$$1$$$ ($$$2$$$ minutes to get dressed and $$$2$$$ minutes on the way).

open Printf
open Scanf

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

let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let read_float _ = bscanf Scanning.stdin " %f " (fun x -> x)
let () =
  let cases = read_int() in
  for case = 1 to cases do
    let n = read_int () in
    let x = Array.init n read_float in
    let t = Array.init n read_float in

    let p = Array.init n (fun i -> (x.(i), t.(i))) in
    Array.sort compare p;
    let x = Array.init n (fun i -> fst p.(i)) in
    let t = Array.init n (fun i -> snd p.(i)) in

    let rec ternary a1 a4 f count =
      if count = 0 then (a1 +. a4) /. 2.0 else
	let a2 = 0.6 *. a1   +.   0.4 *. a4 in
	let a3 = 0.4 *. a1   +.   0.6 *. a4 in
	let v2 = f a2 in
	let v3 = f a3 in
	if v2 < v3 then ternary a1 a3 f (count-1)
	else ternary a2 a4 f (count-1)
    in

    let score x0 =
      maxf 0 (n-1) (fun i -> t.(i) +. (abs_float (x0 -. x.(i))))
    in

    let answer = ternary x.(0) x.(n-1) score 100 in

    printf "%f\n" answer
  done


You've got an array, consisting of n integers a1, a2, ..., an. Also, you've got m queries, the i-th query is described by two integers li, ri. Numbers li, ri define a subsegment of the original array, that is, the sequence of numbers ali, ali + 1, ali + 2, ..., ari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1, b2, ..., bk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1 ≤ x ≤ k), that the following inequation fulfills: b1 ≤ b2 ≤ ... ≤ bx ≥ bx + 1 ≥ bx + 2... ≥ bk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.

The first line contains two integers n and m (1 ≤ n, m ≤ 105) — the number of array elements and the number of queries. The second line contains the sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1 ≤ li ≤ ri ≤ n) — the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.

Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.

let read_int () = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x)
let read_pair () = let u = -1+read_int() in (u,-1+read_int())

let () =
  let n = read_int() in
  let m = read_int() in
  let a = Array.init n (fun _ -> read_int()) in
  let q = Array.init m (fun _ -> read_pair()) in

  let amap = Array.make n 0 in
  let a1 = Array.copy a in

  let rec fill_amap i j = if i=n then j else
    if a.(i-1) <> a.(i) then (
      amap.(i) <- j;
      a1.(j) <- a.(i);
      fill_amap (i+1) (j+1)
    ) else (
      amap.(i) <- amap.(i-1);
      fill_amap (i+1) j
    )
  in

  let n1 = fill_amap 1 1 in

(* now a1.(amap.(i)) = a.(i)  *)
(*
    for i=0 to n-1 do
      Printf.printf "amap.(%d) = %d\n" i amap.(i)
    done;

    for i=0 to n1-1 do
      Printf.printf "a1.(%d) = %d\n" i a1.(i)
    done;
*)

  let nc = n1-1 in

    if nc = 0 then (
          for i = 0 to m-1 do Printf.printf "Yes\n" done
    ) else
  let c = Array.init nc (fun i-> if a1.(i) < a1.(i+1) then 0 else 1) in

  let cmap = Array.make nc 0 in
  let c1 = Array.copy c in

  let rec fill_cmap i j = if i=nc then j else
    if c.(i-1) <> c.(i) then (
      cmap.(i) <- j;
      c1.(j) <- c.(i);
      fill_cmap (i+1) (j+1)
    ) else (
      cmap.(i) <- cmap.(i-1);
      fill_cmap (i+1) j
    )
  in

  let _ = fill_cmap 1 1 in

(*
    for i=0 to nc-1 do
      Printf.printf "cmap.(%d) = %d\n" i cmap.(i)
    done;

    for i=0 to n2-1 do
      Printf.printf "c1.(%d) = %d\n" i c1.(i)
    done;
*)
    for i = 0 to m-1 do
      let answer =
	let (l,r) = q.(i) in
	let (l,r) = (amap.(l), amap.(r)) in
(*	  Printf.printf "1: (l,r) = (%d,%d)\n" l r; *)
	  if l=r || l=r-1 then "Yes" else (
	    let r = r-1 in
	    let (l,r) = (cmap.(l), cmap.(r)) in
(*	      Printf.printf "2: (l,r) = (%d,%d)\n" l r; *)
	      if (l=r) || (l=r-1 && c1.(l) = 0) then "Yes" else "No"
	  )
      in
	Printf.printf "%s\n" answer
    done

let read_int () = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x)
let read_pair () = let u = -1+read_int() in (u,-1+read_int())

let () =
  let n = read_int() in
  let m = read_int() in
  let a = Array.init n (fun _ -> read_int()) in
  let q = Array.init m (fun _ -> read_pair()) in

  let amap = Array.make n 0 in
  let a1 = Array.copy a in

  let rec fill_amap i j = if i=n then j else
    if a.(i-1) <> a.(i) then (
      amap.(i) <- j;
      a1.(j) <- a.(i);
      fill_amap (i+1) (j+1)
    ) else (
      amap.(i) <- amap.(i-1);
      fill_amap (i+1) j
    )
  in

  let n1 = fill_amap 1 1 in

(* now a1.(amap.(i)) = a.(i)  *)
(*
    for i=0 to n-1 do
      Printf.printf "amap.(%d) = %d\n" i amap.(i)
    done;

    for i=0 to n1-1 do
      Printf.printf "a1.(%d) = %d\n" i a1.(i)
    done;
*)

  let nc = n1-1 in

    if nc = 0 then (
      Printf.printf "Yes\n";
    ) else
  let c = Array.init nc (fun i-> if a1.(i) < a1.(i+1) then 0 else 1) in

  let cmap = Array.make nc 0 in
  let c1 = Array.copy c in

  let rec fill_cmap i j = if i=nc then j else
    if c.(i-1) <> c.(i) then (
      cmap.(i) <- j;
      c1.(j) <- c.(i);
      fill_cmap (i+1) (j+1)
    ) else (
      cmap.(i) <- cmap.(i-1);
      fill_cmap (i+1) j
    )
  in

  let _ = fill_cmap 1 1 in

(*
    for i=0 to nc-1 do
      Printf.printf "cmap.(%d) = %d\n" i cmap.(i)
    done;

    for i=0 to n2-1 do
      Printf.printf "c1.(%d) = %d\n" i c1.(i)
    done;
*)
    for i = 0 to m-1 do
      let answer =
	let (l,r) = q.(i) in
	let (l,r) = (amap.(l), amap.(r)) in
(*	  Printf.printf "1: (l,r) = (%d,%d)\n" l r; *)
	  if l=r || l=r-1 then "Yes" else (
	    let r = r-1 in
	    let (l,r) = (cmap.(l), cmap.(r)) in
(*	      Printf.printf "2: (l,r) = (%d,%d)\n" l r; *)
	      if (l=r) || (l=r-1 && c1.(l) = 0) then "Yes" else "No"
	  )
      in
	Printf.printf "%s\n" answer
    done


Twilight Sparkle once got a crystal from the Crystal Mine. A crystal of size n (n is odd; n > 1) is an n × n matrix with a diamond inscribed into it.You are given an odd integer n. You need to draw a crystal of size n. The diamond cells of the matrix should be represented by character "D". All other cells of the matrix should be represented by character "*". Look at the examples to understand what you need to draw.

The only line contains an integer n (3 ≤ n ≤ 101; n is odd).

***D***
**DDD**
*DDDDD*
DDDDDDD
*DDDDD*
**DDD**
***D***

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

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

module Array = ArrayLabels ;;
module List  = ListLabels ;;

let () =
  sf "%d " (fun n ->
    for i = 0 to n / 2 do
      for j = 0 to n - 1 do
        pf (if n / 2 - i <= j && j <= n / 2 + i then "D" else "*");
      done;
      pf "\n";
    done;
    for i = 1 to n / 2 do
      for j = 0 to n - 1 do
        pf (if i <= j && j <= n - 1 - i then "D" else "*");
      done;
      pf "\n";
    done)
;;


Vasya came up with a password to register for EatForces — a string $$$s$$$. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits.But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string $$$s$$$ is a string $$$x = s_l s_{l + 1} \dots s_{l + len - 1} (1 \le l \le |s|, 0 \le len \le |s| - l + 1)$$$. $$$len$$$ is the length of the substring. Note that the empty string is also considered a substring of $$$s$$$, it has the length $$$0$$$.Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length.Note that the length of $$$s$$$ should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits.

The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$) — the number of testcases. Each of the next $$$T$$$ lines contains the initial password $$$s~(3 \le |s| \le 100)$$$, consisting of lowercase and uppercase Latin letters and digits. Only $$$T = 1$$$ is allowed for hacks.

For each testcase print a renewed password, which corresponds to given conditions.  The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is $$$0$$$. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" $$$\rightarrow$$$ "a7cdEf" is $$$4$$$, because the changed positions are $$$2$$$ and $$$5$$$, thus $$$(5 - 2) + 1 = 4$$$. It is guaranteed that such a password always exists. If there are several suitable passwords — output any of them.

NoteIn the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1.In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0).

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

let llen l = List.length l
let () =
	let t = get_i64 0 in
	rep 1L t (fun _ ->
		let s = scanf " %s" (fun v -> v) in
		let a = s |> char_list_of_string |> Array.of_list in
		let b = s |> char_list_of_string |> List.mapi (fun i v -> (i,v)) |> Array.of_list in
		let ld,lu,ln = Array.fold_left (fun (d,u,n) (i,v) ->
			if '0' <= v && v <= '9' then (d,u,(i,v)::n)
			else if 'a' <= v && v <= 'z' then ((i,v)::d,u,n)
			else if 'A' <= v && v <= 'Z' then (d,(i,v)::u,n)
			else (d,u,n)
		)	([],[],[]) b
		in
			(* let (i,p),(j,q),(k,r) = List.hd ld,List.hd lu,List.hd ln in *)
			let i,j,k = llen ld,llen lu,llen ln in
			let ld,lu,ln = ref ld,ref lu,ref ln in
			(* printf "%d %d %d\n" i j k; *)
			let i,j,k = ref i,ref j,ref k in
			if !i = 0 then (
				if !j >= 2 then (
					let w,_ = List.hd !lu in
					a.(w) <- 'a'; j:=!j-$1; lu := List.tl !lu
				) else if !k >= 2 then (
					let w,_ = List.hd !ln in
					a.(w) <- 'a'; k:=!k-$1; ln := List.tl !ln
				)
			);
			(* printf "%d %d %d\n" !i !j !k; *)
			if !j = 0 then (
				if !k >= 2 then (
					let w,_ = List.hd !ln in
					a.(w) <- 'A'; k:=!k-$1; ln := List.tl !ln
				) else if !i >= 2 then (
					let w,_ = List.hd !ld in
					a.(w) <- 'A'; i:=!i-$1; ld := List.tl !ld
				)
			);
			(* printf "%d %d %d\n" !i !j !k; *)
			if !k = 0 then (
				if !i >= 2 then (
					let w,_ = List.hd !ld in
					a.(w) <- '3'; i:=!i-$1; ld := List.tl !ld
				) else if !j >= 2 then (
					let w,_ = List.hd !lu in
					a.(w) <- '3'; j:=!j-$1; lu := List.tl !lu
				)
			);
			(* printf "%d %d %d\n" !i !j !k; *)
			a |> Array.to_list |> string_of_list ~separator:"" (String.make 1) |> print_endline; `Ok
	)

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

let llen l = List.length l
let () =
	let t = get_i64 0 in
	rep 1L t (fun _ ->
		let s = scanf " %s" (fun v -> v) in
		let a = s |> char_list_of_string |> Array.of_list in
		let b = s |> char_list_of_string |> List.mapi (fun i v -> (i,v)) |> Array.of_list in
		let ld,lu,ln = Array.fold_left (fun (d,u,n) (i,v) ->
			if '0' <= v && v <= '9' then (d,u,(i,v)::n)
			else if 'a' <= v && v <= 'z' then ((i,v)::d,u,n)
			else if 'A' <= v && v <= 'Z' then (d,(i,v)::u,n)
			else (d,u,n)
		)	([],[],[]) b
		in
			(* let (i,p),(j,q),(k,r) = List.hd ld,List.hd lu,List.hd ln in *)
			let i,j,k = llen ld,llen lu,llen ln in
			let ld,lu,ln = ref ld,ref lu,ref ln in
			printf "%d %d %d\n" i j k;
			let i,j,k = ref i,ref j,ref k in
			if !i = 0 then (
				if !j >= 2 then (
					let w,_ = List.hd !lu in
					a.(w) <- 'a'; j:=!j-$1; lu := List.tl !lu
				) else if !k >= 2 then (
					let w,_ = List.hd !ln in
					a.(w) <- 'a'; k:=!k-$1; ln := List.tl !ln
				)
			);
			printf "%d %d %d\n" !i !j !k;
			if !j = 0 then (
				if !k >= 2 then (
					let w,_ = List.hd !ln in
					a.(w) <- 'A'; k:=!k-$1; ln := List.tl !ln
				) else if !i >= 2 then (
					let w,_ = List.hd !ld in
					a.(w) <- 'A'; i:=!i-$1; ld := List.tl !ld
				)
			);
			printf "%d %d %d\n" !i !j !k;
			if !k = 0 then (
				if !i >= 2 then (
					let w,_ = List.hd !ld in
					a.(w) <- '3'; i:=!i-$1; ld := List.tl !ld
				) else if !j >= 2 then (
					let w,_ = List.hd !lu in
					a.(w) <- '3'; j:=!j-$1; lu := List.tl !lu
				)
			);
			printf "%d %d %d\n" !i !j !k;
			a |> Array.to_list |> string_of_list ~separator:"" (String.make 1) |> print_endline; `Ok
	)

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

let llen l = List.length l
let () =
	let t = get_i64 0 in
	rep 1L t (fun _ ->
		let s = scanf " %s" (fun v -> v) in
		let a = s |> char_list_of_string |> Array.of_list in
		let b = s |> char_list_of_string |> List.mapi (fun i v -> (i,v)) |> Array.of_list in
		let ld,lu,ln = Array.fold_left (fun (d,u,n) (i,v) ->
			if '0' <= v && v <= '9' then (d,u,(i,v)::n)
			else if 'a' <= v && v <= 'z' then ((i,v)::d,u,n)
			else if 'A' <= v && v <= 'Z' then (d,(i,v)::u,n)
			else (d,u,n)
		)	([],[],[]) b
		in
			(* let (i,p),(j,q),(k,r) = List.hd ld,List.hd lu,List.hd ln in *)
			let i,j,k = llen ld,llen lu,llen ln in
			(* printf "%d %d %d\n" i j k; *)
			let i,j,k = ref i,ref j,ref k in
			if !i = 0 then (
				if !j >= 2 then (
					a.(!i) <- 'a'; j:=!j-$1;
				) else if !k >= 2 then (
					a.(!i) <- 'a'; k:=!k-$1;
				)
			);
			if !j = 0 then (
				if !k >= 2 then (
					a.(!j) <- 'A'; k:=!k-$1;
				) else if !i >= 2 then (
					a.(!j) <- 'A'; i:=!i-$1;
				)
			);
			if !k = 0 then (
				if !i >= 2 then (
					a.(!k) <- '0'; i:=!i-$1;
				) else if !j >= 2 then (
					a.(!k) <- '0'; j:=!j-$1;
				)
			);
		a |> Array.to_list |> string_of_list ~separator:"" (String.make 1) |> print_endline; `Ok
	)


You are given two strings a and b. You have to remove the minimum possible number of consecutive (standing one after another) characters from string b in such a way that it becomes a subsequence of string a. It can happen that you will not need to remove any characters at all, or maybe you will have to remove all of the characters from b and make it empty.Subsequence of string s is any such string that can be obtained by erasing zero or more characters (not necessarily consecutive) from string s.

The first line contains string a, and the second line — string b. Both of these strings are nonempty and consist of lowercase letters of English alphabet. The length of each string is no bigger than 105 characters.

On the first line output a subsequence of string a, obtained from b by erasing the minimum number of consecutive characters. If the answer consists of zero characters, output «-» (a minus sign).

NoteIn the first example strings a and b don't share any symbols, so the longest string that you can get is empty.In the second example ac is a subsequence of a, and at the same time you can obtain it by erasing consecutive symbols cepted from string b.

let print_result s =
	match s with
	| "" ->
		Printf.printf "-\n";
	| _ ->
		Printf.printf "%s\n" s;
;;

let left_arrangements s p =
	let ns = String.length s in
	let np = String.length p in
	let arr = Array.make (ns+1) 0 in
	let kp = ref 0 in
	let js = ref 0 in
	begin
		(*
		Invariant:
			1. forall j' < js, arr[j'] is amount of matches between strings p and s[0..j'-1]
			2. kp - amount of matches between strings p[0..kp-1] and s[0..js-1]
		Base case: 1,2. vacuous truth
		Termination: js == ns, array range of indices j=[0..ns-1] contains amount of matches between p and corresponding substrings s[0..j-1]
				kp - maximal amount of matches between s[0..ns-1] (i.e. the whole string s) and p
		*)
		while !js < ns do

			arr.(!js) <- !kp; (* now arr[!js] contains amount of matches between p and s[0..js-1] *)

			if (!kp < np) && (String.get p !kp) == (String.get s !js) then
			begin
				kp := !kp + 1;
			end;

			js := !js + 1;

		done;

		(* the last value just duplicates amount if matches between the whole strings p and s *)
		arr.(!js) <- !kp;

		arr
	end
;;

let right_arrangements s p =
	let ns = String.length s in
	let np = String.length p in
	let arr = Array.make (ns+1) 0 in
	let kp = ref np in
	let js = ref ns in
	begin

		(*
		Invariant:
			1. forall j' > js, arr[j'] is amount of matches between strings p and s[j'..ns-1]
			2. kp - position of the last match (if any) in string p or np otherwise.
		Base case: 1,2. vacuous truth
		Termination: js == 0, array range of indices j=[1..ns-1] contains amount of matches between p and corresponding substrings s[j..ns-1]
					arr[ns] contains 0 as there is no matches beyond string length
				kp - maximal amount of matches between s[1..ns-1] and p
		*)
		while !js > 0 do

			arr.(!js) <- (np - !kp);

			if (!kp > 0) && (String.get p (!kp-1) ) == (String.get s (!js-1) ) then
			begin
				kp := !kp - 1;
			end;

			js := !js - 1;

		done;

		(* amount of matches between the whole strings p and s *)
		arr.(!js) <- (np - !kp);

		arr;
	end
;;

let process_strings s p =
	let ns = String.length s in
	let np = String.length p in
	let l = left_arrangements s p in
	let r = right_arrangements s p in
	let best_ks = ref 0 in
	let best_sum = ref 0 in
	begin
		for ks = 0 to ns do
			let sum = l.(ks) + r.(ks) in
			if sum > !best_sum then
				begin
					best_sum := sum;
					best_ks := ks;
				end;
		done;

		let res = if !best_sum <= np
				then (String.sub p 0 l.(!best_ks)) ^ (String.sub p (np - r.(!best_ks)) r.(!best_ks))
				else p in
		print_result res;
	end
;;

let read_input () =
	Scanf.bscanf Scanf.Scanning.stdin " %s %s " process_strings
;;

read_input ()

let print_result s =
	match s with
	| "" ->
		Printf.printf "-\n";
	| _ ->
		Printf.printf "%s\n" s;
;;

let left_subsequence_lengths a b =

	let n = String.length a in
	let m = String.length b in
	let l = Array.make (n+1) 0 in
	let j = ref 0 in
	begin

	l.(0) <- !j;

	for k=0 to (n-1) do

		if (!j < m) && ((String.get a k) == (String.get b !j))  then
			begin
			j := !j + 1;
			end;

		l.(k+1) <- !j; (* now l[!js] contains amount of matches between b and a[0..js-1] *)

	done;

	l
	end
;;

let right_subsequence_lengths a b =
	let n = String.length a in
	let m = String.length b in
	let r = Array.make (n+1) 0 in
	let j = ref m in
	begin

	r.(n) <- (m - !j);

	for k = (n-1) downto 0 do

		if (!j > 0) && ((String.get a k ) == (String.get b (!j-1)))  then
			begin
			j := !j - 1;
			end;

		r.(k) <- (m - !j);

	done;

	r;
	end
;;

let longest_subsequence a b =
	let n = String.length a in
	let m = String.length b in
	let l = left_subsequence_lengths a b in
	let r = right_subsequence_lengths a b in
	let s_best = ref (l.(0) + r.(0)) in
	let k_best = ref 0 in
	begin
	for k = 1 to n do
		let s = l.(k) + r.(k) in
		if s > !s_best then
			begin
			s_best := s;
			k_best := k;
			end;
	done;

	if !s_best >= m
		then b
		else (String.sub b 0 l.(!k_best)) ^ (String.sub b (m - r.(!k_best)) r.(!k_best))
	end
;;

let read_input () =
	Scanf.bscanf Scanf.Scanning.stdin " %s %s " (fun a b -> print_result (longest_subsequence a b))
;;

read_input ()

let _ =
  let sb = Scanf.Scanning.stdib in
  let a = Scanf.bscanf sb "%s " (fun s -> s) in
  let b = Scanf.bscanf sb "%s " (fun s -> s) in
  let c2i c = Char.code c - Char.code 'a' in
  let n = String.length a
  and m = String.length b in
  let inf = 1000000000 in
  let prev = Array.make_matrix 26 n 0 in
  let next = Array.make_matrix 26 n 0 in
  let p = Array.make 26 inf in
  let p' = Array.make 26 (-1) in
  let _ =
    for i = 0 to n - 1 do
      let c = c2i a.[i] in
	for j = 0 to 25 do
	  prev.(j).(i) <- p'.(j)
	done;
	p'.(c) <- i;
    done;
    for i = n - 1 downto 0 do
      let c = c2i a.[i] in
	for j = 0 to 25 do
	  next.(j).(i) <- p.(j)
	done;
	p.(c) <- i;
    done;
  in
  let d = Array.make m (-1) in
  let pos = ref p.(c2i b.[0]) in
  let _ =
    d.(0) <- !pos;
    for i = 1 to m - 1 do
      if !pos < inf then (
	let c = c2i b.[i] in
	  pos := next.(c).(!pos);
      );
      d.(i) <- !pos;
    done;
  in
  let d' = Array.make m (-1) in
  let _ =
    pos := p'.(c2i b.[m - 1]);
    d'.(m - 1) <- !pos;
    for i = m - 2 downto 0 do
      if !pos >= 0 then (
	let c = c2i b.[i] in
	  pos := prev.(c).(!pos);
      );
      d'.(i) <- !pos;
    done;
  in
    (*for i = 0 to m - 1 do
      Printf.printf "asd %d %d\n" d.(i) d'.(i)
    done;*)
  let bl = ref (-1) in
  let br = ref m in
  let r = ref 0 in
    while !r < m && d'.(!r) < 0 do
      incr r;
    done;
    if !br - !bl > !r - (-1) then (
      br := !r;
      bl := -1;
    );
    for l = 0 to m - 1 do
      while !r < m && (!r <= l || d'.(!r) <= d.(l)) do
	incr r;
      done;
      if d.(l) < inf && !br - !bl > !r - l then (
	br := !r;
	bl := l;
      )
    done;
    (*Printf.printf "qwe %d %d\n" !bl !br;*)
    let res = String.sub b 0 (!bl + 1) ^ String.sub b !br (m - !br) in
      if res = ""
      then Printf.printf "-\n"
      else Printf.printf "%s\n" res

let print_result s =
	match s with
	| "" ->
		Printf.printf "-\n";
	| _ ->
		Printf.printf "%s\n" s;
;;

let left_mismatch s p =
	let ns = String.length s in
	let np = String.length p in
	let kp = ref 0 in
	let js = ref 0 in
	let jl = ref 0 in (* index in s after last match with p *)
	begin
		while (!kp < np) && (!js < ns)
		do
			if (String.get p !kp) == (String.get s !js) then
			begin
				kp := !kp + 1;
				jl := !js + 1;
			end;
			js := !js + 1;
		done; (* for k : (p_b..p_e) *)
		(!kp, !jl)
	end
;;

let right_mismatch s p s_b p_b =
	let ns = String.length s in
	let np = String.length p in
	let kp = ref np in
	let js = ref ns in
	begin
		while (!kp > p_b) && (!js > s_b)
		do
			if (String.get p (!kp - 1) ) == (String.get s (!js - 1) ) then
			begin
				kp := !kp - 1;
			end;
			js := !js - 1;
		done; (* for k : (p_b..p_e) *)
		!kp
	end
;;

let process_strings s p =
	begin
		let m = String.length p in
		let (lp, ls) = left_mismatch s p in
		let rp = right_mismatch s p ls lp in
		let nr = m - rp in
		let res = (String.sub p 0 lp) ^ (String.sub p rp nr) in
		print_result res;
	end
;;

let read_input () =
	Scanf.bscanf Scanf.Scanning.stdin " %s %s " process_strings
;;

read_input ()

let print_result s =
	match s with
	| "" ->
		Printf.printf "-\n";
	| _ ->
		Printf.printf "%s\n" s;
;;

let left_arrangements s p =
	let ns = String.length s in
	let np = String.length p in
	let arr = Array.make ns 0 in
	let kp = ref 0 in
	let js = ref 0 in
	begin
		while (!kp < np) && (!js < ns) do

			arr.(!js) <- !kp;

			if (String.get p !kp) == (String.get s !js) then
			begin
				kp := !kp + 1;
			end;

			js := !js + 1;
		done;

		while !js < ns do
			arr.(!js) <- np;
			js := !js + 1;
		done;

		arr
	end
;;

let right_arrangements s p =
	let ns = String.length s in
	let np = String.length p in
	let arr = Array.make ns 0 in
	let kp = ref np in
	let js = ref ns in
	begin
		while (!kp > 0) && (!js > 0) do

			if (String.get p (!kp-1) ) == (String.get s (!js-1) ) then
			begin
				kp := !kp - 1;
			end;

			js := !js - 1;

			arr.(!js) <- (np - !kp);
		done;

		while (!js > 0) do
			js := !js - 1;
			arr.(!js) <- np;
		done;

		arr;
	end
;;

let process_strings s p =
	let n = String.length s in
	let m = String.length p in
	let l = left_arrangements s p in
	let r = right_arrangements s p in
	let best_k = ref 0 in
	let best_sum = ref 0 in
	begin
		for k = 0 to (n-1) do
			let sum = l.(k) + r.(k) in
			if sum > !best_sum then
				begin
					best_sum := sum;
					best_k := k;
				end;
		done;

		let res = if !best_sum <= m
				then (String.sub p 0 l.(!best_k)) ^ (String.sub p (m - r.(!best_k)) r.(!best_k))
				else p in
		print_result res;
	end
;;

let read_input () =
	Scanf.bscanf Scanf.Scanning.stdin " %s %s " process_strings
;;

read_input ()

let print_result s =
	match s with
	| "" ->
		Printf.printf "-\n";
	| _ ->
		Printf.printf "%s\n" s;
;;

let left_arrangements s p =
	let ns = String.length s in
	let np = String.length p in
	let arr = Array.make ns 0 in
	let kp = ref 0 in
	let js = ref 0 in
	begin
		while (!kp < np) && (!js < ns) do

			arr.(!js) <- !kp;

			if (String.get p !kp) == (String.get s !js) then
			begin
				kp := !kp + 1;
			end;

			js := !js + 1;
		done;

		while !js < ns do
			arr.(!js) <- np;
			js := !js + 1;
		done;

		arr
	end
;;

let right_arrangements s p =
	let ns = String.length s in
	let np = String.length p in
	let arr = Array.make ns 0 in
	let kp = ref np in
	let js = ref ns in
	begin
		while (!kp > 0) && (!js > 0) do

			if (String.get p (!kp-1) ) == (String.get s (!js-1) ) then
			begin
				kp := !kp - 1;
			end;

			js := !js - 1;

			arr.(!js) <- (np - !kp);
		done;

		while (!js > 0) do
			arr.(!js) <- np;
			js := !js - 1;
		done;

		arr;
	end
;;

let process_strings s p =
	let n = String.length s in
	let m = String.length p in
	let l = left_arrangements s p in
	let r = right_arrangements s p in
	let best_k = ref 0 in
	let best_sum = ref 0 in
	begin
		for k = 0 to (n-1) do
			let sum = l.(k) + r.(k) in
			if sum > !best_sum then
				begin
					best_sum := sum;
					best_k := k;
				end;
		done;

		let res = (String.sub p 0 l.(!best_k)) ^ (String.sub p (m - r.(!best_k)) r.(!best_k)) in
		print_result res;
	end
;;

let read_input () =
	Scanf.bscanf Scanf.Scanning.stdin " %s %s " process_strings
;;

read_input ()

let print_result s =
	match s with
	| "" ->
		Printf.printf "-\n";
	| _ ->
		Printf.printf "%s\n" s;
;;

let left_arrangements s p =
	let ns = String.length s in
	let np = String.length p in
	let arr = Array.make ns 0 in
	let kp = ref 0 in
	let js = ref 0 in
	begin
		while (!kp < np) && (!js < ns) do

			arr.(!js) <- !kp;

			if (String.get p !kp) == (String.get s !js) then
			begin
				kp := !kp + 1;
			end;

			js := !js + 1;

		done;

		arr
	end
;;

let right_arrangements s p =
	let ns = String.length s in
	let np = String.length p in
	let arr = Array.make ns 0 in
	let kp = ref np in
	let js = ref ns in
	begin
		while (!kp > 0) && (!js > 0) do

			if (String.get p (!kp-1) ) == (String.get s (!js-1) ) then
			begin
				kp := !kp - 1;
			end;

			js := !js - 1;

			arr.(!js) <- (np - !kp);

		done;

		arr;
	end
;;

let process_strings s p =
	let n = String.length s in
	let m = String.length p in
	let l = left_arrangements s p in
	let r = right_arrangements s p in
	let best_k = ref 0 in
	let best_sum = ref 0 in
	begin
		for k = 0 to (n-1) do
			let sum = l.(k) + r.(k) in
			if sum > !best_sum then
				begin
					best_sum := sum;
					best_k := k;
				end;
		done;

		let res = (String.sub p 0 l.(!best_k)) ^ (String.sub p (m - r.(!best_k)) r.(!best_k)) in
		print_result res;
	end
;;

let read_input () =
	Scanf.bscanf Scanf.Scanning.stdin " %s %s " process_strings
;;

read_input ()

let print_result s =
	match s with
	| "" ->
		Printf.printf "-\n";
	| _ ->
		Printf.printf "%s\n" s;
;;

let left_arrangements s p =
	let ns = String.length s in
	let np = String.length p in
	let arr = Array.make ns 0 in
	let kp = ref 0 in
	let js = ref 0 in
	begin
		while (!kp < np) && (!js < ns) do

			arr.(!js) <- !kp;

			if (String.get p !kp) == (String.get s !js) then
			begin
				kp := !kp + 1;
			end;

			js := !js + 1;
		done;

		while !js < ns do
			arr.(!js) <- np;
			js := !js + 1;
		done;

		arr
	end
;;

let right_arrangements s p =
	let ns = String.length s in
	let np = String.length p in
	let arr = Array.make ns 0 in
	let kp = ref np in
	let js = ref ns in
	begin
		while (!kp > 0) && (!js > 0) do

			if (String.get p (!kp-1) ) == (String.get s (!js-1) ) then
			begin
				kp := !kp - 1;
			end;

			js := !js - 1;

			arr.(!js) <- (np - !kp);
		done;

		while (!js > 0) do
			js := !js - 1;
			arr.(!js) <- np;
		done;

		arr;
	end
;;

let process_strings s p =
	let n = String.length s in
	let m = String.length p in
	let l = left_arrangements s p in
	let r = right_arrangements s p in
	let best_k = ref 0 in
	let best_sum = ref 0 in
	begin
		for k = 0 to (n-1) do
			let sum = l.(k) + r.(k) in
			if sum > !best_sum then
				begin
					best_sum := sum;
					best_k := k;
				end;
		done;

		let res = (String.sub p 0 l.(!best_k)) ^ (String.sub p (m - r.(!best_k)) r.(!best_k)) in
		print_result res;
	end
;;

let read_input () =
	Scanf.bscanf Scanf.Scanning.stdin " %s %s " process_strings
;;

read_input ()

let print_result s =
	match s with
	| "" ->
		Printf.printf "-\n";
	| _ ->
		Printf.printf "%s\n" s;
;;

let left_mismatch s p s_e p_e =
	let kp = ref 0 in
	let js = ref 0 in
	let jl = ref 0 in (* index in s after last match with p *)
	begin
		while (!kp < p_e) && (!js < s_e)
		do
			if (String.get p !kp) == (String.get s !js) then
			begin
				kp := !kp + 1;
				jl := !js + 1;
			end;
			js := !js + 1;
		done; (* for k : (p_b..p_e) *)
		(!kp, !jl)
	end
;;

let right_mismatch s p s_b p_b =
	let ns = String.length s in
	let np = String.length p in
	let kp = ref np in
	let js = ref ns in
	let jl = ref ns in
	begin
		while (!kp > p_b) && (!js > s_b)
		do
			if (String.get p (!kp - 1) ) == (String.get s (!js - 1) ) then
			begin
				kp := !kp - 1;
				jl := !js - 1;
			end;
			js := !js - 1;
		done; (* for k : (p_b..p_e) *)
		(!kp, !jl)
	end
;;

let process_strings s p =
	begin
		let n = String.length s in
		let m = String.length p in
		let (lp1, ls1) = left_mismatch s p n m in
		let (rp1, _) = right_mismatch s p ls1 lp1 in
		let (rp2, rs2) = right_mismatch s p 0 0 in
		let (lp2, _) = left_mismatch s p rs2 rp2 in
		let nr1 = m - rp1 in
		let nr2 = m - rp2 in
		let n1 = lp1 + nr1  in
		let n2 = lp2 + nr2 in
		let res = if (n1 > n2)
			then (String.sub p 0 lp1) ^ (String.sub p rp1 nr1)
			else (String.sub p 0 lp2) ^ (String.sub p rp2 nr2) in
		print_result res;
	end
;;

let read_input () =
	Scanf.bscanf Scanf.Scanning.stdin " %s %s " process_strings
;;

read_input ()

let _ =
  let sb = Scanf.Scanning.stdib in
  let a = Scanf.bscanf sb "%s " (fun s -> s) in
  let b = Scanf.bscanf sb "%s " (fun s -> s) in
  let c2i c = Char.code c - Char.code 'a' in
  let n = String.length a
  and m = String.length b in
  let inf = 1000000000 in
  let prev = Array.make_matrix 26 n 0 in
  let next = Array.make_matrix 26 n 0 in
  let p = Array.make 26 inf in
  let p' = Array.make 26 (-1) in
  let _ =
    for i = 0 to n - 1 do
      let c = c2i a.[i] in
	for j = 0 to 25 do
	  prev.(j).(i) <- p'.(j)
	done;
	p'.(c) <- i;
    done;
    for i = n - 1 downto 0 do
      let c = c2i a.[i] in
	for j = 0 to 25 do
	  next.(j).(i) <- p.(j)
	done;
	p.(c) <- i;
    done;
  in
  let d = Array.make m (-1) in
  let pos = ref p.(c2i b.[0]) in
  let _ =
    d.(0) <- !pos;
    for i = 1 to m - 1 do
      if !pos < inf then (
	let c = c2i b.[i] in
	  pos := next.(c).(!pos);
      );
      d.(i) <- !pos;
    done;
  in
  let d' = Array.make m (-1) in
  let _ =
    pos := p'.(c2i b.[m - 1]);
    d'.(m - 1) <- !pos;
    for i = m - 2 downto 0 do
      if !pos >= 0 then (
	let c = c2i b.[i] in
	  pos := prev.(c).(!pos);
      );
      d'.(i) <- !pos;
    done;
  in
    for i = 0 to m - 1 do
      Printf.printf "asd %d %d\n" d.(i) d'.(i)
    done;
  let bl = ref (-1) in
  let br = ref m in
  let r = ref 0 in
    (*while !r < m && d'.(!r) < 0 do
      incr r;
    done;*)
    if !br - !bl > !r - (-1) then (
      br := !r;
      bl := -1;
    );
    for l = 0 to m - 1 do
      while !r < m && (!r <= l || d'.(!r) <= d.(l)) do
	incr r;
      done;
      if d.(l) < inf && !br - !bl > !r - l then (
	br := !r;
	bl := l;
      )
    done;
    (*Printf.printf "qwe %d %d\n" !bl !br;*)
    let res = String.sub b 0 (!bl + 1) ^ String.sub b !br (m - !br) in
      if res = ""
      then Printf.printf "-\n"
      else Printf.printf "%s\n" res

let _ =
  let sb = Scanf.Scanning.stdib in
  let a = Scanf.bscanf sb "%s " (fun s -> s) in
  let b = Scanf.bscanf sb "%s " (fun s -> s) in
  let c2i c = Char.code c - Char.code 'a' in
  let n = String.length a
  and m = String.length b in
  let inf = 1000000000 in
  let prev = Array.make_matrix 26 n 0 in
  let next = Array.make_matrix 26 n 0 in
  let p = Array.make 26 inf in
  let p' = Array.make 26 (-1) in
  let _ =
    for i = 0 to n - 1 do
      let c = c2i a.[i] in
	for j = 0 to 25 do
	  prev.(j).(i) <- p'.(j)
	done;
	p'.(c) <- i;
    done;
    for i = n - 1 downto 0 do
      let c = c2i a.[i] in
	for j = 0 to 25 do
	  next.(j).(i) <- p.(j)
	done;
	p.(c) <- i;
    done;
  in
  let d = Array.make m (-1) in
  let pos = ref p.(c2i b.[0]) in
  let _ =
    d.(0) <- !pos;
    for i = 1 to m - 1 do
      if !pos < inf then (
	let c = c2i b.[i] in
	  pos := next.(c).(!pos);
      );
      d.(i) <- !pos;
    done;
  in
  let d' = Array.make m (-1) in
  let _ =
    pos := p'.(c2i b.[m - 1]);
    d'.(m - 1) <- !pos;
    for i = m - 2 downto 0 do
      if !pos >= 0 then (
	let c = c2i b.[i] in
	  pos := prev.(c).(!pos);
      );
      d'.(i) <- !pos;
    done;
  in
    (*for i = 0 to m - 1 do
      Printf.printf "asd %d %d\n" d.(i) d'.(i)
    done;*)
  let bl = ref (-1) in
  let br = ref m in
  let r = ref 0 in
    for l = 0 to m - 1 do
      while !r < m && d'.(!r) <= d.(l) do
	incr r;
      done;
      if d.(l) < inf && !br - !bl > !r - l then (
	br := !r;
	bl := l;
      )
    done;
    (*Printf.printf "qwe %d %d\n" !bl !br;*)
    let res = String.sub b 0 (!bl + 1) ^ String.sub b !br (m - !br) in
      if res = ""
      then Printf.printf "-\n"
      else Printf.printf "%s\n" res

let _ =
  let sb = Scanf.Scanning.stdib in
  let a = Scanf.bscanf sb "%s " (fun s -> s) in
  let b = Scanf.bscanf sb "%s " (fun s -> s) in
  let c2i c = Char.code c - Char.code 'a' in
  let n = String.length a
  and m = String.length b in
  let inf = 1000000000 in
  let prev = Array.make_matrix 26 n 0 in
  let next = Array.make_matrix 26 n 0 in
  let p = Array.make 26 inf in
  let p' = Array.make 26 (-1) in
  let _ =
    for i = 0 to n - 1 do
      let c = c2i a.[i] in
	for j = 0 to 25 do
	  prev.(j).(i) <- p'.(j)
	done;
	p'.(c) <- i;
    done;
    for i = n - 1 downto 0 do
      let c = c2i a.[i] in
	for j = 0 to 25 do
	  next.(j).(i) <- p.(j)
	done;
	p.(c) <- i;
    done;
  in
  let d = Array.make m (-1) in
  let pos = ref p.(c2i b.[0]) in
  let _ =
    d.(0) <- !pos;
    for i = 1 to m - 1 do
      if !pos < inf then (
	let c = c2i b.[i] in
	  pos := next.(c).(!pos);
      );
      d.(i) <- !pos;
    done;
  in
  let d' = Array.make m (-1) in
  let _ =
    pos := p'.(c2i b.[m - 1]);
    d'.(m - 1) <- !pos;
    for i = m - 2 downto 0 do
      if !pos >= 0 then (
	let c = c2i b.[i] in
	  pos := prev.(c).(!pos);
      );
      d'.(i) <- !pos;
    done;
  in
    (*for i = 0 to m - 1 do
      Printf.printf "asd %d %d\n" d.(i) d'.(i)
    done;*)
  let bl = ref (-1) in
  let br = ref m in
  let r = ref 0 in
    for l = 0 to m - 1 do
      while !r < m && d'.(!r) <= d.(l) do
	incr r;
      done;
      if d.(l) < m && !br - !bl > !r - l then (
	br := !r;
	bl := l;
      )
    done;
    (*Printf.printf "asd %d %d\n" !bl !br;*)
    let res = String.sub b 0 (!bl + 1) ^ String.sub b !br (m - !br) in
      if res = ""
      then Printf.printf "-\n"
      else Printf.printf "%s\n" res


Andryusha is an orderly boy and likes to keep things in their place.Today he faced a problem to put his socks in the wardrobe. He has n distinct pairs of socks which are initially in a bag. The pairs are numbered from 1 to n. Andryusha wants to put paired socks together and put them in the wardrobe. He takes the socks one by one from the bag, and for each sock he looks whether the pair of this sock has been already took out of the bag, or not. If not (that means the pair of this sock is still in the bag), he puts the current socks on the table in front of him. Otherwise, he puts both socks from the pair to the wardrobe.Andryusha remembers the order in which he took the socks from the bag. Can you tell him what is the maximum number of socks that were on the table at the same time?

The first line contains the single integer n (1 ≤ n ≤ 105) — the number of sock pairs. The second line contains 2n integers x1, x2, ..., x2n (1 ≤ xi ≤ n), which describe the order in which Andryusha took the socks from the bag. More precisely, xi means that the i-th sock Andryusha took out was from pair xi. It is guaranteed that Andryusha took exactly two socks of each pair.

Print single integer — the maximum number of socks that were on the table at the same time.

NoteIn the first example Andryusha took a sock from the first pair and put it on the table. Then he took the next sock which is from the first pair as well, so he immediately puts both socks to the wardrobe. Thus, at most one sock was on the table at the same time.In the second example Andryusha behaved as follows:   Initially the table was empty, he took out a sock from pair 2 and put it on the table.  Sock (2) was on the table. Andryusha took out a sock from pair 1 and put it on the table.  Socks (1, 2) were on the table. Andryusha took out a sock from pair 1, and put this pair into the wardrobe.  Sock (2) was on the table. Andryusha took out a sock from pair 3 and put it on the table.  Socks (2, 3) were on the table. Andryusha took out a sock from pair 2, and put this pair into the wardrobe.  Sock (3) was on the table. Andryusha took out a sock from pair 3 and put this pair into the wardrobe.  Thus, at most two socks were on the table at the same time.

open Printf
open Scanf

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

  let out = Array.make (2*n) false in

  let rec loop i nout maxout = if i=2*n then maxout else (
    let sock = x.(i)-1 in
    if out.(sock) then (
      out.(sock) <- false;
      loop (i+1) (nout-1) maxout
    ) else (
      out.(sock) <- true;
      loop (i+1) (nout+1) (max maxout (nout+1))
    )
  ) in

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

type state = Unseen | One | Both

let () =
  let n = Scanf.scanf "%d" (fun x -> x) in
  let pairs = Array.make (n + 1) Unseen in
  let on_table = ref 0 in
  let m = ref 0 in
  for i = 1 to 2*n do
    let k = Scanf.scanf " %d" (fun x -> x) in
    match pairs.(k) with
    | Unseen -> begin
        pairs.(k) <- One;
        incr on_table;
        m := max !m !on_table
      end
    | One -> begin
        pairs.(k) <- Both;
        decr on_table
      end
    | Both -> ()
  done;
  Printf.printf "%d\n" !m

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 read_int in

  let out = Array.make n false in

  let rec loop i nout maxout = if i=n then maxout else (
    let sock = x.(i)-1 in
    if out.(sock) then (
      out.(sock) <- false;
      loop (i+1) (nout-1) maxout
    ) else (
      out.(sock) <- true;
      loop (i+1) (nout+1) (max maxout (nout+1))
    )
  ) in

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


Petya has a string of length n consisting of small and large English letters and digits.He performs m operations. Each operation is described with two integers l and r and a character c: Petya removes from the string all characters c on positions between l and r, inclusive. It's obvious that the length of the string remains the same or decreases after each operation.Find how the string will look like after Petya performs all m operations.

The first string contains two integers n and m (1 ≤ n, m ≤ 2·105) — the length of the string and the number of operations. The second line contains the string of length n, consisting of small and large English letters and digits. Positions in the string are enumerated from 1. Each of the next m lines contains two integers l and r (1 ≤ l ≤ r), followed by a character c, which is a small or large English letter or a digit. This line describes one operation. It is guaranteed that r doesn't exceed the length of the string s before current operation.

Print the string Petya will obtain after performing all m operations. If the strings becomes empty after all operations, print an empty line.

9 5
aAAaBBccD
1 4 a
5 6 c
2 3 B
4 4 D
2 3 A

NoteIn the first example during the first operation both letters 'a' are removed, so the string becomes "bc". During the second operation the letter 'c' (on the second position) is removed, and the string becomes "b".In the second example during the first operation Petya removes '0' from the second position. After that the string becomes "Az". During the second operations the string doesn't change.

(* 6:40 *)

let parent v = v/2
let lc v = 2*v
let rc v = 2*v+1

let superceil n =
  let rec loop i ac = if i=0 then ac else loop (i lsr 1) (ac lor i) in
  1 + (loop (n-1) 0)

(* segtree for computing the modified indices *)
(* It's a set of variables for each position in the original
   string.  0 if it has been deleted, 1 otherwise *)

let make_indextree n =
  let nn = superceil n in
  let a = Array.make (2*nn) 0 in
  (nn,a)

let assign_index (nn,a) v x =
  (* do a(v) <- x *)
  let x = x - a.(nn+v) in
  let rec loop j = if j > 0 then (
    a.(j) <- a.(j) + x;
    loop (parent j)
  ) in
  loop (nn+v)

let get_index_var (nn,a) i = a.(nn+i)

(* we don't need sum
let sum (nn,a) i j =
  let rec f v l r ql qr =
    let m = (l+r)/2 in
    if l=ql && r=qr then a.(v) else
      let t1 = if ql<=m then f (lc v) l m ql (min m qr) else 0 in
      let t2 = if qr>m then f (rc v) (m+1) r (max ql (m+1)) qr else 0 in
      t1 + t2
  in
  f 1 0 (nn-1) i j
*)

let get_ith (nn,a) i =
  (* return the position of the ith non deleted char *)
  (* zero-based indexing is used throughout *)
  let rec loop k i = (* at node k looking for the ith *)
    if k >= nn then k-nn
    else if a.(lc k) > i
    then loop (lc k) i else loop (rc k) (i-a.(lc k))
  in
  loop 1 i

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 ( land ) a b = Int64.logand a b
let ( lor ) a b = Int64.logor a b
let ( lsl) a b = Int64.shift_left a b
let long x = Int64.of_int x

let make_findtree n =
  let nn = superceil n in
  let a = Array.make (2*nn) 0L in
  (nn,a)

let assign_find (nn,a) v ci add =
(* v is an index, ci [0,63] is a char index we want to put into
   place v in the segtree  This puts it there and propagates
   it up the tree.  If add=true, then add it else remove it *)
  let rec loop j = if j > 0 then (
    a.(j) <- a.(lc j) lor a.(rc j);
    loop (parent j)
  ) in
  a.(nn+v) <- if add then 1L lsl ci else 0L;
  loop (parent (nn+v))

let make_index_list (nn,a) ci ql qr =
(* return the index list of places where the char ci is stored
   restricted to the range [ql,qr] *)
  let mask = 1L lsl ci in
  let rec scan v l r ac =
    let m = (l+r)/2 in
    if a.(v) land mask = 0L then ac
    else if r < ql || l > qr then ac
    else if v >= nn then (v-nn)::ac
    else scan (lc v) l m (scan (rc v) (m+1) r ac)
  in
  scan 1 0 (nn-1) []

let char_index c =
  if '0' <= c && c <= '9' then (int_of_char c) - (int_of_char '0')
  else if 'a' <= c && c <= 'z' then 10 + (int_of_char c) - (int_of_char 'a')
  else if 'A' <= c && c <= 'Z' then 36 + (int_of_char c) - (int_of_char 'A')
  else failwith "bad char"

open Printf
open Scanf

let read_pair _ = bscanf Scanning.stdib " %d %d " (fun x y -> (x,y))
let read_string _ = bscanf Scanning.stdib " %s " (fun x -> x)
let () =
  let (n,m) = read_pair() in
  let s = read_string() in
  let len = String.length s in

  let ft = make_findtree len in
  let it = make_indextree len in

  for i=0 to len-1 do
    assign_find ft i (char_index s.[i]) true;
    assign_index it i 1
  done;

  for q = 1 to m do
    let (l,r) = read_pair() in
    let c = (read_string()).[0] in
    let ci = char_index c in
    let l = get_ith it (l-1) in
    let r = get_ith it (r-1) in
    let il = make_index_list ft ci l r in
    List.iter (fun i -> assign_index it i 0) il;
    List.iter (fun i -> assign_find ft i 0 false) il;
  done;

  for i=0 to len-1 do
    if get_index_var it i = 1 then printf "%c" s.[i]
  done;
  print_newline()


You have $$$n$$$ students under your control and you have to compose exactly two teams consisting of some subset of your students. Each student had his own skill, the $$$i$$$-th student skill is denoted by an integer $$$a_i$$$ (different students can have the same skills).So, about the teams. Firstly, these two teams should have the same size. Two more constraints:  The first team should consist of students with distinct skills (i.e. all skills in the first team are unique).  The second team should consist of students with the same skills (i.e. all skills in the second team are equal). Note that it is permissible that some student of the first team has the same skill as a student of the second team.Consider some examples (skills are given):  $$$[1, 2, 3]$$$, $$$[4, 4]$$$ is not a good pair of teams because sizes should be the same;  $$$[1, 1, 2]$$$, $$$[3, 3, 3]$$$ is not a good pair of teams because the first team should not contain students with the same skills;  $$$[1, 2, 3]$$$, $$$[3, 4, 4]$$$ is not a good pair of teams because the second team should contain students with the same skills;  $$$[1, 2, 3]$$$, $$$[3, 3, 3]$$$ is a good pair of teams;  $$$[5]$$$, $$$[6]$$$ is a good pair of teams. Your task is to find the maximum possible size $$$x$$$ for which it is possible to compose a valid pair of teams, where each team size is $$$x$$$ (skills in the first team needed to be unique, skills in the second team should be the same between them). A student cannot be part of more than one team.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of students. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the skill of the $$$i$$$-th student. Different students can have the same skills. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).

For each test case, print the answer — the maximum possible size $$$x$$$ for which it is possible to compose a valid pair of teams, where each team size is $$$x$$$.

NoteIn the first test case of the example, it is possible to construct two teams of size $$$3$$$: the first team is $$$[1, 2, 4]$$$ and the second team is $$$[4, 4, 4]$$$. Note, that there are some other ways to construct two valid teams of size $$$3$$$.

(* Codeforces 1335 CTwo Teams Composing train  *)

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

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

let rec num_same cursm mxsm last l = match l with
    | [] -> mxsm
    | h :: t ->
        let n_cursm = if h==last then (cursm+1) else 1 in
        let n_mxsm = if n_cursm > mxsm then n_cursm else mxsm in
        num_same n_cursm n_mxsm h t;;

let rec num_diff dif last l = match l with
    | [] -> dif
    | h :: t ->
        let n_dif = if h != last then (dif + 1) else dif in
        num_diff n_dif h t;;

let do_one () =
	let n = gr () in
    let a = readlist n [] in
    let a = List.sort compare a in
    let nsame = num_same 0 0 (-1) a in
    let ndiff = num_diff 0 (-1) a in
    (* let _ = Printf.printf "same,dif = %d %d\n" nsame ndiff in *)
    let cmd = if nsame > ndiff then ndiff
        else if nsame < ndiff then nsame
        else nsame - 1 in
    Printf.printf "%d\n" cmd;;

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

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

main();;