Billy investigates the question of applying greedy algorithm to different spheres of life. At the moment he is studying the application of greedy algorithm to the problem about change. There is an amount of n coins of different face values, and the coins of each value are not limited in number. The task is to collect the sum x with the minimum amount of coins. Greedy algorithm with each its step takes the coin of the highest face value, not exceeding x. Obviously, if among the coins' face values exists the face value 1, any sum x can be collected with the help of greedy algorithm. However, greedy algorithm does not always give the optimal representation of the sum, i.e. the representation with the minimum amount of coins. For example, if there are face values {1, 3, 4} and it is asked to collect the sum 6, greedy algorithm will represent the sum as 4 + 1 + 1, while the optimal representation is 3 + 3, containing one coin less. By the given set of face values find out if there exist such a sum x that greedy algorithm will collect in a non-optimal way. If such a sum exists, find out the smallest of these sums.

The first line contains an integer n (1 ≤ n ≤ 400) — the amount of the coins' face values. The second line contains n integers ai (1 ≤ ai ≤ 109), describing the face values. It is guaranteed that a1 > a2 > ... > an and an = 1.

If greedy algorithm collects any sum in an optimal way, output -1. Otherwise output the smallest sum that greedy algorithm collects in a non-optimal way.

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

let () =
  let n = read_int 0 in
  let a = Array.init n read_int in
  let ans = ref (-1) in
  for i = 1 to n-1 do
    for j = i to n-1 do
      (* representation of a_{i-1}-1 *)
      let p = a.(i-1)-1 |> ref
      and c = ref 1
      and w = ref a.(j) in
      for k = i to j do
        let t = !p / a.(k) in
        c := !c + t;
        w := !w + a.(k) * t;
        p := !p mod a.(k)
      done;

      p := !w;
      for k = 0 to n-1 do
        c := !c - !p / a.(k);
        p := !p mod a.(k)
      done;
      if !c < 0 && (!ans < 0 || !w < !ans) then
        ans := !w;
    done
  done;
  Printf.printf "%d\n" !ans

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

let () =
  let n = read_int 0 in
  let a = Array.init n read_int in
  let ans = ref (-1) in
  for i = 1 to n-1 do
    for j = i to n-1 do
      let p = a.(i-1)-1 |> ref
      and c = ref 1
      and w = ref a.(j) in
      for k = i to j do
        let t = !p / a.(k) in
        c := !c + t;
        w := !w + a.(k) * t;
        p := !p mod a.(k)
      done;
      if !p = 0 then (
        p := !w;
        for k = 0 to n-1 do
          c := !c - !p / a.(k);
          p := !p mod a.(k)
        done
      );
      if !c < 0 && (!ans < 0 || !w < !ans) then
        ans := !w;
    done
  done;
  Printf.printf "%d\n" !ans

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

let () =
  let n = read_int 0 in
  let a = Array.init n read_int in
  let ans = ref (-1) in
  for i = 1 to n-1 do
    for j = i to n-1 do
      let p = a.(i-1)-1 |> ref
      and c = ref 0
      and w = ref a.(j) in
      for k = i to j do
        let t = !p / a.(k) in
        c := !c + t;
        w := !w + a.(k) * t;
        p := !p mod a.(k)
      done;
      if !p = 0 then (
        p := !w;
        for k = 0 to n-1 do
          c := !c - !p / a.(k);
          p := !p mod a.(k)
        done
      );
      if !c < 0 && (!ans < 0 || !w < !ans) then
        ans := !w;
    done
  done;
  Printf.printf "%d\n" !ans


You are given an array of n integer numbers a0, a1, ..., an - 1. Find the distance between two closest (nearest) minimums in it. It is guaranteed that in the array a minimum occurs at least two times.

The first line contains positive integer n (2 ≤ n ≤ 105) — size of the given array. The second line contains n integers a0, a1, ..., an - 1 (1 ≤ ai ≤ 109) — elements of the array. It is guaranteed that in the array a minimum occurs at least two times.

Print the only number — distance between two nearest minimums in the array.

let split = Str.split (Str.regexp " ")

let () =
  let _ = read_line () in (* skip the 1st line *)
  let ns = read_line ()
  |> split
  |> (List.map int_of_string) in
  let minimum = List.fold_left min max_int ns in
  let rec nearest d p i ns =
    match ns with
      [] -> d
    | head :: tail -> if head = minimum
      then nearest (min d (i-p)) i (i+1) tail
      else nearest d p (i+1) tail in
  print_int (nearest 100000 (-100000) 0 ns);
  print_newline ()


Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings.There are n rings in factory's stock. The i-th ring has inner radius ai, outer radius bi and height hi. The goal is to select some subset of rings and arrange them such that the following conditions are satisfied:  Outer radiuses form a non-increasing sequence, i.e. one can put the j-th ring on the i-th ring only if bj ≤ bi.  Rings should not fall one into the the other. That means one can place ring j on the ring i only if bj > ai.  The total height of all rings used should be maximum possible.

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of rings in factory's stock. The i-th of the next n lines contains three integers ai, bi and hi (1 ≤ ai, bi, hi ≤ 109, bi > ai) — inner radius, outer radius and the height of the i-th ring respectively.

Print one integer — the maximum height of the tower that can be obtained.

NoteIn the first sample, the optimal solution is to take all the rings and put them on each other in order 3, 2, 1.In the second sample, one can put the ring 3 on the ring 4 and get the tower of height 3, or put the ring 1 on the ring 2 and get the tower of height 4.

(*********************************************************************************)

type disk = {
	a : int; (* inner radius *)
	b : int; (* outer radius *)
	h : int64; (* dist height *)
};;

(*********************************************************************************)

let read_disks n =
	let arr = Array.make n {a=0; b=0; h=Int64.zero} in
	begin
		for k=0 to (n - 1) do
			Scanf.bscanf Scanf.Scanning.stdin " %d %d %Ld " (fun a b h -> arr.(k) <- {a=a; b=b; h=h};);
		done;
		arr;
	end
;;

(*********************************************************************************)

let lower_bound arr n x less =
	let l = ref 0 in
	let r = ref n in
	begin
		while !l < !r do
			let mid = !l + (!r - !l) / 2 in
			if (less arr.(mid) x) then
				begin
					l := mid + 1;
				end
			else
				begin
					r := mid;
				end
		done;
		!r
	end
;;

(*********************************************************************************)
let upper_bound arr n x less =
	let l = ref 0 in
	let r = ref n in
	begin
		while !l < !r do
			let mid = !l + (!r - !l) / 2 in
			if (less x arr.(mid)) then
				begin
					r := mid;
				end
			else (* a[mid] <= x *)
				begin
					l := mid + 1;
				end
		done;
		!r
	end
;;

(*********************************************************************************)

let find_highest_tower arr =
	(* precondition: arr is sorted by increasing disk.b *)
	let n = Array.length arr in
	let tw = Array.make n 0 in
	(*
		Towers are arranged according to increase of width and decrease of height.
		In essence tower with a smaller width can precede a tower with a greater width only if it is higher.
	*)
	let th = Array.make n Int64.zero in
	let m = ref 0 in (* amount of towers to lookup *)
	begin
	for k = 0 to n - 1 do
		let d = arr.(k) in
		let j = upper_bound tw !m d.a (<) in (* find the tower with width > outer radius of the disk, or return the end of tower list *)
		let t = if j < !m then (Int64.add th.(j) d.h) else d.h in
		let mm = lower_bound th j t (>) in (* find first tower which is not taller than the new one *)
		begin
			m := mm + 1; (* no point to search beyond the widest tower *)
			tw.(mm) <- d.b;
			th.(mm) <- t;
			(*Printf.printf "j=%d, m=%d, w=%d, h=%Ld\n" j !m d.b t;*)
		end
	done;
	th.( 0 )
	end
;;

(*********************************************************************************)

let cmp_disks d1 d2 =
	if d1.b < d2.b then -1
	else if d2.b < d1.b then 1
	else compare d1.a d2.a
;;

(*********************************************************************************)

let read_and_process n =
	let a = read_disks n in
	begin
		Array.sort cmp_disks a;
		Printf.printf "%Ld\n" (find_highest_tower a);
	end
;;

(*********************************************************************************)

let read_input () =
	Scanf.bscanf Scanf.Scanning.stdin " %d " read_and_process
;;

(*********************************************************************************)

read_input ()

(*********************************************************************************)

type disk = {
	a : int; (* inner radius *)
	b : int; (* outer radius *)
	h : int; (* dist height *)
	mutable t : int; (* height of tallest tower containing this disk *)
};;

(*********************************************************************************)

let print_disks a =
	let n = Array.length a in
	for k = 0 to n - 1 do
		let d = a.(k) in
		Printf.printf "a=%d; b=%d; h=%d; t=%d;\n" d.a d.b d.h d.t;
	done;
	(*print_newline ();*)
;;

(*********************************************************************************)

let create_disk va vb vh = { a=va; b=vb; h=vh; t=vh };;

(*********************************************************************************)

let read_disks n =
	let arr = Array.make n (create_disk 0 0 0) in
	begin
		for k=0 to (n - 1) do
			Scanf.bscanf Scanf.Scanning.stdin " %d %d %d " (fun a b h -> arr.(k) <- (create_disk a b h););
		done;
		arr;
	end
;;

(*********************************************************************************)

let find_highest_tower arr =
	(* precondition: arr is sorted by increasing disk.b *)
	let n = Array.length arr in
	let tt = ref 0 in (* height of the tallest tower *)
	begin
		for k = 0 to n - 1 do (* Invariant k: tt contains height of the tallest tower from arr[0] to arr[k-1] *)
			let d = arr.(k) in
			let t = ref 0 in (* height of the tallest tower involving disk d.(k) *)
			begin
				(*
				*	We need to check only the towers ending with the disk of outer radius <= d.b
				*	because the towers ending with wider disks cannot be placed on top of disk d.
				*	All these good towers are to the left of k-th disk because by precondition our array is sorted by disk.b
				*
				*	Invariant j:
				*	------------
				*	t is the height of the tallest tower such that it contains some (possibly all or none)
				*	properly arranged disks in the range (j..k)
				*)
				for j=(k-1) downto 0 do
					let dd = arr.(j) in
					(*
					*	Only disks with outer radius greater than arr[k].b can be put on top of k-th disk
					*)
					t := if dd.b > d.a then max !t dd.t else !t;
				done;
				d.t <- (!t + d.h);
				(*
					The tallest tower from 0-th to k-th is either:
					1. The tallest one among those containing the k-th disk
					2. The tallest one among those which do not contain k-th disk
				*)
				tt := max !tt d.t;
			end
		done;

		!tt
	end
;;

(*********************************************************************************)

let read_and_process n =
	let a = read_disks n in
	begin
		Array.sort (fun d1 d2 -> compare d1.b d2.b) a;
		Printf.printf "%d\n" (find_highest_tower a);
	end
;;

(*********************************************************************************)

let read_input () =
	Scanf.bscanf Scanf.Scanning.stdin " %d " read_and_process
;;

(*********************************************************************************)

read_input ()

(*********************************************************************************)

type disk = {
	a : int; (* inner radius *)
	b : int; (* outer radius *)
	h : int; (* dist height *)
	mutable t : int; (* height of tallest tower containing this disk *)
};;

(*********************************************************************************)

let print_disks a =
	let n = Array.length a in
	for k = 0 to n - 1 do
		let d = a.(k) in
		Printf.printf "a=%d; b=%d; h=%d; t=%d;\n" d.a d.b d.h d.t;
	done;
	(*print_newline ();*)
;;

(*********************************************************************************)

let create_disk va vb vh = { a=va; b=vb; h=vh; t=vh };;

(*********************************************************************************)

let read_disks n =
	let arr = Array.make n (create_disk 0 0 0) in
	begin
		for k=0 to (n - 1) do
			Scanf.bscanf Scanf.Scanning.stdin " %d %d %d " (fun a b h -> arr.(k) <- (create_disk a b h););
		done;
		arr;
	end
;;

(*********************************************************************************)

let find_highest_tower arr =
	(* precondition: arr is sorted by increasing disk.b *)
	let n = Array.length arr in
	let tt = ref 0 in (* height of the tallest tower *)
	begin
		for k = 0 to n - 1 do (* Invariant k: tt contains height of the tallest tower from arr[0] to arr[k-1] *)
			let d = arr.(k) in
			let t = ref 0 in (* height of the tallest tower involving disk d.(k) *)
			begin
				(*
				*	We need to check only the towers ending with the disk of outer radius <= d.b
				*	because the towers ending with wider disks cannot be placed on top of disk d.
				*	All these good towers are to the left of k-th disk because by precondition our array is sorted by disk.b
				*
				*	Invariant j:
				*	------------
				*	t is the height of the tallest tower such that it contains some (possibly all or none)
				*	properly arranged disks in the range (j..k)
				*)
				for j=(k-1) downto 0 do
					let dd = arr.(j) in
					(*
					*	Only disks with outer radius greater than arr[k].b can be put on top of k-th disk
					*)
					t := if dd.b > d.a then max !t dd.t else !t;
				done;
				d.t <- (!t + d.h);
				(*
					The tallest tower from 0-th to k-th is either:
					1. The tallest one among those containing the k-th disk
					2. The tallest one among those which do not contain k-th disk
				*)
				tt := max !tt d.t;
			end
		done;

		!tt
	end
;;

(*********************************************************************************)

let cmp_disks d1 d2 =
	if d1.b < d2.b then -1
	else if d2.b < d1.b then 1
	else compare d1.a d2.a
;;

(*********************************************************************************)

let read_and_process n =
	let a = read_disks n in
	begin
		Array.sort cmp_disks a;
		Printf.printf "%d\n" (find_highest_tower a);
	end
;;

(*********************************************************************************)

let read_input () =
	Scanf.bscanf Scanf.Scanning.stdin " %d " read_and_process
;;

(*********************************************************************************)

read_input ()


In the evening, after the contest Ilya was bored, and he really felt like maximizing. He remembered that he had a set of n sticks and an instrument. Each stick is characterized by its length li.Ilya decided to make a rectangle from the sticks. And due to his whim, he decided to make rectangles in such a way that maximizes their total area. Each stick is used in making at most one rectangle, it is possible that some of sticks remain unused. Bending sticks is not allowed.Sticks with lengths a1, a2, a3 and a4 can make a rectangle if the following properties are observed:  a1 ≤ a2 ≤ a3 ≤ a4  a1 = a2  a3 = a4 A rectangle can be made of sticks with lengths of, for example, 3 3 3 3 or 2 2 4 4. A rectangle cannot be made of, for example, sticks 5 5 5 7.Ilya also has an instrument which can reduce the length of the sticks. The sticks are made of a special material, so the length of each stick can be reduced by at most one. For example, a stick with length 5 can either stay at this length or be transformed into a stick of length 4.You have to answer the question — what maximum total area of the rectangles can Ilya get with a file if makes rectangles from the available sticks?

The first line of the input contains a positive integer n (1 ≤ n ≤ 105) — the number of the available sticks. The second line of the input contains n positive integers li (2 ≤ li ≤ 106) — the lengths of the sticks.

The first line of the output must contain a single non-negative integer — the maximum total area of the rectangles that Ilya can make from the available sticks.

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n 0 in
  let _ =
    for i = 0 to n - 1 do
      a.(i) <- Scanf.bscanf sb "%d " (fun s -> s)
    done;
    Array.sort compare a;
  in
  let xs = ref [] in
  let pos = ref (n - 1) in
  let _ =
    while !pos > 0 do
      if a.(!pos) = a.(!pos - 1) ||
	a.(!pos) = a.(!pos - 1) + 1
      then (
	xs := a.(!pos - 1) :: !xs;
	pos := !pos - 2
      ) else decr pos
    done;
  in
  let xs = Array.of_list (List.rev !xs) in
  let res = ref 0L in
    for i = 0 to Array.length xs / 2 - 1 do
      res := Int64.add !res
	(Int64.mul (Int64.of_int xs.(2 * i)) (Int64.of_int xs.(2 * i + 1)));
    done;
    Printf.printf "%Ld\n" !res;

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n 0 in
  let _ =
    for i = 0 to n - 1 do
      a.(i) <- Scanf.bscanf sb "%d " (fun s -> s)
    done;
    Array.sort compare a;
  in
  let xs = ref [] in
  let pos = ref (n - 1) in
  let _ =
    while !pos > 0 do
      if a.(!pos) = a.(!pos - 1) ||
	a.(!pos) = a.(!pos - 1) + 1
      then (
	xs := a.(!pos - 1) :: !xs;
	pos := !pos - 2
      ) else decr pos
    done;
  in
  let xs = Array.of_list !xs in
  let res = ref 0L in
    for i = 0 to Array.length xs / 2 - 1 do
      res := Int64.add !res
	(Int64.mul (Int64.of_int xs.(2 * i)) (Int64.of_int xs.(2 * i + 1)));
    done;
    Printf.printf "%Ld\n" !res;


Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome.Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn't contain any leading zeroes.Thus, "localization" will be spelt as "l10n", and "internationalization» will be spelt as "i18n".You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.

The first line contains an integer n (1 ≤ n ≤ 100). Each of the following n lines contains one word. All the words consist of lowercase Latin letters and possess the lengths of from 1 to 100 characters.

Print n lines. The i-th line should contain the result of replacing of the i-th word from the input data.

4
word
localization
internationalization
pneumonoultramicroscopicsilicovolcanoconiosis

let _ =
    let main () =
        let n = read_int () in
        for i = 1 to n do
            let s = read_line () in
            let l = String.length s in
            if l > 10 then (
                Printf.printf "%c%d%c\n" s.[0] (l - 2) s.[l - 1]
            ) else print_endline s
        done
    in
    main ()

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

let abbreviate w =
    let l = String.length w in
    let first_char = String.make 1 (String.get w 0) in
    let last_char = String.make 1 (String.get w (l - 1)) in
    if l > 10
    then first_char ^ Pervasives.string_of_int (l - 2) ^ last_char
    else w

let rec reading_words n =
    if n > 0
    then
        let word = Scanf.scanf "%s " (fun word -> word) in
        Printf.printf "%s\n" (abbreviate word);
        reading_words (n - 1)
    else
    ()

let () = reading_words number_of_words

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

let abbreviate w =
    let l = String.length w in
    let first_char = String.make 1 (String.get w 0) in
    let last_char = String.make 1 (String.get w (l - 1)) in
    if l > 10
    then first_char ^ string_of_int (l - 2) ^ last_char
    else w

let rec reading_words n =
    if n > 0
    then
        let word = Scanf.scanf "%s " (fun word -> word) in
        Printf.printf "%s\n" (abbreviate word);
        reading_words (n - 1)
    else
    ()

let () = reading_words number_of_words

let input () =
  Scanf.bscanf Scanf.Scanning.stdin " %s" (fun x -> x)

let eat () =
  let s = input ()
  in let len = String.length s
  in if len > 10 then
    Printf.printf "%c%d%c\n" (String.get s 0) (len - 2) (String.get s (len - 1))
  else
    Printf.printf "%s\n" s

let main () =
  let n = Scanf.bscanf Scanf.Scanning.stdin " %d" (fun x -> x)
  in let rec int x =
       if x = 0 then ()
       else (eat (); int (x - 1))
  in int n

;;
main ();;

(* Code Forces : Way Too Long Words *)

open Scanf
open Printf

let i = scanf " %d" (fun i -> i)

let l_w =
  let rec loop i out =
    if i = 0 then
      out
    else
      loop (i-1) ((scanf " %s" (fun w -> w))::out)
  in loop i []

let () =
  let l_w' =
    List.rev_map
      (fun w ->
        let len = String.length w in
        if len > 10 then
          let b = String.sub w 0 1 in
          let e = String.sub w (len - 1) 1 in
          let slen = string_of_int (len-2) in
          b ^ slen ^ e
        else
          w)
      l_w
  in
  List.iter (fun w -> printf "%s\n" w) l_w'

let () =
  let n=read_int () in
  for i=1 to n do
    let s=read_line () in
    let len=String.length s in
    if len > 10 then
      print_endline ((String.sub s 0 1) ^ (string_of_int(len - 2)) ^ (String.sub s (len - 1) 1))
    else
      print_endline s
  done

for i=1to read_int()do let s,c=read_line(),print_char in let l=String.length s in if l>10then(c s.[0];print_int(l-2);c s.[l-1])else print_string s; c '\n' done

let short s =
	let len = String.length s in
	if len > 10 then
		(String.sub s 0 1) ^ (string_of_int (len-2)) ^ (String.sub s (len-1) 1)
	else
		s
;;

let () =
	let n = read_int() in
	for i = 1 to n do print_string (short (read_line()) ^ "\n") done
;;

let readList n f =
  let rec loop n =
    if n == 0 then
      []
    else
      f() :: loop (n-1)
  in
  List.rev(loop n);;

let solve() =
  let str = read_line() in
  let len = String.length str in
  if (len) <= 10 then
    Printf.printf "%s\n" str
  else
    Printf.printf "%c%d%c\n" (str.[0]) (len-2) (str.[len-1]);;

let n = read_int() in
readList n solve

let process s len =
    String.sub s 0 1 ^ string_of_int (len-2) ^ String.sub s (len-1) 1

let rec solve n =
    match n with
    | 0 -> ()
    | n ->
        let s = read_line () in
        let f s =
            let len = String.length s in
            match s with
            | s when len <= 10 -> print_string (s ^ "\n")
            | s -> print_string ((process s len) ^ "\n")
        in let top n =
            f s;
            solve (n-1) in top n

let n = read_int() in
    solve n

let n=read_int();;
for i=1 to n do
   let s=read_line() in
   if String.length(s)<=10 then print_endline(s)
   else print_endline((String.sub s 0 1)^string_of_int(String.length(s)-2)^(String.sub s (String.length(s)-1) 1))
done;;

let () =
  let n = read_int () in
  for i = 1 to n do
    let str = read_line () in
    let len = String.length str in
    if len>10 then
      Printf.printf "%c%d%c\n" (String.get str 0) (len-2) (String.get str (len-1))
    else
      Printf.printf "%s\n" str
  done;;

let next_int ()=
	Scanf.scanf " %d" (function i -> i);;

let next_string ()=
	Scanf.scanf " %s" (function i -> i);;

let string_of_char c =
	String.make 1 c
;;

let n = next_int();;

let rec read n =
	if n = 0 then []
	else next_string() :: (read (n - 1))
;;

let data = read n;;

let change s =
	let len = String.length s in
	if len <= 10 then s
	else string_of_char (String.get s 0) ^ (string_of_int (len - 2)) ^ string_of_char (String.get s (len - 1))
;;

let ans = List.map change data;;

List.map (function x -> print_endline x) (List.rev ans);;

let n=read_int();;
for i=1 to n do
    let str=read_line() in
    let va=String.length(str) in
    if va<=10 then print_endline(str) else print_endline((String.make 1 str.[0])^(string_of_int (va-2))^(String.make 1 str.[va-1]));
done;;

let abbr s =
  let len = String.length s in
  let head = String.sub s 0 1 in
  let last = String.sub s (len - 1) 1
  in
  if 10 < len then
    head ^ (string_of_int (len - 2)) ^ last
  else
    s
;;

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

(* solve :: bytes list -> bytes *)
let solve xs =
  String.concat "\n" (List.map abbr xs)
;;

let () =
  Printf.printf "%s\n"
                (solve (make_list (read_int ())))
;;

let rec read_lines n =
  match n with
  | 0 -> []
  | _ -> (read_line ()) :: read_lines (n - 1)

let () =
  let num = read_int() in
  read_lines num
  |> List.rev
  |> List.iter (fun str ->
      let len = String.length str in
      if len <= 10
      then
        Printf.printf "%s\n" str
      else
        Printf.printf "%c%d%c\n" str.[0] (len - 2) str.[len - 1]
    )
;;

open Scanf
open Num
open Printf
open String

let n = Scanf.bscanf Scanf.Scanning.stdin "%d " ( fun x -> x)

for i = 1 to n do
  let s = Scanf.bscanf Scanf.Scanning.stdin "%s " ( fun x -> x) in
  let l = length s in
  if ( l <= 10) then
    print_endline s
  else
      let s1 = String.make 1 (String.get s 0) in
      let se = String.make 1 (String.get s ( l - 1)) in
      let num = string_of_int ( l - 2 ) in
      print_endline (s1 ^ num ^ se)
done

let abbrev word =
    let len = (String.length word) in
    if len < 11 then word
    else (String.make 1 (String.get word 0))
    ^ (string_of_int (len-2))
    ^ (String.make 1 (String.get word (len-1)))

let main () =
    let gr () = Scanf.scanf " %d\n" (fun i -> i) in
    let n = gr () in
    for i = 1 to n do
        let grr () = Scanf.scanf "%s\n" (fun i -> i) in
        let word = grr () in
        Printf.printf "%s\n" (abbrev word)
    done
;;

let _ = main();;

let len word = String.length word;;
let first word = String.make 1 (String.get word 0);;
let last word = String.make 1 (String.get word ((len word) - 1));;
let abbr word = (first word) ^ (string_of_int ((len word) - 2)) ^ (last word);;
let solve word = if len word > 10 then abbr word else word;;

let rec read_and_solve n = if n > 0 then (
  Scanf.scanf "%s\n" (fun s -> print_string ((solve s) ^ "\n"));
  read_and_solve (n - 1);
) else ();;

let () = Scanf.scanf "%d\n" read_and_solve;;

open Printf
open Scanf

let id x = x

let abbreviate s =
  let n = String.length s in
  if n > 10 then sprintf "%c%d%c" s.[0] (n - 2) s.[n-1] else s

let () =
  let n = scanf " %d" id in
  for i = 1 to n do begin
    let s = scanf " %s" id in
    let t = abbreviate s in
    printf "%s\n" t
  end done

let trans = function
  | s when (String.length s) <= 10 -> s
  | s -> let head = String.get s 0
    and len = String.length s in
    let tail = String.get s (len - 1)
    in
    Printf.sprintf "%c%d%c" head (len - 2) tail;;

let case_num = read_int ();;
for i = 1 to case_num do
  let s = read_line ()
  in Printf.printf "%s\n" (trans s)
done ;;

open Printf

let compress word =
    let l = String.length word in
    if l > 10 then
        sprintf "%c%d%c" (word.[0]) (l - 2) (word.[l - 1])
    else
        word

let main =
    let n = read_int() in
    for i = 1 to n do
        let word = read_line() in
        printf "%s\n" (compress word)
    done

let read_string() = Scanf.bscanf Scanf.Scanning.stdib " %s " (fun x -> x)

let () =
  let n = read_int() in
    for i=1 to n do
      let s = read_string() in
      let l = String.length s in
      if l > 10 then Printf.printf "%c%d%c\n" s.[0] (l - 2) s.[l - 1]
      else  print_endline s
    done

let _ =
    let main () =
        let n = read_int () in
        for i = 1 to n do
            let s = read_line () in
            let l = String.length s in
            if l >= 10 then (
                Printf.printf "%c%d%c\n" s.[0] (l - 2) s.[l - 1]
            ) else print_endline s
        done
    in
    main ()

let input () =
  Scanf.bscanf Scanf.Scanning.stdin "%s" (fun x -> x)

let main () =
  let s = input ()
  in let len = String.length s
  in if len > 10 then
    Printf.printf "%c%d%c" (String.get s 0) (len - 2) (String.get s (len - 1))
  else
    Printf.printf "%s" s
;;

main ();;

let n=read_int();;
for i=1 to n do
   let s=read_line() in
   if String.length(s)<10 then print_endline(s)
   else print_endline((String.sub s 0 1)^string_of_int(String.length(s)-2)^(String.sub s (String.length(s)-1) 1))
done;;

let rec read_lines n =
  match n with
  | 0 -> []
  | _ -> (read_line ()) :: read_lines (n - 1)

let () =
  let num = read_int() in
  read_lines num
  |> List.rev
  |> List.iter (fun str ->
      let len = String.length str in
      if len <= num
      then
        Printf.printf "%s\n" str
      else
        Printf.printf "%c%d%c\n" str.[0] (len - 2) str.[len - 1]
    )
;;

let rec read_lines n =
  match n with
  | 0 -> []
  | _ -> (read_line ()) :: read_lines (n - 1)

let () =
  let num = read_int() in
  read_lines num
  |> List.iter (fun str ->
      let len = String.length str in
      if len <= num
      then
        Printf.printf "%s\n" str
      else
        Printf.printf "%c%d%c\n" str.[0] (len - 2) str.[len - 1]
    )
;;

let abbrev word =
    let len = (String.length word) in
    if len < 10 then word
    else (String.make 1 (String.get word 0))
    ^ (string_of_int (len-2))
    ^ (String.make 1 (String.get word (len-1)))

let main () =
    let gr () = Scanf.scanf " %d\n" (fun i -> i) in
    let n = gr () in
    for i = 1 to n do
        let grr () = Scanf.scanf "%s\n" (fun i -> i) in
        let word = grr () in
        Printf.printf "%s\n" (abbrev word)
    done
;;

let _ = main();;


Polycarp loves geometric progressions very much. Since he was only three years old, he loves only the progressions of length three. He also has a favorite integer k and a sequence a, consisting of n integers.He wants to know how many subsequences of length three can be selected from a, so that they form a geometric progression with common ratio k.A subsequence of length three is a combination of three such indexes i1, i2, i3, that 1 ≤ i1 < i2 < i3 ≤ n. That is, a subsequence of length three are such groups of three elements that are not necessarily consecutive in the sequence, but their indexes are strictly increasing.A geometric progression with common ratio k is a sequence of numbers of the form b·k0, b·k1, ..., b·kr - 1.Polycarp is only three years old, so he can not calculate this number himself. Help him to do it.

The first line of the input contains two integers, n and k (1 ≤ n, k ≤ 2·105), showing how many numbers Polycarp's sequence has and his favorite number. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — elements of the sequence.

Output a single number — the number of ways to choose a subsequence of length three, such that it forms a geometric progression with a common ratio k.

NoteIn the first sample test the answer is four, as any of the two 1s can be chosen as the first element, the second element can be any of the 2s, and the third element of the subsequence must be equal to 4.

open Printf
open Scanf

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

let divides a b = (* does b divide a? *)
  let c = a//b in
  let d = c ** b in
  d = a

let long x = Int64.of_int x
let short x = Int64.to_int x

let read_long () = bscanf Scanning.stdib " %Ld " (fun x -> x)
let () =
  let n = short (read_long ()) in
  let k = read_long () in
  let a = Array.init n (fun _ -> read_long()) in

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

  let add h key d =
    Hashtbl.replace h key ((get h key) ++ d)
  in

  let h1 = Hashtbl.create 10 in
  let h2 = Hashtbl.create 10 in
  let h3 = Hashtbl.create 10 in

  for i=0 to n-1 do
    let x = a.(i) in
    if divides x k then (
      (* update h3 first *)
      let y = x//k in
      let c = get h2 y in
      add h3 x c;

      (* now update h2 *)
      let c = get h1 y in
      add h2 x c;
    );

    (* now update h1 *)
    add h1 x 1L
  done;

  let total = Hashtbl.fold (
    fun _ v tot -> tot ++ v
  ) h3 0L
  in

  printf "%Ld\n" total;


The Little Elephant very much loves sums on intervals.This time he has a pair of integers l and r (l ≤ r). The Little Elephant has to find the number of such integers x (l ≤ x ≤ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.Help him and count the number of described numbers x for a given pair l and r.

The single line contains a pair of integers l and r (1 ≤ l ≤ r ≤ 1018) — the boundaries of the interval. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.

On a single line print a single integer — the answer to the problem.

NoteIn the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.

#load "str.cma";;

let array_of_int n =
  let rec iter ls cur =
	if Int64.compare cur 0L = 0 then ls
	else
	  let r = Int64.to_int (Int64.rem cur 10L) and
		  l = Int64.div cur 10L in
	  iter (r :: ls) l in
  Array.of_list (iter [] n);;

let int_of_array arr l r =
  let rec iter i res =
	if i > r then res
	else iter (i + 1) (Int64.add (Int64.mul res 10L) (Int64.of_int arr.(i))) in
  iter l 0L;;

let pow_int n e =
  let rec iter i cur =
	if i >= e then cur
	else iter (i + 1) (Int64.mul cur (Int64.of_int n))
  in iter 0 1L;;

let rec num_x n =
  if Int64.compare n 10L < 0 then n
  else if Int64.compare n 100L < 0 then
	let l = Int64.div n 10L and
		r = Int64.rem n 10L in
	Int64.add (if Int64.compare r l >= 0 then l else Int64.pred l) 9L
  else
	let na = array_of_int n in
	let	l = na.(Array.length na - 1) and
		r = na.(0) and
		mid = int_of_array na 1 (Array.length na - 2) in
	let m = num_x (Int64.sub (pow_int 10 (Array.length na - 1)) 1L) and
		mm = Int64.mul (pow_int 10 (Array.length na - 2)) (Int64.of_int (r - 1)) and
		mmm = Int64.add mid (if r > l then 0L else 1L) in
	Int64.add (Int64.add m mm) mmm;;

let get_input () =
  let ls = List.map (fun x -> (Int64.of_string x))
	(Str.split (Str.regexp "[ \t]+") (read_line ())) in
(List.hd ls, List.hd (List.tl ls));;

let (x, y) = get_input () in
begin
  Printf.printf "%Ld\n" (Int64.sub (num_x y) ( (num_x (Int64.sub x 1L)) ));
end

#load "str.cma";;

let array_of_int n =
  let rec iter ls cur =
	if Int64.compare cur 0L = 0 then ls
	else
	  let r = Int64.to_int (Int64.rem cur 10L) and
		  l = Int64.div cur 10L in
	  iter (r :: ls) l in
  Array.of_list (iter [] n);;

let int_of_array arr l r =
  let rec iter i res =
	if i > r then res
	else iter (i + 1) (Int64.add (Int64.mul res 10L) (Int64.of_int arr.(i))) in
  iter l 0L;;

let pow_int n e =
  let rec iter i cur =
	if i >= e then cur
	else iter (i + 1) (Int64.mul cur (Int64.of_int n))
  in iter 0 1L;;

let rec num_x n =
  if Int64.compare n 10L < 0 then n
  else if Int64.compare n 100L < 0 then
	let l = Int64.div n 10L and
		r = Int64.rem n 10L in
	Int64.add (if Int64.compare r l >= 0 then l else Int64.pred l) 9L
  else
	let na = array_of_int n in
	let	l = na.(Array.length na - 1) and
		r = na.(0) and
		mid = int_of_array na 1 (Array.length na - 2) in
	let m = num_x (Int64.sub (pow_int 10 (Array.length na - 1)) 1L) and
		mm = Int64.mul (pow_int 10 (Array.length na - 2)) (Int64.of_int (r - 1)) and
		mmm = Int64.add mid (if r >= l then 0L else 1L) in
	Int64.add (Int64.add m mm) mmm;;

let get_input () =
  let ls = List.map (fun x -> (Int64.of_string x))
	(Str.split (Str.regexp "[ \t]+") (read_line ())) in
(List.hd ls, List.hd (List.tl ls));;

let (x, y) = get_input () in
Printf.printf "%Ld\n" (Int64.sub (num_x y) (Int64.sub (num_x x) 1L));;


The ancient Berlanders believed that the longer the name, the more important its bearer is. Thus, Berland kings were famous for their long names. But long names are somewhat inconvenient, so the Berlanders started to abbreviate the names of their kings. They called every king by the first letters of its name. Thus, the king, whose name was Victorious Vasily Pupkin, was always called by the berlanders VVP.In Berland over its long history many dynasties of kings replaced each other, but they were all united by common traditions. Thus, according to one Berland traditions, to maintain stability in the country, the first name of the heir should be the same as the last name his predecessor (hence, the first letter of the abbreviated name of the heir coincides with the last letter of the abbreviated name of the predecessor). Berlanders appreciate stability, so this tradition has never been broken. Also Berlanders like perfection, so another tradition requires that the first name of the first king in the dynasty coincides with the last name of the last king in this dynasty (hence, the first letter of the abbreviated name of the first king coincides with the last letter of the abbreviated name of the last king). This tradition, of course, has also been always observed.The name of a dynasty is formed by very simple rules: we take all the short names of the kings in the order in which they ruled, and write them in one line. Thus, a dynasty of kings "ab" and "ba" is called "abba", and the dynasty, which had only the king "abca", is called "abca".Vasya, a historian, has recently found a list of abbreviated names of all Berland kings and their relatives. Help Vasya to find the maximally long name of the dynasty that could have existed in Berland.Note that in his list all the names are ordered by the time, that is, if name A is earlier in the list than B, then if A and B were kings, then king A ruled before king B.

The first line contains integer n (1 ≤ n ≤ 5·105) — the number of names in Vasya's list. Next n lines contain n abbreviated names, one per line. An abbreviated name is a non-empty sequence of lowercase Latin letters. Its length does not exceed 10 characters.

Print a single number — length of the sought dynasty's name in letters. If Vasya's list is wrong and no dynasty can be found there, print a single number 0.

NoteIn the first sample two dynasties can exist: the one called "abcca" (with the first and second kings) and the one called "abccba" (with the first and third kings). In the second sample there aren't acceptable dynasties.The only dynasty in the third sample consists of one king, his name is "c".

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n 0 in
  let b = Array.make n 0 in
  let c = Array.make n 0 in
  let () =
    for i = 0 to n - 1 do
      let s = Scanf.bscanf sb "%s " (fun s -> s) in
	a.(i) <- Char.code s.[0] - Char.code 'a';
	b.(i) <- Char.code s.[String.length s - 1] - Char.code 'a';
	c.(i) <- String.length s;
    done;
  in
  let d = Array.make_matrix 26 26 0 in
  let max (x : int) y = if x > y then x else y in
    for i = 0 to n - 1 do
      let aa = a.(i)
      and bb = b.(i) in
	for j = 0 to 25 do
	  if d.(j).(aa) > 0
	  then d.(j).(bb) <- max d.(j).(bb) (d.(j).(aa) + c.(i));
	done;
	d.(aa).(bb) <- max d.(aa).(bb) c.(i);
    done;
    let res = ref 0 in
      for i = 0 to 25 do
	res := max !res d.(i).(i)
      done;
      Printf.printf "%d\n" !res


Programmer Sasha is a student at MIPT (Moscow Institute of Physics and Technology) and he needs to make a laboratory work to pass his finals.A laboratory unit is a plane with standard coordinate axes marked on it. Physicists from Moscow Institute of Physics and Technology charged the axes by large electric charges: axis X is positive and axis Y is negative.Experienced laboratory worker marked n points with integer coordinates (xi, yi) on the plane and stopped the time. Sasha should use "atomic tweezers" to place elementary particles in these points. He has an unlimited number of electrons (negatively charged elementary particles) and protons (positively charged elementary particles). He can put either an electron or a proton at each marked point. As soon as all marked points are filled with particles, laboratory worker will turn on the time again and the particles will come in motion and after some time they will stabilize in equilibrium. The objective of the laboratory work is to arrange the particles in such a way, that the diameter of the resulting state (the maximum distance between the pairs of points of the set) is as small as possible.Since Sasha is a programmer, he naively thinks that all the particles will simply "fall" into their projections on the corresponding axes: electrons will fall on axis X, while protons will fall on axis Y. As we are programmers too, we will consider the same model as Sasha. That is, a particle gets from point (x, y) to point (x, 0) if it is an electron and to point (0, y) if it is a proton.As the laboratory has high background radiation and Sasha takes care of his laptop, he did not take it with him, and now he can't write a program that computes the minimum possible diameter of the resulting set. Therefore, you will have to do it for him.Print a square of the minimum possible diameter of the set.

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of points marked on the plane. Each of the next n lines contains two integers xi and yi ( - 108 ≤ xi, yi ≤ 108) — the coordinates of the i-th point. It is guaranteed that no two points coincide.

Print a single integer — the square of the minimum possible diameter of the set.

NoteIn the first sample Sasha puts electrons at all points, all particles eventually fall at a single point (1, 0).In the second sample Sasha puts an electron at point (1, 10), and a proton at point (10, 1). The result is a set of two points (1, 0) and (0, 1), which has a diameter of .

open Printf
open Scanf

let ( ** ) a b = Int64.mul a b
let ( ++ ) a b = Int64.add a b
let ( -- ) a b = Int64.sub a b
let ( // ) a b = Int64.div a b
let long x = Int64.of_int x
let abs = Int64.abs
let sq x = x**x

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 minf i j f = fold (i+1) j (fun k a -> min (f k) a) (f i)
let minmax (a,b) x = (min a x, max b x)
let minmax1 (a,b) (c,d) = (min a c, max b d)

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_pair () = bscanf Scanning.stdib " %d %d " (fun x y -> (x,y))
let () =
  let n = read_int () in
  let pointx = Array.make (n+1) 0 in
  let pointy = Array.make (n+1) 0 in

  for i=0 to n-1 do
    let (x,y) = read_pair() in
    pointy.(i) <- x;
    pointx.(i) <- y;
  done;

  let maxx = long (maxf 0 (n-1) (fun i -> pointx.(i))) in
  let minx = long (minf 0 (n-1) (fun i -> pointx.(i))) in
  let maxy = long (maxf 0 (n-1) (fun i -> pointy.(i))) in
  let miny = long (minf 0 (n-1) (fun i -> pointy.(i))) in

  let opt1 = sq (maxx -- minx) in
  let opt2 = sq (maxy -- miny) in

  let n = n+1 in  (* add the zero point in for convenience *)

  let perm = Array.init n (fun i -> i) in
  Array.fast_sort (fun i j -> compare pointx.(i) pointx.(j)) perm;

  let search pointx pointy =

    let prefix = Array.make n (0L,0L) in
    let suffix = Array.make n (0L,0L) in

    prefix.(0) <- (pointy.(0), pointy.(0));
    for i=1 to n-1 do
      prefix.(i) <- minmax prefix.(i-1) pointy.(i)
    done;

    suffix.(n-1) <- (pointy.(n-1), pointy.(n-1));
    for i=n-2 downto 0 do
      suffix.(i) <- minmax suffix.(i+1) pointy.(i)
    done;

    let get ar j = if j<0 || j>=n then (0L,0L) else ar.(j) in

    (* now prefix.(i) = (ylo, yhi), where ylo is the min
       value among pointy.(0)...pointy.(i), yhi is analogous.
       also suffix.(i) = same thing but for range i...n-1
    *)

    let rec findzero i =
      if (pointx.(i), pointy.(i)) = (0L,0L) then i else findzero (i+1)
    in
    let zero_index = findzero 0 in

    let rec values i j =
      (* for this (i,j) returns (horiz_score, max(diag_score, vert_score))
	 We assume pointx.(i) <= 0 and pointx.(j) >= 0 and
         abs pointx.(i) >= abs pointx.(j)
      *)
      let (miny, maxy) = minmax1 (get prefix (i-1)) (get suffix (j+1)) in
      let vert_score = sq (maxy -- miny) in
      let diag_score = max (sq miny) (sq maxy) ++ (sq pointx.(i)) in
      let horiz_score = sq (pointx.(j) -- pointx.(i)) in
      (horiz_score, max vert_score diag_score)
    in

    let test i j = let (h,v) = values i j in h >= v in

    let starthi = ref (n-1) in

    minf 0 zero_index (fun i ->
      let rec fixst () =
	if abs pointx.(!starthi) > abs pointx.(i) then (
	  starthi := !starthi - 1;
	  fixst ()
	)
      in
      fixst();

      let rec bsearch lo hi =
	if lo+1 = hi then lo else
	  let m = (lo + hi) / 2 in
	  if test i m then bsearch lo m else bsearch m hi
      in

      if !starthi = zero_index || test i zero_index then
	let (h,v) = values i zero_index in max h v
      else
	let index = bsearch zero_index !starthi in
	let v1 = let (h,v) = values i index in max h v in
	let v2 = let (h,v) = values i (index+1) in max h v in
	min v1 v2
    )
  in

  let pointx = Array.init n (fun i -> long pointx.(perm.(i))) in
  let pointy = Array.init n (fun i -> long pointy.(perm.(i))) in
  let opt3 = search pointx pointy in

  let rev ar n =
    for i=0 to n/2 - 1 do
      let (p,q) = (ar.(i), ar.(n-1-i)) in
      ar.(i) <- q;
      ar.(n-1-i) <- p
    done
  in
  rev pointx n;
  rev pointy n;

  let opt4 = search pointx pointy in

  printf "%Ld\n" (min (min opt1 opt2) (min opt3 opt4))

open Printf
open Scanf

let ( ** ) a b = Int64.mul a b
let ( ++ ) a b = Int64.add a b
let ( -- ) a b = Int64.sub a b
let ( // ) a b = Int64.div a b
let long x = Int64.of_int x

let sq x = x**x

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 minf i j f = fold (i+1) j (fun k a -> min (f k) a) (f i)
let minmax (a,b) x = (min a x, max b x)
let minmax1 (a,b) (c,d) = (min a c, max b d)

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_pair () = bscanf Scanning.stdib " %d %d " (fun x y -> (x,y))
let () =
  let n = read_int () in
  let pointx = Array.make n 0 in
  let pointy = Array.make n 0 in

  for i=0 to n-1 do
    let (x,y) = read_pair() in
    pointx.(i) <- x;
    pointy.(i) <- y;
  done;

  let perm = Array.init n (fun i -> i) in
  Array.fast_sort (fun i j -> compare pointx.(i) pointx.(j)) perm;

  let pointx = Array.init n (fun i -> long pointx.(perm.(i))) in
  let pointy = Array.init n (fun i -> long pointy.(perm.(i))) in

  let prefix = Array.make n (0L,0L) in
  let suffix = Array.make n (0L,0L) in

  prefix.(0) <- (pointy.(0), pointy.(0));
  for i=1 to n-1 do
    prefix.(i) <- minmax prefix.(i-1) pointy.(i)
  done;

  suffix.(n-1) <- (pointy.(n-1), pointy.(n-1));
  for i=n-2 downto 0 do
    suffix.(i) <- minmax suffix.(i+1) pointy.(i)
  done;

  let dist_works d =
    let rec scan i j =
      if i=0 && j=n-1 then true else
      (* (i,j) is a new range. Check it *)
	let (min3, max3) =
	  match (i=0, j=n-1) with
	    | (true,true) -> failwith "no way"
	    | (true,false) -> suffix.(j+1)
	    | (false,true) -> prefix.(i-1)
	    | (false,false) -> minmax1 suffix.(j+1) prefix.(i-1)
	in
	let dist2_from_axis a b = max (sq a) (sq b) in
	let dx = dist2_from_axis pointx.(i) pointx.(j) in
	let dy = dist2_from_axis min3 max3 in
	if sq (max3 -- min3) <= d && dx ++ dy <= d then true
	else if j = n-1 && i = n-1 then false else
	  let j' = if j=n-1 || sq (pointx.(j+1)--pointx.(i)) > d then j else j+1 in
	  let i' = if j=j' then i+1 else i in
	  scan i' j'
    in
    let rec findj j = if j=n-1 then j else if pointx.(j+1) >= 0L then j else findj (j+1) in
    let j = findj 0 in
    let rec findi i = if sq (pointx.(j)--pointx.(i)) <= d then i else findi (i+1) in
    let i = findi 0 in
    scan i j
  in

  let rec bsearch lo hi =  (* lo fails, hi works *)
    if lo++1L >= hi then hi else
      let mid = (lo ++ hi) // 2L in
      if dist_works mid then bsearch lo mid else bsearch mid hi
  in

  (* let rec findlo lo = if not (dist_works lo) then lo else findlo (lo//4L) in
  let lo = findlo 40000000000000000L in *)

  let maxd = sq (pointx.(0) -- pointx.(n-1)) in
  let maxy = maxf 0 (n-1) (fun i -> pointy.(i)) in
  let miny = minf 0 (n-1) (fun i -> pointy.(i)) in
  let opt1 = min maxd (sq (maxy -- miny)) in
  let opt2 = if dist_works 0L then 0L else bsearch 0L opt1 in

  printf "%Ld\n" opt2

open Printf
open Scanf

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

let long x = Int64.of_int x

let 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 minf i j f = fold (i+1) j (fun k a -> min (f k) a) (f i)

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

  let point = Array.init n (fun _ -> read_pair()) in

  Array.sort (fun (x1,y1) (x2,y2) -> compare (Int64.abs y2) (Int64.abs y1) ) point;
  (* sorted in decreasing order by absolute value *)

  let score a b = a**a ++ b**b in

  let maxx = maxf 0 (n-1) (fun i -> fst point.(i)) in
  let minx = minf 0 (n-1) (fun i -> fst point.(i)) in
  let maxy = maxf 0 (n-1) (fun i -> snd point.(i)) in
  let miny = minf 0 (n-1) (fun i -> snd point.(i)) in

  let rec loop i width ac = if i=n then ac else
  (* delete point i-1 from y *)
      let width = max width (Int64.abs (fst point.(i-1))) in
      let height = Int64.abs (snd point.(i)) in
      loop (i+1) width (min (score width height) ac)
  in

  let opt1 = score (maxx -- minx) 0L in
  let opt2 = score (maxy -- miny) 0L in

  let answer = loop 1 0L (min opt1 opt2) in
  (* this loop only considers projecting to two axes *)

  printf "%Ld\n" answer

open Printf
open Scanf

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

let long x = Int64.of_int x

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

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

  (*
  let xx = Array.make n (0L,0) in
  let yy = Array.make n (0L,0) in
  *)

  let point = Array.init n (fun i -> (read_pair(), i)) in

  let pointy = Array.copy point in

  Array.sort (fun ((x1,y1),i) ((x2,y2),j) -> compare (Int64.abs y2) (Int64.abs y1) ) pointy;
  (* sorted in decreasing order by absolute value *)

  let rec loop i width ac = if i=n then ac else
  (* delete point i from y *)
      let ((x,y),_) = pointy.(i) in
      let width = max width (Int64.abs x) in
      let height = Int64.abs y in
      let score = width ** width ++ height ** height in
      loop (i+1) width (min score ac)
  in

  let answer = loop 0 0L 0L in

  printf "%Ld\n" answer

open Printf
open Scanf

let ( ** ) a b = Int64.mul a b
let ( ++ ) a b = Int64.add a b
let ( -- ) a b = Int64.sub a b
let ( // ) a b = Int64.div a b
let long x = Int64.of_int x

let sq x = x**x

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 minf i j f = fold (i+1) j (fun k a -> min (f k) a) (f i)
let minmax (a,b) x = (min a x, max b x)
let minmax1 (a,b) (c,d) = (min a c, max b d)

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_pair () = bscanf Scanning.stdib " %d %d " (fun x y -> (x,y))
let () =
  let n = read_int () in
  let pointx = Array.make n 0 in
  let pointy = Array.make n 0 in

  for i=0 to n-1 do
    let (x,y) = read_pair() in
    pointx.(i) <- x;
    pointy.(i) <- y;
  done;

  let perm = Array.init n (fun i -> i) in
  Array.fast_sort (fun i j -> compare pointx.(i) pointx.(j)) perm;

  let pointx = Array.init n (fun i -> long pointx.(perm.(i))) in
  let pointy = Array.init n (fun i -> long pointy.(perm.(i))) in

  let prefix = Array.make n (0L,0L) in
  let suffix = Array.make n (0L,0L) in

  prefix.(0) <- (pointy.(0), pointy.(0));
  for i=1 to n-1 do
    prefix.(i) <- minmax prefix.(i-1) pointy.(i)
  done;

  suffix.(n-1) <- (pointy.(n-1), pointy.(n-1));
  for i=n-2 downto 0 do
    suffix.(i) <- minmax suffix.(i+1) pointy.(i)
  done;

  let dist_works d =
    let rec scan i j =
      if i=0 && j=n-1 then true else
      (* (i,j) is a new range. Check it *)
	let (min3, max3) =
	  match (i=0, j=n-1) with
	    | (true,true) -> failwith "no way"
	    | (true,false) -> suffix.(j+1)
	    | (false,true) -> prefix.(i-1)
	    | (false,false) -> minmax1 suffix.(j+1) prefix.(i-1)
	in
	let dist2_from_axis a b = max (sq a) (sq b) in
	let dx = dist2_from_axis pointx.(i) pointx.(j) in
	let dy = dist2_from_axis min3 max3 in
	if sq (max3 -- min3) <= d && dx ++ dy <= d then true
	else if j = n-1 && i = n-1 then false else
	  let j' = if j=n-1 || sq (pointx.(j+1)--pointx.(i)) > d then j else j+1 in
	  let i' = if j=j' then i+1 else i in
	  scan i' j'
    in
    let rec findj j = if j=n-1 then j else if pointx.(j) >= 0L then j else findj (j+1) in
    let j = findj 0 in
    let rec findi i = if sq (pointx.(j)--pointx.(i)) <= d then i else findi (i+1) in
    let i = findi 0 in
    scan i j
  in

  let rec bsearch lo hi =  (* lo fails, hi works *)
    if lo++1L >= hi then hi else
      let mid = (lo ++ hi) // 2L in
      if dist_works mid then bsearch lo mid else bsearch mid hi
  in

  (* let rec findlo lo = if not (dist_works lo) then lo else findlo (lo//4L) in
  let lo = findlo 40000000000000000L in *)

  let maxd = sq (pointx.(0) -- pointx.(n-1)) in
  let maxy = maxf 0 (n-1) (fun i -> pointy.(i)) in
  let miny = minf 0 (n-1) (fun i -> pointy.(i)) in
  let opt1 = min maxd (sq (maxy -- miny)) in
  let opt2 = if dist_works 0L then 0L else bsearch 0L opt1 in

  printf "%Ld\n" opt2

open Printf
open Scanf

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

let sq x = x**x

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 minf i j f = fold (i+1) j (fun k a -> min (f k) a) (f i)
let minmax (a,b) x = (min a x, max b x)
let minmax1 (a,b) (c,d) = (min a c, max b d)

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

  let point = Array.init n (fun _ -> read_pair()) in

  Array.sort compare point;
  (* sorted by increasing x value *)

  let pointx = Array.init n (fun i -> fst point.(i)) in
  let pointy = Array.init n (fun i -> snd point.(i)) in

  let prefix = Array.make n (0L,0L) in
  let suffix = Array.make n (0L,0L) in

  prefix.(0) <- (snd point.(0), pointy.(0));
  for i=1 to n-1 do
    prefix.(i) <- minmax prefix.(i-1) pointy.(i)
  done;

  suffix.(n-1) <- (pointy.(n-1), pointy.(n-1));
  for i=n-2 downto 0 do
    suffix.(i) <- minmax suffix.(i+1) pointy.(i)
  done;

  let dist_works d =
    let rec scan (i,j) =
      if i=0 && j=n-1 then true else
      (* (i,j) is a new range. Check it *)
	let (min3, max3) =
	  match (i=0, j=n-1) with
	    | (true,true) -> failwith "no way"
	    | (true,false) -> suffix.(j+1)
	    | (false,true) -> prefix.(i-1)
	    | (false,false) -> minmax1 suffix.(j+1) prefix.(i-1)
	in
	let dist2_from_axis a b = max (sq a) (sq b) in
	let dx = dist2_from_axis min3 max3 in
	let dy = dist2_from_axis pointx.(i) pointx.(j) in
	if sq (max3 -- min3) <= d && dx ++ dy <= d then true
	else if j = n-1 then false else
	  let j = j+1 in
	  let rec advancei i = if sq (pointx.(j) -- pointx.(i)) <= d then i
	    else advancei (i+1)
	  in
	  scan (advancei i, j)
    in
    scan (0,0)
  in

  let rec bsearch lo hi =  (* lo fails, hi works *)
    if lo++1L = hi then hi else
      let mid = (lo ++ hi) // 2L in
      if dist_works mid then bsearch lo mid else bsearch mid hi
  in
  let maxd = sq (pointx.(0) -- pointx.(n-1)) in
  let opt1 = if dist_works 0L then 0L else bsearch 0L maxd in

  let maxy = maxf 0 (n-1) (fun i -> pointy.(i)) in
  let miny = minf 0 (n-1) (fun i -> pointy.(i)) in
  let opt2 = sq (maxy -- miny) in

  printf "%Ld\n" (min opt1 opt2)

open Printf
open Scanf

let ( ** ) a b = Int64.mul a b
let ( ++ ) a b = Int64.add a b
let ( -- ) a b = Int64.sub a b
let ( // ) a b = Int64.div a b
let long x = Int64.of_int x

let sq x = x**x

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 minf i j f = fold (i+1) j (fun k a -> min (f k) a) (f i)
let minmax (a,b) x = (min a x, max b x)
let minmax1 (a,b) (c,d) = (min a c, max b d)

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_pair () = bscanf Scanning.stdib " %d %d " (fun x y -> (x,y))
let () =
  let n = read_int () in
  let pointx = Array.make n 0 in
  let pointy = Array.make n 0 in

  for i=0 to n-1 do
    let (x,y) = read_pair() in
    pointx.(i) <- x;
    pointy.(i) <- y;
  done;

  let perm = Array.init n (fun i -> i) in
  Array.fast_sort (fun i j -> compare pointx.(j) pointx.(i)) perm;

  let pointx = Array.init n (fun i -> long pointx.(perm.(i))) in
  let pointy = Array.init n (fun i -> long pointy.(perm.(i))) in

  let prefix = Array.make n (0L,0L) in
  let suffix = Array.make n (0L,0L) in

  prefix.(0) <- (pointy.(0), pointy.(0));
  for i=1 to n-1 do
    prefix.(i) <- minmax prefix.(i-1) pointy.(i)
  done;

  suffix.(n-1) <- (pointy.(n-1), pointy.(n-1));
  for i=n-2 downto 0 do
    suffix.(i) <- minmax suffix.(i+1) pointy.(i)
  done;

  let dist_works d =
    let rec scan i j =
      if i=0 && j=n-1 then true else
      (* (i,j) is a new range. Check it *)
	let (min3, max3) =
	  match (i=0, j=n-1) with
	    | (true,true) -> failwith "no way"
	    | (true,false) -> suffix.(j+1)
	    | (false,true) -> prefix.(i-1)
	    | (false,false) -> minmax1 suffix.(j+1) prefix.(i-1)
	in
	let dist2_from_axis a b = max (sq a) (sq b) in
	let dx = dist2_from_axis pointx.(i) pointx.(j) in
	let dy = dist2_from_axis min3 max3 in
	if sq (max3 -- min3) <= d && dx ++ dy <= d then true
	else if j = n-1 && i = n-1 then false else
	  let j' = if j=n-1 || sq (pointx.(j+1)--pointx.(i)) > d then j else j+1 in
	  let i' = if j=j' then i+1 else i in
	  scan i' j'
    in
    let rec findj j = if j=n-1 then j else if pointx.(j+1) <= 0L then j else findj (j+1) in
    let j = findj 0 in
    let rec findi i = if sq (pointx.(j)--pointx.(i)) <= d then i else findi (i+1) in
    let i = findi 0 in
    scan i j
  in

  let rec bsearch lo hi =  (* lo fails, hi works *)
    if lo++1L >= hi then hi else
      let mid = (lo ++ hi) // 2L in
      if dist_works mid then bsearch lo mid else bsearch mid hi
  in

  (* let rec findlo lo = if not (dist_works lo) then lo else findlo (lo//4L) in
  let lo = findlo 40000000000000000L in *)

  let maxd = sq (pointx.(0) -- pointx.(n-1)) in
  let maxy = maxf 0 (n-1) (fun i -> pointy.(i)) in
  let miny = minf 0 (n-1) (fun i -> pointy.(i)) in
  let opt1 = min maxd (sq (maxy -- miny)) in
  let opt2 = if dist_works 0L then 0L else bsearch 0L opt1 in

  printf "%Ld\n" opt2

open Printf
open Scanf

let ( ** ) a b = Int64.mul a b
let ( ++ ) a b = Int64.add a b
let ( -- ) a b = Int64.sub a b
let ( // ) a b = Int64.div a b
let long x = Int64.of_int x

let sq x = x**x

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 minf i j f = fold (i+1) j (fun k a -> min (f k) a) (f i)
let minmax (a,b) x = (min a x, max b x)
let minmax1 (a,b) (c,d) = (min a c, max b d)

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_pair () = bscanf Scanning.stdib " %d %d " (fun x y -> (x,y))
let () =
  let n = read_int () in
  let pointx = Array.make n 0 in
  let pointy = Array.make n 0 in

  for i=0 to n-1 do
    let (x,y) = read_pair() in
    pointy.(i) <- x;
    pointx.(i) <- y;
  done;

  let perm = Array.init n (fun i -> i) in
  Array.fast_sort (fun i j -> compare pointx.(i) pointx.(j)) perm;

  let pointx = Array.init n (fun i -> long pointx.(perm.(i))) in
  let pointy = Array.init n (fun i -> long pointy.(perm.(i))) in

  let prefix = Array.make n (0L,0L) in
  let suffix = Array.make n (0L,0L) in

  prefix.(0) <- (pointy.(0), pointy.(0));
  for i=1 to n-1 do
    prefix.(i) <- minmax prefix.(i-1) pointy.(i)
  done;

  suffix.(n-1) <- (pointy.(n-1), pointy.(n-1));
  for i=n-2 downto 0 do
    suffix.(i) <- minmax suffix.(i+1) pointy.(i)
  done;

  let dist_works d =
    let rec scan i j =
      if i=0 && j=n-1 then true else
      (* (i,j) is a new range. Check it *)
	let (min3, max3) =
	  match (i=0, j=n-1) with
	    | (true,true) -> failwith "no way"
	    | (true,false) -> suffix.(j+1)
	    | (false,true) -> prefix.(i-1)
	    | (false,false) -> minmax1 suffix.(j+1) prefix.(i-1)
	in
	let dist2_from_axis a b = max (sq a) (sq b) in
	let dx = dist2_from_axis pointx.(i) pointx.(j) in
	let dy = dist2_from_axis min3 max3 in
	if sq (max3 -- min3) <= d && dx ++ dy <= d then true
	else if j = n-1 && i = n-1 then false else
	  let j' = if j=n-1 || sq (pointx.(j+1)--pointx.(i)) > d then j else j+1 in
	  let i' = if j=j' then i+1 else i in
	  scan i' j'
    in
    let rec findj j = if j=n-1 then j else if pointx.(j+1) >= 0L then j else findj (j+1) in
    let j = findj 0 in
    let rec findi i = if sq (pointx.(j)--pointx.(i)) <= d then i else findi (i+1) in
    let i = findi 0 in
    scan i j
  in

  let rec bsearch lo hi =  (* lo fails, hi works *)
    if lo++1L >= hi then hi else
      let mid = (lo ++ hi) // 2L in
      if dist_works mid then bsearch lo mid else bsearch mid hi
  in

  (* let rec findlo lo = if not (dist_works lo) then lo else findlo (lo//4L) in
  let lo = findlo 40000000000000000L in *)

  let maxd = sq (pointx.(0) -- pointx.(n-1)) in
  let maxy = maxf 0 (n-1) (fun i -> pointy.(i)) in
  let miny = minf 0 (n-1) (fun i -> pointy.(i)) in
  let opt1 = min maxd (sq (maxy -- miny)) in
  let opt2 = if dist_works 0L then 0L else bsearch 0L opt1 in

  printf "%Ld\n" opt2

open Printf
open Scanf

let ( ** ) a b = Int64.mul a b
let ( ++ ) a b = Int64.add a b
let ( -- ) a b = Int64.sub a b
let ( // ) a b = Int64.div a b
let long x = Int64.of_int x

let sq x = x**x

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 minf i j f = fold (i+1) j (fun k a -> min (f k) a) (f i)
let minmax (a,b) x = (min a x, max b x)
let minmax1 (a,b) (c,d) = (min a c, max b d)

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_pair () = bscanf Scanning.stdib " %d %d " (fun x y -> (x,y))
let () =
  let n = read_int () in
  let pointx = Array.make n 0 in
  let pointy = Array.make n 0 in

  for i=0 to n-1 do
    let (x,y) = read_pair() in
    pointx.(i) <- x;
    pointy.(i) <- y;
  done;

  let perm = Array.init n (fun i -> i) in
  Array.fast_sort (fun i j -> compare pointx.(j) pointx.(i)) perm;

  let pointx = Array.init n (fun i -> long pointx.(perm.(i))) in
  let pointy = Array.init n (fun i -> long pointy.(perm.(i))) in

  let prefix = Array.make n (0L,0L) in
  let suffix = Array.make n (0L,0L) in

  prefix.(0) <- (pointy.(0), pointy.(0));
  for i=1 to n-1 do
    prefix.(i) <- minmax prefix.(i-1) pointy.(i)
  done;

  suffix.(n-1) <- (pointy.(n-1), pointy.(n-1));
  for i=n-2 downto 0 do
    suffix.(i) <- minmax suffix.(i+1) pointy.(i)
  done;

  let dist_works d =
    let rec scan i j =
      if i=0 && j=n-1 then true else
      (* (i,j) is a new range. Check it *)
	let (min3, max3) =
	  match (i=0, j=n-1) with
	    | (true,true) -> failwith "no way"
	    | (true,false) -> suffix.(j+1)
	    | (false,true) -> prefix.(i-1)
	    | (false,false) -> minmax1 suffix.(j+1) prefix.(i-1)
	in
	let dist2_from_axis a b = max (sq a) (sq b) in
	let dx = dist2_from_axis pointx.(i) pointx.(j) in
	let dy = dist2_from_axis min3 max3 in
	if sq (max3 -- min3) <= d && dx ++ dy <= d then true
	else if j = n-1 && i = n-1 then false else
	  let j' = if j=n-1 || sq (pointx.(j+1)--pointx.(i)) > d then j else j+1 in
	  let i' = if j=j' then i+1 else i in
	  scan i' j'
    in
    let rec findj j = if j=n-1 then j else if pointx.(j+1) >= 0L then j else findj (j+1) in
    let j = findj 0 in
    let rec findi i = if sq (pointx.(j)--pointx.(i)) <= d then i else findi (i+1) in
    let i = findi 0 in
    scan i j
  in

  let rec bsearch lo hi =  (* lo fails, hi works *)
    if lo++1L >= hi then hi else
      let mid = (lo ++ hi) // 2L in
      if dist_works mid then bsearch lo mid else bsearch mid hi
  in

  (* let rec findlo lo = if not (dist_works lo) then lo else findlo (lo//4L) in
  let lo = findlo 40000000000000000L in *)

  let maxd = sq (pointx.(0) -- pointx.(n-1)) in
  let maxy = maxf 0 (n-1) (fun i -> pointy.(i)) in
  let miny = minf 0 (n-1) (fun i -> pointy.(i)) in
  let opt1 = min maxd (sq (maxy -- miny)) in
  let opt2 = if dist_works 0L then 0L else bsearch 0L opt1 in

  printf "%Ld\n" opt2


You have matrix a of size n × n. Let's number the rows of the matrix from 1 to n from top to bottom, let's number the columns from 1 to n from left to right. Let's use aij to represent the element on the intersection of the i-th row and the j-th column. Matrix a meets the following two conditions:   for any numbers i, j (1 ≤ i, j ≤ n) the following inequality holds: aij ≥ 0;  . Matrix b is strictly positive, if for any numbers i, j (1 ≤ i, j ≤ n) the inequality bij > 0 holds. You task is to determine if there is such integer k ≥ 1, that matrix ak is strictly positive.

The first line contains integer n (2 ≤ n ≤ 2000) — the number of rows and columns in matrix a. The next n lines contain the description of the rows of matrix a. The i-th line contains n non-negative integers ai1, ai2, ..., ain (0 ≤ aij ≤ 50). It is guaranteed that .

If there is a positive integer k ≥ 1, such that matrix ak is strictly positive, print "YES" (without the quotes). Otherwise, print "NO" (without the quotes).

(*
   In graph theoretic terms, b_ij > 0 in the matrix A^k means that there is a
   path of k steps from i to j.  Now, because there is at least one non-zero
   on the diagonal, it means that node represents a cycle of length 1.  So if
   the graph has one strong component then eventually with a high enough power
   you will e able to get from anywhere to anywhere in that number of steps.
   Also, if the graph is not strongly connected, then there will be some b_ij
   which always remain 0.

   So the property holds if and only if the graph has one big strong component.

   If they had not made the condition that there is a non-zero on the diagonal,
   then we would also have to somehow determine if the GCD of the cycle lengths
   in the graph. I'm not sure how to do that efficiently.

                             Danny Sleator, March 2014
*)

open Printf
open Scanf

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

let strong edge degree =
  let n = Array.length degree in
  let num = Array.make n 0 in
  let low = Array.make n 0 in
  let answer = Array.make n 0 in
  let onstack = Array.make n false in

  let stacktop = ref [] in
  let stackpush x =
    stacktop := x :: !stacktop;
    onstack.(x) <- true
  in

  let stackpop () =
    match !stacktop with [] -> failwith "popping an empty stack"
      | x::t ->
	stacktop := t;
	onstack.(x) <- false;
	x
  in

  let count = ref 0 in
  let comp = ref 0 in

  let rec dfs u =
    if num.(u) <> 0 then () else (
      count := !count + 1;
      num.(u) <- !count;
      low.(u) <- !count;
      stackpush u;

      for i=0 to degree.(u)-1 do dfs edge.(u).(i) done;
      for i=0 to degree.(u)-1 do
	let v = edge.(u).(i) in
	if onstack.(v) then low.(u) <- min low.(u) low.(v);
      done;

      if num.(u) = low.(u) then (
	let rec loop () =
	  let x = stackpop() in
	  answer.(x) <- !comp;
	  if x <> u then loop();
	in
	loop ();
	comp := !comp + 1
      )
    )
  in

  for x=0 to n-1 do dfs x done;
  (!comp, answer)

let () =
  let n = read_int () in
  let edge = Array.make_matrix n n 0 in
  let degree = Array.make n 0 in
  for i=0 to n-1 do
    for j=0 to n-1 do
      if read_int() > 0 then (
	let d = degree.(i) in
	edge.(i).(d) <- j;
	degree.(i) <- d + 1;
      )
    done
  done;
  let (comp,_) = strong edge degree in
  if comp=1 then printf "YES\n" else printf "NO\n";


There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way.

The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$.

Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable.

NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below:

open! Printf;;

module String = struct
	include String
	let split c str =
		let str = String.concat "" [str; String.make 1 c] in
		let cur = ref [] in
		let res = ref [] in
		String.iter (fun a ->
			match a = c with
			| false -> cur := (String.make 1 a) :: !cur
			| true ->
				let char_list = List.rev (!cur) in
				res := (String.concat "" char_list) :: !res;
				cur := []) str ;
			List.rev !res
	;;
end;;

let read_int_list () =
	read_line () |> String.split ' ' |> List.map int_of_string
;;

let read_2_ints () =
	match read_int_list () with
	| [a; b] -> a, b
	| _ -> assert false
;;

let compare (a, b) (c, d) =
	if a == c then compare b  d
	else compare a c
;;

let tests = read_int () in
for _ = 1 to tests do
	let n = read_int () in
	let list = ref [] in
	for _ = 1 to n do
		list := (read_2_ints ()) :: !list;
		()
	done;
	let list = List.rev (!list) |> List.sort compare in
	let check (a, b, bo) (c, d) =
		c, d, (match bo with
		| false -> false
		| true -> (a<=c) && (b<=d))
	in
	let check = List.fold_left check (0, 0, true) list in
	(match check with
	| _, _, false -> print_string "NO"
	| _, _, true ->
		print_string "YES\n";
		let move (a, b) (c, d) =
			print_string (String.make (c-a) 'R');
			print_string (String.make (d-b) 'U');
			(c, d)
		in
		let _ = List.fold_left move (0, 0) list in
		());
	print_newline ()
done;


You are given two strings of equal length $$$s$$$ and $$$t$$$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.For example, if $$$s$$$ is "acbc" you can get the following strings in one operation:   "aabc" (if you perform $$$s_2 = s_1$$$);  "ccbc" (if you perform $$$s_1 = s_2$$$);  "accc" (if you perform $$$s_3 = s_2$$$ or $$$s_3 = s_4$$$);  "abbc" (if you perform $$$s_2 = s_3$$$);  "acbb" (if you perform $$$s_4 = s_3$$$); Note that you can also apply this operation to the string $$$t$$$.Please determine whether it is possible to transform $$$s$$$ into $$$t$$$, applying the operation above any number of times.Note that you have to answer $$$q$$$ independent queries.

The first line contains one integer $$$q$$$ ($$$1 \le q \le 100$$$) — the number of queries. Each query is represented by two consecutive lines. The first line of each query contains the string $$$s$$$ ($$$1 \le |s| \le 100$$$) consisting of lowercase Latin letters. The second line of each query contains the string $$$t$$$ ($$$1 \le |t| \leq 100$$$, $$$|t| = |s|$$$) consisting of lowercase Latin letters.

For each query, print "YES" if it is possible to make $$$s$$$ equal to $$$t$$$, and "NO" otherwise. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as positive answer).

NoteIn the first query, you can perform two operations $$$s_1 = s_2$$$ (after it $$$s$$$ turns into "aabb") and $$$t_4 = t_3$$$ (after it $$$t$$$ turns into "aabb"). In the second query, the strings are equal initially, so the answer is "YES".In the third query, you can not make strings $$$s$$$ and $$$t$$$ equal. Therefore, the answer is "NO".

module SS = Set.Make(struct type t = char let compare = compare end)

let to_set string =
  let s = ref SS.empty in
  String.iter (fun c -> s := SS.add c !s) string;
  !s

let _ =
  let n = int_of_string (input_line stdin) in
  for i = 1 to n do
    let l = input_line stdin in
    let r = input_line stdin in
    let l = to_set l in
    let r = to_set r in
    if SS.is_empty (SS.inter l r) then
      Format.printf "NO@."
    else
      Format.printf "YES@."

  done


You might have remembered Theatre square from the problem 1A. Now it's finally getting repaved.The square still has a rectangular shape of $$$n \times m$$$ meters. However, the picture is about to get more complicated now. Let $$$a_{i,j}$$$ be the $$$j$$$-th square in the $$$i$$$-th row of the pavement.You are given the picture of the squares:  if $$$a_{i,j} = $$$ "*", then the $$$j$$$-th square in the $$$i$$$-th row should be black;  if $$$a_{i,j} = $$$ ".", then the $$$j$$$-th square in the $$$i$$$-th row should be white. The black squares are paved already. You have to pave the white squares. There are two options for pavement tiles:  $$$1 \times 1$$$ tiles — each tile costs $$$x$$$ burles and covers exactly $$$1$$$ square;  $$$1 \times 2$$$ tiles — each tile costs $$$y$$$ burles and covers exactly $$$2$$$ adjacent squares of the same row. Note that you are not allowed to rotate these tiles or cut them into $$$1 \times 1$$$ tiles. You should cover all the white squares, no two tiles should overlap and no black squares should be covered by tiles.What is the smallest total price of the tiles needed to cover all the white squares?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of testcases. Then the description of $$$t$$$ testcases follow. The first line of each testcase contains four integers $$$n$$$, $$$m$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 100$$$; $$$1 \le m \le 1000$$$; $$$1 \le x, y \le 1000$$$) — the size of the Theatre square, the price of the $$$1 \times 1$$$ tile and the price of the $$$1 \times 2$$$ tile. Each of the next $$$n$$$ lines contains $$$m$$$ characters. The $$$j$$$-th character in the $$$i$$$-th line is $$$a_{i,j}$$$. If $$$a_{i,j} = $$$ "*", then the $$$j$$$-th square in the $$$i$$$-th row should be black, and if $$$a_{i,j} = $$$ ".", then the $$$j$$$-th square in the $$$i$$$-th row should be white. It's guaranteed that the sum of $$$n \times m$$$ over all testcases doesn't exceed $$$10^5$$$.

For each testcase print a single integer — the smallest total price of the tiles needed to cover all the white squares in burles.

NoteIn the first testcase you are required to use a single $$$1 \times 1$$$ tile, even though $$$1 \times 2$$$ tile is cheaper. So the total price is $$$10$$$ burles.In the second testcase you can either use two $$$1 \times 1$$$ tiles and spend $$$20$$$ burles or use a single $$$1 \times 2$$$ tile and spend $$$1$$$ burle. The second option is cheaper, thus the answer is $$$1$$$.The third testcase shows that you can't rotate $$$1 \times 2$$$ tiles. You still have to use two $$$1 \times 1$$$ tiles for the total price of $$$20$$$.In the fourth testcase the cheapest way is to use $$$1 \times 1$$$ tiles everywhere. The total cost is $$$6 \cdot 3 = 18$$$.

open Printf
open Scanf

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

let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let read_string _ = bscanf Scanning.stdin " %s " (fun x -> x)
let read_pair _ = bscanf Scanning.stdin " %d %d " (fun x y -> (x,y))
let () =
  let cases = read_int () in
  for case = 1 to cases do
    let (n,m) = read_pair() in
    let (x,y) = read_pair() in
    let board = Array.init n read_string in

    if y >= 2*x then (
      let n_white = sum 0 (n-1) (fun i -> sum 0 (m-1) (fun j ->
	if board.(i).[j] = '.' then 1 else 0))
      in
      printf "%d\n" (n_white * x)
    ) else (
      let rec loop i j ac1 ac2 =
	(* filled all cells before i.  ac1 = # singles, ac2 = # dominoes *)
	if j=m then (ac1, ac2) else
	  match board.(i).[j] with
	    | '.' ->
	      if j+1 < m && board.(i).[j+1] = '.'
	      then loop i (j+2) ac1 (ac2+1)
	      else loop i (j+1) (ac1+1) ac2
	    | '*' -> loop i (j+1) ac1 ac2
	    | _ -> failwith "bad board"
      in
      let singles = sum 0 (n-1) (fun i -> fst (loop i 0 0 0)) in
      let doubles = sum 0 (n-1) (fun i -> snd (loop i 0 0 0)) in
      printf "%d\n" (singles * x + doubles * y)
    )

  done


Ayush and Ashish play a game on an unrooted tree consisting of $$$n$$$ nodes numbered $$$1$$$ to $$$n$$$. Players make the following move in turns:   Select any leaf node in the tree and remove it together with any edge which has this node as one of its endpoints. A leaf node is a node with degree less than or equal to $$$1$$$. A tree is a connected undirected graph without cycles.There is a special node numbered $$$x$$$. The player who removes this node wins the game. Ayush moves first. Determine the winner of the game if each player plays optimally.

The first line of the input contains a single integer $$$t$$$ $$$(1 \leq t \leq 10)$$$ — the number of testcases. The description of the test cases follows. The first line of each testcase contains two integers $$$n$$$ and $$$x$$$ $$$(1\leq n \leq 1000, 1 \leq x \leq n)$$$ — the number of nodes in the tree and the special node respectively. Each of the next $$$n-1$$$ lines contain two integers $$$u$$$, $$$v$$$ $$$(1 \leq u, v \leq n, \text{ } u \ne v)$$$, meaning that there is an edge between nodes $$$u$$$ and $$$v$$$ in the tree.

For every test case, if Ayush wins the game, print "Ayush", otherwise print "Ashish" (without quotes).

NoteFor the $$$1$$$st test case, Ayush can only remove node $$$2$$$ or $$$3$$$, after which node $$$1$$$ becomes a leaf node and Ashish can remove it in his turn.For the $$$2$$$nd test case, Ayush can remove node $$$2$$$ in the first move itself.

open Printf
open Scanf

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

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

  for case = 1 to cases do
    let (n,x) = read_pair() in
    let degx = ref 0 in
    for i=1 to n-1 do
      let (u,v) = read_pair() in
      if u=x || v=x then degx := 1 + !degx
    done;

    let answer = if !degx = 1 || !degx = 0 then "Ayush"
      else if n mod 2 = 0 then "Ayush" else "Ashish"
    in

    printf "%s\n" answer
  done


Petya has n integers: 1, 2, 3, ..., n. He wants to split these integers in two non-empty groups in such a way that the absolute difference of sums of integers in each group is as small as possible. Help Petya to split the integers. Each of n integers should be exactly in one group.

The first line contains a single integer n (2 ≤ n ≤ 60 000) — the number of integers Petya has.

Print the smallest possible absolute difference in the first line. In the second line print the size of the first group, followed by the integers in that group. You can print these integers in arbitrary order. If there are multiple answers, print any of them.

NoteIn the first example you have to put integers 1 and 4 in the first group, and 2 and 3 in the second. This way the sum in each group is 5, and the absolute difference is 0.In the second example there are only two integers, and since both groups should be non-empty, you have to put one integer in the first group and one in the second. This way the absolute difference of sums of integers in each group is 1.

open Printf
open Scanf

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

let () =
  let n = read_int () in

  let printmod4 st n =
    for i = 1 to n/4 do
      printf "%d %d " (st+i-1) (st+n-i)
    done;
    print_newline()
  in

  match n mod 4 with
    | 0 ->
      printf "0\n";
      printf "%d " (n/2);
      printmod4 1 n
    | 1 ->
      printf "1\n";
      printf "%d " ((n-1)/2);
      printmod4 2 (n-1);
    | 2 ->
      printf "1\n";
      printf "%d " (1 + (n-2)/2);
      printf "%d " 1;
      printmod4 3 (n-2);
    | 3 ->
      printf "0\n";
      printf "%d " (1 + (n-3)/2);
      printf "%d " 3;
      printmod4 4 (n-3);
    | _ -> failwith "can't happen"


Nian is a monster which lives deep in the oceans. Once a year, it shows up on the land, devouring livestock and even people. In order to keep the monster away, people fill their villages with red colour, light, and cracking noise, all of which frighten the monster out of coming.Little Tommy has n lanterns and Big Banban has m lanterns. Tommy's lanterns have brightness a1, a2, ..., an, and Banban's have brightness b1, b2, ..., bm respectively.Tommy intends to hide one of his lanterns, then Banban picks one of Tommy's non-hidden lanterns and one of his own lanterns to form a pair. The pair's brightness will be the product of the brightness of two lanterns.Tommy wants to make the product as small as possible, while Banban tries to make it as large as possible.You are asked to find the brightness of the chosen pair if both of them choose optimally.

The first line contains two space-separated integers n and m (2 ≤ n, m ≤ 50). The second line contains n space-separated integers a1, a2, ..., an. The third line contains m space-separated integers b1, b2, ..., bm. All the integers range from  - 109 to 109.

Print a single integer — the brightness of the chosen pair.

NoteIn the first example, Tommy will hide 20 and Banban will choose 18 from Tommy and 14 from himself.In the second example, Tommy will hide 3 and Banban will choose 2 from Tommy and 1 from himself.

open Scanf
open Printf

module Int64M = struct
  include Int64
  let (+) = Int64.add
  let (-) = Int64.sub
  let ( * ) = Int64.mul
  let compare = Int64.compare
  let rem = Int64.rem
  let of_int = Int64.of_int
  let of_string = Int64.of_string
end

let input_list n =
  let input_int _ = scanf " %d" (fun x -> x) in
  Array.init n input_int

let array_count f a =
  let g i v = if f v then i + 1 else i in
  Array.fold_left g 0 a

let count_polarity a =
  let negzero = array_count (fun v -> v <= 0) a in
  (negzero, Array.length a - negzero)

let array_max a = Array.fold_left max a.(0) a
let array_min a = Array.fold_left min a.(0) a

let filter_once n a =
  let filtered = ref false in
  let f i =
    if !filtered = true
    then a.(i + 1)
    else
      if a.(i) = n
      then (filtered := true; a.(i + 1))
      else a.(i)
  in
  Array.init (Array.length a - 1) f

let do_compute tommy banban =
  let ret = ref (Int64.min_int, min_int, min_int) in
  let open Int64M in
  Array.iter (fun t ->
    Array.iter (fun b ->
      let (curmax, _, _) = !ret in
      if t * b > curmax then ret := (t * b, to_int t, to_int b)
    ) banban
  ) tommy;
  !ret

let rec compute tommy banban =
  let (max1, t1, b1) = do_compute tommy banban in
  (* Printf.printf "%d %d %d\n" (Int64.to_int max1) t1 b1; *)
  let tommy_filtered = filter_once (Int64.of_int t1) tommy in
  let (max2, t2, b2) = do_compute tommy_filtered banban in
  (* Printf.printf "%d %d %d\n" (Int64.to_int max2) t2 b2; *)
  max2

let () =
  let n, m = scanf " %u %u " (fun x y -> x, y) in
  let tommy = input_list n in
  let banban = input_list m in
  let tommy = Array.map Int64.of_int tommy in
  let banban = Array.map Int64.of_int banban in
  let ret = compute tommy banban in
  ret |> Int64.to_string |> print_endline

  (* let tn, tp = count_polarity tommy in
  let bn, bp = count_polarity banban in *)
  (* the decision tree is too fucking tricky and
    I'm going to bruteforce as the tag suggested ... *)
  (* let ret: int =
    if bp > 0
      then if tn = 0 then
      (* T no negative, B has positive -> max2 T * max B *)
        0
      else if ...  *)
  (* in *)


In this task you need to process a set of stock exchange orders and use them to create order book.An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price pi, direction di — buy or sell, and integer qi. This means that the participant is ready to buy or sell qi stocks at price pi for one stock. A value qi is also known as a volume of an order.All orders with the same price p and direction d are merged into one aggregated order with price p and direction d. The volume of such order is a sum of volumes of the initial orders.An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order.An order book of depth s contains s best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than s aggregated orders for some direction then all of them will be in the final order book.You are given n stock exhange orders. Your task is to print order book of depth s for these orders.

The input starts with two positive integers n and s (1 ≤ n ≤ 1000, 1 ≤ s ≤ 50), the number of orders and the book depth. Next n lines contains a letter di (either 'B' or 'S'), an integer pi (0 ≤ pi ≤ 105) and an integer qi (1 ≤ qi ≤ 104) — direction, price and volume respectively. The letter 'B' means buy, 'S' means sell. The price of any sell order is higher than the price of any buy order.

Print no more than 2s lines with aggregated orders from order book of depth s. The output format for orders should be the same as in input.

NoteDenote (x, y) an order with price x and volume y. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample.You need to print no more than two best orders for each direction, so you shouldn't print the order (10 3) having the worst price among buy orders.

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

let rec takeTo n l =
    match l with
    | [] -> []
    | h :: t -> if n = 0 then [] else h :: takeTo (n - 1) t

let addOrder s p q =
    let prev = if IntMap.mem p s
    then IntMap.find p s
    else 0
    in

    IntMap.add p (q + prev) s

let () = Scanf.scanf "%d %d\n" (fun n s ->
    let buy = ref IntMap.empty
    and sell = ref IntMap.empty in

    for i = 1 to n do
        (* direction, price and quantity *)
        Scanf.scanf "%c %d %d\n" (fun d p q ->
            match d with
            | 'B' -> buy := addOrder !buy ~-p q
            | 'S' -> sell := addOrder !sell p q
        )
    done;

    List.iter (fun (p, q) -> Printf.printf "S %d %d\n" p q)
              (List.rev (takeTo s (IntMap.bindings !sell)));
    List.iter (fun (p, q) -> Printf.printf "B %d %d\n" ~-p q)
              (takeTo s (IntMap.bindings !buy))
)

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

let rec takeTo n l =
    match l with
    | [] -> []
    | h :: t -> if n = 0 then [] else h :: takeTo (n - 1) t

let addOrder s p q =
    let prev = if IntMap.mem p s
    then IntMap.find p s
    else 0
    in

    IntMap.add p (q + prev) s

let () = Scanf.scanf "%d %d\n" (fun n s ->
    let buy = ref IntMap.empty
    and sell = ref IntMap.empty in

    for i = 1 to n do
        (* direction, price and quantity *)
        Scanf.scanf "%c %d %d\n" (fun d p q ->
            match d with
            | 'B' -> buy := addOrder !buy ~-p q
            | 'S' -> sell := addOrder !sell p q
        )
    done;

    List.iter (fun (p, q) -> Printf.printf "S %d %d\n" p q)
              (takeTo s (IntMap.bindings !sell));
    List.iter (fun (p, q) -> Printf.printf "B %d %d\n" ~-p q)
              (takeTo s (IntMap.bindings !buy))
)


An undirected graph is called k-regular, if the degrees of all its vertices are equal k. An edge of a connected graph is called a bridge, if after removing it the graph is being split into two connected components.Build a connected undirected k-regular graph containing at least one bridge, or else state that such graph doesn't exist.

The single line of the input contains integer k (1 ≤ k ≤ 100) — the required degree of the vertices of the regular graph.

Print "NO" (without quotes), if such graph doesn't exist.  Otherwise, print "YES" in the first line and the description of any suitable graph in the next lines. The description of the made graph must start with numbers n and m — the number of vertices and edges respectively.  Each of the next m lines must contain two integers, a and b (1 ≤ a, b ≤ n, a ≠ b), that mean that there is an edge connecting the vertices a and b. A graph shouldn't contain multiple edges and edges that lead from a vertex to itself. A graph must be connected, the degrees of all vertices of the graph must be equal k. At least one edge of the graph must be a bridge. You can print the edges of the graph in any order. You can print the ends of each edge in any order. The constructed graph must contain at most 106 vertices and 106 edges (it is guaranteed that if at least one graph that meets the requirements exists, then there also exists the graph with at most 106 vertices and at most 106 edges).

NoteIn the sample from the statement there is a suitable graph consisting of two vertices, connected by a single edge.

let f k =
	for i=1 to k-1 do
		for j=k to 2*k-2 do
			Printf.printf "%d %d\n" i j;
		done;
		if i mod 2 = 1
			then Printf.printf "%d %d\n" i (i+1);
	done;
	for j=k to 2*k-2 do
		Printf.printf "%d %d\n" j (2*k-1);
	done;
	for i=1 to k-1 do
		for j=k to 2*k-2 do
			Printf.printf "%d %d\n" (2*k - 1 + i) (2*k - 1 + j);
		done;
		if i mod 2 = 1
			then Printf.printf "%d %d\n" (2*k - 1 + i) (2*k + i);
	done;
	for j=k to 2*k-2 do
		Printf.printf "%d %d\n" (2*k - 1 + j) (4*k-2);
	done;
	Printf.printf "%d %d\n" (4*k -2) (2*k - 1)

let () =
	let k = Scanf.scanf "%d" (fun i -> i) in
		if k mod 2 = 0
			then Printf.printf "NO"
			else begin
			     Printf.printf "YES\n";
			     Printf.printf "%d %d\n" (4*(k-1) +2) (k*(2*(k-1) + 1));
			     f k
			     end

let f k =
	for i=1 to k-1 do
		for j=k to 2*k-2 do
			Printf.printf "%d %d\n" i j;
			Printf.printf "%d %d\n" j i;
		done;
		if i mod 2 = 1
			then (Printf.printf "%d %d\n" i (i+1);
			      Printf.printf "%d %d\n" (i+1) i);
	done;
	for j=k to 2*k-2 do
		Printf.printf "%d %d\n" j (2*k-1);
		Printf.printf "%d %d\n" (2*k-1) j;
	done;
	for i=1 to k-1 do
		for j=k to 2*k-2 do
			Printf.printf "%d %d\n" (2*k - 1 + i) (2*k - 1 + j);
			Printf.printf "%d %d\n" (2*k - 1 + j) (2*k - 1 + i);
		done;
		if i mod 2 = 1
			then (Printf.printf "%d %d\n" (2*k - 1 + i) (2*k + i);
Printf.printf "%d %d\n" (2*k + i) (2*k - 1 + i));
	done;
	for j=k to 2*k-2 do
		Printf.printf "%d %d\n" (2*k - 1 + j) (4*k-2);
		Printf.printf "%d %d\n" (4*k-2)  (2*k - 1 + j);
	done;
	Printf.printf "%d %d\n" (4*k -2) (2*k - 1);
	Printf.printf "%d %d\n" (2*k -1) (4*k - 2)

let () =
	let k = Scanf.scanf "%d" (fun i -> i) in
		if k mod 2 = 0
			then Printf.printf "NO"
			else begin
			     Printf.printf "YES\n";
			     f k
			     end