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

module ISet = Set.Make(struct type t = int64 * int64 let compare = compare end)
let n = get_i64 0
let dat = Array.init (i32 n) (fun _ -> get_3_i64 0)
let ff = ref false
let xx,yy,hh = ref 0L,ref 0L,ref 0L
let ng = ref @@ ISet.empty
let rec f x0 y0 =
	let y0 = ref y0 in
	rep x0 100L (fun x ->
		rep !y0 100L (fun y ->
			if ISet.exists ((=) (x,y)) !ng then `Ok
			else
			(* printf "%Ld %Ld\n" x y; *)
			let h,f = Array.fold_left (fun (h,f) (p,q,r) ->
				let d = labs (p-x) + labs (q-y) in
				let h2 = r + d in
				(* printf "  %Ld %Ld\n" h h2; *)
				if h = -1L then (
					(h2,f)
				) else if r = 0L then (
					(h,f)
				) else if h <> h2 then (
					(h,false)
				) else (
					(h,f)
				)
			) (-1L,true) dat in
			if f then (
				ff := true; xx := x; yy := y; hh := h; `Break
			) else `Ok
		);
		if !ff then `Break else `Ok
	);
	y0 := 0L;
	if !hh <= 0L then (
		ng := ISet.add (!xx,!yy) !ng;
		ff := false;
		f !xx !yy;
	) else ()

let () =
	f 0L 0L;
	printf "%Ld %Ld %Ld\n" !xx !yy !hh


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

module ISet = Set.Make(struct type t = int64 * int64 let compare = compare end)
let n = get_i64 0
let dat = Array.init (i32 n) (fun _ -> get_3_i64 0)
let ff = ref false
let xx,yy,hh = ref 0L,ref 0L,ref 0L
let ng = ref @@ ISet.empty
let rec f _ =
	rep 0L 100L (fun x ->
		rep 0L 100L (fun y ->
			if ISet.exists ((=) (x,y)) !ng then `Ok
			else
			(* printf "%Ld %Ld\n" x y; *)
			let h,f = Array.fold_left (fun (h,f) (p,q,r) ->
				let d = labs (p-x) + labs (q-y) in
				let h2 = r + d in
				(* printf "  %Ld %Ld\n" h h2; *)
				if h = -1L then (
					(h2,f)
				) else if r = 0L then (
					(h,f)
				) else if h <> h2 then (
					(h,false)
				) else (
					(h,f)
				)
			) (-1L,true) dat in
			if f then (
				ff := true; xx := x; yy := y; hh := h; `Break
			) else `Ok
		);
		if !ff then `Break else `Ok
	);
	if !hh <= 0L then (
		ng := ISet.add (!xx,!yy) !ng;
		ff := false;
		f 0;
	) else ()

let () =
	f 0;
	printf "%Ld %Ld %Ld\n" !xx !yy !hh

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Score: 300 points

Problem Statement

In the Ancient Kingdom of Snuke, there was a pyramid to strengthen the authority of Takahashi, the president of AtCoder Inc.

The pyramid had center coordinates (C_X, C_Y) and height H. The altitude of coordinates (X, Y) is max(H - |X - C_X| - |Y - C_Y|, 0).

Aoki, an explorer, conducted a survey to identify the center coordinates and height of this pyramid. As a result, he obtained the following information:

C_X, C_Y was integers between 0 and 100 (inclusive), and H was an integer not less than 1.

Additionally, he obtained N pieces of information. The i-th of them is: "the altitude of point (x_i, y_i) is h_i."

This was enough to identify the center coordinates and the height of the pyramid. Find these values with the clues above.

Constraints

N is an integer between 1 and 100 (inclusive).

x_i and y_i are integers between 0 and 100 (inclusive).

h_i is an integer between 0 and 10^9 (inclusive).

The N coordinates (x_1, y_1), (x_2, y_2), (x_3, y_3), ..., (x_N, y_N) are all different.

The center coordinates and the height of the pyramid can be uniquely identified.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1 h_1
x_2 y_2 h_2
x_3 y_3 h_3
:
x_N y_N h_N

Output

Print values C_X, C_Y and H representing the center coordinates and the height of the pyramid in one line, with spaces in between.

Sample Input 1

4
2 3 5
2 1 5
1 2 5
3 2 5

Sample Output 1

2 2 6

In this case, the center coordinates and the height can be identified as (2, 2) and 6.

Sample Input 2

2
0 0 100
1 1 98

Sample Output 2

0 0 100

In this case, the center coordinates and the height can be identified as (0, 0) and 100.

Note that C_X and C_Y are known to be integers between 0 and 100.

Sample Input 3

3
99 1 191
100 1 192
99 0 192

Sample Output 3

100 0 193

In this case, the center coordinates and the height can be identified as (100, 0) and 193.

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


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

let () =
	let n = get_i64 0 in
	let dat = Array.init (i32 n) (fun _ -> get_3_i64 0) in
	let ff = ref false in
	let xx,yy,hh = ref 0L,ref 0L,ref 0L in
	rep 0L 100L (fun x ->
		rep 0L 100L (fun y ->
			(* printf "%Ld %Ld\n" x y; *)
			let h,f = Array.fold_left (fun (h,f) (p,q,r) ->
				let d = labs (p-x) + labs (q-y) in
				let h2 = r + d in
				(* printf "  %Ld %Ld\n" h h2; *)
				if h = -1L then (
					(h2,f)
				) else if r = 0L then (
					(h,f)
				) else if h <> h2 then (
					(h,false)
				) else (
					(h,f)
				)
			) (-1L,true) dat in
			if f then (
				ff := true; xx := x; yy := y; hh := h; `Break
			) else `Ok
		);
		if !ff then `Break else `Ok
	);
	printf "%Ld %Ld %Ld\n" !xx !yy !hh


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

let () =
	let n = get_i64 0 in
	let dat = Array.init (i32 n) (fun _ -> get_3_i64 0) in
	let ff = ref false in
	let xx,yy,hh = ref 0L,ref 0L,ref 0L in
	rep 0L 100L (fun x ->
		rep 0L 100L (fun y ->
			(* printf "%Ld %Ld\n" x y; *)
			let h,f = Array.fold_left (fun (h,f) (p,q,r) ->
				let d = labs (p-x) + labs (q-y) in
				let h2 = r + d in
				(* printf "  %Ld %Ld\n" h h2; *)
				if h = -1L then (
					(h2,f)
				) else if h <> h2 then (
					(h,false)
				) else (
					(h,f)
				)
			) (-1L,true) dat in
			if f then (
				ff := true; xx := x; yy := y; hh := h; `Break
			) else `Ok
		);
		if !ff then `Break else `Ok
	);
	printf "%Ld %Ld %Ld\n" !xx !yy !hh


let maximize a b c =
  if a >= b && a >= c then a * 10 + b + c
  else if b >= a && b >= c then b * 10 + a + c
  else c * 10 + a + b

let () = Scanf.scanf "%d %d %d" maximize |> Printf.printf "%d\n"

Score : 100 points

Problem Statement

You have decided to give an allowance to your child depending on the outcome of the game that he will play now.

The game is played as follows:

There are three "integer panels", each with a digit between 1 and 9 (inclusive) printed on it, and one "operator panel" with a + printed on it.

The player should construct a formula of the form X + Y, by arranging the four panels from left to right. (The operator panel should not be placed at either end of the formula.)

Then, the amount of the allowance will be equal to the resulting value of the formula.

Given the values A, B and C printed on the integer panels used in the game, find the maximum possible amount of the allowance.

Constraints

All values in input are integers.

1 \leq A, B, C \leq 9

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the maximum possible amount of the allowance.

Sample Input 1

1 5 2

Sample Output 1

53

The amount of the allowance will be 53 when the panels are arranged as 52+1, and this is the maximum possible amount.

Sample Input 2

9 9 9

Sample Output 2

108

Sample Input 3

6 6 7

Sample Output 3

82


Scanf.scanf "%d %d %d" (fun a b c ->
    let sum = a + b + c in
    let ma = max (max a b) c in
    Printf.printf "%d\n" (ma * 9 + sum)
)


let _ = Scanf.sscanf (read_line ()) "%d %d %d" (fun a b c ->
  Printf.printf "%d\n" (max (a * 10 + b + c) @@ max (a + b * 10 + c) (a + b + c * 10))
)


let ans = Scanf.sscanf (read_line ()) "%s %s %s" (fun a b c ->
  [
    int_of_string (a ^ b) + (int_of_string c);
    int_of_string (a ^ c) + (int_of_string b);
    int_of_string (b ^ a) + (int_of_string c);
    int_of_string (b ^ c) + (int_of_string a);
    int_of_string (c ^ a) + (int_of_string b);
    int_of_string (c ^ b) + (int_of_string a);
  ]
)
let _ = print_endline (string_of_int (List.hd (List.rev (List.sort compare ans))))


open Printf
open Scanf

let solve a b c = max (a * 10 + b + c) @@ max (a + b * 10 + c) (a + b + c * 10)

let () =
  scanf "%d %d %d " solve |> printf "%d\n"


let m, s = Scanf.scanf " %d %d %d" @@ fun a b c -> max a @@ max b c, a + b + c
let _ = Printf.printf "%d\n" @@ 9 * m + s


let a, b, c = Scanf.scanf " %d %d %d" @@ fun a b c -> a, b, c
let m = max a @@ max b c
let _ = Printf.printf "%d\n" @@ 9 * m + a + b + c


let f a b c = max (max (a * 10 + b + c) (b * 10 + a + c)) (c * 10 + a + b)
let () = Scanf.scanf "%d %d %d" f |> Printf.printf "%d\n"


let a, b, c = Scanf.scanf " %d %d %d" @@ fun a b c -> a, b, c
let m = max a @@ max b c
let _ = m * 10 + a + b + c - m |> Printf.printf "%d\n"


let f a b c = 9*(max a (max b c)) + a + b + c;;
let () = Scanf.scanf "%d %d %d" f
|> Printf.printf "%d\n"


let () =
	Scanf.scanf "%d %d %d\n"
    (fun a b c ->
    if a > b then
    	if b > c then (a*10+b) + c
        else if a > c then (a*10+c) + b
        else (* c > a > b *) (c*10+a) + b
    else (* b >= a *)
    	if a > c then (b*10+a) + c
    	else if b > c then (b*10+c) + a
        else (c*10+b) + a)
    |> print_int


let () =
	Scanf.scanf "%d %d %d\n"
    (fun a b c ->
    if a > b then
    	if b > c then (a*10+b) + c
        else if a > c then (a*10+c) + b
        else (* c > a > b *) (c*10+a) + c
    else (* b >= a *)
    	if a > c then (b*10+a) + c
    	else if b > c then (b*10+c) + a
        else (c*10+b) + a)
    |> print_int


let _ =
	let a, b, c = Scanf.scanf "%d %d %d\n" (fun a b c -> a, b, c) in
    let ls = List.sort (fun x y -> compare y x) [a; b; c] in
    (List.nth ls 0) * 10 + (List.nth ls 1) + (List.nth ls 2)
    |> print_int


Scanf.scanf"%d %d %d"@@fun a b c->print_int@@a+b+c+9*List.([a;b;c]|>sort compare|>nth)2


Printf.printf "%d\n" @@ Scanf.scanf "%d %d %d" @@ fun a b c -> let [a;b;c] = List.sort (-) [a;b;c] in 10*c+b+a


Printf.printf "%d\n" @@ Scanf.scanf "%d %d %d" @@ fun a b c -> let [a;b;c] = List.sort (-) [a;b;c] in 10*a+b+c


open Scanf
open Printf

let f a b c = let [x; y; z] = List.sort compare [a; b; c] in z + y + x*10

let () = let ans = scanf "%d %d %d\n" f in
  printf "%d\n" ans


open Scanf
open Printf

let f a b c = let x :: y :: _ :: [] = List.sort compare [a; b; c] in x + y

let () = let ans = scanf "%d %d %d\n" f in
  printf "%d\n" ans


open Printf
open Scanf

let id x = x
let pair x y = x, y

let vs = scanf "%d %d %d" (fun x y z -> [|x; y; z|])

let () = Array.sort compare vs

let () =
  let x = vs.(2) * 10 + vs.(1) + vs.(0) in
  printf "%d\n" x


let () = Scanf.scanf "%d %d %d" @@ fun a b c ->
  Printf.printf "%d\n" @@ List.fold_left max min_int @@
  [ 10 * a + b + c;
    a + 10 * b + c;
    a + b + 10 * c ]


let n, m, x, y = Scanf.scanf " %d %d %d %d" @@ fun a b c d -> a, b, c, d
let xs = Array.init n @@ fun _ -> Scanf.scanf " %d" @@ (+) 0
let ys = Array.init m @@ fun _ -> Scanf.scanf " %d" @@ (+) 0
let xmax = max x @@ Array.fold_left max xs.(0) xs
let ymin = min y @@ Array.fold_left min ys.(0) ys
let _ = print_endline @@ if xmax < ymin then "No War" else "War"

Score : 200 points

Problem Statement

Our world is one-dimensional, and ruled by two empires called Empire A and Empire B.

The capital of Empire A is located at coordinate X, and that of Empire B is located at coordinate Y.

One day, Empire A becomes inclined to put the cities at coordinates x_1, x_2, ..., x_N under its control, and Empire B becomes inclined to put the cities at coordinates y_1, y_2, ..., y_M under its control.

If there exists an integer Z that satisfies all of the following three conditions, they will come to an agreement, but otherwise war will break out.

X < Z \leq Y

x_1, x_2, ..., x_N < Z

y_1, y_2, ..., y_M \geq Z

Determine if war will break out.

Constraints

All values in input are integers.

1 \leq N, M \leq 100

-100 \leq X < Y \leq 100

-100 \leq x_i, y_i \leq 100

x_1, x_2, ..., x_N \neq X

x_i are all different.

y_1, y_2, ..., y_M \neq Y

y_i are all different.

Input

Input is given from Standard Input in the following format:

N M X Y
x_1 x_2 ... x_N
y_1 y_2 ... y_M

Output

If war will break out, print War; otherwise, print No War.

Sample Input 1

3 2 10 20
8 15 13
16 22

Sample Output 1

No War

The choice Z = 16 satisfies all of the three conditions as follows, thus they will come to an agreement.

X = 10 < 16 \leq 20 = Y

8, 15, 13 < 16

16, 22 \geq 16

Sample Input 2

4 2 -48 -1
-20 -35 -91 -23
-22 66

Sample Output 2

War

Sample Input 3

5 3 6 8
-10 3 1 5 -100
100 6 14

Sample Output 3

War


let rec check num min_num x y =
  if num = min_num then "War"
  else if x < num && num <= y then "No War"
  else check (num-1) min_num x y
let () =
  let (n, m, x, y) = Scanf.scanf "%d %d %d %d\n" (fun a b c d -> (a, b, c, d)) in
  let xlst = Array.to_list (Array.init n (fun _ -> Scanf.scanf "%d " (fun x -> x))) in
  let ylst = Array.to_list (Array.init m (fun _ -> Scanf.scanf "%d " (fun x -> x))) in
  let max_x = List.fold_left max min_int xlst in
  let min_y = List.fold_left min max_int ylst in
  Printf.printf "%s\n" (check y (x+1) max_x min_y)


open Scanf
open Printf

let rec scan_list = function
  | 1 -> scanf "%d " (fun k -> [k])
  | n -> scanf "%d " (fun k -> k :: scan_list (n-1))

let f n m x y =
  let xs = scan_list n in
  let ys = scan_list m in
  let xmax = List.fold_left max (-100) (x :: xs) in
  let ymin = List.fold_left min 100 (y :: ys) in
  if xmax < ymin
  then "No War"
  else "War"

let () = let ans = scanf "%d %d %d %d\n" f in
  printf "%s\n" ans


open Printf
open Scanf

let id x = x
let pair x y = x, y

let n, m, x, y = scanf "%d %d %d %d" (fun n m x y -> n, m, x, y)

let array_cons v vs = Array.of_list (v :: Array.to_list vs)

let xs = array_cons x @@ Array.init n (fun _ -> scanf " %d" id)
let ys = array_cons y @@ Array.init m (fun _ -> scanf " %d" id)

let () = Array.sort (fun x y -> y - x) xs
let () = Array.sort (fun x y -> x - y) ys

let () =
  if xs.(0) < ys.(0) then
    print_endline "No War"
  else
    print_endline "War"


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

let () =
	let n,m,x,y = get_4_i64 0 in
	let ax = input_i64_array n in
	let ay = input_i64_array m in
	Array.sort compare ax;
	Array.sort compare ay;
	repm (x+1L) y false (fun f z ->
		if ax.(i32 n -$ 1) < z && ay.(0) >= z then `Break_m true
		else `Ok f
	)
	|> (fun b -> if b then "No War" else "War")
	|> print_endline


let () = Scanf.scanf "%d %d %d %d\n" @@ fun n m x y ->
  let max_xs = Array.fold_left max x @@
    Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun x -> x in
  let min_ys = Array.fold_left min y @@
    Array.init m @@ fun _ -> Scanf.scanf "%d " @@ fun y -> y in
  print_endline @@ if max_xs < min_ys then "No War" else "War"


let abc333 a b =
  if a mod 2 = 1 && b mod 2 = 1 then "Yes" else "No"

let () = Scanf.scanf "%d %d" abc333 |> Printf.printf "%s\n"

Score : 100 points

Problem Statement

You are given integers A and B, each between 1 and 3 (inclusive).

Determine if there is an integer C between 1 and 3 (inclusive) such that A \times B \times C is an odd number.

Constraints

All values in input are integers.

1 \leq A, B \leq 3

Input

Input is given from Standard Input in the following format:

A B

Output

If there is an integer C between 1 and 3 that satisfies the condition, print Yes; otherwise, print No.

Sample Input 1

3 1

Sample Output 1

Yes

Let C = 3. Then, A \times B \times C = 3 \times 1 \times 3 = 9, which is an odd number.

Sample Input 2

1 2

Sample Output 2

No

Sample Input 3

2 2

Sample Output 3

No


Scanf.scanf "%d %d" (fun a b ->
    print_endline @@ if (a * b) mod 2 = 1 then "Yes" else "No"
)


let _ = Scanf.sscanf (read_line ()) "%d %d" (fun a b ->
  if a * b mod 2 = 1 then print_endline "Yes"
  else print_endline "No"
)


open Printf
open Scanf

let is_odd x = x mod 2 <> 0

let solve a b =
  if is_odd a && is_odd b then "Yes" else "No"

let () =
  scanf "%d %d " solve |> printf "%s\n"


let a, b = Scanf.scanf " %d %d" @@ fun a b -> a, b
let _ = print_endline @@ if a * b mod 2 = 1 then "Yes" else "No"


Scanf.scanf "%d %d" @@ fun a b -> print_endline @@ if a * b mod 2 = 0 then "No" else "Yes"


let () =
  Scanf.scanf "%d %d\n"
    (fun a b ->
       if a mod 2 = 1 && b mod 2 = 1 then "Yes " else "No ")
  |> print_string


let _ =
	let a, b = Scanf.scanf "%d %d\n" (fun a b -> a, b) in
    (if (a * b) mod 2 = 0 then "No" else "Yes")
    |> print_string


let () =
  Scanf.scanf "%d %d" @@ fun a b ->
    match (a mod 2 = 1) && (b mod 2 = 1) with
    | true -> print_endline "Yes"
    | false -> print_endline "No"


let () = Scanf.scanf "%d %d" @@ fun a b ->
  print_endline @@
  if a mod 2 = 0 || b mod 2 = 0
  then "No"
  else "Yes"


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

let () = Scanf.scanf "%d %d\n" @@ fun n x ->
  let xs = Array.init n @@ fun _ ->
    Scanf.scanf "%d " @@ fun x -> x in
  Printf.printf "%d\n" @@ Array.fold_right (fun x' -> gcd @@ abs @@ x' - x) xs 0


let (a, b) = Scanf.scanf("%d %d") (fun x y -> (x, y)) in
print_string (if a * b mod 2 = 1 then "Yes" else "No");
print_newline ();


let solve a b =
  if a mod 2 > 0 && b mod 2 > 0 then "Yes" else "No"

let () =
  let rec read () =
    try let ans = (Scanf.scanf "%d %d\n" solve) in
        Printf.printf "%s\n" ans
    with End_of_file -> ()
  in
  read ()
;;


open Printf open Scanf
module MyInt = struct let (+) = (+) let (-) = (-) let (/) = (/) let ( * ) = ( * ) let (mod) = (mod) let (%) = (mod) let (+=) r v = r := !r + v let (-=) r v = r := !r - v let ( *=) r v= r := !r * v let (/=) r v = r := !r / v end
module MyInt64 = struct
	let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (+) p q = Int64.add p q let (-) p q = Int64.sub p q let ( * ) p q = Int64.mul p q let (/) p q = Int64.div p q let (+=) r v = r := !r + v let (-=) r v = r := !r - v let ( *=) r v= r := !r * v let (/=) r v = r := !r / v let labs p = if p < 0L then -1L*p else p let (~|) p = Int64.to_int p let (~~|) p = Int64.of_int p let input_i64_array n = Array.init (~| n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v)) let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v) let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v) let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w) let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x) let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y) let print_i64_endline n = n |> Int64.to_string |> print_endline let (mod) m n = m - (m/n) * n let (%) = (mod) let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n) let lcm m n = (m*n) / gcd m n let rep from_ to_ fbod = let i,f = ref ~|from_,ref true in while !i <= ~|to_ && !f do match fbod ~~| !i with | `Break -> f := false | _ -> i := !i +$ 1; done
	let string_of_list ?(separator=" ") f ls = let rec f0 a s = match a with | [] -> s | [h] -> s ^ (f h) ^ separator | h::t -> f0 t (s ^ (f h) ^ separator) in f0 ls "" let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) [] let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
	let (@@@) = (@) let (@) a i = a.(~|i) let (<@) a (i,v) = a.(~|i) <- v
	let ceildiv m n = (m+n-1L) / n
end open MyInt64

let repb from_ to_ fbod = let i,f = ref ~|from_,ref true in while !i <= ~|to_ && !f do match fbod (~~| !i) with | `Break -> f := false | _ -> i := !i +$ 1 done; !f

let () =
	let a,b = get_2_i64 0 in
	Array.init 3 (fun i -> if a*b*((~~|i)+1L) mod 2L = 1L then 1L else 0L)
	|> Array.fold_left (+) 0L
	|> (fun v -> if v > 0L then "Yes" else "No")
	|> print_endline


open Printf
open Scanf

let id x = x

let a, b = scanf "%d %d" (fun a b -> a, b)

let () =
  let n1 = a * b * 1 in
  let n2 = a * b * 2 in
  let n3 = a * b * 3 in
  if n1 mod 2 = 1 || n2 mod 2 = 1 || n3 mod 2 = 1 then
    print_endline "Yes"
  else
    print_endline "No"


print_endline @@ Scanf.scanf "%d %d" @@ fun a b -> if a * b mod 2 = 0 then "No" else "Yes"


Scanf.scanf "%d" (fun n ->
    let w = Array.init n (fun _ -> Scanf.scanf " %s" (fun w -> w)) in
    let module S = Set.Make (struct type t = string let compare = compare end) in
    let rec loop i prev set =
        if i = n then "Yes" else
            let k = String.length prev in
            if S.mem w.(i) set || prev.[k - 1] <> w.(i).[0] then "No"
            else loop (i + 1) w.(i) (S.add w.(i) set)
    in
    loop 1 w.(0) (S.singleton w.(0)) |> print_endline
)

Score : 200 points

Problem Statement

Takahashi is practicing shiritori alone again today.

Shiritori is a game as follows:

In the first turn, a player announces any one word.

In the subsequent turns, a player announces a word that satisfies the following conditions:

That word is not announced before.

The first character of that word is the same as the last character of the last word announced.

In this game, he is practicing to announce as many words as possible in ten seconds.

You are given the number of words Takahashi announced, N, and the i-th word he announced, W_i, for each i. Determine if the rules of shiritori was observed, that is, every word announced by him satisfied the conditions.

Constraints

N is an integer satisfying 2 \leq N \leq 100.

W_i is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
W_1
W_2
:
W_N

Output

If every word announced by Takahashi satisfied the conditions, print Yes; otherwise, print No.

Sample Input 1

4
hoge
english
hoge
enigma

Sample Output 1

No

As hoge is announced multiple times, the rules of shiritori was not observed.

Sample Input 2

9
basic
c
cpp
php
python
nadesico
ocaml
lua
assembly

Sample Output 2

Yes

Sample Input 3

8
a
aa
aaa
aaaa
aaaaa
aaaaaa
aaa
aaaaaaa

Sample Output 3

No

Sample Input 4

3
abc
arc
agc

Sample Output 4

No


open Hashtbl
let n = read_int ()
let ws = Array.init n @@ fun _ -> read_line ()
let h = create n
let _ = Array.fold_left (fun p w -> if mem h w || p <> w.[0] then (print_endline "No"; exit 0); add h w 1; w.[String.length w - 1]) ws.(0).[0] ws |> ignore; print_endline "Yes"


open Hashtbl
let n = Scanf.scanf " %d" (+) 0
let ws = Array.init n @@ fun _ -> Scanf.scanf " %s" @@ fun s -> s
let h = create n
let _ = Array.fold_left (fun c w -> if mem h w || c <> w.[0] then (print_endline "No"; exit 0); add h w 1; w.[String.length w - 1]) ws.(0).[0] ws |> ignore; print_endline "Yes"


let puts st = print_string st; print_string "\n";;
let puti num = print_int num; print_string "\n";;
let getSL () = Str.split (Str.regexp " ") (read_line ());;
let getLI () = List.map int_of_string (getSL ());;
let getI () = int_of_string (read_line ());;
let rep n f =
    let rec repimpl i n = if i < n then (f i; repimpl (i+1) n) else () in repimpl 0 n;;
let rep_from from until f =
    let rec repimpl i n = if i < n then (f i; repimpl (i+1) n) else () in repimpl from until;;

let len = getI ();;
let arr =
    let arr = Array.make len "" in
    rep len (fun i -> arr.(i) <- read_line ());
    arr;;

module SetSt = Set.Make(String);;
let st = ref SetSt.empty

let solve () =
    rep len (fun i -> st := SetSt.add arr.(i) !st);
    if SetSt.cardinal !st = len then begin
        let res = ref true in
        rep (len-1) (fun i ->
            res := !res && (Str.last_chars arr.(i) 1) = (Str.first_chars arr.(i+1) 1);
            (* puts @@ arr.(i) ^ arr.(i+1) *)
        );
        puts @@ if !res then "Yes" else "No";
    end else puts "No";;

solve ();;


let () =
  Scanf.scanf "%d\n" @@ fun n ->
  let rec inner (dict : string list) (i : int) =
    match (i < n) with
    | false -> true
    | true ->
      Scanf.scanf "%s\n" (fun str ->
          (match dict with
           | [] -> inner [str] (i + 1)
           | x :: xl -> (match (x.[(String.length x) - 1]) = str.[0] with
               | false -> false
               | true  -> (match (List.mem str dict) with
                   | true -> false
                   | false -> inner (str :: dict) (i + 1)))))
  in
  match inner [] 0 with
  | true -> print_endline "Yes"
  | false -> print_endline "No"


let solve list =
  let already = Hashtbl.create (List.length list) in
  let rec inner last rest = match rest with
    | [] -> true
    | x::xs ->
       if (String.length last > 0) && (String.get x 0) <> (String.get last (String.length last - 1)) then false
       else
         begin
           if Hashtbl.mem already x then false
           else
             begin
               Hashtbl.add already x 1;
               inner x xs
             end
         end
  in
  inner "" list

let read_list_of_string () =
  let rec read_list_of_string_inner lst =
    try let ans = (Scanf.scanf "%s\n" (fun x->x)) in
        read_list_of_string_inner (ans::lst)
    with End_of_file ->
      List.rev lst
  in
  read_list_of_string_inner []

let () =
  let _ = read_int() in
  let list = read_list_of_string () in
  let ans = solve list in
  Printf.printf "%s\n" (if ans then "Yes" else "No")
;;


module StringSet = Set.Make (String)

let () =
  let n = int_of_string @@ read_line () in
  let ws = Array.init n @@ fun _ -> read_line () in
  let wss = Array.make (n + 1) StringSet.empty in
  for i = 0 to n - 1 do
    wss.(i + 1) <- StringSet.add ws.(i) wss.(i)
  done;
  print_endline @@
  if
    Array.fold_left ( && ) true @@
    Array.init (n - 1) @@ fun i ->
      not (StringSet.mem ws.(i + 1) wss.(i + 1)) && ws.(i).[String.length ws.(i) - 1] = ws.(i + 1).[0]
  then "Yes"
  else "No"


let rec is_uniq l = match l with
    [] -> true
  | x::xs -> not (List.mem x xs) && is_uniq xs;;

let rec ok l = match l with
    [] -> true
  | x::[] -> true
  | x::y::xs -> x.[String.length x - 1] = y.[0] && ok (y::xs);;

let n = read_int () in
let a = Array.make n "" in
for i = 0 to n - 1 do
  a.(i) <- read_line ()
done;
if is_uniq (Array.to_list a) && ok (Array.to_list a) then
  print_string "Yes"
else
  print_string "No";
print_newline ()


open Printf open Scanf
module MyInt = struct let (+) = (+) let (-) = (-) let (/) = (/) let ( * ) = ( * ) let (mod) = (mod) let (%) = (mod) let (+=) r v = r := !r + v let (-=) r v = r := !r - v let ( *=) r v= r := !r * v let (/=) r v = r := !r / v end
module MyInt64 = struct
	let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (+) p q = Int64.add p q let (-) p q = Int64.sub p q let ( * ) p q = Int64.mul p q let (/) p q = Int64.div p q let (+=) r v = r := !r + v let (-=) r v = r := !r - v let ( *=) r v= r := !r * v let (/=) r v = r := !r / v let labs p = if p < 0L then -1L*p else p let (~|) p = Int64.to_int p let (~~|) p = Int64.of_int p let input_i64_array n = Array.init (~| n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v)) let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v) let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v) let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w) let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x) let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y) let print_i64_endline n = n |> Int64.to_string |> print_endline let (mod) m n = m - (m/n) * n let (%) = (mod) let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n) let lcm m n = (m*n) / gcd m n let rep from_ to_ fbod = let i,f = ref ~|from_,ref true in while !i <= ~|to_ && !f do match fbod ~~| !i with | `Break -> f := false | _ -> i := !i +$ 1; done
	let string_of_list ?(separator=" ") f ls = let rec f0 a s = match a with | [] -> s | [h] -> s ^ (f h) ^ separator | h::t -> f0 t (s ^ (f h) ^ separator) in f0 ls "" let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) [] let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
	let (@@@) = (@) let (@) a i = a.(~|i) let (<@) a (i,v) = a.(~|i) <- v
	let ceildiv m n = (m+n-1L) / n
end open MyInt64
module SSet = Set.Make(String)
let () =
	let n = get_i64 0 in
	let a = Array.init (~|n) (fun _ -> scanf " %s" (fun v -> v))
	in
	let st = SSet.of_list (a |> Array.to_list)
	in let siz = SSet.fold (fun _ u -> u+$1) st 0
	in
	(if siz <> Array.length a then "No" else
	Array.fold_left (fun (f,c) v ->
		let ct = v.[String.length v -$ 1] in
		if v.[0] = c then (f,ct)
		else (false,ct)
	) (true,a.(0).[0]) a
	|> (fun (f,_) -> if f then "Yes" else "No")
	)
	|> print_endline


let rec for_fold a b v f = if a >= b then v else for_fold (a+1) b (f v a) f
let () = print_endline @@
  Scanf.scanf "%d" @@ fun n ->
  let w = Array.init n (fun _ -> Scanf.scanf " %s" (fun x -> x)) in
  let c = Array.to_list w |> List.sort_uniq compare |> List.length in
  let b =
    for_fold 0 (n-1) true (fun b i ->
      b && (w.(i).[String.length w.(i) - 1] = w.(i+1).[0]))
  in
  if b && c = n then "Yes" else "No"


Scanf.scanf "%d %d" (fun n xx ->
    let rec gcd a b = if b = 0 then a else gcd b (a mod b) in
    let x = Array.init n (fun _ -> Scanf.scanf " %d" (fun x -> abs(x - xx))) in
    let rec loop i acc =
        if i = n then acc else loop (i + 1) (gcd acc x.(i))
    in
    loop 1 x.(0) |> Printf.printf "%d\n"
)

Score : 300 points

Problem Statement

There are N cities on a number line. The i-th city is located at coordinate x_i.

Your objective is to visit all these cities at least once.

In order to do so, you will first set a positive integer D.

Then, you will depart from coordinate X and perform Move 1 and Move 2 below, as many times as you like:

Move 1: travel from coordinate y to coordinate y + D.

Move 2: travel from coordinate y to coordinate y - D.

Find the maximum value of D that enables you to visit all the cities.

Here, to visit a city is to travel to the coordinate where that city is located.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq X \leq 10^9

1 \leq x_i \leq 10^9

x_i are all different.

x_1, x_2, ..., x_N \neq X

Input

Input is given from Standard Input in the following format:

N X
x_1 x_2 ... x_N

Output

Print the maximum value of D that enables you to visit all the cities.

Sample Input 1

3 3
1 7 11

Sample Output 1

2

Setting D = 2 enables you to visit all the cities as follows, and this is the maximum value of such D.

Perform Move 2 to travel to coordinate 1.

Perform Move 1 to travel to coordinate 3.

Perform Move 1 to travel to coordinate 5.

Perform Move 1 to travel to coordinate 7.

Perform Move 1 to travel to coordinate 9.

Perform Move 1 to travel to coordinate 11.

Sample Input 2

3 81
33 105 57

Sample Output 2

24

Sample Input 3

1 1
1000000000

Sample Output 3

999999999


(* O(n log(max xs)) *)
let n, x0 = Scanf.scanf " %d %d" @@ fun a b -> a, b
let xs = Array.init n @@ fun _ -> Scanf.scanf " %d" @@ (+) 0
let rec gcd a b = if b = 0 then a else gcd b @@ a mod b
let _ = Array.fold_left (fun g x -> gcd g @@ abs @@ x0 - x) 0 xs |> Printf.printf "%d\n"


(* O(n) *)
let n, x0 = Scanf.scanf " %d %d" @@ fun a b -> a, b
let xs = Array.init n @@ fun _ -> Scanf.scanf " %d" @@ (+) 0
let rec gcd a b = if b = 0 then a else gcd b (a mod b)
let f g x = gcd g @@ abs @@ x0 - x
let _ = Array.fold_left f 0 xs |> Printf.printf "%d\n"


let rec gcd x y = if y = 0 then x else gcd y (x mod y);;

let (n, x) = Scanf.scanf "%d %d" (fun x y -> (x, y)) in
let xs = Array.make n 0 in
for i = 0 to n - 1 do
  xs.(i) <- Scanf.scanf (if i = 0 then "\n%d" else " %d") (fun x -> x)
done;
print_int (xs |> Array.to_list |> List.map ((-) x) |> List.map abs |> List.fold_left gcd 0);
print_newline ()


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

let () = Scanf.scanf "%d %d\n" @@ fun n x ->
  let xs = Array.init n @@ fun _ ->
    Scanf.scanf "%d " @@ fun x -> x in
  Printf.printf "%d\n" @@ Array.fold_right (fun x' -> gcd @@ abs @@ x' - x) xs 0


open Printf
open Scanf

let id x = x

let n, x = scanf "%d %d" (fun n x -> n, x)
let xs = Array.init n (fun i -> scanf " %d" id)

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

let lcm m n = m * n / gcd m n

let calc n = abs (n - x)

let () =
  let r = ref (calc xs.(0)) in
  for i = 1 to n - 1 do
    r := gcd !r (calc xs.(i))
  done;
  printf "%d\n" !r


open Printf open Scanf
module MyInt = struct let (+) = (+) let (-) = (-) let (/) = (/) let ( * ) = ( * ) let (mod) = (mod) let (%) = (mod) let (+=) r v = r := !r + v let (-=) r v = r := !r - v let ( *=) r v= r := !r * v let (/=) r v = r := !r / v end
module MyInt64 = struct
	let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (+) p q = Int64.add p q let (-) p q = Int64.sub p q let ( * ) p q = Int64.mul p q let (/) p q = Int64.div p q let (+=) r v = r := !r + v let (-=) r v = r := !r - v let ( *=) r v= r := !r * v let (/=) r v = r := !r / v let labs p = if p < 0L then -1L*p else p let (~|) p = Int64.to_int p let (~~|) p = Int64.of_int p let input_i64_array n = Array.init (~| n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v)) let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v) let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v) let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w) let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x) let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y) let print_i64_endline n = n |> Int64.to_string |> print_endline let (mod) m n = m - (m/n) * n let (%) = (mod) let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n) let lcm m n = (m*n) / gcd m n let rep from_ to_ fbod = let i,f = ref ~|from_,ref true in while !i <= ~|to_ && !f do match fbod ~~| !i with | `Break -> f := false | _ -> i := !i +$ 1; done
	let string_of_list ?(separator=" ") f ls = let rec f0 a s = match a with | [] -> s | [h] -> s ^ (f h) ^ separator | h::t -> f0 t (s ^ (f h) ^ separator) in f0 ls "" let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) [] let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
	let (@@@) = (@) let (@) a i = a.(~|i) let (<@) a (i,v) = a.(~|i) <- v
	let ceildiv m n = (m+n-1L) / n
end open MyInt64

let () =
	let n,x = get_2_i64 0 in
	let a = input_i64_array n
	in
	let a = Array.map (fun v -> labs (v - x)) a in
	Array.fold_left (fun u v ->
		gcd u v
	) a.(0) a
	|> printf "%Ld\n"


let rec gcd a b = if b = 0 then a else gcd b (a mod b)
let () =
  Scanf.scanf "%d %d" @@ fun n x0 ->
  let d = Array.init n (fun _ -> Scanf.scanf " %d" ((+) 0))
    |> Array.map (fun x -> abs (x - x0))
  in
  Array.fold_left (fun u v -> gcd u v) d.(0) d
    |> Printf.printf "%d\n"


let d, n = Scanf.scanf " %d %d" @@ fun a b -> a, b
let m = match d with 0 -> 1 | 1 -> 100 | _ -> 10000
let _ = Printf.printf "%d\n" @@ m * if n = 100 then 101 else n

Score: 200 points

Problem Statement

Today, the memorable AtCoder Beginner Contest 100 takes place. On this occasion, Takahashi would like to give an integer to Ringo.

As the name of the contest is AtCoder Beginner Contest 100, Ringo would be happy if he is given a positive integer that can be divided by 100 exactly D times.

Find the N-th smallest integer that would make Ringo happy.

Constraints

D is 0, 1 or 2.

N is an integer between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

D N

Output

Print the N-th smallest integer that can be divided by 100 exactly D times.

Sample Input 1

0 5

Sample Output 1

5

The integers that can be divided by 100 exactly 0 times (that is, not divisible by 100) are as follows: 1, 2, 3, 4, 5, 6, 7, ...

Thus, the 5-th smallest integer that would make Ringo happy is 5.

Sample Input 2

1 11

Sample Output 2

1100

The integers that can be divided by 100 exactly once are as follows: 100, 200, 300, 400, 500, 600, 700, 800, 900, 1 \ 000, 1 \ 100, ...

Thus, the integer we are seeking is 1 \ 100.

Sample Input 3

2 85

Sample Output 3

850000

The integers that can be divided by 100 exactly twice are as follows: 10 \ 000, 20 \ 000, 30 \ 000, ...

Thus, the integer we are seeking is 850 \ 000.


let () =
  let d, n = Scanf.scanf "%d %d\n" (fun a b -> a, b) in
  Printf.printf "%d\n" (if d = 0 then if n <> 100 then n else 101 else if d = 1 then 100 * n else 10000 * n)


let () =
  let d, n = Scanf.scanf "%d %d\n" (fun a b -> a, b) in
  Printf.printf "%d\n" (if d = 0 then if n <> 100 then n else n+1 else if d = 1 then 100 * n else 10000 * n)


let calc n = function 0 -> n | 1 -> 100 * n | _ -> 10000 * n
let _ = Scanf.scanf "%d %d" (fun d n -> if n = 100 then calc 101 d else calc n d) |> Printf.printf "%d"


let rec pow n = function 1 -> n | m -> n * pow n (m-1)
let _ = Scanf.scanf "%d %d" (fun d n -> if d = 0 then n else (pow 100 d) * n) |> Printf.printf "%d"


let rec pow n d = if d = 0 then 1 else if d = 1 then n else pow (n * n) (d - 1)
let calc d n = (n + n / 100) * (pow 100 d)
let main = print_int (Scanf.scanf "%d %d" calc)


let getTop n = if n = 100 then 101 else n
let getZero d =
	let rec zero d s =
    	if d = 0 then s else zero (d - 1) s ^ "00"
	in zero d ""
let calc d n = string_of_int (getTop n) ^ getZero d
let main = print_string (Scanf.scanf "%d %d" calc)


let rec pow_int a n = if n <= 0 then 1 else a * pow_int a (n-1)
let () =
  Scanf.scanf "%d %d" (fun d n -> (n + n / 100) * pow_int 100 d)
  |> Printf.printf "%d\n"


let rec power x n =
  if n <= 0 then 1
  else if n mod 2 = 0 then
    power (x*x) (n/2)
  else
    x * power x (n - 1)
let () =
  let d, n = Scanf.scanf "%d %d" (fun d n -> d, n) in
  let s = Array.init 101 (fun i -> if d = 0 then
                                     (if i = 100 then 101
                                      else i)
                                   else (if i = 100 then
                                           (i+1) * (power 100 d)
                                         else
                                           i * (power 100 d))) in
  Printf.printf "%d\n" (s.(n))


let rec power x n =
  if n <= 0 then 1
  else if n mod 2 = 0 then
    power (x*x) (n/2)
  else
    x * power x (n - 1)
let () =
  let d, n = Scanf.scanf "%d %d" (fun d n -> d, n) in
  let s = Array.init 101 (fun i -> if d = 0 then i else i * (power 100 d)) in
  Printf.printf "%d\n" (s.(n))


open Batteries
let () =
  let d,n = Scanf.scanf "%d %d " (fun a b -> a,b) in

  let d = match d with
      0 -> 1
    | 1 -> 100
    | 2 -> 10000
  in

  let n = if n = 100 then 101 else n in

  Printf.printf "%d\n" @@
  d*n


let intpow x n = int_of_float @@ float_of_int x ** float_of_int n

let () = Scanf.scanf "%d %d" @@ fun d n ->
  Printf.printf "%d\n" @@ (if n = 100 then 101 else n) * intpow 100 d


let intpow x n = int_of_float @@ float_of_int x ** float_of_int n

let () = Scanf.scanf "%d %d" @@ fun d n ->
  Printf.printf "%d\n" @@ n * intpow 100 d


let () =
  let d, n = Scanf.scanf "%d %d" (fun d n -> d, n) in
  let s = Array.init 101 (fun i -> if d = 0 then i else i * 100) in
  Printf.printf "%d\n" (s.(n))


open Batteries
let () =
  let d,n = Scanf.scanf "%d %d " (fun a b -> a,b) in

  let d = match d with
      0 -> 1
    | 1 -> 100
    | 2 -> 10000
  in

  Printf.printf "%d\n" @@
  d*n


open Core

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

let ar = List.range ~stop:`inclusive 0 a

let br = List.range ~stop:`inclusive 0 b

let cr = List.range ~stop:`inclusive 0 c

let space = List.cartesian_product (List.cartesian_product ar br) cr

let n =
  List.map space ~f:(fun abc ->
      match abc with
      | (a, b), c when (500 * a) + (100 * b) + (50 * c) = x ->
          true
      | _ ->
          false)
  |> List.count ~f:Fn.id

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

Score : 200 points

Problem Statement

You have A 500-yen coins, B 100-yen coins and C 50-yen coins (yen is the currency of Japan).
In how many ways can we select some of these coins so that they are X yen in total?

Coins of the same kind cannot be distinguished. Two ways to select coins are distinguished when, for some kind of coin, the numbers of that coin are different.

Constraints

0 \leq A, B, C \leq 50

A + B + C \geq 1

50 \leq X \leq 20 000

A, B and C are integers.

X is a multiple of 50.

Input

Input is given from Standard Input in the following format:

A
B
C
X

Output

Print the number of ways to select coins.

Sample Input 1

2
2
2
100

Sample Output 1

2

There are two ways to satisfy the condition:

Select zero 500-yen coins, one 100-yen coin and zero 50-yen coins.

Select zero 500-yen coins, zero 100-yen coins and two 50-yen coins.

Sample Input 2

5
1
0
150

Sample Output 2

0

Note that the total must be exactly X yen.

Sample Input 3

30
40
50
6000

Sample Output 3

213


let a = read_int()
let b = read_int()
let c = read_int()
let x = read_int()

let rec count i j k n =
  if i > a then n
  else if j > b then (count (i+1) 0 0 n)
  else if k > c then (count i (j+1) 0 n)
  else if 500*i + 100*j + 50*k = x then (count i j (k+1) n+1)
  else (count i j (k+1) n)

let answer = string_of_int (count 0 0 0 0)

let () = print_endline answer


Scanf.scanf "%d %d %d %d" (fun a b c x ->
    let rec loop aa acc =
        let rec loop2 bb acc =
            let rec loop3 cc acc =
                if cc > c || aa * 500 + bb * 100 + cc * 50 > x then acc else
                    if aa * 500 + bb * 100 + cc * 50 = x then acc + 1 else
                        loop3 (cc + 1) acc
            in
            if bb > b || aa * 500 + bb * 100 > x then acc else loop2 (bb + 1) (loop3 0 acc)
        in
        if aa > a || aa * 500 > x then acc else loop (aa + 1) (loop2 0 acc)
    in
    loop 0 0 |> Printf.printf "%d\n"
)


let ans = ref 0
let _ = Scanf.scanf "%d %d %d %d" @@ fun a b c x -> for i = 0 to a do for j = 0 to b do for k = 0 to c do if 500 * i + 100 * j + 50 * k = x then incr ans done done done; Printf.printf "%d\n" !ans


let a, b, c, x = Scanf.scanf " %d %d %d %d" @@ fun a b c d -> a, b, c, d
let ans = ref 0
let _ = for i = 0 to a do
  for j = 0 to b do
    for k = 0 to c do if 500 * i + 100 * j + 50 * k = x then incr ans done done done; Printf.printf "%d\n" !ans


let a, b, c, x = Scanf.scanf " %d %d %d %d" @@ fun a b c d -> a, b, c, d
let ans = ref 0
let _ =
  for i = 0 to a do
    for j = 0 to b do
      for k = 0 to c do if 500 * i + 100 * j + 50 * k = x then incr ans done done done;
  Printf.printf "%d\n" !ans


let fold_int f a n =
  let rec fold f a i n =
    if i >= n then
      a
    else
      fold f (f a i) (i + 1) n
  in
    fold f a 0 n

(* O(n^3) (n = max {a, b, c}) *)
let solve a b c x =
  fold_int
    (fun sofar_a i ->
      fold_int
        (fun sofar_b j ->
          fold_int
            (fun sofar_c k ->
              if 500 * i + 100 * j + 50 * k = x then
                sofar_c + 1
              else
                sofar_c)
            sofar_b
            (c + 1))
        sofar_a
        (b + 1))
    0
    (a + 1)

let _ =
  let a = read_int () in
  let b = read_int () in
  let c = read_int () in
  let x = read_int () in
  solve a b c x |> string_of_int |> print_endline


let solve a b c x =
  let rec f acc i j k =
    if i > a then acc
    else if j > b then f acc (i+1) 0     0
    else if k > c then f acc i     (j+1) 0
    else f (acc + if 500*i + 100*j + 50*k = x then 1 else 0) i j (k+1)
  in f 0 0 0 0

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


let rec seq a b = if a > b then [] else a :: seq (a+1) b
let (>>=) x f = List.map f x |> List.concat
let () =
  Scanf.scanf "%d %d %d %d" @@ fun a b c x ->
    (seq 0 a >>= fun i ->
     seq 0 b >>= fun j ->
     seq 0 c >>= fun k ->
       [if 500*i + 100*j + 50*k = x then 1 else 0])
    |> List.fold_left (+) 0
    |> Printf.printf "%d\n"


let () = Scanf.scanf "%d %d %d %d" @@ fun a b c x ->
  Printf.printf "%d\n" @@ List.length @@ List.filter (( = ) x) @@
    List.concat @@ Array.to_list @@ Array.init (a + 1) @@ fun i ->
      List.concat @@ Array.to_list @@ Array.init (b + 1) @@ fun j ->
        Array.to_list @@ Array.init (c + 1) @@ fun k -> 500 * i + 100 * j + 50 * k


let get_4_ints () = Scanf.scanf "%d %d %d %d" (fun u v w x -> u,v,w,x)
module Rep = struct
	let __range ?(stride=1) u v =
		let rec f0 i = if i>v then [] else i::(f0 (i+stride))
    in f0 u
  let __ls_product l r =
    List.map (fun u -> List.map (fun v -> u,v) r) l
    |> List.concat
	let init f = f ()
	let rep ?(stride=1) f t = __range ~stride f t
	let (=+>) v p = (v,p)
	let (=>) (v,p) q = (v,__ls_product p q)
	let (=>>) (v,p) f = List.fold_left (fun x y -> f x y) v p
end

let () =
  let (a,b,c,x) = get_4_ints ()
  in let open Rep in
  init (fun () -> 0)
  =+> rep 0 a => rep 0 b => rep 0 c =>>
  (fun a ((u,v),w) -> if u*500+v*100+w*50=x then a+1 else a)
  |> Printf.printf "%d\n"


let main () =
  let a = Scanf.scanf "%d\n" Batteries.identity in
  let b = Scanf.scanf "%d\n" Batteries.identity in
  let c = Scanf.scanf "%d\n" Batteries.identity in
  let x = Scanf.scanf "%d\n" Batteries.identity in
  let xs = ref [] in
  for i = 0 to a do
    for j = 0 to b do
      for k = 0 to c do
        xs := (i,j,k) :: !xs;
      done
    done
  done;
  let count = List.fold_left (fun acc (a,b,c) -> if 500 * a + 100 * b + 50 * c == x then acc + 1 else acc) 0 !xs in
  Printf.printf "%d\n" count

let _ = main ()


let main () =
  let a = Scanf.scanf "%d\n" Batteries.identity in
  let b = Scanf.scanf "%d\n" Batteries.identity in
  let c = Scanf.scanf "%d\n" Batteries.identity in
  let x = Scanf.scanf "%d\n" Batteries.identity in
  let xs = ref [] in
  for i = 0 to a do
    for j = 0 to b do
      for k = 0 to c do
        xs := (a,b,c) :: !xs;
      done
    done
  done;
  let count = List.fold_left (fun acc (a,b,c) -> if 500 * a + 100 * b + 50 * c = x then acc + 1 else acc) 0 !xs in
  Printf.printf "%d\n" count

let _ = main ()


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

let rec calc a' b' tmp =
  let rec aux c' tmp' =
    if c' > c then tmp'
    else if a' * 500 + b' * 100 + c' * 50 = x then aux (c' + 1) (tmp' + 1)
    else aux (c' + 1) tmp'
  in
  aux 0 tmp

let rec secound a' tmp =
  let rec aux b' tmp' =
    if b' > b then tmp'
    else
      let tmp'' = calc a' b' tmp' in
      aux (b' + 1) tmp''
  in
  aux 0 tmp

let rec main a' tmp =
  if a' > a then tmp
  else
    let tmp' = secound a' tmp in
    main (a' + 1) tmp'

let () = main 0 0 |> Printf.printf "%d\n"


open Scanf
open Printf

let rec for_iter a b f = if a >= b then () else (f a; for_iter (a+1) b f)
let rec for_iterr a b f = if a <= b then () else (f a; for_iter (a-1) b f)
let rec for_iters a b s f = if (a-b >= 0) <> (s >= 0) then () else (f a; for_iter (a+s) b f)

let read_int () = scanf " %d" (fun x -> x)
let rec read_ints n = if n = 0 then [] else let i = read_int () in i :: read_ints (n-1)
let read_ints_arr n = Array.init n (fun _ -> read_int ())
let read_ints_arr_ref n = Array.init n (fun _ -> ref (read_int ()))

let solve a b c x =
  let rec f u v w acc =
    let acc' = acc + if 500 * u + 100 * v + 50 * w = x then 1 else 0 in
    if w < c then f u v (w+1) acc'
    else if v < b then f u (v+1) 0 acc'
    else if u < a then f (u+1) 0 0 acc'
    else acc'
  in f 0 0 0 0

let () =
  let a = read_int () in
  let b = read_int () in
  let c = read_int () in
  let x = read_int () in
  printf "%d\n" (solve a b c x)


let (a,b,c,x) = Scanf.scanf "%d\n%d\n%d\n%d\n" (fun a b c x ->a,b,c,x ) in

    let x' = if (x mod 50) = 0 then x/50 else 0 in

    let rec judge (x,a,b,c) =
      match x,a,b,c with
      | 0,_,_,_ -> 1
      | _,0,b,c ->
         begin
         match b,c with
         | 0,_ ->
            begin
            match c with
            | 0 -> 0
            | _ -> judge (x-1,0,0,c-1)
            end
         | _, _-> judge (x-2,0,b-1,c) + judge (x, 0, 0, c)
         end

      | _,a,b,c ->
         judge(x-10,a-1,b,c) + judge(x,0,b,c)
    in

    let ans = judge (x',a,b,c) in

    print_int ans
;;


open Scanf
open Printf
let () = scanf "%d %d" (fun a b -> if (a * b) mod 2 = 1 then  "Odd" else "Even")
  |> printf "%s\n"

Score : 100 points

Problem Statement

AtCoDeer the deer found two positive integers, a and b.
Determine whether the product of a and b is even or odd.

Constraints

1 ≤ a,b ≤ 10000

a and b are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

If the product is odd, print Odd; if it is even, print Even.

Sample Input 1

3 4

Sample Output 1

Even

As 3 × 4 = 12 is even, print Even.

Sample Input 2

1 21

Sample Output 2

Odd

As 1 × 21 = 21 is odd, print Odd.


open Core

type input = {a: int; b: int}

let i = Scanf.scanf "%d %d" (fun a b -> {a; b})

let even = (i.a * i.b) mod 2 = 0

let () = print_endline (if even then "Even" else "Odd")


let () = Scanf.scanf "%d %d" (fun a b ->
    let num = a * b in
    if num mod 2 = 0 then
    print_string("Even")
    else
    print_string("Odd")
    )


let a, b = Scanf.scanf "%d %d\n" @@ fun a b -> a, b
let ans = if a * b mod 2 = 0 then "Even" else "Odd"
let () = print_endline ans


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

let solve = function
  | (a, b) when (a * b) mod 2 = 0 -> "Even"
  | _ -> "Odd"

let () =
  (a, b)
    |> solve
    |> Printf.printf "%s\n"


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

let () =
  ()
    |> (fun () -> let c = a * b in if c mod 2 = 0 then "Even" else "Odd")
    |> Printf.printf "%s\n"


let _ = Scanf.sscanf (read_line ()) "%d %d" (fun a b -> if (a*b)mod 2 = 1 then "Odd" else "Even") |> print_endline


open Printf
open Scanf

let solve a b =
  match a mod 2, b mod 2 with
    0, _ -> "Even"
  | _, 0 -> "Even"
  | _, _ -> "Odd"

let () =
  scanf "%d %d " solve |> printf "%s\n"


(* Using Scanf for standard input *)
open Scanf;;

let () = Scanf.scanf "%d %d"
           (fun a b -> print_endline
                         (if (a*b) mod 2 = 0 then "Even" else "Odd"));;

exit 0;;


let a, b = Scanf.scanf " %d %d" @@ fun a b -> a, b
let _ = print_endline @@ if a * b mod 2 = 1 then "Odd" else "Even"


let f a b = if (a*b) mod 2 = 0 then "Even" else "Odd";;
let () = Scanf.scanf "%d %d" f
|> Printf.printf "%s\n"


Scanf.scanf "%d %d" @@ fun a b ->
  print_endline @@ if a * b mod 2 = 0 then "Even" else "Odd"


let () = Scanf.scanf "%d %d" (fun a b ->
  print_endline @@ if a*b mod 2 = 0 then "Even" else "Odd")


let () =
  let a, b = Scanf.scanf "%d %d\n" (fun x y -> x, y) in
  Printf.printf "%s\n" (if a*b mod 2 = 0 then "Even" else "Odd")


let () =
  let (a, b) = Scanf.scanf "%d %d" (fun x y -> (x, y)) in
    print_string @@ if (a * b) mod 2 = 0 then "Even\n" else "Odd\n"


let f a b = if a * b mod 2 = 0
  then Printf.printf "Even"
  else Printf.printf "Odd"

let () = Scanf.bscanf Scanf.Scanning.stdin "%d %d" f


let () =
  let a,b = Scanf.scanf "%d %d " (fun a b -> a,b) in
  Printf.printf "%s\n" (if (a * b) mod 2 = 1 then "Odd" else "Even");


let main () =
  let i, j = Scanf.scanf "%d %d\n" (fun i j -> i, j) in
  if (i * j) mod 2 = 0 then Printf.printf "%s\n" "Even" else Printf.printf "%s\n" "Odd"

let _ = main ()


let () =
  Scanf.scanf "%d %d" (fun a b -> if a * b mod 2 = 1 then "Odd" else "Even")
  |> print_endline


let () = Scanf.scanf "%d %d" (fun a b -> if (a*b) mod 2 = 0 then "Even" else "Odd") |> print_endline


open Scanf
open Printf

let flip f x y = f y x

let rec range a b = if a >= b then [] else a :: range (a+1) b

let read_int () = scanf " %d" (fun x -> x)
let rec read_ints n = if n = 0 then [] else let i = read_int () in i :: read_ints (n-1)

let () =
  let a = read_int () in let b = read_int () in
  printf "%s\n" (if (a * b) mod 2 = 0 then "Even" else "Odd")


let a = int_of_string(read_line());;
let b = int_of_string(read_line());;
if (a * b) mod 2 = 0 then print_string("Even") else print_string("Odd");;


let solve a b =
  if (a * b) mod 2 = 1 then "Odd" else "Even"

let () = Scanf.scanf "%d %d" solve |> print_string


let ans = Scanf.scanf "%d %d\n" (fun x y -> if (x*y) mod  2 = 0 then "Even" else "Odd") in

print_string ans
;;


let main () =
  let n, y = Scanf.sscanf (read_line ()) "%d %d" (fun a b -> (a, b)) in
  let numOf10000 = ref (-1) in
  let numOf5000 = ref (-1) in
  let numOf1000 = ref (-1) in
  (
    for i = 0 to n do
      for j = 0 to n - i do
        let k = n - i - j in
        let sum = 10000 * i + 5000 * j + 1000 * k in
        if sum = y
        then
          (
            numOf10000 := i;
            numOf5000 := j;
            numOf1000 := k
          )
      done
    done;
    Printf.printf "%d %d %d" !numOf10000 !numOf5000 !numOf1000;
    print_newline ()
  )

let _ = main ()

Score : 300 points

Problem Statement

The commonly used bills in Japan are 10000-yen, 5000-yen and 1000-yen bills. Below, the word "bill" refers to only these.

According to Aohashi, he received an otoshidama (New Year money gift) envelope from his grandfather that contained N bills for a total of Y yen, but he may be lying. Determine whether such a situation is possible, and if it is, find a possible set of bills contained in the envelope. Assume that his grandfather is rich enough, and the envelope was large enough.

Constraints

1 ≤ N ≤ 2000

1000 ≤ Y ≤ 2 × 10^7

N is an integer.

Y is a multiple of 1000.

Input

Input is given from Standard Input in the following format:

N Y

Output

If the total value of N bills cannot be Y yen, print -1 -1 -1.

If the total value of N bills can be Y yen, let one such set of bills be "x 10000-yen bills, y 5000-yen bills and z 1000-yen bills", and print x, y, z with spaces in between. If there are multiple possibilities, any of them may be printed.

Sample Input 1

9 45000

Sample Output 1

4 0 5

If the envelope contained 4 10000-yen bills and 5 1000-yen bills, he had 9 bills and 45000 yen in total. It is also possible that the envelope contained 9 5000-yen bills, so the output 0 9 0 is also correct.

Sample Input 2

20 196000

Sample Output 2

-1 -1 -1

When the envelope contained 20 bills in total, the total value would be 200000 yen if all the bills were 10000-yen bills, and would be at most 195000 yen otherwise, so it would never be 196000 yen.

Sample Input 3

1000 1234000

Sample Output 3

14 27 959

There are also many other possibilities.

Sample Input 4

2000 20000000

Sample Output 4

2000 0 0


open Core

let n, y = Scanf.scanf "%d %d" (fun n y -> (n, y / 1000))

let pred i j = (10 * i) + (5 * j) + (n - i - j) = y

let rec find i j =
  if i > n then (-1, -1, -1)
  else if i + j > n then find (i + 1) 0
  else if pred i j then (i, j, n - i - j)
  else find i (j + 1)

let a, b, c = find 0 0

let () = Printf.printf "%d %d %d\n" a b c


let i_inp_list : unit -> int list = fun () ->
  let inp_split = Str.split (Str.regexp " ") (input_line stdin) in
  List.map (fun x -> int_of_string x) (inp_split)

let () =
  let (n, y) = Scanf.sscanf (input_line stdin) "%d %d" (fun a b -> (a, b)) in
  let solve =
    let rec loop_10000 i =
      if i > n || 10000 * i > y then None
      else
        match loop_5000 i 0 with
          None -> loop_10000 (i + 1)
        | Some x -> Some x
    and loop_5000 i j =
      if i + j > n || 10000 * i + 5000 * j > y then None
      else
        let k = (y - 10000 * i - 5000 * j) / 1000 in
        if i + j + k = n then Some (i, j, k)
        else loop_5000 i (j + 1)
    in
    match loop_10000 0 with
      None -> (-1,-1,-1)
    | Some (i,j,k) -> (i,j,k)
  in
  let (i, j, k) = solve
  in
  print_int i;
  print_char ' ';
  print_int j;
  print_char ' ';
  print_int k;
  print_newline ()


let i_inp_list : unit -> int list = fun () ->
  let inp_split = Str.split (Str.regexp " ") (input_line stdin) in
  List.map (fun x -> int_of_string x) (inp_split)

let () =
  let (n, y) = Scanf.sscanf (input_line stdin) "%d %d" (fun a b -> (a, b)) in
  let solve =
    let rec loop_10000 i =
      if i > n || 10000 * i > y then None
      else
        match loop_5000 i 0 with
          None -> loop_10000 (i + 1)
        | Some x -> Some x
    and loop_5000 i j =
      if i + j > n || 10000 * i + 5000 * j > y then None
      else
        let k = (y - 10000 * i + 5000 * j) / 1000 in
        if i + j + k = n then Some (i, j, k)
        else loop_5000 i (j + 1)
    in
    match loop_10000 0 with
      None -> (-1,-1,-1)
    | Some (i,j,k) -> (i,j,k)
  in
  let (i, j, k) = solve
  in
  print_int i;
  print_char ' ';
  print_int j;
  print_char ' ';
  print_int k;
  print_newline ()


(* O(n^2) *)
let n, m = Scanf.scanf " %d %d" @@ fun a b -> a, b
let _ =
  for x = 0 to n do
    for y = 0 to n - x do
      let z = n - x - y in
      if 10000 * x + 5000 * y + 1000 * z = m then (Printf.printf "%d %d %d \n" x y z; exit 0) done done;
  print_endline "-1 -1 -1"


(* O(n^2) *)
let n, m = Scanf.scanf " %d %d" @@ fun a b -> a, b
let _ =
  for x = 0 to n do
    for y = 0 to n do
      let z = n - x - y in
      if z >= 0 && 10000 * x + 5000 * y + 1000 * z = m then (Printf.printf "%d %d %d \n" x y z; exit 0) done done;
  print_endline "-1 -1 -1"


(* O(n) *)
let find_fold_int f a n =
  let rec find a i =
    if i >= n then
      a, false
    else
      let (res, detected) as r = f a i in
        if detected then r else find res (i + 1)
  in
    find a 0

(* O(N^2) *)
let solve n y =
  find_fold_int
    (fun sofar_a a ->
      find_fold_int
        (fun sofar_b b ->
          let c = n - a - b in
          if c >= 0 && 10000 * a + 5000 * b + 1000 * c = y then (* cが負の場合は除外 *)
            (a, b, c), true
          else
            sofar_b, false)
        sofar_a
        (n + 1))
    (-1, -1, -1)
    (n + 1) |> fst

let read_ints () = read_line () |> Str.split (Str.regexp " ") |> List.map int_of_string

let _ =
  match read_ints () with
  | [n; y] -> let (a, b, c) = solve n y in Printf.printf "%d %d %d\n" a b c
  | _ -> ()


(* O(n) *)
let find_fold_int f a n =
  let rec find a i =
    if i >= n then
      a, false
    else
      let (res, detected) as r = f a i in
        if detected then r else find res (i + 1)
  in
    find a 0

(* O(n^2) *)
let solve n y =
  find_fold_int
    (fun sofar_a a ->
      find_fold_int
        (fun sofar_b b ->
          let c = n - a - b in
          if 10000 * a + 5000 * b + 1000 * c = y then
            (a, b, c), true
          else
            sofar_b, false)
        sofar_a
        (n - a + 1))
    (-1, -1, -1)
    (n + 1) |> fst

let read_ints () = read_line () |> Str.split (Str.regexp " ") |> List.map int_of_string

let _ =
  match read_ints () with
  | [n; y] -> let (a, b, c) = solve n y in Printf.printf "%d %d %d\n" a b c
  | _ -> ()


let rec range m n = if m > n then [] else m :: range (m+1) n
let (>>=) xs f = List.map f xs |> List.concat

let solve n y =
  (range 0 n >>= fun a ->
   range 0 (n-a) >>= fun b ->
      if 10000*a + 5000*b + 1000*(n-a-b) = y then [a,b] else [])
  |> function
      | [] -> -1, -1, -1
      | (a, b)::_ -> a, b, n-a-b

let () =
  Scanf.scanf "%d %d\n" @@ fun n y ->
  let a,b,c = solve n y in
  Printf.printf "%d %d %d\n" a b c


let rec seq a b = if a > b then [] else a :: seq (a+1) b
let (>>=) x f = List.map f x |> List.concat
let () =
  Scanf.scanf "%d %d" @@ fun n y ->
    (seq 0 n >>= fun i ->
     seq 0 (n-i) >>= fun j ->
       if 10000*i + 5000*j + 1000*(n-i-j) = y then [i, j, n-i-j] else [])
    |> function
      | (i,j,k)::_ -> Printf.printf "%d %d %d\n" i j k
      | [] -> print_endline "-1 -1 -1"


let get_2_ints () = Scanf.scanf "%d %d" (fun u v -> u,v)

let () =
  let (n,y) = get_2_ints ()
  in let r = ref (-1,-1,-1)
  in
  for i=0 to n do
    for j=0 to n-i do
      let k = n-i-j
      in if i*10000+j*5000+k*1000=y then r:=(i,j,k)
    done
  done;
  !r |> (fun (u,v,w) -> Printf.printf "%d %d %d\n" u v w);


let get_2_ints () = Scanf.scanf "%d %d" (fun u v -> u,v)

module Rep = struct
	let __range ?(stride=1) u v =
    let rec f0 i a = if i>v then a else f0 (i+stride) (i::a)
    in f0 u []
  let __ls_product l r =
    List.map (fun u -> List.map (fun v -> u,v) r) l
    |> List.concat
	let init f = f ()
  let rep ?(stride=1) f t = __range ~stride f t
	let (=+>) v p = (v,p)
	let (=>) (v,p) q = (v,__ls_product p q)
	let (=>>) (v,p) f = List.fold_left f v p
end

let () =
  let (n,y) = get_2_ints ()
  in let open Rep
  in init (fun () -> (-1,-1,-1))
  =+> rep 0 n => rep 0 n =>>
  (fun op (u,v) ->
    let w = n-u-v in
    if w>=0 && u*10000+v*5000+w*1000=y then (u,v,w) else op)
  |> (fun (u,v,w) -> Printf.printf "%d %d %d\n" u v w)


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

  let res = ref [-1; -1; -1] in

  for i=0 to n do
    for j=0 to n-i do
      let k = n-i-j in
      if 10000 * i + 5000 * j + 1000 * k = y then
        res := [i;j;k]
      else ()
    done;
  done;

  Printf.printf "%d %d %d\n" (List.nth !res 0) (List.nth !res 1) (List.nth !res 2)


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

  let res = ref [-1; -1; -1] in
  for i=0 to n do
    for j=0 to n-i do
      for k=0 to n-i-j do
        if 10000 * i + 5000 * j + 1000 * k = y then
          res := [i;j;k]
        else ()
      done;
    done;
  done;
  Printf.printf "%d %d %d\n" (List.nth !res 0) (List.nth !res 1) (List.nth !res 2)


let main () =
  let n, x = Scanf.scanf "%d %d\n" (fun a b -> a, b) in
  let xs = ref [] in
  for i = 0 to n do
    for j = 0 to n do
      if i + j <= n then xs := (i,j, n - (i+j)) :: !xs;
    done;
  done;
  List.iter (fun (i,j,k) -> if 10000 * i + 5000 * j + 1000 * k = x then (Printf.printf "%d %d %d\n" i j k; exit 0)) !xs;
  Printf.printf "-1 -1 -1\n"

let _ = main ()


let main () =
  let n, x = Scanf.scanf "%d %d\n" (fun a b -> a, b) in
  let xs = ref [] in
  for i = 0 to n do
    for j = 0 to n do
      for k = 0 to n do
        if i + j + k = n then xs := (i,j,k) :: !xs;
      done;
    done;
  done;
  List.iter (fun (i,j,k) -> if 10000 * i + 5000 * j + 1000 * k = x then (Printf.printf "%d %d %d\n" i j k; exit 0)) !xs;
  Printf.printf "-1 -1 -1\n"

let _ = main ()


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

let rec solve tt =
  let rec aux tt ft =
    if tt + ft > n then None
    else
      let t = n - tt - ft in
      if tt * 10000 + ft * 5000 + t * 1000 = y then Some (tt, ft, t) else aux tt (ft + 1)
  in
  if tt > n then (-1, -1, -1)
  else match aux tt 0 with Some x -> x | None -> solve (tt + 1)

let () =
  let tt, ft, t = solve 0 in Printf.printf "%d %d %d\n" tt ft t


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

let rec calc tt ft =
  let t = n - tt - ft in
  if tt * 10000 + ft * 5000 + t * 1000 = y then Some (tt, ft, t) else None

let rec inner tt =
  let rec aux ft =
    if tt + ft > n then None
    else match calc tt ft with Some x -> Some x | None -> aux (ft + 1)
  in
  aux 0

let rec main tt =
  if tt > n then (-1, -1, -1)
  else match inner tt with Some x -> x | None -> main (tt + 1)

let p (tt, ft, t) = Printf.printf "%d %d %d\n" tt ft t

let () = main 0 |> p


exception Exodus
let call_cc f =
  let return = ref (fun () -> raise Not_found) in
  try f (fun x -> (return := fun () -> x); raise Exodus)
  with Exodus -> !return ()

let () = print_endline @@ Scanf.scanf "%d %d" (fun n y ->
  call_cc (fun k ->
    for i = 0 to min n ((y / 1000 - n) / 9) do
      for j = 0 to min (n - i) ((y / 1000 - n - 9 * i) / 4) do
        if 9 * i + 4 * j = y / 1000 - n then
          k (Printf.sprintf "%d %d %d" i j (n - i - j))
      done
    done;
    "-1 -1 -1"))


Scanf.scanf "%d %d" (fun n h ->
    let ab = Array.init n (fun _ -> Scanf.scanf " %d %d" (fun a b -> -b, a)) in
    Array.sort compare ab;

    let amax = Array.fold_left (fun acc (_, a) -> max acc a) 0 ab in

    let phase2 damage count =
        Printf.printf "%d\n" @@ count + (h - damage + amax - 1) / amax
    in

    let rec loop i damage count =
        if i = n then phase2 damage count else
            let (b, _) = ab.(i) in
            let damage, count = if -b >= amax then damage - b, count + 1
                                              else damage, count
            in
            if damage >= h then Printf.printf "%d\n" count else
                loop (i + 1) damage count
    in
    loop 0 0 0
)

Score : 400 points

Problem Statement

You are going out for a walk, when you suddenly encounter a monster. Fortunately, you have N katana (swords), Katana 1, Katana 2, …, Katana N, and can perform the following two kinds of attacks in any order:

Wield one of the katana you have. When you wield Katana i (1 ≤ i ≤ N), the monster receives a_i points of damage. The same katana can be wielded any number of times.

Throw one of the katana you have. When you throw Katana i (1 ≤ i ≤ N) at the monster, it receives b_i points of damage, and you lose the katana. That is, you can no longer wield or throw that katana.

The monster will vanish when the total damage it has received is H points or more. At least how many attacks do you need in order to vanish it in total?

Constraints

1 ≤ N ≤ 10^5

1 ≤ H ≤ 10^9

1 ≤ a_i ≤ b_i ≤ 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N H
a_1 b_1
:
a_N b_N

Output

Print the minimum total number of attacks required to vanish the monster.

Sample Input 1

1 10
3 5

Sample Output 1

3

You have one katana. Wielding it deals 3 points of damage, and throwing it deals 5 points of damage. By wielding it twice and then throwing it, you will deal 3 + 3 + 5 = 11 points of damage in a total of three attacks, vanishing the monster.

Sample Input 2

2 10
3 5
2 6

Sample Output 2

2

In addition to the katana above, you also have another katana. Wielding it deals 2 points of damage, and throwing it deals 6 points of damage. By throwing both katana, you will deal 5 + 6 = 11 points of damage in two attacks, vanishing the monster.

Sample Input 3

4 1000000000
1 1
1 10000000
1 30000000
1 99999999

Sample Output 3

860000004

Sample Input 4

5 500
35 44
28 83
46 62
31 79
40 43

Sample Output 4

9


Scanf.scanf "%d %d" (fun n h ->
    let ab = Array.init n (fun _ -> Scanf.scanf " %d %d" (fun a b -> -b, a)) in

    let amax = Array.fold_left (fun acc (_, a) -> max acc a) 0 ab in

    let phase2 damage count =
        Printf.printf "%d\n" @@ count + (h - damage + amax - 1) / amax
    in

    let rec loop i damage count =
        if i = n then phase2 damage count else
            let (b, _) = ab.(i) in
            let damage, count = if -b >= amax then damage - b, count + 1
                                              else damage, count
            in
            if damage >= h then Printf.printf "%d\n" count else
                loop (i + 1) damage count
    in
    loop 0 0 0
)


let m, c, (n, h) = ref 0, ref 0, Scanf.scanf " %d %d" @@ fun a b -> a, ref b
let bs = Array.init n @@ fun _ -> Scanf.scanf " %d %d" @@ fun a b -> m := max !m a; b
let _ = Array.(sort (fun x y -> y - x) bs; iter (fun b -> if b > !m && !h > 0 then (incr c; h := !h - b)) bs); Printf.printf "%d\n" @@ !c + (max 0 !h + !m - 1) / !m


let m, c, (n, h) = ref 0, ref 0, Scanf.scanf " %d %d" @@ fun a b -> a, ref b
let bs = Array.init n @@ fun _ -> Scanf.scanf " %d %d" @@ fun a b -> m := max !m a; b
let _ = Array.(sort (fun x y -> y - x) bs; iter (fun b -> if b > !m then (if !h > 0 then (incr c; h := !h - b))) bs); Printf.printf "%d\n" @@ !c + (max 0 !h + !m - 1) / !m


let n, h = Scanf.scanf " %d %d" @@ fun a b -> a, ref b
let m, ans = ref 0, ref 0
let f _ = Printf.printf "%d\n" !ans; exit 0
let bs = Array.init n @@ fun _ -> Scanf.scanf " %d %d" @@ fun a b -> m := max !m a; b
let _ = Array.(sort (fun x y -> y - x) bs; iter (fun b -> if b > !m then (incr ans; h := !h - b; if !h <= 0 then f ())) bs); ans := !ans + (!h + !m - 1) / !m; f ()


let n, h = Scanf.scanf " %d %d" @@ fun a b -> a, ref b
let ab_s, ans = Array.make (n + n) (0, false), ref 0
let f _ = Printf.printf "%d\n" !ans; exit 0
let _ = for i = 0 to n - 1 do Scanf.scanf " %d %d" @@ fun a b -> ab_s.(2 * i) <- a, false; ab_s.(2 * i + 1) <- b, true done;
  Array.(sort (fun x y -> compare y x) ab_s; iter (fun (x, b) -> if b then (h := !h - x; incr ans; if !h <= 0 then f ()) else (ans := !ans + (!h + x - 1) / x; f ()))) ab_s


let () =
  let n, h = Scanf.scanf "%d %d " (fun a b -> a, b) in
  let l =  Array.to_list @@ Array.init n (fun _ -> Scanf.scanf "%d %d " (fun a b -> (a, b))) in

  let a = List.map (fun (a,b) -> a) l in
  let b = List.map (fun (a,b) -> b) l in

  let a =  List.sort (fun x y -> if x > y then -1 else if x = y then 0 else 1) a in
  let b =  List.sort (fun x y -> if x > y then -1 else if x = y then 0 else 1) b in

  let max_a = List.hd a in

  let bs = List.filter (fun x -> x > max_a) b in

  let rec f last res = function
    | [] -> res
    | hd :: tl ->
      let last = last - hd in f last (last :: res) tl
  in
  let bd1 = f h [] bs in
  let bd2 = List.filter (fun x -> x > 0) bd1 in
  let bd1_len = List.length bd1 in
  let bd2_len = List.length bd2 in

  if bd1_len <> bd2_len then Printf.printf "%d\n" @@ bd2_len + 1
  else
    let b_sum = List.fold_left (+) 0 bs in

    Printf.printf "%d\n" @@ bd1_len +
                            (
                              if (h - b_sum) mod max_a = 0 then
                                (h - b_sum) / max_a
                              else
                                (h - b_sum) / max_a + 1
                            )


exception Exodus
let call_cc f =
  let return = ref (fun () -> raise Not_found) in
  try f (fun x -> (return := fun () -> x); raise Exodus)
  with Exodus -> !return ()

let () =
  let n, h = Scanf.scanf "%d %d\n" (fun n h -> n, h) in
  let abs = Array.to_list @@ Array.init n (fun _ ->
    Scanf.scanf "%d %d\n" (fun a b -> a, b)) in
  let max_a = List.fold_left max 0 @@ List.map fst abs in
  Printf.printf "%d\n" @@ call_cc (fun k ->
    List.map snd abs
    |> List.sort (fun a b -> compare b a)
    |> List.filter (( <= ) max_a)
    |> List.fold_left (fun (n, h) b ->
        if h <= b then k (n + 1)
        else (n + 1, h - b)) (0, h)
    |> (fun (n, h) ->
        n + (h + max_a - 1) / max_a))


exception Exodus
let call_cc f =
  let return = ref (fun () -> raise Exodus) in
  try f (fun x -> return := (fun () -> x); raise Exodus)
  with Exodus -> !return ()

let () =
  let n, h = Scanf.scanf "%d %d\n" (fun n h -> n, h) in
  let abs = Array.to_list @@ Array.init n (fun _ ->
    Scanf.scanf "%d %d\n" (fun a b -> a, b)) in
  let max_a = List.fold_left max 0 @@ List.map fst abs in
  Printf.printf "%d\n" @@ call_cc (fun k ->
    List.map snd abs
    |> List.sort (fun a b -> compare b a)
    |> List.fold_left (fun (n, h) b ->
        if h <= b then k (n + 1)
        else (n + 1, h - b)) (0, h)
    |> (fun (n, h) -> n + (h + max_a - 1) / max_a))


let () =
  let n, h = Scanf.scanf "%d %d\n" (fun n h -> n, h) in
  let abs = Array.to_list @@ Array.init n (fun _ -> Scanf.scanf "%d %d\n" (fun a b -> a, b)) in
  let max_a = List.fold_left max 0 @@ List.map fst abs in
  try
    List.map snd abs
    |> List.sort (fun a b -> compare b a)
    |> List.filter (( <= ) max_a)
    |> List.fold_left (fun (n, h) b ->
        if h <= b then (Printf.printf "%d\n" (n + 1); raise Not_found)
        else (n + 1, h - b)) (0, h)
    |> (fun (n, h) -> n + (h + max_a - 1) / max_a)
    |> Printf.printf "%d\n"
  with Not_found -> ()


open Core

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

let nums = List.range 1 (n + 1)

let rec digits i = if i <= 0 then 0 else (i mod 10) + digits (i / 10)

let s =
  nums
  |> List.map ~f:(fun n ->
         let d = digits n in
         if a <= d && d <= b then n else 0)
  |> List.fold_left ~init:0 ~f:( + )

let () = Printf.printf "%d\n" s

Score : 200 points

Problem Statement

Find the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive).

Constraints

1 \leq N \leq 10^4

1 \leq A \leq B \leq 36

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive).

Sample Input 1

20 2 5

Sample Output 1

84

Among the integers not greater than 20, the ones whose sums of digits are between 2 and 5, are: 2,3,4,5,11,12,13,14 and 20. We should print the sum of these, 84.

Sample Input 2

10 1 2

Sample Output 2

13

Sample Input 3

100 4 16

Sample Output 3

4554


let fold_int f a n =
  let rec fold f a i n =
    if i >= n then
      a
    else
      fold f (f a i) (i + 1) n
  in
    fold f a 0 n

let int_of_digit c = int_of_char c - 48 (* 48 = int_of_char '0' *)

(* O(log(n)) *)
let sum_digits n =
  let rec loop sofar m =
    if m <= 0 then
      sofar
    else
      loop (sofar + m mod 10) (m / 10)
  in
    loop 0 n

(* O(n * log(n)) *)
let solve n a b =
  fold_int
    (fun sum i ->
      let i = i + 1 in
      let d = sum_digits i in
      sum + if a <= d && d <= b then i else 0)
    0
    n

let read_ints () = read_line () |> Str.split (Str.regexp " ") |> List.map int_of_string

let _ =
  match read_ints () with
  | [n; a; b] -> solve n a b |> string_of_int |> print_endline
  | _ -> ()


let fold_int f a n =
  let rec fold f a i n =
    if i >= n then
      a
    else
      fold f (f a i) (i + 1) n
  in
    fold f a 0 n

let int_of_digit c = int_of_char c - 48 (* 48 = int_of_char '0' *)

let sum_digits n =
  let s = string_of_int n in
  fold_int
    (fun sum i -> sum + int_of_digit s.[i])
    0
    (String.length s)

let solve n a b =
  fold_int
    (fun sum i ->
      let i = i + 1 in
      let d = sum_digits i in
      sum + if a <= d && d <= b then i else 0)
    0
    n

let read_ints () = read_line () |> Str.split (Str.regexp " ") |> List.map int_of_string

let _ =
  match read_ints () with
  | [n; a; b] -> solve n a b |> string_of_int |> print_endline
  | _ -> ()


let rec sum_of_digits x res =
  if x = 0 then res else
  sum_of_digits (x/10) (res+x mod 10)

let solve n a b =
  let rec count m res =
    if m > n then res else
    let s = sum_of_digits m 0 in
    count (m+1) (res + if a <= s && s <= b then m else 0)
  in
  count 1 0

let () =
  Scanf.scanf "%d %d %d\n" @@ fun n a b ->
  Printf.printf "%d\n" @@ solve n a b


let rec seq a b = if a > b then [] else a :: seq (a+1) b
let rec dig_sum x = if x = 0 then 0 else x mod 10 + dig_sum (x / 10)
let () =
  Scanf.scanf "%d %d %d" @@ fun n a b ->
    seq 1 n
    |> List.fold_left (fun s x ->
        let d = dig_sum x in
        if a <= d && d <= b then s+x else s) 0
    |> Printf.printf "%d\n"


let () = Scanf.scanf "%d %d %d" @@ fun n a b ->
  Array.init n (fun i ->
    let s = string_of_int @@ i + 1 in
    Array.init (String.length s) (fun i -> Char.code s.[i] - Char.code '0')
    |> Array.fold_left ( + ) 0
    |> (fun n -> if a <= n && n <= b then i + 1 else 0))
  |> Array.fold_left ( + ) 0
  |> Printf.printf "%d\n"


let get_3_ints () = Scanf.scanf "%d %d %d" (fun u v w -> u,v,w)
let range ?(stride=1) u v =
  let rec f0 i a = if i>v then a else f0 (i+stride) (i::a)
  in f0 u []
let digits v =
  let rec f0 p a = if p=0 then a else f0 (p/10) ((p mod 10)::a)
  in f0 v []

let () =
  let (n,a,b) = get_3_ints ()
  in range 1 n
  |> List.fold_left (fun u v ->
    let k = v |> digits |> List.fold_left (+) 0
    in if a<=k&&k<=b then u+v else u
  ) 0
  |> Printf.printf "%d\n"


let () =
  let n,a,b = Scanf.scanf "%d %d %d " (fun a b c -> a,b,c) in

  let res = ref 0 in
  for i=1 to n do
    let cs = i |> string_of_int |> Batteries.String.to_list |> List.map (fun x -> int_of_char x - 48) in
    let sum = List.fold_left (+) 0 cs in
    if sum >= a && sum <= b then res := !res + i
  done;

  Printf.printf "%d\n" !res


let main () =
  let n,a,b = Scanf.scanf "%d %d %d\n" (fun a b c -> a, b, c) in
  let rec func acc n = if n = 0 then acc else func (acc + (n mod 10)) (n / 10) in
  let res = ref 0 in
  for i = 1 to n do
    let s = func 0 i in
    if a <= s && s <= b then res := !res + i;
  done;
  Printf.printf "%d\n" !res

let _ = main ()


let sum_of_digits n =
  let rec aux n' tmp =
    if n' < 1 then tmp else aux (n' / 10) (tmp + n' mod 10)
  in
  aux n 0

let solve n a b =
  let rec aux i ans =
    if i > n then ans
    else match sum_of_digits i with
      | x when a <= x && x <= b -> aux (i + 1) (ans + i)
      | _ -> aux (i + 1) ans
  in
  aux 1 0

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


open Scanf
open Printf

let flip f x y = f y x

let rec range a b = if a >= b then [] else a :: range (a+1) b

let read_int () = scanf " %d" (fun x -> x)
let rec read_ints n = if n = 0 then [] else let i = read_int () in i :: read_ints (n-1)

let rec for_iter a b f = if a >= b then [] else let x = f a in x :: for_iter (a+1) b f

let () =
  let n = read_int () in let a = read_int() in let b = read_int () in
  for_iter 0 (n+1) (fun i ->
    let v, w, x, y, z = i/10000, (i/1000) mod 10, (i/100) mod 10, (i/10) mod 10, i mod 10 in
    let s = v+w+x+y+z in
    if a <= s && s <= b then i else 0)
  |> List.fold_left (+) 0
  |> printf "%d\n"


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

let pr x = print_endline @@ string_of_int x

let (--) m n =
  let rec iter i res =
    if i >= m then iter (i - 1) (i::res)
    else res in
  let rec iter' i res =
    if i <= m then iter' (i + 1) (i::res)
    else res in
  if m <= n then iter n []
  else iter' n []

let digit_list_of_num n =
  let rec iter rem res =
    let top = rem mod 10 in
    if rem < 10 then top::res
    else iter (rem / 10) (top::res) in
  iter n []

let sum_digits n =  List.fold_left (+) 0 @@ digit_list_of_num n

let () = List.fold_left
           (fun sum x ->
             let s = sum_digits x in
             if s >= a && s <= b then sum + x
             else sum) 0 (1--n)
         |> pr


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

let pr x = print_endline (string_of_int x)

let (--) m n =
  let rec iter i res =
    if i >= m then iter (i - 1) (i::res)
    else res in
  let rec iter' i res =
    if i <= m then iter' (i + 1) (i::res)
    else res in
  if m <= n then iter n []
  else iter' n []

let digit_list_of_num n =
  let rec iter rem res =
    let top = rem mod 10 in
    if rem < 10 then top::res
    else iter (rem / 10) (top::res) in
  iter n []

let sum_digits n = digit_list_of_num n |> List.fold_left (+) 0

let () = List.fold_left
           (fun sum x ->
             let s = sum_digits x in
             if s >= a && s <= b then sum + x
             else sum) 0 (1--n)
         |> pr


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

let pr x = print_endline (string_of_int x)

let (--) m n =
  let rec iter i res =
    if i >= m then iter (i - 1) (i::res)
    else res in
  let rec iter' i res =
    if i <= m then iter' (i + 1) (i::res)
    else res in
  if m <= n then iter n []
  else iter' n []

let digit_list_of_num n =
  let digit n = int_of_float (floor (log10 (float_of_int n))) in
  let base digit = int_of_float (10.0 ** (float_of_int digit)) in
  let rec iter rem res =
    let base = digit rem
              |> base in
    let top = rem / base in
    if base = 1 then top :: res
    else iter (rem - (top * base)) (top :: res) in
  iter n []

let digit_sum n =
  digit_list_of_num n
  |> List.fold_left (fun sum x -> x + sum) 0

let () = List.fold_left
           (fun sum x ->
             let ds = digit_sum x in
             if ds >= a && ds <= b then
               sum + x
             else sum) 0 (1--n)
         |> pr


let () = Scanf.scanf "%s" (fun s ->
    int_of_string (Char.escaped s.[0])
  + int_of_string (Char.escaped s.[1])
  + int_of_string (Char.escaped s.[2])
  )
  |> Printf.printf "%d\n"

Score : 100 points

Problem Statement

Snuke has a grid consisting of three squares numbered 1, 2 and 3.
In each square, either 0 or 1 is written. The number written in Square i is s_i.

Snuke will place a marble on each square that says 1.
Find the number of squares on which Snuke will place a marble.

Constraints

Each of s_1, s_2 and s_3 is either 1 or 0.

Input

Input is given from Standard Input in the following format:

s_{1}s_{2}s_{3}

Output

Print the answer.

Sample Input 1

101

Sample Output 1

2

A marble will be placed on Square 1 and 3.

Sample Input 2

000

Sample Output 2

0

No marble will be placed on any square.


open Core

let i = Scanf.scanf "%c%c%c" (fun a b c -> [|a;b;c|])

let n = Array.count i (fun c -> Char.(c = '1'))

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


let () = Scanf.scanf "%d" (fun d ->
  print_endline @@ string_of_int (d/100 + d/10 mod 10 + d mod 2))


let () =
  Scanf.scanf "%d"
  @@ fun a -> Printf.printf "%d\n"
  @@ a / 100 + (a / 10 mod 10) + a mod 10


let n = read_int ()
let ans = n / 100 + (n / 10 - n / 100 * 10) + n mod 10
let () = print_int ans


Scanf.scanf " %1d%1d%1d" @@ fun a b c -> Printf.printf "%d\n" @@ a + b + c


let s = read_line ()
let ans = ref 0
let f i = if s.[i] = '1' then incr ans
let _ = f 0; f 1; f 2; Printf.printf "%d\n" !ans


let s = Array.init 3 @@ fun _ -> Scanf.scanf " %c" @@ function '1' -> 1 | _ -> 0
let _ = Array.fold_left (+) 0 s |> Printf.printf "%d\n"


let rec f s = if String.length s=0 then 0 else f (String.sub s 1 (String.length s-1)) + if s.[0]='1' then 1 else 0;;
Scanf.scanf "%s" f
|> Printf.printf "%d\n"


let string_count f s =
  let count = ref 0 in
  String.iter (fun c -> if f c then incr count) s;
  !count

let solve s = string_count ((=) '1') s

let _ = read_line () |> solve |> string_of_int |> print_endline


let rec solve x =
  if x = 0
  then 0
  else x mod 10 + solve (x/10)

let () =
  let s = Scanf.scanf "%d\n" (fun x -> x) in
  Printf.printf "%d\n" (solve s)