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

let n = read_int () ;;

let s =
    let rec loop i n f =
        if i > n then f []
        else let si = read_int () in loop (i + 1) n (fun l -> f (si :: l))
    in loop 1 n (fun x -> x) ;;

let count x =
    let rec loop total = function
        | [] -> total
        | h :: t -> loop (total + if h = x then 1 else 0) t
    in loop 0 ;;

let one = count 1 s
    and two = count 2 s
    and three = count 3 s
    and four = count 4 s ;;

let group31 = min one three ;;
let group21 = min (two mod 2) (one - group31) ;;
let group2 = two / 2 + if group21 = 1 then 0 else (two mod 2) ;;
let group1 = ((max 0 (one - group31 - group21 * 2)) + 3) / 4 ;;

let taxis = four + three + group2 + group21 + group1 ;;

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

let _ =
    let main () =
        let n = read_int () in
        let arr = Array.create 5 0 in
        let rec loop i =
            if i > 0 then (
                Scanf.scanf " %d" (fun x -> arr.(x) <- arr.(x) + 1);
                loop (i - 1)
            )
        in
        loop n;
        let c = arr.(4) + arr.(3) in
        let _ = arr.(1) <- arr.(1) - min arr.(1) arr.(3) in
        let c = c + (arr.(2) + 1) / 2 in
        let _ = arr.(1) <- arr.(1) - min arr.(1) ((arr.(2) mod 2) * 2) in
        let r = c + (arr.(1) + 3) / 4 in
        Printf.printf "%d\n" r
    in
    main ()

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

let numx = Array.make 4 0;;
let n = read_int ();;
  for i = 1 to n do
    Scanf.scanf "%d " (fun x -> numx.(x-1)<- numx.(x-1)+1)
  done

let num_cars = ref ( numx.(3) + numx.(1)/2 );;
let () = numx.(1) <- numx.(1) mod 2;;
let () = if (numx.(0) <= numx.(2)  ) then
           begin
             numx.(2) <- numx.(2) - numx.(0);
             num_cars := !num_cars + numx.(0);
             numx.(0) <- 0
           end
         else
           begin
             num_cars := !num_cars + numx.(2);
             numx.(0) <- numx.(0) - numx.(2);
           end
;;

let () = if (numx.(0) == 0) then
           num_cars := !num_cars + numx.(2) + numx.(1)
         else
           begin
             if(numx.(1) == 1) then
               begin
                 num_cars := !num_cars + 1;
                 numx.(0) <- numx.(0) -2
               end;
             if (numx.(0) > 0) then
               num_cars := !num_cars + ( numx.(0) + 3) /4
           end
;;
let ()= print_int !num_cars; print_endline "";;

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

let n = read_int ();;
let a = Array.init n (fun i -> read_int () );;
let numx = Array.make 4 0;;
let vote i = numx.(i-1) <- numx.(i-1) + 1;;
let () = Array.iter (fun x -> vote x) a;;

let num_cars = ref ( numx.(3) + numx.(1)/2 );;

let () = numx.(1) <- numx.(1) mod 2;;

let () = if (numx.(0) <= numx.(2)  ) then
           begin
             numx.(2) <- numx.(2) - numx.(0);
             num_cars := !num_cars + numx.(0);
             numx.(0) <- 0
           end
         else
           begin
             num_cars := !num_cars + numx.(2);
             numx.(0) <- numx.(0) - numx.(2);
           end
;;

let () = if (numx.(0) == 0) then
           num_cars := !num_cars + numx.(2) + numx.(1)
         else
           begin
             if(numx.(1) == 1) then
               begin
                 num_cars := !num_cars + 1;
                 numx.(0) <- numx.(0) -2
               end;
             if (numx.(0) > 0) then
               num_cars := !num_cars + ( numx.(0) + 3) /4
           end
;;

let ()= print_int !num_cars; print_endline "";;

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

let rec read list n =
    if n > 0
    then
        let var = Scanf.scanf "%d " (fun n -> n) in
        read (var :: list) (n - 1)
    else
        List.rev list

let separate list s =
    let rec filter list s current_counts =
    if s > 0
    then
        let count = List.length(List.filter (fun int -> int = s) list) in
        filter list (s - 1) (count :: current_counts)
    else
        current_counts
    in filter list s []

let no_owerflow n =
    if n < 0
    then
        0
    else
        n

let taxis list = match (separate list 4) with
|[ones; twos; threes; fours] -> fours + threes + (twos * 2 + no_owerflow(ones - threes) + 3) / 4

let solve input = taxis (read [] input)

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

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

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

let n = read_int ();;
let a = Array.init n (fun i -> read_int () );;
let numx = Array.make 4 0;;
let vote i = numx.(i-1) <- numx.(i-1) + 1;;
let () = Array.iter (fun x -> vote x) a;;

let num_cars = ref ( numx.(3) + numx.(1)/2 );;

let () = numx.(1) <- numx.(1) mod 2;;

let () = if (numx.(0) <= numx.(2)  ) then
           begin
             numx.(2) <- numx.(2) - numx.(0);
             num_cars := !num_cars + numx.(0);
             numx.(0) <- 0
           end
         else
           begin
             num_cars := !num_cars + numx.(2);
             numx.(0) <- numx.(0) - numx.(2);
             numx.(2) <- 0
           end
;;

let () = if (numx.(0) == 0) then
           num_cars := !num_cars + numx.(2) + numx.(1)
         else
           begin
             if(numx.(1) == 1) then
               begin
                 num_cars := !num_cars + 1;
                 numx.(0) <- numx.(0) -2
               end;
             if (numx.(0) > 0) then
               begin
                 if (numx.(0) mod 4 == 0) then
                   num_cars := !num_cars + numx.(0)/4
                 else
                   num_cars := !num_cars + numx.(0)/4 + 1
               end
           end
;;

let ()= print_int !num_cars; print_endline "";;

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

let solve n =
  let data = [|0; 0; 0; 0; 0|] in
    for i = 1 to n do
      match read_int () with
      | 1 when data.(3) > 0 -> data.(3) <- data.(3) - 1; data.(4) <- data.(4) + 1
      | 2 when data.(2) > 0 -> data.(2) <- data.(2) - 1; data.(4) <- data.(4) + 1
      | 3 when data.(1) > 0 -> data.(1) <- data.(1) - 1; data.(4) <- data.(4) + 1
      | i -> data.(i) <- data.(i) + 1
    done;

    if data.(2) > 0 && data.(1) > 0 then begin
      data.(2) <- 0;
      data.(4) <- data.(4) + 1;
      if data.(1) > 1
      then data.(1) <- data.(1)-2
      else data.(1) <- 0
    end;

    if data.(1) mod 4 = 0
    then data.(4) + data.(3) + data.(2) + data.(1) / 4
    else data.(4) + data.(3) + data.(2) + data.(1) / 4 + 1

let () =
  let n = read_int ()
  in Printf.printf "%d\n" (solve n)

open Scanf;;

let parse n =
  let rec parseInt n acc =
    if n <= 0 then acc
    else
      match bscanf Scanning.stdin " %d" (fun x -> x) with
      | d -> parseInt (n - 1) (d :: acc)
      | exception _ -> acc
  in parseInt n [];;

let taxis numList =
  let ones, twos, threes, fourths =
    List.fold_left ( fun (ones, twos, threes, fourths) x ->
      match x with
      | 1 -> (ones + 1, twos, threes, fourths)
      | 2 -> (ones, twos + 1, threes, fourths)
      | 3 -> (ones, twos, threes + 1, fourths)
      | 4 -> (ones, twos, threes, fourths + 1)
      | _ -> (ones, twos, threes, fourths)
    ) (0, 0, 0, 0) numList
  in let needed = fourths
  in let needed = needed + twos / 2
  in let twos = twos mod 2
  in let needed = needed + threes
  in if ones > threes then
    let ones = ones - threes
    in let needed = needed + ones / 4
    in if twos = 1 then
      if ones  mod 4 > 2 then
        needed + 2
      else
        needed + 1
    else
      if ones mod 4 = 0 then
        needed
      else
        needed + 1
  else
    needed + twos / 2 + twos mod 2

let main () =
  let n = bscanf Scanning.stdin "%d\n" (fun x -> x)
  in let numList = parse n
  in taxis numList;;

Printf.printf "%d" (main ());;

open Scanf
open Printf
let n = scanf "%d\n" (fun n -> n)

let (n1,n2,n3,n4) =
  let a = Array.make 10 0 in
  for i=0 to n-1 do
    let i =  scanf "%d " (fun x -> x) in
    a.(i) <- a.(i) + 1
  done;
  (a.(1),a.(2),a.(3),a.(4))

let () = (*
  printf "%d %d %d %d\n" n1 n2 n3 n4; *)
  ()
let ans = ref n4
let n4 = 0
let incr2 x =
  ans := !ans +x;
  (*printf "incr by %d to %d\n" x !ans;*)
  ()
let info x = (*
  printf "Evaling %d \n" x;*)
  ()

let () =
  let m =  (min n1 n3) in
  incr2 m;
  let n1 = n1 - m in
  let n3 = n3-m in
  let n3 =
    info 3;
    if n3>0 then (incr2 n3; 0) else n3 in
  let n2 =
    info 2;
    incr2 (n2/2);
    n2 mod  2 in
  let (n1,n2) =
    info 2;
    if n2>0 then (incr2 1; (n1-2,0)) else (n1,n2) in
  let () =
    let last =
      if n1>0 then ( (if n1 mod 4 = 0 then 0 else 1) + (n1/4) )
      else 0
    in
    incr2 last
  in ()

let () = print_int !ans; print_newline ()

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

let readList n f =
  let rec loop n =
    if n == 0 then
      []
    else
      f() :: loop (n-1)
  in
  List.rev(loop n);;

let incValue arr x =
  arr.(x) <- arr.(x) + 1;;

let groupByList l =
  let counts = (Array.make 5 0) in
  let _ = List.map (incValue counts) l in
  counts;;

let rec fitTaxis counts =
  let sol = ref 0 in
  sol := counts.(4);

  let taxi3pack = min counts.(1) counts.(3) in
  sol := !sol + taxi3pack;
  counts.(1) <- counts.(1) - taxi3pack;
  counts.(3) <- counts.(3) - taxi3pack;
  sol := !sol + counts.(3);
  let remainder = counts.(1) + (counts.(2) * 2) in
  if remainder mod 4 == 0 then
    remainder / 4 + !sol
  else
    remainder / 4 + 1 + !sol;;

let _ =
  let n = read_int() in
  let l = readList n nextInt in
  let counts = groupByList l in
  print_int(fitTaxis counts);
  print_newline()

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

let readList n f =
  let rec loop n =
    if n == 0 then
      []
    else
      f() :: loop (n-1)
  in
  List.rev(loop n);;

let incValue arr x =
  arr.(x) <- arr.(x) + 1;;

let groupByList l =
  let counts = (Array.make 5 0) in
  let _ = List.map (incValue counts) l in
  counts;;

let fitTaxis counts =
  let sol = ref 0 in
  sol := counts.(4);

  let taxi3pack = min counts.(1) counts.(3) in
  sol := !sol + taxi3pack;
  counts.(1) <- counts.(1) - taxi3pack;
  counts.(3) <- counts.(3) - taxi3pack;
  sol := !sol + counts.(3);
  !sol + (counts.(1) + (counts.(2) * 2) + 3) / 4;;

let _ =
  let n = read_int() in
  let l = readList n nextInt in
  let counts = groupByList l in
  print_int(fitTaxis counts);
  print_newline()

open Scanf;;
open Printf;;
open List;;

let read_and_count n =
  let rec read n acc =
    match n with
    | 1 -> let y = (scanf "%d" (fun x->x) ) in
       acc.(y) <- acc.(y) + 1;
       acc
    | x -> let y = (scanf "%d " (fun x->x)) in
       acc.(y) <- acc.(y) + 1;
       read (x-1) acc
  in read n (Array.make 5 0)
;;

let n = scanf "%d\n" (fun x -> x) in
let counters = (read_and_count n) in
let sol = ref 0 in
sol := !sol + counters.(4);
let mini = (min counters.(3) counters.(1)) in
sol := !sol + mini;
counters.(3) <- counters.(3) - mini;
counters.(1) <- counters.(1) - mini;
sol := !sol + counters.(3);
sol := !sol + ( (counters.(2)*2 + counters.(1) + 3) / 4);
printf "%d\n" !sol;

let read_int()=Scanf.scanf " %d" (fun x->x);;
let n=read_int();;
let tab=Array.make 5 0;;
for i=1 to n do
   let k=read_int() in tab.(k)<-tab.(k)+1
done;;
if (tab.(2) mod 2)=0 then (tab.(1)<-max 0 (tab.(1)-tab.(3));tab.(2)<-tab.(2)/2)
else (tab.(1)<-max 0 (tab.(1)-tab.(3)-2);tab.(2)<-tab.(2)/2+1);;
if (tab.(1) mod 4)=0 then tab.(1)<-tab.(1)/4 else tab.(1)<-tab.(1)/4+1;;
print_int(tab.(1)+tab.(2)+tab.(3)+tab.(4));;

(* Codeforces 158 B Taxi *)

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

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

let printlisti li = List.iter (fun i -> (print_int i; print_string " ")) li;;

let taxi1 n1 =
    let n14 = n1/4 in
    let nn1 = n1 - n14*4 in
    let t1 = if nn1 > 0 then (n14 + 1)
            else n14 in
    (* let _ = Printf.printf "n1 t1 = %d %d\n" n1 t1 in *)
    t1;;

let taxi2 n1 n2 =
    let n22 = n2/2 in
    let nn2 = n2 - 2 * n22 in
    let nn1 = if nn2 == 0 then n1
        else if n1 > 2 then n1 - 2
        else 0 in
    let t2 = n22 + nn2 + taxi1 nn1 in
    (* let _ = Printf.printf "n1,n2 = %d %d t2 = %d\n" n1 n2 t2 in *)
    t2;;

let taxi3 n1 n2 n3 =
    let nn1 = if n3 > n1 then 0 else (n1 - n3) in
    n3 + taxi2 nn1 n2;;

let taxi4 n1 n2 n3 n4 =
    n4 + taxi3 n1 n2 n3;;

let rec count4 n1 n2 n3 n4 a = match a with
    | [] -> (n1, n2, n3, n4)
    | h :: t ->
    match h with
      | 1 -> count4 (n1 + 1) n2 n3 n4 t
      | 2 -> count4 n1 (n2 + 1) n3 n4 t
      | 3 -> count4 n1 n2 (n3 + 1) n4 t
      | 4 -> count4 n1 n2 n3 (n4 + 1) t
      | _ ->
      let _ = print_string "bad h in count4\n" in
      (-9999,0,0,0);;

let main() =
	let n = gr () in
	let s = readlist n [] in
	let n1,n2,n3,n4 = count4 0 0 0 0 s in
    (* let _ = Printf.printf "n1-4 = %d %d %d %d\n" n1 n2 n3 n4 in *)
	let taxi = taxi4 n1 n2 n3 n4 in
	begin
		print_int taxi;
		print_string "\n"
	end;;

main();;

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

let n = read_int ();;
let a = Array.init n (fun i -> read_int () );;
let numx = Array.make 4 0;;
let vote i = numx.(i-1) <- numx.(i-1) + 1;;
let () = Array.iter (fun x -> vote x) a;;

let num_cars = ref ( numx.(3) + numx.(1)/2 + numx.(0)/4 );;

let remain_2 = numx.(1) mod 2;;
let remain_1 = numx.(0) mod 4;;

let () = if (remain_1 > numx.(2)) then
             num_cars := !num_cars + numx.(2) + (remain_1 - numx.(2) )
         else if (remain_1 == numx.(2)) then
           num_cars := !num_cars + numx.(2) + remain_2
         else
           num_cars := !num_cars + remain_1 + (numx.(2) - remain_1) + remain_2
;;

let () = (print_int !num_cars; print_endline "");;

         (* This is the easy way *)
(*
         let b = Array.to_list cars
        |> List.filter (fun x -> (x>0))
        |> List.length
;;

let ()= print_int b; print_endline "";;
 *)

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

let n = read_int ();;

let a = Array.init n (fun i -> read_int () );;

let () = a |> Array.sort (fun x y -> (if (x < y ) then 1 else -1) );;

let cars = Array.make n 0;; (* Call n empty cars *)

  exception BreakLoop;;
  for i = 0 to n-1 do
    try
    for j = 0 to n-1 do
      let x = a.(i) + cars.(j) in
      if ( x <= 4 ) then
        begin
          cars.(j) <- x;
          raise BreakLoop
        end
    done
    with BreakLoop -> ()

  done;;

  let num_cars = ref 0;;
    try
    for i = 0; to n-1 do
      if(cars.(i)>0) then
        num_cars := !num_cars + 1
      else
        raise BreakLoop
    done
    with BreakLoop -> print_int !num_cars; print_endline "";;

         (* This is the easy way *)
(*
         let b = Array.to_list cars
        |> List.filter (fun x -> (x>0))
        |> List.length
;;

let ()= print_int b; print_endline "";;
 *)

let _ =
    let main () =
        let n = read_int () in
        let arr = Array.create 5 0 in
        let rec loop i =
            if i > 0 then (
                Scanf.scanf " %d" (fun x -> arr.(x) <- arr.(x) + 1);
                loop (i - 1)
            )
        in
        loop n;
        let c = arr.(4) + arr.(3) in
        let _ = arr.(1) <- arr.(1) - min arr.(1) arr.(3) in
        let c = c + (arr.(2) + 1) / 2 in
        let _ = arr.(1) <- arr.(1) - min arr.(1) ((arr.(2) mod 1) * 2) in
        let r = c + (arr.(1) + 3) / 4 in
        Printf.printf "%d\n" r
    in
    main ()

open Scanf;;

let parse n =
  let rec parseInt n acc =
    if n <= 0 then acc
    else
      match bscanf Scanning.stdin " %d" (fun x -> x) with
      | d -> parseInt (n - 1) (d :: acc)
      | exception _ -> acc
  in parseInt n [];;

let taxis numList =
  let ones, twos, threes, fourths =
    List.fold_left ( fun (ones, twos, threes, fourths) x ->
      match x with
      | 1 -> (ones + 1, twos, threes, fourths)
      | 2 -> (ones, twos + 1, threes, fourths)
      | 3 -> (ones, twos, threes + 1, fourths)
      | 4 -> (ones, twos, threes, fourths + 1)
      | _ -> (ones, twos, threes, fourths)
    ) (0, 0, 0, 0) numList
  in let needed = fourths
  in let needed = needed + twos / 2
  in let twos = twos mod 2
  in let needed = needed + threes
  in if ones > threes then
    let ones = ones - threes
    in let needed = needed + ones / 4
    in if twos = 1 then
      if ones  mod 4 > 2 then
        needed + 2
      else
        needed + 1
    else
      if (ones - threes) mod 4 = 0 then
        needed
      else
        needed + 1
  else
    needed + twos / 2 + twos mod 2

let main () =
  let n = bscanf Scanning.stdin "%d\n" (fun x -> x)
  in let numList = parse n
  in taxis numList;;

Printf.printf "%d" (main ());;

open Scanf;;

let parse () =
  let rec parseInt acc =
    match bscanf Scanning.stdin " %d" (fun x -> x) with
    | d -> parseInt (d :: acc)
    | exception _ -> acc
  in parseInt [];;

let taxis numList =
  let ones, twos, threes, fourths =
    List.fold_left ( fun (ones, twos, threes, fourths) x ->
      match x with
      | 1 -> (ones + 1, twos, threes, fourths)
      | 2 -> (ones, twos + 1, threes, fourths)
      | 3 -> (ones, twos, threes + 1, fourths)
      | 4 -> (ones, twos, threes, fourths + 1)
      | _ -> (ones, twos, threes, fourths)
    ) (0, 0, 0, 0) numList
  in let needed = fourths
  in let needed = needed + twos / 2
  in let twos = twos mod 2
  in let needed = needed + threes
  in if ones > threes then
    let needed = needed + ones / 4
    in if twos = 1 then
      if ones mod 4 > 2 then
        needed + 2
      else
        needed + 1
    else
      if ones mod 4 = 0 then
        needed
      else
        needed + 1
  else
    needed + twos / 2 + twos mod 2

let main () =
  let _ = bscanf Scanning.stdin "%d\n" (fun x -> x)
  in let numList = parse ()
  in taxis numList;;

Printf.sprintf "%d" (main ());;

open Scanf;;

let parse n =
  let rec parseInt n acc =
    if n <= 0 then acc
    else
      match bscanf Scanning.stdin " %d" (fun x -> x) with
      | d -> parseInt (n - 1) (d :: acc)
      | exception _ -> acc
  in parseInt n [];;

let taxis numList =
  let ones, twos, threes, fourths =
    List.fold_left ( fun (ones, twos, threes, fourths) x ->
      match x with
      | 1 -> (ones + 1, twos, threes, fourths)
      | 2 -> (ones, twos + 1, threes, fourths)
      | 3 -> (ones, twos, threes + 1, fourths)
      | 4 -> (ones, twos, threes, fourths + 1)
      | _ -> (ones, twos, threes, fourths)
    ) (0, 0, 0, 0) numList
  in let needed = fourths
  in let needed = needed + twos / 2
  in let twos = twos mod 2
  in let needed = needed + threes
  in if ones > threes then
    let needed = needed + ones / 4
    in if twos = 1 then
      if ones mod 4 > 2 then
        needed + 2
      else
        needed + 1
    else
      if ones - threes mod 4 = 0 then
        needed
      else
        needed + 1
  else
    needed + twos / 2 + twos mod 2

let main () =
  let n = bscanf Scanning.stdin "%d\n" (fun x -> x)
  in let numList = parse n
  in taxis numList;;

Printf.printf "%d" (main ());;

open Scanf;;

let parse n =
  let rec parseInt n acc =
    if n <= 0 then acc
    else
      match bscanf Scanning.stdin " %d" (fun x -> x) with
      | d -> parseInt (n - 1) (d :: acc)
      | exception _ -> acc
  in parseInt n [];;

let taxis numList =
  let ones, twos, threes, fourths =
    List.fold_left ( fun (ones, twos, threes, fourths) x ->
      match x with
      | 1 -> (ones + 1, twos, threes, fourths)
      | 2 -> (ones, twos + 1, threes, fourths)
      | 3 -> (ones, twos, threes + 1, fourths)
      | 4 -> (ones, twos, threes, fourths + 1)
      | _ -> (ones, twos, threes, fourths)
    ) (0, 0, 0, 0) numList
  in let needed = fourths
  in let needed = needed + twos / 2
  in let twos = twos mod 2
  in let needed = needed + threes
  in if ones > threes then
    let ones = ones - threes
    in let needed = needed + ones / 4
    in if twos = 1 then
      if ones  mod 4 > 2 then
        needed + 2
      else
        needed + 1
    else
      if (ones - threes) mod 4 = 0 then
        needed + 300
      else
        needed + 1
  else
    needed + twos / 2 + twos mod 2

let main () =
  let n = bscanf Scanning.stdin "%d\n" (fun x -> x)
  in let numList = parse n
  in taxis numList;;

Printf.printf "%d" (main ());;

open Scanf;;

let parse n =
  let rec parseInt n acc =
    if n <= 0 then acc
    else
      match bscanf Scanning.stdin " %d" (fun x -> x) with
      | d -> parseInt (n - 1) (d :: acc)
      | exception _ -> acc
  in parseInt n [];;

let taxis numList =
  let ones, twos, threes, fourths =
    List.fold_left ( fun (ones, twos, threes, fourths) x ->
      match x with
      | 1 -> (ones + 1, twos, threes, fourths)
      | 2 -> (ones, twos + 1, threes, fourths)
      | 3 -> (ones, twos, threes + 1, fourths)
      | 4 -> (ones, twos, threes, fourths + 1)
      | _ -> (ones, twos, threes, fourths)
    ) (0, 0, 0, 0) numList
  in let needed = fourths
  in let needed = needed + twos / 2
  in let twos = twos mod 2
  in let needed = needed + threes
  in if ones > threes then
    let needed = needed + ones / 4
    in if twos = 1 then
      if ones mod 4 > 2 then
        needed + 2
      else
        needed + 1
    else
      if ones mod 4 = 0 then
        needed
      else
        needed + 1
  else
    needed + twos / 2 + twos mod 2

let main () =
  let n = bscanf Scanning.stdin "%d\n" (fun x -> x)
  in let numList = parse n
  in taxis numList;;

Printf.printf "%d" (main ());;

open Scanf;;

let parse n =
  let rec parseInt n acc =
    if n <= 0 then acc
    else
      match bscanf Scanning.stdin " %d" (fun x -> x) with
      | d -> parseInt (n - 1) (d :: acc)
      | exception _ -> acc
  in parseInt n [];;

let taxis numList =
  let ones, twos, threes, fourths =
    List.fold_left ( fun (ones, twos, threes, fourths) x ->
      match x with
      | 1 -> (ones + 1, twos, threes, fourths)
      | 2 -> (ones, twos + 1, threes, fourths)
      | 3 -> (ones, twos, threes + 1, fourths)
      | 4 -> (ones, twos, threes, fourths + 1)
      | _ -> (ones, twos, threes, fourths)
    ) (0, 0, 0, 0) numList
  in let needed = fourths
  in let needed = needed + twos / 2
  in let twos = twos mod 2
  in let needed = needed + threes
  in if ones > threes then
    let needed = needed + ones / 4
    in if twos = 1 then
      if ones mod 4 > 2 then
        needed + 2
      else
        needed + 1
    else
      if (ones - threes) mod 4 = 0 then
        needed
      else
        needed + 1
  else
    needed + twos / 2 + twos mod 2

let main () =
  let n = bscanf Scanning.stdin "%d\n" (fun x -> x)
  in let numList = parse n
  in taxis numList;;

Printf.printf "%d" (main ());;


open StdLabels

type vertex =
  { x : int;
    y : int;
    rot : int;
  }

let [n; m] = read_line () |> Str.(split (regexp " ")) |> List.map ~f:int_of_string
let s = n * m

let rotate = function
  | '+' -> '+'
  | '-' -> '|'
  | '|' -> '-'
  | '^' -> '>'
  | '>' -> 'v'
  | 'v' -> '<'
  | '<' -> '^'
  | 'U' -> 'R'
  | 'R' -> 'D'
  | 'D' -> 'L'
  | 'L' -> 'U'
  | '*' -> '*'
  | _ -> failwith "Unknown block"

let maze = Bytes.make (4 * s) ' '
let () =
  for y = 0 to n-1 do
    Bytes.blit ~src:(read_line ()) ~src_pos:0 ~dst:maze ~dst_pos:(y * m) ~len:m
  done;
  for i = s to 4 * s - 1 do
    maze.[i] <- rotate maze.[i-s]
  done

let read_coords () =
  read_line ()
  |> Str.(split (regexp " "))
  |> List.map ~f:(fun s -> int_of_string s - 1)
  (* Discovered that (X,Y) are named (Y,X) in problem! *)
  |> fun [y; x] -> y * m + x

let start = read_coords ()
let goal = read_coords ()

let default d = function
  | Some x -> x
  | None -> d

module Int = struct
  type t = int
  let compare = compare
end

module Vertex =
  struct
    type t = vertex
    let compare = compare
  end

module PriorityQueue(Priority : Map.OrderedType) =
  struct
    module PriorityMap = Map.Make(Priority)

    let create = ref PriorityMap.empty

    let add prio elt pq =
      let queue =
        if PriorityMap.mem prio !pq then
          PriorityMap.find prio !pq
        else
          let q = Queue.create () in
          pq := PriorityMap.add prio q !pq;
          q in
      Queue.add elt queue

    let pop pq =
      if PriorityMap.is_empty !pq then
        None
      else
        let prio, queue = PriorityMap.min_binding !pq in
        let r = Queue.pop queue in
        if Queue.is_empty queue then
          pq := PriorityMap.remove prio !pq;
        Some r
  end

module PriorityVertex =
  struct
    type t = int * Vertex.t
    let compare = compare
  end

module VertexSet = Set.Make(Vertex)

module VertexQueue = PriorityQueue(Int)

let astar ?(heuristic = fun v -> 0) ~start ~is_goal =
  let edge_length = 1 in
  let marks = Bytes.make (4*m*n) ' ' in
  let fringe = VertexQueue.create in
  VertexQueue.add (heuristic start) (start, 0) fringe;
  let rec go () =
    match VertexQueue.pop fringe with
    | None -> None
    | Some (v, dist) ->
       if is_goal v then
         Some dist
       else if marks.[v] = '*' then
         go ()
       else
         let dist' = dist + edge_length in
         marks.[v] <- '*';
         let enqueue u =
           if marks.[u] <> '*' then
             VertexQueue.add (dist' + heuristic u) (u, dist') fringe in
         let c = maze.[v] in
         let pl, pu, pr, pd = "+-<URD", "+|^RDL", "+->DLU", "+|vLUR" in
         let sc = String.contains in
         let xy = v mod s in
         let x = xy mod m and y = xy / m in
         enqueue ((v + s) mod (4 * s));
         if x > 0 && sc pl c && sc pr maze.[v-1] then
           enqueue (v-1);
         if y > 0 && sc pu c && sc pd maze.[v-m] then
           enqueue (v-m);
         if x < m-1 && sc pr c && sc pl maze.[v+1] then
           enqueue (v+1);
         if y < n-1 && sc pd c && sc pu maze.[v+m] then
           enqueue (v+m);
         go () in
  go ()

let is_goal v = (v - goal) mod s = 0

let heuristic v =
  let xy = v mod s in
  let dx = xy mod m - goal mod m and dy = xy/m - goal/m in
  abs dx + abs dy

let () =
  astar ~heuristic ~start ~is_goal
  |> default (-1)
  |> Printf.printf "%d\n"

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

let () =
  let (n,m) = read_pair () in
  let lab_str = Array.init n (fun _ -> read_string()) in
  let (xt,yt) = read_pair() in
  let (xt,yt) = (xt-1, yt-1) in
  let (xm,ym) = read_pair() in
  let (xm,ym) = (xm-1, ym-1) in

  let vt = Array.make_matrix n m 0 in

  let visited (i,j,r) = vt.(i).(j) land (1 lsl r) <> 0 in
  let visit (i,j,r) = vt.(i).(j) <- vt.(i).(j) lor (1 lsl r) in

  let lab = Array.make_matrix n m [||] in

  for i=0 to n-1 do
    for j=0 to m-1 do
      lab.(i).(j) <-
	match lab_str.(i).[j] with
	  | '+' -> [|1;1;1;1|]
	  | '-' -> [|1;0;1;0|]
	  | '|' -> [|0;1;0;1|]
	  | '^' -> [|0;1;0;0|]
	  | '>' -> [|1;0;0;0|]
	  | '<' -> [|0;0;1;0|]
	  | 'v' -> [|0;0;0;1|]
	  | 'L' -> [|1;1;0;1|]
	  | 'R' -> [|0;1;1;1|]
	  | 'U' -> [|1;0;1;1|]
	  | 'D' -> [|1;1;1;0|]
	  | '*' -> [|0;0;0;0|]
	  | _ -> failwith "bad input"
    done
  done;

  let out_bounds i j = i < 0 || j < 0 || i > n-1 || j > m-1 in

  let neighbors (i,j,r) =
    List.fold_left (fun ac (di,dj,s0) ->
      if lab.(i).(j).((s0+r) mod 4) = 0 then ac
      else if out_bounds (i+di) (j+dj) then ac else
	let s1 = (r + s0 + 2) mod 4 in
	if lab.(i+di).(j+dj).(s1) = 0 then ac else (i+di, j+dj, r)::ac
    ) [(i, j, (r+1) mod 4)] [(-1,0,1);(1,0,3);(0,-1,2);(0,1,0)]
  in

  let rec bfs nodes level = if nodes = [] then None else (
    let new_nodes = List.fold_left (fun ac x ->
      List.fold_left (fun ac y ->
	if visited y then ac else (visit y; y::ac)
      ) ac (neighbors x)
    ) [] nodes in
    if visited (xm,ym,0) || visited (xm,ym,1) || visited (xm,ym,2) || visited (xm,ym,3)
    then Some (level+1) else bfs new_nodes (level+1)
  ) in

  visit (xt,yt,0);

  let answer =
    if (xm,ym) = (xt,yt) then 0
    else match bfs [(xt,yt,0)] 0 with None -> -1 | Some level -> level
  in

  printf "%d\n" answer

open StdLabels

type vertex =
  { x : int;
    y : int;
    rot : int;
  }

let [n; m] = read_line () |> Str.(split (regexp " ")) |> List.map ~f:int_of_string
let s = n * m

let rotate = function
  | '+' -> '+'
  | '-' -> '|'
  | '|' -> '-'
  | '^' -> '>'
  | '>' -> 'v'
  | 'v' -> '<'
  | '<' -> '^'
  | 'U' -> 'R'
  | 'R' -> 'D'
  | 'D' -> 'L'
  | 'L' -> 'U'
  | '*' -> '*'
  | _ -> failwith "Unknown block"

let maze = Bytes.make (4 * s) ' '
let () =
  for y = 0 to n-1 do
    Bytes.blit ~src:(read_line ()) ~src_pos:0 ~dst:maze ~dst_pos:(y * m) ~len:m
  done;
  for i = s to 4 * s - 1 do
    maze.[i] <- rotate maze.[i-s]
  done

let read_coords () =
  read_line ()
  |> Str.(split (regexp " "))
  |> List.map ~f:(fun s -> int_of_string s - 1)
  (* Discovered that (X,Y) are named (Y,X) in problem! *)
  |> fun [y; x] -> y * m + x

let start = read_coords ()
let goal = read_coords ()

let default d = function
  | Some x -> x
  | None -> d

module Int = struct
  type t = int
  let compare = compare
end

module Vertex =
  struct
    type t = vertex
    let compare = compare
  end

module PriorityQueue(Priority : Map.OrderedType) =
  struct
    module PriorityMap = Map.Make(Priority)

    let create = ref PriorityMap.empty

    let add prio elt pq =
      let queue =
        if PriorityMap.mem prio !pq then
          PriorityMap.find prio !pq
        else
          let q = Queue.create () in
          pq := PriorityMap.add prio q !pq;
          q in
      Queue.add elt queue

    let pop pq =
      if PriorityMap.is_empty !pq then
        None
      else
        let prio, queue = PriorityMap.min_binding !pq in
        let r = Queue.pop queue in
        if Queue.is_empty queue then
          pq := PriorityMap.remove prio !pq;
        Some r
  end

module Graph =
  struct
    module PriorityVertex =
      struct
        type t = int * Vertex.t
        let compare = compare
      end

    module VertexSet = Set.Make(Vertex)

    module VertexQueue = PriorityQueue(Int)

    let astar ?(heuristic = fun v -> 0) ~edges ~start ~is_goal =
      let edge_length = 1 in
      let marks = Bytes.make (4*m*n) ' ' in
      let fringe = VertexQueue.create in
      VertexQueue.add (heuristic start) (start, 0) fringe;
      let rec go () =
        match VertexQueue.pop fringe with
        | None -> None
        | Some (v, dist) ->
           if is_goal v then
             Some dist
           else if marks.[v] = '*' then
             go ()
           else
             let dist' = dist + edge_length in
             marks.[v] <- '*';
             List.iter
               ~f:(fun u ->
                   if marks.[u] <> '*' then
                     VertexQueue.add (dist' + heuristic u) (u, dist') fringe)
               (edges v);
             go () in
      go ()
  end

let edges v =
  let c = maze.[v] in
  let pl, pu, pr, pd = "+-<URD", "+|^RDL", "+->DLU", "+|vLUR" in
  let sc = String.contains in
  let xy = v mod s in
  let x = xy mod m and y = xy / m in
  let r = ref [(v + s) mod (4 * s)] in
  if x > 0 && sc pl c && sc pr maze.[v-1] then
    r := v-1 :: !r;
  if y > 0 && sc pu c && sc pd maze.[v-m] then
    r := v-m :: !r;
  if x < m-1 && sc pr c && sc pl maze.[v+1] then
    r := v+1 :: !r;
  if y < n-1 && sc pd c && sc pu maze.[v+m] then
    r := v+m :: !r;
  !r

let is_goal v = (v - goal) mod s = 0

let heuristic v =
  let xy = v mod s in
  let dx = xy mod m - goal mod m and dy = xy/m - goal/m in
  abs dx + abs dy

let () =
  Graph.astar ~heuristic ~edges ~start ~is_goal
  |> default (-1)
  |> Printf.printf "%d\n"


let input = open_in "input.txt" ;;
let output = open_out "output.txt" ;;

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

let n = read_int () ;;

let olympiads =
    let rec loop i f =
        if i = n then f []
        else let m = read_int () and d = read_int () and
                 p = read_int () and t = read_int ()
        in loop (i + 1) (fun l -> f ((m, d, p, t) :: l))
    in loop 0 (fun x -> x) ;;

(* Create a cumulative array of days since January 1, 2012 for the year 2013 *)
let months = [|0; 31; 28; 31; 30; 31; 30; 31; 31; 30; 31; 30; 31|]
let cumulative_days = Array.make 13 366 ;;
for i = 1 to 12 do
    cumulative_days.(i) <- months.(i) + cumulative_days.(i - 1)
done ;;

let start m d t = cumulative_days.(m - 1) + d - t ;;

let olympiads =
    let compare (m1, d1, p1, t1) (m2, d2, p2, t2) =
        (start m1 d1 t1) - (start m2 d2 t2)
    in List.sort compare olympiads ;;

let jury_size olympiads =
    let rec loop oly intersection size = match oly with
        | [] ->  size
        | (m1, d1, p1, t1) as hd1 :: tl1 ->
            let rec filter l s f = match l with
                | [] -> (s, f [hd1])
                | (m2, d2, p2, t2) as hd2 :: tl2 ->
                    if (start m1 d1 t1) > (start m2 d2 1) then filter tl2 s f
                    else filter tl2 (s + p2) (fun v -> f (hd2 :: v))
            in let (newsize, newinter) = filter intersection p1 (fun x -> x)
            in loop tl1 newinter (max newsize size)
    in loop olympiads [] 0 ;;

Printf.fprintf output "%d\n" (jury_size olympiads);;

let chan = Scanf.Scanning.from_file "input.txt"
let read_int () = Scanf.bscanf chan " %d" (fun x -> x)

let ochan = open_out "output.txt"
let print_string s = Printf.fprintf ochan "%s" s

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 maxf i j f = fold (i+1) j (fun k a -> max (f k) a) (f i)

let h = Array.make 1000 0
let offset = 500

let day_number m d =
  let dpm = [|31;28;31;30;31;30;31;31;30;31;30;31|] in
  let md = sum 0 (m-2) (fun i->dpm.(i)) in
    md + (d-1)

let () =
  let n = read_int() in
    for i=1 to n do
      let m = read_int() in
      let d = read_int() in
      let p = read_int() in
      let t = read_int() in
      let dn = day_number m d in
    for j=dn-t to dn-1 do
      h.(j+offset) <- h.(j+offset) + p
    done;
    done;
    print_string (Printf.sprintf "%d\n" (maxf 0 999 (fun i-> h.(i))))

let chan = Scanf.Scanning.from_file "input.txt"
let read_int () = Scanf.bscanf chan " %d " (fun x -> x)

let ochan = open_out "output.txt"
let print_string s = Printf.fprintf ochan "%s" s

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 maxf i j f = fold (i+1) j (fun k a -> max (f k) a) (f i)

let h = Array.make 1000 0
let offset = 500

let day_number m d =
  let dpm = [|31;28;31;30;31;30;31;31;30;31;30;31|] in
  let md = sum 0 (m-2) (fun i->dpm.(i)) in
    md + (d-1)

let () =
  let n = read_int() in
    for i=1 to n do
      let m = read_int() in
      let d = read_int() in
      let p = read_int() in
      let t = read_int() in
      let dn = day_number m d in
    for j=dn-t to dn-1 do
      h.(j+offset) <- h.(j+offset) + p
    done;
    done;
    print_string (Printf.sprintf "%d\n" (maxf 0 999 (fun i-> h.(i))))

let chan = Scanf.Scanning.from_channel (open_in "input.txt")
let read_int () = Scanf.bscanf chan " %d " (fun x -> x)

let ochan = open_out "output.txt"
let print_string s = Printf.fprintf ochan "%s" s

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 maxf i j f = fold (i+1) j (fun k a -> max (f k) a) (f i)

let h = Array.make 1000 0
let offset = 500

let day_number m d =
  let dpm = [|31;28;31;30;31;30;31;31;30;31;30;31|] in
  let md = sum 0 (m-2) (fun i->dpm.(i)) in
    md + (d-1)

let () =
  let n = read_int() in
    for i=1 to n do
      let m = read_int() in
      let d = read_int() in
      let p = read_int() in
      let t = read_int() in
      let dn = day_number m d in
	for j=dn-t to dn-1 do
	  h.(j+offset) <- h.(j+offset) + p
	done;
    done;
    print_string (Printf.sprintf "%d\n" (maxf 0 999 (fun i-> h.(i))))


let change c =
  char_of_int (int_of_char 'a' + (1 - ((int_of_char c) - (int_of_char 'a'))))

let solve s =
  let rec count s i res =
    if i < String.length s then
      if (s.[i] != s.[i-1])
      then count s (i+1) (res+1)
      else count s (i+1) res
    else res
  in let res = count s 1 0 in
  if (res mod 2) == 1
       then (String.make 1 (change s.[0])) ^ (String.sub s 1 ((String.length s) - 1))
       else s

let () =
  let tc = Scanf.scanf " %d" (fun x -> x) in
  let rec loop tc =
    match tc with
    |0 -> ()
    |_ ->  Printf.printf "%s\n" (solve (Scanf.scanf " %s" (fun x ->x))); loop (tc - 1);
  in loop tc

let solve s =
  (String.make 1 s.[(String.length s - 1)]) ^ String.sub s 1 (String.length s - 1)

let () =
  let tc = Scanf.scanf " %d" (fun x -> x) in
  (let rec loop tc =
    match tc with
    | 0 -> ()
    | _ ->  Printf.printf "%s\n" (solve (Scanf.scanf " %s" (fun x ->x))); loop (tc - 1);
  in loop tc)


open Printf
open Scanf
open String
open Array
open List
open Int64

let a = make 8 0;;

let rec read n =
	if n=0 then ()
	else (let x = scanf " %d" (fun y->y)
		in a.(x) <- a.(x)+1; read (n-1));;

let rec print s n =
	if n=0 then ()
	else (printf s; print s (n-1));;

let ok m =
	let n136 = a.(3) in
	let n124 = a.(4) in
	let n126 = m - n136 - n124 in
	if n126 < 0 then false
	else if a.(1) <> m then false
	else if a.(2) <> n124 + n126 then false
	else if a.(6) <> n126 + n136 then false
	else
		(print "1 2 4\n" n124;
		print "1 2 6\n" n126;
		print "1 3 6\n" n136;
		true);;

let solve n =
	read n;
	if not (ok (n/3)) then printf "-1"
	else ()
in scanf "%d" solve;;

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

let explode s =
  let rec exp i l =
    if i < 0 then l else exp (i - 1) (s.[i] :: l) in
  exp (String.length s - 1) []

 let implode l =
  let result = String.create (List.length l) in
  let rec imp i = function
  | [] -> result
  | c :: l -> result.[i] <- c; imp (i + 1) l in
  imp 0 l;;

let (--) i j =
  let rec aux n acc =
    if n < i then acc else aux (n-1) (n :: acc)
  in aux j []

  let () =
    let n = read_int () in
    let arr = Array.init n read_int in
    let count = Array.init 8 (fun _ -> 0) in
    let ok = Array.fold_right (fun x ok ->
      if ok = false || (x = 5 || x = 7) then false
      else begin
        count.(x) <- count.(x) + 1;
        true
      end
    ) arr true
    in
    let bad = ref (ok = false) in
    bad := !bad || (count.(1) <> n / 3);
    bad := !bad || (count.(4) + count.(6) <> n / 3);
    bad := !bad || (count.(2) + count.(3) <> n / 3);
    bad := !bad || (count.(3) > count.(6));
    bad := !bad || (count.(2) <> count.(4) + count.(6) - count.(3));
    if !bad then Printf.printf "-1\n"
    else begin
      let limit = count.(3) in
      for i = 1 to limit do
        Printf.printf "1 3 6\n";
        count.(6) <- count.(6) - 1;
      done;
      for i = 1 to count.(4) do
        Printf.printf "1 2 4\n";
      done;
      for i = 1 to count.(6) do
        Printf.printf "1 2 6\n";
      done;
    end

open String
open List
open Printf
open Scanf

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

let explode s =
  let rec exp i l =
    if i < 0 then l else exp (i - 1) (s.[i] :: l) in
  exp (String.length s - 1) []

 let implode l =
  let result = String.create (List.length l) in
  let rec imp i = function
  | [] -> result
  | c :: l -> result.[i] <- c; imp (i + 1) l in
  imp 0 l;;

let (--) i j =
  let rec aux n acc =
    if n < i then acc else aux (n-1) (n :: acc)
  in aux j []

  let () =
    let n = read_int () in
    let arr = Array.init n read_int in
    let count = Array.init 8 (fun _ -> 0) in
    let ok = Array.fold_right (fun x ok ->
      if ok = false || (x = 5 || x = 7) then false
      else begin
        count.(x) <- count.(x) + 1;
        true
      end
    ) arr true
    in
    let bad = ref (ok = false) in
    bad := !bad || (count.(1) <> n / 3);
    bad := !bad || (count.(4) + count.(6) <> n / 3);
    bad := !bad || (count.(2) + count.(3) <> n / 3);
    bad := !bad || (count.(2) <> count.(4) + count.(6) - count.(3));
    if !bad then Printf.printf "-1\n"
    else begin
      let limit = count.(3) in
      for i = 1 to limit do
        Printf.printf "1 3 6\n";
        count.(6) <- count.(6) - 1;
      done;
      for i = 1 to count.(4) do
        Printf.printf "1 2 4\n";
      done;
      for i = 1 to count.(6) do
        Printf.printf "1 2 6\n";
      done;
    end


let get_tbl n =
  let tbl = Hashtbl.create 100000 in
  let rec loop count =
    if count > n then tbl
    else
      begin
	Hashtbl.add tbl (Scanf.scanf " %d" (fun x -> x)) (Int64.of_int(count));
	loop (count+1)
      end
  in
  loop 1

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

let solve tbl len queries =
  let accm_func ans q=
    let nth = Hashtbl.find tbl q in
    ((Int64.add (fst ans) nth),
      (Int64.sub (Int64.add (snd ans) (Int64.of_int(len+1))) nth))
  in
  List.fold_left accm_func (Int64.zero, Int64.zero) queries

let _ =
  let n = Scanf.scanf " %d" (fun x -> x) in
  let tbl = get_tbl n in
  let m = Scanf.scanf " %d" (fun x -> x) in
  let queries = get_int_list m in
  let ans = solve tbl n queries in
  Printf.printf "%Ld %Ld\n" (fst ans) (snd ans)

let read format f = Scanf.bscanf Scanf.Scanning.stdib format f

let () =
  let n = read "%d\n" (fun x -> x) in
  let nowhere = -1 in
  let lefts = Array.make n nowhere in
  let rights = Array.make n nowhere in
  for i = 1 to n do
    let a = read "%d " (fun x -> x - 1) in
    if lefts.(a) = nowhere then lefts.(a) <- i;
    rights.(a) <- n - i + 1
  done;
  let left = ref (Int64.of_int 0) in
  let right = ref (Int64.of_int 0) in
  let m = read "%d\n" (fun x -> x) in
  for i = 1 to m do
    let q = read " %d" (fun x -> x - 1) in
    left := Int64.add !left (Int64.of_int lefts.(q));
    right := Int64.add !right (Int64.of_int rights.(q))
  done;
  Printf.printf "%Ld %Ld\n" !left !right

let read format f = Scanf.bscanf Scanf.Scanning.stdib format f

let () =
  let n = read "%d\n" (fun x -> x) in
  let nowhere = -1 in
  let lefts = Array.make n nowhere in
  let rights = Array.make n nowhere in
  for i = 1 to n do
    let a = read "%d " (fun x -> x - 1) in
    if lefts.(a) = nowhere then lefts.(a) <- i;
    rights.(a) <- n - i + 1
  done;
  let left = ref 0 in
  let right = ref 0 in
  let m = read "%d\n" (fun x -> x) in
  for i = 1 to m do
    let q = read " %d" (fun x -> x - 1) in
    left := !left + lefts.(q);
    right := !right + rights.(q)
  done;
  Printf.printf "%d %d\n" !left !right

let get_tbl n =
  let tbl = Hashtbl.create 100000 in
  let rec loop count =
    if count > n then tbl
    else
      begin
	Hashtbl.add tbl (Scanf.scanf " %d" (fun x -> x)) (Int32.of_int(count));
	loop (count+1)
      end
  in
  loop 1

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

let solve tbl len queries =
  let accm_func ans q=
    let nth = Hashtbl.find tbl q in
    ((Int32.add (fst ans) nth),
      (Int32.sub (Int32.add (snd ans) (Int32.of_int(len+1))) nth))
  in
  List.fold_left accm_func (Int32.zero, Int32.zero) queries

let _ =
  let n = Scanf.scanf " %d" (fun x -> x) in
  let tbl = get_tbl n in
  let m = Scanf.scanf " %d" (fun x -> x) in
  let queries = get_int_list m in
  let ans = solve tbl n queries in
  Printf.printf "%ld %ld\n" (fst ans) (snd ans)

let get_tbl n =
  let tbl = Hashtbl.create 100000 in
  let rec loop count =
    if count > n then tbl
    else
      begin
	Hashtbl.add tbl (Scanf.scanf " %d" (fun x -> x)) count;
	loop (count+1)
      end
  in
  loop 1

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

let solve tbl len queries =
  let accm_func ans q=
    let nth = Hashtbl.find tbl q in
    ((fst ans)+nth, (snd ans)+len-nth+1)
  in
  List.fold_left accm_func (0, 0) queries

let _ =
  let n = Scanf.scanf " %d" (fun x -> x) in
  let tbl = get_tbl n in
  let m = Scanf.scanf " %d" (fun x -> x) in
  let queries = get_int_list m in
  let ans = solve tbl n queries in
  Printf.printf "%d %d\n" (fst ans) (snd ans)


(*    Added sentenials to the left and right end of the list.
      Their (l,r,c) would be (-1,0,0) on the left and
      (n,n+1,0) on the right.

      The histogram table keeps characters 0...k.  In other
      words an additional character 0 in order to simplify
      the cases in the code.
*)

module Pset = Set.Make (struct type t = int*int*int let compare = compare end)
(*                                    (l,r+1,character)                     *)

(* a string s is represented as a set of these triples.  For example if
   the string is "abbbbaacce" then it is:
                  0123456789
   (-1,0,k) (0,1,a) (1,5,b) (5,7,a) (7,9,c) (9,10,e) (10,11,k)

   The histogram is a (k+1) by (k+1) matrix of counts.  It's the
   number of times each such transition occurs in the 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 l2n c = (int_of_char c) - (int_of_char 'a')

let split2 set e put_tie_left =
  let (a,x,b) = Pset.split e set in
  if not x then (a,b) else
    if put_tie_left then (Pset.add e a, b) else (a, Pset.add e b)

let pred set e =
  (* e is in the set.  Return it's predecessor.  It must be there *)
  let (l,_,_) = Pset.split e set in
  Pset.max_elt l

let succ set e =
  let (_,_,r) = Pset.split e set in
  Pset.min_elt r

let inc hist i j d =
  hist.(i).(j) <- hist.(i).(j) + d

let update str n l r c hist =
  (* Update the str (of length n) with a new block of chars c from l
     to r-1.  (Note this is different from the problem statement.)
     Return str, the updated version of the string.  the histogram is
     updated in place.  *)

  let first = (* the first one we're going to replace *)
    let (partl, _, _) = Pset.split (l,n+2,0) str in
    Pset.max_elt partl
  in

  let last =  (* the last one we're going to replace *)
    let (partl, _, _) = Pset.split (r,-2,0) str in
    Pset.max_elt partl
  in

  let (left_part,_,_) = Pset.split first str in
  let (_,_,right_part) = Pset.split last str in

  let list_of_range str a b =
    let (_,partr) = split2 str a false in
    let (partl,_) = split2 partr b true in
    List.rev (Pset.fold (fun e ac -> e::ac) partl [])
  in

  let before_first = pred str first in
  let after_last = succ str last in
  let rem_t = list_of_range str before_first after_last in

  let rec rem_scan (l0,r0,c0) li =
    match li with
      | [] -> failwith "scan called with empty list"
      | (l,r,c)::[] -> inc hist c0 c (-1)
      | (l,r,c)::tail ->
	inc hist c0 c (-1);
	inc hist c c (-(r-l-1));
	rem_scan (l,r,c) tail
  in

  rem_scan (List.hd rem_t) (List.tl rem_t);
  (* removes all the obsolete transitions *)

  let (l0,r0,c0) = first in
  let (l1,r1,c1) = last in

  let lphantom = if l0 < l then [(l0,l,c0)] else [] in
  let rphantom = if r1 > r then [(r,r1,c1)] else [] in
  let new_middle = lphantom @ [(l,r,c)] @ rphantom in

  let rec add_scan (l0,r0,c0) li =
    match li with
      | [] -> failwith "scan called with empty list"
      | (l,r,c)::[] -> inc hist c0 c 1
      | (l,r,c)::tail ->
	inc hist c0 c 1;
	inc hist c c (r-l-1);
	add_scan (l,r,c) tail
  in

  add_scan before_first (new_middle @ [after_last]);

  let newstr = Pset.union left_part right_part in
  List.fold_left (fun ac e -> Pset.add e ac) newstr new_middle

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

  let rec find_changes s n i ac = if i=n then List.rev (n::ac) else
      if s.[i-1] <> s.[i] then find_changes s n (i+1) (i::ac)
      else find_changes s n (i+1) ac
  in

  let changes = Array.of_list (find_changes s n 1 []) in
(*
  printf "changes = ";
  for i=0 to -1+ Array.length changes do
    printf "%d " changes.(i)
  done;
  print_newline();
*)
  let str = fold 0 (-2 + Array.length changes) (
    fun i ac -> Pset.add (changes.(i), changes.(i+1), l2n s.[changes.(i)]) ac
  ) (Pset.singleton (0, changes.(0), l2n s.[0])) in

  let str = Pset.add (-1,0,k) str in
  let str = Pset.add (n,n+1,k) str in

  let hist = Array.make_matrix (k+1) (k+1) 0 in
  for i=0 to n-2 do
    inc hist (l2n s.[i]) (l2n s.[i+1]) 1
  done;
  inc hist k (l2n s.[0]) 1;
  inc hist (l2n s.[n-1]) k 1;

  let rec loop i str = if i=m then () else (
      match read_int() with
	| 1 ->
	  let l = -1 + read_int () in
	  let r = read_int () in
	  let c = l2n (read_string()).[0] in
	  let str = update str n l r c hist in
	  loop (i+1) str
	| 2 ->
	  let p = read_string() in
	  let p = Array.init k (fun i -> l2n p.[i]) in
	  let inv = Array.make k 0 in
	  for i=0 to k-1 do
	    inv.(p.(i)) <- i
	  done;
	  let cycles = sum 0 (k-1) (fun i -> sum 0 (k-1) (fun j ->
	      if inv.(i) < inv.(j) then 0 else hist.(i).(j)
	  )) in
	  printf "%d\n" (cycles+1);
	  loop (i+1) str
	| _ -> failwith "bad input"
  ) in

  loop 0 str

module Pset = Set.Make (struct type t = int*int*int let compare = compare end)

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 l2n c = (int_of_char c) - (int_of_char 'a')

let split2 set e put_tie_left =
  let (a,x,b) = Pset.split e set in
  if not x then (a,b) else
    if put_tie_left then (Pset.add e a, b) else (a, Pset.add e b)

let pred set e =
  let (l,_,_) = Pset.split e set in
  Pset.max_elt l

let succ set e =
  let (_,_,r) = Pset.split e set in
  Pset.min_elt r

let inc h i j d =
    h.(i).(j) <- h.(i).(j) + d

let update str n l r c hist =
  let first = (* the first one we're going to replace *)
    let (partl, _, _) = Pset.split (l,n+2,0) str in
    Pset.max_elt partl
  in

  let last =  (* the last one we're going to replace *)
    let (partl, _, _) = Pset.split (r,-2,0) str in
    Pset.max_elt partl
  in

  let (left_part,_,_) = Pset.split first str in
  let (_,_,right_part) = Pset.split last str in

  let list_of_range str a b =
    let (_,partr) = split2 str a false in
    let (partl,_) = split2 partr b true in
    List.rev (Pset.fold (fun e ac -> e::ac) partl [])
  in

  let before_first = pred str first in
  let after_last = succ str last in
  let rem_t = list_of_range str before_first after_last in

  let rec rem_scan (l0,r0,c0) li =
    match li with
      | [] -> failwith "scan called with empty list"
      | (l,r,c)::[] -> inc hist c0 c (-1)
      | (l,r,c)::tail ->
	inc hist c0 c (-1);
	inc hist c c (-(r-l-1));
	rem_scan (l,r,c) tail
  in

  rem_scan (List.hd rem_t) (List.tl rem_t);

  let (l0,r0,c0) = first in
  let (l1,r1,c1) = last in

  let lphantom = if l0 < l then [(l0,l,c0)] else [] in
  let rphantom = if r1 > r then [(r,r1,c1)] else [] in
  let new_middle = lphantom @ [(l,r,c)] @ rphantom in

  let rec add_scan (l0,r0,c0) li =
    match li with
      | [] -> failwith "scan called with empty list"
      | (l,r,c)::[] -> inc hist c0 c 1
      | (l,r,c)::tail ->
	inc hist c0 c 1;
	inc hist c c (r-l-1);
	add_scan (l,r,c) tail
  in

  add_scan before_first (new_middle @ [after_last]);

  let newstr = Pset.union left_part right_part in
  List.fold_left (fun ac e -> Pset.add e ac) newstr new_middle

open Printf
open Scanf

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_conv_str () =
  let s = bscanf Scanning.stdib " %s " (fun x -> x) in
  Array.init (String.length s) (fun i -> l2n s.[i])

let () =
  let n = read_int () in
  let m = read_int () in
  let k = read_int () in
  let s = read_conv_str () in

  let str = Pset.singleton (-1,0,k) in
  let str = Pset.add (n,n+1,k) str in

  let str = fold 0 (n-1) (
    fun i ac -> Pset.add (i, i+1, s.(i)) ac
  ) str in

  let hist = Array.make_matrix (k+1) (k+1) 0 in
  for i=0 to n-2 do
    inc hist s.(i) s.(i+1) 1
  done;

  ignore (fold 0 (m-1) (fun _ str ->
    match read_int() with
      | 1 ->
	let l = -1 + read_int () in
	let r = read_int () in
	let c = (read_conv_str()).(0) in
	update str n l r c hist
      | 2 ->
	let p = read_conv_str() in
	let cycles = sum 0 (k-1) (fun i -> sum 0 i (fun j -> hist.(p.(i)).(p.(j)))) in
	printf "%d\n" (cycles+1);
	str
      | _ -> failwith "bad input"
  ) str)


let rec watch curr skip watched lst =
  let is_skippable curr skip range =
    match (curr, skip, range) with
      c, s, [l; r;] when c + s <= l -> true
    | _ -> false
  in
  match lst with
    [] -> watched
  | _ ->
     let next = List.hd lst in
     let right = List.nth next 1
     in
     if is_skippable curr skip next then
           watch (curr + skip) skip watched lst
         else
           let minutes = right - curr + 1
           in
           watch (right + 1) skip (watched + minutes) (List.tl lst)
;;

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

let rec make_ranges lines =
  let rec inner lines rv =
    let range s = List.map int_of_string (words s) in
    match lines with
      [] -> rv
    | (l::ls) -> inner ls (List.append rv [range l])
  in
  inner lines []
;;

let rec read_lines n =
  let rec inner m rv =
    match m with
      0 -> rv
    | k -> inner (k - 1) (List.append rv [read_line ()])
  in
  inner n []
;;

let () =
  let nk_line = read_line () in
  let [nline; skip] = List.map int_of_string (words nk_line) in
  let scenes = make_ranges (read_lines nline)  in
  Printf.printf "%d\n" (watch 1 skip 0 scenes)
;;

let () = Scanf.scanf "%d %d\n" (fun n x ->
  let rec aux a t i =
    if i = n then a
    else begin
      let l, r = Scanf.scanf "%d %d\n" (fun l r -> (l, r)) in
      let b = (l - t) mod x in
      let a = a + b + r - l + 1 in
      aux a (succ r) (succ i)
    end
  in
  Printf.printf "%d\n" (aux 0 1 0)
)

module A = Array;;
module C = Char;;
module L = List;;
module S = String;;

let pf = Printf.printf;;
let sf = Scanf.scanf;;
let ssf = Scanf.sscanf;;

let (|>) x f = f x;;
let (@@) f x = f x;;
let id x = x;;
let id2 x y = (x, y);;
let id3 x y z = (x, y, z);;

exception Error of string

let inf = 1000000000;;
let eps = 1e-11;;

let _ =
  let (n, x) = sf "%d %d\n" id2 in
  let now = ref 0 in
  let res = ref 0 in
  for i = 1 to n do
    let (l, r) = sf "%d %d\n" id2 in
    res := !res + (r - !now) - (l - !now - 1) / x * x;
    now := r
  done;
  pf "%d\n" !res
;;


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

let () =
  let a = read_string() in
  let b = read_string() in
  let n = String.length a in
  let rec count i a47 a74 = if i=n then (a47,a74) else
    match (a.[i],b.[i]) with
      | ('4','7') -> count (i+1) (a47+1) a74
      | ('7','4') -> count (i+1) a47 (a74+1)
      | _ -> count (i+1) a47 a74
  in
  let (c47,c74) = count 0 0 0 in
    Printf.printf "%d\n" (max c47 c74)


let read_int () = Scanf.scanf " %d" (fun x -> x)
let read_point () = Scanf.scanf " %d %d" (fun x y -> float x, float y)

let n = read_int ()
let p = read_point ()
let first = read_point ()
let prev = ref (0., 0.)
let () = prev := first
let minimum = ref infinity
let maximum = ref 0.
let pi = 3.14159265

let on_segment (x1, y1) (x, y) (x2, y2) =
  (x -. x1) *. (x -. x2) <= 0. &&  (y -. y1) *. (y -. y2) <= 0.

let min_and_max p1 p2 =
  let d (x, y) = sqrt ((fst p -. x)**2. +. (snd p -. y)**2.) in
  let rzut_p (x1, y1) (x2, y2) =
    let skal (x1, y1) (x2, y2) (x3, y3) =
      (x2 -. x1) *. (x3 -. x1) +. (y2 -. y1) *. (y3 -. y1)
    in
    let s = skal (x1, y1) p (x2, y2) /. skal (x1, y1) (x2, y2) (x2, y2) in
    ((x1 +. (x2 -. x1) *. s), (y1 +. (y2 -. y1) *. s))
  in
  let rzut = rzut_p p1 p2 in
  let min = if on_segment p1 rzut p2 then d rzut else min (d p1) (d p2) in
  (min, max (d p1) (d p2) )

let print_point (x, y) =
  print_float x;
  print_string " ";
  print_float y;
  print_newline()

let add cur =
  let mini, maxi = min_and_max !prev cur in
  (if mini < !minimum then minimum := mini else ());
  (if maxi > !maximum then maximum := maxi else ());
  prev := cur
;;

let () =
  for i = 1 to n - 1 do
    let cur = read_point () in
    add cur
  done;
  add first

let () =
  (pi *. !maximum ** 2. -. pi *. !minimum ** 2.) |> print_float;
  print_newline ()

open Printf
open Scanf

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

let long x = Int64.of_int x

let cross (x1,y1) (x2,y2) = (x1*.y2) -. (y1*.x2)
let ( -- ) (x1,y1) (x2,y2) = (x1-.x2, y1-.y2)
let len (x,y) = sqrt(x*.x +. y*.y)
let dot (x1,y1) (x2,y2) = x1*.x2 +. y1*.y2
let mul c (x,y) = (c*.x, c*.y)
let sq x = x *. x

let dist p q r = (* the distance from point r to the line from point p to point q *)
  let a = q -- p in
  let b = r -- p in
  let an = mul (1.0 /. (len a)) a in
  let v = mul (dot an b) an in
  if len v <= len a && (dot v a) >= 0.0 then len (b -- v) else 1e15

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 pi = 2.0 *. asin 1.0

let dist2 (a,b) (c,d) =
  let x = a-.c in
  let y = b-.d in
  ((x*.x) +. (y*.y))

let () =
  let n = read_int () in
  let p = read_pair () in
  let poly = Array.init n (fun _ -> read_pair()) in

  let inner = minf 0 (n-1) (fun i->
    min (dist2 p poly.(i)) (sq (dist poly.(i) poly.((i+1) mod n) p))
  ) in
  let outer = maxf 0 (n-1) (fun i-> dist2 p poly.(i)) in

  printf "%.8f\n" (pi *. (outer -. inner))

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 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 pi = 2.0 *. asin 1.0

let dist (a,b) (c,d) =
  let x = float (a-c) in
  let y = float (b-d) in
  sqrt ((x*.x) +. (y*.y))

let () =
  let n = read_int () in
  let p = read_pair () in
  let poly = Array.init n (fun _ -> read_pair()) in

  let inner = minf 0 (n-1) (fun i-> dist p poly.(i)) in
  let outer = maxf 0 (n-1) (fun i-> dist p poly.(i)) in

  printf "%.8f\n" (pi *. (outer *. outer -. inner *. inner))

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 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 pi = 2.0 *. asin 1.0

let dist (a,b) (c,d) =
  let x = float (a-c) in
  let y = float (b-d) in
  sqrt ((x*.x) +. (y*.y))

let () =
  let n = read_int () in
  let p = read_pair () in
  let poly = Array.init n (fun _ -> read_pair()) in

  let inner = minf 0 (n-1) (fun i-> dist p poly.(i)) in
  let outer = maxf 0 (n-1) (fun i-> dist p poly.(i)) in

  printf "%.8f\n" (pi *. (outer -. inner) *. (outer +. inner))

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 ( ** ) a b = Int64.mul a b
let ( ++ ) a b = Int64.add a b
let ( -- ) a b = Int64.sub a b
let ( // ) a b = Int64.div a b
let ( %% ) a b = Int64.rem a b

let long x = Int64.of_int x

let 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 pi = 2.0 *. asin 1.0

let dist2 (a,b) (c,d) =
  let x = long (a-c) in
  let y = long (b-d) in
  ((x**x) ++ (y**y))

let () =
  let n = read_int () in
  let p = read_pair () in
  let poly = Array.init n (fun _ -> read_pair()) in

  let inner = minf 0 (n-1) (fun i-> dist2 p poly.(i)) in
  let outer = maxf 0 (n-1) (fun i-> dist2 p poly.(i)) in

  printf "%.8f\n" (pi *. Int64.to_float (outer -- inner))


let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n 0L in
  let () =
    for i = 0 to n - 1 do
      let k = Scanf.bscanf sb "%d " (fun s -> s) in
	a.(i) <- Int64.of_int k
    done
  in
  let res = ref 0L in
    for i = 0 to n - 2 do
      let p = ref 1 in
	while !p * 2 + i < n do
	  p := !p * 2;
	done;
	res := Int64.add !res a.(i);
	a.(i + !p) <- Int64.add a.(i + !p) a.(i);
	a.(i) <- 0L;
	Printf.printf "%Ld\n" !res;
    done


let split_on_char sep s =
  let r = ref [] in
  let j = ref (String.length s) in
  for i = String.length s - 1 downto 0 do
    if String.unsafe_get s i = sep then begin
      r := String.sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  String.sub s 0 !j :: !r
;;

let n,l,a = Scanf.sscanf (read_line ()) "%i %i %i" (fun n l a -> n,l,a) in
let num = ref 0 in
let time = ref 0 in
for i = 0 to n-1 do
  let ti, li = Scanf.sscanf (read_line()) "%i %i" (fun ti li -> ti,li) in
  num := !num + ((ti - !time)/a);
  time := ti + li;
done;
print_int (!num + ((l - !time)/a))

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

(* module Imos = struct
	let make n = Array.make (i32 n +$ 2) 0L
	let add_destructive_closed l r a =
		try
		a.(l+$1) <- a.(l+$1) + 1L;
		a.(r+$2) <- a.(r+$2) - 1L
		with Invalid_argument _ -> ()
	let add_destructive_1orig_closed l r a =
		a.(l) <- a.(l) + 1L;
		a.(r+$1) <- a.(r+$1) - 1L
	let cumulate_destructive a = for i=1 to Array.length a -$ 2 do
		a.(i) <- a.(i) + a.(i-$1)
	done
end *)

let () =
	let n,l,a = get_3_i64 9 in
	let b = Array.init (i32 n) (fun _ -> get_2_i64 0)
	in
	let p = ref 0L in
	let c = ref 0L in
	Array.iter (fun (t,l) ->
		(* printf "%Ld %Ld %Ld %Ld\n" !p t l !c; *)
		let d = t - !p in c += (d / a);
		p := t + l
	) b;
	printf "%Ld\n" (!c + (l- !p)/a)


let () =
  let (@@) f x = f x in
  let (%) f g x = f @@ g x in
  let (|>) x f = f x in
  let open List in
  let open Num in
  let split = Str.split @@ Str.regexp_string " " in
  read_line () |> split |> map int_of_string |> fun n_k ->
  let n = hd n_k and k = hd @@ tl n_k in
  read_line () |> split |> map int_of_string |> mapi (fun i a -> i + 1, a) |> fun ratings ->
  let rec loop rev_result acc i n = function
    | (_, ai) as r :: rs when acc -/ (Int ai) */ (Int n -/ Int i) */ (Int i -/ Int 1) </ Int k ->
      loop (r :: rev_result) acc i (n - 1) rs
    | (_, ai) :: rs -> loop rev_result (acc +/ (Int ai) */ (Int i -/ Int 1)) (i + 1) n rs
    | [] -> rev rev_result
  in
  loop [] (Int 0) 1 n ratings |> map (string_of_int % fst) |> iter print_endline

let () =
  let (@@) f x = f x in
  let (%) f g x = f @@ g x in
  let (|>) x f = f x in
  let open List in
  let open Num in
  let open Scanf in
  scanf "%d %d\n" @@ fun n k ->
  Array.init n (fun i -> scanf "%d " @@ fun a -> i + 1, a) |> Array.to_list |> fun ratings ->
  let rec loop rev_result acc i n = function
    | (_, ai) as r :: rs when acc -/ (Int ai) */ (Int n -/ Int i) */ (Int i -/ Int 1) </ Int k ->
      loop (r :: rev_result) acc i (n - 1) rs
    | (_, ai) :: rs -> loop rev_result (acc +/ (Int ai) */ (Int i -/ Int 1)) (i + 1) n rs
    | [] -> rev rev_result
  in
  loop [] (Int 0) 1 n ratings |> map (string_of_int % fst) |> iter print_endline

let () =
  let (@@) f x = f x in
  let (%) f g x = f @@ g x in
  let (|>) x f = f x in
  let (|*>) (a, b) f = f a b in
  let open List in
  let split = Str.split @@ Str.regexp_string " " in
  read_line () |> split |> map int_of_string |> fun n_k ->
  let n = hd n_k and k = hd @@ tl n_k in
  read_line () |> split |> map int_of_string |> mapi (fun i a -> i + 1, a) |> fun ratings ->
  let d ratings (i, ai) =
     (0, ratings) |*> fold_left (fun s (j, aj) -> s + if j < i then aj * (j - 1) - (n - i) * ai else 0)
  in
  let extract_next ratings =
    let rec loop rev_head = function
      | r :: rs when d ratings r < k -> (r, rev_append rev_head rs)
      | r :: rs -> loop (r :: rev_head) rs
      | [] -> raise Not_found
    in
    loop [] ratings
  in
  let rec loop rev_result rs =
    try let r, rs = extract_next rs in loop (r :: rev_result) rs
    with Not_found -> rev rev_result
  in
  loop [] ratings |> map (string_of_int % fst) |> iter print_endline

let () =
  let (@@) f x = f x in
  let (%) f g x = f @@ g x in
  let (|>) x f = f x in
  let (|*>) (a, b) f = f a b in
  let open List in
  let open Int64 in
  let split = Str.split @@ Str.regexp_string " " in
  read_line () |> split |> map int_of_string |> fun n_k ->
  let n = hd n_k and k = hd @@ tl n_k in
  read_line () |> split |> map int_of_string |> mapi (fun i a -> i + 1, a) |> fun ratings ->
  let d ratings (i, ai) =
     (0L, ratings) |*> fold_left (fun s (j, aj) ->
       add s @@
         if j < i then sub (mul (of_int aj) (sub (of_int j) 1L)) @@ mul (sub (of_int n) @@ of_int i) (of_int ai)
         else 0L)
  in
  let extract_next ratings =
    let rec loop rev_head = function
      | r :: rs when d ratings r < (of_int k) -> (r, rev_append rev_head rs)
      | r :: rs -> loop (r :: rev_head) rs
      | [] -> raise Not_found
    in
    loop [] ratings
  in
  let rec loop rev_result rs =
    try let r, rs = extract_next rs in loop (r :: rev_result) rs
    with Not_found -> rev rev_result
  in
  loop [] ratings |> map (string_of_int % fst) |> iter print_endline


let read_int()=Scanf.scanf "%d " (fun x->x);;
let n=read_int();;
let v=read_int();;
let tab=Array.make (n+1) 0;;
let vec=Array.make (n+1) 0;;
let mat=Array.make 3001 0;;
for i=1 to n do
   let x=read_int() and y=read_int() in (tab.(i)<-x;mat.(x)<-mat.(x)+y)
done;;
let r=ref 0;;
let nb=ref 0;;
for i=1 to 3000 do
   nb:= !nb+min (!r) v;
   let vv=v-min (!r) v in
   nb:= !nb+min vv mat.(i);
   r:= mat.(i)-min vv mat.(i)
done;;
print_int (!nb+min !r v);;

open Printf
open Scanf

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

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

let nn = 3001
let () =
  let n = read_int () in
  let v = read_int () in
  let fruit = Array.make (nn+1) 0 in
  for i=0 to n-1 do
    let a = read_int() in
    let b = read_int() in
    fruit.(a) <- fruit.(a) + b
  done;

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

  let count = ref 0L in

  for t=1 to nn do
    let prev = fruit.(t-1) in
    let take_prev = min v prev in
    count := !count ++ (Int64.of_int take_prev);
    fruit.(t-1) <- fruit.(t-1) - take_prev;
    let v = v - take_prev in
    let current = fruit.(t) in
    let take_current = min v current in
    count := !count ++ (Int64.of_int take_current);
    fruit.(t) <- fruit.(t) - take_current;
  done;

  printf "%Ld\n" !count


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

exception Found of int

let _ =
  let n = 100000 in
  let prime = Array.make (n + 1) true in
  let d = Array.make (n + 1) 0 in
  let p = Array.make 10000 0 in
  let pk = ref 0 in
  let _ =
    for i = 2 to n do
      if prime.(i) then (
	p.(!pk) <- i;
	d.(i) <- !pk;
	incr pk;
	let j = ref (2 * i) in
          while !j <= n do
	    let jj = !j in
              prime.(jj) <- false;
	      d.(jj) <- !pk - 1;
              j := jj + i
          done
      )
    done;
  in
  let pk = !pk - 1 in
  let n = getnum () in
  let m = getnum () in
  let a = Array.make (n + 1) false in
  let h = Array.init 10000 (fun _ -> Hashtbl.create 1) in
    for i = 1 to m do
      let q = getnum () in
      let k = getnum () in
	match q with
	  | -5 -> (* + *)
	      if a.(k)
	      then Printf.printf "Already on\n"
	      else (
		try
		  (let r = ref k in
		     while !r > 1 do
		       let pn = d.(!r) in
		       let p = p.(pn) in
			 while !r mod p = 0 do
			   r := !r / p
			 done;
			 let ht = h.(pn) in
			   if Hashtbl.length ht <> 0 then (
			     Hashtbl.iter (fun c () -> raise (Found c)) ht
			   )
		     done);
		  a.(k) <- true;
		  (let r = ref k in
		     while !r > 1 do
		       let pn = d.(!r) in
		       let p = p.(pn) in
			 while !r mod p = 0 do
			   r := !r / p
			 done;
			 let ht = h.(pn) in
			   Hashtbl.replace ht k ()
		     done);
		    Printf.printf "Success\n"
		with
		  | Found c ->
		      Printf.printf "Conflict with %d\n" c
	      )
	  | 0 -> (* - *)
	      if not a.(k)
	      then Printf.printf "Already off\n"
	      else (
		a.(k) <- false;
		(let r = ref k in
		   while !r > 1 do
		     let pn = d.(!r) in
		     let p = p.(pn) in
		       while !r mod p = 0 do
			 r := !r / p
		       done;
		       let ht = h.(pn) in
			 Hashtbl.remove ht k
		   done);
		Printf.printf "Success\n"
	      )
	  | _ -> assert false
    done;

let main () =
    let n, m = Scanf.sscanf (read_line ()) "%d %d" (fun n m -> n, m) in
    let arr = Array.create (n + 1) [] in
    for i = 2 to n do
        if arr.(i) = [] then (
            let rec loop j =
                if j <= n then (
                    arr.(j) <- i :: arr.(j);
                    loop (j + i)
                )
            in
            loop i
        )
    done;

    let col  = Array.create (n + 1) false in
    let prim = Array.create (n + 1) 0 in

    let rec loop i =
        if i > 0 then (
            let (c, r) = Scanf.sscanf (read_line ()) "%c %d" (fun c r -> c, r) in
            if c = '+' then (
                if col.(r) then
                    print_endline "Already on"
                else
                    let k = List.fold_left (fun acc v -> if prim.(v) > 0 then prim.(v) else acc) 0 arr.(r) in
                    if k > 0 then Printf.printf "Conflict with %d\n" k else (
                        print_endline "Success";
                        col.(r) <- true;
                        List.iter (fun v -> prim.(v) <- r) arr.(r)
                    )
            ) else (
                if not col.(r) then print_endline "Already off" else
                let _ = print_endline "Success" in
                let _ = col.(r) <- false in
                List.iter (fun v -> prim.(v) <- 0) arr.(r)
            );
            loop (i - 1)
        )
    in
    loop m
;;
main()

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

let main () =
    let n, m = Scanf.sscanf (read_line ()) "%d %d" (fun n m -> n, m) in
    let col = Array.create (n + 1) false in
    let check col p =
        let rec loop i =
            if i * i > n then None else
            if col.(i) && gcd p i > 1 then Some i else loop (i + 1)
        in
        loop 2
    in
    for i = 1 to m do
        let (c, r) = Scanf.sscanf (read_line ()) "%c %d" (fun c r -> c, r) in
        if c = '+' then (
            if col.(r) then print_endline "Already on" else
            match check col r with
            | None -> print_endline "Success"; col.(r) <- true
            | Some k -> Printf.printf "Conflict with %d\n" k
        ) else
            if col.(r) = false then print_endline "Already off" else
            let _ = col.(r) <- false in
            print_endline "Success"
    done
;;
main()

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

let main () =
    let n, m = Scanf.sscanf (read_line ()) "%d %d" (fun n m -> n, m) in
    let col = Array.create (n + 1) false in
    let check col p =
        let rec loop i =
            if i * i > n then None else
            if col.(i) && gcd p i > 1 then Some i else loop (i + 1)
        in
        loop 1
    in
    for i = 1 to m do
        let (c, r) = Scanf.sscanf (read_line ()) "%c %d" (fun c r -> c, r) in
        if c = '+' then (
            if col.(r) then print_endline "Already on" else
            match check col r with
            | None -> print_endline "Success"; col.(r) <- true
            | Some k -> Printf.printf "Conflict with %d\n" k
        ) else
            if col.(r) = false then print_endline "Already off" else
            let _ = col.(r) <- false in
            print_endline "Success"
    done
;;
main()


open Printf open Scanf
module Int64 = struct
	include Int64
	(* int *)
	let (+$) = (+)
	let (-$) = (-)
	let (/$) = (/)
	let ( *$) = ( * )
	(* int64 *)
	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 labs p = if p < 0L then -1L*p else p
	(* let (~|) p = Int64.of_int p *)
	let (~|) p = Int64.to_int p

	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 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
end open Int64

let () =
  let n,d = get_2_i64 ()
  in let ar = input_i64_array n
  in
  let res = ref 0L
  in
  for i=0 to ~|n-$1 do
    let lef = ar.(i) - d in
      if (i>=1 && labs (lef - ar.(i-$1)) >= d) || i=0 then (
      (* printf "%Ld\n" lef; *)
      res+=1L;
    );
    let rig = ar.(i) + d in
      if i= ~|n-$1 ||
        (i<= ~|n-$2 && labs (rig - ar.(i+$1)) >= d && ar.(i+$1)-ar.(i)<>2L*d)
        then (
        (* printf "%Ld\n" rig; *)
        res+=1L
      );
  done;
  !res |> print_i64_endline

open Int64

let split_on_char sep s =
  let r = ref [] in
  let j = ref (String.length s) in
  for i = String.length s - 1 downto 0 do
    if String.unsafe_get s i = sep then begin
      r := String.sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  String.sub s 0 !j :: !r
;;

let n,d = Scanf.sscanf (read_line ()) "%s %s" (fun n d -> of_string n,of_string d)  in
let hotel = read_line () |> split_on_char ' ' |> List.map of_string in
snd (List.fold_left (fun (old_x, tmp) x ->
    if sub x old_x > mul 2L d then (x, add tmp 2L)
    else if sub x old_x = mul 2L d then (x, add tmp 1L)
    else (x, tmp)) (List.hd hotel, 2L) (List.tl hotel))
|> to_string
|> print_endline

(* Codeforces 1004 A  Sonya and Hotels *)

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

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

let rec hotel_cnt d cnt l = match l with
	| [] -> cnt
	| h :: [] -> cnt+1
	| h :: nh :: tl ->
		let nl = nh :: tl in
		let mtwo = Int64.of_int 2 in
		let nhh = Int64.sub nh (Int64.mul mtwo d) in
		let icomp = Int64.compare h nhh in
		let ncnt =
			if icomp == 0 then cnt + 1
			else if icomp < 0 then cnt + 2
			else cnt in
		(* let _ = Printf.printf "h,nhh,cnt,ncnt= %Ld %Ld %d %d\n" h nhh cnt ncnt in *)
		hotel_cnt d ncnt nl;;

let rec to_i_64 l = match l with
| [] -> []
| h :: tl ->
	let ih = Int64.of_int h in
	ih :: to_i_64 tl;;

let main() =
	let n = gr () in
	let d = Int64.of_int (gr ()) in
	let a = readlist n [] in
	let ra = List.rev a in
	let iaa = to_i_64 ra in
	let cnt = hotel_cnt d 1 iaa in
	begin
		print_int cnt;
		print_string "\n"
	end;;

main();;

(* Codeforces 1004 A  Sonya and Hotels *)

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

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

let rec hotel_cnt d cnt l = match l with
	| [] -> cnt
	| h :: [] -> cnt+1
	| h :: nh :: tl ->
		let nl = nh :: tl in
		let mtwo = Int64.of_int 2 in
		let nhh = Int64.sub nh (Int64.mul mtwo d) in
		let icomp = Int64.compare h nhh in
		let ncnt =
			if icomp == 0 then cnt + 1
			else if icomp < 0 then cnt + 2
			else cnt in
		let _ = Printf.printf "h,nhh,cnt,ncnt= %Ld %Ld %d %d\n" h nhh cnt ncnt in
		hotel_cnt d ncnt nl;;

let rec to_i_64 l = match l with
| [] -> []
| h :: tl ->
	let ih = Int64.of_int h in
	ih :: to_i_64 tl;;

let main() =
	let n = gr () in
	let d = Int64.of_int (gr ()) in
	let a = readlist n [] in
	let ra = List.rev a in
	let iaa = to_i_64 ra in
	let cnt = hotel_cnt d 1 iaa in
	begin
		print_int cnt;
		print_string "\n"
	end;;

main();;

(* Codeforces 1004 A  Sonya and Hotels *)

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

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

let rec hotel_cnt d cnt l = match l with
	| [] -> cnt
	| h :: [] -> cnt+1
	| h :: nh :: tl ->
		let nl = nh :: tl in
		let ncnt =
			if h == nh - 2 * d then cnt + 1
			else if h < nh - 2 * d then cnt + 2
			else cnt in
		(*let _ = Printf.printf "h,n,cnt,ncnt= %d %d %d %d\n" h nh cnt ncnt in*)
		hotel_cnt d ncnt nl;;

let main() =
	let n = gr () in
	let d = gr () in
	let a = readlist n [] in
	let ra = List.rev a in
	let cnt = hotel_cnt d 1 ra in
	begin
		print_int cnt;
		print_string "\n"
	end;;

main();;

open Printf open Scanf
module Int64 = struct
	include Int64
	(* int *)
	let (+$) = (+)
	let (-$) = (-)
	let (/$) = (/)
	let ( *$) = ( * )
	(* int64 *)
	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 labs p = if p < 0L then -1L*p else p
	(* let (~|) p = Int64.of_int p *)
	let (~|) p = Int64.to_int p

	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 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
end open Int64

let () =
  let n,d = get_2_i64 ()
  in let ar = input_i64_array n
  in
  let res = ref 0L
  in
  for i=0 to ~|n-$1 do
    let lef = ar.(i) - d in if i>=1 && lef>ar.(i-$1) then res+=1L;
    let rig = ar.(i) + d in if i<= ~|n-$2 && rig<ar.(i+$1) && ar.(i+$1)-ar.(i)<>2L*d then res+=1L;
  done;
  !res |> print_i64_endline

let ( */ ) = Big_int.mult_big_int;;
let ( +/ ) = Big_int.add_big_int;;
let ( -/ ) = Big_int.sub_big_int;;
let to_int = Big_int.int_of_big_int;;
let of_int = Big_int.big_int_of_int;;
let min = Big_int.min_big_int;;
let (modi) = Big_int.mod_big_int;;
let to_string = Big_int.string_of_big_int;;
let of_string = Big_int.big_int_of_string;;
let (</) = Big_int.lt_big_int;;

let split_on_char sep s =
  let r = ref [] in
  let j = ref (String.length s) in
  for i = String.length s - 1 downto 0 do
    if String.unsafe_get s i = sep then begin
      r := String.sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  String.sub s 0 !j :: !r
;;

let n,d = Scanf.sscanf (read_line ()) "%i %i" (fun n d -> n,d)  in
let hotel = read_line () |> split_on_char ' ' |> List.map int_of_string in
print_int @@ snd (List.fold_left (fun (old_x, tmp) x ->
    if x - old_x > 2 * d then (x,tmp + 2)
    else if x - old_x = 2 * d then (x, tmp+1)
    else (x, tmp)) (List.hd hotel, 2) (List.tl hotel));
print_newline ();


(* Codeforces 1005 A Tanya and Stairways *)

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

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

let rec to_tania acc last l = match l with
	| [] -> last :: acc
	| h :: t -> match h with
			| 1 ->
				let lacc = if last == 0 then acc else last :: acc in
				to_tania lacc 1 t
			| _ -> to_tania acc h t;;

let main() =
	let n = gr () in
	let ra = readlist n [] in
	let a = List.rev ra in
	let ro = to_tania [] 0 a in
	let o = List.rev ro in
	let lno = List.length o in
	begin
		print_int lno;
		print_string "\n";
		printlisti o;
		print_string "\n"
	end;;

main();;

open Scanf
open Printf

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

let l = Array.to_list (Array.init n (fun i -> scanf " %d" (fun i -> i)))

let rec process_l l out =
  match l with
  | [] -> l, out
  | h::t ->
    if h > out then
      process_l t h
    else
      l, out

let rec process l out out_st =
  match l with
  | [] -> out, out_st
  | _ ->
    let l', st = process_l l 0 in
    process l' (out + 1) (st::out_st)

let () =
  let out, out_st = process l 0 [] in
  printf "%d\n" out;
  List.iter (fun i -> printf "%d " i) (List.rev out_st);
  ()

open Printf
let input_int_array n =
	Array.init n (fun _ -> Scanf.scanf " %d" (fun v -> v))

let get_int () = Scanf.scanf " %d" (fun v -> v)
let get_2_ints () = Scanf.scanf " %d %d" (fun u v -> u,v)
let get_3_ints () = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w)
let get_4_ints () = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x)
let get_5_ints () = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y)

let print_int_endline n = n |> string_of_int |> print_endline

let () =
  let n = get_int ()
  in let ar = input_int_array n
  in
  let l = Array.fold_left (fun (c,l) v ->
    if v=c+1 then (v,l)
    else if v=1 then (v,c::l)
    else failwith ""
  ) (0,[]) ar
  |> (fun (last,l) -> last::l)
  |> List.rev
  in
  print_endline @@ (List.length l |> string_of_int);
  l |> List.iter (printf "%d ");
  print_newline ()


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

let a = read_int () and b = read_int () and n = read_int () ;;

let result =
    let rec loop d =
        if d >= 10 then -1
        else if (a * 10 + d) mod b = 0 then (a * 10 + d)
        else loop (d + 1)
    in loop 0 ;;

Printf.printf "%d" result ;;

if result <> -1 then
    for i = 1 to n - 1 do
        Printf.printf "0"
    done ;;

Printf.printf "\n" ;;

(* Codeforces 246C KonkursKrasoti written *)

open String;;
open Hashtbl;;

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 Pervasives.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 rec readlist n acc = match n with
	| 0 -> List.rev acc
	| _ -> readlist (n -1) (gr() :: acc);;

let debug = false;;

let a = gr();;
let b = gr();;
let n = gr();;
let ch = Array.make n 0;;

let rec nextchar ao c idx ca =
	if idx >= n then 0
	else if c > 9 then -1
	else if ((10 * ao + c) mod b) != 0 then nextchar ao (c +1) idx ca
	else
		let oo = 0 in
		begin
			ca.(idx) <- c;
			nextchar oo 0 (idx +1) ca
		end;;

let prichars a ca n =
	begin
		print_int a;
		for i = 1 to n do
			print_int ca.(i -1)
		done;
		print_newline()
	end;;

let main () =
	let ret = nextchar a 0 0 ch in
	if ret < 0 then begin print_int (-1); print_newline() end
	else prichars a ch n;;

main();;

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

let () =
    let a = read_int() in
    let b = read_int() in
    let n = read_int() in
    let rec f d = if d = 10 then (-1) else if (10*a+d) mod b = 0 then d else f (d+1) in
    let d = f 0 in
        if d = -1 then Printf.printf "-1\n"
        else (
            Printf.printf "%d%d" a d;
            for i = 1 to n-1 do Printf.printf "0" done;
            print_newline()
        )

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

let () =
  let a = read_int() in
  let b = read_int() in
  let n = read_int() in

  let rec first d = if d=10 then -1 else if (10*a+d) mod b = 0 then d else first (d+1) in

  let d = first 0 in
    if d = -1 then (
      Printf.printf "-1\n"
    ) else (
      Printf.printf "%d%d" a d;
      for i=1 to n-1 do Printf.printf "0" done;
      print_newline()
    )


let _ = read_int () in
let str = read_line () in

let trans c = match c with
| 'A' ->  1
| 'D' -> -1
| _   ->  0 in

let rec count_ad s i l =
        if i >= l then 0
        else (count_ad s (i+1) l) + trans s.[i] in

let get_output c =
        if c = 0 then "Friendship"
        else if c > 0 then "Anton"
        else "Danik" in

print_endline (get_output (count_ad str 0 (String.length str)))

let a_count = ref 0 and
    d_count = ref 0 and
    n = int_of_string (read_line ()) and
    s = read_line () in
for i = 0 to n - 1 do
 begin
   a_count := !a_count + (if s.[i] = 'A' then 1 else 0);
   d_count := !d_count + (if s.[i] = 'D' then 1 else 0);
 end
done;
print_string ( if a_count = d_count then "Friendship"
    else if  a_count > d_count then "Anton"
    else "Danik"
)

let () =
  Scanf.scanf "%d " @@ fun n ->
  let anton = ref 0 in
  let danik = ref 0 in
  Scanf.scanf "%s " @@ String.iter (function 'A' -> incr anton | 'D' -> incr danik | _ -> failwith "");
  if !anton = !danik then
    print_endline "Friendship"
  else if !anton > !danik then
    print_endline "Anton"
  else
    print_endline "Danik"

let char_count s len c =
  let rec cc i sum =
    if i = len then
      sum
    else
      let n = if s.[i] = c then 1 else 0 in
      cc (i + 1) (sum + n)
  in cc 0 0

let n = read_int () ;;
let s = read_line () ;;
let a = char_count s n 'A'
and d = char_count s n 'D'
in
if a = d then
  print_string "Friendship\n"
else if a > d then
  print_string "Anton\n"
else
  print_string "Danik\n"
;;

let input () =
    let total = Scanf.scanf "%d " (fun n -> n) in
    let rec read list n =
        if n > 0
        then
            let var = Scanf.scanf "%c" (fun c -> c) in
            read (var :: list) (n - 1)
        else
            List.rev list
    in read [] total

let solve list =
    let anton_wins = List.length (List.filter (fun c -> c = 'A') list) in
    let danik_wins = List.length (List.filter (fun c -> c = 'D') list) in
    if anton_wins = danik_wins
    then
        "Friendship"
    else
        if anton_wins > danik_wins
        then
            "Anton"
        else
            "Danik"

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


open Printf
let read_string () = Scanf.bscanf Scanf.Scanning.stdin " %s" (fun x -> x)
let read_int () = Scanf.bscanf Scanf.Scanning.stdin " %d" (fun x -> x)
let read_long_int () = Scanf.bscanf Scanf.Scanning.stdin " %Ld" (fun x -> x)
let (+) x y = Int64.add x y

let () =
    let n = read_int() in
    let s = read_string() in
    let tab = Array.make n 0L in
    let h = ref 0L and a = ref 0L and r = ref 0L and d = ref 0L in

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

    for i = 0 to n - 1 do
        if s.[i] = 'h' then h := !h + tab.(i)
        else if s.[i] = 'a' then a := min !h (!a + tab.(i))
        else if s.[i] = 'r' then r := min !a (!r + tab.(i))
        else if s.[i] = 'd' then d := min !r (!d + tab.(i))
    done;

    printf "%Ld\n" !d

let max_i64,(++) = Int64.(shift_left 1L 60,add);;
Scanf.(Array.(
  scanf " %d %s" @@ fun n s ->
    let t = "hard" in
    let a = init n (fun _ -> scanf " %Ld" @@ fun v -> v)
    in
    let dp = make_matrix (n+2) 5 max_i64 in
    let update i j v =
      dp.(i).(j) <- min dp.(i).(j) v
    in
    dp.(0).(0) <- 0L;
    for i=1 to n do
      for j=0 to 3 do
        if s.[i-1] = t.[j] then (
          update i (j+1) @@ dp.(i-1).(j);
          update i j     @@ dp.(i-1).(j) ++ a.(i-1)
        ) else (
          update i j     @@ dp.(i-1).(j)
        )
      done
    done;
    (* for i=0 to n do
      for j=0 to 4 do
        Printf.printf "%Ld " dp.(i).(j)
      done;
      print_newline ()
    done; *)
    Printf.printf "%Ld" @@
      List.fold_left min max_i64 @@
        List.tl @@ List.rev @@ to_list dp.(n)
))

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

let () =
    let n = read_int() in
    let s = read_string() in
    let tab = Array.make n 0 in
    let h = ref 0 and a = ref 0 and r = ref 0 and d = ref 0 in

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

    for i = n - 1 downto 0 do
        if s.[i] = 'd' then d := !d + tab.(i)
        else if s.[i] = 'r' && !d <> 0 then r := !r + tab.(i)
        else if s.[i] = 'a' && !r <> 0 then a := !a + tab.(i)
        else if s.[i] = 'h' && !a <> 0 then h := !h + tab.(i)
    done;

    if !h = 0 then printf "0\n"
    else printf "%d\n" (min (min !h !a) (min !r !d))

open Printf
let read_string () = Scanf.bscanf Scanf.Scanning.stdin " %s" (fun x -> x)
let read_int () = Scanf.bscanf Scanf.Scanning.stdin " %d" (fun x -> x)
let read_long_int () = Scanf.bscanf Scanf.Scanning.stdin " %Ld" (fun x -> x)
let (+) x y = Int64.add x y

let () =
    let n = read_int() in
    let s = read_string() in
    let tab = Array.make n 0L in
    let h = ref 0L and a = ref 0L and r = ref 0L and d = ref 0L in

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

    for i = n - 1 downto 0 do
        if s.[i] = 'd' then d := !d + tab.(i)
        else if s.[i] = 'r' && !d <> 0L then r := !r + tab.(i)
        else if s.[i] = 'a' && !r <> 0L then a := !a + tab.(i)
        else if s.[i] = 'h' && !a <> 0L then h := !h + tab.(i)
    done;

    if !h = 0L then printf "0\n"
    else printf "%Ld\n" (min (min !h !a) (min !r !d))

let max_i64,(++) = Int64.(max_int,add);;
Scanf.(Array.(
  scanf " %d %s" @@ fun n s ->
    let t = "hard" in
    let a = init n (fun _ -> scanf " %Ld" @@ fun v -> v)
    in
    let dp = make_matrix (n+2) 5 max_i64 in
    let update i j v =
      dp.(i).(j) <- min dp.(i).(j) v
    in
    dp.(0).(0) <- 0L;
    for i=1 to n do
      for j=0 to 3 do
        if s.[i-1] = t.[j] then (
          update i (j+1) @@ dp.(i-1).(j);
          update i j     @@ dp.(i-1).(j) ++ a.(i-1)
        ) else (
          update i j     @@ dp.(i-1).(j)
        )
      done
    done;
    (* for i=0 to n do
      for j=0 to 4 do
        Printf.printf "%Ld " dp.(i).(j)
      done;
      print_newline ()
    done; *)
    Printf.printf "%Ld" @@ fold_left min max_i64 dp.(n)
))

let max_i64,(++) = Int64.(shift_left 1L 60,add);;
Scanf.(Array.(
  scanf " %d %s" @@ fun n s ->
    let t = "hard" in
    let a = init n (fun _ -> scanf " %Ld" @@ fun v -> v)
    in
    let dp = make_matrix (n+2) 5 max_i64 in
    let update i j v =
      if v>=0L then dp.(i).(j) <- min dp.(i).(j) v
    in
    dp.(0).(0) <- 0L;
    for i=1 to n do
      for j=0 to 3 do
        if s.[i-1] = t.[j] then (
          update i (j+1) @@ dp.(i-1).(j);
          update i j     @@ dp.(i-1).(j) ++ a.(i-1)
        ) else (
          update i j     @@ dp.(i-1).(j)
        )
      done
    done;
    (* for i=0 to n do
      for j=0 to 4 do
        Printf.printf "%Ld " dp.(i).(j)
      done;
      print_newline ()
    done; *)
    Printf.printf "%Ld" @@ fold_left min max_i64 dp.(n)
));;


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

let rec count (num : int list) (modi : int) (ind : int)=
  match num with
  |[] -> 0
  |h :: t -> if ((ind mod 2) <> modi && h mod 2 = modi) then 1 + count t modi (ind + 1) else count t modi (ind + 1)
;;

let rec main (t : int) =
  if t = 0 then
    ()
  else
    (
      let siz = read_int () in
      let a = read_ints () in
      let one_zero = count a 0 0 in
      let zero_one = count a 1 0 in
      if (one_zero <> zero_one) then print_endline "-1" else print_endline (string_of_int one_zero);
      main (t - 1)
    )
;;
let t = read_int() in
main t

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

let () =
  let t = read_int () in
  for i = 1 to t do
    let n = read_int () in
    let (a, b) =
      let rec loop a b = function
        | 0 -> (a, b)
        | m ->
            let x = read_int () in
            if not ((x mod 2) = ((n-m) mod 2)) then
              if ((n-m) mod 2) = 0 then
                loop (a+1) b (m-1)
              else
                loop a (b+1) (m-1)
            else
              loop a b (m-1)
      in loop 0 0 n
    in
    if not (a = b) then Printf.printf "-1\n" else Printf.printf "%d\n" a
  done