let read_int () = Scanf.scanf " %d" (fun x -> x) in
let n = read_int() in
  let f = read_int() in
  let mmin = ref f and mmax = ref f in
  let count = ref 0 in
  for i = 1 to n-1 do
  let next = read_int() in
  if next > !mmax then begin
  mmax := next;
  incr count;
  end else if next < !mmin then begin
  mmin := next;
  incr count;
  end;
  done;
  print_int !count;
  print_newline();;

let rdint =
    let l = ref 0 in
    let i = ref 0 in
    let s = String.create 65536 in
    let refill () =
        let n = input stdin s 0 65536 in
        i := 0;
        l := n in
    refill ();

    fun () ->
        let rec loop2 j k t f =
            let fin j t f =
                let _ = i := j in
                t * f in

            if j = k then (
                let _ = refill () in
                if !l > 0 then loop2 !i !l t f else fin j t f
            ) else if s.[j] >= '0' && s.[j] <= '9' then
                loop2 (j + 1) k (t * 10 + Char.code s.[j] - 48) f
            else fin j t f in

        let rec loop1 j k =
            if j = k then (
                let _ = refill () in
                if !l > 0 then loop1 !i !l else 0
            ) else if s.[j] = '-' then loop2 (j + 1) k 0 (-1)
            else if s.[j] >= '0' && s.[j] <= '9' then loop2 (j + 1) k (Char.code s.[j] - 48) 1
            else loop1 (j + 1) k in
        loop1 !i !l

let main () =
    let rec loop i min0 max0 acc =
        if i = 0 then Printf.printf "%d\n" acc else
            let k = rdint () in
            let acc = if k < min0 || k > max0 then acc + 1 else acc in
            loop (i - 1) (min min0 k) (max max0 k) acc
    in
    let n = rdint() in
    let s = rdint() in
    loop (n - 1) s s 0
;;
main ()

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

let solve () =
    let n = read () in
    let first = read () in
    let rec loop i count max min =
        if i < n
        then
            let score = read () in
            if score < min
            then
                loop (i + 1) (count + 1) max score
            else
                if score > max
                then
                    loop (i + 1) (count + 1) score min
                else
                    loop (i + 1) count max min
        else
            count
    in loop 1 0 first first

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


(*
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
4
9
25
49

*)

let _ =
   let tab =
[|
3;
5;
7;
11;
13;
17;
19;
23;
29;
31;
37;
41;
43;
47;
4;
9;
25;
49
|] in
   let cpt = ref (Printf.printf "2\n"; flush stdout; Scanf.scanf "%s" (fun i -> if i = "yes" then 1 else 0)) in
      Array.iter (fun i -> Printf.printf "%d\n" i; flush stdout; Scanf.scanf " %s" (fun i -> if i = "yes" then incr cpt)) tab;
      if !cpt < 2
      then Printf.printf "prime"
      else Printf.printf "composite";
      flush stdout

open Printf
open String

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 primes_small = [2; 3; 5; 7]
let primes_big = [11; 13; 17; 19; 23; 29; 31; 37; 41; 43; 47]
let squares = [4; 9; 25; 49]

let () =

  let try_a_d d =
    printf "%d\n%!" d;
    let s = read_line() in
    match s with "yes" -> true | "no" -> false | _ -> failwith "bad input"
  in

  let countlist li = List.fold_left (
    fun ac e -> if try_a_d e then ac+1 else ac
  ) 0 li in

  let nsmall = countlist primes_small in
  let nbig = countlist primes_big in
  let nsquares = countlist squares in

  if nsmall = 0 then printf "prime\n"
  else if nsmall >= 2 || nsquares > 0 then printf "composite\n"
  else if nbig = 0 then printf "prime\n"
  else printf "composite\n"

open Printf
open String

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 primes_small = [2; 3; 5; 7]
let primes_big = [11; 13; 17; 19; 23; 29; 31; 37; 41; 43; 47]
let squares = [4; 9; 25; 49]

let () =

  let try_a_d d =
    printf "%d\n%!" d;
    let s = read_line() in
    match s with "yes" -> true | "no" -> false | _ -> failwith "bad input"
  in

  let countlist li = List.fold_left (
    fun ac e -> if try_a_d e then 1 else 0
  ) 0 li in

  let nsmall = countlist primes_small in
  let nbig = countlist primes_big in
  let nsquares = countlist squares in

  if nsmall = 0 then printf "prime\n"
  else if nsmall >= 2 || nsquares > 0 then printf "composite\n"
  else if nbig = 0 then printf "prime\n"
  else printf "composite\n"


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

let () =
	let n = read_int ()
	and m = read_int ()
	and wyn = ref 0 in

	let tab = Array.make (n + 1) 100000 in

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

	for i = 1 to n - 1 do
		if tab.(i) + tab.(i - 1) < m then begin
			wyn := !wyn + abs(m - (tab.(i) + tab.(i - 1)));
			tab.(i) <- m - tab.(i - 1);
		end;
	done;

	printf "%d\n" !wyn;

	for i = 0 to n - 1 do
		printf "%d " tab.(i)
	done;

open Printf

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

let rec readi n a =
  match n with
  | 0 -> (List.rev a)
  | _ -> let x = Scanf.scanf "%d " (fun x -> x) in
      readi (n-1) (x::a)

let l = readi n1 []

(*
let () =
  List.iter (fun x -> printf "%d " x) l;
  print_endline ""
  *)

let rec f a al ll =
  match ll with
  |[] -> (a, List.rev al)
  |x1::[] -> (a,List.rev (x1::al))
  |x1::(x2::tl) ->
      if x1+x2<k1 then
       (f (a+(k1-x1-x2)) (x1::al) ((k1-x1)::tl))
      else
        (f a (x1::al) (x2::tl))

let (t,al) = f 0 [] l

let () =
  printf "%d\n" t;
  List.iter (fun x -> printf "%d " x) al

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

let input () =
    let n = read () in
    let k = read () in
    let arr = Array.append (Array.make 1 k) (Array.init n (fun n -> read ())) in
    (n, k, arr)

let solve (n, k, arr) =
    let rec loop sum i =
        if i < n + 1
        then
            let a = arr.(i - 1) in
            let b = arr.(i) in
            if a + b < k
            then
                (arr.(i) <- k - a;
                let new_sum = sum + (k - a - b) in
                loop new_sum (i + 1))
            else
                loop sum (i + 1)
        else
            (sum, Array.sub arr 1 n)
    in loop 0 1

let print (sum, array) =
    Printf.printf "%d\n" sum;
    Array.iter (Printf.printf "%d ") array;
    Printf.printf "\n"

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

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

let () =
	let n = read_int ()
	and m = read_int ()
	and wyn = ref 0 in

	let tab = Array.make (n + 1) 100000 in

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

	for i = 1 to n - 1 do
		if tab.(i) + tab.(i - 1) < m then
			wyn := !wyn + abs(tab.(i) - tab.(i - 1));
		tab.(i) <- m - tab.(i - 1);
	done;

	printf "%d\n" !wyn;

	for i = 0 to n - 1 do
		printf "%d " tab.(i)
	done;

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

let () =
	let n = read_int ()
	and m = read_int ()
	and wyn = ref 0 in

	let tab = Array.make (n + 1) 100000 in

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

	for i = 1 to n - 1 do
		if tab.(i) + tab.(i - 1) < m then
			wyn := !wyn + abs(m - (tab.(i) + tab.(i - 1)));
		tab.(i) <- m - tab.(i - 1);
	done;

	printf "%d\n" !wyn;

	for i = 0 to n - 1 do
		printf "%d " tab.(i)
	done;

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

let input () =
    let n = read () in
    let k = read () in
    let arr = Array.append (Array.make 1 k) (Array.init n (fun n -> read ())) in
    (n, k, arr)

let solve (n, k, arr) =
    let rec loop sum i =
        if i < n + 1
        then
            let a = arr.(i - 1) in
            let b = arr.(i) in
            if a + b < k
            then
                (arr.(i) <- k - a;
                let new_sum = sum + (k - a) in
                loop new_sum (i + 1))
            else
                loop sum (i + 1)
        else
            (sum, Array.sub arr 1 n)
    in loop 0 1

let print (sum, array) =
    Printf.printf "%d\n" sum;
    Array.iter (Printf.printf "%d ") array;
    Printf.printf "\n"

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

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

let input () =
    let n = read () in
    let k = read () in
    let arr = Array.append (Array.make 1 k) (Array.init n (fun n -> read ())) in
    (n, k, arr)

let solve (n, k, arr) =
    let rec loop i =
        if i < n + 1
        then
            let a = arr.(i - 1) in
            let b = arr.(i) in
            if a + b < k
            then
                (arr.(i) <- k - a;
                loop (i + 1))
            else
                loop (i + 1)
        else
            Array.sub arr 1 n
    in loop 1

let print array = Array.iter (Printf.printf "%d ") array;
    Printf.printf "\n"

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

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

let input () =
    let n = read () in
    let k = read () in
    let arr = Array.append (Array.make 1 k) (Array.init n (fun n -> read ())) in
    (n, k, arr)

let solve (n, k, arr) =
    let rec loop sum i =
        if i < n + 1
        then
            let a = arr.(i - 1) in
            let b = arr.(i) in
            if a + b < k
            then
                (arr.(i) <- k - a - b;
                let new_sum = sum + (k - a) in
                loop new_sum (i + 1))
            else
                loop sum (i + 1)
        else
            (sum, Array.sub arr 1 n)
    in loop 0 1

let print (sum, array) =
    Printf.printf "%d\n" sum;
    Array.iter (Printf.printf "%d ") array;
    Printf.printf "\n"

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


let n = int_of_string (input_line stdin) in
for i = 0 to n-1 do
  let str = (input_line stdin) in
  let rep = ref true in
  if str.[0] = 'B' || str.[String.length str -1] = 'A' then rep := false
  else begin
    let tot = ref 0 in
    for j = 0 to String.length str -1 do
      if str.[j] = 'B' then incr tot else decr tot ;
      if !tot > 0 then rep := false
    done;
  end;
  if !rep then print_endline "YES" else print_endline "NO";
done;;

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

let rec create_char_list input input_length = function
    | 0 -> []
    | n -> (String.get input  (input_length-n))::(create_char_list input input_length (n-1))
;;

type acceptable_char = A | B | AB ;;

let rec run_case a_count = function
    | A, [x] -> false
    | _, [x] -> if (x=='B') then true else false
    | A, x::xs ->
        if (x=='A') then run_case (a_count+1) (AB, xs)
        else false
    | B, x::xs ->
        if (x=='B') then run_case (a_count-1) (A, xs)
        else false
    | AB, x::xs ->
        if (x=='B' && a_count==1) then run_case 0 (A, xs)
        else if (x=='B') then run_case (a_count-1) (AB, xs)
        else run_case (a_count+1) (AB, xs)
;;

let rec run_cases = function
    | 0 -> 0
    | n ->
        let str_case = read_word () in
        let lst_case = create_char_list str_case (String.length str_case) (String.length str_case) in
        if (run_case 0 (A, lst_case)) then begin
            print_string "YES";
            print_newline ();
            run_cases (n-1)
        end
        else begin
            print_string "NO";
            print_newline ();
            run_cases (n-1)
        end
;;

let num_cases = read_int () in
run_cases num_cases;;

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

let rec create_char_list input input_length = function
    | 0 -> []
    | n -> (String.get input  (input_length-n))::(create_char_list input input_length (n-1))
;;

type acceptable_char = A | B | AB ;;

let rec run_case = function
    | A, [x] -> false
    | _, [x] -> if (x=='B') then true else false
    | A, x::xs -> if (x=='A') then run_case (AB, xs) else false
    | B, x::xs -> if (x=='B') then run_case (A, xs) else false
    | AB, x::xs -> run_case (AB, xs)
;;

let rec run_cases = function
    | 0 -> 0
    | n ->
        let str_case = read_word () in
        let lst_case = create_char_list str_case (String.length str_case) (String.length str_case) in
        if (run_case (A, lst_case)) then begin
            print_string "YES";
            print_newline ();
            run_cases (n-1)
        end
        else begin
            print_string "NO";
            print_newline ();
            run_cases (n-1)
        end
;;

let num_cases = read_int () in
run_cases num_cases;;

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

let rec create_char_list input input_length = function
    | 0 -> []
    | n -> (String.get input  (input_length-n))::(create_char_list input input_length (n-1))
;;

type acceptable_char = A | B | AB ;;

let rec run_case = function
    | A, [x] -> false
    | _, [x] -> if (x=='B') then true else false
    | A, x::xs -> if (x=='A') then run_case (AB, xs) else false
    | B, x::xs -> if (x=='B') then run_case (A, xs) else false
    | AB, x::xs -> if (x=='B') then run_case (A, xs) else run_case (AB, xs)
;;

let rec run_cases = function
    | 0 -> 0
    | n ->
        let str_case = read_word () in
        let lst_case = create_char_list str_case (String.length str_case) (String.length str_case) in
        if (run_case (A, lst_case)) then begin
            print_string "YES";
            print_newline ();
            run_cases (n-1)
        end
        else begin
            print_string "NO";
            print_newline ();
            run_cases (n-1)
        end
;;

let num_cases = read_int () in
run_cases num_cases;;

let n = int_of_string (input_line stdin) in
for i = 0 to n-1 do
  let str = (input_line stdin) in
  let rep = ref true in
  if str.[0] = 'B' || str.[String.length str -1] = 'A' then rep := false
  else begin
    for j = 0 to String.length str -2 do
      if str.[j] = 'B' && str.[j+1] = 'B' then rep := false
    done;
  end;
  if !rep then print_endline "YES" else print_endline "NO";
done;;


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

let rec pow n = if n=0 then 1 else
  let m = n/2 in
  let p = pow m in
    (if m+m=n then 1 else 10)*p*p mod 210

let () =
  let n = read_int() in
    if n<=2 then print_string "-1\n"
    else if n=3 then print_string "210\n"
    else
      let x = pow (n-1) in
      let y = 210-x in
	print_string "1";
	for i=2 to n-3 do
	  print_string "0"
	done;
	Printf.printf "%03d\n" y

(* Codeforces 248B PaperWork done *)

open Big_int;;
open List;;

(* you can use either gr or rdln but not both *)
let gr () = Scanf.scanf " %d" (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;;

let sp = [2; 3; 5; 7];;

let cvo o p = (o * 10) mod p;;

(* remainders 10^n from dividing by 2 3 5 7 *)
let rec ostat n p o = match n with
| 0 -> o
| _ -> ostat (n-1) p (cvo o p);;

let checkonep o p k = ((o + k) mod p) = 0;;

let rec checklp lo lp k = match lo with
| [] -> true
| h :: t -> (checkonep h (List.hd lp) k) && (checklp t (List.tl lp) k);;

let rec findfirst lo lp k =
	if checklp lo lp k then k
	else findfirst lo lp (k+1);;

let rec pow10 n = match n with
| 0 -> Big_int.unit_big_int
| _ ->
	if n < 10 then Big_int.mult_int_big_int 10 (pow10 (n-1))
	else
		let a = n/2 in
		let b = n - 2*a in
		let pa = pow10 a in
		let pb = pow10 b in
		Big_int.mult_big_int pa (Big_int.mult_big_int pa pb);;

let main () =
	let n = gr() in
	let lo = List.map (fun p -> ostat (n-1) p 1) sp in
	let k = findfirst lo sp 1 in
	if n < 3 then print_string "-1"
	else begin
		(* print_string "lo = ";                               *)
		(* printlisti lo;                                      *)
		(* print_string ("\nk = " ^ (string_of_int k) ^ "\n"); *)
		let pown = pow10 (n-1) in
		let ret = Big_int.add_int_big_int k pown in
		print_string (Big_int.string_of_big_int ret)
	end;;

main();;


open Printf
open Scanf

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 () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_int()) in

  let rec remove d x = if x mod d = 0 then remove d (x/d) else x in

  for i=0 to n-1 do
    a.(i) <- remove 3 (remove 2 a.(i))
  done;

  if forall 1 (n-1) (fun i -> a.(0) = a.(i)) then printf "Yes\n" else printf "No\n"

open Printf
open Scanf

let id x = x

let rec gen_norm k x = if x mod k = 0 then gen_norm k (x / k) else x
let norm x = gen_norm 2 (gen_norm 3 x)

let () =
  let n = scanf " %d" id in
  let a = scanf " %d" norm in
  let rec check = function
    | 0 -> true
    | n -> (scanf " %d" norm = a) && check (n - 1) in
  printf "%s\n" (if check (n - 1) then "Yes" else "No")

open Printf
open Scanf

(* returns true iff f t = true for all i <= t <= j *)
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 () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_int()) in

  let rec remove d x = if x mod d = 0 then remove d (x/d) else x in

  for i=0 to n-1 do
    a.(i) <- remove 3 (remove 2 a.(i))

  done;

  if forall 1 (n-1) (fun i -> (a.(0) = a.(i)))
  then printf "Yes\n" else printf "No\n"

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

(* factorise : int -> (int * int) list *)
let reachable n =
    let rec aux d n =
        if n = 1 then true else
        if d > 3 then false else
        if n mod d = 0 then aux d (n / d) else aux (d + 1) n
    in
    aux 2 n

let rec gcd n m =
    if m = 0 then n else gcd m (n mod m)

let () = Scanf.scanf "%d\n" (fun n ->
    let a = Array.init n (fun _ -> Scanf.scanf "%d%_c" (fun x -> x)) in
    (* calculate lcm *)
    let g = Array.fold_left (fun a b -> gcd a b)
                            a.(0) (Array.sub a 1 (Array.length a - 1))
    in
    let ap = Array.map (fun n -> reachable (n / g)) a in
    print_endline (if List.for_all (fun x -> x) (Array.to_list ap) then "Yes" else "No"))


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

let input () =
    let total = (read ()) - 1 in
    let destination = (read ()) - 1 in
    let portals = Array.init total (fun n -> read ()) in
    (total, destination, portals)

let solve (total, destination, portals) =
    let rec loop i =
        if i < total
        then
            let portal_in = portals.(i) in
            let portal_out = portal_in + i in
            if portal_out < destination
            then
                loop (portal_out)
            else
                if portal_out = destination
                then
                    "YES"
                else
                    "NO"
        else
            failwith "Corrupted input"
    in loop 0

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

let () = Scanf.scanf "%d %d\n" (fun n t ->
  let p = Array.init (n - 1) (fun _ ->
    Scanf.scanf "%d%_c" (fun x -> x))
  in
  let rec f i =
    if i > t then false
    else if i = t then true
    else f (i + p.(i - 1))
  in
  print_endline (if f 1 then "YES" else "NO")
)

let words line = Str.split (Str.regexp " ") line;;

let transport curr arr = curr + arr.(curr - 1);;

let rec step arr start goal =
  if start = goal then
    "YES"
  else if goal < start then
    "NO"
  else
    step arr (transport start arr) goal
;;

let () =
  let nk = words (read_line ()) in
  let goal = int_of_string (List.nth nk 1) in
  let line = words (read_line ()) in
  let arr = Array.of_list (List.map int_of_string line)
  in
  Printf.printf "%s\n"
                (step arr 1 goal)
;;

open Printf
open Scanf

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let () =
  let n = read_int () in
  let t = read_int () in
  let a = Array.init n (fun i -> if i=0 then 0 else read_int()) in

  let rec scan c =
    if c = t then true else if c = n then false else scan (c + a.(c))
  in

  if scan 1 then printf "YES\n" else printf "NO\n"

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

let solve (n, m) =
    let rec loop i =
        if i < n
        then
            let portal_in = Scanf.scanf "%d " (fun p -> p) in
            let portal_out = portal_in + i in
            if portal_out < m
            then
                loop (i + 1)
            else
                if portal_out = m
                then
                    "YES"
                else
                    "NO"
        else
            failwith "Corrupted input"
    in loop 1

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


input_line stdin

let eval a b =
    if a + b == 0 then 0 else 1 ;;

    let rec solve acc =
        let i = Scanf.scanf "%d %d " eval in
            try solve (acc + i) with _ -> acc + i ;;

let ans = solve 0 ;;

Printf.printf "%d\n" ans ;;

let [n; m]= List.map (int_of_string) (Str.split (Str.regexp " ") (read_line ())) and ans = ref 0;;
for i= 1 to n do
	let s = List.map (int_of_string) (Str.split (Str.regexp " ") (read_line ())) in
		for j= 1 to m do
			if (List.nth s (2 * j - 1) = 1) || (List.nth s (2 * j - 2) = 1) then
				ans:= !ans + 1;
		done
done;;
print_int !ans

let read_int() = Scanf.scanf "%d " (fun x->x) ;;
let rec f = function 0 -> 0 | c -> ( read_int() lor read_int() ) + f (c-1) in print_int @@ f ( read_int() * read_int() )

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 n = read_int () in
  let m = read_int () in

  let awake a b = if a+b > 0 then 1 else 0 in
  let answer = sum 1 n (fun i ->
    sum 1 m (fun j ->
      let a = read_int() in
      let b = read_int() in
      awake a b
    )
  ) in

  printf "%d\n" answer


open Array;;
open String;;
let (|>) x f = f x
let identity = fun x -> x

let flip f y x = f x y

let readStrLn _ =
  try Some (Scanf.scanf "%s\n" identity)
  with _ -> None
let readIntLn _ = Scanf.scanf "%d\n" identity

let foldCont f a cont =
  let rec fold' a = match cont () with
      None -> a
    | Some x -> fold' (f a x)
  in fold' a

let _ = readIntLn ()
let temp = Scanf.scanf "%s\n" identity
let len = length temp;;
let f a b =
  let rec f' i = match b.[i] with
    c when c = temp.[i] ->  f' (i+1)
    | _ -> temp.[i] <- ' '; i
  in f' 0;;
(*let main () = *)
if len = 1 then (print_int 0)
else ( len |> flip (foldCont f) readStrLn |> print_int; ());;

let solve codes =
  let rec loop length =
    let num = String.get (List.hd codes) length in
    if List.for_all (fun str -> (String.get str length) = num) codes
    then loop (length+1)
    else length
  in
  loop 0

let rec input_strings n =
  if n <= 0 then []
  else (Scanf.scanf " %s" (fun x -> x)) :: (input_strings (n-1))

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

open Array;;
open String;;
let (|>) x f = f x
let identity = fun x -> x

let flip f y x = f x y

let readStrLn _ =
  try Some (Scanf.scanf "%s\n" identity)
  with _ -> None
let readIntLn _ = Scanf.scanf "%d\n" identity

let foldCont f a cont =
  let rec fold' a = match cont () with
      None -> a
    | Some x -> fold' (f a x)
  in fold' a

let _ = readIntLn ()
let temp = Scanf.scanf "%s\n" identity
let len = length temp ;;
let f a b =
  let rec f' i = match b.[i] with
    c when c = temp.[i] -> f' (i+1)
    | _ -> i
  in f' 0;;
(*let main () = *)
if len = 1 then (print_int 0)
else ( len |> flip (foldCont f) readStrLn |> print_int; ());;


let int_of_hex c = match c with
| '0' .. '9' -> int_of_char c - int_of_char '0'
| 'A' .. 'F' -> int_of_char c - int_of_char 'A' + 10
| _ -> failwith ""
let rec fix f x = f (fix f) x
let gcd = fix (fun f m n ->
  if n=0 then m else f n (m mod n))
;;
Scanf.(Array.(scanf " %d" @@ fun n ->
  let mat = make_matrix n n 0 in
  init n (fun i -> scanf " %s" @@ fun s ->
    String.iteri (fun j c ->
      let ih = int_of_hex c in
      mat.(i).(4*j)   <- ih land 8 / 8;
      mat.(i).(4*j+1) <- ih land 4 / 4;
      mat.(i).(4*j+2) <- ih land 2 / 2;
      mat.(i).(4*j+3) <- ih land 1;
    ) s
  ) |> ignore;
  let visited = make_matrix n n false in
  let g = ref n in
  let fy y x pt = fix (fun f y ->
    if y>=n || visited.(y).(x) || mat.(y).(x) <> pt then y-1 else f (y+1)) y in
  for i=0 to n-1 do
    for j=0 to n-1 do
      if visited.(i).(j) then ()
      else (
        let pat = mat.(i).(j) in
        let bottom = fy i j pat in
        let finalize x =
          for ii=i to bottom do
            for jj=j to x-1 do
              visited.(ii).(jj) <- true
            done
          done;
        in
        let p,q = fix (fun f (p,q) x ->
          if x>=n then (
            finalize x; (p,q)
          ) else
          let b1 = fy i x pat in
          if b1 <> bottom then (
            finalize x; (p,q)
          ) else f (p,q+1) (x+1)
        ) (bottom+1,1) (j+1) in
        g := gcd !g @@ gcd p q;
        (* Printf.printf "%2d %2d :: %2d %2d\n" i j p q;
        iter (fun b ->
          iter (fun f -> Printf.printf @@ if f then "t " else "_ ") b; print_newline ()
        ) visited *)
      )
    done;
  done;
  print_int @@ !g
))

let int_of_hex c = match c with
| '0' .. '9' -> int_of_char c - int_of_char '0'
| 'A' .. 'F' -> int_of_char c - int_of_char 'A' + 10
| _ -> failwith ""
let rv f p q = f q p
let divisors n = (* O(sqrt n) *)
  let rec f0 i a =
    if i*i > n then a
    else if i*i = n && n mod i = 0 then i::a
    else if n mod i = 0 then f0 (i+1) (i::(n/i)::a)
    else f0 (i+1) a in f0 1 []
;;
Scanf.(Array.(scanf " %d" @@ fun n ->
  let mat = make_matrix n n 0 in
  init n (fun i -> scanf " %s" @@ fun s ->
    String.iteri (fun j c ->
      let ih = int_of_hex c in
      mat.(i).(4*j)   <- ih land 8 / 8;
      mat.(i).(4*j+1) <- ih land 4 / 4;
      mat.(i).(4*j+2) <- ih land 2 / 2;
      mat.(i).(4*j+3) <- ih land 1;
    ) s
  ) |> ignore;
  (* iter (fun a ->
    iter (fun v ->
      Printf.printf "%d " v
    ) a; print_newline ()
  ) mat; *)
  let cml = make_matrix (n+1) (n+1) 0 in
  iteri (fun i a ->
    iteri (fun j _ ->
      cml.(i+1).(j+1) <- cml.(i+1).(j) + mat.(i).(j)
    ) a
  ) mat;
  iteri (fun j a ->
    iteri (fun i _ ->
      cml.(i+1).(j+1) <- cml.(i+1).(j+1) + cml.(i).(j+1)
    ) a
  ) mat;
  (* iter (fun a ->
    iter (fun v ->
      Printf.printf "%2d " v
    ) a; print_newline ()
  ) cml; *)
  let get d y x =
    let y,x = y+1,x+1 in
    cml.(y+d-1).(x+d-1) - cml.(y+d-1).(x-1) - cml.(y-1).(x+d-1) + cml.(y-1).(x-1)
  in
  divisors n
  |> List.sort (rv compare)
  |> List.iter (fun d ->
    if d=1 then (
      print_int 1; exit 0;
    );
    let f = ref true in
    init (n/d) (fun y ->
      init (n/d) (fun x ->
        let w = get d (y*d) (x*d) in
        if w=d*d || w=0 then ()
        else f := false
      ) |> ignore
    ) |> ignore;
    if !f then (
      print_int d; exit 0
    )
  )
));;

let int_of_hex c = match c with
| '0' .. '9' -> int_of_char c - int_of_char '0'
| 'A' .. 'F' -> int_of_char c - int_of_char 'A' + 10
| _ -> failwith ""
let divisors n = (* O(sqrt n) *)
  let rec f0 i a =
    if i*i > n then a
    else if i*i = n && n mod i = 0 then i::a
    else if n mod i = 0 then f0 (i+1) (i::(n/i)::a)
    else f0 (i+1) a in f0 1 []
let binary_search ng ok f = let d = if ng < ok then 1 else -1 in
  let rec f0 ng ok =
    if abs (ok - ng) <= 1 then ok
    else let mid = ng + (ok - ng) / 2 in if f mid then f0 ng mid else f0 mid ok
  in f0 (ng-d) (ok+d)
;;
Scanf.(Array.(scanf " %d" @@ fun n ->
  let mat = make_matrix n n 0 in
  init n (fun i -> scanf " %s" @@ fun s ->
    String.iteri (fun j c ->
      let ih = int_of_hex c in
      mat.(i).(4*j)   <- ih land 8 / 8;
      mat.(i).(4*j+1) <- ih land 4 / 4;
      mat.(i).(4*j+2) <- ih land 2 / 2;
      mat.(i).(4*j+3) <- ih land 1;
    ) s
  ) |> ignore;
  (* iter (fun a ->
    iter (fun v ->
      Printf.printf "%d " v
    ) a; print_newline ()
  ) mat; *)
  let cml = make_matrix (n+1) (n+1) 0 in
  iteri (fun i a ->
    iteri (fun j _ ->
      cml.(i+1).(j+1) <- cml.(i+1).(j) + mat.(i).(j)
    ) a
  ) mat;
  iteri (fun j a ->
    iteri (fun i _ ->
      cml.(i+1).(j+1) <- cml.(i+1).(j+1) + cml.(i).(j+1)
    ) a
  ) mat;
  (* iter (fun a ->
    iter (fun v ->
      Printf.printf "%2d " v
    ) a; print_newline ()
  ) cml; *)
  let get d y x =
    cml.(y+d).(x+d) - cml.(y+d).(x) - cml.(y).(x+d) + cml.(y).(x)
  in
  let divs = divisors n |> List.sort compare |> of_list in
  (* iter (Printf.printf "%2d; ") divs; print_newline (); *)
  print_int @@ divs.(
    binary_search (length divs-1) 0 (fun i ->
      let d = divs.(i) in
      let f = ref true in
      (* Printf.printf "%d:\n" d; *)
      init (n/d) (fun y ->
        init (n/d) (fun x ->
          let w = get d (y*d) (x*d) in
          (* Printf.printf "  %2d,%2d :: %2d\n" (y*d) (x*d) w; *)
          if w=d*d || w=0 then ()
          else f := false
        ) |> ignore
      ) |> ignore; !f
    ))
));;

let int_of_hex c = match c with
| '0' .. '9' -> int_of_char c - int_of_char '0'
| 'A' .. 'F' -> int_of_char c - int_of_char 'A' + 10
| _ -> failwith ""
let divisors n = (* O(sqrt n) *)
  let rec f0 i a =
    if i*i > n then a
    else if i*i = n && n mod i = 0 then i::a
    else if n mod i = 0 then f0 (i+1) (i::(n/i)::a)
    else f0 (i+1) a in f0 1 []
let binary_search ng ok f = let d = if ng < ok then 1 else -1 in
  let rec f0 ng ok =
    if abs (ok - ng) <= 1 then ok
    else let mid = ng + (ok - ng) / 2 in if f mid then f0 ng mid else f0 mid ok
  in f0 (ng-d) (ok+d)
;;
Scanf.(Array.(scanf " %d" @@ fun n ->
  let mat = make_matrix n n 0 in
  init n (fun i -> scanf " %s" @@ fun s ->
    String.iteri (fun j c ->
      let ih = int_of_hex c in
      mat.(i).(4*j)   <- ih land 8 / 8;
      mat.(i).(4*j+1) <- ih land 4 / 4;
      mat.(i).(4*j+2) <- ih land 2 / 2;
      mat.(i).(4*j+3) <- ih land 1;
    ) s
  ) |> ignore;
  (* iter (fun a ->
    iter (fun v ->
      Printf.printf "%d " v
    ) a; print_newline ()
  ) mat; *)
  let cml = make_matrix (n+1) (n+1) 0 in
  iteri (fun i a ->
    iteri (fun j _ ->
      cml.(i+1).(j+1) <- cml.(i+1).(j) + mat.(i).(j)
    ) a
  ) mat;
  iteri (fun j a ->
    iteri (fun i _ ->
      cml.(i+1).(j+1) <- cml.(i+1).(j+1) + cml.(i).(j+1)
    ) a
  ) mat;
  (* iter (fun a ->
    iter (fun v ->
      Printf.printf "%2d " v
    ) a; print_newline ()
  ) cml; *)
  let get d y x =
    cml.(y+d).(x+d) - cml.(y+d).(x) - cml.(y).(x+d) + cml.(y).(x)
  in
  let divs = divisors n |> List.sort compare |> of_list in
  print_int @@ divs.(
    binary_search (length divs-1) 0 (fun i ->
      let d = divs.(i) in
      let f = ref true in
      init (n/d) (fun y ->
        init (n/d) (fun x ->
          let w = get d y x in
          if w=d*d || w=0 then ()
          else f := false
        ) |> ignore
      ) |> ignore; !f
    ))
))

let int_of_hex c = match c with
| '0' .. '9' -> int_of_char c - int_of_char '0'
| 'A' .. 'F' -> int_of_char c - int_of_char 'A' + 10
| _ -> failwith ""
let rec fix f x = f (fix f) x
let gcd = fix (fun f m n ->
  if n=0 then m else f n (m mod n))
;;
Scanf.(Array.(scanf " %d" @@ fun n ->
  let mat = make_matrix n n 0 in
  init n (fun i -> scanf " %s" @@ fun s ->
    String.iteri (fun j c ->
      let ih = int_of_hex c in
      mat.(i).(4*j)   <- ih land 8 / 8;
      mat.(i).(4*j+1) <- ih land 4 / 4;
      mat.(i).(4*j+2) <- ih land 2 / 2;
      mat.(i).(4*j+3) <- ih land 1;
    ) s
  ) |> ignore;
  let visited = make_matrix n n false in
  let g = ref n in
  let rec visit y x c =
    if y >= n || x >= n || visited.(y).(x) || mat.(y).(x) <> c then 0,0
    else (
      visited.(y).(x) <- true;
      let p,q = visit (y+1) x c in
      let r,s = visit y (x+1) c in
      p+r+1,q+s+1
    )
  in
  for i=0 to n-1 do
    for j=0 to n-1 do
      if visited.(i).(j) then ()
      else (
        let p,q = visit i j mat.(i).(j) in
        g := gcd !g @@ gcd p q;
      )
    done;
  done;
  print_int @@ !g
))


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

(* kmp algorithm. *)
(* memo.(i) will store the length of the longest prefix of s
   that matches the tail of s1...si *)
let kmptable s n =
  let memo = Array.make n 0 in
    for i=1 to (n-1) do
      let rec loop j =
	if s.[i] = s.[j] then j+1 else if j=0 then 0 else loop memo.(j-1)
      in memo.(i) <- loop memo.(i-1)
    done;
    memo

(*
(* returns table.
   table.(i) will store the length of the longest prefix of ms
   that matches the tail of ts0...tsi *)
let tailtable ts n ms m =
  let memo = kmptable ms m in
  let table = Array.make n 0 in
    table.(0) <- if ts.[0] = ms.[0] then 1 else 0;
    for i=1 to (n-1) do
      let rec loop j =
	if ts.[i] = ms.[j] then j+1 else if j=0 then 0 else loop memo.(j-1)
      in
	table.(i) <- loop (if table.(i-1) < m then table.(i-1) else memo.(m-1))
    done;
    table
*)

let solve child n pattern =
  let m = String.length pattern in
  let memo = kmptable pattern m in

  let run_kmp s j =
    let n = String.length s in
    let rec outer_loop i count j = if i=n then (j, count) else
      let rec loop j =
	if s.[i] = pattern.[j] then j+1 else if j=0 then 0 else loop memo.(j-1)
      in
      let j = loop j in
      let count = if j=m then count+1 else count in
      let j = if j=m then memo.(j-1) else j in
	outer_loop (i+1) count j
    in
      outer_loop 0 0 j
  in

  let rec dfs v j ac =
(*    Printf.printf "at node %d (j=%d, ac=%d)\n" v j ac; *)
    List.fold_left (
      fun ac (w,s) ->
(*	Printf.printf "   edge: (%d, %d) = \"%s\"\n" v w s; *)
	let (j', count) = run_kmp s j in
	  dfs w j' (ac + count)
    ) ac child.(v)
  in
    dfs 0 0 0

let () =
  let n = read_int() in
  let child = Array.make n [] in
    for i=1 to n-1 do
      let p = read_int() -1 in
      let s = read_string() in
	child.(p) <- (i,s)::child.(p);
    done;

    let pattern = read_string() in

      Printf.printf "%d\n" (solve child n pattern)

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

(* kmp algorithm. *)
(* memo.(i) will store the length of the longest prefix of s
   that matches the tail of s1...si *)
let kmptable s n =
  let memo = Array.make n 0 in
    for i=1 to (n-1) do
      let rec loop j =
	if s.[i] = s.[j] then j+1 else if j=0 then 0 else loop memo.(j-1)
      in memo.(i) <- loop memo.(i-1)
    done;
    memo

(*
(* returns table.
   table.(i) will store the length of the longest prefix of ms
   that matches the tail of ts0...tsi *)
let tailtable ts n ms m =
  let memo = kmptable ms m in
  let table = Array.make n 0 in
    table.(0) <- if ts.[0] = ms.[0] then 1 else 0;
    for i=1 to (n-1) do
      let rec loop j =
	if ts.[i] = ms.[j] then j+1 else if j=0 then 0 else loop memo.(j-1)
      in
	table.(i) <- loop (if table.(i-1) < m then table.(i-1) else memo.(m-1))
    done;
    table
*)

let l2n c = (int_of_char c) - (int_of_char 'a')
let n2l n = char_of_int ((int_of_char 'a') + n)

let solve child n pattern =
  let m = String.length pattern in
  let memo = kmptable pattern m in
  let jupdate_array = Array.make_matrix m 26 (-1) in

  let rec jupdate c x j =
    if jupdate_array.(j).(x) >= 0 then jupdate_array.(j).(x) else
      let j' = if c = pattern.[j] then j+1 else if j=0 then 0 else jupdate c x memo.(j-1) in
	jupdate_array.(j).(x) <- j';
	j'
  in

  let run_kmp s j =
    let n = String.length s in
    let rec loop i count j = if i=n then (j, count) else
      let j = jupdate s.[i] (l2n s.[i]) j in
      let count = if j=m then count+1 else count in
      let j = if j=m then memo.(j-1) else j in
	loop (i+1) count j
    in
      loop 0 0 j
  in

  let rec dfs v j ac =
(*    Printf.printf "at node %d (j=%d, ac=%d)\n" v j ac; *)
    List.fold_left (
      fun ac (w,s) ->
(*	Printf.printf "   edge: (%d, %d) = \"%s\"\n" v w s; *)
	let (j', count) = run_kmp s j in
	  dfs w j' (ac + count)
    ) ac child.(v)
  in
    dfs 0 0 0

let () =
  let n = read_int() in
  let child = Array.make n [] in
    for i=1 to n-1 do
      let p = read_int() -1 in
      let s = read_string() in
	child.(p) <- (i,s)::child.(p);
    done;

    let pattern = read_string() in

      Printf.printf "%d\n" (solve child n pattern)


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

  let inc = Array.make n 0 in
  let id = Array.make n 0 in

  for i=0 to n-1 do
    inc.(i) <- if read_string() = "-" then -1 else 1;
    id.(i) <- read_int()
  done;

  let rogue = ref [] in

  let h = Hashtbl.create 10 in

  for i=0 to n-1 do
    if not (Hashtbl.mem h id.(i)) then (
      Hashtbl.replace h id.(i) true;
      if inc.(i) = -1 then rogue := id.(i) :: !rogue
    )
  done;

  let rec make_events ac i = if i=n then List.rev ac else
      make_events (id.(i) :: ac) (i+1)
  in

  let events = (!rogue) @ (make_events [] 0) in

  let h = Hashtbl.create 10 in

  let rec scan ev maxsize =
    match ev with [] -> maxsize
      | id::tail ->
	if Hashtbl.mem h id then (
	  Hashtbl.remove h id;
	  scan tail maxsize
	) else (
	  Hashtbl.replace h id true;
	  scan tail (max maxsize (Hashtbl.length h))
	)
  in

  let answer = scan events 0 in

  printf "%d\n" answer;

let () = Scanf.scanf "%d\n" (fun n ->
    let a = Array.make 1_000_000 false in
    let cur = ref 0 and best = ref 0 in
    for i = 1 to n do
        Scanf.scanf "%c %d\n" (fun c x ->
            if c = '-' && not a.(x - 1)
            then incr best
            else begin
                a.(x - 1) <- c = '+';
                if a.(x - 1) then begin
                    incr cur;
                    best := max !best !cur
                end else decr cur
            end)
    done;

    Printf.printf "%d\n" !best)


module A = Array
module L = List
module I = Int64

let pf = Printf.printf
let sf = Scanf.scanf
let read_int () = sf " %d" (fun x -> x)
let read_array read n = A.init n (fun _ -> read ())

let () =
  let t = read_int () in

  for _ = 1 to t do
    let n = read_int () in
    let a = read_array read_int n in
    let pos = A.make (n + 1) (-1) in
    A.iteri (fun i x -> pos.(x) <- i) a;

    let i = ref (n - 2) in
    while !i >= 0 && a.(!i) <= a.(!i + 1) do i := !i - 1 done;
    let decr_pos = !i + 1 in
    if (decr_pos = 0) then
      pf "0\n"
    else begin
      let diff = Array.make (n + 1) false in
      let cnt = ref 0 in
      let mx = ref 0 in
      i := 0;
      while !i < n do
        mx := max !mx pos.(a.(!i));
        if (not diff.(a.(!i))) then cnt := !cnt + 1;
        diff.(a.(!i)) <- true;
        if !i = !mx && !mx + 1 >= decr_pos then begin
          pf "%d\n" !cnt;
          i := n;
        end;
        i := !i + 1;
      done
    end
  done

let get_num () =
  let rec f acc =
    try
      let c = input_byte stdin in
      if int_of_char '0' <= c && c <= int_of_char '9' then
        f ((acc * 10) + c - int_of_char '0')
      else if acc > 0 then Some acc
      else None
    with End_of_file -> if acc > 0 then Some acc else None
  in
  f 0

module Int_Set = Set.Make (struct
  type t = int

  let compare = compare
end)

let solve_case () =
  match get_num () with
  | Some n ->
      let rec f i set pending prev =
        if i = n then set
        else
          let i = i + 1 in
          match get_num () with
          | Some a ->
              let a = if Int_Set.mem a set then 0 else a in
              if a < prev then
                let set = Int_Set.union set pending in
                f i set (Int_Set.singleton a) a
              else f i set (Int_Set.add a pending) a
          | _ -> exit 1
      in
      let s = f 0 Int_Set.empty Int_Set.empty 0 in
      let s = Int_Set.remove 0 s in
      let fold_f a x = x + 1 in
      let ans = Int_Set.fold fold_f s 0 in
      print_int ans
  | _ -> exit 1

let () =
  match get_num () with
  | Some t ->
      let rec f i =
        if i = 0 then ()
        else
          let () = solve_case () in
          let () = print_newline () in
          f (i - 1)
      in
      f t
  | _ -> exit 1


let () = Scanf.scanf "%d\n" (fun n ->
let maxw = 1000100 in
let fr = Array.init (maxw+10) (fun _ -> 0) in

let rec read_loop i =
	if i < n then
	begin
		let w = (Scanf.scanf "%d%_c" (fun x -> x)) in Array.set fr w (succ (Array.get fr w));
		read_loop (i+1)
	end
in read_loop 0;

let rec solve_loop i rem needed_steps =
	if i <= maxw then
		let total = rem + fr.(i) in
		solve_loop (i+1) (total/2) (needed_steps + (total mod 2))
	else
		Printf.printf "%d\n" needed_steps
in solve_loop 0 0 0);;

let () = Scanf.scanf "%d\n" (fun n ->
let maxw = 1000100 in
let fr = Array.init (maxw+10) (fun _ -> 0) in

let rec read_loop i =
	if i < n then
	begin
		let w = (Scanf.scanf "%d%_c" (fun x -> x)) in fr.(w) <- succ fr.(w);
		read_loop (i+1)
	end
in read_loop 0;

let rec solve_loop i rem needed_steps =
	if i <= maxw then
		let total = rem + fr.(i) in
		solve_loop (i+1) (total/2) (needed_steps + (total mod 2))
	else
		Printf.printf "%d\n" needed_steps
in solve_loop 0 0 0);;

let _ =
  let sb = Scanf.Scanning.stdib in
  let m = 1000030 in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n 0 in
    for i = 0 to n - 1 do
      a.(i) <- Scanf.bscanf sb "%d " (fun s -> s)
    done;
    Array.sort compare a;
    let b = Array.make m false in
      for i = 0 to n - 1 do
	let pos = ref a.(i) in
	  while b.(!pos) do
	    b.(!pos) <- false;
	    incr pos;
	  done;
	  b.(!pos) <- true
      done;
      let res = ref 0 in
	for i = 0 to m - 1 do
	  if b.(i)
	  then incr res
	done;
	Printf.printf "%d\n" !res

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

let () =
  let m = 1_000_100 in
  let a = Array.make m 0 in
  let n = read_int() in
  for i = 1 to n do
    let x = read_int() in
    a.(x) <- a.(x) + 1
  done;
  let rec go i carry acc =
    if i = m then acc
    else go (i+1) ((carry + a.(i)) / 2) (acc + ((carry + a.(i)) mod 2))
  in
  Printf.printf "%d\n" (go 0 0 0);;

let () = Scanf.scanf "%d\n" (fun n ->
    let w = Array.init n (fun _ -> Scanf.scanf "%d%_c" (fun x -> x)) in
    let s = Array.make 2_000_000 false in
    let on = ref 0 in

    let rec insert a =
        if s.(a) then begin
            s.(a) <- false;
            decr on;
            insert (a + 1)
        end else begin
            s.(a) <- true;
            incr on
        end
    in

    for i = 0 to n - 1 do
        insert w.(i)
    done;

    Printf.printf "%d\n" !on)

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n 0 in
    for i = 0 to n - 1 do
      a.(i) <- Scanf.bscanf sb "%d " (fun s -> s)
    done;
    let b = Array.make n 0 in
    let pos = ref 0 in
      b.(0) <- a.(0);
      for i = 1 to n - 1 do
	incr pos;
	b.(!pos) <- a.(i);
	while !pos > 0 && b.(!pos) = b.(!pos - 1) do
	  decr pos;
	  b.(!pos) <- b.(!pos) + 1
	done;
      done;
      Printf.printf "%d\n" (!pos + 1)

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n 0 in
    for i = 0 to n - 1 do
      a.(i) <- Scanf.bscanf sb "%d " (fun s -> s)
    done;
    Array.sort compare a;
    let b = Array.make n 0 in
    let pos = ref 0 in
      b.(0) <- a.(0);
      for i = 1 to n - 1 do
	incr pos;
	b.(!pos) <- a.(i);
	while !pos > 0 && b.(!pos) = b.(!pos - 1) do
	  decr pos;
	  b.(!pos) <- b.(!pos) + 1
	done;
      done;
      Printf.printf "%d\n" (!pos + 1)


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

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

let solve moves member_count =
    let rec solve_aux i ret =
        if i < String.length moves then begin
            if i >= 3 && moves.[i-1] == moves.[i-2]
                        && moves.[i-2] == moves.[i-3] then
                solve_aux (i+member_count) (ret+1)
            else
                solve_aux (i+member_count) ret
        end else ret
    in solve_aux 0 0
;;

let member_count = input_int in
let moves = input_string in
Printf.printf "%d\n" (solve moves member_count);

(* betaveros's polyglot adventure *)

let people = int_of_string (read_line ());;
let moves = read_line ();;

let canDrink s i = s.[i-1] == s.[i-2] && s.[i-2] == s.[i-3];;

let rec addDrinkCount s n i accum =
	if i >= String.length s then accum
	else addDrinkCount s n (i + n) (
		accum + if canDrink s i then 1 else 0);;

let solution = addDrinkCount moves people people 0 in
	print_endline (string_of_int solution);;

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

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

let solve moves member_count =
    let rec solve_aux i ret =
        if i < String.length moves then begin
            if i >= 3 && moves.[i-1] == moves.[i-2]
                        && moves.[i-2] == moves.[i-3] then
                solve_aux (i+1) (ret+1)
            else
                solve_aux (i+1) ret
        end else ret
    in solve_aux 0 0
;;

let member_count = input_int in
let moves = input_string in
Printf.printf "%d\n" (solve moves member_count);


let read_ints () =
  let str = read_line () in
  List.map (fun x -> int_of_string x) (Str.split (Str.regexp " +") str)
;;

let rec main (t : int) =
  if t = 0 then
    ()
  else
    let [a;b;c] = read_ints () in
    (if a < c then print_int 1
     else print_int (-1);
     print_string " ";
     let a2 = Int64.of_int a in
     let b2 = Int64.of_int b in
     let c2 = Int64.of_int c in
     if (Int64.mul a2 b2) > c2 then
       print_int b
    else
      print_int (-1);
    print_endline "";
    main (t-1))

;;

let n = read_int () in
main n

(* Codeforces 1373 A Donut Shops 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 shops a b c  =
    let n1 = if a < c then Int64.one else Int64.minus_one in
		let n2 = if c < (Int64.mul b a) then b else Int64.minus_one in
		(n1, n2);;

let do_one () =
    let a = grl () in
    let b = grl () in
    let c = grl () in
    let n1,n2 = shops a b c in
    Printf.printf "%Ld %Ld\n" n1 n2;;

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

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

main();;

(* Codeforces 1373 A Donut Shops 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 shops a b c  =
    let n1 = if a < c then 1 else -1 in
		let n2 = if c < (b * a) then b else -1 in
		(n1, n2);;

let do_one () =
    let a = gr () in
    let b = gr () in
    let c = gr () in
    let n1,n2 = shops a b c in
    Printf.printf "%d %d\n" n1 n2;;

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

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

main();;


open Printf
open Scanf

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

let () =
  let n = read_int () in
  let pair = Array.make (2*n) 0 in
  let pair_index = Array.make n 0 in
  for i=0 to n-1 do
    let (a,b) = read_pair() in
    let (a,b) = (a-1,b-1) in
    pair.(a) <- b;
    pair.(b) <- a;
    pair_index.(i) <- a
  done;

  let color = Array.make (2*n) 0 in

  let rec dfs1 v = if color.(v) = 0 then (
    color.(v) <- 1;
    dfs2 pair.(v)
  )
  and dfs2 v =
    color.(v) <- 2;
    dfs1 (v lxor 1)
  in

  for v=0 to 2*n-1 do dfs1 v done;

  for i=0 to n-1 do
    let a = pair_index.(i) in
    printf "%d %d\n" color.(a) color.(pair.(a))
  done

open Printf
open Scanf

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

let () =
  let n = read_int () in
  let pair = Array.make (2*n) 0 in
  let pair_index = Array.make n 0 in
  for i=0 to n-1 do
    let (a,b) = read_pair() in
    let (a,b) = (a-1,b-1) in
    pair.(a) <- b;
    pair.(b) <- a;
    pair_index.(i) <- a
  done;

  let color = Array.make (2*n) (-1) in

  let rec dfs0 v = if color.(v) < 0 then (
    color.(v) <- 0;
    dfs1 pair.(v)
  )
  and dfs1 v =
    color.(v) <- 1;
    dfs0 (v lxor 1)
  in

  for v=0 to 2*n-1 do dfs0 v done;

  for i=0 to n-1 do
    let a = pair_index.(i) in
    printf "%d %d\n" (1+color.(a)) (1+color.(pair.(a)))
  done

let n = read_int () in
let nn = 2 * n in
let io2chair = Array.make nn 0 and chair2io = Array.make nn 0 in
for i = 0 to nn - 1 do
    let chair = Scanf.scanf "%d " (fun x -> x - 1) in
    io2chair.(i) <- chair ; chair2io.(chair) <- i
done;
let out = Array.make nn 0 in
let rec fill_cycle i =
    if out.(i) == 0 then (
        out.(i) <- 1;
        let neighbor = chair2io.(io2chair.(i) lxor 1) in
            out.(neighbor) <- 2;
            fill_cycle (neighbor lxor 1)
    ) in
for i = 0 to nn - 1 do
    fill_cycle i
done;
Array.iter (Printf.printf "%d ") out

open Printf
open Scanf

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

let () =
  let n = read_int () in
  let pair = Array.make (2*n) 0 in
  let pair_index = Array.make n 0 in
  for i=0 to n-1 do
    let (a,b) = read_pair() in
    let (a,b) = (a-1,b-1) in
    pair.(a) <- b;
    pair.(b) <- a;
    pair_index.(i) <- a
  done;

  let color = Array.make (2*n) (-1) in

  let rec dfs0 v = if color.(v) < 0 then (
    color.(v) <- 0;
    dfs1 pair.(v)
  )
  and dfs1 v =
    color.(v) <- 1;
    dfs0 (v lxor 1)
  in

  let rec dfs_loop v =
    if v < 2*n then if color.(v) < 0 then dfs0 v else dfs_loop (v+1)
  in

  dfs_loop 0;

  for i=0 to n-1 do
    let a = pair_index.(i) in
    printf "%d %d\n" (1+color.(a)) (1+color.(pair.(a)))
  done


let str: string list ref = ref []

let cool = "cool"

let mem s =
  s = cool

let result l =
  List.exists (fun s -> mem s) l

let n = ["cool";"not bad";"great";"don't think so"; "don't touch me!";]

let g = ["don't even"; "are you serious?" ; "worse"; "terrible"; "go die in a hole"; "no way"]
let _ =
  let i = ref 0 in
  while !i < 10
  do
    Format.printf "%d@." !i;
    let s = read_line () in
    flush_all ();
    if List.mem s n then
      begin
        Format.printf "normal@.";
        exit 0
      end
    else if List.mem s g then
      begin
        Format.printf "grumpy@.";
        exit 0
      end
    else
      incr i
  done

module StringSet = Set.Make(String)

type cat =
  | Normal
  | Grumpy

let cats = [
  Normal, StringSet.of_list [
    "great!";
    "don't think so";
    "nod bad";
    "don't touch me";
    "cool"
  ]
  ; Grumpy, StringSet.of_list [
    "don't even";
    "are you serious?";
    "go die in a hole";
    "no way";
    "worse";
    "terrible"
  ]
]

let cats_re = [
  Normal, List.map Str.regexp [
    "cool";
    "great";
  ]
  ; Grumpy, List.map Str.regexp [
    "die";
    "away";
    "even";
  ]
]

let find_opt (f: 'a -> bool) (l: 'a list): 'a option =
  try Some (List.find f l) with
  | Not_found -> None

let determine_heuristic (a: string): cat option =
  (* didn't realize that a simple string find is so hard ... *)
  let str_exist p s =
    try Str.search_forward p s 0; true with
    | Not_found -> false
  in
  match find_opt (fun (_, res) -> List.exists (fun re -> str_exist re a) res) cats_re with
  | None -> None
  | Some (c, _) -> Some c

let rec determine (i: int): cat =
  if i >= 10
  then Grumpy
  else begin
    i |> string_of_int |> print_endline;
    let reaction = read_line () in
    (* OCaml 4.02.1 has no List.find_opt ... *)
    if reaction = "no"
    then i + 1 |> determine
    else
      let found = List.find_all (fun (_, res) -> StringSet.mem reaction res) cats in
      match found with
      | (c, _) :: _ -> c
      | [] -> match determine_heuristic reaction with
        | Some c -> c
        | None -> i + 1 |> determine
  end

let () =
  (match determine 0 with
  | Normal -> "normal"
  | Grumpy -> "grumpy") |> print_endline

let str = ref []

let cool = "cool"

let mem s =
  s = cool

let result l =
  List.exists (fun s -> mem s) l

let _ =
  for i = 0 to 9
  do
    Format.printf "%d@." i;
    flush_all ();
    str := read_line ()::!str
  done;
  if result !str then
    Format.printf "normal@."
  else
    Format.printf "grumpy@."

let str: string list ref = ref []

let cool = "cool"

let mem s =
  s = cool

let result l =
  List.exists (fun s -> mem s) l

let n = ["cool";"not bad";"great";"don't think so"; "don't touch me!";]

let _ =
  let i = ref 0 in
  while !i < 10
  do
    Format.printf "%d@." !i;
    let s = read_line () in
    flush_all ();
    if s = "don't think so" then
      ignore(1/0)
    else if s = "don't touch me!" then
      ignore(1/0)
    else if List.mem s n then
      begin
        Format.printf "normal@.";
        exit 0
      end
    else if s = "no" then
      incr i
    else
      begin
        Format.printf "grumpy@.";
        exit 0
      end
  done

let str = ref []

let cool = "cool"

let mem s =
  s = cool

let result l =
  List.exists (fun s -> mem s) l

let _ =
  for i = 0 to 9
  do
    Format.printf "%d@." i;
    flush_all ();
    str := read_line ()::!str
  done;
  if result !str then
    Format.printf "grumpy@."
  else
    Format.printf "grumpy@."

let str: string list ref = ref []

let cool = "cool"

let mem s =
  s = cool

let result l =
  List.exists (fun s -> mem s) l

let n = ["cool";"not bad";"great";"don't think so"; "don't touch me!";]

let _ =
  let i = ref 0 in
  while !i < 10
  do
    Format.printf "%d@." !i;
    let s = read_line () in
    flush_all ();
    if List.mem s n then
      begin
        Format.printf "normal@.";
        exit 0
      end
    else if s = "no" then
      incr i
    else
      begin
        Format.printf "grumpy@.";
        exit 0
      end
  done

let str: string list ref = ref []

let cool = "cool"

let mem s =
  s = cool

let result l =
  List.exists (fun s -> mem s) l

let n = ["cool";"not bad";"great";"don't think so"; "don't touch me!";]

let _ =
  let i = ref 0 in
  while !i < 10
  do
    Format.printf "%d@." !i;
    let s = read_line () in
    flush_all ();
    if s = "don't think so" then
      begin
        Format.printf "grumpy@.";
        exit 0
      end
    else if List.mem s n then
      begin
        Format.printf "normal@.";
        exit 0
      end
    else if s = "no" then
      incr i
    else
      begin
        Format.printf "grumpy@.";
        exit 0
      end
  done

let str = ref []

let cool = "cool"

let mem s =
  Str.string_match (Str.regexp cool) s 0

let result l =
  List.exists (fun s -> mem s) l

let _ =
  for i = 0 to 9
  do
    Format.printf "%d@." i;
    flush_all ();
    str := read_line ()::!str
  done;
  if result !str then
    Format.printf "normal@."
  else
    Format.printf "grumpy@."

let str = ref []

let cool = "cool"

let mem s =
  s = cool

let result l =
  List.exists (fun s -> mem s) l

let _ =
  for i = 0 to 9
  do
    Format.printf "%d@." i;
    flush_all ();
    str := read_line ()::!str
  done;
  if result !str then
    Format.printf "normal@."
  else
    Format.printf "normal@."

let str: string list ref = ref []

let cool = "cool"

let mem s =
  s = cool

let result l =
  List.exists (fun s -> mem s) l

let n = ["cool";"not bad";"great";"don't think so"; "don't touch me!";]

let _ =
  let i = ref 0 in
  while !i < 10
  do
    Format.printf "%d@." !i;
    let s = read_line () in
    flush_all ();
    if s = "don't touch me!" then
      begin
        Format.printf "grumpy@.";
        exit 0
      end
    else if List.mem s n then
      begin
        Format.printf "normal@.";
        exit 0
      end
    else if s = "no" then
      incr i
    else
      begin
        Format.printf "grumpy@.";
        exit 0
      end
  done

module StringSet = Set.Make(String)

type cat =
  | Normal
  | Grumpy

let cats = [
  Normal, StringSet.of_list [
    "great!";
    "don't think so";
    "nod bad";
    "don't touch me";
    "cool"
  ]
  ; Grumpy, StringSet.of_list [
    "don't even";
    "are you serious?";
    "go die in a hole";
    "no way";
    "worse";
    "terrible"
  ]
]

let rec determine (i: int): cat =
  if i >= 10
  then Grumpy
  else begin
    i |> string_of_int |> print_endline;
    let reaction = read_line () in
    (* OCaml 4.02.1 has no List.find_opt ... *)
    let found = List.find_all (fun (_, res) -> StringSet.mem reaction res) cats in
    match found with
    | (c, _) :: _ -> c
    | [] -> i + 1 |> determine
  end

let () =
  (match determine 0 with
  | Normal -> "normal"
  | Grumpy -> "grumpy") |> print_endline

module StringSet = Set.Make(String)

type cat =
  | Normal
  | Grumpy

let cats = [
  Normal, StringSet.of_list [
    "great!";
    "don't think so";
    "nod bad";
    "don't touch me";
    "cool"
  ]
  ; Grumpy, StringSet.of_list [
    "don't even";
    "are you serious?";
    "go die in a hole";
    "no way";
    "worse";
    "terrible"
  ]
]

let rec determine (i: int): cat =
  i |> string_of_int |> print_endline;
  let reaction = read_line () in
  (* OCaml 4.02.1 has no List.find_opt ... *)
  let found = List.find_all (fun (_, res) -> StringSet.mem reaction res) cats in
  match found with
  | (c, _) :: _ -> c
  | [] -> i + 1 |> determine

let () =
  (match determine 0 with
  | Normal -> "normal"
  | Grumpy -> "grumpy") |> print_endline

let str: string list ref = ref []

let cool = "cool"

let mem s =
  s = cool

let result l =
  List.exists (fun s -> mem s) l

let n = ["cool";"not bad";"great";"don't think so"; "don't touch me!";]

let _ =
  let i = ref 0 in
  while !i < 10
  do
    Format.printf "%d@." !i;
    let s = read_line () in
    flush_all ();
    ignore(1/0);
    if s = "don't think so" then
      ignore(1/0)
    else if s = "don't touch me!" then
      ignore(1/0)
    else if List.mem s n then
      begin
        Format.printf "normal@.";
        exit 0
      end
    else if s = "no" then
      incr i
    else
      begin
        Format.printf "grumpy@.";
        exit 0
      end
  done

let str: string list ref = ref []

let cool = "cool"

let mem s =
  s = cool

let result l =
  List.exists (fun s -> mem s) l

let n = ["cool";"not bad";"great";"don't think so"; "don't touch me!";]

let _ =
  let i = ref 0 in
  while !i < 10
  do
    Format.printf "%d@." !i;
    let s = read_line () in
    flush_all ();
    if s = "don't think so" then
      while true do () done
    else if s = "don't touch me!" then
      while true do () done
    else if List.mem s n then
      begin
        Format.printf "normal@.";
        exit 0
      end
    else if s = "no" then
      incr i
    else
      begin
        Format.printf "grumpy@.";
        exit 0
      end
  done


let n = read_int ()
let a = read_line () |> Str.(split (regexp " ")) |> List.map int_of_string

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

type node =
  { level : int;
    up    : int option;
  }

let add_node map value =
  let lefts, _, rights = IntMap.split value map in
  let up, {level} = match IntMap.is_empty lefts, IntMap.is_empty rights with
  | true, true -> value, {level = 0; up = None}
  | false, true -> IntMap.max_binding lefts
  | true, false -> IntMap.min_binding rights
  | false, false ->
     let value_left, left = IntMap.max_binding lefts in
     let value_right, right = IntMap.min_binding rights in
     if left.level > right.level
     then value_left, left
     else value_right, right in
  IntMap.add value { level = level + 1; up = Some up; } map

let node_map = List.fold_left add_node IntMap.empty a

let () =
  List.tl a
  |> List.iter (fun x -> let {up = Some y} = IntMap.find x node_map in
                         Printf.printf "%d " y);
  print_newline ()

let n = read_int ()
let a = read_line () |> Str.(split (regexp " ")) |> List.map int_of_string

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

type node =
  { value : int;
    level : int;
    up    : int option;
    left  : int option;
    right : int option;
  }

let new_node value =
  { value;
    left = None;
    right = None;
    level = 0;
    up = None }

let add_node map value =
  let lefts, _, rights = IntMap.split value map in
  let node = new_node value in
  let attached_left () =
    let _, left = (IntMap.max_binding lefts) in
    map
    |> IntMap.add value { node with left = Some left.value;
                                    level = left.level + 1;
                                    up = Some left.value; }
    |> IntMap.add left.value { left with right = Some node.value } in
  let attached_right () =
    let _, right = (IntMap.min_binding rights) in
    map
    |> IntMap.add value { node with right = Some right.value;
                                    level = right.level + 1;
                                    up = Some right.value; }
    |> IntMap.add right.value { right with left = Some node.value } in
  match IntMap.is_empty lefts, IntMap.is_empty rights with
  | true, true -> IntMap.singleton value node
  | false, true -> attached_left ()
  | true, false -> attached_right ()
  | false, false ->
     let _, left = (IntMap.max_binding lefts) in
     let _, right = (IntMap.min_binding rights) in
     if right.level > left.level then attached_right() else attached_left()

let node_map = List.fold_left add_node IntMap.empty a

let () =
  List.tl a
  |> List.iter (fun x -> let {up = Some y} = IntMap.find x node_map in
                         Printf.printf "%d " y);
  print_newline ()

let n = read_int ()
let a = read_line () |> Str.(split (regexp " ")) |> List.map int_of_string

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

type node =
  { level : int;
    up    : int;
  }

let add_node map value =
  let lefts, _, rights = IntMap.split value map in
  let up, {level} = match IntMap.is_empty lefts, IntMap.is_empty rights with
  | true, true -> value, {level = 0; up = value}
  | false, true -> IntMap.max_binding lefts
  | true, false -> IntMap.min_binding rights
  | false, false ->
     let value_left, left = IntMap.max_binding lefts in
     let value_right, right = IntMap.min_binding rights in
     if left.level > right.level
     then value_left, left
     else value_right, right in
  IntMap.add value { level = level + 1; up; } map

let node_map = List.fold_left add_node IntMap.empty a

let () =
  List.tl a
  |> List.iter (fun x -> let {up} = IntMap.find x node_map in
                         Printf.printf "%d " up);
  print_newline ()

(* 2:35 *)
open Printf
open Scanf

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let () =
  let n = read_int () in
  let seq = Array.init n (fun i -> read_int()) in
  let perm = Array.init n (fun i -> i) in

  Array.sort (fun i j -> compare seq.(i) seq.(j)) perm;
  let nseq = Array.make n 0 in
  for i=0 to n-1 do
    nseq.(perm.(i)) <- i
  done;

  let next = Array.make n 0 in
  let prev = Array.make n 0 in

  for i=0 to n-1 do
    prev.(i) <- i-1;
    next.(i) <- if i = n-1 then -1 else i+1
  done;

  let answer = Array.make n 0 in

  for i = n-1 downto 1 do
    let togo = nseq.(i) in
    let get k = if k<0 then -1 else perm.(k) in
    answer.(perm.(togo)) <- max (get next.(togo)) (get prev.(togo));
    (* now delete togo from the doubly linked list *)
    let (pj, nj) = (prev.(togo), next.(togo)) in
    if pj >= 0 then next.(pj) <- nj;
    if nj >= 0 then prev.(nj) <- pj;
  done;

  for i=1 to n-1 do
    printf "%d " seq.(answer.(i))
  done;
  print_newline()

let n = read_int ()
let a = read_line () |> Str.(split (regexp " ")) |> List.map int_of_string

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

type node =
  { level : int;
    up    : int option;
  }

let add_node map value =
  let lefts, _, rights = IntMap.split value map in
  let up, {level} = match IntMap.is_empty lefts, IntMap.is_empty rights with
  | true, true -> value, {level = 0; up = None}
  | false, true -> IntMap.max_binding lefts
  | true, false -> IntMap.min_binding rights
  | false, false ->
     let value_left, left = IntMap.max_binding lefts in
     let value_right, right = IntMap.min_binding rights in
     if right.level > left.level
     then value_left, left
     else value_right, right in
  IntMap.add value { level = level + 1; up = Some up; } map

let node_map = List.fold_left add_node IntMap.empty a

let () =
  List.tl a
  |> List.iter (fun x -> let {up = Some y} = IntMap.find x node_map in
                         Printf.printf "%d " y);
  print_newline ()


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

let () =
    let n = read_int () in
    let s = Array.init n read_int
    and cnt = Array.make 101 0
    and nice = ref 0
    and swap = ref false in
    for i = 0 to n - 1 do
        cnt.(s.(i)) <- cnt.(s.(i)) + 1;
        if cnt.(s.(i)) = 1 then incr nice
        else if cnt.(s.(i)) = 2 then decr nice
        else if cnt.(s.(i)) = 3 then swap := true
    done;
    if !nice mod 2 = 1 && !swap = false then printf "NO\n"
    else begin
        printf "YES\n";
        let move = ref (!nice / 2) in
        if !nice mod 2 = 0 then swap := false;
        for i = 0 to n - 1 do
            if cnt.(s.(i)) = 1 then
                if !move > 0 then begin
                    printf "B";
                    decr move
                end
                else printf "A"
            else if cnt.(s.(i)) >= 3 then
                if !swap then begin
                    printf "B";
                    swap := false
                end
                else printf "A"
            else printf "A"
        done
    end

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

module ISet = Set.Make (Int64)
module IMap = Map.Make (Int64)
let () =
	let n = get_i64 0 in
	let a = input_i64_array n in
	let f,z = ref false,ref (-1L) in
	let o,n = Array.fold_left (fun (o,n) v ->
		if IMap.exists (fun u _ -> u = v) n then (
			let p = (IMap.find v n) + 1L in
			(* printf "  %Ld\n" p; *)
			if p >= 3L then (
				f := true;z := v
			); (o,IMap.add v p n)
		) else if ISet.exists ((=) v) o then (ISet.remove v o,
			IMap.add v 2L n
		) else (ISet.add v o,n)
	) (ISet.empty,IMap.empty) a
	in
	(* printf "%Ld\n" !z; *)
	let w,x = ISet.elements o |> llen, IMap.bindings n |> llen in
	(* printf "%Ld %Ld\n" w x; *)
	if w mod 2L = 1L && not !f then (
		printf "NO\n"
	) else if w mod 2L = 0L then (
		printf "YES\n";
		Array.fold_left (fun m v ->
			if IMap.exists (fun u _ -> u = v) n then (printf "A"; m)
			else (printf (if m then "A" else "B"); (not m))
		) true a
		|> ignore;
	) else (
		printf "YES\n";
		Array.fold_left (fun (m,q) v ->
			if IMap.exists (fun u _ -> u = v) n then (
				(* if q then (printf "B";(m,false))
				else (printf "A"; (m,q)) *)
				(* printf "%Ld %Ld\n" v !z; *)
				if v = !z then (
					printf "B"; z := -1L; (m,q);
				) else (
					printf "A"; (m,q)
				)
			) else (printf (if m then "A" else "B"); (not m,q))
		) (true,true) a
		|> ignore;
	)

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

module ISet = Set.Make (Int64)
module IMap = Map.Make (Int64)
let () =
	let n = get_i64 0 in
	let a = input_i64_array n in
	let f,z = ref false,ref (-1L) in
	let o,n = Array.fold_left (fun (o,n) v ->
		if IMap.exists (fun u _ -> u = v) n then (
			let p = (IMap.find v n) + 1L in
			printf "  %Ld\n" p;
			if p >= 3L then (
				f := true;z := v
			); (o,IMap.add v p n)
		) else if ISet.exists ((=) v) o then (ISet.remove v o,
			IMap.add v 2L n
		) else (ISet.add v o,n)
	) (ISet.empty,IMap.empty) a
	in
	(* printf "%Ld\n" !z; *)
	let w,x = ISet.elements o |> llen, IMap.bindings n |> llen in
	(* printf "%Ld %Ld\n" w x; *)
	if w mod 2L = 1L && not !f then (
		printf "NO\n"
	) else if w mod 2L = 0L then (
		printf "YES\n";
		Array.fold_left (fun m v ->
			if IMap.exists (fun u _ -> u = v) n then (printf "A"; m)
			else (printf (if m then "A" else "B"); (not m))
		) true a
		|> ignore;
	) else (
		printf "YES\n";
		Array.fold_left (fun (m,q) v ->
			if IMap.exists (fun u _ -> u = v) n then (
				(* if q then (printf "B";(m,false))
				else (printf "A"; (m,q)) *)
				(* printf "%Ld %Ld\n" v !z; *)
				if v = !z then (
					printf "B"; z := -1L; (m,q);
				) else (
					printf "A"; (m,q)
				)
			) else (printf (if m then "A" else "B"); (not m,q))
		) (true,true) a
		|> ignore;
	)

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

module ISet = Set.Make (Int64)
let () =
	let n = get_i64 0 in
	let a = input_i64_array n in
	let o,n = Array.fold_left (fun (o,n) v ->
		if ISet.exists ((=) v) n then (o,n)
		else if ISet.exists ((=) v) o then (ISet.remove v o, ISet.add v n)
		else (ISet.add v o,n)
	) (ISet.empty,ISet.empty) a
	in
	if (ISet.elements o |> llen) mod 2L = 1L then (
		printf "NO\n"
	) else (
		printf "YES\n";
		Array.fold_left (fun m v ->
			if ISet.exists ((=) v) n then (printf "A"; m)
			else (printf (if m then "A" else "B"); (not m))
		) true a
		|> ignore;
	)