let () =
  Scanf.scanf "%d %d" @@ fun n k ->
  let a = Array.init n (fun _ -> Scanf.scanf " %d" ((+) 0)) in
  let dp = Array.make (k+1) 0 in
  for i = 1 to k do
    dp.(i) <-
      1 - Array.fold_left (fun s w -> if w <= i then min s dp.(i-w) else s) 1 a
  done;
  print_endline [|"Second"; "First"|].(dp.(k))

Score : 100 points

Problem Statement

There is a set A = \{ a_1, a_2, \ldots, a_N \} consisting of N positive integers.
Taro and Jiro will play the following game against each other.

Initially, we have a pile consisting of K stones.
The two players perform the following operation alternately, starting from Taro:

Choose an element x in A, and remove exactly x stones from the pile.

A player loses when he becomes unable to play.
Assuming that both players play optimally, determine the winner.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq K \leq 10^5

1 \leq a_1 < a_2 < \cdots < a_N \leq K

Input

Input is given from Standard Input in the following format:

N K
a_1 a_2 \ldots a_N

Output

If Taro will win, print First; if Jiro will win, print Second.

Sample Input 1

2 4
2 3

Sample Output 1

First

If Taro removes three stones, Jiro cannot make a move.
Thus, Taro wins.

Sample Input 2

2 5
2 3

Sample Output 2

Second

Whatever Taro does in his operation, Jiro wins, as follows:

If Taro removes two stones, Jiro can remove three stones to make Taro unable to make a move.

If Taro removes three stones, Jiro can remove two stones to make Taro unable to make a move.

Sample Input 3

2 7
2 3

Sample Output 3

First

Taro should remove two stones. Then, whatever Jiro does in his operation, Taro wins, as follows:

If Jiro removes two stones, Taro can remove three stones to make Jiro unable to make a move.

If Jiro removes three stones, Taro can remove two stones to make Jiro unable to make a move.

Sample Input 4

3 20
1 2 3

Sample Output 4

Second

Sample Input 5

3 21
1 2 3

Sample Output 5

First

Sample Input 6

1 100000
1

Sample Output 6

Second


open Batteries
module EnumL = Enum.Labels
module ListL = List.Labels
module ArrayL = Array.Labels

let dbg0 x = Printf.eprintf "[debug]%s\n" @@ dump x
let dbg1 x = dbg0 x; x

let id = identity

let (++) n m = List.range n `To m
let (++^) n m = if n < m then List.range n `To (pred m) else []

let scan fmt = Scanf.sscanf (read_line ()) fmt

let scan_lines n fmt f =
  if n = 0 then []
  else List.map (fun _ -> scan fmt f) (1 ++ n)

let scan_list sep cnv =
  let line = read_line () in
  (match sep with
   | None -> List.map String.of_char @@ String.to_list line
   | Some sep -> String.split_on_char sep line)
  |> List.map cnv

let scan_array ?sep cnv = Array.of_list @@ scan_list sep cnv

let scan_matrix n m e ?sep conv =
  let arr = Array.make_matrix n m e in
  ListL.iter (0 ++^ n)
    ~f:(fun i -> arr.(i) <- Array.of_list @@ scan_list sep conv);
  arr

let rec powerset e =
  match Enum.get e with
  | None -> Enum.singleton @@ Enum.empty ()
  | Some v ->
    let f = powerset e in
    let g = Enum.clone f in
    EnumL.map f ~f:(fun x -> let y = Enum.clone x in push y v; y)
    |>  Enum.append g

let permutations l =
  let rec aux l =
    let rec interleave x = function
      | [] -> [[x]]
      | (hd::tl) as lst ->
        (x::lst) ::
        (ListL.map ~f:(List.cons hd)  @@ interleave x tl)
    in
    match l with
    | [] -> [[]]
    | hd::tl -> List.concat @@ List.map (interleave hd) @@ aux tl in
  let l = List.sort (List.compare Int.compare) @@ aux @@ List.of_enum l in
  List.enum % ListL.map ~f:List.enum @@ l

let intersection l =
  EnumL.filter ~f:(fun x -> EnumL.exists ~f:((=) x) @@ Enum.clone l)

let lower_bound n f =
  let rec aux l u =
    if u - l > 1 then
      let m = (l + u) / 2 in
      if f m then aux l m
      else aux m u
    else u in aux (-1) n

let between n x m = n <= x && x < m

let n = scan "%d" id
let arr = scan_array ~sep:' ' Int.of_string

let memo = Array.make_matrix n n None

let rec solve i j t =
  if t then
    if i = j then arr.(i)
    else
      match memo.(i).(j)with
      | Some v ->  v
      | None ->
        let v = max (solve (succ i) j  false + arr.(i))
            (solve i (pred j) false + arr.(j)) in
        memo.(i).(j) <- Some v; v
  else
  if i = j then 0
  else
  if arr.(i) < arr.(j) then
    solve i (pred j) true
  else
    solve (succ i) j true

let () =
  solve 0 (pred n) true
  |> (fun x -> Printf.printf "%d\n" @@ 2*x-Array.sum arr)

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

Score : 100 points

Problem Statement

Taro and Jiro will play the following game against each other.

Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N).
Until a becomes empty, the two players perform the following operation alternately, starting from Taro:

Remove the element at the beginning or the end of a. The player earns x points, where x is the removed element.

Let X and Y be Taro's and Jiro's total score at the end of the game, respectively.
Taro tries to maximize X - Y, while Jiro tries to minimize X - Y.

Assuming that the two players play optimally, find the resulting value of X - Y.

Constraints

All values in input are integers.

1 \leq N \leq 3000

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N

Output

Print the resulting value of X - Y, assuming that the two players play optimally.

Sample Input 1

4
10 80 90 30

Sample Output 1

10

The game proceeds as follows when the two players play optimally (the element being removed is written bold):

Taro: (10, 80, 90, 30) → (10, 80, 90)

Jiro: (10, 80, 90) → (10, 80)

Taro: (10, 80) → (10)

Jiro: (10) → ()

Here, X = 30 + 80 = 110 and Y = 90 + 10 = 100.

Sample Input 2

3
10 100 10

Sample Output 2

-80

The game proceeds, for example, as follows when the two players play optimally:

Taro: (10, 100, 10) → (100, 10)

Jiro: (100, 10) → (10)

Taro: (10) → ()

Here, X = 10 + 10 = 20 and Y = 100.

Sample Input 3

1
10

Sample Output 3

10

Sample Input 4

10
1000000000 1 1000000000 1 1000000000 1 1000000000 1 1000000000 1

Sample Output 4

4999999995

The answer may not fit into a 32-bit integer type.

Sample Input 5

6
4 2 9 7 1 5

Sample Output 5

2

The game proceeds, for example, as follows when the two players play optimally:

Taro: (4, 2, 9, 7, 1, 5) → (4, 2, 9, 7, 1)

Jiro: (4, 2, 9, 7, 1) → (2, 9, 7, 1)

Taro: (2, 9, 7, 1) → (2, 9, 7)

Jiro: (2, 9, 7) → (2, 9)

Taro: (2, 9) → (2)

Jiro: (2) → ()

Here, X = 5 + 1 + 9 = 15 and Y = 4 + 7 + 2 = 13.

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.


(* O(√p) *)
let n, p = Scanf.scanf " %d %d" @@ fun a b -> a, b
let factorize_primes n =
  let rec f acc c d m =
    if d > m || d * d > n then
      List.rev @@ if c > 0 then (d, c) :: acc else if m > 1 then (m, 1) :: acc else acc
    else
      if m mod d = 0 then f acc (c + 1) d @@ m / d
      else f (if c > 0 then (d, c) :: acc else acc) 0 (d + 1) m in
  f [] 0 2 n
let rec pow b n =
  if n <= 0 then 1
  else let p = pow b (n / 2) in p * p * if n mod 2 = 0 then 1 else b
let ps = factorize_primes p
let _ = List.fold_left (fun acc (b, c) -> acc * pow b (c / n)) 1 ps |> Printf.printf "%d\n"

Score : 300 points

Problem Statement

There are N integers a_1, a_2, ..., a_N not less than 1.
The values of a_1, a_2, ..., a_N are not known, but it is known that a_1 \times a_2 \times ... \times a_N = P.

Find the maximum possible greatest common divisor of a_1, a_2, ..., a_N.

Constraints

1 \leq N \leq 10^{12}

1 \leq P \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N P

Output

Print the answer.

Sample Input 1

3 24

Sample Output 1

2

The greatest common divisor would be 2 when, for example, a_1=2, a_2=6 and a_3=2.

Sample Input 2

5 1

Sample Output 2

1

As a_i are positive integers, the only possible case is a_1 = a_2 = a_3 = a_4 = a_5 = 1.

Sample Input 3

1 111

Sample Output 3

111

Sample Input 4

4 972439611840

Sample Output 4

206


let (n, p) = Scanf.scanf "%d %d\n" @@ fun n p -> (n, p)

let prime_fact n =
  let rec f arr i n =
    if n <= 1 then arr
    else if n < i * i then n :: arr
    else if n mod i = 0 then f (i :: arr) i (n / i)
    else f arr (i + 1) n
  in
  match f [] 2 n with
    | [] -> []
    | p :: ps ->
        let (acc, p, n) = List.fold_left (fun (acc, p, n) q ->
          if p = q
          then (acc, p, n + 1)
          else ((p, n) :: acc, q, 1)) ([], p, 1) ps in
        (p, n) :: acc

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

let () =
  prime_fact p
    |> List.map (fun (i, j) -> pow i (j / n))
    |> List.fold_left ( * ) 1
    |> print_int


let () =
  Scanf.scanf "%d" @@ fun n ->
  let idx =
    Array.init n (fun _ -> Scanf.scanf " %d" (fun x -> x mod 2))
    |> Array.fold_left (lor) 0
  in print_endline [|"second"; "first"|].(idx)

Score : 500 points

Problem Statement

There is an apple tree that bears apples of N colors. The N colors of these apples are numbered 1 to N, and there are a_i apples of Color i.

You and Lunlun the dachshund alternately perform the following operation (starting from you):

Choose one or more apples from the tree and eat them. Here, the apples chosen at the same time must all have different colors.

The one who eats the last apple from the tree will be declared winner. If both you and Lunlun play optimally, which will win?

Constraints

1 \leq N \leq 10^5

1 \leq a_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1
a_2
:
a_N

Output

If you will win, print first; if Lunlun will win, print second.

Sample Input 1

2
1
2

Sample Output 1

first

Let Color 1 be red, and Color 2 be blue. In this case, the tree bears one red apple and two blue apples.

You should eat the red apple in your first turn. Lunlun is then forced to eat one of the blue apples, and you can win by eating the other in your next turn.

Note that you are also allowed to eat two apples in your first turn, one red and one blue (not a winning move, though).

Sample Input 2

3
100000
30000
20000

Sample Output 2

second


let () = print_endline @@ Scanf.scanf "%d\n" @@ function
  | 1 -> Scanf.scanf "%d\n" @@ fun a ->
      if a mod 2 = 0 then "second" else "first"
  | n ->
      let as_ = Array.init n @@ fun _ -> Scanf.scanf "%d\n" @@ fun a -> a in
      Array.sort (fun i j -> compare j i) as_;
      if (as_.(0) - as_.(1)) mod 2 = 0 || as_.(1) mod 2 = 0
      then "first"
      else "second"


let id x = x

let init n f =
  let rec g i ns = if i = 0 then 0 :: ns else g (i - 1) (i :: ns) in
  List.map f (g n [])

let split_string ?(pattern="") = Str.split @@ Str.regexp pattern

let ss = split_string @@ Scanf.scanf "%s\n" id

let len = List.length ss

let () =
  ss
    |> List.mapi (fun idx s -> (len - idx - 1, s))
    |> (fun lst -> List.fold_right (fun (idx, s) (sum, b_cnt) -> if s = "B" then (sum + idx - b_cnt, b_cnt + 1) else (sum, b_cnt)) lst (0, 0))
    |> (fun (sum, _) -> sum)
    |> print_int;
  print_newline ()

Score : 300 points

Problem Statement

There are N Reversi pieces arranged in a row. (A Reversi piece is a disc with a black side and a white side.)
The state of each piece is represented by a string S of length N.
If S_i=B, the i-th piece from the left is showing black;
If S_i=W, the i-th piece from the left is showing white.

Consider performing the following operation:

Choose i (1 \leq i < N) such that the i-th piece from the left is showing black and the (i+1)-th piece from the left is showing white, then flip both of those pieces. That is, the i-th piece from the left is now showing white and the (i+1)-th piece from the left is now showing black.

Find the maximum possible number of times this operation can be performed.

Constraints

1 \leq |S| \leq 2\times 10^5

S_i=B or W

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum possible number of times the operation can be performed.

Sample Input 1

BBW

Sample Output 1

2

The operation can be performed twice, as follows:

Flip the second and third pieces from the left.

Flip the first and second pieces from the left.

Sample Input 2

BWBWBW

Sample Output 2

6


let s, k, ans = read_line (), ref 0, ref 0
let _ = String.iter (fun c -> if c = 'B' then incr k else ans := !ans + !k) s; Printf.printf "%d\n" !ans


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

module IMap = Map.Make(Int64)

module UnionFind_i32 = struct
  let init n = Array.make (i32 n) (-1)
  let is_root i a = a.(i) < 0
  let rec root_destructive x a = (* find the root of the tree x belongs to *)
    if a.(x) < 0 then x
    else (
      a.(x) <- root_destructive a.(x) a;
      a.(x))
  let size_destructive i a = let r = root_destructive i a in -r
  let same_destructive x y a = root_destructive x a = root_destructive y a
  let connect_destructive x y a = (* merge the trees x and y belongs to respectively *)
    let p,q = root_destructive x a,root_destructive y a
    in if p = q then () else (
      let p,q = if a.(p) > a.(q) then q,p else p,q
      in
      a.(p) <- a.(p) +$ a.(q);
      a.(q) <- p)
end
let coordinate_compression a =
  (* zip:i64->int, unzip:int->i64 *)
  let ls = a |> Array.to_list |> List.sort_uniq compare in
  let unzip = Array.make (List.length ls) 0L in
  let zip = fst @@ List.fold_left (fun (m,i) v ->
    unzip.(i) <- v; IMap.add v i m,i+$1) (IMap.empty,0) ls
  in zip,unzip
let () =
  let n=gi 0 in let a = iia n in
  let mp = ref IMap.empty in
  iter (fun v ->
    mp := IMap.add v (1L + try IMap.find v !mp with _ -> 0L) !mp
  ) a;
  let zip,unz = coordinate_compression a in
  let uf = UnionFind_i32.init (alen unz) in
  let size = init (i32 @@ alen unz) (fun i ->
    let v = unz.(i) in
    let z = fix (fun f a -> if a>v then a else f (a*2L)) 1L in
    (* printf "%Ld %Ld\n" z v; *)
    if z=v+v then IMap.find v !mp else 1L)
  in
  let size_con x y a = UnionFind_i32.(
    let p,q = root_destructive x a,root_destructive y a
    in if p = q then () else (
      let p,q = if a.(p) > a.(q) then q,p else p,q
      in
      size.(p) <- size.(p) + size.(q);
      size.(q) <- 0L
    )
  ) in
  IMap.iter (fun v c ->
    let z = fix (fun f a -> if a>v then a else f (a*2L)) 1L in
    let cp = z - v in
    (* printf "%Ld + %Ld = %Ld\n" v cp z; *)
    try
      let _ = IMap.find cp !mp in
      let p,q = IMap.find v zip,IMap.find cp zip in
      size_con p q uf;
      UnionFind_i32.connect_destructive p q uf;
      (* print_array (sprintf "%2d") uf;
      print_array (sprintf "%2Ld") size; *)
    with _ -> ()
  ) !mp;
  let r = ref 0L in iter (fun v ->
    if v>=2L then r += v / 2L
  ) size; printf "%Ld" @@ !r

Score : 600 points

Problem Statement

Takahashi has N balls with positive integers written on them. The integer written on the i-th ball is A_i.
He would like to form some number of pairs such that the sum of the integers written on each pair of balls is a power of 2.
Note that a ball cannot belong to multiple pairs.
Find the maximum possible number of pairs that can be formed.

Here, a positive integer is said to be a power of 2 when it can be written as 2^t using some non-negative integer t.

Constraints

1 \leq N \leq 2\times 10^5

1 \leq A_i \leq 10^9

A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible number of pairs such that the sum of the integers written on each pair of balls is a power of 2.

Sample Input 1

3
1 2 3

Sample Output 1

1

We can form one pair whose sum of the written numbers is 4 by pairing the first and third balls.
Note that we cannot pair the second ball with itself.

Sample Input 2

5
3 11 14 5 13

Sample Output 2

2


let n = Scanf.scanf " %d" @@ (+) 0
let a_s = Array.init n @@ fun _ -> Scanf.scanf " %d" @@ (+) 0
let s = Array.fold_left (+) 0 a_s
let m = Array.fold_left max 0 a_s
let _ = Printf.printf "%d\n" @@ s - m / 2

Score : 200 points

Problem Statement

In some other world, today is the day before Christmas Eve.

Mr. Takaha is buying N items at a department store. The regular price of the i-th item (1 \leq i \leq N) is p_i yen (the currency of Japan).

He has a discount coupon, and can buy one item with the highest price for half the regular price. The remaining N-1 items cost their regular prices. What is the total amount he will pay?

Constraints

2 \leq N \leq 10

100 \leq p_i \leq 10000

p_i is an even number.

Input

Input is given from Standard Input in the following format:

N
p_1
p_2
:
p_N

Output

Print the total amount Mr. Takaha will pay.

Sample Input 1

3
4980
7980
6980

Sample Output 1

15950

The 7980-yen item gets the discount and the total is 4980 + 7980 / 2 + 6980 = 15950 yen.

Note that outputs such as 15950.0 will be judged as Wrong Answer.

Sample Input 2

4
4320
4320
4320
4320

Sample Output 2

15120

Only one of the four items gets the discount and the total is 4320 / 2 + 4320 + 4320 + 4320 = 15120 yen.


Scanf.(Array.(
  let n,k=scanf" %d %d"@@fun u v->u,v in
  let h=init n@@fun _->scanf" %d"@@fun v->v in
  sort compare h;
  init(n-k+1)(fun i->h.(i+k-1)-h.(i))
  |>fold_left min max_int
  |>print_int))

Score : 300 points

Problem Statement

In some other world, today is Christmas Eve.

There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters.

He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible.

More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}?

Constraints

2 \leq K < N \leq 10^5

1 \leq h_i \leq 10^9

h_i is an integer.

Input

Input is given from Standard Input in the following format:

N K
h_1
h_2
:
h_N

Output

Print the minimum possible value of h_{max} - h_{min}.

Sample Input 1

5 3
10
15
11
14
12

Sample Output 1

2

If we decorate the first, third and fifth trees, h_{max} = 12, h_{min} = 10 so h_{max} - h_{min} = 2. This is optimal.

Sample Input 2

5 3
5
7
5
7
7

Sample Output 2

0

If we decorate the second, fourth and fifth trees, h_{max} = 7, h_{min} = 7 so h_{max} - h_{min} = 0. This is optimal.

There are not too many trees in these sample inputs, but note that there can be at most one hundred thousand trees (we just can't put a sample with a hundred thousand lines here).


let id x = x

let () = Scanf.scanf "%d %d\n" @@ fun n k ->
    let h = Array.init n (fun _ -> Scanf.scanf "%d\n" id) in
    Array.sort (-) h;
    Array.init (n-k+1) (fun i -> h.(i+k-1) - h.(i))
    |> Array.fold_left min 1000000001
    |> Printf.printf "%d"


let rec size = function
    | 0 -> 1
    | n -> 3 + 2 * size (n - 1)

let rec full = function
    | 0 -> 1
    | n -> 1 + 2 * full (n - 1)

let rec f x = function
    | 0 -> if x > 0 then 1 else 0
    | n -> let m = (size n) / 2 + 1 in
        if x < m
        then f (x - 1) (n - 1)
        else (full (n - 1)) + 1 + f (x - m) (n - 1)

let () = Scanf.scanf "%d %d" @@ fun n x ->
    f x n |> Printf.printf "%d"

Score : 400 points

Problem Statement

In some other world, today is Christmas.

Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing:

A level-0 burger is a patty.

A level-L burger (L \geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom.

For example, a level-1 burger and a level-2 burger look like BPPPB and BBPPPBPBPPPBB (rotated 90 degrees), where B and P stands for a bun and a patty.

The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat?

Constraints

1 \leq N \leq 50

1 \leq X \leq ( the total number of layers in a level-N burger )

N and X are integers.

Input

Input is given from Standard Input in the following format:

N X

Output

Print the number of patties in the bottom-most X layers from the bottom of a level-N burger.

Sample Input 1

2 7

Sample Output 1

4

There are 4 patties in the bottom-most 7 layers of a level-2 burger (BBPPPBPBPPPBB).

Sample Input 2

1 1

Sample Output 2

0

The bottom-most layer of a level-1 burger is a bun.

Sample Input 3

50 4321098765432109

Sample Output 3

2160549382716056

A level-50 burger is rather thick, to the extent that the number of its layers does not fit into a 32-bit integer.


let x = read_int ()
let _ = if x = 3 || x = 5 || x = 7 then print_endline "YES" else print_endline "NO"

Score : 100 points

Problem Statement

Shichi-Go-San (literally "Seven-Five-Three") is a traditional event in a certain country to celebrate the growth of seven-, five- and three-year-old children.

Takahashi is now X years old. Will his growth be celebrated in Shichi-Go-San this time?

Constraints

1 ≤ X ≤ 9

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

If Takahashi's growth will be celebrated, print YES; if it will not, print NO.

Sample Input 1

5

Sample Output 1

YES

The growth of a five-year-old child will be celebrated.

Sample Input 2

6

Sample Output 2

NO

See you next year.


let () = print_endline @@ Scanf.scanf "%d" @@ function
  | 7 | 5 | 3 -> "YES"
  | _ -> "NO"


let s = read_line ();;

let rec f i min_diff =
    let min_diff = min min_diff (abs ((int_of_string (String.sub s i 3)) - 753)) in
    if (String.length s) = (i + 3) then
        min_diff
    else
        f (i+1) min_diff
;;

print_endline (string_of_int (f 0 999999))

Score : 200 points

Problem Statement

There is a string S consisting of digits 1, 2, ..., 9.
Lunlun, the Dachshund, will take out three consecutive digits from S, treat them as a single integer X and bring it to her master. (She cannot rearrange the digits.)

The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753?

Constraints

S is a string of length between 4 and 10 (inclusive).

Each character in S is 1, 2, ..., or 9.

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum possible difference between X and 753.

Sample Input 1

1234567876

Sample Output 1

34

Taking out the seventh to ninth characters results in X = 787, and the difference between this and 753 is 787 - 753 = 34. The difference cannot be made smaller, no matter where X is taken from.

Note that the digits cannot be rearranged. For example, taking out 567 and rearranging it to 765 is not allowed.

We cannot take out three digits that are not consecutive from S, either. For example, taking out the seventh digit 7, the ninth digit 7 and the tenth digit 6 to obtain 776 is not allowed.

Sample Input 2

35753

Sample Output 2

0

If 753 itself can be taken out, the answer is 0.

Sample Input 3

1111111111

Sample Output 3

642

No matter where X is taken from, X = 111, with the difference 753 - 111 = 642.


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

let () =
  let n = i32_get_int 0 in
  let r = [|0;0;0;0;0;0;0;0;0;1;1;1;1;2;2;3;3;3;3;8;8;11;11;11;11;14;14;32;32;35;35;35;35;42;42;42;42;49;49;49;49;49;49;75;75;86;86;86;86;86;86;123;123;131;131;131;131;148;148;153;153;170;170;170;170;170;170;227;227;227;227;227;227;250;250;323;323;324;324;324;324;354;354;354;354;384;384;384;384;384;391;491;491;529;529;529;529;529;529;543;|]
  in
  printf "%d\n" r.(n-$1)
(*
  let sieve n = (* includes 0, O(nloglogn) *)
  let n = i32 n in
  let prime = Array.make (n+$1) true in
  prime.(0) <- false;
  prime.(1) <- false;
  for i=2 to n do
    if prime.(i) then let j = ref @@ i*$2 in
    while !j <= n do prime.(!j) <- false; j := !j +$ i; done
  done; prime

  let divisors_1_to_n n = (* O(nloglogn) *)
    let res = Array.make (n+$1) [] in
    for i=1 to n do
      let j = ref @@ i in
      while !j <= n do res.(!j) <- i::res.(!j); j := !j +$ i; done;
      res.(i) <- List.rev res.(i)
    done; res
    let factors n =
      let m,r = ref n,ref [] in
      rep 2L (n/2L) (fun i ->
        if !m mod i = 0L then (
          let k = ref 0L in
          while !m mod i = 0L do
            k += 1L; m /= i;
          done;
          r := (i,!k) :: !r));
      if !m>1L then (!m,1L)::!r else !r
let () =
  let primes =
    let a = sieve 108L in
    fold_left (fun (l,i) v -> if v then ((i)::l,i+1L) else (l,i+1L)) ([],0L) a
    |> fst |> of_list
  in
  let divs = divisors_1_to_n 108 in
  (* let n = get_i64 0 in *)
  rep 1L 100L (fun n ->
  let nprimes = make 108 0L in
  rep 1L n (fun i ->
    let facts = factors i in
    (* print_list (fun (n,k) -> sprintf "(%Ld,%Ld)" n k) facts; *)
    List.iter (fun (n,k) -> nprimes.(i32 n) <- nprimes.(i32 n) + k) facts;
  );
  (* print_array ist nprimes; *)
  let res = ref 0L in
  (* 3,5,5 *)
  rep 1L n (fun i ->
    rep (i+1L) n (fun j ->
      rep (j+1L) n (fun k ->
        let h = [3L,5L,5L;5L,3L,5L;5L,5L,3L] in
        let i,j,k = nprimes.(i32 i)+1L,nprimes.(i32 j)+1L,nprimes.(i32 k)+1L in
        List.iter (fun (p,q,r) ->
          if p<=i && q<=j && r<=k then (
            res += 1L;
            (* (i-p+1L)*(j-q+1L)*(k-r+1L) *)
            (* let h = [3L,15L;15L,3L] in
            let i,j = nprimes.(i32 i)+1L,nprimes.(i32 j)+1L in
            let f = ref false in
            List.iter (fun (p,q) ->
              if p<=i && q<=j then f:=true
              (* (i-p+1L)*(j-q+1L) *)
            ) h;
            if not !f then res += 1L; *)
          )
        ) h
      )
    )
  );
  (* 5,15 *)
  rep 1L n (fun i ->
    rep (i+1L) n (fun j ->
      let h = [5L,15L;15L,5L] in
      let i,j = nprimes.(i32 i)+1L,nprimes.(i32 j)+1L in
      List.iter (fun (p,q) ->
        if p<=i && q<=j then res += 1L
        (* (i-p+1L)*(j-q+1L) *)
      ) h
    )
  );
  (* 3,25 *)
  rep 1L n (fun i ->
    rep (i+1L) n (fun j ->
      let h = [3L,25L;25L,3L] in
      let i,j = nprimes.(i32 i)+1L,nprimes.(i32 j)+1L in
      List.iter (fun (p,q) ->
        if p<=i && q<=j then res += 1L
        (* (i-p+1L)*(j-q+1L) *)
      ) h
    )
  );
  (* 75 *)
  rep 1L n (fun i ->
    if nprimes.(i32 i)+1L >= 75L then res += 1L
  );
  printf "%Ld;" !res
  )

 *)

Score : 400 points

Problem Statement

You are given an integer N. Among the divisors of N! (= 1 \times 2 \times ... \times N), how many Shichi-Go numbers (literally "Seven-Five numbers") are there?

Here, a Shichi-Go number is a positive integer that has exactly 75 divisors.

Note

When a positive integer A divides a positive integer B, A is said to a divisor of B.
For example, 6 has four divisors: 1, 2, 3 and 6.

Constraints

1 \leq N \leq 100

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of the Shichi-Go numbers that are divisors of N!.

Sample Input 1

9

Sample Output 1

0

There are no Shichi-Go numbers among the divisors of 9! = 1 \times 2 \times ... \times 9 = 362880.

Sample Input 2

10

Sample Output 2

1

There is one Shichi-Go number among the divisors of 10! = 3628800: 32400.

Sample Input 3

100

Sample Output 3

543


Scanf.scanf "%d" (fun n ->
    let a = Array.init n (fun _ -> Scanf.scanf " %d" (fun a -> a)) in
    let s = Array.fold_left (+) 0 a in

    let rec loop i dist df =
        if i = n then df else
            if abs (a.(i) * n - s) < dist then loop (i + 1) (abs (a.(i) * n - s)) i
                                          else loop (i + 1) dist df
    in
    loop 0 max_int 0 |> Printf.printf "%d\n"
)

Score : 200 points

Problem Statement

Niwango-kun is an employee of Dwango Co., Ltd.

One day, he is asked to generate a thumbnail from a video a user submitted.

To generate a thumbnail, he needs to select a frame of the video according to the following procedure:

Get an integer N and N integers a_0, a_1, ..., a_{N-1} as inputs. N denotes the number of the frames of the video, and each a_i denotes the representation of the i-th frame of the video.

Select t-th frame whose representation a_t is nearest to the average of all frame representations.

If there are multiple such frames, select the frame with the smallest index.

Find the index t of the frame he should select to generate a thumbnail.

Constraints

1 \leq N \leq 100

1 \leq a_i \leq 100

All numbers given in input are integers

Input

Input is given from Standard Input in the following format:

N
a_{0} a_{1} ... a_{N-1}

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

1

Since the average of frame representations is 2, Niwango-kun needs to select the index 1, whose representation is 2, that is, the nearest value to the average.

Sample Input 2

4
2 5 2 5

Sample Output 2

0

The average of frame representations is 3.5.

In this case, every frame has the same distance from its representation to the average.

Therefore, Niwango-kun should select index 0, the smallest index among them.


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

let () =
  let n = get_i64 0 in
  let a = input_i64_array n in
  let sum = fold_left (+) 0L a in
  let b = mapi (fun i v -> (labs @@ v*n-sum,i)) a in
  (* sort (fun (u,i) (v,j) -> let p = compare u v in if p<>0 then p else compare j i) b; *)
  sort compare b;
  printf "%d\n" @@ snd b.(0);
  (* print_array (fun (v,i) -> sprintf "<%Ld:%d>" v i) b; *)
  (* printf "%d" @@ snd b.(
    let v = binary_search 0L (alen b-1L) (fun i -> fst b.(i32 i) >= sum)
    in i32 v) *)


let discount_fare x y = x + y / 2

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

Score: 100 points

Problem Statement

There is a train going from Station A to Station B that costs X yen (the currency of Japan).

Also, there is a bus going from Station B to Station C that costs Y yen.

Joisino got a special ticket. With this ticket, she can take the bus for half the fare if she travels from Station A to Station B by train and then travels from Station B to Station C by bus.

How much does it cost to travel from Station A to Station C if she uses this ticket?

Constraints

1 \leq X,Y \leq 100

Y is an even number.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

If it costs x yen to travel from Station A to Station C, print x.

Sample Input 1

81 58

Sample Output 1

110

The train fare is 81 yen.

The train fare is 58 ⁄ 2=29 yen with the 50% discount.

Thus, it costs 110 yen to travel from Station A to Station C.

Sample Input 2

4 54

Sample Output 2

31


Scanf.scanf "%d %d" @@ fun x y ->
  Printf.printf " %d" (x+(y/2))


let n, t, a = Scanf.scanf " %d %d %d" @@ fun a b c -> a, b, c
let hs = Array.init n @@ fun _ -> Scanf.scanf " %d" (+) 0
let f x = abs @@ 1000 * t - 6 * x - 1000 * a
let _ = Array.fold_left (fun (m, i) h -> min m (f h, i), i + 1) ((max_int, -1), 1) hs |> fst |> snd |> Printf.printf "%d\n"

Score: 200 points

Problem Statement

A country decides to build a palace.

In this country, the average temperature of a point at an elevation of x meters is T-x \times 0.006 degrees Celsius.

There are N places proposed for the place. The elevation of Place i is H_i meters.

Among them, Princess Joisino orders you to select the place whose average temperature is the closest to A degrees Celsius, and build the palace there.

Print the index of the place where the palace should be built.

It is guaranteed that the solution is unique.

Constraints

1 \leq N \leq 1000

0 \leq T \leq 50

-60 \leq A \leq T

0 \leq H_i \leq 10^5

All values in input are integers.

The solution is unique.

Input

Input is given from Standard Input in the following format:

N
T A
H_1 H_2 ... H_N

Output

Print the index of the place where the palace should be built.

Sample Input 1

2
12 5
1000 2000

Sample Output 1

1

The average temperature of Place 1 is 12-1000 \times 0.006=6 degrees Celsius.

The average temperature of Place 2 is 12-2000 \times 0.006=0 degrees Celsius.

Thus, the palace should be built at Place 1.

Sample Input 2

3
21 -11
81234 94124 52141

Sample Output 2

3


let n, m = Scanf.scanf " %d %d" @@ fun a b -> a, b
let yps = Array.init m @@ fun i -> Scanf.scanf " %d %d" @@ fun a b -> b, a, i
let rs, ns = Array.(make (n + 1) 1, make m "")
let _ = Array.(sort compare yps; iter (fun (_, p, i) -> ns.(i) <- Printf.sprintf "%06d%06d" p rs.(p); rs.(p) <- rs.(p) + 1) yps; iter print_endline ns)

Score: 300 points

Problem Statement

In Republic of Atcoder, there are N prefectures, and a total of M cities that belong to those prefectures.

City i is established in year Y_i and belongs to Prefecture P_i.

You can assume that there are no multiple cities that are established in the same year.

It is decided to allocate a 12-digit ID number to each city.

If City i is the x-th established city among the cities that belong to Prefecture i, the first six digits of the ID number of City i is P_i, and the last six digits of the ID number is x.

Here, if P_i or x (or both) has less than six digits, zeros are added to the left until it has six digits.

Find the ID numbers for all the cities.

Note that there can be a prefecture with no cities.

Constraints

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq P_i \leq N

1 \leq Y_i \leq 10^9

Y_i are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
P_1 Y_1
:
P_M Y_M

Output

Print the ID numbers for all the cities, in ascending order of indices (City 1, City 2, ...).

Sample Input 1

2 3
1 32
2 63
1 12

Sample Output 1

000001000002
000002000001
000001000001

As City 1 is the second established city among the cities that belong to Prefecture 1, its ID number is 000001000002.

As City 2 is the first established city among the cities that belong to Prefecture 2, its ID number is 000002000001.

As City 3 is the first established city among the cities that belong to Prefecture 1, its ID number is 000001000001.

Sample Input 2

2 3
2 55
2 77
2 99

Sample Output 2

000002000001
000002000002
000002000003


let solve tak aoki k =
    let rec solve_t tak aoki k =
        if k = 0 then tak, aoki
        else solve_a (tak / 2) (aoki + tak / 2) (k - 1)
    and solve_a tak aoki k =
        if k = 0 then tak, aoki
        else solve_t (tak + aoki / 2) (aoki / 2) (k - 1)
    in solve_t tak aoki k

let () =
    let tak, aoki = Scanf.scanf "%d %d %d\n" solve in
    Printf.printf "%d %d\n" tak aoki

Score : 200 points

Problem Statement

In the beginning, Takahashi has A cookies, and Aoki has B cookies.
They will perform the following operation alternately, starting from Takahashi:

If the number of cookies in his hand is odd, eat one of those cookies; if the number is even, do nothing. Then, give one-half of the cookies in his hand to the other person.

Find the numbers of cookies Takahashi and Aoki respectively have after performing K operations in total.

Constraints

1 \leq A,B \leq 10^9

1 \leq K \leq 100

A,B and K are integers.

Input

Input is given from Standard Input in the following format:

A B K

Output

Print the number of cookies Takahashi has, and the number of cookies Aoki has, in this order, after performing K operations in total.

Sample Input 1

5 4 2

Sample Output 1

5 3

The process will go as follows:

In the beginning, Takahashi and Aoki have 5 and 4 cookies, respectively.

Takahashi eats one cookie and gives two cookies to Aoki. They now have 2 and 6 cookies, respectively.

Aoki gives three cookies to Takahashi. They now have 5 and 3 cookies, respectively.

Sample Input 2

3 3 3

Sample Output 2

1 3

Sample Input 3

314159265 358979323 84

Sample Output 3

448759046 224379523


open Printf
open Scanf

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

let n = scanf "%d" id
let vs = Array.init n (fun _ -> scanf " %d" id)

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

let odd1 vs =
  let mid = n / 2 in
  let up = ref (n - 1) in
  let lo = ref 0 in
  let r1 = ref 0 in
  let p = ref vs.(mid) in
  for i = 0 to n / 2 - 1 do
    r1 := !r1 + abs (!p - vs.(!up));
    p := vs.(!up);
    decr up;
    r1 := !r1 + abs (!p - vs.(!lo));
    p := vs.(!lo);
    incr lo
  done;
  !r1

let odd2 vs =
  let mid = n / 2 in
  let up = ref (n - 1) in
  let lo = ref 0 in
  let r1 = ref 0 in
  let p = ref vs.(mid) in
  for i = 0 to n / 2 - 1 do
    r1 := !r1 + abs (!p - vs.(!lo));
    p := vs.(!lo);
    incr lo;
    r1 := !r1 + abs (!p - vs.(!up));
    p := vs.(!up);
    decr up
  done;
  !r1

let even1 vs =
  let mid = n / 2 in
  let up = ref (n - 1) in
  let lo = ref 0 in
  let r1 = ref 0 in
  let p = ref vs.(mid) in
  for i = 0 to n / 2 - 2 do
    r1 := !r1 + abs (!p - vs.(!lo));
    p := vs.(!lo);
    incr lo;
    r1 := !r1 + abs (!p - vs.(!up));
    p := vs.(!up);
    decr up
  done;
  r1 := !r1 + abs (!p - vs.(mid - 1));
  !r1

let even2 vs =
  let mid = n / 2 - 1 in
  let up = ref (n - 1) in
  let lo = ref 0 in
  let r1 = ref 0 in
  let p = ref vs.(mid) in
  for i = 0 to n / 2 - 2 do
    r1 := !r1 + abs (!p - vs.(!up));
    p := vs.(!up);
    decr up;
    r1 := !r1 + abs (!p - vs.(!lo));
    p := vs.(!lo);
    incr lo;
  done;
  r1 := !r1 + abs (!p - vs.(mid + 1));
  !r1

let () =
  if n mod 2 = 0 then begin
    let e1 = even1 vs in
    let e2 = even2 vs in
    printf "%d\n" (max e1 e2)
  end else begin
    let o1 = odd1 vs in
    let o2 = odd2 vs in
    printf "%d\n" (max o1 o2)
  end

Score : 400 points

Problem Statement

You are given N integers; the i-th of them is A_i.
Find the maximum possible sum of the absolute differences between the adjacent elements after arranging these integers in a row in any order you like.

Constraints

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the maximum possible sum of the absolute differences between the adjacent elements after arranging the given integers in a row in any order you like.

Sample Input 1

5
6
8
1
2
3

Sample Output 1

21

When the integers are arranged as 3,8,1,6,2, the sum of the absolute differences between the adjacent elements is |3 - 8| + |8 - 1| + |1 - 6| + |6 - 2| = 21. This is the maximum possible sum.

Sample Input 2

6
3
1
4
1
5
9

Sample Output 2

25

Sample Input 3

3
5
5
1

Sample Output 3

8


let rec power ( * ) e m n =
  if n <= 0 then e
  else power ( * ) (if n land 1 = 0 then e else m * e) (m * m) (n lsr 1)

let m = 1000000007
let ( +^ ) x y = (x + y) mod m
let ( -^ ) x y = x +^ (m - y)
let ( *^ ) x y = (x * y) mod m

let inv x = power ( *^ ) 1 x (m - 2)
let perm n k =
  Array.fold_left ( *^ ) 1 @@
  Array.init k @@ ( + ) @@ n - k + 1

let () = Scanf.scanf "%d\n" @@ fun n ->
  let as_ = Array.init n @@ fun i -> Scanf.scanf "%d " @@ fun a -> a in
  let ps = Array.make n 0 in
  for i = 0 to n - 1 do
    ps.(0) <- inv (1 + i) +^ ps.(0)
  done;
  for i = 1 to n - 1 do
    ps.(i) <- ps.(i - 1) +^ inv (1 + i) -^ inv (1 + n - i)
  done;
  Printf.printf "%d\n" @@
    ( *^ ) (perm n n) @@
      Array.fold_left ( +^ ) 0 @@
        Array.init n @@ fun i -> as_.(i) *^ ps.(i)

Score : 600 points

Problem Statement

There are N blocks arranged in a row, numbered 1 to N from left to right.
Each block has a weight, and the weight of Block i is A_i.
Snuke will perform the following operation on these blocks N times:

Choose one block that is still not removed, and remove it.
The cost of this operation is the sum of the weights of the blocks that are connected to the block being removed (including itself).
Here, two blocks x and y ( x \leq y ) are connected when, for all z ( x \leq z \leq y ), Block z is still not removed.

There are N! possible orders in which Snuke removes the blocks.
For all of those N! orders, find the total cost of the N operations, and calculate the sum of those N! total costs.
As the answer can be extremely large, compute the sum modulo 10^9+7.

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For all of the N! orders, find the total cost of the N operations, and print the sum of those N! total costs, modulo 10^9+7.

Sample Input 1

2
1 2

Sample Output 1

9

First, we will consider the order "Block 1 -> Block 2".
In the first operation, the cost of the operation is 1+2=3, as Block 1 and 2 are connected.
In the second operation, the cost of the operation is 2, as only Block 2 remains.
Thus, the total cost of the two operations for this order is 3+2=5.

Then, we will consider the order "Block 2 -> Block 1".
In the first operation, the cost of the operation is 1+2=3, as Block 1 and 2 are connected.
In the second operation, the cost of the operation is 1, as only Block 1 remains.
Thus, the total cost of the two operations for this order is 3+1=4.

Therefore, the answer is 5+4=9.

Sample Input 2

4
1 1 1 1

Sample Output 2

212

Sample Input 3

10
1 2 4 8 16 32 64 128 256 512

Sample Output 3

880971923


let n = read_int ()
let () =
  if n = 1 then
    print_endline "Hello World"
  else
    Printf.printf "%d\n" @@ read_int () + read_int ()

Score: 100 points

Problem Statement

In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the currency of Japan) has started a business in programming education.

One day, there was an exam where a one-year-old child must write a program that prints Hello World, and a two-year-old child must write a program that receives integers A, B and prints A+B.

Takahashi, who is taking this exam, suddenly forgets his age.

He decides to write a program that first receives his age N (1 or 2) as input, then prints Hello World if N=1, and additionally receives integers A, B and prints A+B if N=2.

Write this program for him.

Constraints

N is 1 or 2.

A is an integer between 1 and 9 (inclusive).

B is an integer between 1 and 9 (inclusive).

Input

Input is given from Standard Input in one of the following formats:

1

2
A
B

Output

If N=1, print Hello World; if N=2, print A+B.

Sample Input 1

1

Sample Output 1

Hello World

As N=1, Takahashi is one year old. Thus, we should print Hello World.

Sample Input 2

2
3
5

Sample Output 2

8

As N=2, Takahashi is two years old. Thus, we should print A+B, which is 8 since A=3 and B=5.


let () = Scanf.scanf "%d\n" @@ function
  | 1 -> print_endline "Hello World"
  | 2 -> Printf.printf "%d\n" @@ Scanf.scanf "%d\n%d" ( + )


let () = Scanf.scanf "%d %d" @@ fun n t ->
  let cts = Array.to_list @@ Array.init n @@ fun _ ->
    Scanf.scanf "\n%d %d" @@ fun c t -> c, t in
  match
    List.concat @@ List.map (fun (ci, ti) ->
      if t < ti then [] else [ci]) cts
  with
  | [] -> print_endline "TLE"
  | c :: cs -> Printf.printf "%d\n" @@ List.fold_left min c cs

Score : 200 points

Problem Statement

When Mr. X is away from home, he has decided to use his smartwatch to search the best route to go back home, to participate in ABC.

You, the smartwatch, has found N routes to his home.

If Mr. X uses the i-th of these routes, he will get home in time t_i at cost c_i.

Find the smallest cost of a route that takes not longer than time T.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq T \leq 1000

1 \leq c_i \leq 1000

1 \leq t_i \leq 1000

The pairs (c_i, t_i) are distinct.

Input

Input is given from Standard Input in the following format:

N T
c_1 t_1
c_2 t_2
:
c_N t_N

Output

Print the smallest cost of a route that takes not longer than time T.

If there is no route that takes not longer than time T, print TLE instead.

Sample Input 1

3 70
7 60
1 80
4 50

Sample Output 1

4

The first route gets him home at cost 7.

The second route takes longer than time T = 70.

The third route gets him home at cost 4.

Thus, the cost 4 of the third route is the minimum.

Sample Input 2

4 3
1 1000
2 4
3 1000
4 500

Sample Output 2

TLE

There is no route that takes not longer than time T = 3.

Sample Input 3

5 9
25 8
5 9
4 10
1000 1000
6 1

Sample Output 3

5


open Printf
open Scanf

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

let n, t = scanf "%d %d" pair

let vs = Array.init n (fun _ -> scanf " %d %d" pair)

let f c' t' = if t' <= t then c' else 2000

let () =
  Array.sort (fun (ac, at) (bc, bt) -> f ac at - f bc bt) vs;
  let (a, b) = vs.(0) in
  if b <= t then printf "%d\n" a else printf "LTE\n"


let ans = ref 0
let f g n = let rec f i = if i * i <= n then (if n mod i = 0 then (g i; if i <> n / i then g (n / i)); f (i + 1)) in f 1
let _ = Scanf.scanf "%d %d" @@ fun n m -> f (fun d -> if m / d >= n then ans := max !ans d) m; Printf.printf "%d\n" !ans

Score : 400 points

Problem Statement

You are given integers N and M.

Consider a sequence a of length N consisting of positive integers such that a_1 + a_2 + ... + a_N = M. Find the maximum possible value of the greatest common divisor of a_1, a_2, ..., a_N.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

N \leq M \leq 10^9

Input

Input is given from Standard Input in the following format:

N M

Output

Print the maximum possible value of the greatest common divisor of a sequence a_1, a_2, ..., a_N that satisfies the condition.

Sample Input 1

3 14

Sample Output 1

2

Consider the sequence (a_1, a_2, a_3) = (2, 4, 8). Their greatest common divisor is 2, and this is the maximum value.

Sample Input 2

10 123

Sample Output 2

3

Sample Input 3

100000 1000000000

Sample Output 3

10000


open Printf open Scanf
let divisors v =
	let rec f0 i a =
		if i*i > v then a
		else if i*i = v && v mod i = 0 then i::a
		else if v mod i = 0 then f0 (i+1) (i::(v/i)::a)
		else f0 (i+1) a in f0 1 []
let () = scanf " %d %d" @@ fun n m ->
	let l = m/n in
	divisors m |> List.sort compare |> List.rev
	|> List.iter @@ fun v ->
		if v <= l then (printf "%d\n" v; exit 0)


let rec floor_sqrt acc acc_x_2_x_r sq_acc_minus_z r sq_r =
  if r = 0 then acc
  else
    let sq_acc_minus_z' = sq_acc_minus_z + acc_x_2_x_r + sq_r in
    ( if sq_acc_minus_z' <= 0 then
        floor_sqrt (acc + r) ((acc_x_2_x_r lsr 1) + sq_r) sq_acc_minus_z'
      else
        floor_sqrt acc (acc_x_2_x_r lsr 1) sq_acc_minus_z) (r lsr 1) (sq_r lsr 2)
let floor_sqrt z = floor_sqrt 0 0 (~-z) (1 lsl 30) (1 lsl 60)

let () = Scanf.scanf "%d %d" @@ fun n m ->
  Printf.printf "%d\n" @@
  if n = 1 then m
  else Array.fold_left (List.fold_left max) 1 @@
    Array.init (floor_sqrt m) @@ fun i ->
      if 0 < m mod (i + 1)
      then []
      else (if m / (i + 1) < n then [] else [i + 1]) @ (if (i + 1) < n then [] else [m / (i + 1)])


open Batteries
let explode s = List.init (String.length s) (String.get s)
let string_of_chars chars =
  let buf = Buffer.create 16 in
  List.iter (Buffer.add_char buf) chars;
  Buffer.contents buf
let n = read_line () |> explode
let rec ans lst =
  match lst with
  | [] -> []
  | first :: rest -> if first = '9' then '1' :: ans rest else '9' :: ans rest

let _ = print_endline (string_of_chars (ans n))

Score : 100 points

Problem Statement

Cat Snuke is learning to write characters.
Today, he practiced writing digits 1 and 9, but he did it the other way around.

You are given a three-digit integer n written by Snuke.
Print the integer obtained by replacing each digit 1 with 9 and each digit 9 with 1 in n.

Constraints

111 \leq n \leq 999

n is an integer consisting of digits 1 and 9.

Input

Input is given from Standard Input in the following format:

n

Output

Print the integer obtained by replacing each occurrence of 1 with 9 and each occurrence of 9 with 1 in n.

Sample Input 1

119

Sample Output 1

991

Replace the 9 in the ones place with 1, the 1 in the tens place with 9 and the 1 in the hundreds place with 9. The answer is 991.

Sample Input 2

999

Sample Output 2

111


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 digits v =
	let rec f0 p a = if p=0L then a else f0 (p/10L) ((p mod 10L)::a)
	in f0 v []
let () =
	let n = get_i64 0 in
	let a = digits n |> Array.of_list in
	let m = a.(0) * 111L in (
		if n <= m then m else (a.(0)+1L)*111L
	) |> printf "%Ld\n"

Score : 200 points

Problem Statement

Kurohashi has never participated in AtCoder Beginner Contest (ABC).

The next ABC to be held is ABC N (the N-th ABC ever held).
Kurohashi wants to make his debut in some ABC x such that all the digits of x in base ten are the same.

What is the earliest ABC where Kurohashi can make his debut?

Constraints

100 \leq N \leq 999

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If the earliest ABC where Kurohashi can make his debut is ABC n, print n.

Sample Input 1

111

Sample Output 1

111

The next ABC to be held is ABC 111, where Kurohashi can make his debut.

Sample Input 2

112

Sample Output 2

222

The next ABC to be held is ABC 112, which means Kurohashi can no longer participate in ABC 111.
Among the ABCs where Kurohashi can make his debut, the earliest one is ABC 222.

Sample Input 3

750

Sample Output 3

777


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 llen l = i64 @@ List.length l
(* module IMap = Map.Make (struct
	type t = int64
	let compare i j =
end) *)
module IMap = Map.Make (Int64)
let find k m = try IMap.find k m with | Not_found -> 0L

let f = List.fold_left (fun (k,u) (j,v) ->
		if u < v then (j,v) else (k,u)
) (-1L,Int64.min_int)

let d l = string_of_list (fun (k,v) -> sprintf "(%Ld,%Ld)" k v) l |> print_endline

let () =
	let n = get_i64 0 in let a = input_i64_array n in
	let a0 = (Array.init (i32 n) (fun i -> a.(i))) in
	Array.sort compare a0;
	if a0.(0) = a0.(i32 n -$ 1) then (
		printf "%Ld\n" (n/2L)
	) else (
		let (o,e,_) = Array.fold_left (fun (o,e,i) v ->
			(* printf "%Ld %Ld\n" i v; *)
			if i mod 2L = 0L then (o,IMap.add v (find v e + 1L) e,i+1L)
			else (IMap.add v (find v o + 1L) o,e,i+1L)
		) (IMap.empty, IMap.empty, 0L) a
		in
		let (o2,e2) = IMap.bindings o,IMap.bindings e in
		(* d o2;
		d e2; *)
		(* List.iter (fun (u,v) -> printf "(%Ld,%Ld);" u v) o2;
		print_endline "";
		List.iter (fun (u,v) -> printf "(%Ld,%Ld);" u v) e2;
		print_endline "";
		let ((k,p),(j,q)) = f o2, f e2
		in
		if k = j then (
			printf "%Ld\n" (n - (max p q))

		) else (
			printf "%Ld %Ld %Ld %Ld\n" (n / 2L) p ((n+1L)/2L) q;
			printf "%Ld\n" (n - p - q)
		) *)
		let (o2,e2) = List.sort (fun (k,u) (j,v) -> -compare u v) o2,List.sort (fun (k,u) (j,v) -> compare u v) e2
		in
		let (o2,e2) = Array.of_list o2,Array.of_list e2 in
		let ((k,p),(j,q)) = o2.(0),e2.(0) in
		(* printf "%Ld %Ld %Ld %Ld\n" k p j q; *)
		if k = j then (
			if snd o2.(1) > snd e2.(1) then (
				printf "%Ld\n" (n - (snd o2.(1)) - q)
			) else (
				printf "%Ld\n" (n - (snd e2.(1)) - p)
			)
		) else (
			printf "%Ld\n" (n - p - q)
		)
	)

Score : 300 points

Problem Statement

A sequence a_1,a_2,... ,a_n is said to be /\/\/\/ when the following conditions are satisfied:

For each i = 1,2,..., n-2, a_i = a_{i+2}.

Exactly two different numbers appear in the sequence.

You are given a sequence v_1,v_2,...,v_n whose length is even.
We would like to make this sequence /\/\/\/ by replacing some of its elements.
Find the minimum number of elements that needs to be replaced.

Constraints

2 \leq n \leq 10^5

n is even.

1 \leq v_i \leq 10^5

v_i is an integer.

Input

Input is given from Standard Input in the following format:

n
v_1 v_2 ... v_n

Output

Print the minimum number of elements that needs to be replaced.

Sample Input 1

4
3 1 3 2

Sample Output 1

1

The sequence 3,1,3,2 is not /\/\/\/, but we can make it /\/\/\/ by replacing one of its elements: for example, replace the fourth element to make it 3,1,3,1.

Sample Input 2

6
105 119 105 119 105 119

Sample Output 2

0

The sequence 105,119,105,119,105,119 is /\/\/\/.

Sample Input 3

4
1 1 1 1

Sample Output 3

2

The elements of the sequence 1,1,1,1 are all the same, so it is not /\/\/\/.


Scanf.(Array.(let s,q,a,c,f,d=(fun f->Scanf.scanf" %d"f),make 100005(0,0),(fun a i->a.(i)<-fst a.(i)+1,i),(fun u v->compare v u),fst,snd in s@@fun n->let e,o=init n(fun i->s@@fun v->i,v)|>fold_left(fun(e,o)(i,v)->(if i mod 2=0 then a e v else a o v);e,o)(q,q)in sort c e;sort c o;print_int@@n-(if d e.(0)=d o.(0)then max(f e.(0)+f o.(1))(f e.(1)+f o.(0))else(f e.(0)+f o.(0)));0,0))


open Printf
open Scanf

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

let n = scanf "%d" id
let vs = Array.init n @@ fun _ -> scanf " %d" id

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

let odd = ref IMap.empty
let even = ref IMap.empty

let incrmap mapr key =
  if IMap.mem key !mapr then
    mapr := IMap.add key (IMap.find key !mapr + 1) !mapr
  else
    mapr := IMap.add key 1 !mapr

let maxi mapr =
  let lis = IMap.bindings !mapr in
  let lis = List.sort (fun (k1,v1) (k1,v2) -> v2 - v1) lis in
  match lis with
  | [] -> failwith ""
  | (v, n) :: rest -> (v, n, rest)

let () =
  for i = 0 to n - 1 do
    if i mod 2 == 0 then begin
      incrmap odd vs.(i)
    end else begin
      incrmap even vs.(i)
    end
  done;
  let (k1, v1, rest1) = maxi odd in
  let (k2, v2, rest2) = maxi even in
  let run k1 k2 v1 v2 rest1 rest2 =
    if k1 <> k2 then begin
      let n1 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest1 in
      let n2 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest2 in
      n1 + n2
    end else if v1 > v2 then begin
      let n1 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest1 in
      match rest2 with
      | [] -> n1 + n / 2
      | (k, v) :: _ -> n1 + n / 2 - v
    end else if v1 < v2 then begin
      let n2 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest2 in
      match rest1 with
      | [] -> n2 + n / 2
      | (k, v) :: _ -> n2 + n / 2 - v
    end else begin
      (* k1 = k2, v1 = v2 *)
      let rec loop rest1 rest2 =
        match rest1, rest2 with
        | [], [] -> 0
        | [], (k', v') :: rest' -> v'
        | (k', v') :: rest', [] -> v'
        | (k1', v1') :: rest1', (k2', v2') :: rest2' ->
          if v1' < v2' then
            let n1 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest1' in
            let n2 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest2' in
            n1 + n2 - v1'
          else if v1' > v2' then
            let n1 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest1' in
            let n2 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest2' in
            n1 + n2 - v2'
          else n / 0 (* loop rest1' rest2' *)
      in
      v1 + loop rest1 rest2
    end
  in
  let result = run k1 k2 v1 v2 rest1 rest2 in
  printf "%d\n" result


open Printf
open Scanf

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

let n = scanf "%d" id
let vs = Array.init n @@ fun _ -> scanf " %d" id

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

let odd = ref IMap.empty
let even = ref IMap.empty

let incrmap mapr key =
  if IMap.mem key !mapr then
    mapr := IMap.add key (IMap.find key !mapr + 1) !mapr
  else
    mapr := IMap.add key 1 !mapr

let maxi mapr =
  let lis = IMap.bindings !mapr in
  let lis = List.sort (fun (k1,v1) (k1,v2) -> v2 - v1) lis in
  match lis with
  | [] -> failwith ""
  | (v, n) :: rest -> (v, n, rest)

let () =
  for i = 0 to n - 1 do
    if i mod 2 == 0 then begin
      incrmap odd vs.(i)
    end else begin
      incrmap even vs.(i)
    end
  done;
  let (k1, v1, rest1) = maxi odd in
  let (k2, v2, rest2) = maxi even in
  let run k1 k2 v1 v2 rest1 rest2 =
    if k1 <> k2 then begin
      let n1 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest1 in
      let n2 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest2 in
      n1 + n2
    end else if v1 > v2 then begin
      let n1 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest1 in
      match rest2 with
      | [] -> n1 + n / 2
      | (k, v) :: _ -> n1 + n / 2 - v
    end else if v1 < v2 then begin
      let n2 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest2 in
      match rest1 with
      | [] -> n2 + n / 2
      | (k, v) :: _ -> n2 + n / 2 - v
    end else begin
      (* k1 = k2, v1 = v2 *)
      let rec loop rest1 rest2 =
        match rest1, rest2 with
        | [], [] -> 0
        | [], (k', v') :: rest' -> v'
        | (k', v') :: rest', [] -> v'
        | (k1', v1') :: rest1', (k2', v2') :: rest2' ->
          if v1' < v2' then
            let n1 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest1' in
            let n2 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest2' in
            n1 + n2 + v2'
          else if v1' > v2' then
            let n1 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest1' in
            let n2 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest2' in
            n1 + n2 + v1'
          else n / 0 (* loop rest1' rest2' *)
      in
      v1 + loop rest1 rest2
    end
  in
  let result = run k1 k2 v1 v2 rest1 rest2 in
  printf "%d\n" result


open Printf
open Scanf

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

let n = scanf "%d" id
let vs = Array.init n @@ fun _ -> scanf " %d" id

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

let odd = ref IMap.empty
let even = ref IMap.empty

let incrmap mapr key =
  if IMap.mem key !mapr then
    mapr := IMap.add key (IMap.find key !mapr + 1) !mapr
  else
    mapr := IMap.add key 1 !mapr

let maxi mapr =
  let lis = IMap.bindings !mapr in
  let lis = List.sort (fun (k1,v1) (k1,v2) -> v2 - v1) lis in
  match lis with
  | [] -> failwith ""
  | (v, n) :: rest -> (v, n, rest)

let () =
  for i = 0 to n - 1 do
    if i mod 2 == 0 then begin
      incrmap odd vs.(i)
    end else begin
      incrmap even vs.(i)
    end
  done;
  let (k1, v1, rest1) = maxi odd in
  let (k2, v2, rest2) = maxi even in
  let rec loop k1 k2 v1 v2 rest1 rest2 =
    if k1 <> k2 then begin
      let n1 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest1 in
      let n2 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest2 in
      n1 + n2
    end else begin
      if v1 > v2 then begin
        let n1 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest1 in
        match rest2 with
        | [] -> n1 + n / 2
        | (k, v) :: _ -> n1 + n / 2 - v
      end else if v1 < v2 then begin
        let n2 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest2 in
        match rest1 with
        | [] -> n2 + n / 2
        | (k, v) :: _ -> n2 + n / 2 - v
      end else begin
        match rest1, rest2 with
        | (k1', v1') :: rest1', (k2', v2') :: rest2' ->
          v1 + v2 + loop k1' k2' v1' v2' rest1' rest2'
        | (k1', v1') :: rest1', [] ->
          v1' + List.fold_left (fun acc (v, n) -> acc + n) 0 rest1'
        | [], (k2', v2') :: rest2' ->
          v2' + List.fold_left (fun acc (v, n) -> acc + n) 0 rest2'
        | [], [] ->
          0
      end
    end
  in
  let result = loop k1 k2 v1 v2 rest1 rest2 in
  printf "%d\n" result


let () = Scanf.scanf "%d\n" @@ fun n ->
  let xys = Array.to_list @@ Array.init n @@ fun _ -> Scanf.scanf "%d %d\n" @@ fun x y -> x, y in
  try
    let odd =
      match
        List.for_all (fun (x, y) -> (x land 1) = (y land 1)) xys,
        List.for_all (fun (x, y) -> (x land 1) <> (y land 1)) xys
      with
      | false, true -> true
      | true, false -> false
      | _, _ -> raise Not_found in
    print_string (if odd then "39\n" else "40\n1 ");
    for i = 38 downto 0 do
      Printf.printf "%d " @@ 1 lsl i
    done;
    print_newline ();
    let solve =
      Array.fold_right (fun d (x, y) ->
        match 0 <= x + y, 0 <= x - y with
        | true, true -> print_char 'R'; (x - d, y)
        | true, false -> print_char 'U'; (x, y - d)
        | false, true -> print_char 'D'; (x, y + d)
        | false, false -> print_char 'L'; (x + d, y)) @@
      Array.init 39 @@ ( lsl ) 1 in
    List.iter (fun (x, y) ->
      ignore (solve
        ( if odd
          then (x, y)
          else (print_char 'L'; (x + 1, y)) ));
      print_newline ()) xys
  with Not_found -> print_endline "-1"

Score : 600 points

Problem Statement

Snuke is introducing a robot arm with the following properties to his factory:

The robot arm consists of m sections and m+1 joints. The sections are numbered 1, 2, ..., m, and the joints are numbered 0, 1, ..., m. Section i connects Joint i-1 and Joint i. The length of Section i is d_i.

For each section, its mode can be specified individually. There are four modes: L, R, D and U. The mode of a section decides the direction of that section. If we consider the factory as a coordinate plane, the position of Joint i will be determined as follows (we denote its coordinates as (x_i, y_i)):

(x_0, y_0) = (0, 0).

If the mode of Section i is L, (x_{i}, y_{i}) = (x_{i-1} - d_{i}, y_{i-1}).

If the mode of Section i is R, (x_{i}, y_{i}) = (x_{i-1} + d_{i}, y_{i-1}).

If the mode of Section i is D, (x_{i}, y_{i}) = (x_{i-1}, y_{i-1} - d_{i}).

If the mode of Section i is U, (x_{i}, y_{i}) = (x_{i-1}, y_{i-1} + d_{i}).

Snuke would like to introduce a robot arm so that the position of Joint m can be matched with all of the N points (X_1, Y_1), (X_2, Y_2), ..., (X_N, Y_N) by properly specifying the modes of the sections.
Is this possible?
If so, find such a robot arm and how to bring Joint m to each point (X_j, Y_j).

Constraints

All values in input are integers.

1 \leq N \leq 1000

-10^9 \leq X_i \leq 10^9

-10^9 \leq Y_i \leq 10^9

Partial Score

In the test cases worth 300 points, -10 \leq X_i \leq 10 and -10 \leq Y_i \leq 10 hold.

Input

Input is given from Standard Input in the following format:

N
X_1 Y_1
X_2 Y_2
:
X_N Y_N

Output

If the condition can be satisfied, follow the following format. If the condition cannot be satisfied, print -1.

m
d_1 d_2 ... d_m
w_1
w_2
:
w_N

m and d_i are the configurations of the robot arm. Refer to the problem statement for what each of them means.
Here, 1 \leq m \leq 40 and 1 \leq d_i \leq 10^{12} must hold. Also, m and d_i must all be integers.

w_j is a string of length m that represents the way to bring Joint m of the robot arm to point (X_j, Y_j).
The i-th character of w_j should be one of the letters L, R, D and U, representing the mode of Section i.

Sample Input 1

3
-1 0
0 3
2 -1

Sample Output 1

2
1 2
RL
UU
DR

In the given way to bring Joint m of the robot arm to each (X_j, Y_j), the positions of the joints will be as follows:

To (X_1, Y_1) = (-1, 0): First, the position of Joint 0 is (x_0, y_0) = (0, 0). As the mode of Section 1 is R, the position of Joint 1 is (x_1, y_1) = (1, 0). Then, as the mode of Section 2 is L, the position of Joint 2 is (x_2, y_2) = (-1, 0).

To (X_2, Y_2) = (0, 3): (x_0, y_0) = (0, 0), (x_1, y_1) = (0, 1), (x_2, y_2) = (0, 3).

To (X_3, Y_3) = (2, -1): (x_0, y_0) = (0, 0), (x_1, y_1) = (0, -1), (x_2, y_2) = (2, -1).

Sample Input 2

5
0 0
1 0
2 0
3 0
4 0

Sample Output 2

-1

Sample Input 3

2
1 1
1 1

Sample Output 3

2
1 1
RU
UR

There may be duplicated points among (X_j, Y_j).

Sample Input 4

3
-7 -3
7 3
-3 -7

Sample Output 4

5
3 1 4 1 5
LRDUL
RDULR
DULRD


open Batteries

let calc x li =
  let rec aux c = function
    | [] -> c
    | a :: al when a + c <= x -> aux (1 + c) al
    | a :: al -> c
  in
  aux 0 li

let () =
  Scanf.scanf " %d %d " (fun n x ->
      let ans =
        List.init n (fun _ -> Scanf.scanf " %d" (fun x -> x))
        |> List.sort (fun x y -> x - y)
        |> calc x
      in
      (if ans = n then n - 1 else ans) |> Printf.printf "%d\n" )

Score : 200 points

Problem Statement

There are N children, numbered 1, 2, ..., N.

Snuke has decided to distribute x sweets among them.
He needs to give out all the x sweets, but some of the children may get zero sweets.

For each i (1 \leq i \leq N), Child i will be happy if he/she gets exactly a_i sweets.
Snuke is trying to maximize the number of happy children by optimally distributing the sweets.
Find the maximum possible number of happy children.

Constraints

All values in input are integers.

2 \leq N \leq 100

1 \leq x \leq 10^9

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N x
a_1 a_2 ... a_N

Output

Print the maximum possible number of happy children.

Sample Input 1

3 70
20 30 10

Sample Output 1

2

One optimal way to distribute sweets is (20, 30, 20).

Sample Input 2

3 10
20 30 10

Sample Output 2

1

The optimal way to distribute sweets is (0, 0, 10).

Sample Input 3

4 1111
1 10 100 1000

Sample Output 3

4

The optimal way to distribute sweets is (1, 10, 100, 1000).

Sample Input 4

2 10
20 20

Sample Output 4

0

No children will be happy, no matter how the sweets are distributed.


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 (@) a (i,j) = a.(~|i).(~|j) let (<@) a (i,j,v) = a.(~|i).(~|j) <- v let (<+) a (i,j,v) = a.(~|i).(~|j) <- v + a.(~|i).(~|j)

let () =
	let h,w = get_2_i64 0 in
	let a = Array.init (~|h) (fun _ -> input_i64_array w)
	in
	let ls = ref [] in
	rep 0L (h-2L) (fun i ->
		rep 0L (w-2L) (fun j ->
			if (a@(i,j)) mod 2L = 1L then (
				a<+(i,j,-1L);
				a<+(i,j+1L,1L);
				ls := (i,j,i,j+1L)::!ls;
			); `Ok
		);
		let j = w-1L in
		if (a@(i,j)) mod 2L = 1L then (
			a<+(i,j,-1L);
			a<+(i+1L,j,1L);
			ls := (i,j,i+1L,j)::!ls;
		);
		`Ok
	);
	let i = h-1L in
	rep 0L (w-2L) (fun j ->
		if (a@(i,j)) mod 2L = 1L then (
			a<+(i,j,-1L);
			a<+(i,j+1L,1L);
			ls := (i,j,i,j+1L)::!ls;
		); `Ok
	);
	printf "%d\n" @@ List.length !ls;
	List.iter (fun (p,q,r,s) -> let p,q,r,s = p+1L,q+1L,r+1L,s+1L
	in printf "%Ld %Ld %Ld %Ld\n" p q r s) @@ List.rev !ls;

Score : 400 points

Problem Statement

There is a grid of square cells with H horizontal rows and W vertical columns. The cell at the i-th row and the j-th column will be denoted as Cell (i, j).

In Cell (i, j), a_{ij} coins are placed.

You can perform the following operation any number of times:

Operation: Choose a cell that was not chosen before and contains one or more coins, then move one of those coins to a vertically or horizontally adjacent cell.

Maximize the number of cells containing an even number of coins.

Constraints

All values in input are integers.

1 \leq H, W \leq 500

0 \leq a_{ij} \leq 9

Input

Input is given from Standard Input in the following format:

H W
a_{11} a_{12} ... a_{1W}
a_{21} a_{22} ... a_{2W}
:
a_{H1} a_{H2} ... a_{HW}

Output

Print a sequence of operations that maximizes the number of cells containing an even number of coins, in the following format:

N
y_1 x_1 y_1' x_1'
y_2 x_2 y_2' x_2'
:
y_N x_N y_N' x_N'

That is, in the first line, print an integer N between 0 and H \times W (inclusive), representing the number of operations.

In the (i+1)-th line (1 \leq i \leq N), print four integers y_i, x_i, y_i' and x_i' (1 \leq y_i, y_i' \leq H and 1 \leq x_i, x_i' \leq W), representing the i-th operation. These four integers represents the operation of moving one of the coins placed in Cell (y_i, x_i) to a vertically or horizontally adjacent cell, (y_i', x_i').

Note that if the specified operation violates the specification in the problem statement or the output format is invalid, it will result in Wrong Answer.

Sample Input 1

2 3
1 2 3
0 1 1

Sample Output 1

3
2 2 2 3
1 1 1 2
1 3 1 2

Every cell contains an even number of coins after the following sequence of operations:

Move the coin in Cell (2, 2) to Cell (2, 3).

Move the coin in Cell (1, 1) to Cell (1, 2).

Move one of the coins in Cell (1, 3) to Cell (1, 2).

Sample Input 2

3 2
1 0
2 1
1 0

Sample Output 2

3
1 1 1 2
1 2 2 2
3 1 3 2

Sample Input 3

1 5
9 9 9 9 9

Sample Output 3

2
1 1 1 2
1 3 1 4


open Printf
open Scanf

let id x = x

let k = scanf "%d" id

let () =
    let n = k / 2 in
    if k mod 2 = 0 then begin
      printf "%d\n" (n * n)
    end else begin
      printf "%d \n" (n * (n + 1))
    end

Score : 100 points

Problem Statement

Find the number of ways to choose a pair of an even number and an odd number from the positive integers between 1 and K (inclusive). The order does not matter.

Constraints

2\leq K\leq 100

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the number of ways to choose a pair of an even number and an odd number from the positive integers between 1 and K (inclusive).

Sample Input 1

3

Sample Output 1

2

Two pairs can be chosen: (2,1) and (2,3).

Sample Input 2

6

Sample Output 2

9

Sample Input 3

11

Sample Output 3

30

Sample Input 4

50

Sample Output 4

625


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

(* open MyInt64 *)
let () =
	let x1,y1,x2,y2 = scanf " %d %d %d %d" (fun u v w x -> (u,v,w,x))
	in
	let dx,dy = x2 - x1, y2 - y1 in
	let px,py = x2 - dy, y2 + dx in
	let qx,qy = px - dx, py - dy in
	printf "%d %d %d %d\n" px py qx qy

Score : 200 points

Problem Statement

There is a square in the xy-plane. The coordinates of its four vertices are (x_1,y_1),(x_2,y_2),(x_3,y_3) and (x_4,y_4) in counter-clockwise order.
(Assume that the positive x-axis points right, and the positive y-axis points up.)

Takahashi remembers (x_1,y_1) and (x_2,y_2), but he has forgot (x_3,y_3) and (x_4,y_4).

Given x_1,x_2,y_1,y_2, restore x_3,y_3,x_4,y_4. It can be shown that x_3,y_3,x_4 and y_4 uniquely exist and have integer values.

Constraints

|x_1|,|y_1|,|x_2|,|y_2| \leq 100

(x_1,y_1) ≠ (x_2,y_2)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

x_1 y_1 x_2 y_2

Output

Print x_3,y_3,x_4 and y_4 as integers, in this order.

Sample Input 1

0 0 0 1

Sample Output 1

-1 1 -1 0

(0,0),(0,1),(-1,1),(-1,0) is the four vertices of a square in counter-clockwise order.
Note that (x_3,y_3)=(1,1),(x_4,y_4)=(1,0) is not accepted, as the vertices are in clockwise order.

Sample Input 2

2 3 6 6

Sample Output 2

3 10 -1 7

Sample Input 3

31 -41 -59 26

Sample Output 3

-126 -64 -36 -131


open Printf
open Scanf

let id x = x

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

let cond v = v mod k = 0

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 rec step_loop st ed step f =
  if st <= ed then begin
    f st; step_loop (st + step) ed step f
  end else ()

let () =
  let count = ref 0 in
  for a = 1 to n do
    let m1 = a mod k in
    let m2 = (k - m1) mod k in
    if m1 = m2 then
      if m1 = 0 then begin
        count := !count + (n / k) * (n / k);
      end else begin
        let v = (n - m1) / k + 1 in
        count := !count + v * v
      end;
  done;
  printf "%d\n" !count

Score : 300 points

Problem Statement

You are given integers N and K. Find the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K.
The order of a,b,c does matter, and some of them can be the same.

Constraints

1 \leq N,K \leq 2\times 10^5

N and K are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K.

Sample Input 1

3 2

Sample Output 1

9

(1,1,1),(1,1,3),(1,3,1),(1,3,3),(2,2,2),(3,1,1),(3,1,3),(3,3,1) and (3,3,3) satisfy the condition.

Sample Input 2

5 3

Sample Output 2

1

Sample Input 3

31415 9265

Sample Output 3

27

Sample Input 4

35897 932

Sample Output 4

114191


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

let () =
	let n,k = scanf " %d %d" (fun u v -> u,v)
	in let q = n/k in
	(
		if k mod 2 = 1 then (
			q*q*q
		) else (
			q*q*q + (
				let v = k/2 in let r = n mod k in
				if r>=v then let qq=q+1 in qq*qq*qq
				else q*q*q
			)
		)
	)
	|> printf "%d\n"


let n, k = Scanf.sscanf (read_line ()) "%d %d" @@ fun n k -> (n, k)

let rec pow x n = if n = 1 then x else x * pow x (n - 1)

let () =
  let a = n / k in
  let b = (n + k / 2) / k in
  Printf.printf "%d %d\n" a b;
  Printf.printf "%d\n" @@ pow a 3 + if k mod 2 = 0 then pow b 3 else 0


Scanf.scanf "%d %d" (fun n i ->
    Printf.printf "%d\n" (n - i + 1)
)

Score : 100 points

Problem Statement

There is an N-car train.

You are given an integer i. Find the value of j such that the following statement is true: "the i-th car from the front of the train is the j-th car from the back."

Constraints

1 \leq N \leq 100

1 \leq i \leq N

Input

Input is given from Standard Input in the following format:

N i

Output

Print the answer.

Sample Input 1

4 2

Sample Output 1

3

The second car from the front of a 4-car train is the third car from the back.

Sample Input 2

1 1

Sample Output 2

1

Sample Input 3

15 11

Sample Output 3

5


let n, i = Scanf.scanf " %d %d" @@ fun a b -> a, b
let _ = Printf.printf "%d\n" @@ n - i + 1


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

  Printf.printf "%d\n"
    (if a = 1 then 1 else a-b+1)