#load "str.cma";;

let rec iter ls cur height =
  match ls with
	  [] -> cur
	| h::ls' ->
	  if height <= (Int64.add h cur) then iter ls' cur (Int64.add h cur)
	  else iter ls' (Int64.sub height h) height ;;

let solve ls =
  iter (List.tl ls) 0L (List.hd ls);;

let get_input () =
  let n = int_of_string (read_line ()) and
	  ls = List.map (fun x -> Int64.of_int (int_of_string x))
	(Str.split (Str.regexp "[ \t]+") (read_line ())) in
  (n, ls);;

let (n, ls) = get_input () in
if n == 1 then Printf.printf "0\n"
else Printf.printf "%Ld\n" (solve ls)

let _ =
  let n = Scanf.scanf " %d" (fun x -> x) in
  let b = ref 0 in
  let s = ref Int64.zero in
  for i = 0 to n - 1 do
    let a = Scanf.scanf " %d" (fun x -> x) in
    if !b > a then s := Int64.add !s (Int64.of_int (!b - a));
    b := a
  done;
  print_string (Int64.to_string !s)
;;

let _ =
  let n = Scanf.scanf " %d" (fun x -> x) in
  let b = ref 0 in
  let s = ref 0 in
  for i = 0 to n - 1 do
    let a = Scanf.scanf " %d" (fun x -> x) in
    s := !s + max 0 (!b - a);
    b := a
  done;
  print_int !s
;;

#load "str.cma";;

let rec iter ls cur height =
  match ls with
	  [] -> cur
	| h::ls' ->
	  if height <= (h + cur) then iter ls' cur (h + cur)
	  else iter ls' ((height - h - cur) + cur) height ;;

let solve ls =
  iter (List.tl ls) 0 (List.hd ls);;

let get_input () =
  let n = int_of_string (read_line ()) and
	  ls = List.map int_of_string (Str.split (Str.regexp "[ \t]+") (read_line ())) in
  (n, ls);;

let (n, ls) = get_input () in
if n == 1 then Printf.printf "0\n"
else Printf.printf "%d\n" (solve ls)

#load "str.cma";;

let rec iter ls cur height =
  match ls with
	  [] -> cur
	| h::ls' ->
	  if height <= (h + cur) then iter ls' cur (h + cur)
	  else iter ls' ((height - h - cur) + cur) height ;;

let solve ls =
  iter (List.tl ls) 0 (List.hd ls);;

let get_input () =
  let n = int_of_string (read_line ()) and
	  ls = List.map int_of_string (Str.split (Str.regexp "[ \t]+") (read_line ())) in
  (n, ls);;

let (n, ls) = get_input () in
if n == 1 then Printf.printf "1\n"
else Printf.printf "%d\n" (solve ls)


let modul x = Int64.sub x (Int64.mul (Int64.div x (Int64.of_int 998244353)) (Int64.of_int 998244353))

let rec  test x = match x with
  |2 -> Int64.of_int 1
  |r when r mod 2 = 0 -> let t = r/2 in
      modul(Int64.mul (modul (Int64.mul (Int64.of_int(t)) (Int64.of_int(t))))  (test (x-2)))
  |_ -> Int64.zero
;;

let n = int_of_string(input_line stdin) in
for i = 0 to n-1 do
  let num = int_of_string(input_line stdin) in
  print_endline (Int64.to_string (test num))
done;
;;

(* https://codeforces.com/problemset/problem/1658/B *)
open Printf
open Scanf
module LL = Int64

let maxn = 1001
let q = 998244353L
let ( %% ) x y = LL.sub x ( LL.mul y (LL.div x y) )
let ( ** ) x y = LL.mul x y
let ( // ) x y = LL.div x y
let (!) x = LL.of_int x

let fac = Array.make maxn 1L;;
for i = 1 to maxn-1 do
    fac.(i) <- !i ** fac.(i-1) %% q
done

let solve n =
    if n mod 2 = 1
        then 0L
    else
        let m = n/2 in
        fac.(m) ** fac.(m) %% q

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

(* https://codeforces.com/problemset/problem/1658/B *)
open Printf
open Scanf

let maxn = 100
let q = 998244353

let fac : int array = Array.make 1001 1;;
for i = 1 to 1000 do
    fac.(i) <- i * fac.(i-1) mod q
done

let solve n =
    if n mod 2 = 1
        then 0
    else
        let m = n/2 in
        fac.(m) * fac.(m) mod q

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

(* https://codeforces.com/problemset/problem/1658/B *)
open Printf
open Scanf

let maxn = 100
let q = 998244353

let fac : int array = Array.make 1001 1;;
for i = 1 to 1000 do
    fac.(i) <- i * fac.(i-1) mod q
done

let solve n =
    if n mod 2 = 1
        then 0
    else
        let m = n/2 in
        fac.(m) * fac.(m) mod q

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


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

let get n =
  let rec test k =
    let tmp = (1 lsl k) - 1 in
    if n mod tmp = 0 then n / tmp
    else test (k + 1) in
  test 2
;;

let _ =
  let t = scan_int () in
  for i = 1 to t do
    Printf.printf "%d\n" (get (scan_int ()))
  done
;;

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

let rec pow x = function
| 0 -> 1
| n -> x * pow x (n - 1);;

let rec solve n k =
   if n mod ((pow 2 k) - 1) = 0 then
      n / ((pow 2 k) - 1)
   else
      solve n (k + 1)
;;

for iTest = 1 to nbTests
do
   let nbBonbons = Scanf.scanf " %d" (fun x -> x) in

   print_int (solve nbBonbons 2);
   print_newline ()
done;;


let (@@) f x = f x ;;

let solve p n arr =
  let htbl = Array.create p false in
  try
    for i = 0 to n - 1 do
      let h = arr.(i) mod p in
      match htbl.(h) with
      | false -> htbl.(h) <- true
      | true -> Printf.printf "%d\n" @@ i + 1; raise Exit
    done;
    Printf.printf "-1\n";
  with
  | Exit -> ()
;;

let () =
  Scanf.scanf "%d %d " (fun p n ->
    let arr = Array.create n 0 in
    for i = 0 to n - 1 do
      Scanf.scanf "%d " (fun x -> arr.(i) <- x);
    done;
    solve p n arr)
;;

(**D. The Child and Sequence*)

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

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

let hashv = Array.create p 0;;

exception BreakLoop;;

let j = ref 0;;
let rslt = ref (-1);;
try
  for i = 1 to n do
    j := !j + 1;
    let x = Scanf.bscanf Scanf.Scanning.stdin "%d " ( fun x -> x) in
    let y = x mod p in
    match hashv.(y) with
    | 0 -> hashv.(y) <- 1
    | 1 -> raise BreakLoop
  done;
with  BreakLoop -> rslt := !j;;

printf "%d\n" !rslt;;


let score_without w =
    let a = ref (w.[0]) and res = ref 1 in
         for i = 1 to String.length w - 1 do
             if w.[i] <> !a
                 then (incr res; a := w.[i]);
         done;
    !res

let score w =
    let l = String.length w and s = score_without w in
         s + min 2 (l - s)

let main () =
    let w = Scanf.scanf "%d %s" (fun _ w -> w) in
        Printf.printf "%d" (score w);;

main ();;

let n = read_int();;
let s = input_line stdin;;
let dp = Array.make_matrix (n+1) 3 0;;

let rec solve i =
if (i=0) then (0,0,0) else
begin
    let (r0,r1,r2) = solve (i-1)
    and ib b = if(b) then 1 else 0 in
    (ib(s.[i]<>s.[i-1]) + r0, max (ib(s.[i]<>s.[i-1]) + r1) (ib(not(s.[i]<>s.[i-1])) + r0), max (ib(s.[i]<>s.[i-1]) + r2) (ib(not(s.[i]<>s.[i-1])) + r1))
end;;

let (a,b,c) = solve(n-1);;
Printf.printf "%d" (1 + max a (max b c));;

open Printf;;
open Scanf;;

let n = read_int();;
let s = input_line stdin;;
let dp = Array.make_matrix (n+1) 3 0;;
for i=1 to n-1 do
    let aux1 = if s.[i]<>s.[i-1] then 1 + dp.(i-1).(1) else dp.(i-1).(1)
    and aux2 = if s.[i]<>s.[i-1] then dp.(i-1).(0) else 1 + dp.(i-1).(0)
    and aux3 = if s.[i]<>s.[i-1] then 1 + dp.(i-1).(2) else dp.(i-1).(2)
    and aux4 = if s.[i]<>s.[i-1] then dp.(i-1).(1) else 1 + dp.(i-1).(1)
    in  let res1 = max aux1 aux2
        and res2 = max aux3 aux4 in
    dp.(i).(0) <- if s.[i]<>s.[i-1] then 1 + dp.(i-1).(0) else dp.(i-1).(0);
    dp.(i).(1) <- res1;
    dp.(i).(2) <- res2;
done;;
printf "%d" (1 + max dp.(n-1).(0) (max dp.(n-1).(1) dp.(n-1).(2)));;

open Printf;;
open Scanf;;

let bi i = if(i=0) then false else true;;
let ib b = if(b) then 1 else 0;;

let n = read_int();;
let s = input_line stdin;;
let dp = Array.make_matrix (n+1) 3 0;;

for i=1 to n-1 do
    let aux1 = ib(s.[i]<>s.[i-1]) + dp.(i-1).(1)
    and aux2 = ib(not(s.[i]<>s.[i-1])) + dp.(i-1).(0)
    and aux3 = ib(s.[i]<>s.[i-1]) + dp.(i-1).(2)
    and aux4 = ib(not(s.[i]<>s.[i-1])) + dp.(i-1).(1)
    in  let res1 = max aux1 aux2
        and res2 = max aux3 aux4 in
    dp.(i).(0) <- ib(s.[i]<>s.[i-1]) + dp.(i-1).(0);
    dp.(i).(1) <- res1;
    dp.(i).(2) <- res2;
done;;
printf "%d" (1 + max dp.(n-1).(0) (max dp.(n-1).(1) dp.(n-1).(2)));;

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

  let k = (* alternation length *)
    let rec loop prev i ac = if i=n then ac
      else if s.[i] = prev
      then loop prev (i+1) ac
      else loop s.[i] (i+1) (ac+1)
    in
    loop 'x' 0 0
  in

  let answer =
    if n=k then n
    else if n=k+1 then n
    else if n >= k+2 then k+2
    else failwith "impossible"
  in

  printf "%d\n" answer

open Scanf

let read_int () = scanf " %d" (fun x -> x)
let read_string () = scanf " %s" (fun x -> x)
let n = read_int ()
let s = read_string ()

let longest_seq s =
    let last = ref '2'
    and cnt = ref 0 in
    begin
        for i = 0 to n - 1 do
            if s.[i] <> !last then
                begin
                    incr cnt;
                    last := s.[i]
                end
        done;
        !cnt
    end

let res = min n (longest_seq s + 2)
let _ = print_int res

open Printf;;
open Scanf;;

let n = read_int();;
let s = input_line stdin;;
let dp0 = Array.make (n+1) 0;;
let dp1 = Array.make (n+1) 0;;
let dp2 = Array.make (n+1) 0;;
for i=1 to n-1 do
    let aux1 = if s.[i]<>s.[i-1] then 1 + dp1.(i-1) else dp1.(i-1)
    and aux2 = if s.[i]<>s.[i-1] then dp0.(i-1) else 1 + dp0.(i-1)
    and aux3 = if s.[i]<>s.[i-1] then 1 + dp2.(i-1) else dp2.(i-1)
    and aux4 = if s.[i]<>s.[i-1] then dp1.(i-1) else 1 + dp1.(i-1)
    in  let res1 = max aux1 aux2
        and res2 = max aux3 aux4 in
    dp0.(i) <- if s.[i]<>s.[i-1] then 1 + dp0.(i-1) else dp0.(i-1);
    dp1.(i) <- res1;
    dp2.(i) <- res2;
done;;
printf "%d" (1 + max dp0.(n-1) (max dp1.(n-1) dp2.(n-1)));;

let n = read_int()
and s = input_line stdin;;

let rec solve i =
if (i=0) then (0,0,0) else
begin
    let (r0,r1,r2) = solve (i-1)
    and ib b = if(b) then 1 else 0 in
    (ib(s.[i]<>s.[i-1]) + r0,
    max (ib(s.[i]<>s.[i-1]) + r1) (ib(not(s.[i]<>s.[i-1])) + r0),
    max (ib(s.[i]<>s.[i-1]) + r2) (ib(not(s.[i]<>s.[i-1])) + r1))
end;;

let (a,b,c) = solve(n-1) in
Printf.printf "%d" (1 + max a (max b c));;

open Printf;;
open Scanf;;

let n = read_int();;
let s = input_line stdin;;
let dp0 = Array.make (n+1) 0;;
let dp1 = Array.make (n+1) 0;;
let dp2 = Array.make (n+1) 0;;
for i=1 to n-1 do
    let aux1 = if s.[i]<>s.[i-1] then 1 + dp1.(i-1) else dp1.(i-1)
    and aux2 = if s.[i]<>s.[i-1] then dp0.(i-1) else 1 + dp0.(i-1)
    and aux3 = if s.[i]<>s.[i-1] then 1 + dp2.(i-1) else dp2.(i-1)
    and aux4 = if s.[i]<>s.[i-1] then dp1.(i-1) else 1 + dp1.(i-1)
    in  let res1 = max aux1 aux2
        and res2 = max aux3 aux4 in
    dp0.(i) <- if s.[i]<>s.[i-1] then 1 + dp0.(i-1) else dp0.(i-1);
    dp1.(i) <- res1;
    dp2.(i) <- res2;
done;;
printf "%d" (max dp0.(n-1) (max dp1.(n-1) dp2.(n-1)));;


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

let n = read_int()
let a = Array.make n 0

let _ =
  for i=0 to n-1 do
    a.(i) <- read_int() -1
  done

let b = Array.make n 0

let _ =
  for i=0 to n-1 do
    b.(a.(i)) <- i
  done;

  for i=0 to n-1 do
    Printf.printf "%d" (b.(i)+1);
    if i<n-1 then Printf.printf " ";
  done;

  Printf.printf "\n"

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

let input () = Array.init (read ()) (fun n -> read ())

let solve gifter =
    let l = Array.length gifter in
    let giftee = Array.make l 0 in
    let rec loop i =
    if i < l
    then
        (let j = Array.get gifter i in
        Array.set giftee (j - 1) (i + 1);
        loop (i + 1))
    else
        giftee
    in loop 0

let () = Array.iter(Printf.printf "%d ") (solve (input()))

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

let () =
  let n = read_int () in
  let arr = Array.make n 0 in
  for i = 0 to n-1 do
    let e = read_int () in
    (Array.set arr (e-1) i)
  done;
  Array.iter (fun x -> (Printf.printf "%d " (x + 1))) arr

let string_of_charlist cl =
    let rec iter cl str =
        match cl with
            [] -> str
          | r :: rest ->
                let s = Char.escaped r in
                iter rest (str ^ s)
    in iter cl ""

let charlist_of_string str =
    let n = String.length str in
    let rec iter i cl =
        if i < 0 then
            cl
        else
            let sc = str.[i] in
            iter (i-1) (sc :: cl)
    in iter (n-1) []

let split c s =
    let n = String.length s in
    let rec spliti i cl sl =
        if i < 0 then
            if cl = [] then
                sl
            else
                let cs = string_of_charlist cl in
                cs :: sl
        else if s.[i] = c then
            if cl = [] then
                spliti (i-1) cl sl
            else
                let cs = string_of_charlist cl in
                spliti (i-1) [] (cs :: sl)
        else
            spliti (i-1) (s.[i] :: cl) sl
    in spliti (n-1) [] []

let n = read_line ();;

let input = List.map int_of_string (split ' ' (read_line ()));;

let rec f i = function
    [] -> []
  | r :: rest -> (i, r) :: (f (i+1) rest);;

let hoge = f 1 input;;

let output =
    let fn (_, a) (_, b) = a <= b in
    let fm (a, b) = string_of_int a in
    let out = Sort.list fn hoge in
    let ou2 = List.map fm out in
    String.concat " " ou2;;

print_endline output;;

let rec split str sep =
	if str = "" then []
	else try
		let i = String.index str sep in
		let x = String.sub str 0 i in
		let xs = String.sub str (i+1) (String.length str - i - 1) in
		x::(split xs sep)
	with _ -> [str]
;;

let n = read_int () in
let nums = List.map int_of_string (split (read_line()) ' ') in
let p = Array.make n 0 in
let _ = for i = 0 to n-1 do
	Array.set p (List.nth nums i - 1) i
done in
for i = 0 to n-1 do
	print_string (string_of_int (p.(i) + 1) ^ " ")
done;;

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

let iteri f =
  let rec iteri' i = function
      [] -> ()
    | x::xs -> f i x; iteri' (i+1) xs
  in
    iteri' 0
let read_int () = Scanf.scanf "%d%c" (fun x c -> x)
let rec read_ints n =
  if n = 0 then []
  else let tmp = read_int () in tmp :: read_ints (n-1)
let print_array v = Array.iter (Printf.printf "%d ") v
let p x = prerr_int x; prerr_newline ()

;;

let _ =
    let n = read_int ()
    in
    let ans = Array.make n 0
    in
      iteri (fun i x -> ans.(x-1) <- i+1) (read_ints n);
      print_array ans

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

let () =
    let l = read () in
    let gifter = Array.init l (fun x -> read ()) in
    let giftee = Array.make l 0 in
    let rec loop i =
    if i < l
    then
        (let j = (Array.get gifter i) - 1 in
        Array.set giftee j (i + 1);
        loop (i + 1))
    else
        Array.iter(Printf.printf "%d ") giftee
    in loop 0

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

let input () = Array.init (read ()) (fun n -> read ())

let solve gifter =
    let l = Array.length gifter in
    let giftee = Array.make l 0 in
    let rec loop i =
    if i < l
    then
        (let j = (Array.get gifter i) - 1 in
        Array.set giftee j (i + 1);
        loop (i + 1))
    else
        giftee
    in loop 0

let () = Array.iter(Printf.printf "%d ") (solve (input()))

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

let input () = Array.init (read ()) (fun n -> read ())

let solve gifter =
    let l = Array.length gifter in
    let giftee = Array.make l 0 in
    let rec loop i =
    if i < l
    then
        (let j = Array.get gifter i in
        Array.set giftee (j - 1) j;
        loop (i + 1))
    else
        giftee
    in loop 0

let () = Array.iter(Printf.printf "%d ") (solve (input()))

open Str;;

print_string (read_line ());;


let q = int_of_string (input_line stdin);;

let output a b c d =
    let other = if a = c then d else c in
        Printf.printf "%d %d\n" a other ;;

let solve () = Scanf.scanf "%d %d %d %d " output ;;

for i = 1 to q do
    solve ()
done

open Str

let twoPoints (l1:int) (r1:int) (l2:int) (r2:int) : int * int=
  if l1 <> l2 then (l1,l2)
  else if r1 > l1 then (r1, l2)
  else (l1,r2)

let rec repeat (n:int) f =
  if n > 0 then (f () ; repeat (n-1) f)

let oneiter () =
  let inp = read_line() in
  let [l1;r1;l2;r2] =
    List.map int_of_string (Str.split (Str.regexp " +") inp) in
  let (x,y) = twoPoints l1 r1 l2 r2 in
  print_string ((string_of_int x) ^ " " ^ (string_of_int y) ^ "\n")

let main () =
  let q = int_of_string (read_line ()) in
  repeat q oneiter

let () = main()

let q = int_of_string (input_line stdin);;

let output a b c d =
    let min = Pervasives.min a c in
    let max = Pervasives.max b d in
        Printf.printf "%d %d\n" min max ;;

let solve () = Scanf.scanf "%d %d %d %d " output ;;

for i = 1 to q do
    solve ()
done

let q = int_of_string (input_line stdin);;

let output a b c d = Printf.printf "%d %d\n" a d ;;
let solve () = Scanf.scanf "%d %d %d %d " output ;;

for i = 1 to q do
    solve ()
done


(* Codeforces 276C - Devochka max sum *)

open Filename;;

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

let cout = open_out "output.txt";;
let gr () = Scanf.bscanf cin " %d" (fun i -> i);;
let grf () = Scanf.scanf " %f" (fun i -> i);;
let grl () = Scanf.scanf " %Ld" (fun i -> i);;
let rdln() = input_line Pervasives.stdin;;
(* let spli s = Str.split (Str.regexp " ") s;; *)
let printlisti li = List.iter (fun i -> (print_int i; print_string " ")) li;;
let printlisti64 li = List.iter (fun i -> (print_string (Int64.to_string i); print_string " ")) li;;
let printarri ai = Array.iter (fun i -> (print_int i; print_string " ")) ai;;
let debug = false;;

let rec readlist n acc = match n with
	| 0 -> acc
	| _ -> readlist (n -1) (gr() :: acc);;

let rec read_pairs_list n acc = match n with
	| 0 -> List.rev acc
	| _ ->
			let x = gr()-1 in
			let y = gr()-1 in
			read_pairs_list (n -1) ((x, y) :: acc);;

let n = gr();;
let q = gr();;
let a = readlist n [];;
let lr = read_pairs_list q [];;
let ocua = Array.make n 0;;
let ocu1000 = Array.make ((n /1000) +1) 0;;

let rec count_ocu lp = match lp with
	| [] -> ()
	| (l, r) :: tl ->
			begin
				let lt = l /1000 in
				let rt = r /1000 in
				if lt < rt -1 then begin
					for i = lt +1 to rt -1 do
						ocu1000.(i) <- ocu1000.(i) + 1
					done;
					for i = l to (lt +1) *1000 -1 do
						ocua.(i) <- ocua.(i) + 1
					done;
					for i = rt *1000 to r do
						ocua.(i) <- ocua.(i) + 1
					done
				end else
					for i = l to r do
						ocua.(i) <- ocua.(i) + 1
					done;
				count_ocu tl
			end;;

let rec use_thousands n = match n with
	| 0 -> ()
	| _ ->
			let n1 = n -1 in (
				ocua.(n1) <- ocua.(n1) + ocu1000.(n1 /1000);
				use_thousands n1
			);;

let rec cntmul sa sb cnt =
	match sa, sb with
	| [], [] -> cnt
	| ha :: ta, hb :: tb ->
			let ncnt = (Int64.add cnt (Int64.mul (Int64.of_int ha) (Int64.of_int hb))) in
			cntmul ta tb ncnt
	| _ -> raise (Failure "Different lengths sa sb");;

let main() =
	begin
		count_ocu lr;
		use_thousands (n -1);
		let sb = List.sort compare (Array.to_list ocua) in
		let sa = List.sort compare a in
		let sum_mul = cntmul sa sb Int64.zero in
		print_string ((Int64.to_string sum_mul) ^ "\n")
	end;;

main();;

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

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

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

let () =
  let (n,q) = read_pair() in

  let a = Array.init n (fun i -> read_long()) in

  let range = Array.init q (fun _ -> read_pair()) in

  let count = Array.make (n+1) 0L in

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

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

    let () = count.(n) <- Int64.max_int in

    let () = Array.sort compare count in

    let () = Array.sort compare a in

    let answer = sum 0 (n-1) (fun i-> count.(i) ** a.(i)) in
      Printf.printf "%Ld\n" answer


open Printf
open Scanf

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

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

  (* the following 3 arrays represent the input, and are not computed *)
  let lhi = Array.make (n+1) 0 in  (* lhi.(x) is the highest node required to be in the left subtree of x *)
  let rlo = Array.make (n+1) (n+1) in  (* everything in [rlo.(x), rhi.(x)] must be in the right subtree of x*)
  let rhi = Array.make (n+1) 0 in

  (* the following two arrays are computed *)
  let rtc = Array.init (n+1) (fun i -> i) in
  (* rtc.(x) (required to contain)
     We know that the subtree rooted at x must contain x,x+1 ... y
     for some y.  rtc.(x) is the minimum such y required by the
     constraints on the nodes x and greater. *)
  let l =   Array.make (n+1) 0 in  (* size of the left subtree of x *)

  try (
    for i=1 to c do
      let x = read_int() in
      let y = read_int() in
      if y <= x then raise (Failure "1");
      match read_string() with
	| "LEFT" ->
	  lhi.(x) <- max lhi.(x) y;
	| "RIGHT" ->
	  rlo.(x) <- min rlo.(x) y;
	  rhi.(x) <- max rhi.(x) y;
	| _ -> failwith "bad command"
    done;

    for x = n-1 downto 2 do
      rtc.(x) <- max rtc.(x) (max lhi.(x-1) (max lhi.(x) rhi.(x)));
      rtc.(x) <- maxf x rtc.(x) (Array.get rtc);
    done;

    for x = 1 to n-1 do
      l.(x) <- if lhi.(x) = 0 then 0 else rtc.(x+1) - (x+1) + 1;
    done;

    for x = 1 to n-1 do
      if x + l.(x) >= rlo.(x) then raise (Failure "2");
    done;

    let rec inorder x s =
      if l.(x) > 0 then inorder (x+1) l.(x);
      printf "%d " x;
      let rsize = s - l.(x) - 1 in
      if rsize > 0 then inorder (x+l.(x)+1) rsize
    in
    inorder 1 n;
    print_newline();

  ) with Failure s -> printf "IMPOSSIBLE\n";

open Printf
open Scanf

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

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

  let l =   Array.make (n+1) 0 in  (* size of the left subtree of x *)
  let lhi = Array.make (n+1) 0 in  (* lhi.(x) is the highest node required to be in the left subtree of x *)
  let rlo = Array.make (n+1) (n+1) in  (* everything in [rlo.(x), rhi.(x)] must be in the right subtree of x*)
  let rhi = Array.make (n+1) 0 in
  let rtc = Array.init (n+1) (fun i -> i) in
  (* rtc.(x) (required to contain)
     We know that the subtree rooted at x must contain x,x+1 ... y
     for some y.  rc.(x) is the minimum such y required by the
     constraints on the nodes x and greater. *)

  try (
    for i=1 to c do
      let x = read_int() in
      let y = read_int() in
      if y <= x then raise (Failure "1");
      match read_string() with
	| "LEFT" ->
	  lhi.(x) <- max lhi.(x) y;
	| "RIGHT" ->
	  rlo.(x) <- min rlo.(x) y;
	  rhi.(x) <- max rhi.(x) y;
	| _ -> failwith "bad command"
    done;

    for x = n-1 downto 2 do
      rtc.(x) <- max rtc.(x) (max lhi.(x-1) (max lhi.(x) rhi.(x)));
      let rec loop rtcx =
	rtc.(x) <- maxf x rtcx (Array.get rtc);
	if rtc.(x) > rtcx then loop rtc.(x)
      in
      loop rtc.(x);
    done;

    for x = 1 to n-1 do
      l.(x) <- if lhi.(x) = 0 then 0 else rtc.(x+1) - (x+1) + 1;
    done;

    for x = 1 to n-1 do
      if x + l.(x) >= rlo.(x) then raise (Failure "2");
    done;

    let rec inorder x s =
      if l.(x) > 0 then inorder (x+1) l.(x);
      printf "%d " x;
      let rsize = s - l.(x) - 1 in
      if rsize > 0 then inorder (x+l.(x)+1) rsize
    in
    inorder 1 n;
    print_newline();

  ) with Failure s -> printf "IMPOSSIBLE\n";

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

  (* the following 3 arrays represent the input, and are not computed *)
  let lhi = Array.make (n+1) 0 in  (* lhi.(x) is the highest node required to be in the left subtree of x *)
  let rlo = Array.make (n+1) (n+1) in  (* everything in [rlo.(x), rhi.(x)] must be in the right subtree of x*)
  let rhi = Array.make (n+1) 0 in

  (* the following two arrays are computed *)
  let rtc = Array.init (n+1) (fun i -> i) in
  (* rtc.(x) (required to contain)
     We know that the subtree rooted at x must contain x,x+1 ... y
     for some y.  rtc.(x) is the minimum such y required by the
     constraints on the nodes x and greater. *)
  let l =   Array.make (n+1) 0 in  (* size of the left subtree of x *)

  try (
    for i=1 to c do
      let x = read_int() in
      let y = read_int() in
      if y <= x then raise (Failure "1");
      match read_string() with
	| "LEFT" ->
	  lhi.(x) <- max lhi.(x) y;
	| "RIGHT" ->
	  rlo.(x) <- min rlo.(x) y;
	  rhi.(x) <- max rhi.(x) y;
	| _ -> failwith "bad command"
    done;

    let rec set_rtc x elist = if x>1 then (
      rtc.(x) <- max rtc.(x) (max lhi.(x-1) (max lhi.(x) rhi.(x)));
      let rec loop elist =
	match elist with
	  | [] -> [(x,rtc.(x))]
	  | (a,b)::tail ->
	    if rtc.(x) < a then (x,rtc.(x))::elist else (
	      rtc.(x) <- max b rtc.(x);
	      loop tail
	    )
      in
      if rtc.(x) > x then set_rtc (x-1) (loop elist) else set_rtc (x-1) elist
    )
    in

    set_rtc (n-1) [];

    for x = 1 to n-1 do
      l.(x) <- if lhi.(x) = 0 then 0 else rtc.(x+1) - (x+1) + 1;
    done;

    for x = 1 to n-1 do
      if x + l.(x) >= rlo.(x) then raise (Failure "2");
    done;

    let rec inorder x s =
      if l.(x) > 0 then inorder (x+1) l.(x);
      printf "%d " x;
      let rsize = s - l.(x) - 1 in
      if rsize > 0 then inorder (x+l.(x)+1) rsize
    in
    inorder 1 n;
    print_newline();

  ) with Failure s -> printf "IMPOSSIBLE\n";

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

  (* the following 3 arrays represent the input, and are not computed *)
  let lhi = Array.make (n+1) 0 in  (* lhi.(x) is the highest node required to be in the left subtree of x *)
  let rlo = Array.make (n+1) (n+1) in  (* everything in [rlo.(x), rhi.(x)] must be in the right subtree of x*)
  let rhi = Array.make (n+1) 0 in

  (* the following two arrays are computed *)
  let rtc = Array.init (n+1) (fun i -> i) in
  (* rtc.(x) (required to contain)
     We know that the subtree rooted at x must contain x,x+1 ... y
     for some y.  rtc.(x) is the minimum such y required by the
     constraints on the nodes x and greater. *)
  let l =   Array.make (n+1) 0 in  (* size of the left subtree of x *)

  try (
    for i=1 to c do
      let x = read_int() in
      let y = read_int() in
      if y <= x then raise (Failure "1");
      match read_string() with
	| "LEFT" ->
	  lhi.(x) <- max lhi.(x) y;
	| "RIGHT" ->
	  rlo.(x) <- min rlo.(x) y;
	  rhi.(x) <- max rhi.(x) y;
	| _ -> failwith "bad command"
    done;

    let rec set_rtc x elist = if x>2 then (
      rtc.(x) <- max rtc.(x) (max lhi.(x-1) (max lhi.(x) rhi.(x)));
      let rec loop elist =
	match elist with
	  | [] -> [(x,rtc.(x))]
	  | (a,b)::tail ->
	    if rtc.(x) < a then (x,rtc.(x))::elist else (
	      rtc.(x) <- max b rtc.(x);
	      loop tail
	    )
      in
      if rtc.(x) > x then set_rtc (x-1) (loop elist) else set_rtc (x-1) elist
    )
    in

    set_rtc (n-1) [];

    for x = 1 to n-1 do
      l.(x) <- if lhi.(x) = 0 then 0 else rtc.(x+1) - (x+1) + 1;
    done;

    for x = 1 to n-1 do
      if x + l.(x) >= rlo.(x) then raise (Failure "2");
    done;

    let rec inorder x s =
      if l.(x) > 0 then inorder (x+1) l.(x);
      printf "%d " x;
      let rsize = s - l.(x) - 1 in
      if rsize > 0 then inorder (x+l.(x)+1) rsize
    in
    inorder 1 n;
    print_newline();

  ) with Failure s -> printf "IMPOSSIBLE\n";


open Printf
open Scanf

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

let pi = 2.0 *. (atan2 1.0 0.0)

let () =
  let n = read_int() in
  let r0 = read_float() in

  let alpha = pi /. (float n) in
  let salpha = sin alpha in

  let r = salpha /. (1.0 -. salpha) in

  printf "%.8f\n" (r0 *. r)

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

let () =
    let n = read_float () and r = read_float () in
    let pi = acos (-1.) in
    let x = sin (pi /. n) in
    let ans = r *. x /. (1. -. x) in
    printf "%f\n" ans

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

let pi = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328
let () =
  let n,r = g2 0 in
  let u = (pi /. (2.*.(Int64.to_float n))) in
  let q = (sin u -. cos u) ** 2. in
  let p = (Int64.to_float r) *. sin (pi /. (Int64.to_float n))
  in
  printf "%.18f" @@ p /. q


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 ( -- ) (x1,y1) (x2,y2) = (x1-.x2, y1-.y2)
let ( ++ ) (x1,y1) (x2,y2) = (x1+.x2, y1+.y2)
let len (x,y) = sqrt(x*.x +. y*.y)

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let () =
  let a = float (read_int()) in
  let b = float (read_int()) in
  let n = read_int () in
  let data = Array.init n (fun _ ->
    let x = float (read_int()) in
    let y = float (read_int()) in
    let v = float (read_int()) in
    ((x,y),v)
  ) in

  let answer = minf 0 (n-1) (fun i ->
    let (p,v) = data.(i) in
    let d = len (p -- (a,b)) in
    d /. v
  ) in

  printf "%.20f\n" answer

let expon
= fun x1 x2 ->
  let foi = float_of_int in
  (foi (x1 - x2)) ** 2.0

let distance
= fun (x1, y1) (x2, y2) ->
  sqrt ((expon x1 x2) +. (expon y1 y2))

let _
= let a, b = Scanf.scanf "%d %d\n" (fun x y -> (x, y)) in
  let n = Scanf.scanf "%d\n" (fun x -> x) in
  let oneCase : unit -> float
  = fun () ->
    let x, y, v = Scanf.scanf "%d %d %d\n" (fun x y z -> (x, y, z)) in
    (distance (a, b) (x, y)) /. (float_of_int v)
  in
  let firstCase = oneCase () in
  let rec minmin : int -> float -> float
  = fun i m ->
    if i <= 0 then m
    else(
      let mm = min m (oneCase ()) in
      minmin (i - 1) mm
    )
  in
  let mmmm = (minmin (n - 1) firstCase) in
  Printf.printf "%.9f" mmmm

type point = (int * int)

let taxi_distance : point -> point -> int
= fun (x1, y1) (x2, y2) -> (abs (x1 - x2)) + (abs (y1 - y2))

let _
= let a, b = Scanf.scanf "%d %d\n" (fun x y -> (x, y)) in
  let n = Scanf.scanf "%d\n" (fun x -> x) in
  let oneCase : unit -> float
  = fun () ->
    let x, y, v = Scanf.scanf "%d %d %d\n" (fun x y z -> (x, y, z)) in
    (taxi_distance (a, b) (x, y) |> float_of_int) /. (float_of_int v)
  in
  let firstCase = oneCase () in
  let rec minmin : int -> float -> float
  = fun i m ->
    if i <= 0 then m
    else(
      let mm = min m (oneCase ()) in
      minmin (i - 1) mm
    )
  in
  let mmmm = (minmin (n - 1) firstCase) in
  Printf.printf "%.9f" mmmm

type point = (int * int)

let taxi_distance : point -> point -> int
= fun (x1, y1) (x2, y2) -> (abs (x1 - x2)) + (abs (y1 - y2))

let _
= let a, b = Scanf.scanf "%d %d\n" (fun x y -> (x, y)) in
  let n = Scanf.scanf "%d\n" (fun x -> x) in
  let oneCase : unit -> float
  = fun () ->
    let x, y, v = Scanf.scanf "%d %d %d\n" (fun x y z -> (x, y, z)) in
    (taxi_distance (a, b) (x, y) |> float_of_int) /. (float_of_int v)
  in
  let firstCase = oneCase () in
  let rec minmin : int -> float -> float
  = fun i m ->
    if i <= 0 then m
    else(
      let mm = min m (oneCase ()) in
      minmin (i - 1) mm
    )
  in
  let mmmm = (minmin (n - 1) firstCase) in
  Printf.printf "%.12f" mmmm


let scan_int () = Scanf.scanf " %d" (fun x -> x);;
type 'a pri_queue =
Node of 'a * 'a pri_queue * 'a pri_queue * int |
Null
exception Empty_Queue
let empty_queue = Null
let is_empty q = q = Null
let size q =
match q with
Null ->0 |
Node (_,_,_,n) ->n
let getmax h =
match h with
Null ->raise Empty_Queue |
Node (r,_,_,_) -> r
let set_root h r =
match h with

Null ->Node (r,Null,Null,1) |
Node (_,l,p,n) ->Node (r,l,p,n)
let rec put h x =
match h with
Null ->Node (x,Null,Null,1) |
Node (r,l,p,n) ->
if size l <= size p then
Node((max x r),(put l (min x r)),p,(n+1))
else
Node((max x r),l,(put p (min x r)),(n+1))
let rec removemax h =
match h with
Null ->raise Empty_Queue |
Node (_,Null,Null,_) ->Null |
Node (_,l,Null,_) ->l |
Node (_,Null,p,_) ->p |
Node (_,(Node (rl,_,_,_) as l),
(Node (rp,_,_,_) as p),n) ->
if rl >= rp then
Node (rl,removemax l,p,n - 1)
else
Node (rp,l,removemax p,n - 1);;
let n = scan_int() and d= scan_int()  and czy = ref 1 and dodaj = ref 0 and wyn = ref 0 and sum = ref 0 and q = ref empty_queue in
let tb = Array.make (n+1) 0 in
begin
  for i=1 to n do
    tb.(i) <- scan_int();
    sum := !sum + tb.(i);
    q := put !q (!sum, i)
  done;
  sum := 0;
  for i=1 to n do
    if tb.(i) <> 0 then sum := !sum + tb.(i)
    else if !sum < 0 then begin
      let a = ref (getmax !q) in
        while (snd(!a)) < i do q := removemax !q; a := getmax !q done;
      wyn := !wyn +1;
      sum := !sum + (d - (fst(!a)) - !dodaj);
      dodaj := d - fst(!a);
      if !sum < 0 then czy := 0 end;
    if !sum > d then czy := 0
  done;
  if !czy = 0 then print_int (-1)
  else print_int (!wyn)
end

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

let n = scan_int () and d = scan_int();;

let x = Array.make n 0 and lim = Array.make n Int32.zero;;
let sumek = ref Int32.zero;;

for i = 0 to n - 1 do
  x.(i) <- scan_int();
  sumek := Int32.add !sumek (Int32.of_int x.(i));
  lim.(i) <- Int32.sub (Int32.of_int d) (!sumek);
done;;

for i = n - 2 downto 0 do
  lim.(i) <- min lim.(i) lim.(i + 1);
done;;

let koncz () =
  print_string("-1");
  exit 0;;

if lim.(0) < Int32.zero then koncz();;

let wyn = ref 0 and dod = ref Int32.zero;;
sumek := Int32.zero;
for i = 0 to n - 1 do
  if x.(i) == 0 && Int32.add !sumek !dod < Int32.zero then begin
    dod := lim.(i);
    incr wyn;
    if Int32.add !sumek !dod < Int32.zero then koncz();
  end;
  sumek := Int32.add !sumek (Int32.of_int x.(i));
done;;

print_int !wyn;;

(*
4 1000000000
-100 0 -250000000 0
*)

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

let n = scan_int () and d = scan_int();;

let x = Array.make n 0 and lim = Array.make n 0;;
let sumek = ref 0;;

for i = 0 to n - 1 do
  x.(i) <- scan_int();
  sumek := !sumek + x.(i);
  lim.(i) <- d - !sumek;
done;;

for i = n - 2 downto 0 do
  lim.(i) <- min lim.(i) lim.(i + 1);
done;;

let koncz () =
  print_string("-1");
  exit 0;;

if lim.(0) < 0 then koncz();;

let wyn = ref 0 and dod = ref 0;;
sumek := 0;
for i = 0 to n - 1 do
  if x.(i) == 0 && !sumek + !dod < 0 then begin
    dod := lim.(i);
    incr wyn;
    if !sumek + !dod < 0 then koncz();
  end;
  sumek := !sumek + x.(i);
done;;

print_int !wyn;;

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

let n = scan_int () and d = scan_int();;

let x = Array.make n 0 and lim = Array.make n 0;;
let sumek = ref 0;;

for i = 0 to n - 1 do
  x.(i) <- scan_int();
  sumek := !sumek + x.(i);
  lim.(i) <- max d (d - !sumek);
done;;

for i = n - 2 downto 0 do
  lim.(i) <- min lim.(i) lim.(i + 1);
done;;

let koncz () =
  print_string("-1");
  exit 0;;

if lim.(0) < 0 then koncz();;

let wyn = ref 0 and dod = ref 0;;
sumek := 0;
for i = 0 to n - 1 do
  if x.(i) == 0 && !sumek + !dod < 0 then begin
    dod := lim.(i);
    incr wyn;
    if !sumek + !dod < 0 then koncz();
  end;
  sumek := !sumek + x.(i);
done;;

print_int !wyn;;

(*
4 1000000000
-100 0 -250000000 0
*)

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

let n = scan_int () and d = scan_int();;

let x = Array.make n 0 and lim = Array.make n 0;;
let sumek = ref 0;;

for i = 0 to n - 1 do
  x.(i) <- scan_int();
  sumek := !sumek + x.(i);
  lim.(i) <- d - !sumek;
done;;

for i = n - 2 downto 0 do
  lim.(i) <- min lim.(i) lim.(i + 1);
done;;

let koncz =
  print_string("-1");
  exit 0;;

if lim.(0) < 0 then koncz

let wyn = ref 0 and dod = ref 0;;
sumek := 0;
for i = 0 to n - 1 do
  if x.(i) == 0 && !sumek + !dod < 0 then begin
    dod := lim.(i);
    incr wyn;
    if !sumek + !dod < 0 then koncz
  end;
  sumek := !sumek + x.(i);
done;;

print_int !wyn;;

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

let n = scan_int () and d = scan_int();;

let x = Array.make n 0 and lim = Array.make n 0;;
let sumek = ref 0;;

for i = 0 to n - 1 do
  x.(i) <- scan_int();
  sumek := !sumek + x.(i);
  lim.(i) <- min d (d - !sumek);
done;;

for i = n - 2 downto 0 do
  lim.(i) <- min lim.(i) lim.(i + 1);
done;;

let koncz () =
  print_string("-1");
  exit 0;;

if lim.(0) < 0 then koncz();;

let wyn = ref 0 and dod = ref 0;;
sumek := 0;
for i = 0 to n - 1 do
  if x.(i) == 0 && !sumek + !dod < 0 then begin
    dod := lim.(i);
    incr wyn;
    if !sumek + !dod < 0 then koncz();
  end;
  sumek := !sumek + x.(i);
done;;

print_int !wyn;;

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

let n = scan_int () and d = scan_int();;

let x = Array.make n 0 and lim = Array.make n 0;;
let sumek = ref 0;;

for i = 0 to n - 1 do
  x.(i) <- scan_int();
  sumek := !sumek + x.(i);
  lim.(i) <- d - !sumek;
done;;

for i = n - 2 downto 0 do
  lim.(i) <- min lim.(i) lim.(i + 1);
done;;

let wyn = ref 0 and dod = ref 0;;
sumek := 0;
for i = 0 to n - 1 do
  if x.(i) == 0 && !sumek + !dod < 0 then begin
    dod := lim.(i);
    incr wyn;
    if !sumek + !dod < 0 then begin
      print_string("-1");
      exit 0;
    end;
  end;
  sumek := !sumek + x.(i);
done;;

print_int !wyn;;


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

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

let () =
  let n = read_int() in
  let input = Array.init n
    (fun _ -> let l = read_int() in let r = read_int() in (l,r)) in
  let minleft = minf 0 (n-1) (fun i -> let (l,r)=input.(i) in l) in
  let maxright = maxf 0 (n-1) (fun i -> let (l,r)=input.(i) in r) in

  let rec findit i = if i=n then None else
    let (l,r) = input.(i) in
      if l<=minleft && r>=maxright then Some i else findit (i+1)
  in

  let ans =
    match findit 0 with
      |	None -> -1
      | Some i -> i+1
  in
    Printf.printf "%d\n" ans

(* Codeforces 242B BigOtres done *)

open String;;
open Int64;;

(* 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_string " "; print_int i)) li;;
let printarri ai = Array.iter (fun i -> (print_string " "; print_int i)) ai;;
let debug = false;;

let rec checkOtr i n mna mxb idx =
	if i > n then idx
	else
		let a = grl() in
		let b = grl() in
		if a <= mna && mxb <= b then checkOtr (i+1) n a b i
		else
			let idx = if mna <= a && b <= mxb then idx else -1 in
			let mna = min a mna in
			let mxb = max b mxb in
			checkOtr (i+1) n mna mxb idx;;

let main () =
	let n = gr() in
	let a = grl() in
	let b = grl() in
	let idx = checkOtr 2 n a b 1 in
	print_int idx;;

main();;


let () =
    Scanf.scanf "%d %d\n" (fun n m ->
        let line = String.make m '#' in
        let empty = String.make (m - 1) '.' in
        for i = 1 to n do
            if i mod 2 = 1 then
                print_endline line
            else
                if i mod 4 = 0 then begin
                    print_char '#';
                    print_endline empty
                end else begin
                    print_string empty;
                    print_endline "#"
                end
        done
    )

let id x = x in

let rec range lo hi =
                if lo >= hi then []
                else lo :: range (lo + 1) hi in

let n = Scanf.scanf " %d" id in
let m = Scanf.scanf " %d" id in

let rec stringn s n = match n with
1 -> s
| _ -> s ^ stringn s (n - 1) in

let get i = if i mod 2 = 0 then stringn "#" m
            else if i mod 4 = 1 then stringn "." (m - 1) ^ "#"
            else "#" ^ stringn "." (m - 1) in

n
|> range 0
|> List.map get
|> List.iter print_endline ;;
(*
Printf.printf " %d %d" n m ;; *)

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

let get_parity row =
    if row mod 2 = 0
        then
            if row / 2 mod 2 = 0
            then
                `even_left
            else
                `even_right
        else
            `odd

let make_line parity m =
    match parity with
    |`even_left -> "#" ^ (String.make (m - 1) '.')
    |`even_right -> (String.make (m - 1) '.') ^ "#"
    |`odd -> String.make m '#'

let solve (n, m) =
    let rec loop i =
        if i <= n
        then
            let line = make_line (get_parity i) m in
            Printf.printf "%s\n" line;
            loop (i + 1)
        else
            ()
    in loop 1

let () = solve (input ())

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

let () =
  let n = read_int () in
  let m = read_int () in
  let rec solve pos str m = function
    | 0 -> str
    | n ->
        if (n mod 2) = 1 then
          solve pos (str ^ (String.make m '#') ^ "\n") m (n - 1)
        else
          if pos then
            solve false (str ^ (String.make (m - 1) '.') ^ "#\n") m (n - 1)
          else
            solve true (str ^ "#" ^ (String.make (m - 1) '.') ^ "\n") m (n - 1)
  in Printf.printf "%s" (solve true "" m n)


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

(*
let ( *| ) = Int64.mul
let ( +| ) = Int64.add
let ( -| ) = Int64.sub
let ( /| ) = Int64.div
let ( %| ) = Int64.rem
*)
let long x = Int64.of_int x
let short x = Int64.to_int x

open Printf
open Scanf

let get h k =
    try Hashtbl.find h k with
    | Not_found -> 0

let set h k v = Hashtbl.replace h k v

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

let () =
    let n = read_int() in
    let a = Array.make n 0 in
    for i = 0 to n - 1 do
        a.(i) <- read_int()
    done;
    let h = Hashtbl.create n in
    for i = 0 to n - 1 do
        set h a.(i) (get h a.(i) + 1)
    done;
    let res = ref 0L in
    Hashtbl.iter (fun k v ->
		let pw2 = ref 1 in
		for _ = 1 to 30 do
			pw2 := !pw2 * 2;
			let k2 = !pw2 - k in
			if k < k2 then
				res := (long v)  ** (long (get h (!pw2 - k))) ++ !res
			else if k = k2 then
				res := (long v)  ** (long (v - 1)) // 2L ++ !res
		done
	) h;
    printf "%Ld\n" !res

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

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

open Printf
open Scanf

let get h k =
    try Hashtbl.find h k with
    | Not_found -> 0

let set h k v = Hashtbl.replace h k v

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

let () =
    let n = read_int() in
    let a = Array.make n 0 in
    for i = 0 to n - 1 do
        a.(i) <- read_int()
    done;
    let h = Hashtbl.create n in
    for i = 0 to n - 1 do
        set h a.(i) (get h a.(i) + 1)
    done;
    let res = ref 0L in
    Hashtbl.iter (fun k v ->
		let pw2 = ref 1 in
		for _ = 1 to 30 do
			pw2 := !pw2 * 2;
			let k2 = !pw2 - k in
			if k < k2 then
				res := (long v)  *| (long (get h (!pw2 - k))) +| !res
			else if k = k2 then
				res := (long v)  *| (long (v - 1)) /| 2L +| !res
		done
	) h;
    printf "%Ld\n" !res

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

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

open Printf
open Scanf

let get h k =
    try Hashtbl.find h k with
    | Not_found -> 0

let set h k v = Hashtbl.replace h k v

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

let () =
    let n = read_int() in
    let a = Array.make n 0 in
    for i = 0 to n - 1 do
        a.(i) <- read_int()
    done;
    let h = Hashtbl.create n in
    for i = 0 to n - 1 do
        set h a.(i) (get h a.(i) + 1)
    done;
    let res = ref 0L in
    Hashtbl.iter (fun k v ->
		let pw2 = ref 1 in
		for _ = 1 to 30 do
			pw2 := !pw2 * 2;
			let k2 = !pw2 - k in
			if k < k2 then
				res := (long v)  ** (long (get h (!pw2 - k))) ++ !res
			else if k = k2 then
				res := (long v)  ** (long (v - 1)) // 2L ++ !res
		done
	) h;
    printf "%Ld\n" !res

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

(*
let ( *| ) a b = Int64.mul
let ( +| ) a b = Int64.add
let ( -| ) a b = Int64.sub
let ( /| ) a b = Int64.div
let ( %| ) a b = Int64.rem
*)
let long x = Int64.of_int x
let short x = Int64.to_int x

open Printf
open Scanf

let get h k =
    try Hashtbl.find h k with
    | Not_found -> 0

let set h k v = Hashtbl.replace h k v

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

let () =
    let n = read_int() in
    let a = Array.make n 0 in
    for i = 0 to n - 1 do
        a.(i) <- read_int()
    done;
    let h = Hashtbl.create n in
    for i = 0 to n - 1 do
        set h a.(i) (get h a.(i) + 1)
    done;
    let res = ref 0L in
    Hashtbl.iter (fun k v ->
		let pw2 = ref 1 in
		for _ = 1 to 30 do
			pw2 := !pw2 * 2;
			let k2 = !pw2 - k in
			if k < k2 then
				res := (long v)  ** (long (get h (!pw2 - k))) ++ !res
			else if k = k2 then
				res := (long v)  ** (long (v - 1)) // 2L ++ !res
		done
	) h;
    printf "%Ld\n" !res

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

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n 0 in
  let _ =
    for i = 0 to n - 1 do
      a.(i) <- Scanf.bscanf sb "%d " (fun s -> s)
    done;
  in
  let ht = Hashtbl.create n in
  let _ =
    for i = 0 to n - 1 do
      let c =
	try
	  Hashtbl.find ht a.(i)
	with
	  | Not_found -> 0
      in
	Hashtbl.replace ht a.(i) (c + 1)
    done
  in
  let res = ref 0L in
    Hashtbl.iter
      (fun x c ->
	 for i = 1 to 30 do
	   try
	     if 1 lsl i - x = x
	     then res := !res +| Int64.of_int c *| Int64.of_int (c - 1)
	     else
	       let c2 = Hashtbl.find ht (1 lsl i - x) in
		 res := !res +| Int64.of_int c *| Int64.of_int c2;
	   with
	     | Not_found -> ()
	 done;
      ) ht;
    res := !res /| 2L;
    Printf.printf "%Ld\n" !res


let () =
	let module Ident = struct
		external id : 'a -> 'a = "%identity" (* test *)
	end in
	let open Printf in
	Scanf.scanf " %d" @@ fun n ->
		if (n-2) mod 3 = 0 then printf "1 2 %d\n" @@ Ident.id (n-3)
		else printf "1 1 %d\n" @@ Ident.id (n-2)

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 () =
	let n = get_i64 0 in
	(
		if (n - 2L) mod 3L = 0L then (
			printf "1 2 %Ld" (n-3L)
		) else (
			printf "1 1 %Ld" (n-2L)
		)
	)

let n = int_of_string (input_line stdin) ;;

if (n - 2) mod 3 != 0 then
    Printf.printf "%d %d %d\n" 1 1 (n - 2)
else
    Printf.printf "%d %d %d\n" 2 2 (n - 4) ;;

let () =
	let module Ident = struct
		external id : 'a -> 'a = "%identity" (* test *)
	end in
	let open Printf in
	Scanf.scanf " %d" @@ fun n ->
		if n-2 mod 3 = 0 then printf "1 2 %d\n" @@ Ident.id (n-3)
		else printf "1 1 %d\n" @@ Ident.id (n-2)


let print_array a =
	let n = Array.length a in
	for k = 0 to n - 1 do
		print_endline a.(k);
	done;
;;

let read_array n =
	let arr = Array.make n "" in
	begin
		for k=0 to (n - 1) do
			Scanf.bscanf Scanf.Scanning.stdin " %s" (fun v -> arr.(k) <- v;);
		done;
		arr
	end
;;

let longest_common_prefix_length l r =
	let n = min (String.length l) (String.length r) in
	let k = ref 1 in (* little optimization - we know that under index 0 we always have a hash-sign '#' *)
	begin
		while (!k < n) && ((String.get l !k) = (String.get r !k) ) do
			k := !k + 1;
		done;
		!k
	end
;;

let max_lex_predecessor l r =
	let nl = String.length l in
	let nr = String.length r in
	let m = longest_common_prefix_length l r in
	if (m = nl) || ((m < nr) && (String.get l m) < (String.get r m)) then l
	else (String.sub l 0 m)
;;

let truncate_hashtags a =
	let n = Array.length a in
	let b = Array.make n "" in
	begin
		b.(n-1) <- a.(n-1);
		for k=(n-2) downto 0 do
			b.(k) <- (max_lex_predecessor a.(k) b.(k+1));
		done;
		b
	end
;;

let load_and_process_hashtags n =
	let a = read_array n in
	let b = truncate_hashtags a in
	print_array b
;;

let read_input () =
	Scanf.bscanf Scanf.Scanning.stdin " %i " load_and_process_hashtags
;;

read_input ()

let print_array a =
	let n = Array.length a in
	for k = 0 to n - 1 do
		print_endline a.(k);
	done;
;;

let read_array n =
	let arr = Array.make n "" in
	begin
		for k=0 to (n - 1) do
			Scanf.bscanf Scanf.Scanning.stdin " %s" (fun v -> arr.(k) <- v;);
		done;
		arr
	end
;;

let longest_common_prefix_length l r =
	let n = min (String.length l) (String.length r) in
	let k = ref 1 in (* little optimization - we know that under index 0 we always have a hash-sign '#' *)
	begin
		while (!k < n) && ((String.get l !k) = (String.get r !k) ) do
			k := !k + 1;
		done;
		!k
	end
;;

let max_lex_predecessor_length l r =
	let nl = String.length l in
	let nr = String.length r in
	let m = longest_common_prefix_length l r in
	if (m = nl) || (m = nr) then m
	else if (String.get l m) < (String.get r m) then nl
	else m
;;

let max_lex_predecessor l r =
	let nl = String.length l in
	let m = max_lex_predecessor_length l r in
	if m = nl then l else (String.sub l 0 m)
;;

let truncate_hashtags a =
	let n = Array.length a in
	let b = Array.make n "" in
	begin
		b.(n-1) <- a.(n-1);
		for k=(n-2) downto 0 do
			b.(k) <- (max_lex_predecessor a.(k) b.(k+1));
		done;
		b
	end
;;

let load_and_process_hashtags n =
	let a = read_array n in
	let b = truncate_hashtags a in
	print_array b
;;

let read_input () =
	Scanf.bscanf Scanf.Scanning.stdin " %i " load_and_process_hashtags
;;

read_input ()

let print_array a =
	let n = Array.length a in
	for k = 0 to n - 1 do
		print_endline a.(k);
	done;
;;

let read_array n =
	let arr = Array.make n "" in
	let last_v = ref "" in
	begin
		for k = 0 to (n - 1) do
			Scanf.bscanf Scanf.Scanning.stdin " %s" (fun v ->
				begin
					arr.(k) <- if v = !last_v then !last_v else v;
					last_v := v;
				end
			);
		done;
		arr
	end
;;

let longest_common_prefix_length l r =
	let n = min (String.length l) (String.length r) in
	let k = ref 1 in (* little optimization - we know that under index 0 we always have a hash-sign '#' *)
	begin
		while (!k < n) && ((String.get l !k) = (String.get r !k) ) do
			k := !k + 1;
		done;
		!k
	end
;;

let truncate_hashtags a =
	let n = Array.length a in
	begin
		for k=(n-2) downto 0 do
			let l = a.(k) in
			let r = a.(k+1) in
			if (l != r) && (String.compare l r) >= 1 then
			begin
				a.(k) <- (String.sub l 0 (longest_common_prefix_length l r));
			end;
		done;
		a
	end
;;

let load_and_process_hashtags n =
	let a = read_array n in
	let b = truncate_hashtags a in
	print_array b
;;

let read_input () =
	Scanf.bscanf Scanf.Scanning.stdin " %i " load_and_process_hashtags
;;

read_input ()

let print_array a =
	let n = Array.length a in
	for k = 0 to n - 1 do
		print_endline a.(k);
	done;
;;

let read_array n =
	let arr = Array.make n "" in
	begin
		for k=0 to (n - 1) do
			Scanf.bscanf Scanf.Scanning.stdin " %s" (fun v -> arr.(k) <- v;);
		done;
		arr
	end
;;

let longest_common_prefix_length l r =
	let n = min (String.length l) (String.length r) in
	let k = ref 1 in (* little optimization - we know that under index 0 we always have a hash-sign '#' *)
	begin
		while (!k < n) && ((String.get l !k) = (String.get r !k) ) do
			k := !k + 1;
		done;
		!k
	end
;;

let max_lex_predecessor_length l r =
	let nl = String.length l in
	let nr = String.length r in
	let m = longest_common_prefix_length l r in
	if (m = nl) || (m = nr) then m
	else if (String.get l m) < (String.get r m) then nl
	else m
;;

let max_lex_predecessor l r =
	let m = max_lex_predecessor_length l r in
	String.sub l 0 m
;;

let truncate_hashtags a =
	let n = Array.length a in
	let b = Array.make n "" in
	begin
		b.(n-1) <- a.(n-1);
		for k=(n-2) downto 0 do
			b.(k) <- (max_lex_predecessor a.(k) b.(k+1));
		done;
		b
	end
;;

let load_and_process_hashtags n =
	let a = read_array n in
	let b = truncate_hashtags a in
	print_array b
;;

let read_input () =
	Scanf.bscanf Scanf.Scanning.stdin " %i " load_and_process_hashtags
;;

read_input ()

let print_array a =
	let n = Array.length a in
	for k = 0 to n - 1 do
		print_endline a.(k);
	done;
;;

let read_array n =
	let arr = Array.make n "" in
	begin
		for k = 0 to (n - 1) do
			Scanf.bscanf Scanf.Scanning.stdin " %s" (fun v -> arr.(k) <- v;);
		done;
		arr
	end
;;

let longest_common_prefix_length l r =
	let n = min (String.length l) (String.length r) in
	let k = ref 1 in (* little optimization - we know that under index 0 we always have a hash-sign '#' *)
	begin
		while (!k < n) && ((String.get l !k) = (String.get r !k) ) do
			k := !k + 1;
		done;
		!k
	end
;;

let max_lex_predecessor l r =
	let nl = String.length l in
	let nr = String.length r in
	let m = longest_common_prefix_length l r in
	if (m = nl) || ((m < nr) && (String.get l m) < (String.get r m)) then l
	else (String.sub l 0 m)
;;

let truncate_hashtags a =
	let n = Array.length a in
	begin
		for k=(n-2) downto 0 do
			a.(k) <- (max_lex_predecessor a.(k) a.(k+1));
		done;
		a
	end
;;

let load_and_process_hashtags n =
	let a = read_array n in
	let b = truncate_hashtags a in
	print_array b
;;

let read_input () =
	Scanf.bscanf Scanf.Scanning.stdin " %i " load_and_process_hashtags
;;

read_input ()

let print_array a =
	let n = Array.length a in
	for k = 0 to n - 1 do
		print_endline a.(k);
	done;
;;

let read_array n =
	let arr = Array.make n "" in
	begin
		for k = 0 to (n - 1) do
			Scanf.bscanf Scanf.Scanning.stdin " %s" (fun v -> arr.(k) <- v;);
		done;
		arr
	end
;;

let longest_common_prefix_length l r =
	let n = min (String.length l) (String.length r) in
	let k = ref 1 in (* little optimization - we know that under index 0 we always have a hash-sign '#' *)
	begin
		while (!k < n) && ((String.get l !k) = (String.get r !k) ) do
			k := !k + 1;
		done;
		!k
	end
;;

let truncate_hashtags a =
	let n = Array.length a in
	begin
		for k=(n-2) downto 0 do
			let l = a.(k) in
			let r = a.(k+1) in
			if (String.compare l r) >= 1 then
			begin
				a.(k) <- (String.sub l 0 (longest_common_prefix_length l r));
			end
		done;
		a
	end
;;

let load_and_process_hashtags n =
	let a = read_array n in
	let b = truncate_hashtags a in
	print_array b
;;

let read_input () =
	Scanf.bscanf Scanf.Scanning.stdin " %i " load_and_process_hashtags
;;

read_input ()


let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x);;
let print_int x = Printf.printf "%d " x;;
let print_int64 x = Printf.printf "%Ld " x;;
let print s = Printf.printf "%s" s;;

module type S = sig
  val set : int -> int -> unit
  val get : int -> int
  val inc : int -> int -> unit
  val max_range : int -> int -> int
  val max_range_value : int -> int -> int
end

module MakeSegTree (M: (sig val size : int end)) : S = struct
  let n = M.size
  let nn = 3 * n

  let arr = Array.make n 0
  let delta = Array.make nn 0

  let set i v = arr.(i) <- v
  let get i = arr.(i)

  let rec update c b e i =
    let upd c =
      if arr.(delta.(2*c)) < arr.(delta.(2*c+1)) then
        delta.(c) <- delta.(2*c+1)
      else
        delta.(c) <- delta.(2*c) in
    if (b > e || i > e || i < b) then ()
    else if ((i = b) && (i = e)) then delta.(c) <- i
    else if i <= (b+e)/2 then begin
      update (2*c) b ((b+e)/2) i; upd c
    end else begin
      update (2*c+1) (((b+e)/2)+1) e i; upd c
    end

  let inc i v =
    arr.(i) <- v;
    update 1 0 (n-1) i

  let rec query c b e i j =
    if (b > e || i > j || b > j || i > e) then -1 else
    if i <= b && e <= j then delta.(c)
    else
      let first = query (2*c) b ((b+e)/2) i j in
      let second = query (2*c+1) ((b+e)/2+1) e i j in
      match first,second with
      | -1,_ -> second
      | _,-1 -> first
      | _,_ ->
        if arr.(first) < arr.(second) then second
        else first

  let max_range i j =
    query 1 0 (n-1) i j

  let max_range_value i j =
    let ind = max_range i j in
    arr.(ind)
end

let () =
  let n = read_int () in
  let module Size = struct
    let size = n
  end in
  let module Segtree = MakeSegTree(Size) in
  let dummy = Array.make (n-1) () in
  let arr = Array.map (fun _ -> read_int() -1) dummy in
  let add_to_seg i e =
    Segtree.inc i e in
  Array.iteri add_to_seg arr;
  Segtree.inc (n-1) 0;
  let dp = Array.make n Int64.zero in
  for i = n-2 downto 0 do
    let ma = Segtree.max_range (i+1) arr.(i) in
    dp.(i) <- Int64.add dp.(ma) (Int64.of_int (- (arr.(i) - ma) + (n - i - 1)));
    (*print_int i;
    print_int ma;
    print_int64 dp.(i);
    print_int64 (Int64.of_int ((arr.(i) - ma) + (n - i - 1)));
    print "\n"; *)
  done;
  print_int64 (Array.fold_left Int64.add Int64.zero dp);;

open Printf
open Scanf

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

let parent v = v/2
let lc v = 2*v
let rc v = 2*v+1

let superceil n =
  let rec loop i ac = if i=0 then ac else loop (i lsr 1) (ac lor i) in
  1 + (loop (n-1) 0)

let max2 (h1,p1) (h2,p2) = let (x,y) = max (h1,-p1) (h2,-p2) in (x,-y)

let rec sum i j f ac = if i>j then ac else sum (i+1) j f (ac ++ (f i))

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

  let nn = superceil n in

  let t = Array.make (2*nn) (0,0) in

  let rec insert ar (x,i) =
    let rec loop j = if j>0 then (
      ar.(j) <- max2 ar.(j) (x,i);
      loop (parent j)
    ) in
    loop (i+nn)
  in

  let max_in_range i j =
    let rec f v l r ql qr =
      let m = (l+r)/2 in
      if l=ql && r=qr then t.(v) else
	let t1 = if ql<=m then f (lc v) l m ql (min m qr) else (0,0) in
	let t2 = if qr>m then f (rc v) (m+1) r (max ql (m+1)) qr else (0,0) in
	max2 t1 t2
    in
    f 1 0 (nn-1) i j
  in

  for i=0 to n-1 do
    insert t (a.(i),i)
  done;

  let dist = Array.make n 0L in
  (* dist.(i) is the sum over all j>i of the number of hops from i to j *)

  for i=n-2 downto 0 do
    let k = a.(i) in
    let (_,j) = max_in_range (i+1) k in
    (* dist.(i) <-  (k-i) + dist.(j) - (k-j) + (n-1-k) *)
    dist.(i) <- (long (j+n-k-i-1)) ++ dist.(j)
  done;

  let answer = sum 0 (n-2) (fun i -> dist.(i)) 0L in
  printf "%Ld\n" answer

let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x);;
let print_int x = Printf.printf "%d " x;;
let print_int64 x = Printf.printf "%Ld " x;;
let print s = Printf.printf "%s" s;;

module type S = sig
  val set : int -> int -> unit
  val get : int -> int
  val inc : int -> int -> unit
  val max_range : int -> int -> int
  val max_range_value : int -> int -> int
end

module MakeSegTree (M: (sig val size : int end)) : S = struct
  let n = M.size
  let nn = 3 * n

  let arr = Array.make n 0
  let delta = Array.make nn 0

  let set i v = arr.(i) <- v
  let get i = arr.(i)

  let rec update c b e i =
    let upd c =
      if arr.(delta.(2*c)) < arr.(delta.(2*c+1)) then
        delta.(c) <- delta.(2*c+1)
      else
        delta.(c) <- delta.(2*c) in
    if (b > e || i > e || i < b) then ()
    else if ((i = b) && (i = e)) then delta.(c) <- i
    else if i <= (b+e)/2 then begin
      update (2*c) b ((b+e)/2) i; upd c
    end else begin
      update (2*c+1) (((b+e)/2)+1) e i; upd c
    end

  let inc i v =
    arr.(i) <- v;
    update 1 0 (n-1) i

  let rec query c b e i j =
    if (b > e || i > j || b > j || i > e) then -1 else
    if i <= b && e <= j then delta.(c)
    else
      let first = query (2*c) b ((b+e)/2) i j in
      let second = query (2*c+1) ((b+e)/2+1) e i j in
      match first,second with
      | -1,_ -> second
      | _,-1 -> first
      | _,_ ->
        if arr.(first) < arr.(second) then second
        else first

  let max_range i j =
    query 1 0 (n-1) i j

  let max_range_value i j =
    let ind = max_range i j in
    arr.(ind)
end

let () =
  let n = read_int () in
  let module Size = struct
    let size = n
  end in
  let module Segtree = MakeSegTree(Size) in
  let dummy = Array.make (n-1) () in
  let arr = Array.map (fun _ -> read_int() -1) dummy in
  let add_to_seg i e =
    Segtree.inc i e in
  Array.iteri add_to_seg arr;
  Segtree.inc (n-1) 0;
  let dp = Array.make n Int64.zero in
  for i = n-2 downto 0 do
    let ma = Segtree.max_range (i+1) arr.(i) in
    dp.(i) <- Int64.sub dp.(ma) (Int64.of_int ((arr.(i) - ma) + (n - i - 1)));
  done;
  print_int64 (Array.fold_left Int64.add Int64.zero dp);;

let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x);;
let print_int x = Printf.printf "%d " x;;
let print s = Printf.printf "%s" s;;

module type S = sig
  val set : int -> int -> unit
  val get : int -> int
  val inc : int -> int -> unit
  val max_range : int -> int -> int
  val max_range_value : int -> int -> int
end

module MakeSegTree (M: (sig val size : int end)) : S = struct
  let n = M.size
  let nn = 3 * n

  let arr = Array.make n 0
  let delta = Array.make nn 0

  let set i v = arr.(i) <- v
  let get i = arr.(i)

  let rec update c b e i =
    let upd c =
      if arr.(delta.(2*c)) < arr.(delta.(2*c+1)) then
        delta.(c) <- delta.(2*c+1)
      else
        delta.(c) <- delta.(2*c) in
    if (b > e || i > e || i < b) then ()
    else if ((i = b) && (i = e)) then delta.(c) <- i
    else if i <= (b+e)/2 then begin
      update (2*c) b ((b+e)/2) i; upd c
    end else begin
      update (2*c+1) (((b+e)/2)+1) e i; upd c
    end

  let inc i v =
    arr.(i) <- v;
    update 1 0 (n-1) i

  let rec query c b e i j =
    if (b > e || i > j || b > j || i > e) then -1 else
    if i <= b && e <= j then delta.(c)
    else
      let first = query (2*c) b ((b+e)/2) i j in
      let second = query (2*c+1) ((b+e)/2+1) e i j in
      match first,second with
      | -1,_ -> second
      | _,-1 -> first
      | _,_ ->
        if arr.(first) < arr.(second) then second
        else first

  let max_range i j =
    query 1 0 (n-1) i j

  let max_range_value i j =
    let ind = max_range i j in
    arr.(ind)
end

let () =
  let n = read_int () in
  let module Size = struct
    let size = n
  end in
  let module Segtree = MakeSegTree(Size) in
  let dummy = Array.make (n-1) () in
  let arr = Array.map read_int dummy in
  let add_to_seg i e =
    Segtree.inc i e in
  Array.iteri add_to_seg arr;
  let dp = Array.make (n-1) 1 in
  let process cur e =
    let res,i = cur in
    let ran = arr.(i) in
    let ma = Segtree.max_range (i+1) (ran-1) in
    if ran < n then dp.(ma) <- dp.(ma) + e;
    (*print_int ma;
    print_int e;
    print_int (res + e*(n-i-1));
    print "\n";*)
    (res + e*(n-i-1)),(i+1) in
  let res,_ = Array.fold_left process (0,0) dp in
  print_int res;;

let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x);;
let print_int x = Printf.printf "%d " x;;
let print s = Printf.printf "%s" s;;

module type S = sig
  val set : int -> int -> unit
  val get : int -> int
  val inc : int -> int -> unit
  val max_range : int -> int -> int
  val max_range_value : int -> int -> int
end

module MakeSegTree (M: (sig val size : int end)) : S = struct
  let n = M.size
  let nn = 3 * n

  let arr = Array.make n 0
  let delta = Array.make nn 0

  let set i v = arr.(i) <- v
  let get i = arr.(i)

  let rec update c b e i =
    let upd c =
      if arr.(delta.(2*c)) < arr.(delta.(2*c+1)) then
        delta.(c) <- delta.(2*c+1)
      else
        delta.(c) <- delta.(2*c) in
    if (b > e || i > e || i < b) then ()
    else if ((i = b) && (i = e)) then delta.(c) <- i
    else if i <= (b+e)/2 then begin
      update (2*c) b ((b+e)/2) i; upd c
    end else begin
      update (2*c+1) (((b+e)/2)+1) e i; upd c
    end

  let inc i v =
    arr.(i) <- v;
    update 1 0 (n-1) i

  let rec query c b e i j =
    if (b > e || i > j || b > j || i > e) then -1 else
    if i <= b && e <= j then delta.(c)
    else
      let first = query (2*c) b ((b+e)/2) i j in
      let second = query (2*c+1) ((b+e)/2+1) e i j in
      match first,second with
      | -1,_ -> second
      | _,-1 -> first
      | _,_ ->
        if arr.(first) < arr.(second) then second
        else first

  let max_range i j =
    query 1 0 (n-1) i j

  let max_range_value i j =
    let ind = max_range i j in
    arr.(ind)
end

let () =
  let n = read_int () in
  let module Size = struct
    let size = n
  end in
  let module Segtree = MakeSegTree(Size) in
  let dummy = Array.make (n-1) () in
  let arr = Array.map (fun _ -> read_int() -1) dummy in
  let add_to_seg i e =
    Segtree.inc i e in
  Array.iteri add_to_seg arr;
  Segtree.inc (n-1) 0;
  let dp = Array.make n 0 in
  for i = n-2 downto 0 do
    let ma = Segtree.max_range (i+1) arr.(i) in
    dp.(i) <- dp.(ma) - (arr.(i) - ma) + (n - i - 1);
  done;
  print_int (Array.fold_left (fun a b -> a + b) 0 dp);;