open Num

let ni = num_of_int

let solve l =
  let update (total_fall, left_fall, left_pos, left_high) (pos, high) =
    if left_fall
    then
      begin
        (* Printf.printf
           "true: pos - left_pos = %d, high = %d\n"
           (pos - left_pos)
           high; *)
        if ni pos -/ ni left_pos >/ ni high
        then (total_fall + 1, true, pos, high)
        else (total_fall, false, pos, high)
      end
    else
      begin
        (* Printf.printf
           "false: pos - left_pos = %d, high = %d, left_high = %d\n"
           (pos - left_pos)
           high
           left_high; *)

        if ni pos -/ ni left_pos >/ ni left_high +/ ni high
        then (total_fall + 2, true, pos, high)

        else if ni pos -/ ni left_pos >/ ni left_high
        then (total_fall + 1, false, pos, high)

        else if ni pos -/ ni left_pos >/ ni high
        then (total_fall + 1, true, pos, high)
        else (total_fall, false, pos, high)
      end
  in
  let (ans, last, _, _) = List.fold_left update (0, true, -1000000001, 0) l in
  (* Printf.printf "last: %b\n" last; *)
  if last then ans
  else ans + 1

let _ =
  let n = read_int () in

  let rec read_pairs acc = function
    | 0 -> List.rev acc
    | n ->
      let pair =
        begin
          match Str.split (Str.regexp " ") (read_line ()) with
          | x :: y :: [] -> int_of_string x, int_of_string y
          | _            -> failwith "mulformed input"
        end
      in
      read_pairs (pair :: acc) (n - 1)
  in

  let ans = solve (read_pairs [] n) in
  print_endline (string_of_int ans)

let rec f lim = function
	|(a,b)::(c,d)::q -> if a - b > lim
				then 1 + f a ((c,d)::q)
				else if c - a > b
					then 1+f (a+b) ((c,d)::q)
					else f a ((c,d)::q)
	| _		 -> 1

let _ =
	let n = Scanf.scanf "%d" (fun i -> i) and l = ref [] in
	for i = 1 to n do
		Scanf.scanf " %d %d" (fun i j -> l := (i,j)::!l);
	done;
	Printf.printf "%d" (f min_int (List.rev !l))

let solve l =
  let update (total_fall, left_fall, left_pos, left_high) (pos, high) =
    if left_fall
    then
      begin
        (* Printf.printf "true: pos - left_pos = %d, high = %d\n" (pos - left_pos) high; *)
        if pos - left_pos > high then (total_fall + 1, true, pos, high)
        else (total_fall, false, pos, high)
      end
    else
      begin
        (* Printf.printf "false: pos - left_pos = %d, high = %d, left_high = %d\n" (pos - left_pos) high left_high; *)
        if pos - left_pos > left_high + high then (total_fall + 2, true, pos, high)
        else if pos - left_pos > left_high then (total_fall + 1, false, pos, high)
        else if pos - left_pos > high then (total_fall + 1, true, pos, high)
        else (total_fall, false, pos, high)
      end
  in
  let (ans, last, _, _) = List.fold_left update (0, true, -1000000001, 0) l in
  (* Printf.printf "last: %b\n" last; *)
  if last then ans
  else ans + 1

let _ =
  let n = read_int () in

  let rec read_pairs acc = function
    | 0 -> List.rev acc
    | n ->
      let pair =
        begin
          match Str.split (Str.regexp " ") (read_line ()) with
          | x :: y :: [] -> int_of_string x, int_of_string y
          | _            -> failwith "mulformed input"
        end
      in
      read_pairs (pair :: acc) (n - 1)
  in

  let ans = solve (read_pairs [] n) in
  print_endline (string_of_int ans)


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

let rec read_ints n =
  match n with
  | 0 -> []
  | x -> read_int () :: (read_ints (n-1))

(* let read_ints () =
 *   let line = read_line () in
 *   let ints = Str.split (Str.regexp " *") line in
 *   List.map int_of_string ints *)

let rec print_list = function
    [] -> ()
  | e::l -> print_int e ; print_string " " ; print_list l

let solve () =
  let n = read_int () in
  let x = read_int () in
  let k = int_of_float
            (ceil ((float_of_int (x + n - 2)) /. (float_of_int x))) in
  print_int (if n <= 2 then 1 else k);
  print_endline ""
(**)

let () =
  let x = read_int () in
  let rec loop n =
    match n with
    | 0 -> ()
    | x -> solve (); loop (n-1) in
  loop x

let rec case t =
    if t > 0 then
        let solve n x =
            if n <= 2 then 1
            else let rec calc acc n x =
                if n <= x then acc + 1 else calc (acc + 1) (n - x) x
            in calc 1 (n - 2) x
        in Scanf.scanf "%i %i\n" (fun n x -> Printf.printf "%d\n" (solve n x); case (t - 1))
in Scanf.scanf "%d\n" (fun t -> case t)

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

let rec read_ints n =
  match n with
  | 0 -> []
  | x -> read_int () :: (read_ints (n-1))

(* let read_ints () =
 *   let line = read_line () in
 *   let ints = Str.split (Str.regexp " *") line in
 *   List.map int_of_string ints *)

let rec print_list = function
    [] -> ()
  | e::l -> print_int e ; print_string " " ; print_list l

let solve () =
  let n = float_of_int (read_int ()) in
  let x = float_of_int (read_int ()) in
  let k = int_of_float (ceil (1.0 +. (n -. 2.0) /. x)) in
  print_int k;
  print_endline ""
(**)

let () =
  let x = read_int () in
  let rec loop n =
    match n with
    | 0 -> ()
    | x -> solve (); loop (n-1) in
  loop x


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 arr = Array.init n
            (fun _ -> Array.make 2 false)
  in
  let sum = ref 0 in
  for i=0 to q-1 do
    let r = read_int() - 1 in
    let c = read_int() - 1 in
    let r3 = r lxor 1 in
    let count = [-1; 0; 1]
    |> List.map (fun (x: int) -> (r3, c + x))
    |> List.filter (fun (x: int * int) -> 0 <= (snd x) && (snd x) < n)
    |> List.filter (fun (x: int * int) -> arr.(snd x).(r3))
    |> List.length in
    if arr.(c).(r) then (
        arr.(c).(r) <- false;
        sum := !sum - count
    ) else (
        arr.(c).(r) <- true;
        sum := !sum + count
    );
    printf (if !sum > 0 then "No\n" else "Yes\n");
  done;

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 arr = Array.init n
            (fun _ -> Array.make 2 false)
  in
  let sum = ref 0 in
  for i=0 to q-1 do
    let r = read_int() in
    let c = read_int() in
    let r3 = r lxor 3 in
    let count = [-1; 0; 1]
    |> List.map (fun (x: int) -> (r3, c + x))
    |> List.filter (fun (x: int * int) -> 1 <= (snd x) && (snd x) <= n)
    |> List.filter (fun (x: int * int) -> arr.((snd x) - 1).(r3 - 1))
    |> List.length in
    if arr.(c - 1).(r - 1) then (
        arr.(c - 1).(r - 1) <- false;
        sum := !sum - count
    ) else (
        arr.(c - 1).(r - 1) <- true;
        sum := !sum + count
    );
    printf (if !sum > 0 then "No\n" else "Yes\n");
  done;


let _ = read_line ()
let nums_str = read_line ()

let rec split str len n acc nums =
  if n = len then nums @ [Int64.of_string acc]
  else if str.[n] = ' ' then split str len (n + 1) "" (nums @ [Int64.of_string acc])
  else split str len (n + 1) (acc ^ String.make 1 str.[n]) nums

let nums = split nums_str (String.length nums_str) 0 "" []

let rec add lst prev moves =
  match lst with
  | [] -> moves
  | h :: t ->
      let o = Int64.div (Int64.add prev h) h in
      add t
        (Int64.mul h o)
        (Int64.add moves o)

let rec sub lst prev moves =
  match lst with
  | [] -> moves
  | h :: t ->
      let o = Int64.abs (Int64.div (Int64.sub prev h) h) in
      sub t
        (Int64.mul (Int64.neg h) o)
        (Int64.add moves o)

let rec attempt lst rev ans =
  match lst with
  | [] -> ans
  | h :: t -> attempt t (h :: rev) (min ans (Int64.add (sub rev 0L 0L) (add t 0L 0L)))

let ans = attempt nums [] Int64.max_int;;

Printf.printf "%Ld\n" ans

let _ = read_line ()
let nums_str = read_line ()

let rec split str len n acc =
  if n = len then [ int_of_string acc ]
  else if str.[n] = ' ' then int_of_string acc :: split str len (n + 1) ""
  else split str len (n + 1) (acc ^ String.make 1 str.[n])

let nums = split nums_str (String.length nums_str) 0 ""

let rec add lst prev moves =
  match lst with
  | [] -> moves
  | h :: t -> add t (h * ((prev + h) / h)) (moves + ((prev + h) / h))

let rec sub lst prev moves =
  match lst with
  | [] -> moves
  | h :: t ->
      if abs (prev - h) < 0 then -1
      else sub t (-h * (abs (prev - h) / h)) (moves + (abs (prev - h) / h))

let rec attempt lst rev ans =
  match lst with
  | [] -> ans
  | h :: t -> attempt t (h :: rev) (min ans (sub rev 0 0 + add t 0 0))

let ans = attempt nums [] max_int;;

Printf.printf "%d\n" ans

let _ = read_line ()
let nums_str = read_line ()

let rec split str len n acc =
  if n = len then [ int_of_string acc ]
  else if str.[n] = ' ' then int_of_string acc :: split str len (n + 1) ""
  else split str len (n + 1) (acc ^ String.make 1 str.[n])

let nums = split nums_str (String.length nums_str) 0 ""

let rec add lst prev moves =
  match lst with
  | [] -> moves
  | h :: t -> add t (h * ((prev + h) / h)) (moves + ((prev + h) / h))

let rec sub lst prev moves =
  match lst with
  | [] -> moves
  | h :: t -> sub t (-h * (abs (prev - h) / h)) (moves + (abs (prev - h) / h))

let rec attempt lst rev i ans =
  match lst with
  | [] -> ans
  | h :: t -> attempt t (h :: rev) (i + 1) (min ans (sub rev 0 0 + add t 0 0))

let ans = attempt nums [] 0 max_int;;

Printf.printf "%d\n" ans

let _ = read_line ()
let nums_str = read_line ()

let rec split str len n acc =
  if n = len then [ int_of_string acc ]
  else if str.[n] = ' ' then int_of_string acc :: split str len (n + 1) ""
  else split str len (n + 1) (acc ^ String.make 1 str.[n])

let nums = split nums_str (String.length nums_str) 0 ""

let rec add lst prev moves =
  match lst with
  | [] -> moves
  | h :: t -> add t (h * (abs (prev + h) / h)) (moves + (abs (prev + h) / h))

let rec sub lst prev moves =
  match lst with
  | [] -> moves
  | h :: t -> sub t (-h * (abs (prev - h) / h)) (moves + (abs (prev - h) / h))

let rec attempt lst rev i ans =
  match lst with
  | [] -> ans
  | h :: t -> attempt t (h :: rev) (i + 1) (min ans (sub rev 0 0 + add t 0 0))

let ans = attempt nums [] 0 max_int;;

Printf.printf "%d\n" ans

let _ = read_line ()
let nums_str = read_line ()

let rec split str len n acc =
  if n = len then [ int_of_string acc ]
  else if str.[n] = ' ' then int_of_string acc :: split str len (n + 1) ""
  else split str len (n + 1) (acc ^ String.make 1 str.[n])

let nums = split nums_str (String.length nums_str) 0 ""

let rec get_rev lst i =
  match lst with
  | [] -> []
  | h :: t -> if i = 0 then [] else get_rev t (i - 1) @ [ h ]

let rec add lst prev moves =
  match lst with
  | [] -> moves
  | h :: t -> add t (h * abs ((prev + h) / h)) (moves + ((prev + h) / h))

let rec sub lst prev moves =
  match lst with
  | [] -> moves
  | h :: t -> sub t (-h * abs ((prev - h) / h)) (moves + (abs (prev - h) / h))

let rec attempt lst rev i ans =
  match lst with
  | [] -> ans
  | h :: t -> attempt t (h :: rev) (i + 1) (min ans (sub rev 0 0 + add t 0 0))

let ans = attempt nums [] 0 max_int;;

Printf.printf "%d\n" ans

let _ = read_line ()
let nums_str = read_line ()

let rec split str len n acc =
  if n = len then [ int_of_string acc ]
  else if str.[n] = ' ' then int_of_string acc :: split str len (n + 1) ""
  else split str len (n + 1) (acc ^ String.make 1 str.[n])

let nums = split nums_str (String.length nums_str) 0 ""

let rec loop lst =
  match lst with
  | [] -> print_newline ()
  | h :: t ->
      print_string (string_of_int h ^ " ");
      loop t

let () = loop nums

let rec get_rev lst i =
  match lst with
  | [] -> []
  | h :: t -> if i = 0 then [] else get_rev t (i - 1) @ [ h ]

let rec add lst prev moves =
  match lst with
  | [] -> moves
  | h :: t -> add t (h * abs ((prev + h) / h)) (moves + ((prev + h) / h))

let rec sub lst prev moves =
  match lst with
  | [] -> moves
  | h :: t -> sub t (-h * abs ((prev - h) / h)) (moves + (abs (prev - h) / h))

let rec attempt lst i len ans =
  match lst with
  | [] -> ans
  | h :: t ->
      attempt t (i + 1) len (min ans (sub (get_rev nums i) 0 0 + add t 0 0))

let ans = attempt nums 0 (List.length nums) max_int;;

Printf.printf "%d\n" ans


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

type node = {key: int; child: node; sibling: node}

let rec nil = {key = min_int; child = nil; sibling = nil}
let is_nil a = a.key = min_int

let merge a b =
  if a.key > b.key then
    {a with child = {b with sibling = a.child}}
  else
    {b with child = {a with sibling = b.child}}

let merge_list x =
  let rec go acc x =
    if is_nil x then
      acc
    else
      if is_nil x.sibling then
        x::acc
      else
        go (merge x x.sibling::acc) x.sibling.sibling
  in
  match go [] x with
  | [] -> nil
  | u::us -> List.fold_left (fun u v -> merge u v) u us

let pop x = merge_list x.child

let singleton k = {key = k; child = nil; sibling = nil}

let print x =
  let rec go d x =
    if not (is_nil x) then (
      go (d+1) x.sibling;
      for i = 1 to d do print_string "  " done;
      Printf.printf "%d\n" x.key;
      go (d+1) x.child;
    )
  in go 0 x

let () =
  let n = read_int 0 in
  let a = Array.init n read_int in
  let rec go st i =
    if i >= n then
      st
    else
      let rec f = function
        | (y,yi)::(x,xi)::xs when x.key > y.key ->
          let z = merge x y in
          let z = if (xi-(if xs=[] then -1 else (List.hd xs |> snd))) land 1 <> 0 &&
                (yi-xi) land 1 <> 0 then pop z else z in
          f ((z,yi)::xs)
        | st -> st
      in
      go ((singleton a.(i),i) :: st |> f) (i+1)
  in

  go [] 0
  |> List.rev
  |> List.fold_left (fun (ans,l) (x,xi) ->
    let rec go ans i =
      if i > xi then
        ans
      else
        let open Int64 in
        go (abs (sub (of_int a.(i)) (of_int x.key)) |> add ans) (i+1)
    in
    go ans (l+1), xi
  ) (0L,-1)
  |> fst |> Printf.printf "%Ld\n"

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

type node = {key: int; child: node; sibling: node}

let rec nil = {key = min_int; child = nil; sibling = nil}
let is_nil a = a.key = min_int

let merge a b =
  if a.key > b.key then
    {a with child = {b with sibling = a.child}}
  else
    {b with child = {a with sibling = b.child}}

let merge_list x =
  let rec go acc x =
    if is_nil x then
      acc
    else
      let y = {x with sibling = nil} in
      if is_nil x.sibling then
        y::acc
      else
        let z = {x.sibling with sibling = nil} in
        go (merge y z::acc) x.sibling.sibling
  in
  match go [] x with
  | [] -> nil
  | u::us -> List.fold_left (fun u v -> merge u v) u us

let pop x = merge_list x.child

let singleton k = {key = k; child = nil; sibling = nil}

let push x k = merge x {key = k; child = nil; sibling = nil}

let print x =
  let rec go d x =
    if not (is_nil x) then (
      go (d+1) x.sibling;
      for i = 1 to d do print_string "  " done;
      Printf.printf "%d\n" x.key;
      go (d+1) x.child;
    )
  in go 0 x

let () =
  let n = read_int 0 in
  let a = Array.init n read_int in
  let rec go st i =
    if i >= n then
      st
    else
      let rec f = function
        | (y,yi)::(x,xi)::xs when x.key > y.key ->
          let z = merge x y in
          let z = if (xi-(if xs=[] then -1 else (List.hd xs |> snd))) land 1 <> 0 &&
                (yi-xi) land 1 <> 0 then pop z else z in
          f ((z,yi)::xs)
        | st -> st
      in
      go ((singleton a.(i),i) :: st |> f) (i+1)
  in

  (*let st = go [] 0 in*)
  (*Printf.printf "+ len: %d\n" (List.length st);*)
  (*Printf.printf "+++++++++\n";*)
  (*List.nth st 0 |> snd |> Printf.printf "%d\n";*)
  (*List.nth st 0 |> fst |> print;*)
  (*Printf.printf "+++++++++\n";*)
  (*List.nth st 1 |> snd |> Printf.printf "%d\n";*)
  (*List.nth st 1 |> fst |> print;*)
  go [] 0
  |> List.rev
  |> List.fold_left (fun (ans,l) (x,xi) ->
    let rec go ans i =
      if i > xi then
        ans
      else
        let open Int64 in
        go (abs (sub (of_int a.(i)) (of_int x.key)) |> add ans) (i+1)
    in
    go ans (l+1), xi
  ) (0L,-1)
  |> fst |> Printf.printf "%Ld\n"

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

let lbound a n x =
  let rec go l h =
    if l >= h then
      l
    else
      let m = (l+h)/2 in
      if a.(m) < x then
        go (m+1) h
      else
        go l m
  in go 0 n

let () =
  let n = read_int 0 in
  let a = Array.init n read_int in
  let b = Array.copy a in
  Array.sort compare b;
  for i = 0 to n-1 do
    a.(i) <- lbound b n a.(i)
  done;

  let s = Array.make_matrix 2 n 0L in
  for i = 0 to n-1 do
    s.(0).(i) <- if a.(0) <= i then 0L else b.(a.(0))-b.(i) |> Int64.of_int
  done;
  for i = 1 to n-1 do
    let cur = i land 1 in
    let pre = 1-cur in
    for j = 0 to n-1 do
      s.(cur).(j) <- Int64.add s.(pre).(j) (b.(a.(i))-b.(j) |> abs |> Int64.of_int)
    done;
    for j = 1 to n-1 do
      s.(cur).(j) <- min s.(cur).(j) s.(cur).(j-1)
    done
  done;
  Printf.printf "%Ld\n" s.((n-1) land 1).(n-1)

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

let lbound a n x =
  let rec go l h =
    if l >= h then
      l
    else
      let m = (l+h)/2 in
      if a.(m) < x then
        go (m+1) h
      else
        go l m
  in go 0 n

let () =
  let n = read_int 0 in
  let a = Array.init n read_int in
  let b = Array.copy a in
  Array.sort compare b;
  for i = 0 to n-1 do
    a.(i) <- lbound b n a.(i)
  done;

  let s = Array.make_matrix 2 n 0 in
  for i = 0 to n-1 do
    s.(0).(i) <- if a.(0) <= i then 0 else b.(a.(0))-b.(i)
  done;
  for i = 1 to n-1 do
    let cur = i land 1 in
    let pre = 1-cur in
    for j = 0 to n-1 do
      s.(cur).(j) <- s.(pre).(j) + abs (b.(a.(i))-b.(j))
    done;
    for j = 1 to n-1 do
      s.(cur).(j) <- min s.(cur).(j) s.(cur).(j-1)
    done
  done;
  Printf.printf "%d\n" s.((n-1) land 1).(n-1)

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

type node = {key: int; child: node; sibling: node}

let rec nil = {key = min_int; child = nil; sibling = nil}
let is_nil a = a.key = min_int

let merge a b =
  if a.key > b.key then
    {a with child = {b with sibling = a.child}}
  else
    {b with child = {a with sibling = b.child}}

let merge_list x =
  let rec go acc x =
    if is_nil x then
      acc
    else
      let y = {x with sibling = nil} in
      if is_nil x.sibling then
        y::acc
      else
        let z = {x.sibling with sibling = nil} in
        go (merge y z::acc) x.sibling.sibling
  in
  match go [] x with
  | [] -> nil
  | u::us -> List.fold_left (fun u v -> merge u v) u us

let pop x = merge_list x.child

let singleton k = {key = k; child = nil; sibling = nil}

let push x k = merge x {key = k; child = nil; sibling = nil}

let print x =
  let rec go d x =
    if not (is_nil x) then (
      go (d+1) x.sibling;
      for i = 1 to d do print_string "  " done;
      Printf.printf "%d\n" x.key;
      go (d+1) x.child;
    )
  in go 0 x

let () =
  let n = read_int 0 in
  let a = Array.init n read_int in
  let rec go st i =
    if i >= n then
      st
    else
      let rec f = function
        | (y,yi)::(x,xi)::xs when x.key > y.key ->
          let z = merge x y in
          let z = if (xi-(if xs=[] then -1 else (List.hd xs |> snd))) land 1 <> 0 &&
                (yi-xi) land 1 <> 0 then pop z else z in
          f ((z,yi)::xs)
        | st -> st
      in
      let st' = f st in
      go ((singleton a.(i),i) :: st') (i+1)
  in

  (*Printf.printf "+ len: %d\n" (List.length st);*)
  (*Printf.printf "+++++++++\n";*)
  (*List.nth st 0 |> snd |> Printf.printf "%d\n";*)
  (*Printf.printf "+++++++++\n";*)
  (*List.nth st 1 |> snd |> Printf.printf "%d\n";*)
  (*Printf.printf "+++++++++\n";*)
  (*List.nth st 2 |> snd |> Printf.printf "%d\n";*)
  go [] 0
  |> List.rev
  |> List.fold_left (fun (ans,l) (x,xi) ->
    let rec go ans i =
      if i > xi then
        ans
      else
        let open Int64 in
        go (abs (sub (of_int a.(i)) (of_int x.key)) |> add ans) (i+1)
    in
    go ans (l+1), xi
  ) (0L,-1)
  |> fst |> Printf.printf "%Ld\n"


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

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 es = Array.make n [] in
  let () =
    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, 1) :: es.(u);
	es.(v) <- (u, -1) :: es.(v);
    done;
  in
  let es = Array.map Array.of_list es in
  let used = Array.make n false in
  let a = Array.make n (-1) in
  let res = ref 0 in
  let pos = ref 0 in
  let stack = Array.make n 0 in
  let rec dfs u =
    used.(u) <- true;
    for i = 0 to Array.length es.(u) - 1 do
      let (v, d) = es.(u).(i) in
	if not used.(v) then (
	  a.(v) <- a.(u) + d;
	  dfs v
	) else (
	  res := gcd !res (abs ((a.(u) + d) - a.(v)))
	)
    done;
  in
  let rec dfs () =
    if !pos > 0 then (
      decr pos;
      let u = stack.(!pos) in
	for i = 0 to Array.length es.(u) - 1 do
	  let (v, d) = es.(u).(i) in
	     if not used.(v) then (
	       a.(v) <- a.(u) + d;
	       stack.(!pos) <- v;
	       incr pos;
	       used.(v) <- true;
	     ) else (
	       res := gcd !res (abs ((a.(u) + d) - a.(v)))
	     )
	done;
	dfs ()
    )
  in
    for i = 0 to n - 1 do
      if not used.(i) then (
	a.(i) <- 0;
	pos := 0;
	stack.(!pos) <- i;
	incr pos;
	used.(i) <- true;
	dfs ()
      )
    done;
    if !res = 0
    then res := n;
    Printf.printf "%d\n" !res

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

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 es = Array.make n [] in
  let () =
    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, 1) :: es.(u);
	es.(v) <- (u, -1) :: es.(v);
    done;
  in
  let es = Array.map Array.of_list es in
  let used = Array.make n false in
  let a = Array.make n (-1) in
  let res = ref 0 in
  let rec dfs u =
    used.(u) <- true;
    for i = 0 to Array.length es.(u) - 1 do
      let (v, d) = es.(u).(i) in
	if not used.(v) then (
	  a.(v) <- a.(u) + d;
	  dfs v
	) else (
	  res := gcd !res (abs ((a.(u) + d) - a.(v)))
	)
    done;
  in
    for i = 0 to n - 1 do
      if not used.(i) then (
	a.(i) <- 0;
	dfs i
      )
    done;
    if !res = 0
    then res := n;
    Printf.printf "%d\n" !res


let n, m = Scanf.scanf " %d %d" (fun x y -> x, y);;
let node = Scanf.scanf " %s" (fun x -> x);;
let adjs = Array.make (n + 1) [];;
let etat = Array.make (n + 1) 0;;
let maxi = Array.make_matrix (n + 1) 26 0;;

for i = 1 to m do
  let u, v = Scanf.scanf " %d %d" (fun x y -> x, y) in
  adjs.(u) <- v :: adjs.(u);
done;;

let rec cycle u =
  match etat.(u) with
  | 1 -> true
  | 2 -> false
  | _ ->
      etat.(u) <- 1;
      let x = List.fold_left (fun x v -> x || cycle v) false adjs.(u) in
      etat.(u) <- 2;
      x;;

let rec calculer u =
  let aux v =
    calculer v;
    let f i x =
      maxi.(u).(i) <- max maxi.(u).(i) x in
    Array.iteri f maxi.(v) in
  if etat.(u) = 0 then (
    etat.(u) <- 1;
    List.iter aux adjs.(u);
    let i = int_of_char node.[u - 1] - int_of_char 'a' in
    maxi.(u).(i) <- maxi.(u).(i) + 1
  );;

let cyclique = ref false in
for u = 1 to n do
  cyclique := !cyclique || cycle u;
done;
if !cyclique then
  print_int (-1)
else (
  Array.fill etat 0 (Array.length etat) 0;
  let r = ref 0 in
  for u = 1 to n do
    calculer u;
    r := max !r (Array.fold_left max 0 maxi.(u));
  done;
  print_int !r
);;

let n, m = Scanf.scanf " %d %d" (fun x y -> x, y);;
let label = Scanf.scanf " %s" (fun x -> x);;
let adjs = Array.make (n + 1) [];;
let etat = Array.make (n + 1) 0;;
let maxi = Array.make_matrix (n + 1) 26 0;;

for i = 1 to m do
  let u, v = Scanf.scanf " %d %d" (fun x y -> x, y) in
  adjs.(u) <- v :: adjs.(u);
done;;

let rec cycle u =
  match etat.(u) with
  | 1 -> true
  | 2 -> false
  | _ ->
      etat.(u) <- 1;
      let x = List.fold_left (fun x v -> x || cycle v) false adjs.(u) in
      etat.(u) <- 2;
      x;;

let rec calculer u =
  let aux v =
    calculer v;
    let f i x =
      maxi.(u).(i) <- max maxi.(u).(i) x in
    Array.iteri f maxi.(v) in
  if etat.(u) = 0 then (
    etat.(u) <- 1;
    List.iter aux adjs.(u);
    let i = int_of_char label.[u - 1] - int_of_char 'a' in
    maxi.(u).(i) <- maxi.(u).(i) + 1
  );;

let cyclique = ref false in
for u = 1 to n do
  cyclique := !cyclique || cycle u;
done;
if !cyclique then
  print_int (-1)
else (
  Array.fill etat 0 (Array.length etat) 0;
  let r = ref 0 in
  for u = 1 to n do
    calculer u;
    r := max !r (Array.fold_left max 0 maxi.(u));
  done;
  print_int !r
);;


let f tab =
	let res = ref 0 in
	for i=0 to Array.length tab - 2 do
		if tab.(i) > 1
			then begin
				let trop = tab.(i) - 1 in
					tab.(i) <- 1;
					tab.(i+1) <- tab.(i+1) + trop;
					res := trop + !res;
			     end

	done;
	!res

let _ =
	let tab = Array.make 6015 0 and n = Scanf.scanf "%d" (fun i -> i) in
	for i=1 to n do
		Scanf.scanf " %d" (fun j -> tab.(j) <- tab.(j) + 1);
	done;
	Printf.printf "%d" (f tab)

let (|>) x f = f x
let ($) f g = fun x -> f (g x)

let solve a =
  let arr = Array.make 10000 0 in
  List.iter
    (fun x -> arr.(x) <- arr.(x) + 1)
    a;
  let res = ref 0 in
  Array.iteri
    (fun i x ->
       if x > 1 then
         (res := !res + x - 1;
          arr.(i + 1) <- arr.(i + 1) + x - 1))
    arr;
  !res

let _ =
  ignore (read_int ());
  let a =
    read_line ()
    |> Str.split (Str.regexp " ")
    |> List.map int_of_string
  in
  print_endline (string_of_int (solve a))


(* For the first archer to win, the possibilities are
 * win(a) - lose(a) & lose(b) & win(a)...*)

let main =
  let (a, b, c, d) = Scanf.scanf "%f %f %f %f\n" (fun a b c d -> (a,b,c,d)) in
  let p_win = a /. b in
  let p_retry = (1. -. p_win) *. (1. -. (c /. d)) in
  let result = p_win /. (1. -. p_retry) in
    Printf.printf "%f\n" result

(* Codeforces contest 300D - Draw Squares - Too slow *)

open Filename;;

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

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

let a = gr();;  (* number of test data *)
let b = gr();;  (* number of test data *)
let c = gr();;  (* number of test data *)
let d = gr();;  (* number of test data *)

let main() =
    let af = float_of_int a in
    let bf = float_of_int b in
    let cf = float_of_int c in
    let df = float_of_int d in

	let adf = af *. df in
	let acaf = (af *. df) +. (cf *. bf) -. (af *. cf) in
	let pp = adf /. acaf in
	Printf.printf "%18.12f" pp;;

main();;


open Str;;

let rec check (a : int list)(n : int) =
  if List.length (List.find_all (fun x -> x == (List.nth a n)) a) == 1 then
    n
  else
    check a (n+1)
;;

let rec solve (t : int) =
  if t = 0 then
    ()
  else
    let n = read_int () in
    let arr = (Str.split (Str.regexp " ") (read_line ())) in
    let a = (List.map (fun x -> int_of_string x) arr) in
    let ind = check a 0 in
    print_endline (string_of_int (ind + 1));
    solve (t-1)
;;

let t = read_int() in
solve t;;

(* https://codeforces.com/contest/1512/problem/0 *)

let intruder arr =
    let rec commoner () =
        let h = Array.init 3 (fun i -> arr.(i)) in
        let count =
            Array.map
                (fun e -> Array.fold_left (fun acc e' -> if e = e' then acc+1 else acc) 0 h)
                h in
        if count.(0) > 1
        then h.(0)
        else h.(1) in
    let c = commoner () in
    let rec helper i =
        if arr.(i) <> c
        then (i+1)
        else helper (i+1) in
    helper 0

let solve_n nb_cases =
    let rec helper i =
        if i > 0
        then (
            let _ = read_int () and
            arr =
                read_line ()
                |> Str.split (Str.regexp " ")
                |> List.map int_of_string
                |> Array.of_list in
            intruder arr
            |> Printf.printf "%d\n";
            helper (i-1)
        ) in
    helper nb_cases

let () =
    let nb_cases = read_int () in
    solve_n nb_cases


open Printf
open Scanf

let mm = 1000000007L

let ( %% ) a b = Int64.rem a b
let ( ** ) a b = (Int64.mul a b) %% mm
let ( ++ ) a b = (Int64.add a b) %% mm
let ( -- ) a b = ((Int64.sub a b) ++ mm) %% mm
let long x = Int64.of_int x

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

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

  let input = Array.init t (fun _ -> read_pair()) in

  let msize = maxf 0 (t-1) (fun i -> snd input.(i)) in

  let count = Array.make (msize+1) (-1L) in
  count.(0) <- 1L;

  let rec ccount b =
    if count.(b) >= 0L then count.(b) else
      let answer =
	if b < k then 1L else
	  ccount (b-k) ++ ccount (b-1)
      in
      count.(b) <- answer;
      answer
  in

  for b = 1 to msize do
    ignore (ccount b)
  done;

  let accum = Array.make (msize+1) 0L in
  accum.(0) <- count.(0);

  for i=1 to msize do
    accum.(i) <- accum.(i-1) ++ count.(i)
  done;

  for i=0 to t-1 do
    let (a,b) = input.(i) in
    printf "%Ld\n" (accum.(b) -- (accum.(a-1)))
  done

open Scanf;;
open String;;
open Printf;;
open Int64;;

(* This version implements
 * a(n) = a(n-1) + a(n-k)
 * where k is the number of continued W
 * - Reduce memory usage.
 *)

let (t,k) = bscanf Scanning.stdin "%d %d " (fun x y -> (x,y) )

let modn i = rem i (of_int 1000000007)

let ni i = of_int i
let sn i = to_string i

(*let nn = 100000 + 5*)
let nn_max = 100000 + 5

let nn = 100000 + 3

let r= Array.make nn_max (ni 0)

for i = 1 to k-1 do
  r.(i) <- (ni 1)
done

let () =
 r.(k) <- ni 2

for i = k+1 to nn do
  r.(i) <-  modn (add r.(i-1) r.(i-k))
done

for i = 2 to nn do
    r.(i) <- add r.(i) r.(i-1)
done

for i = 1 to t do
   let (s,e) = bscanf Scanning.stdin "%d %d " (fun x y -> (x,y) ) in
   let sum = ref (ni 0) in
   sum := sub r.(e) r.(s-1);
   printf "%s\n" (sn (modn !sum) )
done

open Int64;;

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

let modulo = of_int 1000000007;;

let dyn_array = Array.make 100005 (of_int (-1));;

let rec get_ways gap = function
  | index when index = Array.length dyn_array -> one
  | index when index > Array.length dyn_array -> zero
  | index when (compare dyn_array.(index)  (of_int (-1))) != 0 -> dyn_array.(index)
  | index ->
    let res = rem (add (get_ways gap (index+1)) (get_ways gap (index+gap))) modulo in
    dyn_array.(index) <- res;
    res;;

(*#trace get_ways;;*)

let nbTests = read_int () in
let gap = read_int () in
let _ = get_ways gap 0 in
for i = 1 to Array.length dyn_array - 1 do
  dyn_array.(i) <- rem (add dyn_array.(i)  dyn_array.(i - 1)) modulo
done;
for i = 1 to nbTests do
  let start = read_int () in
  let tree_end = Array.length dyn_array - start in
  let fin = read_int () in
  let tree_start = Array.length dyn_array - fin in
  let res = rem (add modulo  (sub dyn_array.(tree_end)  (if tree_start > 0 then dyn_array.(tree_start - 1) else zero))) modulo in
  Printf.printf "%s\n" (to_string res)
done;;

open Scanf;;
open String;;
open Printf;;
open Num;;

(* This version implements
 * a(n) = a(n-1) + a(n-k)
 * where k is the number of continued W
 * - Reduce memory usage.
 *)

let (t,k) = bscanf Scanning.stdin "%d %d " (fun x y -> (x,y) )

let modn i = i mod 1000000007

let ni i = i
let sn i = string_of_int i

(*let nn = 100000 + 5*)
let nn_max = 100000 + 5

let nn = 100000 + 3

let r= Array.make nn_max (ni (-1))

for i = 1 to k-1 do
  r.(i) <- (ni 1)
done

let () =
 r.(k) <- ni 2

let f n =
  if r.(n) = (ni (-1)) then
    begin
    let x = modn ( (modn r.(n-1)) + (modn r.(n-k)) ) in
    r.(n) <- x; x
    end
  else
    r.(n)

for i = k+1 to nn do
  r.(i) <- modn ((modn (f (i-1))) + (modn (f (i-k))))
done

(*
for i = 95515 to nn do
    printf "%s %s\n" (sn r.(i)) (sn w.(i))
done
*)

for i = 1 to t do
   let (s,e) = bscanf Scanning.stdin "%d %d " (fun x y -> (x,y) ) in
   let sum = ref (ni 0) in
   for j = s to e do
       sum := !sum + r.(j)
   done;
   printf "%s\n" (sn (modn !sum) )
done

open Scanf;;
open String;;
open Printf;;
open Num;;

let (t,k) = bscanf Scanning.stdin "%d %d " (fun x y -> (x,y) )

let modn i = mod_num i (num_of_int 1000000007)

let ni i = num_of_int i
let sn i = string_of_num i

(*let nn = 100000 + 5*)
let nn_max = 100000 + 5

let nn = 100000 + 1

let r= Array.make nn_max (ni 0)
let w= Array.make nn_max (ni 0)

let num_w = ref 1

let () =
 r.(1) <- ni 1

if k = 1 then
begin
  w.(1) <- ni 1;
end
else
begin
  w.(1) <- ni 0;
end

for i = 2 to nn do
   r.(i) <- r.(i-1) +/ w.(i-1);
   w.(i) <- w.(i-1);
   num_w := !num_w + 1;

   if !num_w = k then
   begin
   w.(i) <- w.(i) +/ (ni 1);
num_w := 0;
end
done

(*
for i = 0 to nn do
    printf "%s %s\n" (sn r.(i)) (sn w.(i))
done
*)

for i = 1 to t do
   let (s,e) = bscanf Scanning.stdin "%d %d " (fun x y -> (x,y) ) in
   let sum = ref (ni 0) in
   for j = s to e do
       sum := !sum +/ r.(j) +/ w.(j)
   done;
   printf "%s\n" (sn (modn !sum) )
done

open Scanf;;
open String;;
open Printf;;
open Num;;

(* This version implements
 * a(n) = a(n-1) + a(n-k)
 * where k is the number of continued W
 *)

let (t,k) = bscanf Scanning.stdin "%d %d " (fun x y -> (x,y) )

let modn i = mod_num i (num_of_int 1000000007)

let ni i = num_of_int i
let sn i = string_of_num i

(*let nn = 100000 + 5*)
let nn_max = 100000 + 5

let nn = 100000 + 3

let r= Array.make nn_max (ni (-1))

for i = 1 to k-1 do
  r.(i) <- (ni 1)
done

let () =
 r.(k) <- ni 2

let f n =
  if r.(n) = (ni (-1)) then
    begin
    let x = r.(n-1) +/ r.(n-k) in
    r.(n) <- x; x
    end
  else
    r.(n)

(*
for i = 95515 to nn do
    printf "%s %s\n" (sn r.(i)) (sn w.(i))
done
*)

for i = 1 to t do
   let (s,e) = bscanf Scanning.stdin "%d %d " (fun x y -> (x,y) ) in
   let sum = ref (ni 0) in
   for j = s to e do
       sum := !sum +/ (f j)
   done;
   printf "%s\n" (sn (modn !sum) )
done

open Scanf;;
open String;;
open Printf;;
open Int64;;

(* This version implements
 * a(n) = a(n-1) + a(n-k)
 * where k is the number of continued W
 * - Reduce memory usage.
 *)

let (t,k) = bscanf Scanning.stdin "%d %d " (fun x y -> (x,y) )

let modn i = rem i (of_int 1000000007)

let ni i = of_int i
let sn i = to_string i

(*let nn = 100000 + 5*)
let nn_max = 100000 + 5

let nn = 100000 + 3

let r= Array.make nn_max (ni (-1))

for i = 1 to k-1 do
  r.(i) <- (ni 1)
done

let () =
 r.(k) <- ni 2

let f n =
  if r.(n) = (ni (-1)) then
    begin
    let x = ( add (r.(n-1))  (r.(n-k)) ) in
    r.(n) <- x; x
    end
  else
    r.(n)

for i = k+1 to nn do
  r.(i) <-  (add ((f (i-1)))  ((f (i-k))) )
done

(*
for i = 95515 to nn do
    printf "%s %s\n" (sn r.(i)) (sn w.(i))
done
*)

for i = 1 to t do
   let (s,e) = bscanf Scanning.stdin "%d %d " (fun x y -> (x,y) ) in
   let sum = ref (ni 0) in
   for j = s to e do
       sum := add !sum r.(j)
   done;
   printf "%s\n" (sn (modn !sum) )
done

open Scanf;;
open String;;
open Printf;;
open Num;;

(* This version implements
 * a(n) = a(n-1) + a(n-k)
 * where k is the number of continued W
 * - Reduce memory usage.
 *)

let (t,k) = bscanf Scanning.stdin "%d %d " (fun x y -> (x,y) )

let modn i = i mod 1000000007

let ni i = i
let sn i = string_of_int i

(*let nn = 100000 + 5*)
let nn_max = 100000 + 5

let nn = 100000 + 3

let r= Array.make nn_max (ni (-1))

for i = 1 to k-1 do
  r.(i) <- (ni 1)
done

let () =
 r.(k) <- ni 2

let f n =
  if r.(n) = (ni (-1)) then
    begin
    let x = modn ( r.(n-1) + r.(n-k) ) in
    r.(n) <- x; x
    end
  else
    r.(n)

for i = k+1 to nn do
  r.(i) <- (f (i-1)) + (f (i-k))
done

(*
for i = 95515 to nn do
    printf "%s %s\n" (sn r.(i)) (sn w.(i))
done
*)

for i = 1 to t do
   let (s,e) = bscanf Scanning.stdin "%d %d " (fun x y -> (x,y) ) in
   let sum = ref (ni 0) in
   for j = s to e do
       sum := !sum + r.(j)
   done;
   printf "%s\n" (sn (modn !sum) )
done

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

let (--) debut fin =
  let rec range aux start en =
    if start > en then aux
    else range (en::aux) start (en-1)
  in range [] debut fin;;

let print_list f lst =
  let rec print_elements = function
    | [] -> ()
    | h::t -> f h; print_string ";"; print_elements t
  in
  print_string "[";
  print_elements lst;
  print_string "]\n";
  lst;;

let modulo = 1000000007;;

let dyn_array = Array.make 100000 (-1);;

let rec get_ways gap = function
  | index when index = Array.length dyn_array -> 1
  | index when index > Array.length dyn_array -> 0
  | index when dyn_array.(index) != -1 -> dyn_array.(index)
  | index ->
    let res = (get_ways gap (index+1) + get_ways gap (index+gap)) mod modulo in
    dyn_array.(index) <- res;
    res;;

(*#trace get_ways;;*)

type 'a node_data = {
  data:'a;
  start: int;
  fin: int
};;

type 'a node =
  | Leaf of 'a node_data
  | Cell of 'a node_data * 'a node * 'a node;;

let get_data = function
  | Leaf data
  | Cell (data,_,_)
    -> data;;

let computing_function = (+);;
let default_val = 0;;

let rec get_range tree debut fin = match tree with
  | Leaf data ->
    if data.start >= debut && data.fin <= fin then
      data.data
    else
      default_val
  | Cell (d, left, right) ->
    if fin < d.start || debut > d.fin then
      default_val
    else if d.fin > fin || d.start < debut then
      computing_function (get_range left debut fin) (get_range right debut fin)
    else
      d.data;;

let rec buildTree debut fin =
  if debut = fin then
    Leaf {
        data = dyn_array.(debut);
        start = debut;
        fin = fin
      }
  else
    let mid = (debut + fin)/2 in
    let right = buildTree (mid + 1) fin in
    let left = buildTree debut mid in
    let node_d = {
      data = (computing_function (get_data left).data (get_data right).data);
      start = debut;
      fin = fin
    } in
    Cell (node_d, left, right);;

let nbTests = read_int () in
let gap = read_int () in
let _ = get_ways gap 0 in
let tree = buildTree 0 (Array.length dyn_array - 1) in
for i = 1 to nbTests do
  let start = read_int () in
  let tree_end = Array.length dyn_array - start in
  let fin = read_int () in
  let tree_start = Array.length dyn_array - fin in
  let res = get_range tree tree_start tree_end in
  Printf.printf "%d\n" res
done;;

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

let (--) debut fin =
  let rec range aux start en =
    if start > en then aux
    else range (en::aux) start (en-1)
  in range [] debut fin;;

let print_list f lst =
  let rec print_elements = function
    | [] -> ()
    | h::t -> f h; print_string ";"; print_elements t
  in
  print_string "[";
  print_elements lst;
  print_string "]\n";
  lst;;

let modulo = 1000000007;;

let dyn_array = Array.make 100005 (-1);;

let rec get_ways gap = function
  | index when index = Array.length dyn_array -> 1
  | index when index > Array.length dyn_array -> 0
  | index when dyn_array.(index) != -1 -> dyn_array.(index)
  | index ->
    let res = (get_ways gap (index+1) + get_ways gap (index+gap)) mod modulo in
    dyn_array.(index) <- res;
    res;;

(*#trace get_ways;;*)

let nbTests = read_int () in
let gap = read_int () in
let _ = get_ways gap 0 in
for i = 1 to Array.length dyn_array - 1 do
  dyn_array.(i) <- (dyn_array.(i) + dyn_array.(i - 1)) mod modulo
done;
for i = 1 to nbTests do
  let start = read_int () in
  let tree_end = Array.length dyn_array - start in
  let fin = read_int () in
  let tree_start = Array.length dyn_array - fin in
  let res = (modulo + dyn_array.(tree_end) - if tree_start > 0 then dyn_array.(tree_start - 1) else 0) mod modulo in
  Printf.printf "%l\n" res
done;;

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

let (--) debut fin =
  let rec range aux start en =
    if start > en then aux
    else range (en::aux) start (en-1)
  in range [] debut fin;;

let print_list f lst =
  let rec print_elements = function
    | [] -> ()
    | h::t -> f h; print_string ";"; print_elements t
  in
  print_string "[";
  print_elements lst;
  print_string "]\n";
  lst;;

let modulo = 1000000007;;

let dyn_array = Array.make 100000 (-1);;

let rec get_ways gap = function
  | index when index = Array.length dyn_array -> 1
  | index when index > Array.length dyn_array -> 0
  | index when dyn_array.(index) != -1 -> dyn_array.(index)
  | index ->
    let res = (get_ways gap (index+1) + get_ways gap (index+gap)) mod modulo in
    dyn_array.(index) <- res;
    res;;

(*#trace get_ways;;*)

type 'a node_data = {
  data:'a;
  start: int;
  fin: int
};;

type 'a node =
  | Leaf of 'a node_data
  | Cell of 'a node_data * 'a node * 'a node;;

let get_data = function
  | Leaf data
  | Cell (data,_,_)
    -> data;;

let computing_function a b=
  (a + b) mod modulo;;
let default_val = 0;;

let rec get_range tree debut fin = match tree with
  | Leaf data ->
    if data.start >= debut && data.fin <= fin then
      data.data
    else
      default_val
  | Cell (d, left, right) ->
    if fin < d.start || debut > d.fin then
      default_val
    else if d.fin > fin || d.start < debut then
      computing_function (get_range left debut fin) (get_range right debut fin)
    else
      d.data;;

(*#trace get_range;;*)

let rec buildTree debut fin =
  if debut = fin then
    Leaf {
        data = dyn_array.(debut);
        start = debut;
        fin = fin
      }
  else
    let mid = (debut + fin)/2 in
    let right = buildTree (mid + 1) fin in
    let left = buildTree debut mid in
    let node_d = {
      data = (computing_function (get_data left).data (get_data right).data);
      start = debut;
      fin = fin
    } in
    Cell (node_d, left, right);;

let nbTests = read_int () in
let gap = read_int () in
let _ = get_ways gap 0 in
let tree = buildTree 0 (Array.length dyn_array - 1) in
for i = 1 to nbTests do
  let start = read_int () in
  let tree_end = Array.length dyn_array - start in
  let fin = read_int () in
  let tree_start = Array.length dyn_array - fin in
  let res = get_range tree tree_start tree_end in
  Printf.printf "%d\n" res
done;;


open Printf
open Scanf

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

let read_int _ = bscanf Scanning.stdib " %d " (fun x -> x)
let read_string _ = bscanf Scanning.stdib " %s " (fun x -> x)
let () =
  let n = read_int () in
  let m = read_int () in
  let light = Array.init n read_string in
(* light.(f).[r] = is the light on floor f in room r *)
  let ison f r = light.(n-1-f).[r] = '1' in

  let floor_off f = forall 0 (m+1) (fun r -> not (ison f r)) in

  let top_floor = maxf 0 (n-1) (fun f -> if floor_off f then 0 else f) in

  let ftime f st en =
  (* time to clear floor f starting at st (=0/1 for left/right) ending at en *)
    if en <> st then m+1
    else if st=0 then 2 * (maxf 1 m (fun r -> if ison f r then r else 0))
    else if st=1 then 2 * (maxf 1 m (fun r -> if ison f r then (m+1-r) else 0))
    else failwith "no"
  in

  let dp = Array.make_matrix n 2 0 in

  for f=0 to n-2 do
    for en = 0 to 1 do
      dp.(f).(en) <-
	if f=0 then ftime f 0 en
	else minf 0 1 (fun st -> dp.(f-1).(st) + 1 + (ftime f st en))
(*      printf "dp.(%d).(%d) = %d\n" f en dp.(f).(en) *)
    done
  done;

(* handle the last floor *)

  let answer f =
    if f=0 then (ftime 0 0 0)/2
    else minf 0 1 (fun st -> dp.(f-1).(st) + ((ftime f st st)/2) + 1)
  in

  printf "%d\n" (answer top_floor)

open Printf
open Scanf

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

let read_int _ = bscanf Scanning.stdib " %d " (fun x -> x)
let read_string _ = bscanf Scanning.stdib " %s " (fun x -> x)
let () =
  let n = read_int () in
  let m = read_int () in
  let light = Array.init n read_string in
(* light.(f).[r] = is the light on floor f in room r *)
  let ison f r = light.(n-1-f).[r] = '1' in

  let floor_off f = forall 0 (m+1) (fun r -> not (ison f r)) in

  let top_floor = maxf 0 (n-1) (fun f -> if floor_off f then 0 else f) in

  let ftime f st en =
  (* time to clear floor f starting at st (=0/1 for left/right) ending at en *)
    if en <> st then m+1
    else if st=0 then 2 * (maxf 1 m (fun r -> if ison f r then r else 0))
    else if st=1 then 2 * (maxf 1 m (fun r -> if ison f r then (m+1-r) else 0))
    else failwith "no"
  in

  let best_cost = ref 1_000_000 in

  let rec backtrack f side cost =
    if f = top_floor then (
      let top_cost = (ftime f side side)/2 in
      best_cost := min !best_cost (cost + 1 + top_cost)
    ) else (
      backtrack (f+1) side (cost + 1 + ftime f side side);
      backtrack (f+1) (1-side) (cost + 1 + ftime f side (1-side));
    )
  in

  backtrack 0 0 (-1);

  printf "%d\n" !best_cost

open Printf
open Scanf

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

let read_int _ = bscanf Scanning.stdib " %d " (fun x -> x)
let read_string _ = bscanf Scanning.stdib " %s " (fun x -> x)
let () =
  let n = read_int () in
  let m = read_int () in
  let light = Array.init n read_string in
(* light.(f).[r] = is the light on floor f in room r *)
  let ison f r = light.(n-1-f).[r] = '1' in

  let ftime f st en =
  (* time to clear floor f starting at st (=0/1 for left/right) ending at en *)
    if en <> st then m+1
    else if st=0 then 2 * (maxf 1 m (fun r -> if ison f r then r else 0))
    else if st=1 then 2 * (maxf 1 m (fun r -> if ison f r then (m+1-r) else 0))
    else failwith "no"
  in

  let dp = Array.make_matrix n 2 0 in

  for f=0 to n-2 do
    for en = 0 to 1 do
      dp.(f).(en) <-
	if f=0 then ftime f 0 en
	else minf 0 1 (fun st -> dp.(f-1).(st) + 1 + (ftime f st en))
(*      printf "dp.(%d).(%d) = %d\n" f en dp.(f).(en) *)
    done
  done;

(* handle the last floor *)

  let answer f =
    if f=0 then (ftime 0 0 0)/2
    else minf 0 1 (fun st -> dp.(f-1).(st) + ((ftime f st st)/2) + 1)
  in

  printf "%d\n" (answer (n-1))

open Printf
open Scanf

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

let read_int _ = bscanf Scanning.stdib " %d " (fun x -> x)
let read_string _ = bscanf Scanning.stdib " %s " (fun x -> x)
let () =
  let n = read_int () in
  let m = read_int () in
  let light = Array.init n read_string in
(* light.(f).[r] = is the light on floor f in room r *)
  let ison f r = light.(n-1-f).[r] = '1' in

  let inf = 1_000_000_000 in

  let ftime f st en =
  (* time to clear floor f starting at st (=0/1 for left/right) ending at en *)
    if en <> st then m+1
    else if st=0 then 2 * (maxf 1 m (fun r -> if ison f r then r else 0))
    else if st=1 then 2 * (maxf 1 m (fun r -> if ison f r then (m+1-r) else 0))
    else failwith "no"
  in

  let dp = Array.make_matrix n 2 0 in

  for f=0 to n-2 do
    for en = 0 to 1 do
      dp.(f).(en) <-
	if f=0 then ftime f 0 en
	else minf 0 1 (fun st -> dp.(f-1).(st) + 1 + (ftime f st en))
(*      printf "dp.(%d).(%d) = %d\n" f en dp.(f).(en) *)
    done
  done;

(* handle the last floor *)

  let answer f = minf 0 1 (fun st ->
    let prev = if f=0 then (if st=0 then 0 else inf) else dp.(f-1).(st) in
    prev + ((ftime f st st)/2) + (if f<>0 then 1 else 0)
  ) in

  printf "%d\n" (answer (n-1))


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 es = Array.make n [] in
  let ht = Hashtbl.create m in
  let () =
    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);
	Hashtbl.add ht (v, u) ();
    done
  in
  let es = Array.map Array.of_list es in
  let used = Array.make n false in
  let res = ref [] in
  let stack = Array.make (2 * n) (fun _ -> ()) in
  let stack_pos = ref 0 in
  let rec execute () =
    if !stack_pos > 0 then (
      decr stack_pos;
      let f = stack.(!stack_pos) in
	f ();
	execute ()
    )
  in
  let rec dfs u prev =
    used.(u) <- true;
    let rec aux i =
      if i < Array.length es.(u) then (
	stack.(!stack_pos) <- (fun () -> aux (i + 1));
	incr stack_pos;
	let v = es.(u).(i) in
	  if not used.(v) then (
	    (*let f () =
	      (*pe.(v) <- edge;
	      tout.(u) <- !t;
	      incr t;*) ()
	    in
	      (*stack.(!stack_pos) <- f;
	      incr stack_pos;*)*)
	      stack.(!stack_pos) <- (fun () -> dfs v u);
	      incr stack_pos;
	  )
      ) else (
      )
    in
    let f () =
      res := u :: !res
    in
      stack.(!stack_pos) <- f;
      incr stack_pos;
      aux 0
  in
    for u = 0 to n - 1 do
      if not used.(u) then (
	stack_pos := 0;
	dfs u 0;
	execute ();
      )
    done;
    List.iter
      (fun u ->
	 Printf.printf "%d " (u + 1)
      ) (List.rev !res);
    Printf.printf "\n"

let read_int()=Scanf.scanf " %d" (fun x->x);;
let n=read_int() and m=read_int();;
let pred=Array.make (n+1) [0];;
for i=1 to n do
   pred.(i)<-[]
done;;
for i=1 to m do
   let x=read_int() and y=read_int() in
   pred.(x)<-y::pred.(x)
done;;
type etat=Vu|Non_vu|En_cours;;
let mark=Array.make (n+1) Non_vu;;
let res=ref [];;

let rec dfs u=function
|[]->mark.(u)<-Vu;res:=u::(!res)
|h::t->if mark.(h)=Non_vu then
          begin
          mark.(h)<-En_cours;
          dfs h pred.(h)
          end;
       dfs u t;;

let tri()=
for i=1 to n do
   if mark.(i)=Vu then () else
      begin
      mark.(i)<-En_cours;
      dfs i pred.(i)
      end;
done;;

tri();;

let rec print_list=function
|[]->print_newline()
|h::t->print_list t;Printf.printf "%d " h;;

print_list !res;;

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 es = Array.make n [] in
  let ht = Hashtbl.create m in
  let () =
    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.(v) <- u :: es.(u);
	Hashtbl.add ht (v, u) ();
    done
  in
  let es = Array.map Array.of_list es in
  let used = Array.make n false in
  let res = ref [] in
  let stack = Array.make (2 * n) (fun _ -> ()) in
  let stack_pos = ref 0 in
  let rec execute () =
    if !stack_pos > 0 then (
      decr stack_pos;
      let f = stack.(!stack_pos) in
	f ();
	execute ()
    )
  in
  let rec dfs u prev =
    used.(u) <- true;
    let rec aux i =
      if i < Array.length es.(u) then (
	stack.(!stack_pos) <- (fun () -> aux (i + 1));
	incr stack_pos;
	let v = es.(u).(i) in
	  if not used.(v) then (
	    (*let f () =
	      (*pe.(v) <- edge;
	      tout.(u) <- !t;
	      incr t;*) ()
	    in
	      (*stack.(!stack_pos) <- f;
	      incr stack_pos;*)*)
	      stack.(!stack_pos) <- (fun () -> dfs v u);
	      incr stack_pos;
	  )
      ) else (
      )
    in
    let f () =
      res := u :: !res
    in
      stack.(!stack_pos) <- f;
      incr stack_pos;
      aux 0
  in
    for u = 0 to n - 1 do
      if not used.(u) then (
	stack_pos := 0;
	dfs u 0;
	execute ();
      )
    done;
    List.iter
      (fun u ->
	 Printf.printf "%d " (u + 1)
      ) ((*List.rev*) !res);
    Printf.printf "\n"


let () =
	let n = Scanf.scanf "%d" (fun i -> i) and l = ref [] and tab = Array.make 2001 0
	and tab' = Array.make 2001 0 in
		for i=1 to n do
			Scanf.scanf " %d" (fun j -> tab.(j) <- tab.(j) + 1; l := j::!l);
		done;
		for i=2000 downto 1 do
			tab'.(i-1) <- tab'.(i) + tab.(i);
		done;
	List.iter (fun i -> Printf.printf "%d " (1 + tab'.(i))) (List.rev !l)

open Printf
open Scanf

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

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

let () =
    (* let start_time = Sys.time() in *)

    let n = read_int () in
    let a = Array.init n (fun _ -> read_int()) in
    let b = Array.init n (fun i -> string_of_int (
                            1 + sum 0 n (fun x -> if a.(x) > a.(i) then 1 else 0)
                         )) in
    print_string (String.concat " " (Array.to_list b));

    (* let finish_time = Sys.time() in *)
    (* printf "Time of scanning %fs\n" (finish_time -. start_time); *)
    (* b;; *)

open Printf
open Scanf

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

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

let main () =
    let start_time = Sys.time() in

    let n = read_int () in
    let a = Array.init n (fun _ -> read_int()) in
    let b = Array.init n (fun i -> string_of_int (
                            sum 0 n (fun x -> if a.(x) > a.(i) then 1 else 0)
                         )) in
    print_string (String.concat " " (Array.to_list b));

    let finish_time = Sys.time() in
    printf "Time of scanning %fs\n" (finish_time -. start_time);
    b;;

open Printf
open Scanf

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

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

let () =
    (* let start_time = Sys.time() in *)

    let n = read_int () in
    let a = Array.init n (fun _ -> read_int()) in
    let b = Array.init n (fun i -> string_of_int (
                            sum 0 n (fun x -> if a.(x) > a.(i) then 1 else 0)
                         )) in
    print_string (String.concat " " (Array.to_list b));

    (* let finish_time = Sys.time() in *)
    (* printf "Time of scanning %fs\n" (finish_time -. start_time); *)
    (* b;; *)


let cmp x y =
    let a = 10 - (x mod 10)
    and b = 10 - (y mod 10) in
    compare a b

let () = Scanf.scanf "%d %d\n" (fun n k ->
    (* read initial skills *)
    let a = Array.init n (fun _ -> Scanf.scanf "%d%_c" (fun x -> x)) in
    (* sort by distance from the next multiple of 10 *)
    Array.sort cmp a;

    (* calculate the possible improvement *)
    let (rating, rem) = Array.fold_left (fun (acc, rem) skill ->
            (* if we have enough skill points remaining, use them *)
            let diff = 10 - (skill mod 10) in
            if diff <= rem
            then (acc + (skill + diff) / 10, rem - diff)
            else (acc + skill / 10, rem)
        ) (0, k) a in

    Printf.printf "%d\n" (min (rating + rem / 10) (n * 10)))

(*I should use a cyclic list*)
open Scanf;;
open Printf;;

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

let a = Array.make n 0

let rec readi i l =
  if i = n then
    List.rev l
  else
    begin
      let x = Scanf.scanf "%d " (fun x -> x) in
      readi (i+1) (x::l)
    end

let b = readi 0 []

let b1 = List.filter (fun x -> x mod 10 !=0 ) b |> List.sort (fun x y -> compare (y mod 10)  (x mod 10) )

let b2 = List.filter (fun x -> x mod 10 =0 ) b |> List.sort (fun x y -> compare y  x)

       (*
let _ =
  List.iter (fun x -> printf "%d " x) b

let _ = List.mapi (fun i x -> a.(i)<-x) (b2 @ b1)
        *)

let rec f ll rl k =
  if k==0 then
    (((List.rev ll) @ rl), k)
  else
    begin
      match rl with
      | [] -> ( (List.rev ll), k)
      | hd::tl ->
         begin
           if (hd mod 10) + k >= 10 then
             let x = 10 - (hd mod 10) in
             f ((hd + x)::ll) tl (k-x)
           else
             f (hd::ll) tl 0
         end
    end

let (l,t) = f [] b1 k

let l = l @ b2
(*
let _ =
  print_endline "";
  printf "%d\n" t;
  List. iter (fun x -> printf "%d " x) l;
  print_endline ""
 *)

let _ =
  if t>0 then
    let s = List.fold_left (fun x y -> x + (100-y)) 0 l in
    if s<= t then
      printf "%d\n" (10*(List.length l))
    else
      begin
        let s1 = List.fold_left (fun x y -> x + y/10) 0 (t::l) in
        printf "%d\n" s1
      end
  else
    begin
      let s1 = List.fold_left (fun x y -> x + y/10) 0 l in
      printf "%d\n" s1
    end

let cmp x y =
    let a = 10 - (x mod 10)
    and b = 10 - (y mod 10) in
    compare a b

let () = Scanf.scanf "%d %d\n" (fun n k ->
    (* read initial skills *)
    let a = Array.init n (fun _ -> Scanf.scanf "%d%_c" (fun x -> x)) in
    (* sort by distance from the next multiple of 10 *)
    Array.sort cmp a;

    (* calculate the possible improvement *)
    let (rating, rem) = Array.fold_left (fun (acc, rem) skill ->
            (* if we have enough skill points remaining, use them *)
            let diff = 10 - (skill mod 10) in
            if diff <= rem
            then (acc + (skill + diff) / 10, rem - diff)
            else (acc + skill / 10, rem)
        ) (0, k) a in

    Printf.printf "%d\n" (rating + rem / 10))

open Scanf;;
open String;;
open Printf;;
open Int64;;
open Char;;

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

let a = Array.make n 0

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

let cmp x y =
  if x mod 10 = 0 then
    1
  else
    begin
      let xi = x mod 10 in
      let yi = y mod 10 in
      if xi > yi then
        1
      else
        begin
          if xi < yi then
            -1
          else 0
        end
    end
    in
    Array.fast_sort (fun x y -> cmp y x) a
(*
let _ =
  Array.iter (fun x -> printf "%d " x) a
 *)

exception BR

 let _ =
   let rk = ref k in
   try
   for i = 0 to (n-1) do
    if !rk <= 0 then
      raise BR
    else
      if !rk+a.(i) >= 100 then
        begin
          rk := !rk - (100-a.(i));
          a.(i) <- 100
        end
      else
        begin
          if !rk + (a.(i) mod 10) >= 10 then
            begin
              let tmp = !rk+a.(i) in
              let tmp2 = tmp - (tmp mod 10) in
              (
                rk := !rk - (tmp2 - a.(i));
                a.(i) <- tmp2
              )
            end
          else ()
        end
   done
   with BR -> ()

(*
 let _ =
   print_endline "";
   Array.iter (fun x -> printf "%d " x) a
 *)

 let rslt =
   Array.fold_left (fun x y -> x + y/10) 0 a
 in
 printf "%d " rslt

open Scanf;;
open String;;
open Printf;;
open Int64;;
open Char;;

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

let a = Array.make n 0

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

let cmp x y =
  if x mod 10 = 0 then
    1
  else begin
    let xi = x mod 10 in
    let yi = y mod 10 in
    if xi > yi then
      1
    else if xi < yi then
      -1
    else 0
  end
  in
  Array.fast_sort (fun x y -> cmp x y) a

  (*
 let _ =
   Array.iter (fun x -> printf "%d " x) a
   *)

exception BR

 let _ =
   let rk = ref k in
   try
   for i = 0 to (n-1) do
    if !rk <= 0 then
      raise BR
    else
      if !rk+a.(i) >= 100 then
        begin
          rk := !rk - (100-a.(i));
          a.(i) <- 100
        end
      else
        begin
          let tmp = !rk+a.(i) in
          let tmp2 = tmp - (tmp mod 10) in
          (
          rk := !rk - (tmp2 - a.(i));
          a.(i) <- tmp2
          )
        end
   done
   with BR -> ()

 let rslt =
   Array.fold_left (fun x y -> x + y/10) 0 a
 in
 printf "%d " rslt


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

let () =
	let n=get_i64 0 in let n=i32 n in
	let e=make n [] in
	let visited = make n false in
	repi 2 n (fun _->
		let u,v = get_2_i64 0 in
		let u,v = i32 u-$1,i32 v-$1 in
		e.(u) <- v::e.(u);
		e.(v) <- u::e.(v);
	);
	let res = ref 0L in
	let s = fix (fun f i ->
		visited.(i) <- true;
		List.fold_left (fun sum j->
			if visited.(j) then sum
			else (
				let v = f j in
				if v %$ 2 = 0 then (
					res += 1L; sum
				) else sum +$ v
			)
		) 1 e.(i)
	) 0 in
	if s %$ 2 = 0 then printf "%Ld\n" !res
	else printf "-1\n"

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 subtree = Array.make (n + 1) 1
    and visited = Array.make (n + 1) false
    and ans = ref 0 in
    let rec dfs v =
        if not visited.(v) then begin
            visited.(v) <- true;
            subtree.(v) <- List.fold_left (fun acc x ->
                if not visited.(x) then begin
                    dfs x;
                    acc + subtree.(x)
                end
                else acc
            ) 1 g.(v);
            if subtree.(v) mod 2 = 0 then incr ans
        end in
    for i = 1 to n - 1 do
        let a = read_int () and b = read_int () in
        g.(a) <- b::g.(a);
        g.(b) <- a::g.(b)
    done;
    dfs 1;
    if n mod 2 = 1 then printf "-1\n"
    else printf "%d\n" (!ans - 1)

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 subtree = Array.make (n + 1) 1
    and visited = Array.make (n + 1) false
    and ans = ref 0
    and err = ref false in
    let rec dfs v =
        let flag = ref false in
        if not visited.(v) then begin
            visited.(v) <- true;
            subtree.(v) <- List.fold_left (fun acc x ->
                if not visited.(x) then begin
                    dfs x;
                    if subtree.(x) mod 2 = 1 then flag := true;
                    acc + subtree.(x)
                end
                else acc
            ) 1 g.(v);
            if subtree.(v) mod 2 = 0 then incr ans
            else if !flag then err := true;
        end in
    for i = 1 to n - 1 do
        let a = read_int () and b = read_int () in
        g.(a) <- b::g.(a);
        g.(b) <- a::g.(b)
    done;
    dfs 1;
    if !err then printf "-1\n"
    else printf "%d\n" (!ans - 1)


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

let () =
  let n = xint() and m = xint() in
  let cost = ref 0 in
  for i = 0 to n - 1 do
    let tmp = ref 1000000000 in
    for j = 0 to m - 1 do
      let x = xint() in
      tmp := if x < !tmp then x else !tmp;
    done;
    cost := if !cost < !tmp then !tmp else !cost;
  done;
  Printf.printf "%d\n" !cost;

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

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

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

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

let _ =
  let (n, m) = read2 () in
  let maxi = ref 0 in
  for i=1 to n do
    let mini = ref max_int in
    for i=1 to m-1 do
      Scanf.scanf "%d " ( fun x -> if x < !mini then mini := x )
    done;
    Scanf.scanf "%d\n" ( fun x -> if x < !mini then mini := x );
    if !mini > !maxi then maxi := !mini;
  done;
  Printf.printf "%d\n" !maxi ;;


let parse s len = let rep = Array.make len 0 in
  let temp = ref "" in
  let count = ref 0 in
  for j  = 0 to String.length s -1 do
    if s.[j] = ' ' then (rep.(!count)<-(int_of_string !temp) ; incr count ; temp := "")
    else (temp := !temp ^ (String.make 1 s.[j]))
  done; rep.(!count)<-(int_of_string !temp) ; rep ;;

let maxes tab = let max1 = ref tab.(0) in
	let count = ref 0 in
  for i = 1 to Array.length tab -1 do
	if tab.(i) = !max1 then incr count;
    if tab.(i) > !max1 then (max1 := tab.(i); count := 0) ;

  done;
let toto = ref false in
  if !count <> 0 then true else begin
	for i = 0 to Array.length tab -1 do
	if tab.(i) = (!max1 -1) then toto := true
done ; !toto

end ;;

let n = int_of_string (input_line stdin) in
for i = 0 to n-1 do
  let len = int_of_string (input_line stdin) in
  let str = input_line stdin in
  let tab = parse str len in
  if len < 2 then (if tab.(0) = 1 then print_endline "YES" else print_endline "NO")
  else begin
    if maxes tab then print_endline "YES" else print_endline "NO" end
done;;

open Printf
open Scanf
module LL = Int64
let ( !! ) x = LL.to_int x
let ( ++ ) x y = LL.add x y
let ( -- ) x y = LL.sub x y
let ( ** ) x y = LL.mul x y
let ( // ) x y = LL.div x y
let ( %% ) x y = LL.rem x y
let ( >> ) x y = (LL.compare x y) > 0
let ( << ) x y = (LL.compare x y) < 0
let ( == ) x y = (LL.compare x y) = 0
let rd_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let rd_ll _ = bscanf Scanning.stdin " %Ld " (fun x -> x)
let rd_arr n = Array.init n rd_ll

let solve n a =
  Array.sort LL.compare a;
  let dx =
    if n = 1 then a.(0)
    else a.(n-1) -- a.(n-2) in

  if dx >> 1L then "NO" else "YES"

let () =
  let t = read_int () in
  for i=1 to t do
    let n = rd_int () in
    let a = rd_arr n in
    printf "%s\n" (solve n a)
  done;

let parse s len = let rep = Array.make len 0 in
  let temp = ref "" in
  let count = ref 0 in
  for j  = 0 to String.length s -1 do
    if s.[j] = ' ' then (rep.(!count)<-(int_of_string !temp) ; incr count ; temp := "")
    else (temp := !temp ^ (String.make 1 s.[j]))
  done; rep.(!count)<-(int_of_string !temp) ; rep ;;

let maxes tab = let max1 = ref tab.(0) in
	let count = ref 0 in
  for i = 1 to Array.length tab -1 do
    if tab.(i) > !max1 then (max1 := tab.(i); count := 0) ;
	if tab.(i) = !max1 then incr count;
  done;
let toto = ref false in
  if !count <> 0 then true else begin
	for i = 0 to Array.length tab -1 do
	if tab.(i) = !max1 -1 then toto := true
done ; !toto

end ;;

let n = int_of_string (input_line stdin) in
for i = 0 to n-1 do
  let len = int_of_string (input_line stdin) in
  let str = input_line stdin in
  let tab = parse str len in
  if len < 2 then (if tab.(0) = 1 then print_endline "YES" else print_endline "NO")
  else begin
    if maxes tab then print_endline "YES" else print_endline "NO" end
done;;

let parse s len = let rep = Array.make len 0 in
  let temp = ref "" in
  let count = ref 0 in
  for j  = 0 to String.length s -1 do
    if s.[j] = ' ' then (rep.(!count)<-(int_of_string !temp) ; incr count ; temp := "")
    else (temp := !temp ^ (String.make 1 s.[j]))
  done; rep.(!count)<-(int_of_string !temp) ; rep ;;

let maxes tab = let max1 = ref tab.(0) in let max2 = ref tab.(1) in
  for i = 2 to Array.length tab -1 do
    if !max1 < !max2 && tab.(i) > !max1 then max1 := tab.(i) ;
    if !max1 >= !max2 && tab.(i) > !max2 then max2 := tab.(i) ;
  done;
  !max1 = !max2 || !max1 = !max2 -1 || !max2 = !max1 -1;;

let n = int_of_string (input_line stdin) in
for i = 0 to n-1 do
  let len = int_of_string (input_line stdin) in
  let str = input_line stdin in
  let tab = parse str len in
  if len < 2 then (if tab.(0) = 1 then print_endline "YES" else print_endline "NO")
  else begin
    if maxes tab then print_endline "YES" else print_endline "NO" end
done;;

let parse s len = let rep = Array.make len 0 in
  let temp = ref "" in
  let count = ref 0 in
  for j  = 0 to String.length s -1 do
    if s.[j] = ' ' then (rep.(!count)<-(int_of_string !temp) ; incr count ; temp := "")
    else (temp := !temp ^ (String.make 1 s.[j]))
  done; rep.(!count)<-(int_of_string !temp) ; rep ;;

let maxes tab = let max1 = ref tab.(0) in let max2 = ref tab.(1) in
  for i = 2 to Array.length tab -1 do
    if !max1 < !max2 && tab.(i) > !max1 then max1 := tab.(i) ;
    if !max1 >= !max2 && tab.(i) > !max2 then max2 := tab.(i) ;
  done;
  !max1 = !max2 || !max1 = !max2 +1 || !max2 = !max1 +1;;

let n = int_of_string (input_line stdin) in
for i = 0 to n-1 do
  let len = int_of_string (input_line stdin) in
  let str = input_line stdin in
  let tab = parse str len in
  if len < 2 then (if tab.(0) = 1 then print_endline "YES" else print_endline "NO")
  else begin
    if maxes tab then print_endline "YES" else print_endline "NO" end
done;;


open Scanf;;
open String;;
open Num;;

let mprint z =
  print_endline (Num.string_of_num z);;

let one_jump () =
  let (a,b,k) = Scanf.bscanf Scanf.Scanning.stdin "%d %d %d " (fun x y z -> (x,y,z) ) in
  let k2 = (num_of_int (k/2)) in
  let aa = num_of_int a in
  let bb = num_of_int b in
  let rslt = k2*/aa -/ k2*/bb +/ (num_of_int (k mod 2)) */ aa in
  mprint rslt
;;

let n = Scanf.bscanf Scanf.Scanning.stdin "%d " (fun x -> x) in
for i = 1 to n do
  one_jump ()
done

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

let () =
  let t = get_i64 0 in
  rep 1L t (fun _ ->
    let a,b,k = get_3_i64 0 in
    let n = k/2L in
    let d = a-b in
    printf "%Ld\n" @@ d * n + (if k mod 2L = 1L then a else 0L)
  )

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

let read_input () =
  let a = ri() and
  b = ri() and
  c = ri() in
    (Int64.of_int a, Int64.of_int b, Int64.of_int c)

let res a b k =
  let res1 = Int64.mul (Int64.sub a b) (Int64.div k (Int64.of_int 2)) and
  res2 = Int64.mul (Int64.rem k (Int64.of_int 2)) a in
  (*print_int a; print_int b; print_int k; *)
  Int64.add res1 res2

let solveCase () =
  let (a, b, k) = read_input () in
  let x = res a b k in
  print_endline (Int64.to_string x)

let rec solve t =
  (*print_int t; print_endline ". test"; *)
  if t=0 then ()
  else
    (solveCase ();
    solve (t-1))

let () =
  let t = read_int() in
  solve t

open Scanf;;
open String;;

let one_jump () =
  let (a,b,k) = Scanf.bscanf Scanf.Scanning.stdin "%d %d %d " (fun x y z -> (x,y,z) ) in
  let k2 = k/2 in
  let rslt = k2*a - k2*b + (k mod 2) * a in
  print_int rslt; print_string "\n"
;;

let n = Scanf.bscanf Scanf.Scanning.stdin "%d " (fun x -> x) in
for i = 1 to n do
  one_jump ()
done


open Printf
open Scanf

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

let solve () =
  let n = read_int() in
  let arr = Array.init n read_int in
  Array.sort compare arr;
  let rec loop i =
    if i >= n then (
        printf "YES\n";
    ) else if arr.(i) - arr.(i - 1) > 1 then (
        printf "NO\n";
    ) else (
        loop (i + 1)
    )
  in
  loop 1

let () =
    let cases = read_int() in
    for case = 1 to cases do
        solve ()
    done

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

let rec fill acc = function
  | 0 -> acc
  | n -> fill (acc @ [read_int ()]) (n - 1)

let solve (n : int) : string =
  let rec f (l : int list) : string =
    match l with
    | []
    | [_] -> "YES"
    | a :: b :: tl -> if (b - a) > 1 then "NO" else f ([b] @ tl)
  in f (List.sort compare (fill [] n))

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

open Printf
open Scanf

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

let solve () =
  let n = read_int() in
  let arr = Array.init n read_int in
  Array.sort compare arr;
  for i = 1 to n - 1 do
    if arr.(i) - arr.(i - 1) > 1 then (
        printf "NO\n";
    ) else if i = n - 1 then (
        printf "YES\n"
    )
  done;
  ()

let () =
    let cases = read_int() in
    for case = 1 to cases do
        solve ()
    done

open Printf
open Scanf

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

let solve () =
  let n = read_int() in
  let arr = Array.init n read_int in
  Array.sort compare arr;
  let rec loop i =
    if i >= n - 1 then (
        printf "YES\n";
    ) else if arr.(i) - arr.(i - 1) > 1 then (
        printf "NO\n";
    ) else (
        loop (i + 1)
    )
  in
  loop 1

let () =
    let cases = read_int() in
    for case = 1 to cases do
        solve ()
    done


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

open Printf

let init (x : int) (f : int -> 'a) : 'a list =
  let rec build k =
    if k = x then []
    else (f k) :: build (k + 1) in
  build 0
;;

let run () =
  let n = scan_int () in
  let k = scan_int () in
  if n < k || (k mod 2 = 0 && n mod 2 = 1) then
    printf "NO\n"
  else begin
    let lst =
      if n mod 2 = 1 then
        init k (fun x -> if x = 0 then n - k + 1 else 1)
      else if k mod 2 = 1 then
        init k (fun x -> if x = 0 then n - 2 * (k - 1) else 2)
      else
        init k (fun x -> if x = 0 then (n - k + 1) else 1) in
    if List.hd lst < 1 then printf "NO\n"
    else begin
      printf "YES\n";
      List.iter (printf "%d ") lst;
      print_endline "";
    end
  end
;;

let () =
  let t = scan_int () in
  for i = 1 to t do
    run ()
  done
;;

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

open List
open Printf

let solve () =
  let n = re () in
  let k = re () in
  if (n mod 2 = 0 && ((k mod 2 = 1 && n < 2 * k) || (k mod 2 = 0 && n < k))) || (n mod 2 = 1 && (n < k || k mod 2 = 0)) then printf "NO\n"
  else let _ = printf "YES\n" in
    for i = 1 to k - 1 do
      if n mod 2 = 0 then
        if k mod 2 = 1 then printf "%d " 2
        else printf "%d " 1
      else printf "%d " 1
    done;
    if n mod 2 = 0 then
      if k mod 2 = 1 then printf "%d \n" (n - 2 * (k - 1))
      else printf "%d \n" (n - (k - 1))
    else printf "%d \n" (n - (k - 1))
;;

let _ =
  let t = re () in
  for i = 1 to t do
    solve ()
  done
;;

(* Codeforces 1352 B Same Parity Summands train  *)

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

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

let rec repeat a n = match n with
  | 0 -> []
  | _ -> a :: repeat a (n-1);;

let rec repeat2 o d k = match k with
	| 0 -> []
	| _ ->
		if o == 0 then repeat d k
		else
			(2 + d) :: repeat2 (o - 2) d (k - 1);;

let rec summands n k =
	if k == 1 then [ n ]
	else if n < k then []
  else if n == k then repeat 1 k
  else if n == (k+1) then []
	else if (n mod 2) == 1 && (k mod 2) == 0 then []
	else
		let o = n mod k in
		let d = n / k in
		if (o mod 2) == 0 then	(d + o) :: repeat d (k-1)
		else if d == 1 then []
		else
			repeat2 (o + k) (d - 1) k;;

let do_one () =
    let n = gr () in
    let k = gr () in
		let suml = summands n k in
		if (List.length suml) == 0 then print_string "NO\n"
		else
			begin
				print_string "YES\n";
				printlisti suml;
				print_string "\n"
			end;;

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

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

main();;


(** val add : int -> int -> int **)

let rec add = Int64.add

(** val mul : int -> int -> int **)

let rec mul = Int64.mul

let rec leq x y = Int64.compare x y <= 0

module Nat =
 struct
 end

(** val can_distribute : int -> int -> int -> bool **)

let can_distribute r b d =
  (&&) ((<=) b (mul r (add d (Int64.one))))
    ((<=) r (mul b (add d (Int64.one))))

let rec solve = function
  | 0 -> ()
  | n -> let (r,b,d) = Scanf.scanf "%d %d %d\n" (fun a b c -> (Int64.of_int a, Int64.of_int b,Int64.of_int c)) in
         let ans = if can_distribute r b d then "YES" else "NO" in
         print_endline ans;
         solve (n-1)

let t = read_int();;
solve t;;

(** val add : int64 -> int64 -> int64 **)

let rec add = Int64.add

(** val mul : int64 -> int64 -> int64 **)

let rec mul = Int64.mul

module Nat =
 struct
  (** val leb : int64 -> int64 -> bool **)

  let rec leb = (fun x y -> Int64.compare x y <= 0)
 end

(** val can_distribute : int64 -> int64 -> int64 -> bool **)

let can_distribute r b d =
  if Nat.leb b
       (mul r (add d ((fun x -> Int64.succ x) Int64.zero)))
  then Nat.leb r
         (mul b (add d ((fun x -> Int64.succ x) Int64.zero)))
  else false

let rec solve = function
  | 0 -> ()
  | n -> let (r,b,d) = Scanf.scanf "%d %d %d\n" (fun a b c -> (Int64.of_int a, Int64.of_int b,Int64.of_int c)) in
         let ans = if can_distribute r b d then "YES" else "NO" in
         print_endline ans;
         solve (n-1)

let t = read_int();;
solve t;;

(** val add : int -> int -> int **)

let rec add = (+)

(** val mul : int -> int -> int **)

let rec mul = ( * )

module Nat =
 struct
 end

(** val can_distribute : int -> int -> int -> bool **)

let can_distribute r b d =
  (&&) ((<=) b (mul r (add d (Pervasives.succ 0))))
    ((<=) r (mul b (add d (Pervasives.succ 0))))

let rec solve = function
  | 0 -> ()
  | n -> let (r,b,d) = Scanf.scanf "%d %d %d\n" (fun a b c -> (a,b,c)) in
         let ans = if can_distribute r b d then "YES" else "NO" in
         print_endline ans;
         solve (n-1)

let t = read_int();;
solve t;;


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

let () =
  let n, q = Scanf.scanf "%d %d\n" (fun x y -> x, y) in
  let a = Array.init n (fun _ -> Scanf.scanf "%Ld " (fun x -> x)) in
  let max = ref a.(0)in
  let max_idx = ref 0 in
  for i = 1 to n-1 do
    if a.(i) > !max then begin max := a.(i); max_idx := i end;
  done;
  let b = Array.make (n-1) 0L in
  for i = 0 to n- !max_idx - 2 do
    b.(i) <- a.(!max_idx + 1 + i);
  done;
  let first = ref a.(0) in
  let c = Array.make !max_idx (a.(0), a.(1)) in
  for i = 1 to !max_idx do
    c.(i-1) <- (!first, a.(i));
    if a.(i) <= !first then
      b.(n- !max_idx - 2 + i) <- a.(i)
    else begin
      b.(n- !max_idx - 2 + i) <- !first;
      first := a.(i);
    end;
  done;

  for i = 1 to q do
    let m = Scanf.scanf "%Ld " (fun x -> x) in
    if m > Int64.of_int !max_idx then begin
      let r = Int64.rem (m -- Int64.of_int !max_idx -- 1L) (Int64.of_int (n-1)) |> Int64.to_int in
      Printf.printf "%Ld %Ld\n" !max b.(r)
    end
    else
      let m = Int64.to_int m in
      let x, y = c.(m-1) in
      Printf.printf "%Ld %Ld\n" x y;
  done
;;

let readInt () = Scanf.scanf " %d" (fun x -> x)
let readInt64 () = Scanf.scanf " %Ld" (fun x -> x)

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

type solver = {n: int; a: int array; k: int; cyca: int array}

let precomp (n, a) =
  (* No fold_*i :( *)
  let maxi = ref 0 in
  for i=1 to n-1 do
    if a.(i) > a.(!maxi) then maxi := i
  done;
  let maxi = !maxi in
  let cyca = Array.make (maxi + n) (-1) in
  Array.blit a 0 cyca 0 n;
  for i=0 to maxi-1 do
    let x = cyca.(i) in
    let y = cyca.(i+1) in
    let x = max x y and y = min x y in
    cyca.(i+1) <- x;
    cyca.(i+n) <- y
  done;
  {n; a; k=maxi; cyca}

let query {n; a; k; cyca} m =
  let m =
    let open Int64 in
    let n = of_int n in
    let k = of_int k in
    to_int (if m < k then m else (m -- k) %% (n -- 1L) ++ k)
  in
  if m < k then (cyca.(m), a.(m+1))
  else (cyca.(k), cyca.(m+1))

let main () =
  let n = readInt () in
  let q = readInt () in
  let a = Array.init n (fun _ -> readInt ()) in
  let s = precomp (n, a) in
  for i=0 to q-1 do
    let m = readInt64 () -- 1L in
    let (x, y) = query s m in
    Printf.printf "%d %d\n" x y
  done

let () = main ()


open Printf
open Scanf

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

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

let () =
  let n = read_int() in
  let m = read_int() in
  let k = read_int() in
  let a = Array.init (n+1) (fun i -> if i=0 then 0L else read_long()) in
  let op = Array.init (m+1) (fun i -> if i=0 then (0,0,0L) else
			       let l = read_int() in
			       let r = read_int() in
			       let d = read_long() in
				 (l,r,d)
			    ) in
  let q = Array.init (k+1) (fun i -> if i=0 then (0,0) else
			      let l = read_int() in
			      let r = read_int() in
				(l,r)
			   ) in

  let opcount = Array.make (m+2) 0L in
  let rcount = Array.make (n+2) 0L in

    for i=1 to k do
      let (l,r) = q.(i) in
      opcount.(l) <- opcount.(l) ++ 1L;
      opcount.(r+1) <- opcount.(r+1) -- 1L;
    done;

    for i=1 to m do
      opcount.(i) <- opcount.(i-1) ++ opcount.(i)
    done;

    for i=1 to m do
      let (l,r,d) = op.(i) in
	rcount.(l) <- rcount.(l) ++ (d ** opcount.(i));
	rcount.(r+1) <- rcount.(r+1) -- (d ** opcount.(i));
    done;

    for i=1 to n do
      rcount.(i) <- rcount.(i-1) ++ rcount.(i);
    done;

    for i=1 to n do
      if i>1 then printf " ";
      printf "%Ld" (rcount.(i) ++ a.(i))
    done;

    print_newline()

open Printf;;
open Scanf;;
let getll()=bscanf Scanning.stdib " %Ld " (fun x->x);;
let getint()=bscanf Scanning.stdib " %d " (fun x->x);;
let ( ** ) a b=Int64.mul a b;;
let (++) a b=Int64.add a b;;
let (--) a b=Int64.sub a b;;
let (//) a b=Int64.div a b;;

let n=getint();;
let m=getint();;
let k=getint();;
let aa=Array.init (n+1) (function 0->0L|_->getll());;
let o1=Array.init (m+1) (function 0->(0,0,0)|_ -> let a=getint()in let b=getint()in let c=getint() in (a,b,c));;
let o2=Array.init (k+1) (function 0->(0,0)|_ -> let a=getint()in let b=getint()in (a,b));;
let a=Array.create (m+2) 0;;
let b=Array.create (n+2) 0L;;
for i=1 to k do
	let l=fst o2.(i)
	and r=snd o2.(i) in
		a.(l)<-a.(l)+1;
		a.(r+1)<-a.(r+1)-1;
done;;
let sumup a=let len=Array.length a in for i=1 to len-1 do a.(i)<-a.(i-1)+a.(i); done;;
let sumupl a=let len=Array.length a in for i=1 to len-1 do a.(i)<-a.(i-1)++a.(i); done;;
sumup a;;
let ff c=function (l,r,x)->let d=Int64.of_int(x)**Int64.of_int(c)in b.(l)<-b.(l)++d;b.(r+1)<-b.(r+1)--d;;
for i=1 to m do ff a.(i) o1.(i); done;;
sumupl b;;
for i=1 to n do
	if i>1 then print_char ' ';
	printf "%Ld" (aa.(i)++b.(i));
done;;
print_newline();;


let scan_int () = Scanf.scanf " %d" (fun x -> x);;
let scan_string () = Scanf.scanf " %s" (fun x -> x);;
let psp () = print_string " ";;
let b2i x = if x == true then 1 else 0;;

module type GRAPH_LABELS =
  sig
    type label
  end;;

(* Typ modułu implementującego grafy o wierzchołkach numerowanych od 0 do n-1. *)
module type GRAPH =
  sig
    (* Etykiety krawędzi. *)
    type label

    (* Typ grafów o wierzchołkach typu 'a. *)
    type graph

    (* Pusty graf. *)
    val init : int -> graph

    (* Rozmiar grafu *)
    val size : graph -> int

    (* Dodanie krawędzi skierowanej łączącej dwa wierzchołki. *)
    val insert_directed_edge : graph -> int -> int -> label -> unit

    (* Dodanie krawędzi nieskierowanej (dwukierunkowej) łączącej dwa wierzchołki. *)
    val insert_edge : graph -> int -> int -> label -> unit

    (* Lista incydencji danego wierzchołka. *)
    val neighbours :  graph -> int -> (int*label) list
  end;;

module Graph = functor (L : GRAPH_LABELS) ->
(struct
  (* Etykiety krawędzi. *)
  type label = L.label

  (* Typ grafów - tablica list sąsiedztwa. *)
  type graph = {n : int; e : (int*label) list array}

  (* Pusty graf. *)
  let init s = {n = s; e = Array.make s []}

  (* Rozmiar grafu. *)
  let size g = g.n

  (* Dodanie krawędzi skierowanej łączącej dwa wierzchołki. *)
  let insert_directed_edge g x y l =
    assert ((x<>y)  && (x >= 0) && (x < size g) && (y >= 0) && (y < size g));
    g.e.(x) <- (y,l)::g.e.(x)

  (* Dodanie krawędzi łączącej dwa (istniejące) wierzchołki. *)
  let insert_edge g x y l =
    insert_directed_edge g x y l;
    insert_directed_edge g y x l

  (* Lista incydencji danego wierzchołka. *)
  let neighbours g x =
    g.e.(x)
end : GRAPH with type label = L.label);;

module IntLabel =
  struct
    type label = int
  end;;

module G = Graph(IntLabel);;

open G;;

let n = scan_int();;
let m = scan_int();;

let g = G.init(n);;
let values = Array.make n 0;;

for i = 0 to n - 1 do
  let x = scan_int() in
  values.(i) <- x;
done;

for i = 0 to m - 1 do
  let (x, y) = (scan_int() - 1, scan_int() - 1) in
  insert_edge g x y 1;
done;;

let vis = Array.make n false;;

let rec dfs x =
  vis.(x) <- true;
  let res = ref values.(x) in
  List.iter (fun(y, _) ->
    if vis.(y) = false then res := min (!res) (dfs y)
  ) (neighbours g x);
  !res;;

open Int64;;
let sumek = ref zero;;

for i = 0 to n - 1 do
  if vis.(i) = false then sumek := add (!sumek) (Int64.of_int(dfs i));
done;

print_string(Int64.to_string(!sumek));;

open Int64;;
let scan_int () = Scanf.scanf " %d" (fun x -> x);;
let n = scan_int() and m= scan_int() in let odw = Array.make n 0 and wyn = ref zero
and p = Array.init n (fun x ->x) and w = Array.init n (fun x->scan_int())  in
let rec find x =
  if p.(x) = x then x
  else begin
	p.(x) <- find p.(x);
    p.(x)
  end
in let uni x y =
  let u = find x and v = find y in
  if u <> v then
    if w.(u) < w.(v) then p.(v) <- u else p.(u) <- v
in begin for i=1 to m do
  let u = scan_int () and v = scan_int() in uni (u-1) (v-1)
done;
for i=0 to (n-1) do
  if odw.(find i) = 0 then begin
    odw.(find i) <- 1; wyn := add (!wyn) (Int64.of_int(w.(find i))) end
done;
print_string(Int64.to_string(!wyn))
end

open Printf
open Scanf

let ( ++ ) a b = Int64.add a b

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

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

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

  let cost = Array.init n (fun i -> long(read_int())) in
  let comp = Array.make n (-1) in

  let graph = Array.make n [] in

  for i=0 to m-1 do
    let (u,v) = read_pair() in
    let (u,v) = (u-1,v-1) in
    graph.(u) <- v::graph.(u);
    graph.(v) <- u::graph.(v)
  done;

  let rec dfs u p c =
    if comp.(u) = -1 then (
      comp.(u) <- c;
      List.iter (fun v -> if v<>p then dfs v u c) graph.(u)
    )
  in

  let rec loop i c = if i=n then c else (
    if comp.(i) = -1 then (
      dfs i (-1) c;
      loop (i+1) (c+1)
    ) else loop (i+1) c
  ) in

  let ncomps = loop 0 0 in

  let mincomp = Array.make ncomps 1_000_000_001L in

  for i=0 to n-1 do
    mincomp.(comp.(i)) <- min cost.(i) mincomp.(comp.(i))
  done;

  let answer = sum 0 (ncomps-1) (fun i -> mincomp.(i)) in

  printf "%Ld\n" answer

let scan_int () = Scanf.scanf " %d" (fun x -> x);;
let n = scan_int() and m= scan_int() in let odw = Array.make n 0 and wyn = ref 0
and p = Array.init n (fun x ->x) and w = Array.init n (fun x->scan_int())  in
let rec find x =
  if p.(x) = x then x
  else begin
	p.(x) <- find p.(x);
    p.(x)
  end
in let uni x y =
  let u = find x and v = find y in
  if u <> v then
    if w.(u) < w.(v) then p.(v) <- u else p.(u) <- v
in begin for i=1 to m do
  let u = scan_int () and v = scan_int() in uni (u-1) (v-1)
done;
for i=0 to (n-1) do
  if odw.(find i) = 0 then begin
    odw.(find i) <- 1; wyn := !wyn + w.(find i) end
done;
print_int (!wyn)
end