You received a notebook which is called Death Note. This notebook has infinite number of pages. A rule is written on the last page (huh) of this notebook. It says: "You have to write names in this notebook during $$$n$$$ consecutive days. During the $$$i$$$-th day you have to write exactly $$$a_i$$$ names.". You got scared (of course you got scared, who wouldn't get scared if he just receive a notebook which is named Death Note with a some strange rule written in it?).Of course, you decided to follow this rule. When you calmed down, you came up with a strategy how you will write names in the notebook. You have calculated that each page of the notebook can contain exactly $$$m$$$ names. You will start writing names from the first page. You will write names on the current page as long as the limit on the number of names on this page is not exceeded. When the current page is over, you turn the page. Note that you always turn the page when it ends, it doesn't matter if it is the last day or not. If after some day the current page still can hold at least one name, during the next day you will continue writing the names from the current page.Now you are interested in the following question: how many times will you turn the page during each day? You are interested in the number of pages you will turn each day from $$$1$$$ to $$$n$$$.

The first line of the input contains two integers $$$n$$$, $$$m$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$1 \le m \le 10^9$$$) — the number of days you will write names in the notebook and the number of names which can be written on each page of the notebook. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ means the number of names you will write in the notebook during the $$$i$$$-th day.

Print exactly $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$, where $$$t_i$$$ is the number of times you will turn the page during the $$$i$$$-th day.

NoteIn the first example pages of the Death Note will look like this $$$[1, 1, 1, 2, 2], [2, 2, 2, 2, 2], [3, 3, 3, 3, 3], [3, 3, 3, 3]$$$. Each number of the array describes during which day name on the corresponding position will be written. It is easy to see that you should turn the first and the second page during the second day and the third page during the third day.

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 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_-$1 && !f do match fbod !i with | `Ok -> i := !i +$ 1; | `Ng -> f := false done
	let repi64 from_ to_ fbod = let i,f = ref ~|from_,ref true in while !i <= ~|to_-$1 && !f do match fbod ~~| !i with | `Ok -> i := !i +$ 1; | `Ng -> f := false done
end

open MyInt64
let () =
	let n,m = get_2_i64 ()
	in let a = input_i64_array n
	in
	Array.fold_left (fun (res,sum,cur) v ->
		let w = cur + v
		in if w >= m then (
			let q = w / m in let r = w - q * m
			in
			(q::res,sum+q,r)
			(* if sum+q >= n then (
				let q' = n - sum in
				(q'::res,n,r)
			) else (
				(q::res,sum+q,r)
			) *)
		) else (0L::res,sum,w)
	) ([],0L,0L) a
	|> (fun (r,_,_) -> r)
	|> List.rev
	|> List.iter (printf "%Ld ")

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

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%Ld " (fun s -> s) in
  let c = ref 0L in
    for i = 1 to n do
      let k = Scanf.bscanf sb "%Ld " (fun s -> s) in
	c := !c +| k;
	Printf.printf "%Ld " (!c /| m);
	c := Int64.rem !c m;
    done;
    Printf.printf "\n"

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 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_-$1 && !f do match fbod !i with | `Ok -> i := !i +$ 1; | `Ng -> f := false done
	let repi64 from_ to_ fbod = let i,f = ref ~|from_,ref true in while !i <= ~|to_-$1 && !f do match fbod ~~| !i with | `Ok -> i := !i +$ 1; | `Ng -> f := false done
end

open MyInt64
let () =
	let n,m = get_2_i64 ()
	in let a = input_i64_array n
	in
	Array.fold_left (fun (res,sum,cur) v ->
		if sum = n then (res,sum,cur)
		else if sum > n then failwith ""
		else
		let w = cur + v
		in if w >= m then (
			let q = w / m in let r = w - q * m
			in
			if sum+q >= n then (
				let q' = n - sum in
				(q'::res,n,r)
			) else (
				(q::res,sum+q,r)
			)
		) else (0L::res,sum,w)
	) ([],0L,0L) a
	|> (fun (r,_,_) -> r)
	|> List.rev
	|> List.iter (printf "%Ld ")


You have a new professor of graph theory and he speaks very quickly. You come up with the following plan to keep up with his lecture and make notes.You know two languages, and the professor is giving the lecture in the first one. The words in both languages consist of lowercase English characters, each language consists of several words. For each language, all words are distinct, i.e. they are spelled differently. Moreover, the words of these languages have a one-to-one correspondence, that is, for each word in each language, there exists exactly one word in the other language having has the same meaning.You can write down every word the professor says in either the first language or the second language. Of course, during the lecture you write down each word in the language in which the word is shorter. In case of equal lengths of the corresponding words you prefer the word of the first language.You are given the text of the lecture the professor is going to read. Find out how the lecture will be recorded in your notes.

The first line contains two integers, n and m (1 ≤ n ≤ 3000, 1 ≤ m ≤ 3000) — the number of words in the professor's lecture and the number of words in each of these languages. The following m lines contain the words. The i-th line contains two strings ai, bi meaning that the word ai belongs to the first language, the word bi belongs to the second language, and these two words have the same meaning. It is guaranteed that no word occurs in both languages, and each word occurs in its language exactly once. The next line contains n space-separated strings c1, c2, ..., cn — the text of the lecture. It is guaranteed that each of the strings ci belongs to the set of strings {a1, a2, ... am}. All the strings in the input are non-empty, each consisting of no more than 10 lowercase English letters.

Output exactly n words: how you will record the lecture in your notebook. Output the words of the lecture in the same order as in the input.

4 3
codeforces codesecrof
contest round
letter message
codeforces contest letter contest

5 3
joll wuqrd
euzf un
hbnyiyc rsoqqveh
hbnyiyc joll joll euzf joll

codeforces round letter round
hbnyiyc joll joll un joll

module StringMap = Map.Make(String)

let read_int () = Scanf.scanf "%d %d " (fun n m -> (n, m))
let read_str () = Scanf.scanf "%s " (fun s -> s)

let get_short str1 str2 =
    if String.length str1 <= String.length str2
    then
        str1
    else
        str2

let fill_map m =
     let map = StringMap.empty in
     let rec loop i map =
        if i < m
        then
            let key = read_str () in
            let temp = read_str () in
            let value = get_short key temp in
            let map = StringMap.add key value map in
            loop (i + 1) map
        else
            map
    in loop 0 map

let fill_list n =
    let rec loop i list =
        if i < n
        then
            let temp = read_str () in
            loop (i + 1) (temp :: list)
        else
            List.rev list
    in loop 0 []

let input () =
    let (n, m) = read_int () in
    let words = fill_map m in
    let text = fill_list n in
    (words, text)

let solve (words, text) =
    let rec loop list acc =
        match list with
        | (head :: tail) ->
            let short = StringMap.find head words in
            loop tail (short :: acc)
        | [] -> List.rev acc
    in loop text []

let print result =
    (List.iter (Printf.printf "%s ") result;
    Printf.printf "\n")

let () = print (solve (input ()))

let () = Scanf.scanf "%d %d\n" (fun n m ->
  let h = Hashtbl.create n in
  for i = 1 to m do
    Scanf.scanf "%[^ ] %s\n" (Hashtbl.add h)
  done;
  let w = Array.init n (fun _ ->
    Scanf.scanf "%[^ \n]%_c" (fun s ->
      let t = Hashtbl.find h s in
      if String.length t < String.length s
      then t else s
    )) in
  print_endline (String.concat " " (Array.to_list w))
)

module A = Array;;
module C = Char;;
module L = List;;
module S = String;;

let pf = Printf.printf;;
let sf = Scanf.scanf;;
let ssf = Scanf.sscanf;;

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

exception Error of string

let inf = 1000000000;;
let eps = 1e-11;;

type ('a, 'b) avl_data =
  | Empty
  | Node of int * ('a * 'b) * ('a, 'b) avl_data * ('a, 'b) avl_data
;;

type 'a option = Nothing | Just of 'a;;

type ('a, 'b) avl_tree =
  { set : ('a * 'b) -> unit; value : 'a -> 'b option }
;;

let new_avl_tree cmp =
  let tree = ref Empty in
  let err() = raise @@ Error "AVL-Tree Internal Error." in
  let height = function Empty -> 0 | Node (h, _, _, _) -> h in
  let make_node a l r = Node (max (height l) (height r) + 1, a, l, r) in
  let rotate = function
      Empty -> Empty
    | Node (_, a, l, r) ->
       if height l > height r + 1
       then (match l with
               Empty -> err()
             | Node (_, b, ll, lr) ->
                if height ll >= height lr
                then make_node b ll (make_node a lr r)
                else (match lr with
                        Empty -> err()
                      | Node (_, c, lrl, lrr) ->
                         make_node c (make_node b ll lrl) (make_node a lrr r)))
       else if height l + 1 < height r
       then (match r with
               Empty -> err()
             | Node (_, b, rl, rr) ->
                if height rl <= height rr
                then make_node b (make_node a l rl) rr
                else (match rl with
                        Empty -> err()
                      | Node (_, c, rll, rlr) ->
                         make_node c (make_node a l rll) (make_node b rlr rr)))
       else make_node a l r in
  let rec insert (x, y) = function
      Empty -> Node (1, (x, y), Empty, Empty)
    | Node (h, (k, v), l, r) ->
       let t = if cmp x k = 0
               then Node (h, (x, y), l, r)
               else if cmp x k < 0
               then Node (h, (k, v), insert (x, y) l, r)
               else Node (h, (k, v), l, insert (x, y) r) in
       rotate t in
  let rec find x = function
      Empty -> Nothing
    | Node (_, (k, v), l, r) ->
       if cmp x k = 0
       then Just v
       else if cmp x k < 0
       then find x l
       else find x r in
  let this() =
    { set = (fun x -> tree := insert x !tree);
      value = (fun x -> find x !tree) } in
  this();
;;

let _ =
  let str = read_line() in
  let (n, m) = ssf str "%d %d" (fun x y -> x, y) in
  let avl = new_avl_tree compare in
  for i = 1 to m do
    let s = read_line() in
    let (a, b) = ssf s "%s %s" (fun x y -> x, y) in
    let res = if String.length a <= String.length b then a else b in
    avl.set (a, res); avl.set (b, res);
  done;
  let str = read_line() in
  let slist = Str.split (Str.regexp " ") str in
  let reveal = function Nothing -> " " | Just a -> a in
  List.map (fun s -> print_string @@ reveal (avl.value s) ^ " ") slist
;;


You are given two rectangles on a plane. The centers of both rectangles are located in the origin of coordinates (meaning the center of the rectangle's symmetry). The first rectangle's sides are parallel to the coordinate axes: the length of the side that is parallel to the Ox axis, equals w, the length of the side that is parallel to the Oy axis, equals h. The second rectangle can be obtained by rotating the first rectangle relative to the origin of coordinates by angle α.  Your task is to find the area of the region which belongs to both given rectangles. This region is shaded in the picture.

The first line contains three integers w, h, α (1 ≤ w, h ≤ 106; 0 ≤ α ≤ 180). Angle α is given in degrees.

In a single line print a real number — the area of the region which belongs to both given rectangles. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 6.

NoteThe second sample has been drawn on the picture above.

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

let pi = 4.0 *. atan2 1.0 1.0

let line_intersection (a1, b1, c1) (a2, b2, c2) =
  (* non-parallel lines defined by ax+by=c. Intersect them *)
  let (a1,b1,c1,a2,b2,c2) = if a2=0.0 then (a2,b2,c2,a1,b1,c1) else (a1,b1,c1,a2,b2,c2) in
  if a1=0.0 then (
    let y = c1 /. b1 in
    let x = (c2 -. b2 *. y) /. a2 in
      (x,y)
  ) else (
    let (b1,c1) = (b1/.a1, c1/.a1) in
    let (b2,c2) = (b2/.a2, c2/.a2) in
    let y = (c1 -. c2) /. (b1 -. b2) in
    let x = c1 -. b1*.y in
      (x,y)
  )

let triangle_area p q r =
  let sq x = x *. x in
  let dist (x1,y1) (x2,y2) = sqrt ((sq (x1-.x2)) +. (sq (y1-.y2))) in
  let a = dist p q in
  let b = dist q r in
  let c = dist r p in
  let s = (a +. b +. c) /. 2.0 in
    sqrt ((s -. a) *. (s -. b) *. (s -. c) *. s)

let () =
  let w = float (read_int()) in
  let h = float (read_int()) in
  let (w,h) = (max w h, min w h) in
  let alpha = read_int() in
  let alpha = min alpha (180-alpha) in
  let alpha = ((float alpha) /. 180.0) *. pi in

  let alpha0 = 2.0 *. (atan (h /. w)) in

  let area =
    if alpha = 0.0 then w *. h
    else if alpha >= alpha0 then
      let base = h /. (sin alpha) in
	base *. h
    else
      let l1 = (0.0, 1.0, -.(h/.2.0)) in
      let l2 = (1.0, 0.0, (w/.2.0)) in
      let l3 = (0.0, 1.0, (h/.2.0)) in
      let l4 = (sin alpha, -.(cos alpha), h /. 2.0) in
      let l5 = (cos alpha, sin alpha, w /. 2.0) in
      let p1 = line_intersection l1 l4 in
      let p2 = line_intersection l4 l2 in
      let p3 = line_intersection l2 l5 in
      let p4 = line_intersection l5 l3 in
      let p0 = (0.0, -. (h/.2.0)) in
      let p5 = (0.0, (h/.2.0)) in
      let area = triangle_area p0 p1 p2 in
      let area = area +. (triangle_area p0 p2 p3) in
      let area = area +. (triangle_area p0 p3 p4) in
      let area = area +. (triangle_area p0 p4 p5) in
	2.0 *. area
  in
    Printf.printf "%.7f\n" area


The teacher gave Anton a large geometry homework, but he didn't do it (as usual) as he participated in a regular round on Codeforces. In the task he was given a set of n lines defined by the equations y = ki·x + bi. It was necessary to determine whether there is at least one point of intersection of two of these lines, that lays strictly inside the strip between x1 < x2. In other words, is it true that there are 1 ≤ i < j ≤ n and x', y', such that:   y' = ki * x' + bi, that is, point (x', y') belongs to the line number i;  y' = kj * x' + bj, that is, point (x', y') belongs to the line number j;  x1 < x' < x2, that is, point (x', y') lies inside the strip bounded by x1 < x2. You can't leave Anton in trouble, can you? Write a program that solves the given task.

The first line of the input contains an integer n (2 ≤ n ≤ 100 000) — the number of lines in the task given to Anton. The second line contains integers x1 and x2 ( - 1 000 000 ≤ x1 < x2 ≤ 1 000 000) defining the strip inside which you need to find a point of intersection of at least two lines. The following n lines contain integers ki, bi ( - 1 000 000 ≤ ki, bi ≤ 1 000 000) — the descriptions of the lines. It is guaranteed that all lines are pairwise distinct, that is, for any two i ≠ j it is true that either ki ≠ kj, or bi ≠ bj.

Print "Yes" (without quotes), if there is at least one intersection of two distinct lines, located strictly inside the strip. Otherwise print "No" (without quotes).

NoteIn the first sample there are intersections located on the border of the strip, but there are no intersections located strictly inside it.

open List open Str open Int64;;
let cmp x y =
    if(nth x 0) < (nth y 0) || (((nth x 0) = (nth y 0)) && ((nth x 1) < (nth y 1)))
    then -1 else 1;;
let n = read_int ();;
let par = Array.create n [0L;0L] and x_1 :: x_2 :: [] =
    map (of_string) (split (regexp " ") (read_line ()));;
for i = 1 to n do
    let k :: b :: [] =
        map (of_string) (split (regexp " ") (read_line ())) in
            Array.set par (i - 1) [(add (mul k x_1) b); (add (mul k x_2) b)];
done;;
Array.sort cmp par;;
for i = 0 to  (Array.length par) - 2 do
    if (nth par.(i + 1) 1) < (nth par.(i) 1) then begin
        print_endline "YES";
        exit 0;
end done;;
print_endline "NO";;

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 rec forall i j f = (i>j) || ((f i) && forall (i+1) j f)

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 () =
  let n = read_int () in
  let x1 = read_long () in
  let x2 = read_long () in

  let k = Array.make n 0L in
  let b = Array.make n 0L in

  for i=0 to n-1 do
    k.(i) <- read_long();
    b.(i) <- read_long()
  done;

  let y1 = Array.init n (fun i -> (k.(i) ** x1 ++ b.(i), i)) in
  let y2 = Array.init n (fun i -> (k.(i) ** x2 ++ b.(i), i)) in

  let y1_init = Array.copy y1 in
  let y2_init = Array.copy y2 in

  Array.sort compare y1;
  Array.sort compare y2;

  Array.sort (fun (v1,i1) (v2,i2) ->
    if v1 <> v2 then compare v1 v2
    else compare y2_init.(i1) y2_init.(i2)
  ) y1;

  Array.sort (fun (v1,i1) (v2,i2) ->
    if v1 <> v2 then compare v1 v2
    else compare y1_init.(i1) y1_init.(i2)
  ) y2;

  let answer = forall 0 (n-1) (
    fun i ->
      let (_,i1) = y1.(i) in
      let (_,i2) = y2.(i) in
      i1 = i2
  ) in

  if answer then printf "NO\n" else printf "YES\n"

let cmp x y = if x.(0) < y.(0) then -1 else 1;;
let n = read_int ();;
let ls = ref (Array.create n [|0;0|]);;
let x_1 = ref 0 and x_2 = ref 0;;
let ins = List.map (int_of_string) (Str.split (Str.regexp " ") (read_line ())) in
begin
	x_1 := List.nth ins 0;
	x_2 := List.nth ins 1;
end;;
for i = 1 to n do
	let ins = List.map (int_of_string) (Str.split (Str.regexp " ") (read_line ())) in
	let k = List.nth ins 0 and b = List.nth ins 1 in
	begin
		ls := Array.append !ls [|[|(!x_1 * k + b);(!x_2 * k + b)|]|];
	end;
done;
Array.stable_sort cmp !ls;;
let fl = ref 0;;
for i = 0 to Array.length !ls - 2 do
	if (!ls.(i).(1) > !ls.(i + 1).(1)) then fl := 1;
done;;
if !fl = 0 then print_endline "NO" else print_endline "YES";;

(*
for i = 0 to Array.length !ls - 1 do
	print_int !ls.(i).(0);
	print_string " ";
	print_int !ls.(i).(1);
	print_endline "";
done;;*)

let cmp x y = if x.(0) < y.(0) then -1 else 1;;
let n = read_int ();;
let ls = ref (Array.create n [|0;0|]);;
let x_1 = ref 0 and x_2 = ref 0;;
let ins = List.map (int_of_string) (Str.split (Str.regexp " ") (read_line ())) in
begin
	x_1 := List.nth ins 0;
	x_2 := List.nth ins 1;
end;;
for i = 1 to n do
	let ins = List.map (int_of_string) (Str.split (Str.regexp " ") (read_line ())) in
	let k = List.nth ins 0 and b = List.nth ins 1 in
	begin
        Array.set (!ls) (i - 1) [|!x_1 * k + b; !x_2 * k + b|];
	end;
done;
Array.stable_sort cmp !ls;;
let fl = ref 0;;
for i = 0 to Array.length !ls - 2 do
	if ((!ls.(i).(1) > !ls.(i + 1).(1))) && (!ls.(i).(0) != !ls.(i + 1).(0))  then fl := 1;
done;;
if !fl = 0 then print_endline "NO" else print_endline "YES";;
(*
for i = 0 to Array.length !ls - 1 do
	print_int !ls.(i).(0);
	print_string " ";
	print_int !ls.(i).(1);
	print_endline "";
done;;*)

let cmp x y = if x.(0) < y.(0) then -1 else 1;;
let n = read_int ();;
let ls = ref (Array.create n [|0;0|]);;
let x_1 = ref 0 and x_2 = ref 0;;
let ins = List.map (int_of_string) (Str.split (Str.regexp " ") (read_line ())) in
begin
	x_1 := List.nth ins 0;
	x_2 := List.nth ins 1;
end;;
for i = 1 to n do
	let ins = List.map (int_of_string) (Str.split (Str.regexp " ") (read_line ())) in
	let k = List.nth ins 0 and b = List.nth ins 1 in
	begin
        Array.set (!ls) (i - 1) [|!x_1 * k + b; !x_2 * k + b|];
	end;
done;
Array.stable_sort cmp !ls;;
let fl = ref 0;;
for i = 0 to Array.length !ls - 2 do
	if (!ls.(i).(1) > !ls.(i + 1).(1)) then fl := 1;
done;;
if !fl = 0 then print_endline "NO" else print_endline "YES";;
(*
for i = 0 to Array.length !ls - 1 do
	print_int !ls.(i).(0);
	print_string " ";
	print_int !ls.(i).(1);
	print_endline "";
done;;*)


Andrew prefers taxi to other means of transport, but recently most taxi drivers have been acting inappropriately. In order to earn more money, taxi drivers started to drive in circles. Roads in Andrew's city are one-way, and people are not necessary able to travel from one part to another, but it pales in comparison to insidious taxi drivers.The mayor of the city decided to change the direction of certain roads so that the taxi drivers wouldn't be able to increase the cost of the trip endlessly. More formally, if the taxi driver is on a certain crossroads, they wouldn't be able to reach it again if he performs a nonzero trip. Traffic controllers are needed in order to change the direction the road goes. For every road it is known how many traffic controllers are needed to change the direction of the road to the opposite one. It is allowed to change the directions of roads one by one, meaning that each traffic controller can participate in reversing two or more roads.You need to calculate the minimum number of traffic controllers that you need to hire to perform the task and the list of the roads that need to be reversed.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 100\,000$$$, $$$1 \leq m \leq 100\,000$$$) — the number of crossroads and the number of roads in the city, respectively. Each of the following $$$m$$$ lines contain three integers $$$u_{i}$$$, $$$v_{i}$$$ and $$$c_{i}$$$ ($$$1 \leq u_{i}, v_{i} \leq n$$$, $$$1 \leq c_{i} \leq 10^9$$$, $$$u_{i} \ne v_{i}$$$) — the crossroads the road starts at, the crossroads the road ends at and the number of traffic controllers required to reverse this road.

In the first line output two integers the minimal amount of traffic controllers required to complete the task and amount of roads $$$k$$$ which should be reversed. $$$k$$$ should not be minimized. In the next line output $$$k$$$ integers separated by spaces — numbers of roads, the directions of which should be reversed. The roads are numerated from $$$1$$$ in the order they are written in the input. If there are many solutions, print any of them.

NoteThere are two simple cycles in the first example: $$$1 \rightarrow 5 \rightarrow 2 \rightarrow 1$$$ and $$$2 \rightarrow 3 \rightarrow 4 \rightarrow 5 \rightarrow 2$$$. One traffic controller can only reverse the road $$$2 \rightarrow 1$$$ and he can't destroy the second cycle by himself. Two traffic controllers can reverse roads $$$2 \rightarrow 1$$$ and $$$2 \rightarrow 3$$$ which would satisfy the condition.In the second example one traffic controller can't destroy the cycle $$$ 1 \rightarrow 3 \rightarrow 2 \rightarrow 1 $$$. With the help of three controllers we can, for example, reverse roads $$$1 \rightarrow 3$$$ ,$$$ 2 \rightarrow 4$$$, $$$1 \rightarrow 5$$$.

let binary_search ng ok f = let d = if ng < ok then 1 else -1 in
  let rec f0 ng ok =
    if abs (ok - ng) <= 1 then ok
    else let mid = ng + (ok - ng) / 2 in if f mid then f0 ng mid else f0 mid ok
  in f0 (ng-d) (ok+d)
;;
Scanf.(Array.(scanf " %d %d" @@ fun n m ->
  let edges = make n [] in
  let edges0 =
    init m (fun i -> scanf " %d %d %d" @@ fun u v c ->
      let u,v = u-1,v-1 in
      edges.(u) <- (v,c,i) :: edges.(u); u,v)
  in
  let solve w =
    let state = Array.make n `Todo in
    let res = ref [] in
    let rec visit i =
      if state.(i) = `Done then true
      else if state.(i) = `Temp then false
      else (
        state.(i) <- `Temp;
        let f =
          List.fold_left (fun u (j,cost,_) ->
            if cost <= w || not u then u
            else u && visit j
          ) true edges.(i)
        in
        state.(i) <- `Done; res := i :: !res;
        f
      )
    in
    if
      fold_left (fun u i ->
        if state.(i) = `Todo then
          u && visit i
        else u
      ) true @@ init n (fun i -> i)
    then Some !res else None
  in
  let c = binary_search 0 (1000000050) (fun w ->
    match solve w with Some _ -> true | None -> false
  ) in
  let res = match solve c with Some r -> r | None -> failwith "" in
  let edges2 = init n (fun i -> edges.(i)) in
  iteri (fun i -> List.iter (fun (j,cost,idx) ->
    edges2.(j) <- (i,cost,idx) :: edges2.(j)
  )) edges;
  let used = make n false in
  let res2 = ref [] in
  List.iter (fun i ->
    List.iter (fun (j,cost,idx) ->
      if cost <= c && not used.(j)
        && (i,j) <> edges0.(idx)
      then res2 := idx :: !res2
    ) edges2.(i);
    used.(i) <- true;
  ) res;
  Printf.(
    printf "%d %d\n" c @@ List.length !res2;
    List.iter (printf "%d ") @@ List.map ((+) 1) !res2);
))

let binary_search ng ok f = let d = if ng < ok then 1 else -1 in
  let rec f0 ng ok =
    if abs (ok - ng) <= 1 then ok
    else let mid = ng + (ok - ng) / 2 in if f mid then f0 ng mid else f0 mid ok
  in f0 (ng-d) (ok+d)
;;
Scanf.(Array.(scanf " %d %d" @@ fun n m ->
  let edges = make n [] in
  init m (fun i -> scanf " %d %d %d" @@ fun u v c ->
    edges.(u-1) <- (v-1,c,i) :: edges.(u-1);
  ) |> ignore;
  let solve w =
    let state = Array.make n `Todo in
    let res = ref [] in
    let rec visit i =
      if state.(i) = `Done then true
      else if state.(i) = `Temp then false
      else (
        state.(i) <- `Temp;
        let f =
          List.fold_left (fun u (j,cost,_) ->
            if cost <= w || not u then u
            else u && visit j
          ) true edges.(i)
        in
        state.(i) <- `Done;
        res := i :: !res; f
      )
    in
    if
      fold_left (fun u i ->
        if state.(i) = `Todo then
          u && visit i
        else u
      ) true @@ init n (fun i -> i)
    then Some !res else None
  in
  let c = binary_search 0 (1000000050) (fun w ->
    match solve w with Some _ -> true | None -> false
  ) in
  let res = match solve c with Some r -> r | None -> failwith "" in
  let edges2 = init n (fun i -> edges.(i)) in
  iteri (fun i -> List.iter (fun (j,cost,idx) ->
    edges2.(j) <- (i,cost,idx) :: edges2.(j)
  )) edges;
  let used = make n false in
  let res2 = ref [] in
  List.iter (fun i ->
    List.iter (fun (j,cost,idx) ->
      if cost <= c && not used.(j) then (
        res2 := idx :: !res2
      )
    ) edges2.(i);
    used.(i) <- true;
  ) res;
  Printf.(
    printf "%d %d\n" c @@ List.length !res2;
    List.iter (printf "%d ") @@ List.map ((+) 1) !res2)
))


You are given a string $$$s$$$ consisting only of lowercase Latin letters.You can rearrange all letters of this string as you wish. Your task is to obtain a good string by rearranging the letters of the given string or report that it is impossible to do it.Let's call a string good if it is not a palindrome. Palindrome is a string which is read from left to right the same as from right to left. For example, strings "abacaba", "aa" and "z" are palindromes and strings "bba", "xd" are not.You have to answer $$$t$$$ independent queries.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of queries. Each of the next $$$t$$$ lines contains one string. The $$$i$$$-th line contains a string $$$s_i$$$ consisting only of lowercase Latin letter. It is guaranteed that the length of $$$s_i$$$ is from $$$1$$$ to $$$1000$$$ (inclusive).

Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: -1 if it is impossible to obtain a good string by rearranging the letters of $$$s_i$$$ and any good string which can be obtained from the given one (by rearranging the letters) otherwise.

NoteIn the first query we cannot rearrange letters to obtain a good string.Other examples (not all) of correct answers to the second query: "ababaca", "abcabaa", "baacaba".In the third query we can do nothing to obtain a good string.

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  let (+=) r v = r := !r + v
  let (-=) r v = r := !r - v
  let ( *= ) r v = r := !r * v
  let (/=) r v = r := !r / v
  let (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module 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 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
module IMap = Map.Make(struct type t=char let compare=compare end)
let () =
  let t = gi 0 in
  let _ = init (i32 t) (fun _ ->
    let s = scanf " %s" @@ id in
    let a = init (String.length s) (fun i -> s.[i]) in
    (* let same,_,(p,q) = fold_left (fun (f,u,((p,q) as w)) v -> if f=false then false,v,w else if u<>v then false,v,(u,v) else true,v,w) (true,s.[0],(s.[0],s.[0])) a in
    if same then printf "-1\n" else printf "%c%c\n" p q *)
    let mp = ref IMap.empty in
    iter (fun c ->
      mp := IMap.add c (1L+try IMap.find c !mp with _ -> 0L) !mp
    ) a;
    let mp = !mp in
    if IMap.cardinal mp = 1 then printf "-1"
    else (
      IMap.iter (fun c n ->
        rep 1L n (fun _ -> printf "%c" c)
      ) mp;
    );
    print_newline ()
  ) in ()

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  let (+=) r v = r := !r + v
  let (-=) r v = r := !r - v
  let ( *= ) r v = r := !r * v
  let (/=) r v = r := !r / v
  let (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module 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 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 t = gi 0 in
  let _ = init (i32 t) (fun _ ->
    let s = scanf " %s" @@ id in
    let a = init (String.length s) (fun i -> s.[i]) in
    let same,_,(p,q) = fold_left (fun (f,u,((p,q) as w)) v -> if f=false then false,v,w else if u<>v then false,v,(u,v) else true,v,w) (true,s.[0],(s.[0],s.[0])) a in
    if same then printf "-1\n" else printf "%c%c\n" p q
  ) in ()


A flowerbed has many flowers and two fountains.You can adjust the water pressure and set any values r1(r1 ≥ 0) and r2(r2 ≥ 0), giving the distances at which the water is spread from the first and second fountain respectively. You have to set such r1 and r2 that all the flowers are watered, that is, for each flower, the distance between the flower and the first fountain doesn't exceed r1, or the distance to the second fountain doesn't exceed r2. It's OK if some flowers are watered by both fountains.You need to decrease the amount of water you need, that is set such r1 and r2 that all the flowers are watered and the r12 + r22 is minimum possible. Find this minimum value.

The first line of the input contains integers n, x1, y1, x2, y2 (1 ≤ n ≤ 2000,  - 107 ≤ x1, y1, x2, y2 ≤ 107) — the number of flowers, the coordinates of the first and the second fountain. Next follow n lines. The i-th of these lines contains integers xi and yi ( - 107 ≤ xi, yi ≤ 107) — the coordinates of the i-th flower. It is guaranteed that all n + 2 points in the input are distinct.

Print the minimum possible value r12 + r22. Note, that in this problem optimal answer is always integer.

NoteThe first sample is (r12 = 5, r22 = 1):  The second sample is (r12 = 1, r22 = 32):

let read1 () = Scanf.scanf "%d\n" ( fun x -> x );;
let read2 () = Scanf.scanf "%d %d\n" ( fun x y -> x, y );;
let read3 () = Scanf.scanf "%d %d %d\n" ( fun x y z -> x, y, z);;
let read5 () = Scanf.scanf "%d %d %d %d %d\n" ( fun x y z a b-> x, y, z,a,b);;
let read x = (if x = 0 then Scanf.scanf "%d\n" else Scanf.scanf "%d ") ( fun x -> x );;

let (+$) x y =
  if x > max_int - y then max_int
  else x + y;;

let rec ( *$ ) x y =
  if y = 0 then 0 else
  if y mod 2 = 1 then x *$ (y-1) +$ x
  else let a = x *$ (y/2) in a +$ a;;

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

let (n,x1,y1,x2,y2) = read5 ();;

let coord = Array.init n ( fun _ -> read2 () );;

let square a = a *| a;;

let dis (a,b) (c,d) =
  square ( Int64.of_int(a-c) ) +| ( square ( Int64.of_int(b-d) ) );;

let licz ()  =
  Array.sort ( fun a b -> compare ( dis a (x1,y1) ) (dis b (x1,y1) ) ) coord;
  let wynik = ref Int64.max_int in
  for i=0 to n do
    let r1 = if i=0 then Int64.zero else dis coord.(i-1) (x1,y1) in
    let r2 = ref Int64.zero in
    for j=i to (n-1) do
      r2 := max !r2 ( dis coord.(j) (x2,y2) )
    done;
    wynik := min !wynik ( r1 +| !r2 )
  done;
  !wynik;;

let _ =
  Printf.printf "%Ld\n" ( licz () ) ;;

let bs = Scanf.Scanning.stdin
let xint() = Scanf.bscanf bs " %d" (fun x -> x)
let xint32() = Scanf.bscanf bs " %ld" (fun x -> x)
let xint64() = Scanf.bscanf bs " %Ld" (fun x -> x)
let xstr() = Scanf.bscanf bs " %s" (fun x -> x)
let xreal() = Scanf.bscanf bs " %f" (fun x -> x)
let printf = Printf.printf

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

let () =
  let n = xint() in
  let x1 = xint64() and y1 = xint64() in
  let x2 = xint64() and y2 = xint64() in
  let x = Array.make n 0L and y = Array.make n 0L in
  for i = 0 to n - 1 do
    x.(i) <- xint64();
    y.(i) <- xint64();
  done;

  let sqr x = x *| x in
  let max x y =
    if Int64.compare x y > 0 then x
    else y
  in
  let min x y =
    if Int64.compare x y < 0 then x
    else y
  in

  let ret = ref 9223372036854775807L in
  for i = -1 to n - 1 do
    let r1 = (if i = -1 then 0L else ((sqr (x.(i) -| x1)) +| (sqr (y.(i) -| y1)))) in
    let r2 = ref 0L in
    for j = 0 to n - 1 do
      let a = (sqr (x.(j) -| x1)) +| (sqr (y.(j) -| y1)) in
      let b = (sqr (x.(j) -| x2)) +| (sqr (y.(j) -| y2)) in
      if Int64.compare a r1 > 0 then (
        r2 := max !r2 b;
      );
    done;
    ret := min !ret (r1 +| !r2);
  done;
  printf "%Ld\n" !ret;

let bs = Scanf.Scanning.stdin
let xint() = Scanf.bscanf bs " %d" (fun x -> x)
let xint32() = Scanf.bscanf bs " %ld" (fun x -> x)
let xint64() = Scanf.bscanf bs " %Ld" (fun x -> x)
let xstr() = Scanf.bscanf bs " %s" (fun x -> x)
let xreal() = Scanf.bscanf bs " %f" (fun x -> x)
let printf = Printf.printf

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

let () =
  let n = xint() in
  let x1 = xint64() and y1 = xint64() in
  let x2 = xint64() and y2 = xint64() in
  let x = Array.make n 0L and y = Array.make n 0L in
  for i = 0 to n - 1 do
    x.(i) <- xint64();
    y.(i) <- xint64();
  done;

  let sqr x = x *| x in
  let max x y =
    if Int64.compare x y > 0 then x
    else y
  in
  let min x y =
    if Int64.compare x y < 0 then x
    else y
  in

  let ret = ref 9223372036854775807L in
  for i = 0 to n - 1 do
    let r1 = (sqr (x.(i) -| x1)) +| (sqr (y.(i) -| y1)) in
    let r2 = ref 0L in
    for j = 0 to n - 1 do
      let a = (sqr (x.(j) -| x1)) +| (sqr (y.(j) -| y1)) in
      let b = (sqr (x.(j) -| x2)) +| (sqr (y.(j) -| y2)) in
      if Int64.compare a r1 > 0 then (
        r2 := max !r2 b;
      );
    done;
    ret := min !ret (r1 +| !r2);
  done;
  printf "%Ld\n" !ret;


There are $$$n$$$ athletes in front of you. Athletes are numbered from $$$1$$$ to $$$n$$$ from left to right. You know the strength of each athlete — the athlete number $$$i$$$ has the strength $$$s_i$$$.You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team.You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams $$$A$$$ and $$$B$$$ so that the value $$$|\max(A) - \min(B)|$$$ is as small as possible, where $$$\max(A)$$$ is the maximum strength of an athlete from team $$$A$$$, and $$$\min(B)$$$ is the minimum strength of an athlete from team $$$B$$$.For example, if $$$n=5$$$ and the strength of the athletes is $$$s=[3, 1, 2, 6, 4]$$$, then one of the possible split into teams is:   first team: $$$A = [1, 2, 4]$$$,  second team: $$$B = [3, 6]$$$. In this case, the value $$$|\max(A) - \min(B)|$$$ will be equal to $$$|4-3|=1$$$. This example illustrates one of the ways of optimal split into two teams.Print the minimum value $$$|\max(A) - \min(B)|$$$.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case consists of two lines.  The first line contains positive integer $$$n$$$ ($$$2 \le n \le 50$$$) — number of athletes.  The second line contains $$$n$$$ positive integers $$$s_1, s_2, \ldots, s_n$$$ ($$$1 \le s_i \le 1000$$$), where $$$s_i$$$ — is the strength of the $$$i$$$-th athlete. Please note that $$$s$$$ values may not be distinct.

For each test case print one integer — the minimum value of $$$|\max(A) - \min(B)|$$$ with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams.

NoteThe first test case was explained in the statement. In the second test case, one of the optimal splits is $$$A=[2, 1]$$$, $$$B=[3, 2, 4, 3]$$$, so the answer is $$$|2-2|=0$$$.

let split_string ?(pattern="") = Str.split @@ Str.regexp pattern
let t = Scanf.sscanf (read_line ()) "%d" @@ fun t -> t

let () =
  for i = 0 to t - 1 do
    let n = Scanf.sscanf (read_line ()) "%d" @@ fun n -> n in
    let s = read_line () |> split_string ~pattern:" " |> List.map int_of_string |> List.sort compare |> Array.of_list in
    let ans = ref max_int in
    for i = 1 to n - 1 do
      ans := min !ans (s.(i) - s.(i - 1))
    done;
    Printf.printf "%d\n" !ans
  done


You got a box with a combination lock. The lock has a display showing n digits. There are two buttons on the box, each button changes digits on the display. You have quickly discovered that the first button adds 1 to all the digits (all digits 9 become digits 0), and the second button shifts all the digits on the display one position to the right (the last digit becomes the first one). For example, if the display is currently showing number 579, then if we push the first button, the display will show 680, and if after that we push the second button, the display will show 068.You know that the lock will open if the display is showing the smallest possible number that can be obtained by pushing the buttons in some order. The leading zeros are ignored while comparing numbers. Now your task is to find the desired number.

The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of digits on the display. The second line contains n digits — the initial state of the display.

Print a single line containing n digits — the desired state of the display containing the smallest possible number.

let s = read_line ();;
let sx = List.map int_of_string (Str.split (Str.regexp " +") s);;
let n = List.hd sx;;

let s = read_line ();;
let x = List.map int_of_string (Str.split (Str.regexp "") s);;

let shift = function
| [] -> []
| x::xs -> xs @ [x]

let rec add = function
| [] -> []
| 9::xs -> 0 :: (add xs)
| x::xs -> (x+1) :: (add xs);;

let smaller xx yy = let rec aux = function
    | (x::xs, y::ys) when x < y -> xx
    | (x::xs, y::ys) when x = y -> aux (xs, ys)
    | (_, _) -> yy in
    aux (xx, yy);;

let rec find_min min x = function
| i when i = 10 -> min
| i ->
    let rec aux min x = function
    | j when j = n -> find_min min (add x) (i+1)
    | j -> aux (smaller x min) (shift x) (j+1) in
    aux min x 0;;

print_newline(List.iter print_int (find_min x x 0));;


Nowadays it is becoming increasingly difficult to park a car in cities successfully. Let's imagine a segment of a street as long as L meters along which a parking lot is located. Drivers should park their cars strictly parallel to the pavement on the right side of the street (remember that in the country the authors of the tasks come from the driving is right side!). Every driver when parking wants to leave for themselves some extra space to move their car freely, that's why a driver is looking for a place where the distance between his car and the one behind his will be no less than b meters and the distance between his car and the one in front of his will be no less than f meters (if there's no car behind then the car can be parked at the parking lot segment edge; the same is true for the case when there're no cars parked in front of the car). Let's introduce an axis of coordinates along the pavement. Let the parking lot begin at point 0 and end at point L. The drivers drive in the direction of the coordinates' increasing and look for the earliest place (with the smallest possible coordinate) where they can park the car. In case there's no such place, the driver drives on searching for his perfect peaceful haven. Sometimes some cars leave the street and free some space for parking. Considering that there never are two moving cars on a street at a time write a program that can use the data on the drivers, entering the street hoping to park there and the drivers leaving it, to model the process and determine a parking lot space for each car.

The first line contains three integers L, b и f (10 ≤ L ≤ 100000, 1 ≤ b, f ≤ 100). The second line contains an integer n (1 ≤ n ≤ 100) that indicates the number of requests the program has got. Every request is described on a single line and is given by two numbers. The first number represents the request type. If the request type is equal to 1, then in that case the second number indicates the length of a car (in meters) that enters the street looking for a place to park. And if the request type is equal to 2, then the second number identifies the number of such a request (starting with 1) that the car whose arrival to the parking lot was described by a request with this number, leaves the parking lot. It is guaranteed that that car was parked at the moment the request of the 2 type was made. The lengths of cars are integers from 1 to 1000.

For every request of the 1 type print number -1 on the single line if the corresponding car couldn't find place to park along the street. Otherwise, print a single number equal to the distance between the back of the car in its parked position and the beginning of the parking lot zone.

let split_by_whitespace s =
  let rec split_loop s idx res =
    let rec length_of_char s idx len =
      if idx >= (String.length s)
      then len
      else
        begin
          if (s.[idx] == ' ' || s.[idx] == '\t' || s.[idx] == '\n')
          then len
          else length_of_char s (idx+1) (len+1)
        end
    in

    if idx >= (String.length s)
    then res
    else
      begin
        if (s.[idx] != ' ' && s.[idx] != '\t' && s.[idx] != '\n')
        then
          begin
            let len = length_of_char s idx 0 in
            split_loop s (idx+len) ((idx, len)::res)
          end
        else split_loop s (idx+1) res
      end
  in

  let whitespaces = split_loop s 0 [] in
  List.fold_left (fun res (idx, len) ->
    (String.sub s idx len)::res
  ) [] whitespaces

let parking_lot l f b cars =
    let check_behind position car_size behind_position = position + car_size + b <= behind_position in

    let park_car req_no car_size parking_lot parking_history =
        if (List.length parking_lot) = 0
        then (
            if check_behind 0 car_size (l+b)
            then ((req_no, 0, car_size)::parking_lot), (0::parking_history)
            else parking_lot, (-1::parking_history)
        )
        else (
            let position, parking_lot =
                List.fold_left (fun (position, parking_lot) (behind_req_no, behind_position, behind_car_size) ->
                    let front_req_no, front_position, front_car_size = List.hd parking_lot in
                    if front_req_no = behind_req_no
                    then position, parking_lot
                    else if position != -1
                    then position, ((behind_req_no, behind_position, behind_car_size)::parking_lot)
                    else (
                        let candidate_position = front_position + front_car_size + f in
                        if check_behind candidate_position car_size behind_position
                        then candidate_position, ((req_no, candidate_position, car_size)::(behind_req_no, behind_position, behind_car_size)::parking_lot)
                        else position, ((behind_req_no, behind_position, behind_car_size)::parking_lot)
                    )
                ) (-1, [(0, 0, -1 * f)]) (parking_lot @ [(0, l + b, 0)])
            in
            let parking_lot =
                List.sort (fun (_,a,_) (_,b,_) -> compare a b)
                    (List.filter (fun (x,_,_) -> x != 0) parking_lot)
            in
            parking_lot, (position::parking_history)
        )
    in

    let leave_car leaving_req_no parking_lot =
        let parking_lot =
            List.fold_left (fun parking_lot (req_no, position, car_size) ->
                if req_no = leaving_req_no
                then parking_lot
                else (req_no, position, car_size)::parking_lot
            ) [] parking_lot
        in
        List.rev parking_lot
    in

    let rec find_lot req_no parking_lot parking_history cars =
        match cars with
        | [] -> List.rev parking_history
        | (req_type,data)::t -> (
            if req_type = 1
            then (
                let parking_lot, parking_history = park_car req_no data parking_lot parking_history in
                find_lot (req_no + 1) parking_lot parking_history t
            )
            else find_lot (req_no + 1) (leave_car data parking_lot) parking_history t
        )
    in
    find_lot 1 [] [] cars

let main =
    let data = List.map int_of_string (split_by_whitespace (read_line())) in
    let l, f, b = (List.nth data 0), (List.nth data 1), (List.nth data 2) in
    let data = List.map int_of_string (split_by_whitespace (read_line())) in
    let req_size = List.nth data 0 in
    let rec parse_req idx reqs =
        if idx == req_size
        then reqs
        else (
            let data = List.map int_of_string (split_by_whitespace (read_line())) in
            parse_req (idx + 1) (((List.nth data 0), (List.nth data 1))::reqs)
        )
    in
    let reqs = parse_req 0 [] in
    let reqs = List.rev reqs in

    let result = parking_lot l f b reqs in
    List.iter (fun x -> print_endline(string_of_int(x))) result

let _ =
    main

let split_by_whitespace s =
  let rec split_loop s idx res =
    let rec length_of_char s idx len =
      if idx >= (String.length s)
      then len
      else
        begin
          if (s.[idx] == ' ' || s.[idx] == '\t' || s.[idx] == '\n')
          then len
          else length_of_char s (idx+1) (len+1)
        end
    in

    if idx >= (String.length s)
    then res
    else
      begin
        if (s.[idx] != ' ' && s.[idx] != '\t' && s.[idx] != '\n')
        then
          begin
            let len = length_of_char s idx 0 in
            split_loop s (idx+len) ((idx, len)::res)
          end
        else split_loop s (idx+1) res
      end
  in

  let whitespaces = split_loop s 0 [] in
  List.fold_left (fun res (idx, len) ->
    (String.sub s idx len)::res
  ) [] whitespaces

let parking_lot l f b cars =
    let check_behind position car_size behind_position = position + car_size + b <= behind_position in

    let park_car req_no car_size parking_lot parking_history =
        if (List.length parking_lot) = 0
        then (
            if check_behind 0 car_size (l+b)
            then ((req_no, 0, car_size)::parking_lot), (0::parking_history)
            else parking_lot, (-1::parking_history)
        )
        else (
            let position, parking_lot =
                List.fold_left (fun (position, parking_lot) (behind_req_no, behind_position, behind_car_size) ->
                    let front_req_no, front_position, front_car_size = List.hd parking_lot in
                    if front_req_no = behind_req_no
                    then position, parking_lot
                    else if position != -1
                    then position, parking_lot
                    else (
                        let candidate_position = front_position + front_car_size + f in
                        if check_behind candidate_position car_size behind_position
                        then candidate_position, ((req_no, candidate_position, car_size)::(behind_req_no, behind_position, behind_car_size)::parking_lot)
                        else position, ((behind_req_no, behind_position, behind_car_size)::parking_lot)
                    )
                ) (-1, [(0, 0, -1 * f)]) (parking_lot @ [(0, l + b, 0)])
            in
            let parking_lot =
                List.sort (fun (_,a,_) (_,b,_) -> compare a b)
                    (List.filter (fun (x,_,_) -> x != 0) parking_lot)
            in
            parking_lot, (position::parking_history)
        )
    in

    let leave_car leaving_req_no parking_lot =
        let parking_lot =
            List.fold_left (fun parking_lot (req_no, position, car_size) ->
                if req_no = leaving_req_no
                then parking_lot
                else (req_no, position, car_size)::parking_lot
            ) [] parking_lot
        in
        List.rev parking_lot
    in

    let rec find_lot req_no parking_lot parking_history cars =
        match cars with
        | [] -> List.rev parking_history
        | (req_type,data)::t -> (
            if req_type = 1
            then (
                let parking_lot, parking_history = park_car req_no data parking_lot parking_history in
                find_lot (req_no + 1) parking_lot parking_history t
            )
            else find_lot (req_no + 1) (leave_car data parking_lot) parking_history t
        )
    in
    find_lot 1 [] [] cars

let main =
    let data = List.map int_of_string (split_by_whitespace (read_line())) in
    let l, f, b = (List.nth data 0), (List.nth data 1), (List.nth data 2) in
    let data = List.map int_of_string (split_by_whitespace (read_line())) in
    let req_size = List.nth data 0 in
    let rec parse_req idx reqs =
        if idx == req_size
        then reqs
        else (
            let data = List.map int_of_string (split_by_whitespace (read_line())) in
            parse_req (idx + 1) (((List.nth data 0), (List.nth data 1))::reqs)
        )
    in
    let reqs = parse_req 0 [] in
    let reqs = List.rev reqs in

    let result = parking_lot l f b reqs in
    List.iter (fun x -> print_endline(string_of_int(x))) result

let _ =
    main

let split_by_whitespace s =
  let rec split_loop s idx res =
    let rec length_of_char s idx len =
      if idx >= (String.length s)
      then len
      else
        begin
          if (s.[idx] == ' ' || s.[idx] == '\t' || s.[idx] == '\n')
          then len
          else length_of_char s (idx+1) (len+1)
        end
    in

    if idx >= (String.length s)
    then res
    else
      begin
        if (s.[idx] != ' ' && s.[idx] != '\t' && s.[idx] != '\n')
        then
          begin
            let len = length_of_char s idx 0 in
            split_loop s (idx+len) ((idx, len)::res)
          end
        else split_loop s (idx+1) res
      end
  in

  let whitespaces = split_loop s 0 [] in
  List.fold_left (fun res (idx, len) ->
    (String.sub s idx len)::res
  ) [] whitespaces

let parking_lot l f b cars =
    let check_behind position car_size behind_position = position + car_size + b <= behind_position in

    let park_car req_no car_size parking_lot parking_history =
        if (List.length parking_lot) = 0
        then (
            if check_behind 0 car_size (l+b)
            then [(req_no, 0, car_size)], (0::parking_history)
            else [], (-1::parking_history)
        )
        else (
            let position, parking_lot =
                List.fold_left (fun (position, parking_lot) (behind_req_no, behind_position, behind_car_size) ->
                    let front_req_no, front_position, front_car_size = List.hd parking_lot in
                    if front_req_no = behind_req_no
                    then position, parking_lot
                    else if position != -1
                    then position, parking_lot
                    else (
                        let candidate_position = front_position + front_car_size + f in
                        if check_behind candidate_position car_size behind_position
                        then (
                            if behind_req_no = 0
                            then candidate_position, ((req_no, candidate_position, car_size)::parking_lot)
                            else candidate_position, ((req_no, candidate_position, car_size)::(behind_req_no, behind_position, behind_car_size)::parking_lot)
                        )
                        else (
                            if behind_req_no = 0
                            then position, parking_lot
                            else position, ((behind_req_no, behind_position, behind_car_size)::parking_lot)
                        )
                    )
                ) (-1, [List.hd parking_lot]) (parking_lot @ [(0, l + b, 0)])
            in
            (List.rev parking_lot), (position::parking_history)
        )
    in

    let leave_car leaving_req_no parking_lot =
        let parking_lot =
            List.fold_left (fun parking_lot (req_no, position, car_size) ->
                if req_no = leaving_req_no
                then parking_lot
                else (req_no, position, car_size)::parking_lot
            ) [] parking_lot
        in
        List.rev parking_lot
    in

    let rec find_lot req_no parking_lot parking_history cars =
        match cars with
        | [] -> List.rev parking_history
        | (req_type,data)::t -> (
            if req_type = 1
            then (
                let parking_lot, parking_history = park_car req_no data parking_lot parking_history in
                find_lot (req_no + 1) parking_lot parking_history t
            )
            else find_lot (req_no + 1) (leave_car data parking_lot) parking_history t
        )
    in
    find_lot 1 [] [] cars

let main =
    let data = List.map int_of_string (split_by_whitespace (read_line())) in
    let l, f, b = (List.nth data 0), (List.nth data 1), (List.nth data 2) in
    let data = List.map int_of_string (split_by_whitespace (read_line())) in
    let req_size = List.nth data 0 in
    let rec parse_req idx reqs =
        if idx == req_size
        then reqs
        else (
            let data = List.map int_of_string (split_by_whitespace (read_line())) in
            parse_req (idx + 1) (((List.nth data 0), (List.nth data 1))::reqs)
        )
    in
    let reqs = parse_req 0 [] in
    let reqs = List.rev reqs in

    let result = parking_lot l f b reqs in
    List.iter (fun x -> print_endline(string_of_int(x))) result

let _ =
    main

let split_by_whitespace s =
  let rec split_loop s idx res =
    let rec length_of_char s idx len =
      if idx >= (String.length s)
      then len
      else
        begin
          if (s.[idx] == ' ' || s.[idx] == '\t' || s.[idx] == '\n')
          then len
          else length_of_char s (idx+1) (len+1)
        end
    in

    if idx >= (String.length s)
    then res
    else
      begin
        if (s.[idx] != ' ' && s.[idx] != '\t' && s.[idx] != '\n')
        then
          begin
            let len = length_of_char s idx 0 in
            split_loop s (idx+len) ((idx, len)::res)
          end
        else split_loop s (idx+1) res
      end
  in

  let whitespaces = split_loop s 0 [] in
  List.fold_left (fun res (idx, len) ->
    (String.sub s idx len)::res
  ) [] whitespaces

let parking_lot l f b cars =
    let check_behind position car_size behind_position = position + car_size + b <= behind_position in

    let park_car req_no car_size parking_lot parking_history =
        if (List.length parking_lot) = 0
        then (
            if check_behind 0 car_size (l+b)
            then [(req_no, 0, car_size)], [0]
            else [], [-1]
        )
        else (
            let position, parking_lot =
                List.fold_left (fun (position, parking_lot) (behind_req_no, behind_position, behind_car_size) ->
                    let front_req_no, front_position, front_car_size = List.hd parking_lot in
                    if front_req_no = behind_req_no
                    then position, parking_lot
                    else if position != -1
                    then position, parking_lot
                    else (
                        let candidate_position = front_position + front_car_size + f in
                        if check_behind candidate_position car_size behind_position
                        then (
                            if behind_req_no = 0
                            then candidate_position, ((req_no, candidate_position, car_size)::parking_lot)
                            else candidate_position, ((req_no, candidate_position, car_size)::(behind_req_no, behind_position, behind_car_size)::parking_lot)
                        )
                        else (
                            if behind_req_no = 0
                            then position, parking_lot
                            else position, ((behind_req_no, behind_position, behind_car_size)::parking_lot)
                        )
                    )
                ) (-1, [List.hd parking_lot]) (parking_lot @ [(0, l + b, 0)])
            in
            (List.rev parking_lot), (position::parking_history)
        )
    in

    let leave_car leaving_req_no parking_lot =
        let parking_lot =
            List.fold_left (fun parking_lot (req_no, position, car_size) ->
                if req_no = leaving_req_no
                then parking_lot
                else (req_no, position, car_size)::parking_lot
            ) [] parking_lot
        in
        List.rev parking_lot
    in

    let rec find_lot req_no parking_lot parking_history cars =
        match cars with
        | [] -> List.rev parking_history
        | (req_type,data)::t -> (
            if req_type = 1
            then (
                let parking_lot, parking_history = park_car req_no data parking_lot parking_history in
                find_lot (req_no + 1) parking_lot parking_history t
            )
            else find_lot (req_no + 1) (leave_car data parking_lot) parking_history t
        )
    in
    find_lot 1 [] [] cars

let main =
    let data = List.map int_of_string (split_by_whitespace (read_line())) in
    let l, f, b = (List.nth data 0), (List.nth data 1), (List.nth data 2) in
    let data = List.map int_of_string (split_by_whitespace (read_line())) in
    let req_size = List.nth data 0 in
    let rec parse_req idx reqs =
        if idx == req_size
        then reqs
        else (
            let data = List.map int_of_string (split_by_whitespace (read_line())) in
            parse_req (idx + 1) (((List.nth data 0), (List.nth data 1))::reqs)
        )
    in
    let reqs = parse_req 0 [] in
    let reqs = List.rev reqs in

    let result = parking_lot l f b reqs in
    List.iter (fun x -> print_endline(string_of_int(x))) result

let _ =
    main

let split_by_whitespace s =
  let rec split_loop s idx res =
    let rec length_of_char s idx len =
      if idx >= (String.length s)
      then len
      else
        begin
          if (s.[idx] == ' ' || s.[idx] == '\t' || s.[idx] == '\n')
          then len
          else length_of_char s (idx+1) (len+1)
        end
    in

    if idx >= (String.length s)
    then res
    else
      begin
        if (s.[idx] != ' ' && s.[idx] != '\t' && s.[idx] != '\n')
        then
          begin
            let len = length_of_char s idx 0 in
            split_loop s (idx+len) ((idx, len)::res)
          end
        else split_loop s (idx+1) res
      end
  in

  let whitespaces = split_loop s 0 [] in
  List.fold_left (fun res (idx, len) ->
    (String.sub s idx len)::res
  ) [] whitespaces

let parking_lot l f b cars =
    let check_behind position car_size behind_position = position + car_size + b <= behind_position in

    let park_car req_no car_size parking_lot parking_history =
        if (List.length parking_lot) = 0
        then (
            if check_behind 0 car_size (l+b)
            then [(req_no, 0, car_size)], [0]
            else [], [-1]
        )
        else (
            let position, parking_lot =
                List.fold_left (fun (position, parking_lot) (behind_req_no, behind_position, behind_car_size) ->
                    let front_req_no, front_position, front_car_size = List.hd parking_lot in
                    if front_req_no = behind_req_no
                    then position, parking_lot
                    else if position != -1
                    then position, parking_lot
                    else (
                        let candidate_position = front_position + front_car_size + f in
                        if check_behind candidate_position car_size behind_position
                        then (
                            if behind_req_no = 0
                            then candidate_position, ((req_no, candidate_position, car_size)::parking_lot)
                            else candidate_position, ((req_no, candidate_position, car_size)::(behind_req_no, behind_position, behind_car_size)::parking_lot)
                        )
                        else (
                            if behind_req_no = 0
                            then position, parking_lot
                            else position, ((behind_req_no, behind_position, behind_car_size)::parking_lot)
                        )
                    )
                ) (-1, [List.hd parking_lot]) (parking_lot @ [(0, l + b, 0)])
            in
            (List.rev parking_lot), (position::parking_history)
        )
    in

    let leave_car leaving_req_no parking_lot =
        let parking_lot =
            List.fold_left (fun parking_lot (req_no, position, car_size) ->
                if req_no = leaving_req_no
                then parking_lot
                else (req_no, position, car_size)::parking_lot
            ) [] parking_lot
        in
        List.rev parking_lot
    in

    let rec find_lot req_no parking_lot parking_history cars =
        match cars with
        | [] -> List.rev parking_history
        | (req_type,data)::t -> (
            if req_type = 1
            then (
                let parking_lot, parking_history = park_car req_no data parking_lot parking_history in
                find_lot (req_no + 1) parking_lot parking_history t
            )
            else find_lot (req_no + 1) (leave_car data parking_lot) parking_history t
        )
    in
    find_lot 1 [] [] cars

let main =
    let data = List.map int_of_string (split_by_whitespace (read_line())) in
    let l, f, b = (List.nth data 0), (List.nth data 1), (List.nth data 2) in
    let data = List.map int_of_string (split_by_whitespace (read_line())) in
    let req_size = List.nth data 0 in
    let rec parse_req idx reqs =
        if idx == req_size
        then reqs
        else (
            let data = List.map int_of_string (split_by_whitespace (read_line())) in
            parse_req (idx + 1) (((List.nth data 0), (List.nth data 1))::reqs)
        )
    in
    let reqs = parse_req 0 [] in
    let reqs = List.rev reqs in

    let result = parking_lot l f b reqs in
    List.map string_of_int result

let _ =
    main


After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if:   $$$i = j$$$, that is, they are at the same index.  $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times:   choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.

The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation.

Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.

NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$.

open Printf
open Scanf

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let maxf i j f = fold (i+1) j (fun k a -> max (f k) a) (f i)
let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)

let () =
  let n = read_int() in

  let a = Array.init n (fun _ -> -1 + read_int()) in
  let b = Array.init n (fun _ -> -1 + read_int()) in

  let ainv = Array.make n 0 in
  for i=0 to n-1 do
    ainv.(a.(i)) <- i;
  done;

  (*  let b' = Array.init n (fun i -> b.(ainv.(i))) in *)
  let b' = Array.init n (fun i -> ainv.(b.(i))) in
  let bb = Array.init n (fun i -> (b'.(i) - i + n) mod n) in

  let hist = Array.make n 0 in

  for i=0 to n-1 do
    hist.(bb.(i)) <- 1 + hist.(bb.(i))
  done;

  let answer = maxf 0 (n-1) (fun i -> hist.(i)) in

  printf "%d\n" answer

open Printf
open Scanf

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let maxf i j f = fold (i+1) j (fun k a -> max (f k) a) (f i)
let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)

let () =
  let n = read_int() in

  let a = Array.init n (fun _ -> -1 + read_int()) in
  let b = Array.init n (fun _ -> -1 + read_int()) in

  let ainv = Array.make n 0 in
  for i=0 to n-1 do
    ainv.(a.(i)) <- i;
  done;

  let b' = Array.init n (fun i -> b.(ainv.(i))) in
  let bb = Array.init n (fun i -> (b'.(i) - i + n) mod n) in

  let hist = Array.make n 0 in

  for i=0 to n-1 do
    hist.(bb.(i)) <- 1 + hist.(bb.(i))
  done;

  let answer = maxf 0 (n-1) (fun i -> hist.(i)) in

  printf "%d\n" answer


The Little Elephant has an integer a, written in the binary notation. He wants to write this number on a piece of paper.To make sure that the number a fits on the piece of paper, the Little Elephant ought to delete exactly one any digit from number a in the binary record. At that a new number appears. It consists of the remaining binary digits, written in the corresponding order (possible, with leading zeroes).The Little Elephant wants the number he is going to write on the paper to be as large as possible. Help him find the maximum number that he can obtain after deleting exactly one binary digit and print it in the binary notation.

The single line contains integer a, written in the binary notation without leading zeroes. This number contains more than 1 and at most 105 digits.

In the single line print the number that is written without leading zeroes in the binary notation — the answer to the problem.

NoteIn the first sample the best strategy is to delete the second digit. That results in number 112 = 310.In the second sample the best strategy is to delete the third or fourth digits — that results in number 110102 = 2610.

(* Codeforces 257B SlonMagSquare *)

open String;;

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 Pervasives.stdin;;
(* let spli s = Str.split (Str.regexp " ") s;; *)
let printlisti li = List.iter (fun i -> (print_int i; print_string " ")) li;;
let printarri ai = Array.iter (fun i -> (print_int i; print_string " ")) ai;;
let debug = false;;

let remove_first_zero s =
	let ln = String.length s in
	if not (String.contains s '0') then String.sub s 1 (ln-1)
	else
		let idxz = String.index s '0' in
		let a = String.sub s 0 idxz in
		let b = String.sub s (idxz+1) (ln-idxz-1) in
		a ^ b;;

let main () =
	let s = rdln() in
	let os = remove_first_zero s in
	begin
		print_string os;
		print_newline()
	end;;

main();;

let () =
  let s = read_line() in
  let n = String.length s in
  let b = Array.init n (fun i -> if s.[i]='0' then 0 else 1) in

  let rec loop i c =
    if i=n-1 && c=0 then ()
    else if i=n-1 && c>0 then Printf.printf "%d" b.(i)
    else if b.(i)=0 && c>0 then (Printf.printf "0"; loop (i+1) c)
    else if b.(i)=0 && c=0 then loop (i+1) 1
    else (Printf.printf "1"; loop (i+1) c)
  in

    loop 0 0;
    print_newline()


Inna loves sleeping very much, so she needs n alarm clocks in total to wake up. Let's suppose that Inna's room is a 100 × 100 square with the lower left corner at point (0, 0) and with the upper right corner at point (100, 100). Then the alarm clocks are points with integer coordinates in this square.The morning has come. All n alarm clocks in Inna's room are ringing, so Inna wants to turn them off. For that Inna has come up with an amusing game:  First Inna chooses a type of segments that she will use throughout the game. The segments can be either vertical or horizontal.  Then Inna makes multiple moves. In a single move, Inna can paint a segment of any length on the plane, she chooses its type at the beginning of the game (either vertical or horizontal), then all alarm clocks that are on this segment switch off. The game ends when all the alarm clocks are switched off. Inna is very sleepy, so she wants to get through the alarm clocks as soon as possible. Help her, find the minimum number of moves in the game that she needs to turn off all the alarm clocks!

The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of the alarm clocks. The next n lines describe the clocks: the i-th line contains two integers xi, yi — the coordinates of the i-th alarm clock (0 ≤ xi, yi ≤ 100). Note that a single point in the room can contain any number of alarm clocks and the alarm clocks can lie on the sides of the square that represents the room.

In a single line print a single integer — the minimum number of segments Inna will have to draw if she acts optimally.

NoteIn the first sample, Inna first chooses type "vertical segments", and then she makes segments with ends at : (0, 0), (0, 2); and, for example, (1, 0), (1, 1). If she paints horizontal segments, she will need at least 3 segments.In the third sample it is important to note that Inna doesn't have the right to change the type of the segments during the game. That's why she will need 3 horizontal or 3 vertical segments to end the game.

let read_int()=Scanf.scanf " %d" (fun x->x);;
let n=read_int();;
let tab=Array.make 101 0;;
let vec=Array.make 101 0;;
let s=ref 0;;
let t=ref 0;;
for i=1 to n do
   let a=read_int() and b=read_int() in
   if tab.(a)=0 then (tab.(a)<-1;s:= !s+1);
   if vec.(b)=0 then (vec.(b)<-1;t:= !t+1)
done;;
print_int (min !s !t);;

let read_int()=Scanf.scanf " %d" (fun x->x);;
let n=read_int();;
let tab=Array.make 101 0;;
let vec=Array.make 101 0;;
let s=ref 0;;
let t=ref 0;;
for i=1 to n do
   let a=read_int() and b=read_int() in
   if tab.(a)=0 then (tab.(a)<-1;s:= !s+1);
   if vec.(b)=0 then (vec.(b)<-1;t:= !t+1)
done;;
print_int (max !s !t);;


Student Vladislav came to his programming exam completely unprepared as usual. He got a question about some strange algorithm on a graph — something that will definitely never be useful in real life. He asked a girl sitting next to him to lend him some cheat papers for this questions and found there the following definition:The minimum spanning tree T of graph G is such a tree that it contains all the vertices of the original graph G, and the sum of the weights of its edges is the minimum possible among all such trees.Vladislav drew a graph with n vertices and m edges containing no loops and multiple edges. He found one of its minimum spanning trees and then wrote for each edge its weight and whether it is included in the found tree or not. Unfortunately, the piece of paper where the graph was painted is gone and the teacher is getting very angry and demands to see the original graph. Help Vladislav come up with a graph so that the information about the minimum spanning tree remains correct.

The first line of the input contains two integers n and m () — the number of vertices and the number of edges in the graph. Each of the next m lines describes an edge of the graph and consists of two integers aj and bj (1 ≤ aj ≤ 109, bj = {0, 1}). The first of these numbers is the weight of the edge and the second number is equal to 1 if this edge was included in the minimum spanning tree found by Vladislav, or 0 if it was not. It is guaranteed that exactly n - 1 number {bj} are equal to one and exactly m - n + 1 of them are equal to zero.

If Vladislav has made a mistake and such graph doesn't exist, print  - 1. Otherwise print m lines. On the j-th line print a pair of vertices (uj, vj) (1 ≤ uj, vj ≤ n, uj ≠ vj), that should be connected by the j-th edge. The edges are numbered in the same order as in the input. The graph, determined by these edges, must be connected, contain no loops or multiple edges and its edges with bj = 1 must define the minimum spanning tree. In case there are multiple possible solutions, print any of them.

open Printf
open Scanf

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

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

  let e = Array.make ne (0,0) in
  let t = Array.make nt (0,0) in

  let rec read_loop it ie = if it+ie = m then () else
    let a = (read_int(), it+ie) in
    let b = read_int() in
    if b=1 then (
      t.(it) <- a;
      read_loop (it+1) ie
    ) else (
      e.(ie) <- a;
      read_loop it (ie+1)
    )
  in

  read_loop 0 0;

  Array.sort compare t;
  Array.sort compare e;

  let rec gen_pair_list (i,j) k ac = if k = ne then List.rev ac else
      let ac = (i,j)::ac in
      let next = if i+1 = j then (0,j+1) else (i+1,j) in
      gen_pair_list next (k+1) ac
  in

  let pair_array = Array.of_list (gen_pair_list (0,1) 0 []) in

  let rec build_answer i ac = if i=ne then Some (List.rev ac) else
      let (ii,jj) = pair_array.(i) in
      let elength = fst e.(i) in
      if elength < fst t.(jj) then None else
	let ac = (ii,jj)::ac in
	build_answer (i+1) ac
  in

  match build_answer 0 [] with
    | None -> printf "-1\n"
    | Some li ->
      let po = Array.make m (0,0) in
      List.iteri (fun i (ii,jj) ->
	po.(snd e.(i)) <- (ii+2, jj+2)
      ) li;
      for i=0 to nt-1 do
	po.(snd t.(i)) <- (1, i+2)
      done;
      for i=0 to m-1 do
	printf "%d %d\n" (fst po.(i)) (snd po.(i))
      done

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

let () =
  let n = read_int () in
  let m = read_int () in
  let a = Array.init m (fun i -> let a = read_int () in let b = read_int () in (a, b, i)) in
  let cmp (x1, y1, _) (x2, y2, _) =
    if x1 = x2 then y2 - y1
    else x1 - x2
  in
  let _ = Array.sort cmp a in
  let ans = Array.make m (0, 0) in

  let rec add_extra (extra, extra_cnt) x cur =
    if extra_cnt >= m || x = cur then (extra, extra_cnt)
    else add_extra ((x, cur)::extra, extra_cnt + 1) (x+1) cur
  in

  let process_edge (extra, extra_cnt, cur) (a, b, i) =
    if extra_cnt = (-1) then (extra, extra_cnt, cur)
    else if b = 0 then
      begin
        match extra with
        | [] -> (extra, (-1), cur)
        | hd::tl -> ans.(i) <- hd; (tl, extra_cnt-1, cur)
      end
    else
      begin
        ans.(i) <- (0, cur);
        let (extra, extra_cnt) = add_extra (extra, extra_cnt) 1 cur in
        (extra, extra_cnt, cur + 1)
      end
  in

  let (_, extra_cnt, _) = Array.fold_left process_edge ([], 0, 1) a in
  if extra_cnt = (-1) then Printf.printf "-1\n"
  else Array.iter (fun (a, b) -> Printf.printf "%d %d\n" (a+1) (b+1)) ans

open Printf
open Scanf

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

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

  let e = Array.make ne (0,0) in
  let t = Array.make nt (0,0) in

  let rec read_loop it ie = if it+ie = m then () else
    let a = (read_int(), it+ie) in
    let b = read_int() in
    if b=1 then (
      t.(it) <- a;
      read_loop (it+1) ie
    ) else (
      e.(ie) <- a;
      read_loop it (ie+1)
    )
  in

  read_loop 0 0;

  Array.sort compare t;
  Array.sort compare e;

  let rec gen_pair_list (i,j) k ac = if k = ne then ac else
      let ac = (i,j)::ac in
      let next = if i+1 = j then (0,j+1) else (i+1,j) in
      gen_pair_list next (k+1) ac
  in

  let pair_array = Array.of_list (gen_pair_list (0,1) 0 []) in

  let rec build_answer i ac = if i=ne then Some (List.rev ac) else
      let (ii,jj) = pair_array.(i) in
      let elength = fst e.(i) in
      if elength < fst t.(jj) then None else
	let ac = (ii,jj)::ac in
	build_answer (i+1) ac
  in

  match build_answer 0 [] with
    | None -> printf "-1\n"
    | Some li ->
      let po = Array.make m (0,0) in
      List.iteri (fun i (ii,jj) ->
	po.(snd e.(i)) <- (ii+2, jj+2)
      ) li;
      for i=0 to nt-1 do
	po.(snd t.(i)) <- (1, i+2)
      done;
      for i=0 to m-1 do
	printf "%d %d\n" (fst po.(i)) (snd po.(i))
      done

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

let () =
  let n = read_int () in
  let m = read_int () in
  let a = Array.init m (fun i -> let a = read_int () in let b = read_int () in (a, b, i)) in
  let _ = Array.sort (fun (x1, y1, _) (x2, y2, _) -> (x1*2+1-y1) - (x2*2+1-y2)) a in
  let ans = Array.make m (0, 0) in

  let rec add_extra (extra, extra_cnt) x cur =
    if extra_cnt >= m || x = cur then (extra, extra_cnt)
    else add_extra ((x, cur)::extra, extra_cnt + 1) (x+1) cur
  in

  let process_edge (extra, extra_cnt, cur) (a, b, i) =
    if extra_cnt = (-1) then (extra, extra_cnt, cur)
    else if b = 0 then
      begin
        match extra with
        | [] -> (extra, (-1), cur)
        | hd::tl -> ans.(i) <- hd; (tl, extra_cnt-1, cur)
      end
    else
      begin
        ans.(i) <- (0, cur);
        let (extra, extra_cnt) = add_extra (extra, extra_cnt) 1 cur in
        (extra, extra_cnt, cur + 1)
      end
  in

  let (_, extra_cnt, _) = Array.fold_left process_edge ([], 0, 1) a in
  if extra_cnt = (-1) then Printf.printf "-1\n"
  else Array.iter (fun (a, b) -> Printf.printf "%d %d\n" (a+1) (b+1)) ans

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

let () =
  let n = read_int () in
  let m = read_int () in
  let a = Array.init m (fun i -> let a = read_int () in let b = read_int () in (a, b, i)) in
  let cmp (x1, y1, _) (x2, y2, _) =
    if x1 = x2 then y1 - y2
    else x1 - x2
  in
  let _ = Array.sort cmp a in
  let ans = Array.make m (0, 0) in

  let rec add_extra (extra, extra_cnt) x cur =
    if extra_cnt >= m || x = cur then (extra, extra_cnt)
    else add_extra ((x, cur)::extra, extra_cnt + 1) (x+1) cur
  in

  let process_edge (extra, extra_cnt, cur) (a, b, i) =
    if extra_cnt = (-1) then (extra, extra_cnt, cur)
    else if b = 0 then
      begin
        match extra with
        | [] -> (extra, (-1), cur)
        | hd::tl -> ans.(i) <- hd; (tl, extra_cnt-1, cur)
      end
    else
      begin
        ans.(i) <- (0, cur);
        let (extra, extra_cnt) = add_extra (extra, extra_cnt) 1 cur in
        (extra, extra_cnt, cur + 1)
      end
  in

  let (_, extra_cnt, _) = Array.fold_left process_edge ([], 0, 1) a in
  if extra_cnt = (-1) then Printf.printf "-1\n"
  else Array.iter (fun (a, b) -> Printf.printf "%d %d\n" (a+1) (b+1)) ans

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

let () =
  let n = read_int () in
  let m = read_int () in
  let a = Array.init m (fun i -> let a = read_int () in let b = read_int () in (a, b, i)) in
  let _ = Array.sort (fun (x1, _, _) (x2, _, _) -> x1 - x2) a in
  let ans = Array.make m (0, 0) in

  let rec add_extra (extra, extra_cnt) x cur =
    if extra_cnt >= m || x = cur then (extra, extra_cnt)
    else add_extra ((x, cur)::extra, extra_cnt + 1) (x+1) cur
  in

  let process_edge (extra, extra_cnt, cur) (a, b, i) =
    if extra_cnt = (-1) then (extra, extra_cnt, cur)
    else if b = 0 then
      begin
        match extra with
        | [] -> (extra, (-1), cur)
        | hd::tl -> ans.(i) <- hd; (tl, extra_cnt-1, cur)
      end
    else
      begin
        ans.(i) <- (0, cur);
        let (extra, extra_cnt) = add_extra (extra, extra_cnt) 1 cur in
        (extra, extra_cnt, cur + 1)
      end
  in

  let (_, extra_cnt, _) = Array.fold_left process_edge ([], 0, 1) a in
  if extra_cnt = (-1) then Printf.printf "-1\n"
  else Array.iter (fun (a, b) -> Printf.printf "%d %d\n" (a+1) (b+1)) ans