(*
   Imagine the following graph.  There's a node for every prime, and in
   the node an exponent of that prime is stored.  There is an edge from
   prime p to each of the prime factors of p-1.  EG from 13 there are two
   edges to 2 and one edge to 3.  (2*2*3=12).

   To compute Phi of a number represented in this way, you do the following.
   For each non-zero exponent (simultaneously) decrement its exponentent
   by 1. Then for each successor of those that you decremented, you increment
   the exponent by 1.

   The problem here is to be able to iterate the Phi operator a huge
   number of times efficiently.

   Call a state "normal" if there is no pair of nodes p-->q such that
   p is non-zero while q is zero.  The beauty of a normal state is
   that if you apply the Phi operator to it, it remains normal.

   Consider what happens to some node when you hit it with the Phi
   operator in a normal state.  Exactly the same thing happens every
   time locally to it until all of its sources dry up.

   This means we can apply the Phi operator to various different nodes
   in any order we want.  So we apply it in topological order
   (i.e. largest first).  It's easy to apply the operator all k times
   to a node in O(number successors of it).

   It remains to explain how to get the system into a normal state.
   We simply iterate the Phi operator on it until it becomes normal.
   Then we reduce k by the number of times we had to apply the
   operator.  The number of times we have to do this is the length of
   the longest path in the graph.  This is at most logarithmic in the
   largest prime (a pessimistic estimate).

   (There is also an alternative algorithm that "protects" some amount
   of the exponent from being depleted.  You are effectively computing
   the number of iterations until a node becomes non-zero.  That's the
   amount of protection it gets.  This can be computed as you go along.
   Some of the other shorter solutions do it this way.)

                  Danny Sleator, March 2014
*)

open Printf
open Scanf

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

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

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 () =
  let m = read_int () in
  let p = Array.make m 0 in
  let a = Array.make m 0L in
  for i=0 to m-1 do
    p.(i) <- read_int();
    a.(i) <- read_long()
  done;

  let k = read_long () in

  let max_p = p.(m-1) in

  let sieve = Array.make (max_p+1) 0 in
(* sieve.(n) is the smallest prime that divides n.
   sieve.(n) = n iff n is prime. *)

  for i=2 to max_p do
    if sieve.(i) = 0 then
      let rec loop j = if j <= max_p then (
	sieve.(j) <- i;
	loop (j+i);
      ) in
      loop (i)
  done;

  let e = Array.make (max_p+1) 0L in

  for i=0 to m-1 do
    e.(p.(i)) <- a.(i)
  done;

  let is_normal () =
(*    let process p =
      let rec loop r = r=1 || (e.(sieve.(r)) <> 0L && loop (r/sieve.(r))) in
      loop (p-1);
    in *)

    let process p =
      let rec loop r = if r=1 then true
	else if e.(sieve.(r)) = 0L then false else loop (r/sieve.(r))
      in
      loop (p-1);
    in

    let rec loop p = if p > max_p then true
      else if (sieve.(p) <> p || e.(p) = 0L) then loop (p+1)
      else process p && loop (p+1)
    in
    loop 2
  in

  let step () =
    let process p =
      let rec loop r = if r>1 then
	  let s = sieve.(r) in
	  e.(s) <- e.(s) ++ 1L;
	  loop (r/s)
      in
      loop (p-1);
      e.(p) <- e.(p) -- 1L;
    in

    for p=2 to max_p do
      if sieve.(p) = p && e.(p) > 0L then process p
    done;
  in

  let rec steploop k = if k=0L || is_normal() then k else (
    step();
    steploop (k--1L)
  )
  in

  let k = steploop k in

  let elim p =
    let d = min e.(p) k in
    e.(p) <- e.(p) -- d;
      (* now add d to the exponents of all the prime factors of p-1 *)
    let rec loop r = if r=1 then () else
	let s = sieve.(r) in
	e.(s) <- e.(s) ++ d;
	loop (r/s)
    in
    loop (p-1);
  in

  for p=max_p downto 2 do
    if sieve.(p) = p then elim p
  done;

  let np = sum 2 max_p (fun i -> if e.(i) <> 0L then 1 else 0) in

  printf "%d\n" np;

  for i=2 to max_p do
    if e.(i) <> 0L then printf "%d %Ld\n" i e.(i)
  done

open Printf
open Scanf

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

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

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 () =
  let m = read_int () in
  let p = Array.make m 0 in
  let a = Array.make m 0L in
  for i=0 to m-1 do
    p.(i) <- read_int();
    a.(i) <- read_long()
  done;

  let k = read_long () in

  let max_p = p.(m-1) in

  let sieve = Array.make (max_p+1) 0 in
(* sieve.(n) is the smallest prime that divides n.
   sieve.(n) = n iff n is prime. *)

  for i=2 to max_p do
    if sieve.(i) = 0 then
      let rec loop j = if j <= max_p then (
	sieve.(j) <- i;
	loop (j+i);
      ) in
      loop (i)
  done;

  let e = Array.make (max_p+1) 0L in

  for i=0 to m-1 do
    e.(p.(i)) <- a.(i)
  done;

  let is_normal () =
    let process p =
      let rec loop r = r=1 || (e.(sieve.(r)) <> 0L && loop (r/sieve.(r))) in
      loop (p-1);
    in

    let rec loop p = if p = max_p then true
      else if (sieve.(p) <> p || e.(p) = 0L) then loop (p+1)
      else process p && loop (p+1)
    in
    loop 2
  in

  let step () =
    let process p =
      let rec loop r = if r>1 then
	  let s = sieve.(r) in
	  e.(s) <- e.(s) ++ 1L;
	  loop (r/s)
      in
      loop (p-1);
      e.(p) <- e.(p) -- 1L;
    in

    for p=2 to max_p do
      if sieve.(p) = p && e.(p) > 0L then process p
    done;
  in

  let rec steploop k = if k=0L || is_normal() then k else (
    step();
    steploop (k--1L)
  )
  in

  let k = steploop k in

  let elim p =
    let d = min e.(p) k in
    e.(p) <- e.(p) -- d;
      (* now add d to the exponents of all the prime factors of p-1 *)
    let rec loop r = if r=1 then () else
	let s = sieve.(r) in
	e.(s) <- e.(s) ++ d;
	loop (r/s)
    in
    loop (p-1);
  in

  for p=max_p downto 2 do
    if sieve.(p) = p then elim p
  done;

  let np = sum 2 max_p (fun i -> if e.(i) <> 0L then 1 else 0) in

  printf "%d\n" np;

  for i=2 to max_p do
    if e.(i) <> 0L then printf "%d %Ld\n" i e.(i)
  done


Scanf.scanf "%d %d %d" (fun n k q ->
let qs,qt = Queue.create(), Queue.create() in
let dat = Array.make n (0,0) in
let presum=Array.make 211111 0 in
let cnt = ref 0 in
  for i = 0 to n-1 do
    Scanf.scanf "\n%d %d" (fun x y ->dat.(i) <-(x,y))
  done;
  Array.sort (fun (xh,_) (yh,_) -> xh-yh) dat;
  Array.iter (fun (h,_) -> Queue.push h qs) dat;
  Array.sort (fun (_,xt) (_,yt) -> xt-yt) dat;
  Array.iter (fun (_,t) -> Queue.push t qt) dat;
  for i = 1 to 200000 do
    (*Printf.printf "i=%d\n" i;*)
    while not (Queue.is_empty qs) && Queue.top qs <= i do

      (*Printf.printf "qs:%d\n"*)   ( Queue.pop qs );
         cnt:= !cnt +1
      done;
    while not (Queue.is_empty qt) && Queue.top qt < i do
      (*Printf.printf "qt:%d\n"*)   ( Queue.pop qt );
         cnt:= !cnt -1
    done;
    presum.(i) <- (if !cnt>=k then 1 else 0) +presum.(i-1)
  done;
  for i= 1 to q do
    Scanf.scanf "\n%d %d" (fun l r ->
      Printf.printf "%d\n" (presum.(r)-presum.(l-1))
    )
  done)

Scanf.scanf "%d %d %d" (fun n k q ->
let maxn=200001 in
let add,sub = (Array.make maxn 0) ,(Array.make maxn 0) in
let presum=Array.make 211111 0 in
let cnt = ref 0 in
  for i = 0 to n-1 do
    Scanf.scanf "\n%d %d" (fun x y ->
    add.(x) <- add.(x) + 1 ;
    sub.(y) <- sub.(y) + 1 )
  done;
  for i = 1 to maxn-1 do
    cnt:=!cnt+add.(i)-sub.(i-1);
    presum.(i) <- (if !cnt>=k then 1 else 0) +presum.(i-1)
  done;
  for i= 1 to q do
    Scanf.scanf "\n%d %d" (fun l r ->
      Printf.printf "%d\n" (presum.(r)-presum.(l-1))
    )
  done)


let print int = Printf.printf "%d\n" int

let same time prev =
    match time, prev with
        | (h, m), (hh, mm) ->
            if h = hh && m = mm
            then
                true
            else
                false

let () =
    let n = Scanf.scanf "%d " (fun n -> n) in
    let rec loop i prev count max  =
        if i < n
        then
            let time = Scanf.scanf "%d %d " (fun h m -> (h, m)) in
            if same time prev
            then
                let max_upd = Pervasives.max (count + 1) max in
                loop (i + 1) time (count + 1) max_upd
            else
                loop (i + 1) time 1 max
        else
            print max
    in loop 0 (99, 99) 1 1

let input () =
    let n = Scanf.scanf "%d " (fun n -> n) in
    let rec loop i list =
        if i < n
        then
            let str = Scanf.scanf "%s %s " (fun a b -> String.concat "" [a; b]) in
            loop (i + 1) (str :: list)
        else
            List.rev list
    in loop 0 []

let print int = Printf.printf "%d\n" int

let solve list =
    let rec loop rest prev count max =
        match rest with
        | (head :: tail) ->
            if String.compare head prev = 0
            then
                let max_upd = Pervasives.max (count + 1) max in
                loop tail head (count + 1) max_upd
            else
                loop tail head 1 max
        | [] -> max
    in loop list "start" 1 1

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

let input () =
    let n = Scanf.scanf "%d " (fun n -> n) in
    let rec loop i list =
        if i < n
        then
            let time = Scanf.scanf "%d %d " (fun h m -> (h, m)) in
            loop (i + 1) (time :: list)
        else
            list
    in loop 0 []

let print int = Printf.printf "%d\n" int

let solve list =
    let rec loop rest prev count max =
        match rest, prev with
        | ((h, m) as head :: tail), (hh, mm) ->
            if h = hh && m = mm
            then
                let max_upd = Pervasives.max (count + 1) max in
                loop tail head (count + 1) max_upd
            else
                loop tail head 1 max
        | [], _ -> max
    in loop list (99, 99) 1 1

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

(* Codeforces 237A *)

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

let solve n =
  let rec inner n prev_time count max_count =
    if (n == 0) then max count max_count
    else
      begin
	let time = read_time() in
	if (time = prev_time)
	then inner (n-1) time (count+1) max_count
	else
	  if (count > max_count)
	  then inner (n-1) time 1 count
	  else inner (n-1) time 1 max_count
      end
  in
  inner n (-1, -1) 0 0

let _ =
  Printf.printf "%d\n" (Scanf.scanf "%d\n" solve)

(* Codeforces 237A *)

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

let solve n =
  let rec inner n prev_time count max_count =
    if (n == 0) then max_count
    else
      begin
	let time = read_time() in
	let max_count =
	  if (max_count < count)
	  then count
	  else max_count
	in
	let count =
	  if (time = prev_time)
	  then count+1
	  else 1
	in
	inner (n-1) time count max_count
      end
  in
  inner n (-1, -1) 0 0

let _ =
  Printf.printf "%d\n" (Scanf.scanf "%d\n" solve)


let list_of_string s =
  let rec loc i acc =
    if i < 0
    then acc
    else loc (i - 1) (String.get s i :: acc)
  in
  loc (String.length s - 1) []

let combine s t =
  let rec loc acc s t =
    match s, t with
    | [],      []      -> acc
    | x :: rs, y :: rt -> loc ((x, y) :: acc) rs rt
    | _,       _       -> invalid_arg ""
  in
  List.rev (loc [] s t)

exception Impossible

let solve a b =

  let la = list_of_string a
  and lb = list_of_string b in
  let l = combine la lb in
  let n = List.length l in

  let diff =
    List.fold_left
      (fun acc (ca, cb) -> if ca <> cb then acc + 1 else acc)
      0
      l
  in
  if diff mod 2 <> 0 then raise Impossible;

  let ans = String.create n in
  let rec loc i j = function
    | [] -> ()
    | (ca, cb) :: rest ->
      if ca = cb
      then (String.set ans i ca; loc (i + 1) j rest)
      else
        begin
          if j < diff / 2
          then (String.set ans i ca; loc (i + 1) (j + 1) rest)
          else (String.set ans i cb; loc (i + 1) (j + 1) rest)
        end
  in
  loc 0 0 l;
  ans

(* let mk n = String.init n (fun _ -> if Random.bool () then '0' else '1') *)

let _ =
  (* print_endline (mk 100000); *)
  (* print_endline (mk 100000); *)

  let a = read_line ()
  and b = read_line () in
  let ans =
    try solve a b
    with Impossible -> "impossible"
  in
  print_endline ans

let _ =
	let (s1,s2) = Scanf.scanf "%s %s" (fun i j -> (i,j)) in
	let res = String.copy s1 and flg = ref true in
	for i=0 to String.length res - 1 do
		if res.[i] <> s2.[i]
			then begin
				flg := not !flg;
				if !flg
					then res.[i] <- s2.[i]
			      end;
	done;
	Printf.printf "%s" (if !flg then res else "impossible")

let list_of_string s =
  let rec loc i acc =
    if i < 0
    then acc
    else loc (i - 1) (String.get s i :: acc)
  in
  loc (String.length s - 1) []

let combine s t =
  let rec loc acc s t =
    match s, t with
    | [],      []      -> acc
    | x :: rs, y :: rt -> loc ((x, y) :: acc) rs rt
    | _,       _       -> invalid_arg ""
  in
  List.rev (loc [] s t)

exception Impossible

let solve a b =

  let la = list_of_string a
  and lb = list_of_string b in
  let l = combine la lb in
  let n = List.length l in

  let diff =
    List.fold_left
      (fun acc (ca, cb) -> if ca <> cb then acc + 1 else acc)
      0
      l
  in
  if diff mod 2 <> 0 then raise Impossible;

  let ans = String.create n in
  List.iteri
    (fun i (ca, cb) ->
       if i < n / 2
       then String.set ans i ca
       else String.set ans i cb)
    l;
  ans

(* let mk n = String.init n (fun _ -> if Random.bool () then '0' else '1') *)

let _ =
  (* print_endline (mk 100000); *)
  (* print_endline (mk 100000); *)

  let a = read_line ()
  and b = read_line () in
  let ans =
    try solve a b
    with Impossible -> "impossible"
  in
  print_endline ans


open Printf
open Scanf

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

let merge_cycles n goal ncycles p mark =
  let rec build_swaps i ncyc ac = if ncyc=goal then List.rev ac else
      if mark.(i) = 0 then build_swaps (i+1) ncyc ac else
	let rec loop j = if j<>i then (
	  mark.(j) <- 0;
	  loop p.(j)
	)
	in
	loop p.(i);
	build_swaps (i+1) (ncyc-1) ((0,i)::ac)
  in
  build_swaps 1 ncycles []

let split_cycles n goal ncycles p mark =
  let rec build_swaps i ncyc ac = if ncyc=goal then List.rev ac else
      if p.(i) = i then build_swaps (i+1) ncyc ac else
	let rec find_smallest j ac = if j=i then ac else
	    find_smallest p.(j) (min ac j)
	in
	let j = find_smallest p.(i) p.(i) in
	let (oi, oj) = (p.(i), p.(j)) in
	p.(i) <- oj;
	p.(j) <- oi;
	build_swaps i (ncyc+1) ((i,j)::ac)
  in
  build_swaps 0 ncycles []

let () =
  let n = read_int () in
  let p = Array.init n (fun _ -> read_int()-1) in
  let goal = n - (read_int ()) in

  let mark = Array.make n (-1) in

  let rec dfs i c = if mark.(i) < 0 then (
    mark.(i) <- c;
    dfs p.(i) c
  ) in

  let rec count_cycles c i =
    if i=n then c
    else if mark.(i) >= 0 then count_cycles c (i+1) else (
      dfs i c;
      count_cycles (c+1) (i+1)
    )
  in

  let ncycles = count_cycles 0 0 in

  printf "%d\n" (abs (ncycles-goal));

  let answer =
    (if ncycles >= goal then merge_cycles else split_cycles) n goal ncycles p mark
  in

  List.iter (fun (i,j) -> printf "%d %d " (i+1) (j+1)) answer;
  print_newline();

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 read_int () = bscanf Scanning.stdib " %d " (fun x -> x)

let split_cycles n m ncycles p mark =
  failwith "split_cycles called"

let merge_cycles n goal ncycles p mark =
  (* The algorithm is to find the smallest element not in
     cycle 0 and swap it with 0, thus putting it in cycle 0.
     The mark array already has 0s in it at cycle 0.  This is O(n).
  *)
  let rec build_swaps i ncyc ac = if ncyc=goal then List.rev ac else
      if mark.(i) = 0 then build_swaps (i+1) ncyc ac else
	let rec loop j = if j=i then () else (
	  mark.(j) <- 0;
	  loop p.(j)
	)
	in
	loop p.(i);
	build_swaps (i+1) (ncyc-1) ((0,i)::ac)
  in

  build_swaps 1 ncycles []

let split_cycles n goal ncycles p mark =
  let rec build_swaps i ncyc ac = if ncyc=goal then List.rev ac else
      if p.(i) = i then build_swaps (i+1) ncyc ac else
	let rec find_smallest j ac = if j=i then ac else
	    find_smallest p.(j) (min ac j)
	in
	let j = find_smallest p.(i) p.(i) in
	let (oi, oj) = (p.(i), p.(j)) in
	p.(i) <- oj;
	p.(j) <- oi;
	build_swaps i (ncyc+1) ((i,j)::ac)
  in
  build_swaps 0 ncycles []

let () =
  let n = read_int () in
  let p = Array.init n (fun _ -> read_int()-1) in
  let m = read_int () in
  let goal = n-m in (* number of cycles of the goal *)

  let mark = Array.make n (-1) in

  let rec dfs i c = if mark.(i) < 0 then (
    mark.(i) <- c;
    dfs p.(i) c
  ) in

  let rec count_cycles c i =
    if i=n then c
    else if mark.(i) >= 0 then count_cycles c (i+1) else (
      dfs i c;
      count_cycles (c+1) (i+1)
    )
  in

  let ncycles = count_cycles 0 0 in

  printf "%d\n" (abs (ncycles-goal));

  let answer =
    (if ncycles >= goal then merge_cycles else split_cycles) n goal ncycles p mark
  in

  List.iter (fun (i,j) -> printf "%d %d " (i+1) (j+1)) answer;
  print_newline();


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

type container = {
  cap : int;
  id: int
};;

let comp contA contB =
  - (contA.cap - contB.cap);;

let rec get_best tab1 tab2 size = match (tab1,tab2) with
  | ([],[])
    -> []
  | ([], _) when size < 2
    -> []
  | (_, _) when size = 0
    -> []
  | (first :: tail, _) when size = 1
    -> [first]
  | ([], first :: tail)
    -> first :: get_best [] tail (size - 2)
  | (first :: tail, [])
    -> first :: get_best tail [] (size - 1)
  | (first1 :: [], first2 :: tail2)
    -> if first1.cap >= first2.cap then
      first1 :: get_best [] tab2 (size - 1)
    else
      first2 :: get_best tab1 tail2 (size - 2)
  | (first1 :: second1 :: tail1, first2 :: tail2)
    -> if first1.cap + second1.cap >= first2.cap then
      first1 :: get_best (second1::tail1) tab2 (size - 1)
    else
      first2 :: get_best tab1 tail2 (size - 2);;

(*#trace get_best;;*)

let rec read_input index left = match left with
  | 0 -> [],[]
  | n ->
    let size = read_int () in
    let cap = read_int () in
    let cont = {cap = cap; id = index} in
    let size1,size2 = read_input (index + 1) (left - 1) in
    if size = 1 then
      (cont :: size1, size2)
    else
      (size1, cont :: size2);;

let nbVehi = read_int () in
let maxV = read_int () in
let unsort1,unsort2 = (read_input 1 nbVehi)  in
let tab1 = List.sort comp unsort1 in
let tab2 = List.sort comp unsort2 in
let res = get_best tab1 tab2 maxV in
begin
  let maxC = List.fold_left (fun sum cont -> sum + cont.cap) 0 res in
  Printf.printf "%d\n" maxC;
  List.iter (fun cont -> Printf.printf "%d\n" cont.id) res
end;;

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

let () =
  let n = read_int 0 in
  let v = read_int 0 in
  let one = Array.make n (0,0)
  and two = Array.make n (0,0) in
  let rec input i none ntwo =
    if i >= n then
      none,ntwo
    else (
      let t = read_int 0 in
      let c = read_int 0 in
      if t = 1 then (
        one.(none) <- (c,i);
        input (i+1) (none+1) ntwo
      ) else (
        two.(ntwo) <- (c,i);
        input (i+1) none (ntwo+1)
      )
    ) in
  let none,ntwo = input 0 0 0 in
  Array.sort (fun x y -> compare y x) one;
  Array.sort (fun x y -> compare y x) two;

  let ans = ref 0
  and ansp = ref 0
  and ansq = ref 0
  and qs = ref 0
  and ps = Array.make (none+1) 0 in
  for i = 0 to none-1 do
    ps.(i+1) <- ps.(i)+fst one.(i)
  done;
  for i = 0 to min (v/2) ntwo do
    let rest = min (v-2*i) none in
    if ps.(rest) + !qs > !ans then (
      ans := ps.(rest) + !qs;
      ansp := rest;
      ansq := i
    );
    if i < ntwo then
      qs := !qs+fst two.(i)
  done;

  Printf.printf "%d\n" !ans;
  for i = 0 to !ansp-1 do
    Printf.printf "%d " (snd one.(i)+1)
  done;
  for i = 0 to !ansq-1 do
    Printf.printf "%d " (snd two.(i)+1)
  done

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

type container = {
  cap : int;
  id: int
};;

let comp contA contB =
  - (contA.cap - contB.cap);;

let rec get_best tab1 tab2 size = match (tab1,tab2) with
  | ([],[])
    -> []
  | ([], _) when size < 2
    -> []
  | (_, _) when size = 0
    -> []
  | (first :: tail, _) when size = 1
    -> [first]
  | ([], first :: tail)
    -> first :: get_best [] tail (size - 2)
  | (first :: tail, [])
    -> first :: get_best tail [] (size - 1)
  | (first1 :: second1 :: [], first2 :: tail2) when size > 2
    -> first2 :: get_best tab1 tail2 (size - 2)
  | (first1 :: [], first2 :: tail2)
    -> if first1.cap >= first2.cap then
      first1 :: get_best [] tab2 (size - 1)
    else
      first2 :: get_best tab1 tail2 (size - 2)
  | (first1 :: second1 :: tail1, first2 :: tail2)
    -> if first1.cap + second1.cap >= first2.cap then
      first1 :: second1 :: get_best tail1 tab2 (size - 2)
    else
      first2 :: get_best tab1 tail2 (size - 2);;

(*#trace get_best;;*)

let rec read_input index left = match left with
  | 0 -> [],[]
  | n ->
    let size = read_int () in
    let cap = read_int () in
    let cont = {cap = cap; id = index} in
    let size1,size2 = read_input (index + 1) (left - 1) in
    if size = 1 then
      (cont :: size1, size2)
    else
      (size1, cont :: size2);;

let nbVehi = read_int () in
let maxV = read_int () in
let unsort1,unsort2 = (read_input 1 nbVehi)  in
let tab1 = List.sort comp unsort1 in
let tab2 = List.sort comp unsort2 in
let res = get_best tab1 tab2 maxV in
begin
  let maxC = List.fold_left (fun sum cont -> sum + cont.cap) 0 res in
  Printf.printf "%d\n" maxC;
  List.iter (fun cont -> Printf.printf "%d\n" cont.id) res
end;;

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

type container = {
  cap : int;
  id: int
};;

let comp contA contB =
  - (contA.cap - contB.cap);;

let rec get_best tab1 tab2 size = match (tab1,tab2) with
  | ([],[])
    -> []
  | ([], _) when size < 2
    -> []
  | (_, _) when size = 0
    -> []
  | (first :: [], _) when size = 1
    -> [first]
  | ([], first :: tail)
    -> first :: get_best [] tail (size - 2)
  | (first :: tail, [])
    -> first :: get_best tail [] (size - 1)
  | (first1 :: [], first2 :: tail2)
    -> if first1.cap >= first2.cap then
      first1 :: get_best [] tab2 (size - 1)
    else
      first2 :: get_best tab1 tail2 (size - 2)
  | (first1 :: second1 :: tail1, first2 :: tail2)
    -> if first1.cap + second1.cap >= first2.cap then
      first1 :: second1 :: get_best tail1 tab2 (size - 2)
    else
      first2 :: get_best tab1 tail2 (size - 2);;

(*#trace get_best;;*)

let rec read_input index left = match left with
  | 0 -> [],[]
  | n ->
    let size = read_int () in
    let cap = read_int () in
    let cont = {cap = cap; id = index} in
    let size1,size2 = read_input (index + 1) (left - 1) in
    if size = 1 then
      (cont :: size1, size2)
    else
      (size1, cont :: size2);;

let nbVehi = read_int () in
let maxV = read_int () in
let unsort1,unsort2 = (read_input 1 nbVehi)  in
let tab1 = List.sort comp unsort1 in
let tab2 = List.sort comp unsort2 in
let res = get_best tab1 tab2 maxV in
begin
  let maxC = List.fold_left (fun sum cont -> sum + cont.cap) 0 res in
  Printf.printf "%d\n" maxC;
  List.iter (fun cont -> Printf.printf "%d\n" cont.id) res
end;;

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

type container = {
  vol : int;
  size: int;
  id: int
};;

let comp contA contB =
  - (contA.vol * contB.size - contB.vol * contA.size);;

let rec get_best tab size = match tab with
  | [] -> []
  | curr_best :: suite  ->
    match size with
    | 0 -> []
    | 1 ->
      if curr_best.size == 1 then
      [curr_best]
      else
      get_best suite size
    | n ->
      curr_best :: get_best suite (size - curr_best.size);;

let rec read_input index left = match left with
  | 0 -> []
  | n ->
  let size = read_int () in
  let cap = read_int () in
  let cont = {
    size = size;
    vol = cap;
    id = index;
  } in
  cont :: read_input (index + 1) (left - 1);;

let nbVehi = read_int () in
let maxV = read_int () in
let tableau = read_input 1 nbVehi
    |> List.sort comp in
let res = get_best tableau maxV in
begin
  let maxC = List.fold_left (fun sum cont -> sum + cont.vol) 0 res in
  Printf.printf "%d\n" maxC;
  List.iter (fun cont -> Printf.printf "%d\n" cont.id) res
end;;


let calculate_table () =
  let table = Hashtbl.create 1000000 in
  for a = 0 to 500 do
    for b = 0 to 500 do
      let new_value = (a*2020 + b*2021) in
      Hashtbl.add table new_value (a, b)
    done
  done;
  table;;

let find_opt tbl n =
  try
    Some (Hashtbl.find tbl n)
  with Not_found -> None

let solution_exists n solutions =
  let maybe_sol = find_opt solutions n in
  match maybe_sol with
  | Some _ -> true
  | None -> false;;

let cases = read_int () in
let solutions = calculate_table () in
for i = 1 to cases do
  let test = read_int () in
  if solution_exists test solutions then
    print_endline "YES"
  else
    print_endline "NO"
done

module Stdlib = Pervasives
let std_inp = Stdlib.stdin;;
(* let std_inp = Stdlib.open_in "input.txt";; *)
let std_outp = Stdlib.stdout;;

let identity x = x;;

let rec repeat ((f, f_in, f_out) as call) = function
| 0 -> ()
| n ->
  let _ = f f_in f_out in
  repeat call (n - 1);;

let flags =
  let sum_limit = 1_000_000 in
  let flags = Array.make (1 + sum_limit) false in
  let safe_get pos =
    if pos < 0 then false
    else flags.(pos)
  in
  let rec fill n =
    if n > sum_limit then ()
    else
      let _ = flags.(n) <- ((safe_get (n - 2020)) || (safe_get(n - 2021))) in
      fill (n + 1)
  in
  let _ = flags.(0) <- true in
  let _ = fill 1 in
  flags;;

let run_test inp outp =
  let value = let line = input_line inp in Scanf.sscanf line "%d" identity in
  let result = if flags.(value) then "YES" else "NO" in
  let _ = Printf.fprintf outp "%s\n" result in
  ();;

let (_:unit) =
  let inp = std_inp and outp = std_outp in
  let tests_count = let line = input_line inp in Scanf.sscanf line "%d" identity in
  let _ = repeat (run_test, inp, outp) tests_count in
  ();;


let n = read_int () in

let arr = Array.init n (fun i ->
	Scanf.scanf " %d" (fun i->i)
) in

let maxi = ref 0 in
let mini = ref 0 in
let _ = Array.iteri (fun i r ->
	if r <= arr.(!mini) then mini := i;
	if r > arr.(!maxi) then maxi := i
) arr in
print_int (!maxi + (n-1- !mini) - (if !maxi > !mini then 1 else 0))

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

let find_min array l =
    let rec loop min position i =
        if i < l
        then
            let current = array.(i) in
            if current <= min
            then
                loop current i (i + 1)
            else
                loop min position (i + 1)
        else
            position
    in loop array.(0) 0 0

let find_max array l =
    let rec loop max position i =
        if i < l
        then
            let current = array.(i) in
            if current > max
            then
                loop current i (i + 1)
            else
                loop max position (i + 1)
        else
            position
    in loop array.(0) 0 0

let solve () =
    let l = read () in
    let line = Array.init l (fun x -> read ()) in
    let min_i = find_min line l in
    let max_i = find_max line l in
    if max_i > min_i
    then
        (l - 1 - min_i) + (max_i - 1)
    else
        (l - 1 - min_i) + max_i

let print result = Printf.printf "%d\n" result

let () = print (solve ())

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

let find_best f array l =
    let rec loop best position i =
        if i < l
        then
            let current = array.(i) in
            if f current best
            then
                loop current i (i + 1)
            else
                loop best position (i + 1)
        else
            position
    in loop array.(0) 0 0

let find_min = find_best (fun x y -> x <= y)
let find_max = find_best (fun x y -> x > y)

let solve () =
    let l = read () in
    let line = Array.init l (fun x -> read ()) in
    let min_i = find_min line l in
    let max_i = find_max line l in
    if max_i > min_i
    then
        (l - 1 - min_i) + (max_i - 1)
    else
        (l - 1 - min_i) + max_i

let print result = Printf.printf "%d\n" result

let () = print (solve ())

let rec split s i acc =
begin
  if i > String.length s
  then List.rev acc
  else
      let first =
      if String.contains_from s i ' '
         then  String.index_from s i ' '
         else String.length s
    in split s (first+1) (int_of_string (String.init (first-i) (fun x -> String.get s (i+x))) :: acc)
end in
let n = int_of_string(read_line ()) and
    vals = split (read_line ()) 0 [] in
if n == List.length vals
   then
      let maxval = List.fold_right max (List.tl vals) (List.hd vals) and
          minval = List.fold_right min (List.tl vals) (List.hd vals) in
      if (List.nth vals 0) == maxval && (List.nth vals (n-1)) == minval
         then print_int 0
         else
          let maxpos = ref (n-1) and
              minpos = ref 0 in begin
          List.iteri (fun a b -> if b ==maxval then maxpos := min !maxpos a
                             else if b == minval then minpos := max !minpos a
                             else ()) vals;
      print_int (!maxpos + n - 1 - !minpos - (if !maxpos > !minpos then 1 else 0))
  end
  else ()

let ( |> ) x f = f x
let () =
	let n = read_int ()
	in let a =
		read_line ()
		|> Str.split (Str.regexp "\ ")
		|> List.map int_of_string
	in let ((max, left_most_pos), (min, right_most_pos)) =
		let rec search sublist_of_a pos =
			match sublist_of_a with
				| [] -> invalid_arg ""
				| hd :: [] -> ((hd, pos), (hd, pos))
				| hd :: tl ->
					  let ((max, left_most_pos), (min, right_most_pos)) = (search tl (pos + 1))
					  in let (new_max, new_left_most_pos) =
						  if  hd >= max then
							  (hd, pos)
						  else
							  (max, left_most_pos)
					  in let (new_min, new_right_most_pos) =
						  if hd < min then
							  (hd, pos)
						  else
							  (min, right_most_pos)
					  in ((new_max, new_left_most_pos), (new_min, new_right_most_pos))
		in search a 0
	in begin
		if left_most_pos > right_most_pos then
			(left_most_pos + (n - right_most_pos - 2))
		else
			(left_most_pos + (n - right_most_pos - 1))
	end |> print_int; print_newline ()

(* codeforces 103.  Danny Sleator *)
let read_int () = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x)
let read_string () = Scanf.bscanf Scanf.Scanning.stdib " %s " (fun x -> x)

let () =
  let n = read_int() in
  let a = Array.make n 0 in
  let () = for i=0 to n-1 do a.(i) <- read_int() done in
  let rec find_max i ac = if i=n then ac else find_max (i+1) (if ac < 0 || a.(i) > a.(ac) then i else ac) in
  let rec find_min i ac = if i=n then ac else find_min (i+1) (if ac < 0 || a.(i) <= a.(ac) then i else ac) in
  let mx = find_max 0 (-1) in
  let mi = find_min 0 (-1) in
  let guess = mx + (n-1-mi) in
  let answer = if mx < mi then guess else guess-1 in
    Printf.printf "%d\n" answer

let rec split s i acc =
begin
  if i > String.length s
  then List.rev acc
  else
      let first =
      if String.contains_from s i ' '
         then  String.index_from s i ' '
         else String.length s
    in split s (first+1) (int_of_string (String.init (first-i) (fun x -> String.get s (i+x))) :: acc)
end in
let n = int_of_string(read_line ()) and
    vals = split (read_line ()) 0 [] in
if n == List.length vals
   then begin
      let maxval = List.fold_right max (List.tl vals) (List.hd vals) and
          minval = List.fold_right min (List.tl vals) (List.hd vals) in
      let maxpos = ref (n-1) and
          minpos = ref 0 in
      List.iteri (fun a b -> if b ==maxval then maxpos := min !maxpos a
                             else if b == minval then minpos := max !minpos a
                             else ()) vals;
      print_int (!maxpos + n - 1 - !minpos - (if !maxpos > !minpos then 1 else 0))
  end
  else ()

let ( |> ) x f = f x
let () =
	let n = Scanf.bscanf Scanf.Scanning.stdin "%d" (fun x -> x)
	in let a =
		read_line ()
		|> Str.split (Str.regexp "\ ")
		|> List.map int_of_string
	in let ((max, left_most_pos), (min, right_most_pos)) =
		let rec search sublist_of_a pos =
			match sublist_of_a with
				| [] -> invalid_arg ""
				| hd :: [] -> ((hd, pos), (hd, pos))
				| hd :: tl ->
					  let ((max, left_most_pos), (min, right_most_pos)) = (search tl (pos + 1))
					  in let (new_max, new_left_most_pos) =
						  if  hd >= max then
							  (hd, pos)
						  else
							  (max, left_most_pos)
					  in let (new_min, new_right_most_pos) =
						  if hd < min then
							  (hd, pos)
						  else
							  (min, right_most_pos)
					  in ((new_max, new_left_most_pos), (new_min, new_right_most_pos))
		in search a 0
	in begin
		if left_most_pos > right_most_pos then
			(left_most_pos + (n - right_most_pos - 2))
		else
			(left_most_pos + (n - right_most_pos - 1))
	end |> Printf.printf "%d\n"


let can x = if 360 mod (180 - x) = 0 then "YES\n" else "NO\n" in
let n = read_line () |> int_of_string in
for i = 1 to n do
   print_string (read_line () |> int_of_string |> can)
done

open Scanf
open Printf

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

let a = Array.make n (-1);;
let b = Array.make n (-1);;

let nd=181
let c = Array.make nd 0;;

for i = 3 to 360 do
  if (360 mod i) = 0 then
    c.(180 - 360/i) <- 1
done

for i = 1 to n do
  let deg = Scanf.scanf "%d " ( fun x -> x ) in
  if c.(deg) = 0 then print_endline "NO" else print_endline "YES"
done

let ok x =
  let an = 180 - x in
  let n = 360 / an
  in an * n = 360 ;;

let test_num = read_int()
in
for i = 1 to test_num do
  let x = read_int()
  in
  if (ok x) then
    Printf.printf "YES\n"
  else
    Printf.printf "NO\n"
done

let read () = Pervasives.read_int ()
let print string = Pervasives.print_endline string

let () =
    let n = read () in
    let rec loop i =
        if i < n
        then
            let angle = read () in
                if 360 mod (180 - angle) = 0
                then
                    print "YES"
                else
                    print "NO";
                loop (i + 1)
        else
            ()
    in loop 0

open Scanf
open Printf

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

let a = Array.make n (-1);;
let b = Array.make n (-1);;

let nd=181
let c = Array.make nd 0;;

for i = 3 to 360 do
  c.(180 - 360/i) <- 1
done

for i = 1 to n do
  let deg = Scanf.scanf "%d " ( fun x -> x ) in
  if c.(deg) = 0 then print_endline "NO" else print_endline "YES"
done

open Scanf
open Printf

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

let aa = ref a in
let tt = ref a in
let () =
  while ( !aa /b > 0 ) do
    tt := !tt + (!aa)/b; aa := (!aa/b) + (!aa mod b)
  done
in printf "%d\n" !tt

open Scanf
open Printf

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

let a = Array.make n (-1);;
let b = Array.make n (-1);;

let nd=181
let c = Array.make nd 0;;

for i = 3 to 360 do
  if (360 mod 5) = 0 then
    c.(180 - 360/i) <- 1
done

for i = 1 to n do
  let deg = Scanf.scanf "%d " ( fun x -> x ) in
  if c.(deg) = 0 then print_endline "NO" else print_endline "YES"
done


open Printf
open Scanf

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

let () =
  let n = read_int () in

  let x = Array.make n 0 in
  let y = Array.make n 0 in

  for i=0 to n-1 do
    x.(i) <- read_int();
    y.(i) <- read_int();
    ignore (read_int())
  done;

  let abs f =
    sprintf "abs(%s)" f
  in

  let comb f op g =
    sprintf "(%s%s%s)" f op g
  in

  let rec const k =
    sprintf "%d" k
  in

  let absdif k =
    if k>0 then abs (comb "t" "-" (const k)) else abs (comb "t" "+" (const (-k)))
  in

  let basis k =
    let p1 = comb (absdif (k+1)) "-" (absdif (k-1)) in
    comb "2" "-" (abs p1)
  in

  let build_function n x =
    let rec loop i ac = if i=n then ac else
	let p1 = (comb (const ((x.(i)+1)/2)) "*" (basis i)) in
	let ac = if ac="" then p1 else comb p1 "+" ac in
	loop (i+1) ac
    in
    loop 0 ""
  in

  printf "%s\n" (build_function n x);
  printf "%s\n" (build_function n y);

open Printf
open Scanf

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

let () =
  let n = read_int () in

  let x = Array.make n 0 in
  let y = Array.make n 0 in

  for i=0 to n-1 do
    x.(i) <- read_int();
    y.(i) <- read_int();
    x.(i) <- x.(i) + (x.(i) land 1);
    y.(i) <- y.(i) + (y.(i) land 1);
    ignore (read_int())
  done;

  let abs f =
    sprintf "abs(%s)" f
  in

  let comb f op g =
    sprintf "(%s%s%s)" f op g
  in

  let rec const k =
    if k < 0 then comb (const 0) "-" (const (-k)) else sprintf "%d" k
  in

  let absdif k =
    abs (comb "t" "-" (const k))
  in

  let basis k =
    let p1 = comb (absdif (k+1)) "+" (absdif (k-1)) in
    let p2 = comb (absdif k) "+" (absdif k) in
    comb p1 "-" p2
  in

  let build_function n x =
    let rec loop i ac = if i=n then ac else
	let v = x.(i) in
	loop (i+1) (comb ac "+" (comb (const (v/2)) "*" (basis i)))
    in
    loop 0 "0"
  in

  printf "%s\n" (build_function n x);
  printf "%s\n" (build_function n y);

open Printf
open Scanf

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

let () =
  let n = read_int () in

  let x = Array.make n 0 in
  let y = Array.make n 0 in

  for i=0 to n-1 do
    x.(i) <- read_int();
    y.(i) <- read_int();
    x.(i) <- x.(i) + (x.(i) land 1);
    y.(i) <- y.(i) + (y.(i) land 1);
    ignore (read_int())
  done;

  let abs f =
    sprintf "abs(%s)" f
  in

  let comb f op g =
    sprintf "(%s%s%s)" f op g
  in

  let const k =
    sprintf "%d" k
  in

  let absdif k =
    abs (comb "t" "-" (const k))
  in

  let basis k =
    let p1 = comb (absdif (k+1)) "+" (absdif (k-1)) in
    let p2 = comb "2" "*" (absdif k) in
    comb p1 "-" p2
  in

  let build_function n x =
    let rec loop i ac = if i=n then ac else
	let v = x.(i) in
	loop (i+1) (comb ac "+" (comb (const (v/2)) "*" (basis i)))
    in
    loop 0 "0"
  in

  printf "%s\n" (build_function n x);
  printf "%s\n" (build_function n y);

open Printf
open Scanf

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

let () =
  let n = read_int () in

  let x = Array.make n 0 in
  let y = Array.make n 0 in

  for i=0 to n-1 do
    x.(i) <- read_int();
    y.(i) <- read_int();
    x.(i) <- x.(i) + (x.(i) land 1);
    y.(i) <- y.(i) + (y.(i) land 1);
    ignore (read_int())
  done;

  let abs f =
    sprintf "abs(%s)" f
  in

  let comb f op g =
    sprintf "(%s%s%s)" f op g
  in

  let const k =
    sprintf "%d" k
  in

  let absdif k =
    abs (comb "t" "-" (const k))
  in

  let basis k =
    let p1 = comb (absdif (k+1)) "+" (absdif (k-1)) in
    let p2 = comb (absdif k) "+" (absdif k) in
    comb p1 "-" p2
  in

  let build_function n x =
    let rec loop i ac = if i=n then ac else
	let v = x.(i) in
	loop (i+1) (comb ac "+" (comb (const (v/2)) "*" (basis (i+1))))
    in
    loop 0 "0"
  in

  printf "%s\n" (build_function n x);
  printf "%s\n" (build_function n y);

open Printf
open Scanf

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

let () =
  let n = read_int () in

  let x = Array.make n 0 in
  let y = Array.make n 0 in

  for i=0 to n-1 do
    x.(i) <- read_int();
    y.(i) <- read_int();
    x.(i) <- x.(i) + (x.(i) land 1);
    y.(i) <- y.(i) + (y.(i) land 1);
    ignore (read_int())
  done;

  let abs f =
    sprintf "abs(%s)" f
  in

  let comb f op g =
    sprintf "(%s%s%s)" f op g
  in

  let const k =
    sprintf "%d" k
  in

  let absdif k =
    abs (comb "t" "-" (const k))
  in

  let basis k =
    let p1 = comb (absdif (k+1)) "-" (absdif (k-1)) in
    let p2 = comb "2" "*" (absdif k) in
    comb p1 "-" p2
  in

  let build_function n x =
    let rec loop i ac = if i=n then ac else
	let v = x.(i) in
	loop (i+1) (comb ac "+" (comb (const (v/2)) "*" (basis v)))
    in
    loop 0 "0"
  in

  printf "%s\n" (build_function n x);
  printf "%s\n" (build_function n y);

open Printf
open Scanf

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

let () =
  let n = read_int () in

  let x = Array.make n 0 in
  let y = Array.make n 0 in

  for i=0 to n-1 do
    x.(i) <- read_int();
    y.(i) <- read_int();
    x.(i) <- x.(i) + (x.(i) land 1);
    y.(i) <- y.(i) + (y.(i) land 1);
    ignore (read_int())
  done;

  let abs f =
    sprintf "abs(%s)" f
  in

  let comb f op g =
    sprintf "(%s%s%s)" f op g
  in

  let const k =
    sprintf "%d" k
  in

  let absdif k =
    abs (comb "t" "-" (const k))
  in

  let basis k =
    let p1 = comb (absdif (k+1)) "+" (absdif (k-1)) in
    let p2 = comb "2" "*" (absdif k) in
    comb p1 "-" p2
  in

  let build_function n x =
    let rec loop i ac = if i=n then ac else
	let v = x.(i) in
	loop (i+1) (comb ac "+" (comb (const (v/2)) "*" (basis v)))
    in
    loop 0 "0"
  in

  printf "%s\n" (build_function n x);
  printf "%s\n" (build_function n y);

open Printf
open Scanf

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

let () =
  let n = read_int () in

  let x = Array.make n 0 in
  let y = Array.make n 0 in

  for i=0 to n-1 do
    x.(i) <- read_int();
    y.(i) <- read_int();
    x.(i) <- x.(i) + (x.(i) land 1);
    y.(i) <- y.(i) + (y.(i) land 1);
    ignore (read_int())
  done;

  let abs f =
    sprintf "abs(%s)" f
  in

  let comb f op g =
    sprintf "(%s%s%s)" f op g
  in

  let const k =
    sprintf "%d" k
  in

  let absdif k =
    abs (comb "t" "-" (const k))
  in

  let basis k =
    let p1 = comb (absdif (k+1)) "+" (absdif (k-1)) in
    let p2 = comb (absdif k) "+" (absdif k) in
    comb p1 "-" p2
  in

  let build_function n x =
    let rec loop i ac = if i=n then ac else
	let v = x.(i) in
	loop (i+1) (comb ac "+" (comb (const (v/2)) "*" (basis i)))
    in
    loop 0 "0"
  in

  printf "%s\n" (build_function n x);
  printf "%s\n" (build_function n y);

open Printf
open Scanf

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

let () =
  let n = read_int () in

  let x = Array.make n 0 in
  let y = Array.make n 0 in

  for i=0 to n-1 do
    x.(i) <- read_int();
    y.(i) <- read_int();
    x.(i) <- x.(i) + (x.(i) land 1);
    y.(i) <- y.(i) + (y.(i) land 1);
    ignore (read_int())
  done;

  let abs f =
    sprintf "abs(%s)" f
  in

  let comb f op g =
    sprintf "(%s%s%s)" f op g
  in

  let const k =
    sprintf "%d" k
  in

  let absdif k =
    abs (comb "t" "-" (const k))
  in

  let basis k =
    let p1 = comb (absdif (k+1)) "+" (absdif (k-1)) in
    let p2 = comb "2" "*" (absdif k) in
    comb p1 "-" p2
  in

  let build_function n x =
    let rec loop i ac = if i=n then ac else
	let v = x.(i) in
	loop (i+1) (comb ac "+" (comb (const (v/2)) "*" (basis (i+1))))
    in
    loop 0 "0"
  in

  printf "%s\n" (build_function n x);
  printf "%s\n" (build_function n y);


let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make (2 * n) (0, 0, 0) in
  let ll = Array.make n 0 in
    for i = 0 to n - 1 do
      let l = Scanf.bscanf sb "%d " (fun s -> s) in
      let r = Scanf.bscanf sb "%d " (fun s -> s) in
	a.(2 * i) <- (l, 0, i);
	a.(2 * i + 1) <- (r, 1, i);
	ll.(i) <- l;
    done;
    Array.sort compare a;
    let res = ref 0 in
    let s = ref [] in
    let tt = ref (-1) in
      for i = 0 to 2 * n - 1 do
	let (t, k, x) = a.(i) in
	  if k = 0
	  then ()
	  else (
	    if ll.(x) > !tt then (
	      incr res;
	      tt := t;
	    )
	  )
      done;
      Printf.printf "%d\n" !res

open Array;;
open Str;;
let n = read_int () and ans = ref 0;;
let par = make n [|0; 0|] and right = ref (-1);;
for i = 1 to n do set par (i - 1) (of_list (List.map (int_of_string) (split (regexp " ") (read_line ())))) done;;
let cmp [|a; b|] [|c; d|] = (if b < d then -1 else 1);;
sort (cmp) par;;
for i = 0 to n - 1 do if par.(i).(0) > !right then begin
	right := par.(i).(1);
	ans := !ans + 1;
end done;;
print_int !ans

open Array;;
open Str;;
let n = read_int () and ans = ref 0;;
let par = make n [|0; 0|] and right = ref (-1);;
for i = 1 to n do set par (i - 1) (of_list (List.map (int_of_string) (split (regexp " ") (read_line ())))) done;;
let cmp [|a; b|] [|c; d|] = (if b < d then -1 else 1);;
sort (cmp) par;;
for i = 0 to n - 1 do Printf.printf "%d %d\n" par.(i).(0) par.(i).(1) done;;
for i = 0 to n - 1 do if par.(i).(0) > !right then begin
	right := par.(i).(1);
	ans := !ans + 1;
end done;;
print_int !ans


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

let make_list n =
    let rec loop i list =
        if i < n
        then
            let value = read () in
            loop (i + 1) (value :: list)
        else
            List.rev list
    in loop 0 []

let counter count max_delay diff =
    if diff <= max_delay
    then
        count + 1
    else
        1

let input () =
    let n = read () in
    let delay = read () in
    let seconds = make_list n in
    (delay, seconds)

let solve (delay, seconds) =
    let rec loop prev rest count =
        match rest with
        | (head :: tail) -> loop head tail (counter count delay (head - prev))
        | [] -> count
    in loop 0 seconds 0

let print result = Printf.printf "%d\n" result
let () = print (solve (input ()))

let read_nums () =
    let s = read_line () in
    let nums = List.map int_of_string (Str.split (Str.regexp " ") s) in
    nums

;;

let rec get_ans c lst acc prev=
    match lst with
    | hd :: tl -> if (hd - prev <= c) then
                      get_ans c tl (acc + 1) hd
                  else
                      get_ans c tl 1 hd
    | [] -> let () = print_int acc in print_string "\n"

let main () =
    let l = read_nums () in
    match l with
    | [n; c] -> (get_ans c (read_nums ())) 0 0
    | _ -> ()
;;

main () ;;

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

let () =
  let n = read_int () in
  let c = read_int () in
  let time_arr = Array.init n (fun _ ->
      read_int ()
    )
  in
  let counter = ref 1 in
  for i = 0 to n - 2 do
    if time_arr.(i+1) - time_arr.(i) <= c then
      counter := !counter + 1
    else counter := 1
  done;
  Printf.printf "%d" !counter

open Scanf
open Printf

let rec calc a i cur n c = if (i = n) then cur
else if (a.(i) - a.(i - 1) > c) then calc a (i + 1) 1 n c
else calc a (i + 1) (cur + 1) n c;;

let readInt () = scanf " %d " (fun x -> x)

let a =
let answer =
let n = readInt () in
let c = readInt () in
let a  = Array.init n (fun y -> readInt ()) in
calc a 1 1 n c
in printf "%d\n" answer;;

open Scanf
open Printf

let rec calc a i cur n c = if (i = n) then cur
else if (a.(i) - a.(i - 1) > c) then calc a (i + 1) 1 n c
else calc a (i + 1) (cur + 1) n c;;

let a =
let answer =
let n = scanf " %d " (fun x-> x) in
let c = scanf " %d " (fun x-> x) in
let a  = Array.init n (fun y -> scanf " %d " (fun x -> x)) in
calc a 1 1 n c
in printf "%d\n" answer;;


let xstr() = Scanf.scanf "%s " (fun x -> x)

let () =
  let s = xstr() and t = xstr() in
  let n = String.length s and m = String.length t in
  let i = ref 0 and j = ref 0 and res = ref 0 in
  while s.[!i] == '0' && !i + 1 < n do
    i := !i + 1;
  done;
  while t.[!j] == '0' && !j + 1 < m do
    j := !j + 1;
  done;
  res := (if n - !i < m - !j then -1 else (
    if n - !i > m - !j then 1 else 0
  ));
  if n - !i = m - !j then (
    res := 0;
    while !i < n && !j < m && !res = 0 do
      res := if s.[!i] < t.[!j] then -1 else (
        if s.[!i] > t.[!j] then 1 else 0
      );
      i := !i + 1;
      j := !j + 1;
    done;
  );
  if !res = 0 then (
    Printf.printf "=\n";
  );
  if !res < 0 then (
    Printf.printf "<\n";
  );
  if !res > 0 then (
    Printf.printf ">\n";
  );

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 sprawdz n a = (* czy jest jakis niezerowy *)
  let koniec = ref false in
  for i=0 to n do
    if a.[i] <> '0' then koniec := true
  done;
  !koniec;;

let rec porownaj ax bx a b =
  if ax = String.length a then '='
  else if a.[ax] = b.[bx] then porownaj (ax+1) (bx+1) a b
  else if a.[ax] < b.[bx] then '<'
  else '>';;

let check a b =
  let n = String.length a
  and m = String.length b in
  if sprawdz (n-m-1) a then '>'
  else if sprawdz (m-n-1) b then '<'
  else
    let ax = max 0 (n-m)
    and bx = max 0 (m-n) in
    porownaj ax bx a b;;

let _ =
  let (a,b) = Scanf.scanf "%s\n%s\n" (fun x y -> x, y) in
  Printf.printf "%c\n" (check a b) ;;

let xstr() = Scanf.scanf "%s " (fun x -> x)

let () =
  let s = xstr() and t = xstr() in
  let n = String.length s and m = String.length t in
  let i = ref 0 and j = ref 0 and res = ref 0 in
  while s.[!i] == '0' && !i + 1 < n do
    i := !i + 1;
  done;
  while t.[!j] == '0' && !j + 1 < m do
    j := !j + 1;
  done;
  res := (if n - !i < m - !j then -1 else (
    if n - !i < m - !j then 1 else 0
  ));
  if n - !i = m - !j then (
    res := 0;
    while !i < n && !j < m && !res = 0 do
      res := if s.[!i] < t.[!j] then -1 else (
        if s.[!i] > t.[!j] then 1 else 0
      );
      i := !i + 1;
      j := !j + 1;
    done;
  );
  if !res = 0 then (
    Printf.printf "=\n";
  );
  if !res < 0 then (
    Printf.printf "<\n";
  );
  if !res > 0 then (
    Printf.printf ">\n";
  );


open Printf
open Scanf

let ( ++ ) a b = Int64.add a b
let ( -- ) a b = Int64.sub a b
let long x = Int64.of_int x

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 x = long (read_int ()) in

  let rec loop i packs distress = if i=n then (packs,distress) else (
    let s = read_string() in
    let q = long (read_int()) in
    match s with
      |	"+" -> loop (i+1) (packs ++ q) distress
      | "-" ->
	if q > packs then loop (i+1) packs (distress+1)
	else loop (i+1) (packs -- q) distress
      | _ -> failwith "bad input"
  ) in

  let (packs, distress) = loop 0 x 0 in

  printf "%Ld %d\n" packs distress

open Big_int

let (+.) = add_big_int

let rec f init dis = function
   | t::q when ge_big_int t zero_big_int -> f (init +. t) dis q
   | t::q              -> if lt_big_int (init +. t) zero_big_int
                         then f init (dis + 1) q
                         else f (init +. t) dis q
   | _                 -> (init,dis)

let _ =
   let (n,init) = Scanf.scanf "%d %s" (fun i j -> i,big_int_of_string j) in
   let l = ref [] in
      for i = 1 to n do Scanf.scanf " %c %d" (fun c x ->
         l := (big_int_of_int (x * if c = '+' then 1 else -1))::!l);
      done;
   let (a,b) = f init 0 (List.rev !l) in
      Printf.printf "%s %d" (string_of_big_int a) b

(* Codeforces 686 A Free Ice Cream *)

(* 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 grc () = Scanf.scanf " %c" (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;;

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 x = ref (Int64.of_int 0);;
let ndistress = ref 0;;

(* c - + or -, d - nuym of ice cream *)
let one_step c d =
	(* let _ = begin print_int !x; print_string "\n" end in *)
	if c == '+' then
		begin
			x := Int64.add !x d
		end
	else
  		if !x < d then
  			begin
  				ndistress := !ndistress + 1
  			end
			else
				begin
		  		x := Int64.sub !x d
				end;;

let main() =
	let n = gr() in
	let xx = gr () in
	begin
		x := Int64.of_int xx;
		for i = 1 to n do
			let c = grc () in
			let d = Int64.of_int (gr ()) in
			one_step c d
		done;
		print_string (Int64.to_string !x);
		print_string " ";
		print_int !ndistress;
		print_string "\n"
	end;;

main();;

let [n; x] = read_line () |> Str.(split (regexp " ")) |> List.map int_of_string

let store = ref (Int64.of_int x)
let sad = ref 0

let step sign amount =
  let open Int64 in
  let amount = of_int amount in
  match sign with
  | '+' ->
     store := Int64.add !store amount
  | '-' ->
     if !store >= amount then
       store := Int64.sub !store amount
     else
       incr sad
  | _ -> assert false

let () =
  for i = 1 to n do
    Scanf.scanf "%c %d\n" step
  done;
  Printf.printf "%s %d\n" (Int64.to_string !store) !sad

let [n; x] = read_line () |> Str.(split (regexp " ")) |> List.map int_of_string

let store = ref x
let sad = ref 0

let step sign amount =
  match sign with
  | '+' ->
     store := !store + amount
  | '-' ->
     if !store >= amount then
       store := !store - amount
     else
       incr sad
  | _ -> assert false

let () =
  for i = 1 to n do
    Scanf.scanf "%c %d\n" step
  done;
  Printf.printf "%d %d\n" !store !sad

(* Codeforces 686 A Free Ice Cream *)

(* 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 grc () = Scanf.scanf " %c" (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;;

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 x = ref 0;;
let ndistress = ref 0;;

(* c - + or -, d - nuym of ice cream *)
let one_step c d =
	(* let _ = begin print_int !x; print_string "\n" end in *)
	if c == '+' then
		begin
			x := !x + d
		end
	else
  		if !x < d then
  			begin
  				ndistress := !ndistress + 1
  			end
			else
				begin
		  		x := !x-d
				end;;

let main() =
	let n = gr() in
	let xx = gr () in
	begin
		x := xx;
		for i = 1 to n do
			let c = grc () in
			let d = gr () in
			one_step c d
		done;
		print_int !x;
		print_string " ";
		print_int !ndistress;
		print_string "\n"
	end;;

main();;


(* Little penguin Polo adores strings. But most of all he adores strings of length n.

One day he wanted to find a string that meets the following conditions:

  The string consists of n lowercase English letters (that is, the string's length equals n), exactly k of these letters are distinct.
  No two neighbouring letters of a string coincide; that is, if we represent a string as s = s1s2... sn, then the following inequality holds, si ≠ si + 1(1 ≤ i < n).
  Among all strings that meet points 1 and 2, the required string is lexicographically smallest.
  Help him find such string or state that such string doesn't exist.

  Input
  A single line contains two positive integers n and k (1 ≤ n ≤ 106, 1 ≤ k ≤ 26) — the string's length and the number of distinct letters.

  string.length = n, string.distinct_letters = k, string.neighbouring_letters = false *)

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

let () =
  let n = read_int() in
  let k = read_int() in
  let s = String.create n in

  let answer =
    if n < k then "-1"
    else if n=1 then "a"
    else if k<2 then "-1"
    else (
      for i=0 to n-(k-2)-1 do
        s.[i] <- if i mod 2 = 0 then 'a' else 'b';
      done;
      for i=n-(k-2) to n-1 do
        let j = i-(n-(k-2)) in
        let c = char_of_int ((int_of_char 'c') + j) in
        s.[i] <- c;
      done;
      s
        )
        in
    print_string answer;
    print_newline();

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

let read_pair () = let a = read_int () in (a , read_int ());;

let n , k = read_pair ();;

let ans = String.create n;;

for i = 0 to (n-1) do
   if i mod 2 = 0 then ans.[i] <- 'a'
   else ans.[i] <- 'b'
done;;

if n = 1 && k = 1 then Printf.printf "a\n"
else if n != 1 && k = 1 then Printf.printf "-1\n"
else if k > n then Printf.printf "-1\n"
else begin
   for i = (n-k+2) to (n-1) do ans.[i] <- Char.chr ((Char.code 'c') + i - (n-k+2)) done;
   Printf.printf "%s\n" ans
end ;;


(* OCaml template *)

(* Utility functions *)
let sum = List.fold_left (+) 0;;

let aux = function
	| 'U' -> 'D'
	| 'D' -> 'U'
	|  c  ->  c

(* Main *)
let t = read_int ();;

for t' = 1 to t do
	(* Solve test case here *)
	let n = read_int () in
		let s = read_line () in
			print_endline @@ String.map aux s
done

open Printf
open Scanf

let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let read_string _ = bscanf Scanning.stdin " %s " (fun x -> x)
let () =
  let cases = read_int () in
  for case = 1 to cases do
    let n = read_int() in
    let s = read_string() in
    for i=0 to n-1 do
      let y = match s.[i] with
	| 'L' -> 'L'
	| 'R' -> 'R'
	| 'U' -> 'D'
	| 'D' -> 'U'
	| _ -> failwith "bad input"
      in
      printf "%c" y;
    done;
    print_newline()
  done


open Scanf
open Printf

let string_replace s l r o t =
  let f i c =
    if i >= l && i <= r && c = o then
      t
    else
      c
  in
  String.mapi f s
;;

let () =
  let (n, m) = bscanf Scanning.stdin " %d %d" (fun x y -> (x, y)) in
  let s = bscanf Scanning.stdin " %s" (fun x -> x) in
  let rec solve i result =
    if i = m
    then result
    else
      let (l, r, o, t) =
        bscanf Scanning.stdin " %d %d %c %c" (fun a b c d -> (a, b, c, d)) in
      let l = l-1 in
      let r = r-1 in
      let result = string_replace result l r o t in
      solve (i+1) result
  in
  printf "%s\n" (solve 0 s);;

open Scanf
open Printf

let string_replace s l r o t =
  let f i c =
    if i >= l && i <= r && c = o then
      t
    else
      c
  in
  String.mapi f s
;;

let () =
  let (n, m) = bscanf Scanning.stdin " %d %d" (fun x y -> (x, y)) in
  let s = bscanf Scanning.stdin " %s" (fun x -> x) in
  let rec solve i result =
    if i = m
    then result
    else
      let (l, r, o, t) =
        bscanf Scanning.stdin " %d %d %c %c" (fun a b c d -> (a, b, c, d)) in
      let ll = l-1 in
      let rr = r-1 in
      let tmp = string_replace result ll rr o t in
      solve (i+1) tmp
  in
  printf "%s\n" (solve 0 s);;

open Printf
open Scanf

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

let () =
  let (n,m) = read_pair () in
  let s = read_string() in
  let rec loop i ac = if i=m then ac else (
      let (l,r) = read_pair() in
      let r = r-1 in
      let l = l-1 in
      let c1 = (read_string()).[0] in
      let c2 = (read_string()).[0] in
      let ac = String.init n (fun i -> if i < l || i > r then ac.[i]
	else if ac.[i] = c1 then c2 else ac.[i])
      in
      loop (i+1) ac
  )
  in

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

open Scanf
open Printf

let string_replace s l r o t =
  let f i c =
    if i >= l && i <= r && c = o then
      t
    else
      s.[i] in
  String.mapi f s
;;

let () =
  let (n, m) = bscanf Scanning.stdin " %d %d" (fun x y -> (x, y)) in
  let s = bscanf Scanning.stdin " %s" (fun x -> x) in
  let rec solve i result =
    if i = m
    then result
    else
      let (l, r, o, t) =
        bscanf Scanning.stdin " %d %d %c %c" (fun a b c d -> (a, b, c, d)) in
      let ll = l-1 in
      let rr = r-1 in
      let tmp = string_replace s ll rr o t in
      solve (i+1) tmp
  in
  printf "%s\n" (solve 0 s);;


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

let prophesy = read_string () ;;

let isdigit c = (c >= '0' && c <= '9') ;;

let months = [|0; 31; 28; 31; 30; 31; 30; 31; 31; 30; 31; 30; 31|] ;;

let parse_date date =
    Scanf.sscanf date "%d-%d-%d" (fun d m y -> (d, m, y))

let valid_date date =
    let rec loop i =
        if i >= 10 then true
        else if (i = 2 || i = 5) && date.[i] = '-' then loop (i + 1)
        else if i != 2 && i != 5 && isdigit date.[i] then loop (i + 1)
        else false
    in if (loop 0) = false then false
    else let (d, m, y) = parse_date date
    in y >= 2013 && y <= 2015 && m > 0 && m <= 12 && d > 0 && d <= months.(m) ;;

let freq =
    let matrix = Array.make 32 (Array.make_matrix 13 2016 0) in
    for i = 0 to 31 do
        matrix.(i) <- Array.make_matrix 13 2016 0
    done ;
    matrix ;;

let apocalypse =
    let rec loop i date count =
        if i > (String.length prophesy) - 10 then date
        else
            let cur_date = String.sub prophesy i 10
            in if valid_date cur_date then let (d, m, y) = parse_date cur_date
            in
            (
                freq.(d).(m).(y) <- freq.(d).(m).(y) + 1 ;
                if freq.(d).(m).(y) > count then
                    loop (i + 1) cur_date freq.(d).(m).(y)
                else
                    loop (i + 1) date count
            )
            else loop (i + 1) date count
    in loop 0 "" 0 ;;

Printf.printf "%s\n" apocalypse ;;

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

let days = [|31;28;31;30;31;30;31;31;30;31;30;31|]

let is_digit c = c >= '0' && c <= '9'

let valid_date s =
  (List.for_all (fun p -> is_digit s.[p]) [0;1;3;4;6;7;8;9]) &&
    (List.for_all (fun p -> s.[p] = '-') [2;5]) &&
    (
      let d = int_of_string (String.sub s 0 2) in
      let m = int_of_string (String.sub s 3 2) in
      let y = int_of_string (String.sub s 6 4) in
	m>=1 && m<=12 && d>=1 && d<=days.(m-1) && y>=2013 && y<=2015
    )

let () =
  let s = read_string () in
  let n = String.length s in

  let h = Hashtbl.create 10 in

  let increment t =
    let c = try Hashtbl.find h t with Not_found -> 0 in
      Hashtbl.replace h t (c+1)
  in

    for i=0 to n-10 do
      let t = String.sub s i 10 in
	if valid_date t then increment t
    done;

    let m = Hashtbl.fold (fun _ c ac -> max c ac) h 0 in

    let t = Hashtbl.fold (fun s c ac -> if c = m then s else ac) h "" in

      Printf.printf "%s\n" t

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

let prophesy = read_string () ;;

let isdigit c = (c >= '0' && c <= '9') ;;

let months = [|0; 31; 28; 31; 30; 31; 30; 31; 31; 30; 31; 30; 31|] ;;

let parse_date date =
    Scanf.sscanf date "%d-%d-%d" (fun d m y -> (d, m, y))

let valid_date date =
    let rec loop i =
        if i >= 10 then true
        else if (i = 2 || i = 5) && date.[i] = '-' then loop (i + 1)
        else if i != 2 && i != 5 && isdigit date.[i] then loop (i + 1)
        else false
    in if (loop 0) = false then false
    else let (d, m, y) = parse_date date
    in y >= 2013 && y <= 2015 && m > 0 && m <= 12 && d > 0 && d <= months.(m) ;;

let freq = Array.make 32 (Array.make_matrix 13 2016 0) ;;

let apocalypse =
    let rec loop i date count =
        if i > (String.length prophesy) - 10 then date
        else
            let cur_date = String.sub prophesy i 10
            in if valid_date cur_date then let (d, m, y) = parse_date cur_date
            in
            (
                freq.(d).(m).(y) <- freq.(d).(m).(y) + 1 ;
                if freq.(d).(m).(y) >= count then
                    loop (i + 1) cur_date freq.(d).(m).(y)
                else
                    loop (i + 1) date count
            )
            else loop (i + 1) date count
    in loop 0 "" 0 ;;

Printf.printf "%s\n" apocalypse ;;

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

let prophesy = read_string () ;;

let isdigit c = (c >= '0' && c <= '9') ;;

let months = [|0; 31; 28; 31; 30; 31; 30; 31; 31; 30; 31; 30; 31|] ;;

let parse_date date =
    Scanf.sscanf date "%d-%d-%d" (fun d m y -> (d, m, y))

let valid_date date =
    let rec loop i =
        if i >= 10 then true
        else if (i = 2 || i = 5) && date.[i] = '-' then loop (i + 1)
        else if i != 2 && i != 5 && isdigit date.[i] then loop (i + 1)
        else false
    in if (loop 0) = false then false
    else let (d, m, y) = parse_date date
    in y >= 2013 && y <= 2015 && m > 0 && m <= 12 && d > 0 && d <= months.(m) ;;

let freq = Array.make 32 (Array.make_matrix 13 2016 0) ;;

let apocalypse =
    let rec loop i date count =
        if i > (String.length prophesy) - 10 then date
        else
            let cur_date = String.sub prophesy i 10
            in if valid_date cur_date then let (d, m, y) = parse_date cur_date
            in
            (
                freq.(d).(m).(y) <- freq.(d).(m).(y) + 1 ;
                if freq.(d).(m).(y) > count then
                    loop (i + 1) cur_date freq.(d).(m).(y)
                else
                    loop (i + 1) date count
            )
            else loop (i + 1) date count
    in loop 0 "" 0 ;;

Printf.printf "%s\n" apocalypse ;;

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

let days = [|31;28;31;30;31;30;31;31;30;31;31;31|]

let is_digit c = c >= '0' && c <= '9'

let valid_date s =
  (List.for_all (fun p -> is_digit s.[p]) [0;1;3;4;6;7;8;9]) &&
    (List.for_all (fun p -> s.[p] = '-') [2;5]) &&
    (
      let d = int_of_string (String.sub s 0 2) in
      let m = int_of_string (String.sub s 3 2) in
      let y = int_of_string (String.sub s 6 4) in
	m>=1 && m<=12 && d>=1 && d<=days.(m-1) && y>=2013 && y<=2015
    )

let () =
  let s = read_string () in
  let n = String.length s in

  let h = Hashtbl.create 10 in

  let increment t =
    let c = try Hashtbl.find h t with Not_found -> 0 in
      Hashtbl.replace h t (c+1)
  in

    for i=0 to n-10 do
      let t = String.sub s i 10 in
	if valid_date t then increment t
    done;

    let m = Hashtbl.fold (fun _ c ac -> max c ac) h 0 in

    let t = Hashtbl.fold (fun s c ac -> if c = m then s else ac) h "" in

      Printf.printf "%s\n" t