Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity!A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: ci and pi — the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly.We know that for each request, all ci people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment.Unfortunately, there only are k tables in the restaurant. For each table, we know ri — the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing.Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum.

The first line of the input contains integer n (1 ≤ n ≤ 1000) — the number of requests from visitors. Then n lines follow. Each line contains two integers: ci, pi (1 ≤ ci, pi ≤ 1000) — the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≤ k ≤ 1000) — the number of tables in the restaurant. The last line contains k space-separated integers: r1, r2, ..., rk (1 ≤ ri ≤ 1000) — the maximum number of people that can sit at each table.

In the first line print two integers: m, s — the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines — each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them.

let read_int()=Scanf.scanf " %d" (fun x->x);;
let n=read_int();;
let r=ref n;;
let req=Array.make (n+1) (0,0,0);;
for i=1 to n do
   let a=read_int() and b=read_int() in
   req.(i)<-(a,b,i)
done;;
let k=read_int();;
let tab=Array.make (k+1) (0,0);;
for i=1 to k do
   tab.(i)<-(read_int(),i)
done;;

let rec list_of_tab vec i j=
if i=j then [vec.(i)] else vec.(i)::(list_of_tab vec (i+1) j);;

let rec inserer v i=function
|[]->[(v,i)]
|(h,j)::t->if v<=h then (v,i)::(h,j)::t else (h,j)::(inserer v i t);;

let rec tri=function
|[]->[]
|(h,j)::t->inserer h j (tri t);;

let rec inserer2 c p i=function
|[]->[(c,p,i)]
|(c1,p1,j)::t->if (p>p1) then (c,p,i)::(c1,p1,j)::t else if (p=p1)&&(c<=c1) then (c,p,i)::(c1,p1,j)::t else (c1,p1,j)::(inserer2 c p i t);;

let rec tri2=function
|[]->[]
|(c,p,i)::t->inserer2 c p i (tri2 t);;

let lreq=tri2(list_of_tab req 1 n);;
let ltab=tri(list_of_tab tab 1 k);;

let final=ref [];;

let rec faire c p i=function
|[]->r:= !r-1;(0,[])
|(h,j)::t->if c<=h then (final:= (i,j)::( !final);(p,t)) else let (p1,t1)=faire c p i t in (p1,(h,j)::t1);;

let rec make l2=function
|[]->0
|(c,p,i)::t->let (a,b)=faire c p i l2 in a+make b t;;

let score=make ltab lreq;;
print_int !r;;
print_string " ";;
print_int score;;

let rec print_list=function
|[]->()
|(i,j)::t->print_list t;print_newline();print_int i;print_string " ";print_int j;;

print_list !final;;

let read_int()=Scanf.scanf " %d" (fun x->x);;
let n=read_int();;
let r=ref n;;
let req=Array.make (n+1) (0,0,0);;
for i=1 to n do
   let a=read_int() and b=read_int() in
   req.(i)<-(a,b,i)
done;;
let k=read_int();;
let tab=Array.make (k+1) (0,0);;
for i=1 to k do
   tab.(i)<-(read_int(),i)
done;;

let rec list_of_tab vec i j=
if i=j then [vec.(i)] else vec.(i)::(list_of_tab vec (i+1) j);;

let rec inserer v i=function
|[]->[(v,i)]
|(h,j)::t->if v<=h then (v,i)::(h,j)::t else (h,j)::(inserer v i t);;

let rec tri=function
|[]->[]
|(h,j)::t->inserer h j (tri t);;

let rec inserer2 c p i=function
|[]->[(c,p,i)]
|(c1,p1,j)::t->if (p>=p1)&&(c<=c1) then (c,p,i)::(c1,p1,j)::t else (c1,p1,j)::(inserer2 c p i t);;

let rec tri2=function
|[]->[]
|(c,p,i)::t->inserer2 c p i (tri2 t);;

let lreq=tri2(list_of_tab req 1 n);;
let ltab=tri(list_of_tab tab 1 k);;

let final=ref [];;

let rec faire c p i=function
|[]->r:= !r-1;(0,[])
|(h,j)::t->if c<=h then (final:= (i,j)::( !final);(p,t)) else let (p1,t1)=faire c p i t in (p1,(h,j)::t1);;

let rec make l2=function
|[]->0
|(c,p,i)::t->let (a,b)=faire c p i l2 in a+make b t;;

let score=make ltab lreq;;
print_int !r;;
print_string " ";;
print_int score;;

let rec print_list=function
|[]->()
|(i,j)::t->print_list t;print_newline();print_int i;print_string " ";print_int j;;

print_list !final;;


Your working week consists of $$$n$$$ days numbered from $$$1$$$ to $$$n$$$, after day $$$n$$$ goes day $$$1$$$ again. And $$$3$$$ of them are days off. One of the days off is the last day, day $$$n$$$. You have to decide when the other two are.Choosing days off, you pursue two goals:   No two days should go one after the other. Note that you can't make day $$$1$$$ a day off because it follows day $$$n$$$.  Working segments framed by days off should be as dissimilar as possible in duration. More specifically, if the segments are of size $$$l_1$$$, $$$l_2$$$, and $$$l_3$$$ days long, you want to maximize $$$\min(|l_1 - l_2|, |l_2 - l_3|, |l_3 - l_1|)$$$. Output the maximum value of $$$\min(|l_1 - l_2|, |l_2 - l_3|, |l_3 - l_1|)$$$ that can be obtained.

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains the integer $$$n$$$ ($$$6 \le n \le 10^9$$$).

For each test case, output one integer — the maximum possible obtained value.

NoteIn the image below you can see the example solutions for the first two test cases. Chosen days off are shown in purple. Working segments are underlined in green.In test case $$$1$$$, the only options for days off are days $$$2$$$, $$$3$$$, and $$$4$$$ (because $$$1$$$ and $$$5$$$ are next to day $$$n$$$). So the only way to place them without selecting neighboring days is to choose days $$$2$$$ and $$$4$$$. Thus, $$$l_1 = l_2 = l_3 = 1$$$, and the answer $$$\min(|l_1 - l_2|, |l_2 - l_3|, |l_3 - l_1|) = 0$$$.   For test case $$$2$$$, one possible way to choose days off is shown. The working segments have the lengths of $$$2$$$, $$$1$$$, and $$$4$$$ days. So the minimum difference is $$$1 = \min(1, 3, 2) = \min(|2 - 1|, |1 - 4|, |4 - 2|)$$$. It can be shown that there is no way to make it larger.

open Printf
open Scanf

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

let () =
        let t = read_int () in
        for ct = 1 to t do
                let x = read_int () in
                let y = if x > 6 then (x - 3) / 3 - 1 else 0 in
                printf "%d\n" y
        done;

open Printf
open Scanf

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

let () =
        let t = read_int () in
        for ct = 1 to t do
                let x = read_int () in
                let y = if x > 6 then (x - 4) / 3 - 1 else 0 in
                printf "%d\n" y
        done;


Codeforces separates its users into $$$4$$$ divisions by their rating:  For Division 1: $$$1900 \leq \mathrm{rating}$$$  For Division 2: $$$1600 \leq \mathrm{rating} \leq 1899$$$  For Division 3: $$$1400 \leq \mathrm{rating} \leq 1599$$$  For Division 4: $$$\mathrm{rating} \leq 1399$$$ Given a $$$\mathrm{rating}$$$, print in which division the $$$\mathrm{rating}$$$ belongs.

The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of testcases. The description of each test consists of one line containing one integer $$$\mathrm{rating}$$$ ($$$-5000 \leq \mathrm{rating} \leq 5000$$$).

For each test case, output a single line containing the correct division in the format "Division X", where $$$X$$$ is an integer between $$$1$$$ and $$$4$$$ representing the division for the corresponding rating.

Division 4
Division 4
Division 4
Division 4
Division 3
Division 2
Division 1

NoteFor test cases $$$1-4$$$, the corresponding ratings are $$$-789$$$, $$$1299$$$, $$$1300$$$, $$$1399$$$, so all of them are in division $$$4$$$.For the fifth test case, the corresponding rating is $$$1400$$$, so it is in division $$$3$$$.For the sixth test case, the corresponding rating is $$$1679$$$, so it is in division $$$2$$$.For the seventh test case, the corresponding rating is $$$2300$$$, so it is in division $$$1$$$.

let getInt () = Scanf.scanf " %d" (fun i -> i);;
let f((x: int)) =
if x >= 1900 then 1
else if x >= 1600 then 2
else if x >= 1400 then 3
else 4;;

let main() =
  let t = getInt() in
  for i = 1 to t do
    let x = getInt() in print_endline("Division " ^ string_of_int(f(x)))
  done;;

main();;

let id x = x

let div_of_rating rating =
  let ns =
    match rating with
    | r when r <= 1399 -> "4"
    | r when r <= 1599 -> "3"
    | r when r <= 1899 -> "2"
    | _ -> "1"
  in
  "Division " ^ ns

let solve () =
  let t = Scanf.scanf "%d\n" id in
  let () =
    for i = 1 to t do
      let r = Scanf.scanf "%d\n" (fun x -> x) in
      print_endline (div_of_rating r)
    done
  in
  ()

let main = solve ()


You are given an array $$$a_1, a_2, \dots , a_n$$$ and two integers $$$m$$$ and $$$k$$$.You can choose some subarray $$$a_l, a_{l+1}, \dots, a_{r-1}, a_r$$$. The cost of subarray $$$a_l, a_{l+1}, \dots, a_{r-1}, a_r$$$ is equal to $$$\sum\limits_{i=l}^{r} a_i - k \lceil \frac{r - l + 1}{m} \rceil$$$, where $$$\lceil x \rceil$$$ is the least integer greater than or equal to $$$x$$$. The cost of empty subarray is equal to zero.For example, if $$$m = 3$$$, $$$k = 10$$$ and $$$a = [2, -4, 15, -3, 4, 8, 3]$$$, then the cost of some subarrays are:  $$$a_3 \dots a_3: 15 - k \lceil \frac{1}{3} \rceil = 15 - 10 = 5$$$;  $$$a_3 \dots a_4: (15 - 3) - k \lceil \frac{2}{3} \rceil = 12 - 10 = 2$$$;  $$$a_3 \dots a_5: (15 - 3 + 4) - k \lceil \frac{3}{3} \rceil = 16 - 10 = 6$$$;  $$$a_3 \dots a_6: (15 - 3 + 4 + 8) - k \lceil \frac{4}{3} \rceil = 24 - 20 = 4$$$;  $$$a_3 \dots a_7: (15 - 3 + 4 + 8 + 3) - k \lceil \frac{5}{3} \rceil = 27 - 20 = 7$$$. Your task is to find the maximum cost of some subarray (possibly empty) of array $$$a$$$.

The first line contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \le n \le 3 \cdot 10^5, 1 \le m \le 10, 1 \le k \le 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$).

Print the maximum cost of some subarray of array $$$a$$$.

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

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

let solve n m k a =
  let open Int64 in
  let byMod = Array.make m (of_int n ** of_float 1e11) in
  byMod.(0) <- 0L;
  let sum = ref 0L in
  let ans = ref 0L in
  for i=0 to n-1 do
    sum := !sum ++ of_int m ** a.(i) -- of_int k;
    let r = (i+1) mod m in
    for l=0 to m-1 do
      let d = (r-l+m) mod m in
      let adjust = if d = 0 then 0L else of_int k ** of_int (m - d) in
      ans := max !ans (!sum -- byMod.(l) -- adjust);
    done;
    byMod.(r) <- min byMod.(r) !sum;
  done;
  !ans // of_int m

(*
let solveSlow n m k a =
  let open Int64 in
  let ans = ref 0L in
  for l=0 to n-1 do
    for r=l+1 to n do
      let rec go sum i = if i >= r then sum else go (sum ++ a.(i)) (i + 1) in
      let sum = go 0L l in
      let ok = (m > 0) in
      if not ok then Printf.eprintf "%d %d %d\n" n m k;
      assert ok;
      let cur = sum -- of_int k ** (of_int (r - l + m-1) // of_int m) in
      ans := max !ans cur
    done
  done;
  !ans
*)

let cfMain () =
  let n = readInt () in
  let m = readInt () in
  let k = readInt () in
  let a = Array.init n (fun _ -> readInt64 ()) in
  Printf.printf "%Ld\n" (solve n m k a)

(* let () =
  let x = solve 7 3 10 (Array.map Int64.of_int [|7;3;10;2;-4;15;-3;4;8;3|]) in Printf.printf "%Ld\n" x *)

(*
let tenToNine = int_of_float 1e9
let test =
  QCheck.Test.make ~count:1000 ~name:"fast = slow"
    QCheck.(triple (1 -- 10) (1 -- tenToNine) (array_of_size (gen (1 -- 100)) ((-tenToNine) -- tenToNine)))
    (fun (m, k, a) ->
      let a = Array.map Int64.of_int a in
      let n = Array.length a in
      QCheck.assume (n > 0);
      QCheck.assume (m > 0);
      QCheck.assume (k > 0);
      solve n m k a = solveSlow n m k a)

let testMain () = QCheck.Test.check_exn test
*)

(* let () =
  let (n, m, k, a) = (1, 4, 333333335, [|333333335L|]) in
  let x = solve n m k a in
  let y = solveSlow n m k a in
  Printf.printf "%Ld %Ld\n" x y *)

let () = cfMain ()

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

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

let solve n m k a =
  let open Int64 in
  let byMod = Array.make m 0L in
  let sum = ref 0L in
  let ans = ref 0L in
  for i=0 to n-1 do
    sum := !sum ++ of_int m ** a.(i) -- of_int k;
    let r = (i+1) mod m in
    for l=0 to m-1 do
      let d = (r-l+m) mod m in
      let adjust = if d = 0 then 0 else k*(m - d) in
      ans := max !ans (!sum -- byMod.(l) -- of_int adjust);
    done;
    byMod.(r) <- min byMod.(r) !sum;
  done;
  !ans // of_int m

let main () =
  let n = readInt () in
  let m = readInt () in
  let k = readInt () in
  let a = Array.init n (fun _ -> readInt64 ()) in
  Printf.printf "%Ld\n" (solve n m k a)

let () = main ()

(* let () =
  let x = solve 7 3 10 (Array.map Int64.of_int [|7;3;10;2;-4;15;-3;4;8;3|]) in () *)

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

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

let solve n m k a =
  let open Int64 in
  let byMod = Array.make m (of_int n ** of_float 1e9) in
  let sum = ref 0L in
  let ans = ref 0L in
  for i=0 to n-1 do
    sum := !sum ++ of_int m ** a.(i) -- of_int k;
    let r = (i+1) mod m in
    for l=0 to m-1 do
      let d = (r-l+m) mod m in
      let adjust = if d = 0 then 0 else k*(m - d) in
      ans := max !ans (!sum -- byMod.(l) -- of_int adjust);
    done;
    byMod.(r) <- min byMod.(r) !sum;
  done;
  !ans // of_int m

(*
let solveSlow n m k a =
  let open Int64 in
  let ans = ref 0L in
  for l=0 to n-1 do
    for r=l+1 to n do
      let rec go sum i = if i >= r then sum else go (sum ++ a.(i)) (i + 1) in
      let sum = go 0L l in
      let cur = sum -- of_int k ** (of_int (r - l + m-1) // of_int m) in
      ans := max !ans cur
    done
  done;
  !ans
*)

let cfMain () =
  let n = readInt () in
  let m = readInt () in
  let k = readInt () in
  let a = Array.init n (fun _ -> readInt64 ()) in
  Printf.printf "%Ld\n" (solve n m k a)

(* let () =
  let x = solve 7 3 10 (Array.map Int64.of_int [|7;3;10;2;-4;15;-3;4;8;3|]) in Printf.printf "%Ld\n" x *)

(*
let tenToNine = int_of_float 1e9
let test =
  QCheck.Test.make ~count:1000 ~name:"fast = slow"
    QCheck.(triple (1 -- 10) (1 -- tenToNine) (array_of_size (gen (1 -- 100)) ((-tenToNine) -- tenToNine)))
    (fun (m, k, a) ->
      let a = Array.map Int64.of_int a in
      let n = Array.length a in
      solve n m k a = solveSlow n m k a)

let testMain () = QCheck.Test.check_exn test
*)

(* let () = let x = solve 1 2 2 [|2L|] in Printf.printf "%Ld\n" x *)

let () = cfMain ()


A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.The town where Laurenty lives in is not large. The houses in it are located in two rows, n houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.The traffic light on the crosswalk from the j-th house of the i-th row to the (j + 1)-th house of the same row has waiting time equal to aij (1 ≤ i ≤ 2, 1 ≤ j ≤ n - 1). For the traffic light on the crossing from the j-th house of one row to the j-th house of another row the waiting time equals bj (1 ≤ j ≤ n). The city doesn't have any other crossings.The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing.    Figure to the first sample. Help Laurenty determine the minimum total time he needs to wait at the crossroads.

The first line of the input contains integer n (2 ≤ n ≤ 50) — the number of houses in each row.  Each of the next two lines contains n - 1 space-separated integer — values aij (1 ≤ aij ≤ 100).  The last line contains n space-separated integers bj (1 ≤ bj ≤ 100).

Print a single integer — the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home.

NoteThe first sample is shown on the figure above. In the second sample, Laurenty's path can look as follows:   Laurenty crosses the avenue, the waiting time is 3;  Laurenty uses the second crossing in the first row, the waiting time is 2;  Laurenty uses the first crossing in the first row, the waiting time is 1;  Laurenty uses the first crossing in the first row, the waiting time is 1;  Laurenty crosses the avenue, the waiting time is 1;  Laurenty uses the second crossing in the second row, the waiting time is 3.  In total we get that the answer equals 11.In the last sample Laurenty visits all the crossings, so the answer is 4.

let n= read_int () and ans = ref max_int;;
let a= Array.append
	[|( Array.of_list (List.map (int_of_string) (Str.split (Str.regexp " ") (read_line ()))))|]
    [|( Array.of_list (List.map (int_of_string) (Str.split (Str.regexp " ") (read_line ()))))|];;
let b=  Array.of_list (List.map(int_of_string) (Str.split (Str.regexp " ") (read_line ())));;
let f1= Array.create n 0 and f2= Array.create n 0;;
for i= 1 to n - 1 do Array.set f1 i (f1.(i - 1) + a.(1).(i - 1)) done;;
for i= n - 2 downto 0 do Array.set f2 i (f2.(i + 1) + a.(0).(i)) done;;
for i= 0 to n - 1 do for j= 0 to n - 1 do if i <> j then
	ans:= min !ans (f1.(i) + b.(i) + f2.(i) + f2.(j) + f1.(j) + b.(j))
done done;;
print_int !ans;;

let a = ref [] in
let b = ref [] in
let c = ref [] in
let n = read_int () in
	(let l = Scanf.Scanning.from_string (read_line ()) in
	for j = 1 to n-1 do
		try let k = Scanf.bscanf l "%d " (fun x -> x) in a := k::(!a) with _ ->
		let k = Scanf.bscanf l "%d" (fun x -> x) in a := k::(!a)
	done);
	(let l = Scanf.Scanning.from_string (read_line ()) in
	for j = 1 to n-1 do
		try let k = Scanf.bscanf l "%d " (fun x -> x) in b := k::(!b) with _ ->
		let k = Scanf.bscanf l "%d" (fun x -> x) in b := k::(!b)
	done);
	(let l = Scanf.Scanning.from_string (read_line ()) in
	for j = 1 to n do
		try let k = Scanf.bscanf l "%d " (fun x -> x) in c := k::(!c) with _ ->
		let k = Scanf.bscanf l "%d" (fun x -> x) in c := k::(!c)
	done);

let al = List.rev (!a) in
let bl = List.rev (!b) in
let cl = List.rev (!c) in

let rec trav i where st =
	match where with
		true ->
			let k2 = trav i false (2::st) in
			if i = n-1 then ((fst k2) + List.nth cl i, snd k2)
			else
				let k1 = trav (i+1) true (0::st) in
				let i2 = List.nth cl i in
				let t = min (fst k1 + List.nth al i) (fst k2 + i2) in
				if t = (i2 + fst k2) then (t, snd k2)
				else (t, snd k1)
		| false ->
			if i = n-1 then (0, st)
			else let k2 = trav (i+1) false (1::st) in
			(fst k2 + (List.nth bl i), snd k2)
	in
let (stv, stl) = trav 0 true [] in
let st = List.rev stl in

let rec trav2 i where ed w =
	match where with
		false ->
			let k2 = trav2 i true (2::ed) 2 in
			if i = 0 then ((fst k2) + List.nth cl i, snd k2)
			else
				let k1 = trav2 (i-1) false (1::ed) 1 in
				let i2 = List.nth cl i in
				let t = min (fst k1 + List.nth bl (i-1)) (fst k2 + i2) in
				if t = (i2 + fst k2) then (t, snd k2)
				else (t, snd k1)
		| true ->
			if i = 0 then
				(let bb = ed <> st in
				if bb then (0, ed) else (99999999, ed))
			else let k2 = trav2 (i-1) true (0::ed) 0 in
			(fst k2 + (List.nth al (i-1)), snd k2)
	in
let (edv, edl) = trav2 (n-1) false [] 2 in

print_int (stv + edv);;

let () = Scanf.scanf "%d\n" (fun n ->
    let read _ = Scanf.scanf "%d%_c" (fun x -> x) in

    let a1 = Array.init (n - 1) read
    and a2 = Array.init (n - 1) read
    and b  = Array.init n       read in

    (* calculate cumulative sum of a1
     * and reverse cumulative sum of a2 *)
    let s1 = Array.of_list (List.rev (Array.fold_left (fun (hd::_ as acc) x -> (x + hd) :: acc) [0] a1))
    and s2 = Array.of_list (Array.fold_right (fun x (hd::_ as acc) -> (x + hd) :: acc) a2 [0]) in

    let best = ref (-1) in

    (* first crossing *)
    for i = 0 to n - 2 do
        (* second crossing *)
        for j = i + 1 to n - 1 do
            let sum = s1.(i) + s2.(j) + s1.(j) + s2.(i) + b.(i) + b.(j) in
            if !best = (-1) || sum < !best
            then best := sum
        done
    done;

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


Smart Beaver became interested in drawing. He draws suns. However, at some point, Smart Beaver realized that simply drawing suns is boring. So he decided to design a program that will process his drawings. You are given a picture drawn by the beaver. It will have two colors: one for the background and one for the suns in the image. Your task will be to count the number of suns in the image and for each of them to count the number of rays.Sun is arbitrarily rotated ellipse with rays. Ray is a segment which connects point on boundary of the ellipse with some point outside ellipse.   An image where all suns are circles.     An image where all suns are ellipses, their axes are parallel to the coordinate axes.     An image where all suns are rotated ellipses.  It is guaranteed that:   No two suns have common points.  The rays’ width is 3 pixels.  The lengths of the ellipsis suns’ axes will lie between 40 and 200 pixels.  No two rays intersect.  The lengths of all rays will lie between 10 and 30 pixels.

The first line contains two integers h and w — the height and width of the image (1 ≤ h, w ≤ 1600). Next h lines will contain w space-separated integers each. They describe Smart Beaver’s picture. Each number equals either a 0 (the image background), or a 1 (the sun color). The input limits for scoring 30 points are (subproblem F1):    All suns on the image are circles.  The input limits for scoring 70 points are (subproblems F1+F2):    All suns on the image are ellipses with axes parallel to the coordinate axes.  The input limits for scoring 100 points are (subproblems F1+F2+F3):   All suns on the image are ellipses, they can be arbitrarily rotated.

The first line must contain a single number k — the number of suns on the beaver’s image. The second line must contain exactly k space-separated integers, corresponding to the number of rays on each sun. The numbers of the second line must be sorted in the increasing order.

NoteFor each complexity level you are suggested a sample in the initial data. You can download the samples at http://www.abbyy.ru/sun.zip.

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

let _ =
  let sqr x = x *. x in
  let h = getnum () in
  let w = getnum () in
  let a = Array.make_matrix h w 0 in
  let () =
    for i = 0 to h - 1 do
      for j = 0 to w - 1 do
	a.(i).(j) <- getnum ()
      done
    done
  in
  let cand = Array.make_matrix h w false in
  let res = ref 0 in
  let rays = ref [] in
  let used = Array.make_matrix h w 0 in
  let stack = Array.make (h * w) (0, 0) in
  let pos = ref 0 in
  let idx = ref 0 in
  (*let a' = Array.make_matrix n n 0 in
    for i = 0 to n - 1 do
      for j = 0 to n - 1 do
	let b = ref 0 in
	  for i' = -f to f do
	    for j' = -f to f do
	      let x = j + j'
	      and y = i + i' in
		if x >= 0 && y >= 0 && x < n && y < n
		then b := !b + a.(y).(x)
	    done
	  done;
	  if float_of_int !b > float_of_int ((2 * f + 1) * (2 * f + 1)) *. 0.4
	  then a'.(i).(j) <- 1
      done
    done;
    for i = 0 to n - 1 do
      a.(i) <- a'.(i)
    done;*)
    for i = 0 to h - 1 do
      for j = 0 to w - 1 do
	if used.(i).(j) = 0 then (
	  if a.(i).(j) <> 0 then (
	    let d = [| (1, 0); (0, 1); (-1, 0); (0, -1) |] in
	    let border = ref [] in
	    let mx = ref 0 in
	    let my = ref 0 in
	    let cnt = ref 0 in
	    let rec dfs () =
	      if !pos > 0 then (
		decr pos;
		let (x, y) = stack.(!pos) in
		  mx := !mx + x;
		  my := !my + y;
		  incr cnt;
		  for i = 0 to 3 do
		    let (dx, dy) = d.(i) in
		    let x = x + dx
		    and y = y + dy in
		      if x >= 0 && y >= 0 && x < w && y < h &&
			a.(y).(x) > 0
		      then (
			if used.(y).(x) = 0 then (
			  stack.(!pos) <- (x, y);
			  incr pos;
			  used.(y).(x) <- !idx;
			)
		      ) else (
			border := (x, y) :: !border
		      )
		  done;
		  dfs ()
	      )
	    in
	      incr idx;
	      pos := 0;
	      stack.(!pos) <- (j, i);
	      incr pos;
	      used.(i).(j) <- !idx;
	      dfs ();
	      let border = Array.of_list !border in
	      let l = Array.length border in
	      let (mx, my) =
		(*let mx = ref 0 in
		let my = ref 0 in
		  for i = 0 to l - 1 do
		    let (x, y) = border.(i) in
		      mx := !mx + x;
		      my := !my + y;
		  done;
		  (float_of_int !mx /. float_of_int l,
		   float_of_int !my /. float_of_int l)*)
		(float_of_int !mx /. float_of_int !cnt,
		 float_of_int !my /. float_of_int !cnt)
	      in
	      let ps = ref [] in
	      let d = 4 in
	      let () =
		Array.iter
		  (fun (x, y) ->
		     let b = ref true in
		     let dist =
		       let x = float_of_int x
		       and y = float_of_int y in
			 sqrt (sqr (x -. mx) +. sqr (y -. my))
		     in
		     let s = ref 0 in
		     let t = ref 0 in
		       for y' = y - d to y + d do
			 for x' = x - d to x + d do
			   incr t;
			   if x' >= 0 && y' >= 0 && x' < w && y' < h &&
			     (x' - x) * (x' - x) + (y' - y) * (y' - y) <= d * d
			   then (
			     let dist' =
			       let x = float_of_int x'
			       and y = float_of_int y' in
				 sqrt (sqr (x -. mx) +. sqr (y -. my))
			     in
			       if cand.(y').(x')
			       then b := false;
			       if used.(y').(x') = !idx
			       then incr s;
			       if used.(y').(x') = !idx && dist' > dist
			       then b := false;
			   )
			 done
		       done;
		       if !b && float_of_int !s *. 6.5 < float_of_int !t then (
			 cand.(y).(x) <- true;
			 ps := (x, y) :: !ps;
			 (*Printf.printf "asd %d %d  %d %d\n" x y !s !t;*)
		       )
		  ) border
	      in
		rays := (List.length !ps) :: !rays;
		incr res;
		(*let d =
		  Array.map
		    (fun (x, y) ->
		       let x = float_of_int x
		       and y = float_of_int y in
			 sqrt (sqr (x -. mx) +. sqr (y -. my))
		    ) border
		in
		let r =
		  let r = ref 0.0 in
		    for i = 0 to l - 1 do
		      r := !r +. d.(i)
		    done;
		    !r /. float_of_int l
		in
		let sr =
		  let sr = ref 0.0 in
		    for i = 0 to l - 1 do
		      sr := !sr +. sqr (d.(i) -. r)
		    done;
		    !sr /. float_of_int l
		in
		  if sr /. r < 0.03
		  then incr resc
		  else incr ress;
		  (*Printf.printf "asd %f %f %f %f\t%f  %d\n"
		    mx my r sr (sr /. r) l*)
		*)
	    )
	)
      done
    done;
    let rays = List.sort compare !rays in
      Printf.printf "%d\n" !res;
      List.iter (Printf.printf "%d ") rays;
      Printf.printf "\n"

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

let _ =
  let sqr x = x *. x in
  let h = getnum () in
  let w = getnum () in
  let a = Array.make_matrix h w 0 in
  let () =
    for i = 0 to h - 1 do
      for j = 0 to w - 1 do
	a.(i).(j) <- getnum ()
      done
    done
  in
  let cand = Array.make_matrix h w false in
  let res = ref 0 in
  let rays = ref [] in
  let used = Array.make_matrix h w 0 in
  let stack = Array.make (h * w) (0, 0) in
  let pos = ref 0 in
  let idx = ref 0 in
  (*let a' = Array.make_matrix n n 0 in
    for i = 0 to n - 1 do
      for j = 0 to n - 1 do
	let b = ref 0 in
	  for i' = -f to f do
	    for j' = -f to f do
	      let x = j + j'
	      and y = i + i' in
		if x >= 0 && y >= 0 && x < n && y < n
		then b := !b + a.(y).(x)
	    done
	  done;
	  if float_of_int !b > float_of_int ((2 * f + 1) * (2 * f + 1)) *. 0.4
	  then a'.(i).(j) <- 1
      done
    done;
    for i = 0 to n - 1 do
      a.(i) <- a'.(i)
    done;*)
    for i = 0 to h - 1 do
      for j = 0 to w - 1 do
	if used.(i).(j) = 0 then (
	  if a.(i).(j) <> 0 then (
	    let d = [| (1, 0); (0, 1); (-1, 0); (0, -1) |] in
	    let border = ref [] in
	    let mx = ref 0 in
	    let my = ref 0 in
	    let cnt = ref 0 in
	    let rec dfs () =
	      if !pos > 0 then (
		decr pos;
		let (x, y) = stack.(!pos) in
		  mx := !mx + x;
		  my := !my + y;
		  incr cnt;
		  for i = 0 to 3 do
		    let (dx, dy) = d.(i) in
		    let x = x + dx
		    and y = y + dy in
		      if x >= 0 && y >= 0 && x < w && y < h &&
			a.(y).(x) > 0
		      then (
			if used.(y).(x) = 0 then (
			  stack.(!pos) <- (x, y);
			  incr pos;
			  used.(y).(x) <- !idx;
			)
		      ) else (
			border := (x, y) :: !border
		      )
		  done;
		  dfs ()
	      )
	    in
	      incr idx;
	      pos := 0;
	      stack.(!pos) <- (j, i);
	      incr pos;
	      used.(i).(j) <- !idx;
	      dfs ();
	      let border = Array.of_list !border in
	      let l = Array.length border in
	      let (mx, my) =
		(*let mx = ref 0 in
		let my = ref 0 in
		  for i = 0 to l - 1 do
		    let (x, y) = border.(i) in
		      mx := !mx + x;
		      my := !my + y;
		  done;
		  (float_of_int !mx /. float_of_int l,
		   float_of_int !my /. float_of_int l)*)
		(float_of_int !mx /. float_of_int !cnt,
		 float_of_int !my /. float_of_int !cnt)
	      in
	      let ps = ref [] in
	      let d = 4 in
	      let () =
		Array.iter
		  (fun (x, y) ->
		     let b = ref true in
		     let dist =
		       let x = float_of_int x
		       and y = float_of_int y in
			 sqrt (sqr (x -. mx) +. sqr (y -. my))
		     in
		     let s = ref 0 in
		     let t = ref 0 in
		       for y' = y - d to y + d do
			 for x' = x - d to x + d do
			   incr t;
			   if x' >= 0 && y' >= 0 && x' < w && y' < h then (
			     let dist' =
			       let x = float_of_int x'
			       and y = float_of_int y' in
				 sqrt (sqr (x -. mx) +. sqr (y -. my))
			     in
			       if cand.(y').(x')
			       then b := false;
			       if used.(y').(x') = !idx
			       then incr s;
			       if used.(y').(x') = !idx && dist' > dist
			       then b := false;
			   )
			 done
		       done;
		       if !b && !s * 4 < !t then (
			 cand.(y).(x) <- true;
			 ps := (x, y) :: !ps;
			 (*Printf.printf "asd %d %d  %d %d\n" x y !s !t;*)
		       )
		  ) border
	      in
		rays := (List.length !ps) :: !rays;
		incr res;
		(*let d =
		  Array.map
		    (fun (x, y) ->
		       let x = float_of_int x
		       and y = float_of_int y in
			 sqrt (sqr (x -. mx) +. sqr (y -. my))
		    ) border
		in
		let r =
		  let r = ref 0.0 in
		    for i = 0 to l - 1 do
		      r := !r +. d.(i)
		    done;
		    !r /. float_of_int l
		in
		let sr =
		  let sr = ref 0.0 in
		    for i = 0 to l - 1 do
		      sr := !sr +. sqr (d.(i) -. r)
		    done;
		    !sr /. float_of_int l
		in
		  if sr /. r < 0.03
		  then incr resc
		  else incr ress;
		  (*Printf.printf "asd %f %f %f %f\t%f  %d\n"
		    mx my r sr (sr /. r) l*)
		*)
	    )
	)
      done
    done;
    let rays = List.sort compare !rays in
      Printf.printf "%d\n" !res;
      List.iter (Printf.printf "%d ") rays;
      Printf.printf "\n"

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

let _ =
  let sqr x = x *. x in
  let h = getnum () in
  let w = getnum () in
  let a = Array.make_matrix h w 0 in
  let () =
    for i = 0 to h - 1 do
      for j = 0 to w - 1 do
	a.(i).(j) <- getnum ()
      done
    done
  in
  let cand = Array.make_matrix h w false in
  let res = ref 0 in
  let rays = ref [] in
  let used = Array.make_matrix h w 0 in
  let stack = Array.make (h * w) (0, 0) in
  let pos = ref 0 in
  let idx = ref 0 in
  (*let a' = Array.make_matrix n n 0 in
    for i = 0 to n - 1 do
      for j = 0 to n - 1 do
	let b = ref 0 in
	  for i' = -f to f do
	    for j' = -f to f do
	      let x = j + j'
	      and y = i + i' in
		if x >= 0 && y >= 0 && x < n && y < n
		then b := !b + a.(y).(x)
	    done
	  done;
	  if float_of_int !b > float_of_int ((2 * f + 1) * (2 * f + 1)) *. 0.4
	  then a'.(i).(j) <- 1
      done
    done;
    for i = 0 to n - 1 do
      a.(i) <- a'.(i)
    done;*)
    for i = 0 to h - 1 do
      for j = 0 to w - 1 do
	if used.(i).(j) = 0 then (
	  if a.(i).(j) <> 0 then (
	    let d = [| (1, 0); (0, 1); (-1, 0); (0, -1) |] in
	    let border = ref [] in
	    let mx = ref 0 in
	    let my = ref 0 in
	    let cnt = ref 0 in
	    let rec dfs () =
	      if !pos > 0 then (
		decr pos;
		let (x, y) = stack.(!pos) in
		  mx := !mx + x;
		  my := !my + y;
		  incr cnt;
		  for i = 0 to 3 do
		    let (dx, dy) = d.(i) in
		    let x = x + dx
		    and y = y + dy in
		      if x >= 0 && y >= 0 && x < w && y < h &&
			a.(y).(x) > 0
		      then (
			if used.(y).(x) = 0 then (
			  stack.(!pos) <- (x, y);
			  incr pos;
			  used.(y).(x) <- !idx;
			)
		      ) else (
			border := (x, y) :: !border
		      )
		  done;
		  dfs ()
	      )
	    in
	      incr idx;
	      pos := 0;
	      stack.(!pos) <- (j, i);
	      incr pos;
	      used.(i).(j) <- !idx;
	      dfs ();
	      let border = Array.of_list !border in
	      let l = Array.length border in
	      let (mx, my) =
		(*let mx = ref 0 in
		let my = ref 0 in
		  for i = 0 to l - 1 do
		    let (x, y) = border.(i) in
		      mx := !mx + x;
		      my := !my + y;
		  done;
		  (float_of_int !mx /. float_of_int l,
		   float_of_int !my /. float_of_int l)*)
		(float_of_int !mx /. float_of_int !cnt,
		 float_of_int !my /. float_of_int !cnt)
	      in
	      let ps = ref [] in
	      let d = 4 in
	      let () =
		Array.iter
		  (fun (x, y) ->
		     let b = ref true in
		     let dist =
		       let x = float_of_int x
		       and y = float_of_int y in
			 sqrt (sqr (x -. mx) +. sqr (y -. my))
		     in
		     let s = ref 0 in
		     let t = ref 0 in
		       for y' = y - d to y + d do
			 for x' = x - d to x + d do
			   incr t;
			   if x' >= 0 && y' >= 0 && x' < w && y' < h then (
			     let dist' =
			       let x = float_of_int x'
			       and y = float_of_int y' in
				 sqrt (sqr (x -. mx) +. sqr (y -. my))
			     in
			       if cand.(y').(x')
			       then b := false;
			       if used.(y').(x') = !idx
			       then incr s;
			       if used.(y').(x') = !idx && dist' > dist
			       then b := false;
			   )
			 done
		       done;
		       if !b && float_of_int !s *. 3.5 < float_of_int !t then (
			 cand.(y).(x) <- true;
			 ps := (x, y) :: !ps;
			 (*Printf.printf "asd %d %d  %d %d\n" x y !s !t;*)
		       )
		  ) border
	      in
		rays := (List.length !ps) :: !rays;
		incr res;
		(*let d =
		  Array.map
		    (fun (x, y) ->
		       let x = float_of_int x
		       and y = float_of_int y in
			 sqrt (sqr (x -. mx) +. sqr (y -. my))
		    ) border
		in
		let r =
		  let r = ref 0.0 in
		    for i = 0 to l - 1 do
		      r := !r +. d.(i)
		    done;
		    !r /. float_of_int l
		in
		let sr =
		  let sr = ref 0.0 in
		    for i = 0 to l - 1 do
		      sr := !sr +. sqr (d.(i) -. r)
		    done;
		    !sr /. float_of_int l
		in
		  if sr /. r < 0.03
		  then incr resc
		  else incr ress;
		  (*Printf.printf "asd %f %f %f %f\t%f  %d\n"
		    mx my r sr (sr /. r) l*)
		*)
	    )
	)
      done
    done;
    let rays = List.sort compare !rays in
      Printf.printf "%d\n" !res;
      List.iter (Printf.printf "%d ") rays;
      Printf.printf "\n"

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

let _ =
  let sqr x = x *. x in
  let h = getnum () in
  let w = getnum () in
  let a = Array.make_matrix h w 0 in
  let () =
    for i = 0 to h - 1 do
      for j = 0 to w - 1 do
	a.(i).(j) <- getnum ()
      done
    done
  in
  let cand = Array.make_matrix h w false in
  let res = ref 0 in
  let rays = ref [] in
  let used = Array.make_matrix h w false in
  let stack = Array.make (h * w) (0, 0) in
  let pos = ref 0 in
  (*let a' = Array.make_matrix n n 0 in
    for i = 0 to n - 1 do
      for j = 0 to n - 1 do
	let b = ref 0 in
	  for i' = -f to f do
	    for j' = -f to f do
	      let x = j + j'
	      and y = i + i' in
		if x >= 0 && y >= 0 && x < n && y < n
		then b := !b + a.(y).(x)
	    done
	  done;
	  if float_of_int !b > float_of_int ((2 * f + 1) * (2 * f + 1)) *. 0.4
	  then a'.(i).(j) <- 1
      done
    done;
    for i = 0 to n - 1 do
      a.(i) <- a'.(i)
    done;*)
    for i = 0 to h - 1 do
      for j = 0 to w - 1 do
	if not used.(i).(j) then (
	  if a.(i).(j) <> 0 then (
	    let d = [| (1, 0); (0, 1); (-1, 0); (0, -1) |] in
	    let border = ref [] in
	    let rec dfs () =
	      if !pos > 0 then (
		decr pos;
		let (x, y) = stack.(!pos) in
		  for i = 0 to 3 do
		    let (dx, dy) = d.(i) in
		    let x = x + dx
		    and y = y + dy in
		      if x >= 0 && y >= 0 && x < w && y < h &&
			a.(y).(x) > 0
		      then (
			if not used.(y).(x) then (
			  stack.(!pos) <- (x, y);
			  incr pos;
			  used.(y).(x) <- true;
			)
		      ) else (
			border := (x, y) :: !border
		      )
		  done;
		  dfs ()
	      )
	    in
	      pos := 0;
	      stack.(!pos) <- (j, i);
	      incr pos;
	      used.(i).(j) <- true;
	      dfs ();
	      let border = Array.of_list !border in
	      let l = Array.length border in
	      let (mx, my) =
		let mx = ref 0 in
		let my = ref 0 in
		  for i = 0 to l - 1 do
		    let (x, y) = border.(i) in
		      mx := !mx + x;
		      my := !my + y;
		  done;
		  (float_of_int !mx /. float_of_int l,
		   float_of_int !my /. float_of_int l)
	      in
	      let ps = ref [] in
	      let d = 5 in
	      let () =
		Array.iter
		  (fun (x, y) ->
		     let b = ref true in
		     let dist =
		       let x = float_of_int x
		       and y = float_of_int y in
			 sqrt (sqr (x -. mx) +. sqr (y -. my))
		     in
		     let s = ref 0 in
		     let t = ref 0 in
		       for y' = y - d to y + d do
			 for x' = x - d to x + d do
			   incr t;
			   if x' >= 0 && y' >= 0 && x' < w && y' < h then (
			     let dist' =
			       let x = float_of_int x'
			       and y = float_of_int y' in
				 sqrt (sqr (x -. mx) +. sqr (y -. my))
			     in
			       if cand.(y').(x')
			       then b := false;
			       if a.(y').(x') <> 0
			       then incr s;
			       if a.(y').(x') <> 0 && dist' > dist
			       then b := false;
			   )
			 done
		       done;
		       if !b && !s * 3 < !t then (
			 cand.(y).(x) <- true;
			 ps := (x, y) :: !ps;
			 (*Printf.printf "asd %d %d  %d %d\n" x y !s !t;*)
		       )
		  ) border
	      in
		rays := (List.length !ps) :: !rays;
		incr res;
		(*let d =
		  Array.map
		    (fun (x, y) ->
		       let x = float_of_int x
		       and y = float_of_int y in
			 sqrt (sqr (x -. mx) +. sqr (y -. my))
		    ) border
		in
		let r =
		  let r = ref 0.0 in
		    for i = 0 to l - 1 do
		      r := !r +. d.(i)
		    done;
		    !r /. float_of_int l
		in
		let sr =
		  let sr = ref 0.0 in
		    for i = 0 to l - 1 do
		      sr := !sr +. sqr (d.(i) -. r)
		    done;
		    !sr /. float_of_int l
		in
		  if sr /. r < 0.03
		  then incr resc
		  else incr ress;
		  (*Printf.printf "asd %f %f %f %f\t%f  %d\n"
		    mx my r sr (sr /. r) l*)
		*)
	    )
	)
      done
    done;
    let rays = List.sort compare !rays in
      Printf.printf "%d\n" !res;
      List.iter (Printf.printf "%d ") rays;
      Printf.printf "\n"

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

let _ =
  let sqr x = x *. x in
  let h = getnum () in
  let w = getnum () in
  let a = Array.make_matrix h w 0 in
  let () =
    for i = 0 to h - 1 do
      for j = 0 to w - 1 do
	a.(i).(j) <- getnum ()
      done
    done
  in
  let cand = Array.make_matrix h w false in
  let res = ref 0 in
  let rays = ref [] in
  let used = Array.make_matrix h w 0 in
  let stack = Array.make (h * w) (0, 0) in
  let pos = ref 0 in
  let idx = ref 0 in
  (*let a' = Array.make_matrix n n 0 in
    for i = 0 to n - 1 do
      for j = 0 to n - 1 do
	let b = ref 0 in
	  for i' = -f to f do
	    for j' = -f to f do
	      let x = j + j'
	      and y = i + i' in
		if x >= 0 && y >= 0 && x < n && y < n
		then b := !b + a.(y).(x)
	    done
	  done;
	  if float_of_int !b > float_of_int ((2 * f + 1) * (2 * f + 1)) *. 0.4
	  then a'.(i).(j) <- 1
      done
    done;
    for i = 0 to n - 1 do
      a.(i) <- a'.(i)
    done;*)
    for i = 0 to h - 1 do
      for j = 0 to w - 1 do
	if used.(i).(j) = 0 then (
	  if a.(i).(j) <> 0 then (
	    let d = [| (1, 0); (0, 1); (-1, 0); (0, -1) |] in
	    let border = ref [] in
	    let mx = ref 0 in
	    let my = ref 0 in
	    let cnt = ref 0 in
	    let rec dfs () =
	      if !pos > 0 then (
		decr pos;
		let (x, y) = stack.(!pos) in
		  mx := !mx + x;
		  my := !my + y;
		  incr cnt;
		  for i = 0 to 3 do
		    let (dx, dy) = d.(i) in
		    let x = x + dx
		    and y = y + dy in
		      if x >= 0 && y >= 0 && x < w && y < h &&
			a.(y).(x) > 0
		      then (
			if used.(y).(x) = 0 then (
			  stack.(!pos) <- (x, y);
			  incr pos;
			  used.(y).(x) <- !idx;
			)
		      ) else (
			border := (x, y) :: !border
		      )
		  done;
		  dfs ()
	      )
	    in
	      incr idx;
	      pos := 0;
	      stack.(!pos) <- (j, i);
	      incr pos;
	      used.(i).(j) <- !idx;
	      dfs ();
	      let border = Array.of_list !border in
	      let l = Array.length border in
	      let (mx, my) =
		(*let mx = ref 0 in
		let my = ref 0 in
		  for i = 0 to l - 1 do
		    let (x, y) = border.(i) in
		      mx := !mx + x;
		      my := !my + y;
		  done;
		  (float_of_int !mx /. float_of_int l,
		   float_of_int !my /. float_of_int l)*)
		(float_of_int !mx /. float_of_int !cnt,
		 float_of_int !my /. float_of_int !cnt)
	      in
	      let ps = ref [] in
	      let d = 5 in
	      let () =
		Array.iter
		  (fun (x, y) ->
		     let b = ref true in
		     let dist =
		       let x = float_of_int x
		       and y = float_of_int y in
			 sqrt (sqr (x -. mx) +. sqr (y -. my))
		     in
		     let s = ref 0 in
		     let t = ref 0 in
		       for y' = y - d to y + d do
			 for x' = x - d to x + d do
			   incr t;
			   if x' >= 0 && y' >= 0 && x' < w && y' < h then (
			     let dist' =
			       let x = float_of_int x'
			       and y = float_of_int y' in
				 sqrt (sqr (x -. mx) +. sqr (y -. my))
			     in
			       if cand.(y').(x')
			       then b := false;
			       if used.(y').(x') = !idx
			       then incr s;
			       if used.(y').(x') = !idx && dist' > dist
			       then b := false;
			   )
			 done
		       done;
		       if !b && !s * 4 < !t then (
			 cand.(y).(x) <- true;
			 ps := (x, y) :: !ps;
			 (*Printf.printf "asd %d %d  %d %d\n" x y !s !t;*)
		       )
		  ) border
	      in
		rays := (List.length !ps) :: !rays;
		incr res;
		(*let d =
		  Array.map
		    (fun (x, y) ->
		       let x = float_of_int x
		       and y = float_of_int y in
			 sqrt (sqr (x -. mx) +. sqr (y -. my))
		    ) border
		in
		let r =
		  let r = ref 0.0 in
		    for i = 0 to l - 1 do
		      r := !r +. d.(i)
		    done;
		    !r /. float_of_int l
		in
		let sr =
		  let sr = ref 0.0 in
		    for i = 0 to l - 1 do
		      sr := !sr +. sqr (d.(i) -. r)
		    done;
		    !sr /. float_of_int l
		in
		  if sr /. r < 0.03
		  then incr resc
		  else incr ress;
		  (*Printf.printf "asd %f %f %f %f\t%f  %d\n"
		    mx my r sr (sr /. r) l*)
		*)
	    )
	)
      done
    done;
    let rays = List.sort compare !rays in
      Printf.printf "%d\n" !res;
      List.iter (Printf.printf "%d ") rays;
      Printf.printf "\n"

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

let _ =
  let sqr x = x *. x in
  let h = getnum () in
  let w = getnum () in
  let a = Array.make_matrix h w 0 in
  let () =
    for i = 0 to h - 1 do
      for j = 0 to w - 1 do
	a.(i).(j) <- getnum ()
      done
    done
  in
  let cand = Array.make_matrix h w false in
  let res = ref 0 in
  let rays = ref [] in
  let used = Array.make_matrix h w 0 in
  let stack = Array.make (h * w) (0, 0) in
  let pos = ref 0 in
  let idx = ref 0 in
  (*let a' = Array.make_matrix n n 0 in
    for i = 0 to n - 1 do
      for j = 0 to n - 1 do
	let b = ref 0 in
	  for i' = -f to f do
	    for j' = -f to f do
	      let x = j + j'
	      and y = i + i' in
		if x >= 0 && y >= 0 && x < n && y < n
		then b := !b + a.(y).(x)
	    done
	  done;
	  if float_of_int !b > float_of_int ((2 * f + 1) * (2 * f + 1)) *. 0.4
	  then a'.(i).(j) <- 1
      done
    done;
    for i = 0 to n - 1 do
      a.(i) <- a'.(i)
    done;*)
    for i = 0 to h - 1 do
      for j = 0 to w - 1 do
	if used.(i).(j) = 0 then (
	  if a.(i).(j) <> 0 then (
	    let d = [| (1, 0); (0, 1); (-1, 0); (0, -1) |] in
	    let border = ref [] in
	    let mx = ref 0 in
	    let my = ref 0 in
	    let cnt = ref 0 in
	    let rec dfs () =
	      if !pos > 0 then (
		decr pos;
		let (x, y) = stack.(!pos) in
		  mx := !mx + x;
		  my := !my + y;
		  incr cnt;
		  for i = 0 to 3 do
		    let (dx, dy) = d.(i) in
		    let x = x + dx
		    and y = y + dy in
		      if x >= 0 && y >= 0 && x < w && y < h &&
			a.(y).(x) > 0
		      then (
			if used.(y).(x) = 0 then (
			  stack.(!pos) <- (x, y);
			  incr pos;
			  used.(y).(x) <- !idx;
			)
		      ) else (
			border := (x, y) :: !border
		      )
		  done;
		  dfs ()
	      )
	    in
	      incr idx;
	      pos := 0;
	      stack.(!pos) <- (j, i);
	      incr pos;
	      used.(i).(j) <- !idx;
	      dfs ();
	      let border = Array.of_list !border in
	      let l = Array.length border in
	      let (mx, my) =
		(*let mx = ref 0 in
		let my = ref 0 in
		  for i = 0 to l - 1 do
		    let (x, y) = border.(i) in
		      mx := !mx + x;
		      my := !my + y;
		  done;
		  (float_of_int !mx /. float_of_int l,
		   float_of_int !my /. float_of_int l)*)
		(float_of_int !mx /. float_of_int !cnt,
		 float_of_int !my /. float_of_int !cnt)
	      in
	      let ps = ref [] in
	      let d = 3 in
	      let () =
		Array.iter
		  (fun (x, y) ->
		     let b = ref true in
		     let dist =
		       let x = float_of_int x
		       and y = float_of_int y in
			 sqrt (sqr (x -. mx) +. sqr (y -. my))
		     in
		     let s = ref 0 in
		     let t = ref 0 in
		       for y' = y - d to y + d do
			 for x' = x - d to x + d do
			   incr t;
			   if x' >= 0 && y' >= 0 && x' < w && y' < h then (
			     let dist' =
			       let x = float_of_int x'
			       and y = float_of_int y' in
				 sqrt (sqr (x -. mx) +. sqr (y -. my))
			     in
			       if cand.(y').(x')
			       then b := false;
			       if used.(y').(x') = !idx
			       then incr s;
			       if used.(y').(x') = !idx && dist' > dist
			       then b := false;
			   )
			 done
		       done;
		       if !b && !s * 4 < !t then (
			 cand.(y).(x) <- true;
			 ps := (x, y) :: !ps;
			 (*Printf.printf "asd %d %d  %d %d\n" x y !s !t;*)
		       )
		  ) border
	      in
		rays := (List.length !ps) :: !rays;
		incr res;
		(*let d =
		  Array.map
		    (fun (x, y) ->
		       let x = float_of_int x
		       and y = float_of_int y in
			 sqrt (sqr (x -. mx) +. sqr (y -. my))
		    ) border
		in
		let r =
		  let r = ref 0.0 in
		    for i = 0 to l - 1 do
		      r := !r +. d.(i)
		    done;
		    !r /. float_of_int l
		in
		let sr =
		  let sr = ref 0.0 in
		    for i = 0 to l - 1 do
		      sr := !sr +. sqr (d.(i) -. r)
		    done;
		    !sr /. float_of_int l
		in
		  if sr /. r < 0.03
		  then incr resc
		  else incr ress;
		  (*Printf.printf "asd %f %f %f %f\t%f  %d\n"
		    mx my r sr (sr /. r) l*)
		*)
	    )
	)
      done
    done;
    let rays = List.sort compare !rays in
      Printf.printf "%d\n" !res;
      List.iter (Printf.printf "%d ") rays;
      Printf.printf "\n"

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

let _ =
  let sqr x = x *. x in
  let h = getnum () in
  let w = getnum () in
  let a = Array.make_matrix h w 0 in
  let () =
    for i = 0 to h - 1 do
      for j = 0 to w - 1 do
	a.(i).(j) <- getnum ()
      done
    done
  in
  let cand = Array.make_matrix h w false in
  let res = ref 0 in
  let rays = ref [] in
  let used = Array.make_matrix h w false in
  let stack = Array.make (h * w) (0, 0) in
  let pos = ref 0 in
  (*let a' = Array.make_matrix n n 0 in
    for i = 0 to n - 1 do
      for j = 0 to n - 1 do
	let b = ref 0 in
	  for i' = -f to f do
	    for j' = -f to f do
	      let x = j + j'
	      and y = i + i' in
		if x >= 0 && y >= 0 && x < n && y < n
		then b := !b + a.(y).(x)
	    done
	  done;
	  if float_of_int !b > float_of_int ((2 * f + 1) * (2 * f + 1)) *. 0.4
	  then a'.(i).(j) <- 1
      done
    done;
    for i = 0 to n - 1 do
      a.(i) <- a'.(i)
    done;*)
    for i = 0 to h - 1 do
      for j = 0 to w - 1 do
	if not used.(i).(j) then (
	  if a.(i).(j) <> 0 then (
	    let d = [| (1, 0); (0, 1); (-1, 0); (0, -1) |] in
	    let border = ref [] in
	    let mx = ref 0 in
	    let my = ref 0 in
	    let cnt = ref 0 in
	    let rec dfs () =
	      if !pos > 0 then (
		decr pos;
		let (x, y) = stack.(!pos) in
		  mx := !mx + x;
		  my := !my + y;
		  incr cnt;
		  for i = 0 to 3 do
		    let (dx, dy) = d.(i) in
		    let x = x + dx
		    and y = y + dy in
		      if x >= 0 && y >= 0 && x < w && y < h &&
			a.(y).(x) > 0
		      then (
			if not used.(y).(x) then (
			  stack.(!pos) <- (x, y);
			  incr pos;
			  used.(y).(x) <- true;
			)
		      ) else (
			border := (x, y) :: !border
		      )
		  done;
		  dfs ()
	      )
	    in
	      pos := 0;
	      stack.(!pos) <- (j, i);
	      incr pos;
	      used.(i).(j) <- true;
	      dfs ();
	      let border = Array.of_list !border in
	      let l = Array.length border in
	      let (mx, my) =
		(*let mx = ref 0 in
		let my = ref 0 in
		  for i = 0 to l - 1 do
		    let (x, y) = border.(i) in
		      mx := !mx + x;
		      my := !my + y;
		  done;
		  (float_of_int !mx /. float_of_int l,
		   float_of_int !my /. float_of_int l)*)
		(float_of_int !mx /. float_of_int !cnt,
		 float_of_int !my /. float_of_int !cnt)
	      in
	      let ps = ref [] in
	      let d = 5 in
	      let () =
		Array.iter
		  (fun (x, y) ->
		     let b = ref true in
		     let dist =
		       let x = float_of_int x
		       and y = float_of_int y in
			 sqrt (sqr (x -. mx) +. sqr (y -. my))
		     in
		     let s = ref 0 in
		     let t = ref 0 in
		       for y' = y - d to y + d do
			 for x' = x - d to x + d do
			   incr t;
			   if x' >= 0 && y' >= 0 && x' < w && y' < h then (
			     let dist' =
			       let x = float_of_int x'
			       and y = float_of_int y' in
				 sqrt (sqr (x -. mx) +. sqr (y -. my))
			     in
			       if cand.(y').(x')
			       then b := false;
			       if a.(y').(x') <> 0
			       then incr s;
			       if a.(y').(x') <> 0 && dist' > dist
			       then b := false;
			   )
			 done
		       done;
		       if !b && !s * 4 < !t then (
			 cand.(y).(x) <- true;
			 ps := (x, y) :: !ps;
			 (*Printf.printf "asd %d %d  %d %d\n" x y !s !t;*)
		       )
		  ) border
	      in
		rays := (List.length !ps) :: !rays;
		incr res;
		(*let d =
		  Array.map
		    (fun (x, y) ->
		       let x = float_of_int x
		       and y = float_of_int y in
			 sqrt (sqr (x -. mx) +. sqr (y -. my))
		    ) border
		in
		let r =
		  let r = ref 0.0 in
		    for i = 0 to l - 1 do
		      r := !r +. d.(i)
		    done;
		    !r /. float_of_int l
		in
		let sr =
		  let sr = ref 0.0 in
		    for i = 0 to l - 1 do
		      sr := !sr +. sqr (d.(i) -. r)
		    done;
		    !sr /. float_of_int l
		in
		  if sr /. r < 0.03
		  then incr resc
		  else incr ress;
		  (*Printf.printf "asd %f %f %f %f\t%f  %d\n"
		    mx my r sr (sr /. r) l*)
		*)
	    )
	)
      done
    done;
    let rays = List.sort compare !rays in
      Printf.printf "%d\n" !res;
      List.iter (Printf.printf "%d ") rays;
      Printf.printf "\n"

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

let _ =
  let sqr x = x *. x in
  let h = getnum () in
  let w = getnum () in
  let a = Array.make_matrix h w 0 in
  let () =
    for i = 0 to h - 1 do
      for j = 0 to w - 1 do
	a.(i).(j) <- getnum ()
      done
    done
  in
  let cand = Array.make_matrix h w false in
  let res = ref 0 in
  let rays = ref [] in
  let used = Array.make_matrix h w false in
  let stack = Array.make (h * w) (0, 0) in
  let pos = ref 0 in
  (*let a' = Array.make_matrix n n 0 in
    for i = 0 to n - 1 do
      for j = 0 to n - 1 do
	let b = ref 0 in
	  for i' = -f to f do
	    for j' = -f to f do
	      let x = j + j'
	      and y = i + i' in
		if x >= 0 && y >= 0 && x < n && y < n
		then b := !b + a.(y).(x)
	    done
	  done;
	  if float_of_int !b > float_of_int ((2 * f + 1) * (2 * f + 1)) *. 0.4
	  then a'.(i).(j) <- 1
      done
    done;
    for i = 0 to n - 1 do
      a.(i) <- a'.(i)
    done;*)
    for i = 0 to h - 1 do
      for j = 0 to w - 1 do
	if not used.(i).(j) then (
	  if a.(i).(j) <> 0 then (
	    let d = [| (1, 0); (0, 1); (-1, 0); (0, -1) |] in
	    let border = ref [] in
	    let mx = ref 0 in
	    let my = ref 0 in
	    let cnt = ref 0 in
	    let rec dfs () =
	      if !pos > 0 then (
		decr pos;
		let (x, y) = stack.(!pos) in
		  mx := !mx + x;
		  my := !my + y;
		  incr cnt;
		  for i = 0 to 3 do
		    let (dx, dy) = d.(i) in
		    let x = x + dx
		    and y = y + dy in
		      if x >= 0 && y >= 0 && x < w && y < h &&
			a.(y).(x) > 0
		      then (
			if not used.(y).(x) then (
			  stack.(!pos) <- (x, y);
			  incr pos;
			  used.(y).(x) <- true;
			)
		      ) else (
			border := (x, y) :: !border
		      )
		  done;
		  dfs ()
	      )
	    in
	      pos := 0;
	      stack.(!pos) <- (j, i);
	      incr pos;
	      used.(i).(j) <- true;
	      dfs ();
	      let border = Array.of_list !border in
	      let l = Array.length border in
	      let (mx, my) =
		(*let mx = ref 0 in
		let my = ref 0 in
		  for i = 0 to l - 1 do
		    let (x, y) = border.(i) in
		      mx := !mx + x;
		      my := !my + y;
		  done;
		  (float_of_int !mx /. float_of_int l,
		   float_of_int !my /. float_of_int l)*)
		(float_of_int !mx /. float_of_int !cnt,
		 float_of_int !my /. float_of_int !cnt)
	      in
	      let ps = ref [] in
	      let d = 5 in
	      let () =
		Array.iter
		  (fun (x, y) ->
		     let b = ref true in
		     let dist =
		       let x = float_of_int x
		       and y = float_of_int y in
			 sqrt (sqr (x -. mx) +. sqr (y -. my))
		     in
		     let s = ref 0 in
		     let t = ref 0 in
		       for y' = y - d to y + d do
			 for x' = x - d to x + d do
			   incr t;
			   if x' >= 0 && y' >= 0 && x' < w && y' < h then (
			     let dist' =
			       let x = float_of_int x'
			       and y = float_of_int y' in
				 sqrt (sqr (x -. mx) +. sqr (y -. my))
			     in
			       if cand.(y').(x')
			       then b := false;
			       if a.(y').(x') <> 0
			       then incr s;
			       if a.(y').(x') <> 0 && dist' > dist
			       then b := false;
			   )
			 done
		       done;
		       if !b && !s * 3 < !t then (
			 cand.(y).(x) <- true;
			 ps := (x, y) :: !ps;
			 (*Printf.printf "asd %d %d  %d %d\n" x y !s !t;*)
		       )
		  ) border
	      in
		rays := (List.length !ps) :: !rays;
		incr res;
		(*let d =
		  Array.map
		    (fun (x, y) ->
		       let x = float_of_int x
		       and y = float_of_int y in
			 sqrt (sqr (x -. mx) +. sqr (y -. my))
		    ) border
		in
		let r =
		  let r = ref 0.0 in
		    for i = 0 to l - 1 do
		      r := !r +. d.(i)
		    done;
		    !r /. float_of_int l
		in
		let sr =
		  let sr = ref 0.0 in
		    for i = 0 to l - 1 do
		      sr := !sr +. sqr (d.(i) -. r)
		    done;
		    !sr /. float_of_int l
		in
		  if sr /. r < 0.03
		  then incr resc
		  else incr ress;
		  (*Printf.printf "asd %f %f %f %f\t%f  %d\n"
		    mx my r sr (sr /. r) l*)
		*)
	    )
	)
      done
    done;
    let rays = List.sort compare !rays in
      Printf.printf "%d\n" !res;
      List.iter (Printf.printf "%d ") rays;
      Printf.printf "\n"

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

let _ =
  let sqr x = x *. x in
  let h = getnum () in
  let w = getnum () in
  let a = Array.make_matrix h w 0 in
  let () =
    for i = 0 to h - 1 do
      for j = 0 to w - 1 do
	a.(i).(j) <- getnum ()
      done
    done
  in
  let cand = Array.make_matrix h w false in
  let res = ref 0 in
  let rays = ref [] in
  let used = Array.make_matrix h w false in
  let stack = Array.make (h * w) (0, 0) in
  let pos = ref 0 in
  (*let a' = Array.make_matrix n n 0 in
    for i = 0 to n - 1 do
      for j = 0 to n - 1 do
	let b = ref 0 in
	  for i' = -f to f do
	    for j' = -f to f do
	      let x = j + j'
	      and y = i + i' in
		if x >= 0 && y >= 0 && x < n && y < n
		then b := !b + a.(y).(x)
	    done
	  done;
	  if float_of_int !b > float_of_int ((2 * f + 1) * (2 * f + 1)) *. 0.4
	  then a'.(i).(j) <- 1
      done
    done;
    for i = 0 to n - 1 do
      a.(i) <- a'.(i)
    done;*)
    for i = 0 to h - 1 do
      for j = 0 to w - 1 do
	if not used.(i).(j) then (
	  if a.(i).(j) <> 0 then (
	    let d = [| (1, 0); (0, 1); (-1, 0); (0, -1) |] in
	    let border = ref [] in
	    let mx = ref 0 in
	    let my = ref 0 in
	    let cnt = ref 0 in
	    let rec dfs () =
	      if !pos > 0 then (
		decr pos;
		let (x, y) = stack.(!pos) in
		  mx := !mx + x;
		  my := !my + y;
		  incr cnt;
		  for i = 0 to 3 do
		    let (dx, dy) = d.(i) in
		    let x = x + dx
		    and y = y + dy in
		      if x >= 0 && y >= 0 && x < w && y < h &&
			a.(y).(x) > 0
		      then (
			if not used.(y).(x) then (
			  stack.(!pos) <- (x, y);
			  incr pos;
			  used.(y).(x) <- true;
			)
		      ) else (
			border := (x, y) :: !border
		      )
		  done;
		  dfs ()
	      )
	    in
	      pos := 0;
	      stack.(!pos) <- (j, i);
	      incr pos;
	      used.(i).(j) <- true;
	      dfs ();
	      let border = Array.of_list !border in
	      let l = Array.length border in
	      let (mx, my) =
		(*let mx = ref 0 in
		let my = ref 0 in
		  for i = 0 to l - 1 do
		    let (x, y) = border.(i) in
		      mx := !mx + x;
		      my := !my + y;
		  done;
		  (float_of_int !mx /. float_of_int l,
		   float_of_int !my /. float_of_int l)*)
		(float_of_int !mx /. float_of_int !cnt,
		 float_of_int !my /. float_of_int !cnt)
	      in
	      let ps = ref [] in
	      let d = 6 in
	      let () =
		Array.iter
		  (fun (x, y) ->
		     let b = ref true in
		     let dist =
		       let x = float_of_int x
		       and y = float_of_int y in
			 sqrt (sqr (x -. mx) +. sqr (y -. my))
		     in
		     let s = ref 0 in
		     let t = ref 0 in
		       for y' = y - d to y + d do
			 for x' = x - d to x + d do
			   incr t;
			   if x' >= 0 && y' >= 0 && x' < w && y' < h then (
			     let dist' =
			       let x = float_of_int x'
			       and y = float_of_int y' in
				 sqrt (sqr (x -. mx) +. sqr (y -. my))
			     in
			       if cand.(y').(x')
			       then b := false;
			       if a.(y').(x') <> 0
			       then incr s;
			       if a.(y').(x') <> 0 && dist' > dist
			       then b := false;
			   )
			 done
		       done;
		       if !b && !s * 4 < !t then (
			 cand.(y).(x) <- true;
			 ps := (x, y) :: !ps;
			 (*Printf.printf "asd %d %d  %d %d\n" x y !s !t;*)
		       )
		  ) border
	      in
		rays := (List.length !ps) :: !rays;
		incr res;
		(*let d =
		  Array.map
		    (fun (x, y) ->
		       let x = float_of_int x
		       and y = float_of_int y in
			 sqrt (sqr (x -. mx) +. sqr (y -. my))
		    ) border
		in
		let r =
		  let r = ref 0.0 in
		    for i = 0 to l - 1 do
		      r := !r +. d.(i)
		    done;
		    !r /. float_of_int l
		in
		let sr =
		  let sr = ref 0.0 in
		    for i = 0 to l - 1 do
		      sr := !sr +. sqr (d.(i) -. r)
		    done;
		    !sr /. float_of_int l
		in
		  if sr /. r < 0.03
		  then incr resc
		  else incr ress;
		  (*Printf.printf "asd %f %f %f %f\t%f  %d\n"
		    mx my r sr (sr /. r) l*)
		*)
	    )
	)
      done
    done;
    let rays = List.sort compare !rays in
      Printf.printf "%d\n" !res;
      List.iter (Printf.printf "%d ") rays;
      Printf.printf "\n"

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

let _ =
  let sqr x = x *. x in
  let h = getnum () in
  let w = getnum () in
  let a = Array.make_matrix h w 0 in
  let () =
    for i = 0 to h - 1 do
      for j = 0 to w - 1 do
	a.(i).(j) <- getnum ()
      done
    done
  in
  let cand = Array.make_matrix h w false in
  let res = ref 0 in
  let rays = ref [] in
  let used = Array.make_matrix h w 0 in
  let stack = Array.make (h * w) (0, 0) in
  let pos = ref 0 in
  let idx = ref 0 in
  (*let a' = Array.make_matrix n n 0 in
    for i = 0 to n - 1 do
      for j = 0 to n - 1 do
	let b = ref 0 in
	  for i' = -f to f do
	    for j' = -f to f do
	      let x = j + j'
	      and y = i + i' in
		if x >= 0 && y >= 0 && x < n && y < n
		then b := !b + a.(y).(x)
	    done
	  done;
	  if float_of_int !b > float_of_int ((2 * f + 1) * (2 * f + 1)) *. 0.4
	  then a'.(i).(j) <- 1
      done
    done;
    for i = 0 to n - 1 do
      a.(i) <- a'.(i)
    done;*)
    for i = 0 to h - 1 do
      for j = 0 to w - 1 do
	if used.(i).(j) = 0 then (
	  if a.(i).(j) <> 0 then (
	    let d = [| (1, 0); (0, 1); (-1, 0); (0, -1) |] in
	    let border = ref [] in
	    let mx = ref 0 in
	    let my = ref 0 in
	    let cnt = ref 0 in
	    let rec dfs () =
	      if !pos > 0 then (
		decr pos;
		let (x, y) = stack.(!pos) in
		  mx := !mx + x;
		  my := !my + y;
		  incr cnt;
		  for i = 0 to 3 do
		    let (dx, dy) = d.(i) in
		    let x = x + dx
		    and y = y + dy in
		      if x >= 0 && y >= 0 && x < w && y < h &&
			a.(y).(x) > 0
		      then (
			if used.(y).(x) = 0 then (
			  stack.(!pos) <- (x, y);
			  incr pos;
			  used.(y).(x) <- !idx;
			)
		      ) else (
			border := (x, y) :: !border
		      )
		  done;
		  dfs ()
	      )
	    in
	      incr idx;
	      pos := 0;
	      stack.(!pos) <- (j, i);
	      incr pos;
	      used.(i).(j) <- !idx;
	      dfs ();
	      let border = Array.of_list !border in
	      let l = Array.length border in
	      let (mx, my) =
		(*let mx = ref 0 in
		let my = ref 0 in
		  for i = 0 to l - 1 do
		    let (x, y) = border.(i) in
		      mx := !mx + x;
		      my := !my + y;
		  done;
		  (float_of_int !mx /. float_of_int l,
		   float_of_int !my /. float_of_int l)*)
		(float_of_int !mx /. float_of_int !cnt,
		 float_of_int !my /. float_of_int !cnt)
	      in
	      let ps = ref [] in
	      let d = 6 in
	      let () =
		Array.iter
		  (fun (x, y) ->
		     let b = ref true in
		     let dist =
		       let x = float_of_int x
		       and y = float_of_int y in
			 sqrt (sqr (x -. mx) +. sqr (y -. my))
		     in
		     let s = ref 0 in
		     let t = ref 0 in
		       for y' = y - d to y + d do
			 for x' = x - d to x + d do
			   incr t;
			   if x' >= 0 && y' >= 0 && x' < w && y' < h then (
			     let dist' =
			       let x = float_of_int x'
			       and y = float_of_int y' in
				 sqrt (sqr (x -. mx) +. sqr (y -. my))
			     in
			       if cand.(y').(x')
			       then b := false;
			       if used.(y').(x') = !idx
			       then incr s;
			       if used.(y').(x') = !idx && dist' > dist
			       then b := false;
			   )
			 done
		       done;
		       if !b && !s * 4 < !t then (
			 cand.(y).(x) <- true;
			 ps := (x, y) :: !ps;
			 (*Printf.printf "asd %d %d  %d %d\n" x y !s !t;*)
		       )
		  ) border
	      in
		rays := (List.length !ps) :: !rays;
		incr res;
		(*let d =
		  Array.map
		    (fun (x, y) ->
		       let x = float_of_int x
		       and y = float_of_int y in
			 sqrt (sqr (x -. mx) +. sqr (y -. my))
		    ) border
		in
		let r =
		  let r = ref 0.0 in
		    for i = 0 to l - 1 do
		      r := !r +. d.(i)
		    done;
		    !r /. float_of_int l
		in
		let sr =
		  let sr = ref 0.0 in
		    for i = 0 to l - 1 do
		      sr := !sr +. sqr (d.(i) -. r)
		    done;
		    !sr /. float_of_int l
		in
		  if sr /. r < 0.03
		  then incr resc
		  else incr ress;
		  (*Printf.printf "asd %f %f %f %f\t%f  %d\n"
		    mx my r sr (sr /. r) l*)
		*)
	    )
	)
      done
    done;
    let rays = List.sort compare !rays in
      Printf.printf "%d\n" !res;
      List.iter (Printf.printf "%d ") rays;
      Printf.printf "\n"


The Super Duper Secret Meeting of the Super Duper Secret Military Squad takes place in a Super Duper Secret Place. The place is an infinite plane with introduced Cartesian coordinate system. The meeting table is represented as a rectangle whose sides are parallel to the coordinate axes and whose vertexes are located at the integer points of the plane. At each integer point which belongs to the table perimeter there is a chair in which a general sits.Some points on the plane contain radiators for the generals not to freeze in winter. Each radiator is characterized by the number ri — the radius of the area this radiator can heat. That is, if the distance between some general and the given radiator is less than or equal to ri, than the general feels comfortable and warm. Here distance is defined as Euclidean distance, so the distance between points (x1, y1) and (x2, y2) is Each general who is located outside the radiators' heating area can get sick. Thus, you should bring him a warm blanket. Your task is to count the number of warm blankets you should bring to the Super Duper Secret Place.The generals who are already comfortable do not need a blanket. Also the generals never overheat, ever if they are located in the heating area of several radiators. The radiators can be located at any integer points on the plane, even inside the rectangle (under the table) or on the perimeter (directly under some general). Even in this case their radius does not change.

The first input line contains coordinates of two opposite table corners xa, ya, xb, yb (xa ≠ xb, ya ≠ yb). The second line contains integer n — the number of radiators (1 ≤ n ≤ 103). Then n lines contain the heaters' coordinates as "xi yi ri", the numbers are separated by spaces. All input data numbers are integers. The absolute value of all coordinates does not exceed 1000, 1 ≤ ri ≤ 1000. Several radiators can be located at the same point.

Print the only number — the number of blankets you should bring.

NoteIn the first sample the generals are sitting at points: (2, 2), (2, 3), (2, 4), (2, 5), (3, 2), (3, 5), (4, 2), (4, 3), (4, 4), (4, 5). Among them, 4 generals are located outside the heating range. They are the generals at points: (2, 5), (3, 5), (4, 4), (4, 5).In the second sample the generals are sitting at points: (5, 2), (5, 3), (6, 2), (6, 3). All of them are located inside the heating range.

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

let () =
  let xa = read_int() in
  let ya = read_int() in
  let xb = read_int() in
  let yb = read_int() in
  let n = read_int() in
  let rad = Array.make n (0,0,0) in
  let () = for i=0 to n-1 do
    let x = read_int() in
    let y = read_int() in
    let r = read_int() in
    rad.(i) <- (x,y,r)
  done in

  let (xa,ya,xb,yb)= (min xa xb, min ya yb, max xa xb, max ya yb) in

  let sq x = x*x in

  let count = ref 0 in
    for x = xa to xb do
      for y = ya to yb do
	if x=xa || x=xb || y=ya || y=yb then
	  let rec gcov i = if i=n then false else
	    let (xr,yr,r) = rad.(i) in
	      if (sq r) >= (sq (x-xr)) + (sq (y-yr)) then true else gcov (i+1)
	  in
	    if not (gcov 0) then count := !count +1 else ()
	else ()
      done
    done;

    Printf.printf "%d\n" !count;


Many years ago Berland was a small country where only $$$n$$$ people lived. Each person had some savings: the $$$i$$$-th one had $$$a_i$$$ burles.The government considered a person as wealthy if he had at least $$$x$$$ burles. To increase the number of wealthy people Berland decided to carry out several reforms. Each reform looked like that:   the government chooses some subset of people (maybe all of them);  the government takes all savings from the chosen people and redistributes the savings among the chosen people equally. For example, consider the savings as list $$$[5, 1, 2, 1]$$$: if the government chose the $$$1$$$-st and the $$$3$$$-rd persons then it, at first, will take all $$$5 + 2 = 7$$$ burles and after that will return $$$3.5$$$ burles to the chosen people. As a result, the savings will become $$$[3.5, 1, 3.5, 1]$$$.A lot of data was lost from that time, so we don't know how many reforms were implemented and to whom. All we can do is ask you to calculate the maximum possible number of wealthy people after several (maybe zero) reforms.

The first line contains single integer $$$T$$$ ($$$1 \le T \le 1000$$$) — the number of test cases. Next $$$2T$$$ lines contain the test cases — two lines per test case. The first line contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 10^5$$$, $$$1 \le x \le 10^9$$$) — the number of people and the minimum amount of money to be considered as wealthy. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the initial savings of each person. It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$10^5$$$.

Print $$$T$$$ integers — one per test case. For each test case print the maximum possible number of wealthy people after several (maybe zero) reforms.

NoteThe first test case is described in the statement.In the second test case, the government, for example, could carry out two reforms: $$$[\underline{11}, \underline{9}, 11, 9] \rightarrow [10, 10, \underline{11}, \underline{9}] \rightarrow [10, 10, 10, 10]$$$.In the third test case, the government couldn't make even one person wealthy.In the fourth test case, the government could choose all people to carry out a reform: $$$[\underline{9}, \underline{4}, \underline{9}] \rightarrow [7\frac{1}{3}, 7\frac{1}{3}, 7\frac{1}{3}]$$$.

open Printf
open Scanf

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

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

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

  for case = 1 to cases do
    let n = read_int () in
    let x = read_long() in
    let a = Array.init n read_long in
    Array.sort (fun a b -> compare b a) a;
    let rec loop j ac =   (* ac = a0+a(j-1) *)
      if j=n then n else
	let ac = a.(j) ++ ac in (* ac = a0 + ... + aj *)
	if ac < x ** (long (j+1)) then j else loop (j+1) ac
    in
    printf "%d\n" (loop 0 0L)
  done


A new e-mail service "Berlandesk" is going to be opened in Berland in the near future. The site administration wants to launch their project as soon as possible, that's why they ask you to help. You're suggested to implement the prototype of site registration system. The system should work on the following principle. Each time a new user wants to register, he sends to the system a request with his name. If such a name does not exist in the system database, it is inserted into the database, and the user gets the response OK, confirming the successful registration. If the name already exists in the system database, the system makes up a new user name, sends it to the user as a prompt and also inserts the prompt into the database. The new name is formed by the following rule. Numbers, starting with 1, are appended one after another to name (name1, name2, ...), among these numbers the least i is found so that namei does not yet exist in the database.

The first line contains number n (1 ≤ n ≤ 105). The following n lines contain the requests to the system. Each request is a non-empty line, and consists of not more than 32 characters, which are all lowercase Latin letters.

Print n lines, which are system responses to the requests: OK in case of successful registration, or a prompt with a new name, if the requested name is already taken.

let acceptable = "OK\n"

let processName name table =
  if Hashtbl.mem table name
  then let currFreeNum = Hashtbl.find table name in
    begin
      Hashtbl.replace table name (currFreeNum + 1);
      Printf.sprintf "%s%d\n" name currFreeNum
    end
    else
      begin
        Hashtbl.add table name 1;
        acceptable
      end

let () =
  let numNames = Scanf.sscanf (read_line ()) "%d" (fun x -> x) in
  let table = Hashtbl.create numNames in
    for i = 1 to numNames do
      let currName = read_line () in
        print_string (processName currName table)
    done

let num = read_int () in
    let hbl = Hashtbl.create (num / 8) in
        for tot = 1 to num do
            let uc = read_line () in
            let i = try Hashtbl.find hbl uc with Not_found -> 0 in
            if i <> 0 then (print_string uc; print_int i; print_newline ()) else (print_endline "OK");
            Hashtbl.replace hbl uc (i + 1)
        done

open Hashtbl;;

let regs = read_int ();;

let users = Hashtbl.create 0;;

for i = 1 to regs do
	let name = read_line () in
	if Hashtbl.mem users name then begin
		let x = Hashtbl.find users name + 1 in
		let _ = print_string (name ^ (string_of_int x) ^ "\n") in
		Hashtbl.replace users name x

	end else begin
		Hashtbl.add users name 0;
		print_string "OK\n"
	end
done;

let () =
  let n = read_int () in
  let h = Hashtbl.create n in
  for i = 1 to n do
    let s = read_line () in
    let x = try Hashtbl.find h s with _ -> 0 in
    Hashtbl.replace h s (x+1);
    if x = 0 then
      print_endline "OK"
    else
      Printf.printf "%s%d\n" s x
  done

let n = read_int ();;                                                                                let users = Hashtbl.create 1000;;                                                                                for i = 1 to n do                                                                                let name = read_line () in                                                                                if Hashtbl.mem users name then begin                                                                                let _ = print_endline (name ^ (string_of_int (Hashtbl.find users name +1)) ^ "\n") in                                   Hashtbl.replace users name (Hashtbl.find users name+1)                                                          end                                                                                else begin                                                                                let _ = print_string "OK\n" in                                                                                Hashtbl.add users name 0                                                                                end                                                                                done;

open Printf
open String

module type DICTIONARY =
sig
  type key = string
  type 'a dict
  exception NotFound

  val make : unit -> 'a dict
  val insert : 'a dict -> key -> 'a -> 'a dict
  val lookup : 'a dict -> key -> 'a
  val map : ('a -> 'b) -> 'a dict -> 'b dict
end

(* implemented with association list *)
module AssocList : DICTIONARY =
struct
  type key = string
  type 'a dict = (key * 'a) list
  exception NotFound

  let make () : 'a dict = []
  let insert (d : 'a dict) (k : key) (x : 'a) : 'a dict = (k, x) :: d
  let rec lookup (d : 'a dict) (k : key) : 'a =
    match d with
      | [] -> raise NotFound
      | (k', x) :: rest ->
              if k = k' then x
              else lookup rest k
  let map (f : 'a -> 'b) (d : 'a dict) =
        List.map (fun (k, a) -> (k, f a)) d
end

(* implemented with higher-order function *)
module FunctionDict : DICTIONARY =
struct
    type key = string
    type 'a dict = string -> 'a
    exception NotFound

    let make () = fun _ -> raise NotFound
    let lookup (d : 'a dict) (key : string) : 'a = d key
    let insert (d : 'a dict) (k : key) (x : 'a) : 'a dict =
                fun k' -> if k = k' then x else d k'
    let map (f : 'a -> 'b) (d : 'a dict) = fun k -> f (d k)
end

(* a better association list verion *)
module SortedAssocList : DICTIONARY =
struct
  type key = string
  type 'a dict = (key * 'a) list
  exception NotFound

  let make() : 'a dict = []
  let rec insert (d : 'a dict) (k : key) (x : 'a) : 'a dict =
          match d with
          | [] -> (k, x) :: []
          | (k', x') :: rest ->
              match String.compare k k' with
                1 -> (k', x') :: (insert rest k x)
              | 0 -> (k, x) :: rest
              | -1 -> (k, x) :: (k', x') :: rest
              | _ -> failwith "Impossible"

  let rec lookup (d : 'a dict) (k : key) : 'a =
          match d with
            [] -> raise NotFound
          | (k', x) :: rest ->
              match String.compare k k' with
                0 -> x
              | -1 -> raise NotFound
              | 1 -> lookup rest k
              | _ -> failwith "Impossible"

  let map (f : 'a -> 'b) (d : 'a dict) =
           List.map (fun (k,a) -> (k, f a)) d
end

(* implemented with binary tree, logn loopup *)
module AssocTree : DICTIONARY =
struct
  type key = string
  type 'a dict = Empty | Node of key * 'a * 'a dict * 'a dict
  exception NotFound

  let make() : 'a dict = Empty

  let rec insert (d : 'a dict) (k : key) (x : 'a) : 'a dict =
          match d with
            Empty -> Node (k, x, Empty, Empty)
          | Node (k', x', l, r) ->
              match String.compare k k' with
                0 -> Node(k, x, l, r)
              | -1 -> Node(k', x', insert l k x, r)
              | 1 -> Node(k', x', l, insert r k x)
              | _ -> failwith "Impossible"

  let rec lookup (d : 'a dict) (k : key) : 'a =
          match d with
            Empty -> raise NotFound
          | Node(k', x, l, r) ->
              match String.compare k k' with
                0 -> x
              | -1 -> lookup l k
              | 1 -> lookup r k
              | _ -> failwith "Impossible"

  let rec map (f : 'a -> 'b) (d : 'a dict) =
          match d with
            Empty -> Empty
          | Node (k, x, l, r) -> Node (k, f x, map f l, map f r)
end

let tree = AssocTree.make ()

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

let myprint s i =
  let t = s^(string_of_int i) in
  printf "%s\n" t

let rec loop tr ii =
  if ii = 0 then
    ()
  else
    begin
      let open AssocTree in
      let s = Scanf.scanf "%s " (fun x -> x) in
      try
        let x = lookup tr s in
        myprint s (x+1); loop (insert tr s (x+1)) (ii-1)
      with
        NotFound -> printf "OK\n"; loop (insert tr s 0) (ii-1)
    end

 let () = loop tree n

let solve n =
  let tbl = Hashtbl.create 100000 in
  for i = 1 to n do
    let query = Scanf.scanf " %s" (fun x -> x) in
    if Hashtbl.mem tbl query
    then
      begin
	let v = Hashtbl.find tbl query in
	Hashtbl.add tbl query (v+1);
	Printf.printf "%s%d\n" query v;
      end
    else
      begin
	Hashtbl.add tbl query 1;
	Printf.printf "OK\n";
      end
  done

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


It's Petya's birthday party and his friends have presented him a brand new "Electrician-$$$n$$$" construction set, which they are sure he will enjoy as he always does with weird puzzles they give him.Construction set "Electrician-$$$n$$$" consists of $$$2n - 1$$$ wires and $$$2n$$$ light bulbs. Each bulb has its own unique index that is an integer from $$$1$$$ to $$$2n$$$, while all wires look the same and are indistinguishable. In order to complete this construction set one has to use each of the wires to connect two distinct bulbs. We define a chain in a completed construction set as a sequence of distinct bulbs of length at least two, such that every two consecutive bulbs in this sequence are directly connected by a wire. Completed construction set configuration is said to be correct if a resulting network of bulbs and wires has a tree structure, i.e. any two distinct bulbs are the endpoints of some chain.Petya was assembling different configurations for several days, and he noticed that sometimes some of the bulbs turn on. After a series of experiments he came up with a conclusion that bulbs indexed $$$2i$$$ and $$$2i - 1$$$ turn on if the chain connecting them consists of exactly $$$d_i$$$ wires. Moreover, the following important condition holds: the value of $$$d_i$$$ is never greater than $$$n$$$.Petya did his best but was not able to find a configuration that makes all bulbs to turn on, so he seeks your assistance. Please, find out a configuration that makes all bulbs shine. It is guaranteed that such configuration always exists.

The first line of the input contains a single integer $$$n$$$ ($$$1 \leq n \leq 100\,000$$$) — the parameter of a construction set that defines the number of bulbs and the number of wires. Next line contains $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$ ($$$1 \leq d_i \leq n$$$), where $$$d_i$$$ stands for the number of wires the chain between bulbs $$$2i$$$ and $$$2i - 1$$$ should consist of.

Print $$$2n - 1$$$ lines. The $$$i$$$-th of them should contain two distinct integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \leq a_i, b_i \leq 2n$$$, $$$a_i \ne b_i$$$) — indices of bulbs connected by a wire. If there are several possible valid answer you can print any of them.

Note    Answer for the first sample test.      Answer for the second sample test.

let iteri_if action cond lst =
  let rec go i lst =
  match lst with
  | [] -> ()
  | x :: rest -> (if cond i x then action i x else ()) ; go (i + 1) rest
      in go 0 lst

let ans =
  let n = read_int() in
  let s = read_line() in
  let words = Str.split_delim (Str.regexp " ") s in
  let ds = List.map int_of_string words in
  let prs = Array.make n (0, 0) in
  List.iteri (fun i k -> prs.(i) <- (k, i)) ds;
  Array.fast_sort (fun x y -> - (compare x y)) prs;
  let par = Array.make (n * 2 + 1) 0 in
  let lst = Array.make (n * 2) 0 in
  Array.iteri (fun i (_,k) -> lst.(i) <- 2 * k + 1) prs;
  let cur = ref n in
  Array.iteri (fun i (d,k) ->
    if d + i = !cur then
      (lst.(!cur) <- 2*k+2;
      cur := !cur + 1)
    else
      par.(2*k+2) <- lst.(d + i - 1)
    ) prs;
  for i = 1 to !cur-1 do
    par.(lst.(i)) <- lst.(i-1)
  done;
  iteri_if (fun i j -> Printf.printf "%d %d\n" i j)
    (fun i j -> j <> 0) (Array.to_list par)

(* Petya and Construction Set
 * https://codeforces.com/contest/1214/problem/E *)

let ans =
  let n = read_int() in
  let ds = read_line()
           |> Str.split_delim (Str.regexp " ")
           |> List.map int_of_string in
  let prs = Array.make n (0, 0) in
  List.iteri (fun i k -> prs.(i) <- (k, i)) ds;
  Array.fast_sort (fun x y -> - (compare x y)) prs;
  let par = Array.make (n * 2 + 1) 0 in
  let lst = Array.make (n * 2) 0 in
  Array.iteri (fun i (_,k) -> lst.(i) <- 2 * k + 1) prs;
  let cur = ref n in
  let rec iter i cur =
    if i = n then cur
    else
      let (d, k) = prs.(i) in
      let ncur =
        if d + i = cur then
          (lst.(cur) <- 2*k+2;
           cur + 1)
        else (par.(2*k+2) <- lst.(d + i - 1);
              cur) in
      iter (i+1) ncur in
  let cur = iter 0 n in
  (* Array.iteri (fun i (d,k) ->
   *     if d + i = !cur then
   *       (lst.(!cur) <- 2*k+2;
   *        cur := !cur + 1)
   *     else
   *       par.(2*k+2) <- lst.(d + i - 1)
   *   ) prs; *)
  for i = 1 to cur - 1 do
    par.(lst.(i)) <- lst.(i-1)
  done;
  Array.iteri
    (fun i j -> if j = 0 then () else Printf.printf "%d %d\n" i j) par

(* let iteri_if action cond seq =
 *   let rec go i seq =
 *   match seq with
 *   | Seq.Nil -> ()
 *   | Seq.Cons (x, rest) -> (if cond i x then action i x else ()) ; go (i + 1) (rest ())
 *       in go 0 seq *)

let arr_iteri_if action cond arr =
  let rec go i =
    match i with
    | k when k = Array.length arr -> ()
    | _ -> (if cond i arr.(i) then action i arr.(i) else ()) ; go (i + 1)
  in go 0

let ans =
  let n = read_int() in
  let s = read_line() in
  let words = Str.split_delim (Str.regexp " ") s in
  let ds = List.map int_of_string words in
  let prs = Array.make n (0, 0) in
  List.iteri (fun i k -> prs.(i) <- (k, i)) ds;
  Array.fast_sort (fun x y -> - (compare x y)) prs;
  let par = Array.make (n * 2 + 1) 0 in
  let lst = Array.make (n * 2) 0 in
  Array.iteri (fun i (_,k) -> lst.(i) <- 2 * k + 1) prs;
  let cur = ref n in
  Array.iteri (fun i (d,k) ->
    if d + i = !cur then
      (lst.(!cur) <- 2*k+2;
      cur := !cur + 1)
    else
      par.(2*k+2) <- lst.(d + i - 1)
    ) prs;
  for i = 1 to !cur-1 do
    par.(lst.(i)) <- lst.(i-1)
  done;
  arr_iteri_if (fun i j -> Printf.printf "%d %d\n" i j)
    (fun i j -> j <> 0) par

let ans =
  let n = read_int() in
  let s = read_line() in
  let words = Str.split_delim (Str.regexp " ") s in
  let ds = List.map int_of_string words in
  let prs = Array.make n (0, 0) in
  List.iteri (fun i k -> prs.(i) <- (k, i)) ds;
  Array.fast_sort (fun x y -> - (compare x y)) prs;
  let par = Array.make (n * 2 + 1) 0 in
  let lst = Array.make (n * 2) 0 in
  Array.iteri (fun i (_,k) -> lst.(i) <- 2 * k + 1) prs;
  let cur = ref n in
  for i = 0 to n-1 do
    let (d, k) = prs.(i) in
    if d + i = !cur then
      (lst.(!cur) <- 2*k+2;
      cur := !cur + 1)
    else
      par.(2*k+2) <- lst.(d + i - 1)
  done;
  for i = 1 to !cur-1 do
    par.(lst.(i)) <- lst.(i-1)
  done;
  for i = 1 to n * 2 do
    if par.(i) = 0 then
      ()
    else
      Printf.printf "%d %d\n" i par.(i)
  done

(* Petya and Construction Set
 * https://codeforces.com/contest/1214/problem/E *)

let ans =
  let n = read_int() in
  let ds = read_line()
           |> Str.split_delim (Str.regexp " ")
           |> List.map int_of_string in
  let prs = Array.make n (0, 0) in
  List.iteri (fun i k -> prs.(i) <- (k, i)) ds;
  Array.fast_sort (fun x y -> - (compare x y)) prs;
  let par = Array.make (n * 2 + 1) 0 in
  let lst = Array.make (n * 2) 0 in
  Array.iteri (fun i (_,k) -> lst.(i) <- 2 * k + 1) prs;
  let cur = ref n in
  let rec iter i cur =
    if i = n then ()
    else
      let (d, k) = prs.(i) in
      let ncur =
        if d + i = cur then
          (lst.(cur) <- 2*k+2;
           cur + 1)
        else (par.(2*k+2) <- lst.(d + i - 1);
              cur) in
      iter (i+1) ncur in
  iter 0 n;
  (* Array.iteri (fun i (d,k) ->
   *     if d + i = !cur then
   *       (lst.(!cur) <- 2*k+2;
   *        cur := !cur + 1)
   *     else
   *       par.(2*k+2) <- lst.(d + i - 1)
   *   ) prs; *)
  for i = 1 to !cur-1 do
    par.(lst.(i)) <- lst.(i-1)
  done;
  Array.iteri
    (fun i j -> if j = 0 then () else Printf.printf "%d %d\n" i j) par

(* #load "str.cma" *)

let () =
  let n = read_int() in
  let s = read_line() in
  let words = Str.split_delim (Str.regexp " ") s in
  let ds = List.map int_of_string words in
  print_int (List.fold_left (+) 0 ds)


Sereja and his friends went to a picnic. The guys had n soda bottles just for it. Sereja forgot the bottle opener as usual, so the guys had to come up with another way to open bottles.Sereja knows that the i-th bottle is from brand ai, besides, you can use it to open other bottles of brand bi. You can use one bottle to open multiple other bottles. Sereja can open bottle with opened bottle or closed bottle.Knowing this, Sereja wants to find out the number of bottles they've got that they won't be able to open in any way. Help him and find this number.

The first line contains integer n (1 ≤ n ≤ 100) — the number of bottles. The next n lines contain the bottles' description. The i-th line contains two integers ai, bi (1 ≤ ai, bi ≤ 1000) — the description of the i-th bottle.

In a single line print a single integer — the answer to the problem.

let () =
  let (@@) f x = f x in
  let (|>) x f = f x in
  let open List in
  let split = Str.split @@ Str.regexp_string " " in
  let n = read_int () in
  let bottles = Array.make n (0, 0) in
  for i = 0 to n - 1 do
    read_line () |> split |> map int_of_string |> fun l_pair ->
    bottles.(i) <- hd l_pair, hd @@ tl l_pair;
  done;
  let can_open i j = i <> j && snd bottles.(j) = fst bottles.(i) in
  let answer = ref 0 in
  for i = 0 to n - 1 do
    try
      for j = 0 to n - 1 do
        if can_open i j then raise Exit
      done;
      incr answer
    with Exit -> ()
  done;
  print_endline @@ string_of_int !answer