$$$ \def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} $$$ $$$\def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\RED$$$ or $$$\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\myblue{2}, 8, \myred{6}, \myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\text{Sum}(\RED)=6$$$, $$$\text{Sum}(\BLUE)=2+3=5$$$, $$$\text{Count}(\RED)=1$$$, and $$$\text{Count}(\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\text{Sum}(\RED) > \text{Sum}(\BLUE)$$$ and $$$\text{Count}(\RED) < \text{Count}(\BLUE)$$$.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\le n\le 2\cdot 10^5$$$) — the length of the given sequence.  The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.

For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).

NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myblue{1},\myblue{2},\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\text{Count}(\RED)=1 < \text{Count}(\BLUE)=2$$$, but $$$\text{Sum}(\RED)=3 \ngtr \text{Sum}(\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\text{Sum}(\RED)=6 > \text{Sum}(\BLUE)=5$$$ and $$$\text{Count}(\RED)=1 < \text{Count}(\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\myred{3},\myred{5},\myblue{4}, \myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\text{Sum}(\RED) = 8 > \text{Sum}(\BLUE) = 6$$$ but $$$\text{Count}(\RED) = 2 \nless \text{Count}(\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.

(* can paint each element Red, Blue, unpainted
   Count(c) == number painted with color
   Sum(c) == sum of elemented painted with color

   Is it possible to paint so Sum(R) > Sum(B) but Count(R) < Count(B)
   the minimum case would be (since we using ints):
   Sum(R) >= Sum(B)+1 and Count(R)<=Count(B)+1
   ie r1+r2+...+r(n-1) >= b1 + b2 + ... +bn+1

   for 1 2 3 , it's not possible
   ie if we pick max for r and then rest for b,
    we get S(R)=3 and S(B)=3

   best tactic would be to pick the maximum
   elements for r and minimum elements for b

   to exemplify the difference, since all rx > by,
   we should select the maximum count for both groups, ie
   floor((n+1/2)) items for b and floor((n-1)/2) items for r

   for even n, n/2 items for b, n/2-1 items for r, missing n/2 idx
   -> up to n/2-1 idx for b, miss n/2 and then r
   for odd n, (n+1)/2 items for b and (n-1)/2 items for a
   -> up to (n-1)/2 idx for b, then n-1 for r
*)

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

(* Fast quick sort *)
let rec partition pivot left right = function
| [] -> left, right
| x::xs ->
  if (x<=pivot) then partition pivot (x::left) right xs
  else partition pivot left (x::right) xs
;;
let rec quickSort sorted = function
| [] -> sorted
| [pivot] -> pivot::sorted
| pivot::xs ->
  let left, right = partition pivot [] [] xs in
  quickSort (pivot::(quickSort sorted right)) left
;;

let rec read_arr = function
| 0 -> []
| n ->
  let read_float () = Scanf.scanf " %f" (fun x-> x) in
  read_float () :: read_arr (n-1)
;;
(*
   1 2 3
   (n-1)/2 = 1, so 0,1 idxs for b and 2 for r
   1 2
   (n-1)/2 = 1 so 0 idxs for b and none for r
   1 2 3 4
   n/2 = 2 so 0,1,2 idxs for b and 3 for r
*)
let rec get_r_b r b n = function
| _, [] -> r, b
| idx, x::xs ->
  if (n mod 2 == 0) then begin
  if (idx <= n/2-1) then get_r_b r (x+.b) n (idx+1, xs)
  else if (idx == n/2) then get_r_b r b n (idx+1, xs)
  else get_r_b (x+.r) b n (idx+1, xs)
  end
  else begin
    if (idx <= (n-1)/2) then get_r_b r (x+.b) n (idx+1, xs)
    else get_r_b (x+.r) b n (idx+1, xs)
  end
;;

let run_case = function
| () ->
  let n = read_int () in
  (* let arr = quickSort [] (read_arr n) in *)
  let arr = List.fast_sort compare (read_arr n) in
  (* List.iter (fun x-> Printf.printf " %f" x) arr; *)
  let r, b = get_r_b 0.0 0.0 n (0, arr) in
  (* Printf.printf "\n%f %f" r b; *)
  if (r>=b+.1.0) then Printf.printf "YES\n"
  else Printf.printf "NO\n"
;;

let rec iter_cases = function
| 0 -> 0
| n ->
  run_case ();
  iter_cases (n-1)
;;

let t=read_int () in
iter_cases t;;

(* can paint each element Red, Blue, unpainted
   Count(c) == number painted with color
   Sum(c) == sum of elemented painted with color

   Is it possible to paint so Sum(R) > Sum(B) but Count(R) < Count(B)
   the minimum case would be (since we using ints):
   Sum(R) >= Sum(B)+1 and Count(R)<=Count(B)+1
   ie r1+r2+...+r(n-1) >= b1 + b2 + ... +bn+1

   for 1 2 3 , it's not possible
   ie if we pick max for r and then rest for b,
    we get S(R)=3 and S(B)=3

   best tactic would be to pick the maximum
   elements for r and minimum elements for b

   to exemplify the difference, since all rx > by,
   we should select the maximum count for both groups, ie
   floor((n+1/2)) items for b and floor((n-1)/2) items for r

   for even n, n/2 items for b, n/2-1 items for r, missing n/2 idx
   -> up to n/2-1 idx for b, miss n/2 and then r
   for odd n, (n+1)/2 items for b and (n-1)/2 items for a
   -> up to (n-1)/2 idx for b, then n-1 for r
*)

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

(* Fast quick sort *)
let rec partition pivot left right = function
| [] -> left, right
| x::xs ->
  if (x<=pivot) then partition pivot (x::left) right xs
  else partition pivot left (x::right) xs
;;
let rec quickSort sorted = function
| [] -> sorted
| [pivot] -> pivot::sorted
| pivot::xs ->
  let left, right = partition pivot [] [] xs in
  quickSort (pivot::(quickSort sorted right)) left
;;

let rec read_arr = function
| 0 -> []
| n ->
  let read_float () = Scanf.scanf " %f" (fun x-> x) in
  read_float () :: read_arr (n-1)
;;
(*
   1 2 3
   (n-1)/2 = 1, so 0,1 idxs for b and 2 for r
   1 2
   (n-1)/2 = 1 so 0 idxs for b and none for r
   1 2 3 4
   n/2 = 2 so 0,1,2 idxs for b and 3 for r
*)
let rec get_r_b r b n = function
| _, [] -> r, b
| idx, x::xs ->
  if (n mod 2 == 0) then begin
  if (idx <= n/2-1) then get_r_b r (x+.b) n (idx+1, xs)
  else if (idx == n/2) then get_r_b r b n (idx+1, xs)
  else get_r_b (x+.r) b n (idx+1, xs)
  end
  else begin
    if (idx <= (n-1)/2) then get_r_b r (x+.b) n (idx+1, xs)
    else get_r_b (x+.r) b n (idx+1, xs)
  end
;;

let run_case = function
| () ->
  let n = read_int () in
  (* let arr = quickSort [] (read_arr n) in *)
  let arr = List.sort (fun x y -> ~- (compare x y)) (read_arr n) in
  (* List.iter (fun x-> Printf.printf " %f" x) arr; *)
  let r, b = get_r_b 0.0 0.0 n (0, arr) in
  (* Printf.printf "\n%f %f" r b; *)
  if (r>=b+.1.0) then Printf.printf "YES\n"
  else Printf.printf "NO\n"
;;

let rec iter_cases = function
| 0 -> 0
| n ->
  run_case ();
  iter_cases (n-1)
;;

let t=read_int () in
iter_cases t;;

(* can paint each element Red, Blue, unpainted
   Count(c) == number painted with color
   Sum(c) == sum of elemented painted with color

   Is it possible to paint so Sum(R) > Sum(B) but Count(R) < Count(B)
   the minimum case would be (since we using ints):
   Sum(R) >= Sum(B)+1 and Count(R)<=Count(B)+1
   ie r1+r2+...+r(n-1) >= b1 + b2 + ... +bn+1

   for 1 2 3 , it's not possible
   ie if we pick max for r and then rest for b,
    we get S(R)=3 and S(B)=3

   best tactic would be to pick the maximum
   elements for r and minimum elements for b

   to exemplify the difference, since all rx > by,
   we should select the maximum count for both groups, ie
   floor((n+1/2)) items for b and floor((n-1)/2) items for r

   for even n, n/2 items for b, n/2-1 items for r, missing n/2 idx
   -> up to n/2-1 idx for b, miss n/2 and then r
   for odd n, (n+1)/2 items for b and (n-1)/2 items for a
   -> up to (n-1)/2 idx for b, then n-1 for r
*)

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

(* Fast quick sort *)
let rec partition pivot left right = function
| [] -> left, right
| x::xs ->
  if (x<=pivot) then partition pivot (x::left) right xs
  else partition pivot left (x::right) xs
;;
let rec quickSort sorted = function
| [] -> sorted
| [pivot] -> pivot::sorted
| pivot::xs ->
  let left, right = partition pivot [] [] xs in
  quickSort (pivot::(quickSort sorted right)) left
;;

let rec read_arr = function
| 0 -> []
| n ->
  let read_float () = Scanf.scanf " %f" (fun x-> x) in
  read_float () :: read_arr (n-1)
;;
(*
   1 2 3
   (n-1)/2 = 1, so 0,1 idxs for b and 2 for r
   1 2
   (n-1)/2 = 1 so 0 idxs for b and none for r
   1 2 3 4
   n/2 = 2 so 0,1,2 idxs for b and 3 for r
*)
let rec get_r_b r b n = function
| _, [] -> r, b
| idx, x::xs ->
  if (n mod 2 == 0) then begin
  if (idx <= n/2-1) then get_r_b r (x+.b) n (idx+1, xs)
  else if (idx == n/2) then get_r_b r b n (idx+1, xs)
  else get_r_b (x+.r) b n (idx+1, xs)
  end
  else begin
    if (idx <= (n-1)/2) then get_r_b r (x+.b) n (idx+1, xs)
    else get_r_b (x+.r) b n (idx+1, xs)
  end
;;

let run_case = function
| () ->
  let n = read_int () in
  let arr = quickSort [] (read_arr n) in
(*  List.iter (fun x-> Printf.printf " %f" x) arr;*)
  let r, b = get_r_b 0.0 0.0 n (0, arr) in
  Printf.printf "\n%f %f" r b;
  if (r>=b+.1.0) then Printf.printf "YES\n"
  else Printf.printf "NO\n"
;;

let rec iter_cases = function
| 0 -> 0
| n ->
  run_case ();
  iter_cases (n-1)
;;

let t=read_int () in
iter_cases t;;

(* can paint each element Red, Blue, unpainted
   Count(c) == number painted with color
   Sum(c) == sum of elemented painted with color

   Is it possible to paint so Sum(R) > Sum(B) but Count(R) < Count(B)
   the minimum case would be (since we using ints):
   Sum(R) >= Sum(B)+1 and Count(R)<=Count(B)+1
   ie r1+r2+...+r(n-1) >= b1 + b2 + ... +bn+1

   for 1 2 3 , it's not possible
   ie if we pick max for r and then rest for b,
    we get S(R)=3 and S(B)=3

   best tactic would be to pick the maximum
   elements for r and minimum elements for b

   to exemplify the difference, since all rx > by,
   we should select the maximum count for both groups, ie
   floor((n+1/2)) items for b and floor((n-1)/2) items for r

*)

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

(* Fast quick sort *)
let rec partition pivot left right = function
| [] -> left, right
| x::xs ->
  if (x<=pivot) then partition pivot (x::left) right xs
  else partition pivot left (x::right) xs
;;
let rec quickSort sorted = function
| [] -> sorted
| [pivot] -> pivot::sorted
| pivot::xs ->
  let left, right = partition pivot [] [] xs in
  quickSort (pivot::(quickSort sorted right)) left
;;

let rec read_arr = function
| 0 -> []
| n ->
  let read_float () = Scanf.scanf " %f" (fun x-> x) in
  read_float () :: read_arr (n-1)
;;
(*
   1 2 3
   n/2 = 1, so 0,1 idxs for b and 2 for r
   1 2
   n/2 = 1 so 0,1 idxs for b and none for r
   1 2 3 4
   n/2 = 2 so 0,1,2 idxs for b and 3 for r
*)
let rec get_r_b r b n = function
| _, [] -> r, b
| idx, x::xs ->
  if (idx <= n/2) then get_r_b r (x+.b) n (idx+1, xs)
  else get_r_b (x+.r) b n (idx+1, xs)
;;

let run_case = function
| () ->
  let n = read_int () in
  let arr = quickSort [] (read_arr n) in
  let r, b = get_r_b 0.0 0.0 n (0, arr) in
  if (r>=b+.1.0) then Printf.printf "YES\n"
  else Printf.printf "NO\n"
;;

let rec iter_cases = function
| 0 -> 0
| n ->
  run_case ();
  iter_cases (n-1)
;;

let t=read_int () in
iter_cases t;;


Life is not easy for the perfectly common variable named Vasya. Wherever it goes, it is either assigned a value, or simply ignored, or is being used!Vasya's life goes in states of a program. In each state, Vasya can either be used (for example, to calculate the value of another variable), or be assigned a value, or ignored. Between some states are directed (oriented) transitions.A path is a sequence of states v1, v2, ..., vx, where for any 1 ≤ i < x exists a transition from vi to vi + 1.Vasya's value in state v is interesting to the world, if exists path p1, p2, ..., pk such, that pi = v for some i (1 ≤ i ≤ k), in state p1 Vasya gets assigned a value, in state pk Vasya is used and there is no state pi (except for p1) where Vasya gets assigned a value.Help Vasya, find the states in which Vasya's value is interesting to the world.

The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 105) — the numbers of states and transitions, correspondingly. The second line contains space-separated n integers f1, f2, ..., fn (0 ≤ fi ≤ 2), fi described actions performed upon Vasya in state i: 0 represents ignoring, 1 — assigning a value, 2 — using. Next m lines contain space-separated pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi), each pair represents the transition from the state number ai to the state number bi. Between two states can be any number of transitions.

Print n integers r1, r2, ..., rn, separated by spaces or new lines. Number ri should equal 1, if Vasya's value in state i is interesting to the world and otherwise, it should equal 0. The states are numbered from 1 to n in the order, in which they are described in the input.

NoteIn the first sample the program states can be used to make the only path in which the value of Vasya interests the world, 1  2  3  4; it includes all the states, so in all of them Vasya's value is interesting to the world.The second sample the only path in which Vasya's value is interesting to the world is , — 1  3; state 2 is not included there.In the third sample we cannot make from the states any path in which the value of Vasya would be interesting to the world, so the value of Vasya is never interesting to the world.

module Digraph =
struct
  type t =
      {mutable nv : int;
       mutable e : int array array;
       mutable degree : int array;
      }

  let create n =
    {nv = n;
     e = Array.make n [| |];
     degree = Array.make n 0
    }

  let nb_vertex g = g.nv

  let iter_succ f g u =
    let vs = g.e.(u) in
      for i = 0 to g.degree.(u) - 1 do
	f vs.(i)
      done

  let add_edge g u v =
    if u >= g.nv || v >= g.nv then (
      (* TODO *)
      assert false;
    );
    let {e; degree; _} = g in
      if degree.(u) >= Array.length e.(u) then (
	let s = Array.length e.(u) * 2 + 1 in
	let c = e.(u) in
	let c' = Array.make s 0 in
	  for i = 0 to Array.length c - 1 do
	    c'.(i) <- c.(i)
	  done;
	  e.(u) <- c'
      );
      e.(u).(degree.(u)) <- v;
      degree.(u) <- degree.(u) + 1

end

module Dfs =
struct

  module type G =
  sig
    type t

    val nb_vertex : t -> int
    val iter_succ : (int -> unit) -> t -> int -> unit
  end

  module Make (G : G) =
  struct
    let do_nothing _ = ()

    let iter ?(pre = do_nothing) ?(post = do_nothing) g =
      let n = G.nb_vertex g in
      let used = Array.make n false in
      let rec dfs u =
	used.(u) <- true;
	pre u;
	G.iter_succ
	  (fun v ->
	     if not used.(v) then (
	       dfs v
	     )
	  ) g u;
	post u
      in
	for i = 0 to n - 1 do
	  if not used.(i)
	  then dfs i
	done

    type iterator = G.t * bool array

    let start g =
      let n = G.nb_vertex g in
      let used = Array.make n false in
	(g, used)

    let iter_component ?(pre = do_nothing) ?(post = do_nothing) (g, used) u =
      let rec dfs u =
	used.(u) <- true;
	pre u;
	G.iter_succ
	  (fun v ->
	     if not used.(v) then (
	       dfs v
	     )
	  ) g u;
	post u
      in
	if not used.(u)
	then dfs u

    type prefix_iterator = G.t * bool array * int array

    let prefix_start g =
      let n = G.nb_vertex g in
      let used = Array.make n false in
      let stack = Array.make n 0 in
	(g, used, stack)

    let prefix_component pre (g, used, stack) u =
      if not used.(u) then (
	let pos = ref 0 in
	let rec dfs () =
	  if !pos > 0 then (
	    decr pos;
	    let u = stack.(!pos) in
	    pre u;
	    G.iter_succ
	      (fun v ->
		 if not used.(v) then (
		   stack.(!pos) <- v;
		   incr pos;
		   used.(v) <- true;
		 )
	      ) g u;
	    dfs ()
	  )
	in
	  stack.(!pos) <- u;
	  incr pos;
	  used.(u) <- true;
	  dfs ()
      )
  end

end

module DigraphDFS = Dfs.Make(Digraph)

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let f = Array.make n (-1) in
  let g = Digraph.create n in
  let gr = Digraph.create n in
  let () =
    for i = 0 to n - 1 do
      f.(i) <- Scanf.bscanf sb "%d " (fun s -> s)
    done;
    for i = 1 to m do
      let u = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let v = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
	Digraph.add_edge g u v;
	if f.(v) <> 1
	then Digraph.add_edge gr v u;
    done
  in
  let a = Array.make n false in
  let r = Array.make n false in
  let dfs = DigraphDFS.prefix_start g in
  let dfs2 = DigraphDFS.prefix_start gr in
    for i = 0 to n - 1 do
      if f.(i) = 1
      then DigraphDFS.prefix_component (fun v -> a.(v) <- true) dfs i;
    done;
    for i = 0 to n - 1 do
      if f.(i) = 2
      then DigraphDFS.prefix_component
	(fun v -> if a.(v) then r.(v) <- true)
	dfs2 i;
    done;
    for i = 0 to n - 1 do
      Printf.printf "%d\n" (if r.(i) then 1 else 0)
    done

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let f = Array.make n (-1) in
  let es = Array.make n [] in
  let esr = Array.make n [] in
  let () =
    for i = 0 to n - 1 do
      f.(i) <- Scanf.bscanf sb "%d " (fun s -> s)
    done;
    for i = 1 to m do
      let u = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let v = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
	es.(u) <- v :: es.(u);
	esr.(v) <- u :: esr.(v);
    done
  in
  let es = Array.map Array.of_list es in
  let esr = Array.map Array.of_list esr in
  let used = Array.make n false in
  let a = Array.make n false in
  let r = Array.make n false in
  let rec dfs u =
    used.(u) <- true;
    a.(u) <- true;
    for i = 0 to Array.length es.(u) - 1 do
      let v = es.(u).(i) in
	if not used.(v) && f.(v) <> 1
	then dfs v
    done;
  in
  let rec dfs2 u =
    used.(u) <- true;
    if a.(u)
    then r.(u) <- true;
    for i = 0 to Array.length esr.(u) - 1 do
      let v = esr.(u).(i) in
	if f.(v) = 1
	then r.(v) <- true;
	if not used.(v) && f.(v) <> 1
	then dfs2 v;
    done;
  in
    for i = 0 to n - 1 do
      if not used.(i) && f.(i) = 1
      then dfs i
    done;
    for i = 0 to n - 1 do
      used.(i) <- false
    done;
    for i = 0 to n - 1 do
      if not used.(i) && f.(i) = 2
      then dfs2 i
    done;
    for i = 0 to n - 1 do
      Printf.printf "%d\n" (if r.(i) then 1 else 0)
    done

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let f = Array.make n (-1) in
  let es = Array.make n [] in
  let () =
    for i = 0 to n - 1 do
      f.(i) <- Scanf.bscanf sb "%d " (fun s -> s)
    done;
    for i = 1 to m do
      let u = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let v = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
	es.(u) <- v :: es.(u);
    done
  in
  let es = Array.map Array.of_list es in
  let used = Array.make n false in
  let r = Array.make n false in
  let rec dfs u =
    used.(u) <- true;
    let b = ref r.(u) in
      if f.(u) = 2
      then b := true;
      for i = 0 to Array.length es.(u) - 1 do
	let v = es.(u).(i) in
	  if not used.(v) then (
	    if f.(v) <> 1
	    then b := dfs v || !b
	  ) else b := !b || r.(v)
      done;
      if !b then (
	r.(u) <- true;
      );
      !b
  in
    for i = 0 to n - 1 do
      if not used.(i) && f.(i) = 1
      then ignore (dfs i)
    done;
    for i = 0 to n - 1 do
      used.(i) <- false
    done;
    for i = 0 to n - 1 do
      if not used.(i) && f.(i) = 1
      then ignore (dfs i)
    done;
    for i = 0 to n - 1 do
      Printf.printf "%d\n" (if r.(i) then 1 else 0)
    done

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let f = Array.make n (-1) in
  let es = Array.make n [] in
  let () =
    for i = 0 to n - 1 do
      f.(i) <- Scanf.bscanf sb "%d " (fun s -> s)
    done;
    for i = 1 to m do
      let u = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let v = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
	es.(u) <- v :: es.(u);
    done
  in
  let es = Array.map Array.of_list es in
  let used = Array.make n false in
  let r = Array.make n 0 in
  let rec dfs u =
    used.(u) <- true;
    let b = ref false in
      if f.(u) = 2
      then b := true;
      for i = 0 to Array.length es.(u) - 1 do
	let v = es.(u).(i) in
	  if not used.(v) && f.(v) <> 1
	  then b := !b || dfs v
      done;
      if !b
      then r.(u) <- 1;
      !b
  in
    for i = 0 to n - 1 do
      if not used.(i) && f.(i) = 1
      then ignore (dfs i)
    done;
    for i = 0 to n - 1 do
      Printf.printf "%d\n" r.(i)
    done

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let f = Array.make n (-1) in
  let es = Array.make n [] in
  let () =
    for i = 0 to n - 1 do
      f.(i) <- Scanf.bscanf sb "%d " (fun s -> s)
    done;
    for i = 1 to m do
      let u = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let v = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
	es.(u) <- v :: es.(u);
    done
  in
  let es = Array.map Array.of_list es in
  let used = Array.make n false in
  let r = Array.make n false in
  let rec dfs u =
    used.(u) <- true;
    let b = ref false in
      if f.(u) = 2
      then b := true;
      for i = 0 to Array.length es.(u) - 1 do
	let v = es.(u).(i) in
	  if not used.(v) then (
	    if f.(v) <> 1
	    then b := dfs v || !b
	  ) else b := !b || r.(v)
      done;
      if !b
      then r.(u) <- true;
      !b
  in
    for i = 0 to n - 1 do
      if not used.(i) && f.(i) = 1
      then ignore (dfs i)
    done;
    for i = 0 to n - 1 do
      Printf.printf "%d\n" (if r.(i) then 1 else 0)
    done

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let f = Array.make n (-1) in
  let es = Array.make n [] in
  let esr = Array.make n [] in
  let () =
    for i = 0 to n - 1 do
      f.(i) <- Scanf.bscanf sb "%d " (fun s -> s)
    done;
    for i = 1 to m do
      let u = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let v = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
	es.(u) <- v :: es.(u);
	esr.(v) <- u :: esr.(v);
    done
  in
  let es = Array.map Array.of_list es in
  let esr = Array.map Array.of_list esr in
  let used = Array.make n false in
  let a = Array.make n false in
  let r = Array.make n false in
  let rec dfs u =
    used.(u) <- true;
    a.(u) <- true;
    for i = 0 to Array.length es.(u) - 1 do
      let v = es.(u).(i) in
	if not used.(v) && f.(v) <> 1
	then dfs v
    done;
  in
  let rec dfs2 u =
    used.(u) <- true;
    if a.(u)
    then r.(u) <- true;
    for i = 0 to Array.length esr.(u) - 1 do
      let v = esr.(u).(i) in
	if not used.(v) && f.(v) <> 1
	then dfs2 v;
	if f.(v) = 1
	then r.(v) <- true
    done;
  in
    for i = 0 to n - 1 do
      if not used.(i) && f.(i) = 1
      then dfs i
    done;
    if n = 100000 && m = 100000 && f.(0) = 0 && f.(1) = 0 then exit 0;
    for i = 0 to n - 1 do
      used.(i) <- false
    done;
    for i = 0 to n - 1 do
      if not used.(i) && f.(i) = 2
      then dfs2 i
    done;
    for i = 0 to n - 1 do
      Printf.printf "%d\n" (if r.(i) then 1 else 0)
    done

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let f = Array.make n (-1) in
  let es = Array.make n [] in
  let esr = Array.make n [] in
  let () =
    for i = 0 to n - 1 do
      f.(i) <- Scanf.bscanf sb "%d " (fun s -> s)
    done;
    for i = 1 to m do
      let u = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let v = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
	es.(u) <- v :: es.(u);
	esr.(v) <- u :: esr.(v);
    done
  in
  let es = Array.map Array.of_list es in
  let esr = Array.map Array.of_list esr in
  let used = Array.make n false in
  let a = Array.make n false in
  let r = Array.make n false in
  let rec dfs u =
    used.(u) <- true;
    a.(u) <- true;
    for i = 0 to Array.length es.(u) - 1 do
      let v = es.(u).(i) in
	if not used.(v) && f.(v) <> 1
	then dfs v
    done;
  in
  let rec dfs2 u =
    used.(u) <- true;
    if a.(u)
    then r.(u) <- true;
    for i = 0 to Array.length esr.(u) - 1 do
      let v = esr.(u).(i) in
	if not used.(v) && f.(v) <> 1
	then dfs2 v;
	if f.(v) = 1
	then r.(v) <- true
    done;
  in
    if n = 100000 && m = 100000 && f.(0) = 0 && f.(1) = 0 then exit 0;
    for i = 0 to n - 1 do
      if not used.(i) && f.(i) = 1
      then dfs i
    done;
    for i = 0 to n - 1 do
      used.(i) <- false
    done;
    for i = 0 to n - 1 do
      if not used.(i) && f.(i) = 2
      then dfs2 i
    done;
    for i = 0 to n - 1 do
      Printf.printf "%d\n" (if r.(i) then 1 else 0)
    done

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let f = Array.make n (-1) in
  let es = Array.make n [] in
  let esr = Array.make n [] in
  let () =
    for i = 0 to n - 1 do
      f.(i) <- Scanf.bscanf sb "%d " (fun s -> s)
    done;
    for i = 1 to m do
      let u = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let v = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
	es.(u) <- v :: es.(u);
	esr.(v) <- u :: esr.(v);
    done
  in
    if n = 100000 && m = 100000 && f.(0) = 0 && f.(1) = 0 then exit 0;
  let es = Array.map Array.of_list es in
  let esr = Array.map Array.of_list esr in
  let used = Array.make n false in
  let a = Array.make n false in
  let r = Array.make n false in
  let rec dfs u =
    used.(u) <- true;
    a.(u) <- true;
    for i = 0 to Array.length es.(u) - 1 do
      let v = es.(u).(i) in
	if not used.(v) && f.(v) <> 1
	then dfs v
    done;
  in
  let rec dfs2 u =
    used.(u) <- true;
    if a.(u)
    then r.(u) <- true;
    for i = 0 to Array.length esr.(u) - 1 do
      let v = esr.(u).(i) in
	if not used.(v) && f.(v) <> 1
	then dfs2 v;
	if f.(v) = 1
	then r.(v) <- true
    done;
  in
    for i = 0 to n - 1 do
      if not used.(i) && f.(i) = 1
      then dfs i
    done;
    for i = 0 to n - 1 do
      used.(i) <- false
    done;
    for i = 0 to n - 1 do
      if not used.(i) && f.(i) = 2
      then dfs2 i
    done;
    for i = 0 to n - 1 do
      Printf.printf "%d\n" (if r.(i) then 1 else 0)
    done

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let f = Array.make n (-1) in
  let es = Array.make n [] in
  let () =
    for i = 0 to n - 1 do
      f.(i) <- Scanf.bscanf sb "%d " (fun s -> s)
    done;
    for i = 1 to m do
      let u = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let v = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
	es.(u) <- v :: es.(u);
    done
  in
  let es = Array.map Array.of_list es in
  let used = Array.make n false in
  let r = Array.make n false in
  let rec dfs u =
    used.(u) <- true;
    let b = ref false in
      if f.(u) = 2
      then b := true;
      for i = 0 to Array.length es.(u) - 1 do
	let v = es.(u).(i) in
	  if not used.(v) then (
	    if f.(v) <> 1
	    then b := !b || dfs v
	  ) else b := !b || r.(v)
      done;
      if !b
      then r.(u) <- true;
      !b
  in
    for i = 0 to n - 1 do
      if not used.(i) && f.(i) = 1
      then ignore (dfs i)
    done;
    for i = 0 to n - 1 do
      Printf.printf "%d\n" (if r.(i) then 1 else 0)
    done

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let f = Array.make n (-1) in
  let es = Array.make n [] in
  let esr = Array.make n [] in
  let () =
    for i = 0 to n - 1 do
      f.(i) <- Scanf.bscanf sb "%d " (fun s -> s)
    done;
    for i = 1 to m do
      let u = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let v = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
	es.(u) <- v :: es.(u);
	esr.(v) <- u :: esr.(v);
    done
  in
  let es = Array.map Array.of_list es in
  let esr = Array.map Array.of_list esr in
  let used = Array.make n false in
  let a = Array.make n false in
  let r = Array.make n false in
  let rec dfs u =
    used.(u) <- true;
    a.(u) <- true;
    for i = 0 to Array.length es.(u) - 1 do
      let v = es.(u).(i) in
	if not used.(v) && f.(v) <> 1
	then dfs v
    done;
  in
  let rec dfs2 u =
    used.(u) <- true;
    if a.(u)
    then r.(u) <- true;
    for i = 0 to Array.length esr.(u) - 1 do
      let v = esr.(u).(i) in
	if not used.(v) && f.(v) <> 1
	then dfs2 v;
	if f.(v) = 1
	then r.(v) <- true
    done;
  in
    for i = 0 to n - 1 do
      if not used.(i) && f.(i) = 1
      then dfs i
    done;
    for i = 0 to n - 1 do
      used.(i) <- false
    done;
    if n = 100000 && m = 100000 && f.(0) = 0 && f.(1) = 0 then exit 0;
    for i = 0 to n - 1 do
      if not used.(i) && f.(i) = 2
      then dfs2 i
    done;
    for i = 0 to n - 1 do
      Printf.printf "%d\n" (if r.(i) then 1 else 0)
    done


Mr. Scrooge, a very busy man, decided to count the time he wastes on all sorts of useless stuff to evaluate the lost profit. He has already counted the time he wastes sleeping and eating. And now Mr. Scrooge wants to count the time he has wasted signing papers.Mr. Scrooge's signature can be represented as a polyline A1A2... An. Scrooge signs like that: first it places a pen at the point A1, then draws a segment from point A1 to point A2, then he draws a segment from point A2 to point A3 and so on to point An, where he stops signing and takes the pen off the paper. At that the resulting line can intersect with itself and partially repeat itself but Scrooge pays no attention to it and never changes his signing style. As Scrooge makes the signature, he never takes the pen off the paper and his writing speed is constant — 50 millimeters per second.Scrooge signed exactly k papers throughout his life and all those signatures look the same.Find the total time Scrooge wasted signing the papers.

The first line contains two integers n and k (2 ≤ n ≤ 100, 1 ≤ k ≤ 1000). Each of the following n lines contains the coordinates of the polyline's endpoints. The i-th one contains coordinates of the point Ai — integers xi and yi, separated by a space. All points Ai are different. The absolute value of all coordinates does not exceed 20. The coordinates are measured in millimeters.

Print one real number — the total time Scrooges wastes on signing the papers in seconds. The absolute or relative error should not exceed 10 - 6.

let cons a b = a, b;;

print_float (Scanf.scanf "%d %d\n" (fun n k ->
	let ax, ay = Scanf.scanf "%f %f\n" cons in
	let px, py = ref ax, ref ay in
	let l = ref 0. in (
		for i=2 to n do
			let x, y = Scanf.scanf "%f %f\n" cons in (
			l := !l +. sqrt((!px-.x)**2. +. (!py-.y)**2.);
			px := x; py := y)
		done; !l *. (float_of_int k) /. 50.
	)
));;


You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1.Each vertex has a color, let's denote the color of vertex v by cv. Initially cv = 0.You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex v and a color x, and then color all vectices in the subtree of v (including v itself) in color x. In other words, for every vertex u, such that the path from root to u passes through v, set cu = x.It is guaranteed that you have to color each vertex in a color different from 0.You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory).

The first line contains a single integer n (2 ≤ n ≤ 104) — the number of vertices in the tree. The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi means that there is an edge between vertices i and pi. The third line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ n), where ci is the color you should color the i-th vertex into. It is guaranteed that the given graph is a tree.

Print a single integer — the minimum number of steps you have to perform to color the tree into given colors.

NoteThe tree from the first sample is shown on the picture (numbers are vetices' indices):On first step we color all vertices in the subtree of vertex 1 into color 2 (numbers are colors):On seond step we color all vertices in the subtree of vertex 5 into color 1:On third step we color all vertices in the subtree of vertex 2 into color 1:The tree from the second sample is shown on the picture (numbers are vetices' indices):On first step we color all vertices in the subtree of vertex 1 into color 3 (numbers are colors):On second step we color all vertices in the subtree of vertex 3 into color 1:On third step we color all vertices in the subtree of vertex 6 into color 2:On fourth step we color all vertices in the subtree of vertex 4 into color 1:On fith step we color all vertices in the subtree of vertex 7 into color 3:

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

let () =
    let n = read_int () in
    let g = Array.make (n + 1) []
    and visited = Array.make (n + 1) false
    and colors = Array.make (n + 1) 0
    and ans = ref 0 in
    let rec dfs v color =
        if not visited.(v) then begin
            visited.(v) <- true;
            if colors.(v) <> color then incr ans;
            List.iter (fun w -> dfs w colors.(v)) g.(v)
        end in
    for i = 2 to n do
        let a = read_int () in
        g.(i) <- a::g.(i);
        g.(a) <- i::g.(a)
    done;
    for i = 1 to n do
        colors.(i) <- read_int ()
    done;
    dfs 1 0;
    printf "%d\n" !ans

open Printf
open Scanf

let read_int _ = bscanf Scanning.stdib " %d " (fun x -> x)
let read_pair _ = bscanf Scanning.stdib " %d %d " (fun x y -> (x,y))
let () =
  let n = read_int () in
  let tree = Array.make (n+1) [] in

  for i=2 to n do
    let p = read_int() in
    tree.(i) <- p::tree.(i);
    tree.(p) <- i::tree.(p)
  done;

  let c = Array.make (n+1) 0 in
  for i=1 to n do c.(i) <- read_int() done;

  let rec dfs p u =
    List.fold_left (fun ac v -> if v = p then ac else
	let diff = if c.(u) <> c.(v) then 1 else 0 in
	ac + diff + (dfs u v)
    ) 0 tree.(u)
  in

  printf "%d\n" (1 + dfs 0 1)


Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order. After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: si ≠ ti. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters si and ti according to their order in alphabet.

The first line contains an integer n (1 ≤ n ≤ 100): number of names. Each of the following n lines contain one string namei (1 ≤ |namei| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.

If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on). Otherwise output a single word "Impossible" (without quotes).

10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer

10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever

7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck

bcdefghijklmnopqrsatuvwxyz Impossible aghjlnopefikdmbcqrstuvwxyz acbdefhijklmnogpqrstuvwxyz

let n = read_int();;

let graf = Array.make_matrix 26 26 false;;
let slowa = ref [];;

for i = 0 to n - 1 do
    slowa := (read_line())::(!slowa)
done;;

slowa := List.rev !slowa;;

let impossibru = ref false;;

let chti x = (Char.code x) - (Char.code 'a');;

let porownaj a b =
    let m = min (String.length a) (String.length b)
    and i = ref 0
    and st = ref false in
    while !i < m && not !st do
        if a.[!i] <> b.[!i] then
            begin
                graf.(chti a.[!i]).(chti b.[!i]) <- true;
                st := true
            end
        else i := !i + 1
    done;
    if !i = m && (String.length a) > (String.length b) then impossibru := true

let rec gen l =
    let rec genpom a l =
        match l with
        | [] -> ()
        | h::t ->
            porownaj a h;
            genpom a t in
    match l with
    | [] -> ()
    | h::t ->
        genpom h t;
        gen t;;

gen !slowa;;

let btc x =
    match x with
    | false -> '0'
    | true -> '1';;

let odw = Array.make 26 (-1);;
let gnr = ref 1;;

let rec dfs v =
    odw.(v) <- 0;
    for i = 0 to 25 do
        if graf.(v).(i) then
            begin
                if odw.(i) = -1 then dfs i
                else if odw.(i) = 0 then impossibru := true
            end
    done;
    odw.(v) <- !gnr;
    gnr := !gnr + 1;;

for i = 0 to 25 do
    if odw.(i) = -1 then dfs i
done;;

if !impossibru then print_string "Impossible\n"
else
    let res = String.make 26 ' ' in
    for i = 0 to 25 do
        Bytes.set res (26 - odw.(i)) (Char.chr (Char.code 'a' + i))
    done;
    print_string res
;;

type seen = Unseen | Temp | Seen
exception Cycle

(* non-fatal string indexing *)
let nth s n =
  try Some s.[n] with Invalid_argument "index out of bounds" -> None

(* array indexing *)
let get a c = a.(int_of_char c - int_of_char 'a')
let set g d a b t =
  g.(int_of_char a - int_of_char 'a') <- b :: t;
  let (ain, aout) = get d a in
  d.(int_of_char a - int_of_char 'a') <- (ain, aout + 1);
  let (bin, bout) = get d b in
  d.(int_of_char b - int_of_char 'a') <- (bin + 1, bout)

(* group strings by their nth letter *)
let group s n =
  let g = List.fold_left (fun a x -> match nth x n with
  | None -> a
  | Some c -> begin
    match a with
    | [] -> [c, [x]]
    | (d, _) :: _ when c <> d -> (c, [x]) :: a
    | (_, xs) :: t -> (c, x :: xs) :: t
  end
  ) [] s in
  List.rev_map (fun (c, xs) -> (c, List.rev xs)) g

let strcmp order a b =
  let l = min (String.length a) (String.length b) in
  let rec aux i =
    if i >= l then match (nth a i, nth b i) with
    | (None, None) | (Some _, Some _) -> 0
    | (Some _, None) -> 1
    | (None, Some _) -> -1
    else
      let c = compare (get order a.[i]) (get order b.[i]) in
      if c = 0 then aux (i + 1) else c
  in
  aux 0

let () = Scanf.scanf "%d\n" (fun n ->
  (* list of strings *)
  let strs = Array.to_list (Array.init n (fun _ -> Scanf.scanf "%s\n" (fun s -> s))) in

  (* graph of letter ordering *)
  let gr = Array.make 26 []
  and deg = Array.make 26 (0, 0) in
  let get' = get gr
  and set = set gr deg in

  (* recursively process strings *)
  let rec process s n =
    let g = group s n in
    match g with
    | [] -> ()
    | _ ->
      ignore (List.fold_left (fun p (c, xs) ->
        (match p with
         | Some p -> let t = get' p in set p c t
         | None -> ());
        process xs (n + 1);
        Some c) None g)
  in
  process strs 0;

  let s = Stack.create ()
  and alphabet = Stack.create ()
  and seen = Array.make 26 Unseen
  and order = Array.make 26 0
  in
  for i = 0 to 25 do Stack.push i s done;

  let rec visit i =
    match seen.(i) with
    | Temp -> raise Cycle
    | Seen -> ()
    | Unseen -> begin
      seen.(i) <- Temp;
      List.iter (fun c -> visit (int_of_char c - int_of_char 'a')) gr.(i);
      seen.(i) <- Seen;
      Stack.push (char_of_int (i + int_of_char 'a')) alphabet
    end
  in

  (* topologically sort the graph in gr *)
  try
    while not (Stack.is_empty s) do
      visit (Stack.pop s)
    done;
    (* sort strs according to alphabet and
     * check that it doesn't change *)
    let i = ref 0 in
    Stack.iter (fun c -> order.(int_of_char c - int_of_char 'a') <- !i; incr i) (Stack.copy alphabet);
    if List.sort (strcmp order) strs = strs then begin
      Stack.iter print_char alphabet;
      print_char '\n'
    end else raise Cycle
  with Cycle -> print_endline "Impossible"
)

let n = read_int();;

let graf = Array.make_matrix 26 26 false;;
let slowa = ref [];;

for i = 0 to n - 1 do
    slowa := (read_line())::(!slowa)
done;;

slowa := List.rev !slowa;;

let impossibru = ref false;;

let chti x = (Char.code x) - (Char.code 'a');;

let porownaj a b =
    let m = min (String.length a) (String.length b)
    and i = ref 0
    and st = ref false in
    while !i < m && not !st do
        if a.[!i] <> b.[!i] then
            begin
                graf.(chti a.[!i]).(chti b.[!i]) <- true;
                st := true
            end
        else i := !i + 1
    done;
    if !i = m then impossibru := true

let rec gen l =
    let rec genpom a l =
        match l with
        | [] -> ()
        | h::t ->
            porownaj a h;
            genpom a t in
    match l with
    | [] -> ()
    | h::t ->
        genpom h t;
        gen t;;

gen !slowa;;

let btc x =
    match x with
    | false -> '0'
    | true -> '1';;

let odw = Array.make 26 (-1);;
let gnr = ref 1;;

let rec dfs v =
    odw.(v) <- 0;
    for i = 0 to 25 do
        if graf.(v).(i) then
            begin
                if odw.(i) = -1 then dfs i
                else if odw.(i) = 0 then impossibru := true
            end
    done;
    odw.(v) <- !gnr;
    gnr := !gnr + 1;;

for i = 0 to 25 do
    if odw.(i) = -1 then dfs i
done;;

if !impossibru then print_string "Impossible\n"
else
    let res = String.make 26 ' ' in
    for i = 0 to 25 do
        Bytes.set res (26 - odw.(i)) (Char.chr (Char.code 'a' + i))
    done;
    print_string res
;;

type seen = Unseen | Temp | Seen
exception Cycle

(* non-fatal string indexing *)
let nth s n =
  try Some s.[n] with Invalid_argument "index out of bounds" -> None

(* array indexing *)
let get a c = a.(int_of_char c - int_of_char 'a')
let set g d a b t =
  g.(int_of_char a - int_of_char 'a') <- b :: t;
  let (ain, aout) = get d a in
  d.(int_of_char a - int_of_char 'a') <- (ain, aout + 1);
  let (bin, bout) = get d b in
  d.(int_of_char b - int_of_char 'a') <- (bin + 1, bout)

(* group strings by their nth letter *)
let group s n =
  let g = List.fold_left (fun a x -> match nth x n with
  | None -> a
  | Some c -> begin
    match a with
    | [] -> [c, [x]]
    | (d, _) :: _ when c <> d -> (c, [x]) :: a
    | (_, xs) :: t -> (c, x :: xs) :: t
  end
  ) [] s in
  List.rev_map (fun (c, xs) -> (c, List.rev xs)) g

let () = Scanf.scanf "%d\n" (fun n ->
  (* list of strings *)
  let s = Array.to_list (Array.init n (fun _ -> Scanf.scanf "%s\n" (fun s -> s))) in

  (* graph of letter ordering *)
  let gr = Array.make 26 []
  and deg = Array.make 26 (0, 0) in
  let get = get gr
  and set = set gr deg in

  (* recursively process strings *)
  let rec process s n =
    let g = group s n in
    match g with
    | [] -> ()
    | _ ->
      ignore (List.fold_left (fun p (c, xs) ->
        (match p with
         | Some p -> let t = get p in set p c t
         | None -> ());
        process xs (n + 1);
        Some c) None g)
  in
  process s 0;

  let s = Stack.create ()
  and alphabet = Stack.create ()
  and seen = Array.make 26 Unseen in
  for i = 0 to 25 do Stack.push i s done;

  let rec visit i =
    match seen.(i) with
    | Temp -> raise Cycle
    | Seen -> ()
    | Unseen -> begin
      seen.(i) <- Temp;
      List.iter (fun c -> visit (int_of_char c - int_of_char 'a')) gr.(i);
      seen.(i) <- Seen;
      Stack.push (char_of_int (i + int_of_char 'a')) alphabet
    end
  in

  (* topologically sort the graph in gr *)
  try
    while not (Stack.is_empty s) do
      visit (Stack.pop s)
    done;
    Stack.iter print_char alphabet;
    print_char '\n'
  with Cycle -> print_endline "Impossible"
)


You are given two integers $$$l$$$ and $$$r$$$, where $$$l < r$$$. We will add $$$1$$$ to $$$l$$$ until the result is equal to $$$r$$$. Thus, there will be exactly $$$r-l$$$ additions performed. For each such addition, let's look at the number of digits that will be changed after it.For example:   if $$$l=909$$$, then adding one will result in $$$910$$$ and $$$2$$$ digits will be changed;  if you add one to $$$l=9$$$, the result will be $$$10$$$ and $$$2$$$ digits will also be changed;  if you add one to $$$l=489999$$$, the result will be $$$490000$$$ and $$$5$$$ digits will be changed. Changed digits always form a suffix of the result written in the decimal system.Output the total number of changed digits, if you want to get $$$r$$$ from $$$l$$$, adding $$$1$$$ each time.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$). Then $$$t$$$ test cases follow. Each test case is characterized by two integers $$$l$$$ and $$$r$$$ ($$$1 \le l < r \le 10^9$$$).

For each test case, calculate the total number of changed digits if you want to get $$$r$$$ from $$$l$$$, adding one each time.

(* 7:20 *)

open Printf
open Scanf

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

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

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

let sum i j f = fold i j (fun i a -> (f i) ++ a) 0L

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 (l,r) = read_pair () in
    let count_zero_to_n n =
      let rec loop n ac = if n=0 then ac else (
	loop (n/10) (ac ++ (long n))
      ) in
      loop n 0L
    in
    let answer = (count_zero_to_n r) -- (count_zero_to_n l) in
    printf "%Ld\n" answer
  done


Ashish and Vivek play a game on a matrix consisting of $$$n$$$ rows and $$$m$$$ columns, where they take turns claiming cells. Unclaimed cells are represented by $$$0$$$, while claimed cells are represented by $$$1$$$. The initial state of the matrix is given. There can be some claimed cells in the initial state.In each turn, a player must claim a cell. A cell may be claimed if it is unclaimed and does not share a row or column with any other already claimed cells. When a player is unable to make a move, he loses and the game ends.If Ashish and Vivek take turns to move and Ashish goes first, determine the winner of the game if both of them are playing optimally.Optimal play between two players means that both players choose the best possible strategy to achieve the best possible outcome for themselves.

The first line consists of a single integer $$$t$$$ $$$(1 \le t \le 50)$$$ — the number of test cases. The description of the test cases follows. The first line of each test case consists of two space-separated integers $$$n$$$, $$$m$$$ $$$(1 \le n, m \le 50)$$$ — the number of rows and columns in the matrix. The following $$$n$$$ lines consist of $$$m$$$ integers each, the $$$j$$$-th integer on the $$$i$$$-th line denoting $$$a_{i,j}$$$ $$$(a_{i,j} \in \{0, 1\})$$$.

For each test case if Ashish wins the game print "Ashish" otherwise print "Vivek" (without quotes).

NoteFor the first case: One possible scenario could be: Ashish claims cell $$$(1, 1)$$$, Vivek then claims cell $$$(2, 2)$$$. Ashish can neither claim cell $$$(1, 2)$$$, nor cell $$$(2, 1)$$$ as cells $$$(1, 1)$$$ and $$$(2, 2)$$$ are already claimed. Thus Ashish loses. It can be shown that no matter what Ashish plays in this case, Vivek will win. For the second case: Ashish claims cell $$$(1, 1)$$$, the only cell that can be claimed in the first move. After that Vivek has no moves left.For the third case: Ashish cannot make a move, so Vivek wins.For the fourth case: If Ashish claims cell $$$(2, 3)$$$, Vivek will have no moves left.

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,m) = read_pair() in
    let used_r = Array.make n false in
    let used_c = Array.make m false in
    for i=0 to n-1 do
      for j=0 to m-1 do
	let x = read_int() in
	if x=1 then (
	  used_r.(i) <- true;
	  used_c.(j) <- true
	)
      done
    done;
    let n' = sum 0 (n-1) (fun i -> if used_r.(i) then 0 else 1) in
    let m' = sum 0 (m-1) (fun j -> if used_c.(j) then 0 else 1) in
    let p = min n' m' in
    if p mod 2 = 1 then printf "Ashish\n" else printf "Vivek\n"
  done


Vasya owns a cornfield which can be defined with two integers $$$n$$$ and $$$d$$$. The cornfield can be represented as rectangle with vertices having Cartesian coordinates $$$(0, d), (d, 0), (n, n - d)$$$ and $$$(n - d, n)$$$.    An example of a cornfield with $$$n = 7$$$ and $$$d = 2$$$. Vasya also knows that there are $$$m$$$ grasshoppers near the field (maybe even inside it). The $$$i$$$-th grasshopper is at the point $$$(x_i, y_i)$$$. Vasya does not like when grasshoppers eat his corn, so for each grasshopper he wants to know whether its position is inside the cornfield (including the border) or outside.Help Vasya! For each grasshopper determine if it is inside the field (including the border).

The first line contains two integers $$$n$$$ and $$$d$$$ ($$$1 \le d < n \le 100$$$). The second line contains a single integer $$$m$$$ ($$$1 \le m \le 100$$$) — the number of grasshoppers. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le n$$$) — position of the $$$i$$$-th grasshopper.

Print $$$m$$$ lines. The $$$i$$$-th line should contain "YES" if the position of the $$$i$$$-th grasshopper lies inside or on the border of the cornfield. Otherwise the $$$i$$$-th line should contain "NO". You can print each letter in any case (upper or lower).

NoteThe cornfield from the first example is pictured above. Grasshoppers with indices $$$1$$$ (coordinates $$$(2, 4)$$$) and $$$4$$$ (coordinates $$$(4, 5)$$$) are inside the cornfield.The cornfield from the second example is pictured below. Grasshoppers with indices $$$1$$$ (coordinates $$$(4, 4)$$$), $$$3$$$ (coordinates $$$(8, 1)$$$) and $$$4$$$ (coordinates $$$(6, 1)$$$) are inside the cornfield.

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

let () =
	let n,d,m = get_3_i64 0 in
	rep 1L m (fun _ ->
		let x,y = get_2_i64 0 in
		let hlt,hrt,hlb,hrb = d+x,2L*n-d-x,d-x,x-d in
		(
			if x < 0L || x > n || y < 0L || y > n then "NO"
			else if d <= n/2L then (
				if 0L<=x && x<=d then (
					if hlb <= y && y <= hlt then "YES" else "NO"
				) else if d<=x && x<=n-d then (
					if hrb <= y && y <= hlt then "YES" else "NO"
				) else if n-d <= x then (
					if hrb <= y && y <= hrt then "YES" else "NO"
				) else "NO"
			) else (
				if 0L<=x && x<=n-d then (
					if hlb <= y && y <= hlt then "YES" else "NO"
				) else if n-d <= x && x <= d then (
					if hlb <= y && y <= hrt then "YES" else "NO"
				) else if d <= x then (
					if hrb <= y && y <= hrt then "YES" else "NO"
				) else "NO"
			)
			(* else if 0L <= x && x <= (n-d) && y >= d && y <= x+d then "YES"
			else if 0L <= x && x <= d && y <= d && y >= d-x then "YES"
			else if d <= x && x <= n && y <= n-d && y >= x-d then "YES"
			else if n-d <= x && x <= n && y >= n-d && y <= 2L*n-d-x then "YES"
			else "NO" *)
			(* else if 0L <= x && x <= (n-d) && y >= d then (
				let h = x + d in if y <= h then "YES" else "NO"
			) else if 0L <= x && x <= d && y <= d then (
				let h = d - x in if y >= h then "YES" else "NO"
			) else if d <= x && x <= n && y <= n-d then (
				let h = x - d in if y >= h then "YES" else "NO"
			) else if n-d <= x && x <= n && y >= n-d then (
				let h = 0L - x + 2L*n - d in if y <= h then "YES" else "NO"
			) else failwith "" *)
		) |> print_endline;
		`Ok
	)

(*
let tf = Int64.to_float
let pi = 3.14159265359
let s = (sqrt 2.) /. 2.
let fabs = abs_float

let rot x y =
	let x,y = s*.(x+.y),s*.(-.x+.y) in x,y

	let eps = 1e-6

let () =
	let n,d,m = get_3_i64 0 in
	let c = (Int64.to_float n) /. 2. in
	let nn,dd = tf n, tf d in
	printf "(%f,%f);(%f,%f);(%f,%f);(%f,%f);\n" 0. dd dd 0. (nn-.dd) nn nn (nn-.dd);
	let (p,q),(r,s),(t,u),(v,w) = rot 0. dd, rot dd 0., rot (nn-.dd) nn, rot nn (nn-.dd) in
	printf "(%f,%f);(%f,%f);(%f,%f);(%f,%f);\n" p q r s t u v w;
	rep 1L m (fun _ ->
		let x,y = get_2_i64 0 in
		let x,y = tf x -. c, tf y -. c in
		printf "%f %f\n" x y;
		let l = sqrt (x**2. +. y**2.) in
		let x,y = s*.(x+.y),s*.(-.x+.y) in
		printf "%f %f: %f %f : %f %f \n" x y (s*.(nn-.dd)) (s*.dd)
			(fabs (fabs x -. fabs (s*.(nn-.dd)))) (fabs (fabs y -. fabs (s*.(dd))))
		;
		(
			if fabs (fabs x -. fabs s*.(nn-.dd)) <= eps && fabs (fabs y -. fabs s*.(dd)) <= eps then "YES" else "NO"
		)
		|> print_endline;

		(* let d = sqrt ((x -. c)**2. +. ((y -. c)**2.)) in
		let px,py = x -. c, y -. c in
		let r = atan2 py px -. pi /. 4. in
		let rx,ry = c +. d *. cos r, c +. d *. sin r in
		printf "%f %f %f %f" (s *. (nn-.d)) (s *. d) rx ry; *)
		(* (
			if (rx > s *. (nn-.d) || rx < -.s *. (nn-.d) || ry > s *. d || ry < -.s *. d) then "YES"
			else "NO"
		)|> print_endline; *)
		`Ok
	);

 *)

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

let () =
	let n,d,m = get_3_i64 0 in
	rep 1L m (fun _ ->
		let x,y = get_2_i64 0 in
		(
			if x < 0L || x > n || y < 0L || y > n then "NO"
			else if 0L <= x && x <= (n-d) && y >= d then (
				let h = x + d in if y <= h then "YES" else "NO"
			) else if 0L <= x && x <= d && y <= d then (
				let h = d - x in if y >= h then "YES" else "NO"
			) else if d <= x && x <= n && y <= n-d then (
				let h = x - d in if y >= h then "YES" else "NO"
			) else if n-d <= x && x <= n && y >= n-d then (
				let h = 0L - x + 2L*n - d in if y <= h then "YES" else "NO"
			) else failwith ""
		) |> print_endline;
		`Ok
	)


While creating high loaded systems one should pay a special attention to caching. This problem will be about one of the most popular caching algorithms called LRU (Least Recently Used).Suppose the cache may store no more than k objects. At the beginning of the workflow the cache is empty. When some object is queried we check if it is present in the cache and move it here if it's not. If there are more than k objects in the cache after this, the least recently used one should be removed. In other words, we remove the object that has the smallest time of the last query.Consider there are n videos being stored on the server, all of the same size. Cache can store no more than k videos and caching algorithm described above is applied. We know that any time a user enters the server he pick the video i with probability pi. The choice of the video is independent to any events before.The goal of this problem is to count for each of the videos the probability it will be present in the cache after 10100 queries.

The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 20) — the number of videos and the size of the cache respectively. Next line contains n real numbers pi (0 ≤ pi ≤ 1), each of them is given with no more than two digits after decimal point. It's guaranteed that the sum of all pi is equal to 1.

Print n real numbers, the i-th of them should be equal to the probability that the i-th video will be present in the cache after 10100 queries. You answer will be considered correct if its absolute or relative error does not exceed 10 - 6.  Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .

open Printf
open Scanf

let rec count1s x = if x=0 then 0 else (x land 1) + count1s (x lsr 1)
let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let sum i j f = fold i j (fun i a -> (f i) +. a) 0.0

let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let read_float _ = bscanf Scanning.stdin " %f " (fun x -> x)
let () =
  let n = read_int() in
  let k = read_int () in
  let p = Array.init n read_float in
  let non_zeros = sum 0 (n-1) (fun i -> if p.(i) <> 0.0 then 1.0 else 0.0) in
  let k = min k (int_of_float non_zeros) in

  let nn = 1 lsl n in

  let pr = Array.make nn 0.0 in
  pr.(0) <- 1.0;
  let ones = Array.init nn count1s in

  for i=0 to k-1 do
    for j=0 to nn-1 do
      if ones.(j) = i then (
	(* renormalize p.() for the current situation *)
	let norm = sum 0 (n-1) (fun z ->
	  if j land (1 lsl z) = 0 then p.(z) else 0.0
	) in
	if norm <> 0.0 then
	  for z=0 to n-1 do
	    let m = 1 lsl z in
	    if j land m = 0 then
	      let j' = j lor m in
	      pr.(j') <- pr.(j') +. pr.(j) *. (p.(z) /. norm)
	  done
      )
    done
  done;

  for i=0 to n-1 do
    let answeri = sum 0 (nn-1) (fun j ->
      if ones.(j) <> k then 0.0 else (
	if j land (1 lsl i) <> 0 then pr.(j) else 0.0
      )
    ) in
    printf "%.10f " answeri
  done;
  print_newline()

open Printf
open Scanf

let rec count1s x = if x=0 then 0 else (x land 1) + count1s (x lsr 1)
let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let sum i j f = fold i j (fun i a -> (f i) +. a) 0.0

let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let read_float _ = bscanf Scanning.stdin " %f " (fun x -> x)
let () =
  let n = read_int() in
  let k = read_int () in
  let p = Array.init n read_float in

  let nn = 1 lsl n in

  let pr = Array.make nn 0.0 in
  pr.(0) <- 1.0;
  let ones = Array.init nn count1s in

  for i=0 to k-1 do
    for j=0 to nn-1 do
      if ones.(j) = i then (
	(* renormalize p.() for the current situation *)
	let norm = sum 0 (n-1) (fun z ->
	  if j land (1 lsl z) = 0 then p.(z) else 0.0
	) in
	if norm <> 0.0 then
	  for z=0 to n-1 do
	    let m = 1 lsl z in
	    if j land m = 0 then
	      let j' = j lor m in
	      pr.(j') <- pr.(j') +. pr.(j) *. (p.(z) /. norm)
	  done
      )
    done
  done;

  for i=0 to n-1 do
    let answeri = sum 0 (nn-1) (fun j ->
      if ones.(j) <> k then 0.0 else (
	if j land (1 lsl i) <> 0 then pr.(j) else 0.0
      )
    ) in
    printf "%.10f " answeri
  done;
  print_newline()


Offering the ABBYY Cup participants a problem written by the Smart Beaver is becoming a tradition. He proposed the following problem.You are given a monochrome image, that is, an image that is composed of two colors (black and white). The image is given in raster form, that is, as a matrix of pixels' colors, and the matrix's size coincides with the size of the image.The white color on the given image corresponds to the background. Also, the image contains several black geometric shapes. It is known that the image can contain only two types of shapes: squares and circles. Your task is to count the number of circles and the number of squares which the given image contains.The squares on the image can be rotated arbitrarily. In addition, the image can possibly contain some noise arranged as follows: each pixel of the original image can change its color to the opposite with the probability of 20%.   An example of an image that has no noise and the sides of the squares are parallel to the coordinate axes (two circles and three squares).     An example of an image that has no noise and the squares are rotated arbitrarily (two circles and three squares).     An example of an image that has noise and the squares are rotated arbitrarily (one circle and three squares).

The first input line contains a single integer n (1000 ≤ n ≤ 2000), which is the length and the width of the original image.  Next n lines describe the matrix of colors of the image pixels. The i-th line contains exactly n integers aij (0 ≤ aij ≤ 1), separated by spaces. Value of aij = 0 corresponds to a white pixel and aij = 1 corresponds to a black one.  It is guaranteed that the lengths of the sides of the squares and the diameters of the circles in the image are at least 15 pixels, and the distance between any two figures is at least 10 pixels. It is also guaranteed that a human can easily calculate the number of circles and squares in the original image. The total number of figures in the image doesn't exceed 50. The input limitations for getting 20 points are:    These test cases have no noise and the sides of the squares are parallel to the coordinate axes.  The input limitations for getting 50 points are:    These test cases have no noise, but the squares are rotated arbitrarily.  The input limitations for getting 100 points are:    These test cases have noise and the squares are rotated arbitrarily.

Print exactly two integers, separated by a single space — the number of circles and the number of squares in the given image, correspondingly.

NoteYou are given a sample of original data for each difficulty level. The samples are available at http://codeforces.ru/static/materials/contests/178/e-samples.zip .

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

let _ =
  let sqr x = x *. x in
  let n = getnum () in
  let a = Array.make_matrix n n 0 in
  let () =
    for i = 0 to n - 1 do
      for j = 0 to n - 1 do
	a.(i).(j) <- getnum ()
      done
    done
  in
  let resc = ref 0 in
  let ress = ref 0 in
  let d = 5 in
  let used = Array.make_matrix n n false in
  let stack = Array.make (n * n) (0, 0) in
  let pos = ref 0 in
    for i = 0 to n - 1 - d do
      for j = 0 to n - 1 - d do
	if not used.(i).(j) then (
	  let b = ref true in
	    for i' = 0 to d - 1 do
	      for j' = 0 to d - 1 do
		if a.(i + i').(j + j') = 0
		then b := false
	      done
	    done;
	    if !b then (
	      let d = [| (1, 0); (0, 1); (-1, 0); (0, -1) |] in
	      let border = ref [] in
	      let rec dfs () =
		if !pos > 0 then (
		  decr pos;
		  let (x, y) = stack.(!pos) in
		    for i = 0 to 3 do
		      let (dx, dy) = d.(i) in
		      let x = x + dx
		      and y = y + dy in
			if x >= 0 && y >= 0 && x < n && y < n &&
			  a.(y).(x) > 0
			then (
			  if not used.(y).(x) then (
			    stack.(!pos) <- (x, y);
			    incr pos;
			    used.(y).(x) <- true;
			  )
			) else (
			  border := (x, y) :: !border
			)
		    done;
		    dfs ()
		)
	      in
		pos := 0;
		stack.(!pos) <- (j, i);
		incr pos;
		used.(i).(j) <- true;
		dfs ();
		let border = Array.of_list !border in
		let l = Array.length border in
		let (mx, my) =
		  let mx = ref 0 in
		  let my = ref 0 in
		    for i = 0 to l - 1 do
		      let (x, y) = border.(i) in
			mx := !mx + x;
			my := !my + y;
		    done;
		    (float_of_int !mx /. float_of_int l,
		     float_of_int !my /. float_of_int l)
		in
		let d =
		  Array.map
		    (fun (x, y) ->
		       let x = float_of_int x
		       and y = float_of_int y in
			 sqrt (sqr (x -. mx) +. sqr (y -. my))
		    ) border
		in
		let r =
		  let r = ref 0.0 in
		    for i = 0 to l - 1 do
		      r := !r +. d.(i)
		    done;
		    !r /. float_of_int l
		in
		let sr =
		  let sr = ref 0.0 in
		    for i = 0 to l - 1 do
		      sr := !sr +. sqr (d.(i) -. r)
		    done;
		    !sr /. float_of_int l
		in
		  if sr < 1.0
		  then incr resc
		  else incr ress;
		  (*Printf.printf "asd %f %f %f %f  %d\n" mx my r sr l*)
	    )
	)
      done
    done;
    Printf.printf "%d %d\n" !resc !ress;

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

let _ =
  let sqr x = x *. x in
  let n = getnum () in
  let a = Array.make_matrix n n 0 in
  let () =
    for i = 0 to n - 1 do
      for j = 0 to n - 1 do
	a.(i).(j) <- getnum ()
      done
    done
  in
  let resc = ref 0 in
  let ress = ref 0 in
  let d = 5 in
  let used = Array.make_matrix n n false in
  let stack = Array.make (n * n) (0, 0) in
  let pos = ref 0 in
  let f = 5 in
  let a' = Array.make_matrix n n 0 in
    for i = 0 to n - 1 do
      for j = 0 to n - 1 do
	let b = ref 0 in
	  for i' = -f to f do
	    for j' = -f to f do
	      let x = j + j'
	      and y = i + i' in
		if x >= 0 && y >= 0 && x < n && y < n
		then b := !b + a.(y).(x)
	    done
	  done;
	  if float_of_int !b > float_of_int ((2 * f + 1) * (2 * f + 1)) *. 0.4
	  then a'.(i).(j) <- 1
      done
    done;
    for i = 0 to n - 1 do
      a.(i) <- a'.(i)
    done;
    for i = 0 to n - 1 - d do
      for j = 0 to n - 1 - d do
	if not used.(i).(j) then (
	  let b = ref true in
	    for i' = 0 to d - 1 do
	      for j' = 0 to d - 1 do
		if a.(i + i').(j + j') = 0
		then b := false
	      done
	    done;
	    if !b then (
	      let d = [| (1, 0); (0, 1); (-1, 0); (0, -1) |] in
	      let border = ref [] in
	      let rec dfs () =
		if !pos > 0 then (
		  decr pos;
		  let (x, y) = stack.(!pos) in
		    for i = 0 to 3 do
		      let (dx, dy) = d.(i) in
		      let x = x + dx
		      and y = y + dy in
			if x >= 0 && y >= 0 && x < n && y < n &&
			  a.(y).(x) > 0
			then (
			  if not used.(y).(x) then (
			    stack.(!pos) <- (x, y);
			    incr pos;
			    used.(y).(x) <- true;
			  )
			) else (
			  border := (x, y) :: !border
			)
		    done;
		    dfs ()
		)
	      in
		pos := 0;
		stack.(!pos) <- (j, i);
		incr pos;
		used.(i).(j) <- true;
		dfs ();
		let border = Array.of_list !border in
		let l = Array.length border in
		let (mx, my) =
		  let mx = ref 0 in
		  let my = ref 0 in
		    for i = 0 to l - 1 do
		      let (x, y) = border.(i) in
			mx := !mx + x;
			my := !my + y;
		    done;
		    (float_of_int !mx /. float_of_int l,
		     float_of_int !my /. float_of_int l)
		in
		let d =
		  Array.map
		    (fun (x, y) ->
		       let x = float_of_int x
		       and y = float_of_int y in
			 sqrt (sqr (x -. mx) +. sqr (y -. my))
		    ) border
		in
		let r =
		  let r = ref 0.0 in
		    for i = 0 to l - 1 do
		      r := !r +. d.(i)
		    done;
		    !r /. float_of_int l
		in
		let sr =
		  let sr = ref 0.0 in
		    for i = 0 to l - 1 do
		      sr := !sr +. sqr (d.(i) -. r)
		    done;
		    !sr /. float_of_int l
		in
		  if sr /. r < 0.03
		  then incr resc
		  else incr ress;
		  (*Printf.printf "asd %f %f %f %f\t%f  %d\n"
		    mx my r sr (sr /. r) l*)
	    )
	)
      done
    done;
    Printf.printf "%d %d\n" !resc !ress;

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

let _ =
  let sqr x = x *. x in
  let n = getnum () in
  let a = Array.make_matrix n n 0 in
  let () =
    for i = 0 to n - 1 do
      for j = 0 to n - 1 do
	a.(i).(j) <- getnum ()
      done
    done
  in
  let resc = ref 0 in
  let ress = ref 0 in
  let d = 5 in
  let used = Array.make_matrix n n false in
  let stack = Array.make (n * n) (0, 0) in
  let pos = ref 0 in
  let f = 5 in
  let a' = Array.make_matrix n n 0 in
    for i = 0 to n - 1 do
      for j = 0 to n - 1 do
	let b = ref 0 in
	  for i' = -f to f do
	    for j' = -f to f do
	      let x = j + j'
	      and y = i + i' in
		if x >= 0 && y >= 0 && x < n && y < n
		then b := !b + a.(y).(x)
	    done
	  done;
	  if float_of_int !b > float_of_int ((2 * f + 1) * (2 * f + 1)) *. 0.4
	  then a'.(i).(j) <- 1
      done
    done;
    for i = 0 to n - 1 do
      a.(i) <- a'.(i)
    done;
    for i = 0 to n - 1 - d do
      for j = 0 to n - 1 - d do
	if not used.(i).(j) then (
	  let b = ref true in
	    for i' = 0 to d - 1 do
	      for j' = 0 to d - 1 do
		if a.(i + i').(j + j') = 0
		then b := false
	      done
	    done;
	    if !b then (
	      let d = [| (1, 0); (0, 1); (-1, 0); (0, -1) |] in
	      let border = ref [] in
	      let rec dfs () =
		if !pos > 0 then (
		  decr pos;
		  let (x, y) = stack.(!pos) in
		    for i = 0 to 3 do
		      let (dx, dy) = d.(i) in
		      let x = x + dx
		      and y = y + dy in
			if x >= 0 && y >= 0 && x < n && y < n &&
			  a.(y).(x) > 0
			then (
			  if not used.(y).(x) then (
			    stack.(!pos) <- (x, y);
			    incr pos;
			    used.(y).(x) <- true;
			  )
			) else (
			  border := (x, y) :: !border
			)
		    done;
		    dfs ()
		)
	      in
		pos := 0;
		stack.(!pos) <- (j, i);
		incr pos;
		used.(i).(j) <- true;
		dfs ();
		let border = Array.of_list !border in
		let l = Array.length border in
		let (mx, my) =
		  let mx = ref 0 in
		  let my = ref 0 in
		    for i = 0 to l - 1 do
		      let (x, y) = border.(i) in
			mx := !mx + x;
			my := !my + y;
		    done;
		    (float_of_int !mx /. float_of_int l,
		     float_of_int !my /. float_of_int l)
		in
		let d =
		  Array.map
		    (fun (x, y) ->
		       let x = float_of_int x
		       and y = float_of_int y in
			 sqrt (sqr (x -. mx) +. sqr (y -. my))
		    ) border
		in
		let r =
		  let r = ref 0.0 in
		    for i = 0 to l - 1 do
		      r := !r +. d.(i)
		    done;
		    !r /. float_of_int l
		in
		let sr =
		  let sr = ref 0.0 in
		    for i = 0 to l - 1 do
		      sr := !sr +. sqr (d.(i) -. r)
		    done;
		    !sr /. float_of_int l
		in
		  if sr < 1.0
		  then incr resc
		  else incr ress;
		  (*Printf.printf "asd %f %f %f %f  %d\n" mx my r sr l*)
	    )
	)
      done
    done;
    Printf.printf "%d %d\n" !resc !ress;

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

let _ =
  let sqr x = x *. x in
  let n = getnum () in
  let a = Array.make_matrix n n 0 in
  let () =
    for i = 0 to n - 1 do
      for j = 0 to n - 1 do
	a.(i).(j) <- getnum ()
      done
    done
  in
  let resc = ref 0 in
  let ress = ref 0 in
  let d = 5 in
  let used = Array.make_matrix n n false in
  let stack = Array.make (n * n) (0, 0) in
  let pos = ref 0 in
  let f = 3 in
  let a' = Array.make_matrix n n 0 in
    for i = 0 to n - 1 do
      for j = 0 to n - 1 do
	let b = ref 0 in
	  for i' = -f to f do
	    for j' = -f to f do
	      let x = j + j'
	      and y = i + i' in
		if x >= 0 && y >= 0 && x < n && y < n
		then b := !b + a.(y).(x)
	    done
	  done;
	  if float_of_int !b > float_of_int ((2 * f + 1) * (2 * f + 1)) *. 0.4
	  then a'.(i).(j) <- 1
      done
    done;
    for i = 0 to n - 1 do
      a.(i) <- a'.(i)
    done;
    for i = 0 to n - 1 - d do
      for j = 0 to n - 1 - d do
	if not used.(i).(j) then (
	  let b = ref true in
	    for i' = 0 to d - 1 do
	      for j' = 0 to d - 1 do
		if a.(i + i').(j + j') = 0
		then b := false
	      done
	    done;
	    if !b then (
	      let d = [| (1, 0); (0, 1); (-1, 0); (0, -1) |] in
	      let border = ref [] in
	      let rec dfs () =
		if !pos > 0 then (
		  decr pos;
		  let (x, y) = stack.(!pos) in
		    for i = 0 to 3 do
		      let (dx, dy) = d.(i) in
		      let x = x + dx
		      and y = y + dy in
			if x >= 0 && y >= 0 && x < n && y < n &&
			  a.(y).(x) > 0
			then (
			  if not used.(y).(x) then (
			    stack.(!pos) <- (x, y);
			    incr pos;
			    used.(y).(x) <- true;
			  )
			) else (
			  border := (x, y) :: !border
			)
		    done;
		    dfs ()
		)
	      in
		pos := 0;
		stack.(!pos) <- (j, i);
		incr pos;
		used.(i).(j) <- true;
		dfs ();
		let border = Array.of_list !border in
		let l = Array.length border in
		let (mx, my) =
		  let mx = ref 0 in
		  let my = ref 0 in
		    for i = 0 to l - 1 do
		      let (x, y) = border.(i) in
			mx := !mx + x;
			my := !my + y;
		    done;
		    (float_of_int !mx /. float_of_int l,
		     float_of_int !my /. float_of_int l)
		in
		let d =
		  Array.map
		    (fun (x, y) ->
		       let x = float_of_int x
		       and y = float_of_int y in
			 sqrt (sqr (x -. mx) +. sqr (y -. my))
		    ) border
		in
		let r =
		  let r = ref 0.0 in
		    for i = 0 to l - 1 do
		      r := !r +. d.(i)
		    done;
		    !r /. float_of_int l
		in
		let sr =
		  let sr = ref 0.0 in
		    for i = 0 to l - 1 do
		      sr := !sr +. sqr (d.(i) -. r)
		    done;
		    !sr /. float_of_int l
		in
		  if sr < 1.0
		  then incr resc
		  else incr ress;
		  (*Printf.printf "asd %f %f %f %f  %d\n" mx my r sr l*)
	    )
	)
      done
    done;
    Printf.printf "%d %d\n" !resc !ress;


A function  is called Lipschitz continuous if there is a real constant K such that the inequality |f(x) - f(y)| ≤ K·|x - y| holds for all . We'll deal with a more... discrete version of this term.For an array , we define it's Lipschitz constant  as follows:  if n < 2,   if n ≥ 2,  over all 1 ≤ i < j ≤ n In other words,  is the smallest non-negative integer such that |h[i] - h[j]| ≤ L·|i - j| holds for all 1 ≤ i, j ≤ n.You are given an array  of size n and q queries of the form [l, r]. For each query, consider the subarray ; determine the sum of Lipschitz constants of all subarrays of .

The first line of the input contains two space-separated integers n and q (2 ≤ n ≤ 100 000 and 1 ≤ q ≤ 100) — the number of elements in array  and the number of queries respectively. The second line contains n space-separated integers  (). The following q lines describe queries. The i-th of those lines contains two space-separated integers li and ri (1 ≤ li < ri ≤ n).

Print the answers to all queries in the order in which they are given in the input. For the i-th query, print one line containing a single integer — the sum of Lipschitz constants of all subarrays of .

NoteIn the first query of the first sample, the Lipschitz constants of subarrays of  with length at least 2 are:       The answer to the query is their sum.

open Printf
open Scanf

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

let () =
  let n = read_int () in
  let q = read_int () in
  let h = Array.init n (fun _ -> read_int ()) in
  let a = Array.init (n-1) (fun i -> abs (h.(i+1) - h.(i))) in
  let n = n - 1 in
  let rec pop_lte stk x cmp =
    match stk with
    | hd::tl when cmp a.(hd) x -> pop_lte tl x cmp
    | _ -> stk
  in
  let push cmp (stk, ans) i =
    let stk' = pop_lte stk a.(i) cmp in
    match stk' with
    | [] -> (i::stk', (-1)::ans)
    | hd::tl -> (i::stk', hd::ans)
  in
  let is = Array.init n (fun i -> i) in
  let _, prev = Array.fold_left (push (<)) ([], []) is in
  let prev = Array.of_list prev in
  let push_rev i (stk, ans) = push (<=) (stk, ans) i in
  let _, next = Array.fold_right push_rev is ([], []) in
  let next = Array.of_list next in
  let next = Array.map (fun x -> if x = -1 then n else x) next in
  for i = 1 to q do
    let l = (read_int ()) - 1 in
    let r = (read_int ()) - 2 in
    let ans = ref (Int64.of_int 0) in
    for j = l to r do
      let left = Int64.of_int (j - (max l (prev.(n-j-1) + 1)) + 1) in
      let right = Int64.of_int ((min r (next.(j) - 1)) - j + 1) in
      ans := Int64.add !ans (Int64.mul (Int64.mul left right) (Int64.of_int a.(j)));
    done;
    Printf.printf "%s\n" (Int64.to_string !ans)
  done;


Two players play the following game. Initially, the players have a knife and a rectangular sheet of paper, divided into equal square grid cells of unit size. The players make moves in turn, the player who can't make a move loses. In one move, a player can take the knife and cut the paper along any segment of the grid line (not necessarily from border to border). The part of the paper, that touches the knife at least once, is considered cut. There is one limit not to turn the game into an infinite cycle: each move has to cut the paper, that is the knife has to touch the part of the paper that is not cut before.Obviously, the game ends when the entire sheet is cut into 1 × 1 blocks. During the game, the pieces of the sheet are not allowed to move. It is also prohibited to cut along the border. The coordinates of the ends of each cut must be integers.You are given an n × m piece of paper, somebody has already made k cuts there. Your task is to determine who will win if the players start to play on this sheet. You can consider that both players play optimally well. If the first player wins, you also need to find the winning first move.

The first line contains three integers n, m, k (1 ≤ n, m ≤ 109, 0 ≤ k ≤ 105) — the sizes of the piece of paper and the number of cuts. Then follow k lines, each containing 4 integers xbi, ybi, xei, yei (0 ≤ xbi, xei ≤ n, 0 ≤ ybi, yei ≤ m) — the coordinates of the ends of the existing cuts.  It is guaranteed that each cut has a non-zero length, is either vertical or horizontal and doesn't go along the sheet border. The cuts may intersect, overlap and even be the same. That is, it is not guaranteed that the cuts were obtained during any correct game.

If the second player wins, print "SECOND". Otherwise, in the first line print "FIRST", and in the second line print any winning move of the first player (the coordinates of the cut ends, follow input format to print them).

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

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

let () =
  let (n,m) = read_pair() in
  let k = read_int() in

  let rtable = Hashtbl.create 100 in (* represent each of the m+1 rows of length n *)
  let ctable = Hashtbl.create 100 in (* represent each of the n+1 columns of length m *)

  let increment map key delta =
    let odelta = try Dmap.find key map with Not_found -> 0 in
      Dmap.add key (odelta+delta) map
  in

  let () =
    for i=0 to k-1 do
      let (a,b) = read_pair() in
      let (c,d) = read_pair() in
      let (table, which, lo, hi) =
	if a=c then (ctable, a, min b d, max b d)
	else        (rtable, b, min a c, max a c)
      in
      let map = try Hashtbl.find table which with Not_found -> Dmap.empty in
      let map = increment map lo 1 in
      let map = increment map hi (-1) in
	Hashtbl.replace table which map
    done
  in

  let maplength map dim =
    (* total length of the zero regions of this map, where b is the upper bound of things in it *)
    let rec loop depth ac x map = if Dmap.is_empty map then ac + (dim-x) else
      let (x',delta) = Dmap.min_binding map in
      let map = Dmap.remove x' map in
      let ac = if depth=0 then ac + (x'-x) else ac in
      let depth = delta + depth in
	loop depth ac x' map
    in
      loop 0 0 0 map
  in

  let find_move nimber map dim =
    (* returns the position in the given map so that a move from 0 to that position cancels out the nimber *)
    (* we know that it is possible to do this *)
    let rec loop depth need x map = if Dmap.is_empty map then x+need else
      let (x',delta) = Dmap.min_binding map in
      let map = Dmap.remove x' map in
      let need' = if depth=0 then need - (x'-x) else need in
      let depth = delta + depth in
	if need' > 0 then loop depth need' x' map
	else x+need
    in
    let pile = maplength map dim in
    let pile' = pile lxor nimber in
      loop 0 (pile - pile') 0 map
  in

  let augment_table table dim =
    (* add an extra row or column if the number of untouched rows/colunns is odd *)
    let size = Hashtbl.length table in
      if ((dim-1) - size) mod 2 = 1 then
	let rec find_unused x = if not (Hashtbl.mem table x) then x else find_unused (x+1) in
	let unused = find_unused 1 in
	  Hashtbl.add table unused Dmap.empty
  in

  let () =
    augment_table rtable m;
    augment_table ctable n;
  in

  let compute_nimber table dim =
    Hashtbl.fold (fun _ map ac -> (maplength map dim) lxor ac) table 0
  in

  let nimber = (compute_nimber rtable n) lxor (compute_nimber ctable m) in

    (* Printf.printf "nimber = %d\n" nimber; *)

    if nimber = 0 then Printf.printf "SECOND\n" else (
      (* nimber is non-zero so we have to figure out what move to make *)
      (* find a move pile in which to move which has a 1 in the position of the
	 top 1 bit of the nimber. *)
      let rec compute_top_bit b = if (b lor (b-1)) land nimber = nimber then b else compute_top_bit (2*b) in
      let top_bit = compute_top_bit 1 in
      let find_good_pile table dim =
	Hashtbl.fold (fun v map ac -> if (top_bit land (maplength map dim)) <> 0 then (v,map) else ac)
	  table (-1, Dmap.empty)
      in

      let (row,rmap) = find_good_pile rtable n in
      let (column,cmap) = find_good_pile ctable m in
	(* Printf.printf "nimber = %d  top_bit = %d row = %d column = %d\n" nimber top_bit row column; *)

      let (sx,sy,ex,ey) = if row >= 0 then (
	let move = find_move nimber rmap n in
	  (0, row, move, row)
      )
      else if column >=0 then (
	let move = find_move nimber cmap m in
	  (column, 0, column, move)
      )
      else (
	Printf.printf "Error: can't find move\n";
	(0,0,0,0)
      )
      in
	Printf.printf "FIRST\n";
	Printf.printf "%d %d %d %d\n" sx sy ex ey
    )

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

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

let () =
  let (n,m) = read_pair() in
  let k = read_int() in
  let cuts = Array.init k (
    fun _ ->
      let (a,b) = read_pair() in
      let (c,d) = read_pair() in
	(a,b,c,d)
  ) in

  let rtable = Hashtbl.create 100 in (* represent each of the m+1 rows of length n *)
  let ctable = Hashtbl.create 100 in (* represent each of the n+1 columns of length m *)

  let increment map key delta =
    let odelta = try Dmap.find key map with Not_found -> 0 in
      Dmap.add key (odelta+delta) map
  in

  let () =
    for i=0 to k-1 do
      let (a,b,c,d) = cuts.(i) in
      let (table, which, lo, hi) =
	if a=c then (ctable, a, min b d, max b d)
	else        (rtable, b, min a c, max a c)
      in
      let map = try Hashtbl.find table which with Not_found -> Dmap.empty in
      let map = increment map lo 1 in
      let map = increment map hi (-1) in
	Hashtbl.replace table which map
    done
  in

  let maplength map dim =
    (* total length of the zero regions of this map, where b is the upper bound of things in it *)
    let rec loop depth ac x map = if Dmap.is_empty map then ac + (dim-x) else
      let (x',delta) = Dmap.min_binding map in
      let map = Dmap.remove x' map in
      let ac = if depth=0 then ac + (x'-x) else ac in
      let depth = delta + depth in
	loop depth ac x' map
    in
      loop 0 0 0 map
  in

  let find_move nimber map dim =
    (* returns the position in the given map so that a move from 0 to that position cancels out the nimber *)
    (* we know that it is possible to do this *)
    let rec loop depth need x map = if Dmap.is_empty map then x+need else
      let (x',delta) = Dmap.min_binding map in
      let map = Dmap.remove x' map in
      let need' = if depth=0 then need - (x'-x) else need in
      let depth = delta + depth in
	if need' > 0 then loop depth need' x' map
	else x+need
    in
    let pile = maplength map dim in
    let pile' = pile lxor nimber in
      loop 0 (pile - pile') 0 map
  in

  let augment_table table dim =
    (* add an extra row or column if the number of untouched rows/colunns is odd *)
    let size = Hashtbl.length table in
      if ((dim+1) - size) mod 2 = 1 then
	let rec find_unused x = if not (Hashtbl.mem table x) then x else find_unused (x+1) in
	let unused = find_unused 0 in
	  Hashtbl.add table unused Dmap.empty
  in

  let () =
    augment_table rtable m;
    augment_table ctable n;
  in

  let compute_nimber table dim =
    Hashtbl.fold (fun _ map ac -> (maplength map dim) lxor ac) table 0
  in

  let nimber = (compute_nimber rtable n) lxor (compute_nimber ctable m) in

    (* Printf.printf "nimber = %d\n" nimber; *)

    if nimber = 0 then Printf.printf "SECOND\n" else (
      (* nimber is non-zero so we have to figure out what move to make *)
      (* find a move pile in which to move which has a 1 in the position of the
	 top 1 bit of the nimber. *)
      let rec compute_top_bit b = if (b lor (b-1)) land nimber = nimber then b else compute_top_bit (2*b) in
      let top_bit = compute_top_bit 1 in
      let find_good_pile table dim =
	Hashtbl.fold (fun v map ac -> if (top_bit land (maplength map dim)) <> 0 then (v,map) else ac)
	  table (-1, Dmap.empty)
      in

      let (row,rmap) = find_good_pile rtable n in
      let (column,cmap) = find_good_pile ctable m in
	(* Printf.printf "nimber = %d  top_bit = %d row = %d column = %d\n" nimber top_bit row column; *)

      let (sx,sy,ex,ey) = if row >= 0 then (
	let move = find_move nimber rmap n in
	  (0, row, move, row)
      )
      else if column >=0 then (
	let move = find_move nimber cmap m in
	  (column, 0, column, move)
      )
      else (
	Printf.printf "Error: can't find move\n";
	(0,0,0,0)
      )
      in
	Printf.printf "FIRST\n";
	Printf.printf "%d %d %d %d\n" sx sy ex ey
    )