Scanf.scanf "%d" (fun n ->
    let rec loop i acc =
        if i = n then acc else
            let acc = Scanf.scanf " %f %s" (fun x u ->
                if u = "JPY" then acc +. x else acc +. x *. 380000.
            )
            in
            loop (i + 1) acc
    in
    loop 0 0. |> Printf.printf "%f\n"
)

Score : 200 points

Problem Statement

Takahashi received otoshidama (New Year's money gifts) from N of his relatives.

You are given N values x_1, x_2, ..., x_N and N strings u_1, u_2, ..., u_N as input. Each string u_i is either JPY or BTC, and x_i and u_i represent the content of the otoshidama from the i-th relative.

For example, if x_1 = 10000 and u_1 = JPY, the otoshidama from the first relative is 10000 Japanese yen; if x_2 = 0.10000000 and u_2 = BTC, the otoshidama from the second relative is 0.1 bitcoins.

If we convert the bitcoins into yen at the rate of 380000.0 JPY per 1.0 BTC, how much are the gifts worth in total?

Constraints

2 \leq N \leq 10

u_i = JPY or BTC.

If u_i = JPY, x_i is an integer such that 1 \leq x_i \leq 10^8.

If u_i = BTC, x_i is a decimal with 8 decimal digits, such that 0.00000001 \leq x_i \leq 100.00000000.

Input

Input is given from Standard Input in the following format:

N
x_1 u_1
x_2 u_2
:
x_N u_N

Output

If the gifts are worth Y yen in total, print the value Y (not necessarily an integer).

Output will be judged correct when the absolute or relative error from the judge's output is at most 10^{-5}.

Sample Input 1

2
10000 JPY
0.10000000 BTC

Sample Output 1

48000.0

The otoshidama from the first relative is 10000 yen. The otoshidama from the second relative is 0.1 bitcoins, which is worth 38000.0 yen if converted at the rate of 380000.0 JPY per 1.0 BTC. The sum of these is 48000.0 yen.

Outputs such as 48000 and 48000.1 will also be judged correct.

Sample Input 2

3
100000000 JPY
100.00000000 BTC
0.00000001 BTC

Sample Output 2

138000000.0038

In this case, outputs such as 138001000 and 1.38e8 will also be judged correct.


open Printf open Scanf
open Array

let () = scanf " %d" @@ fun n ->
  printf "%.120f" (
    init n (fun _ -> scanf " %f %s" @@ fun x -> function
    | "BTC" -> x *. 380000.
    | "JPY" -> x
    ) |> fold_left (+.) 0.
  )

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

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.


let (>>=) xs f = List.map f xs |> List.concat
let rec choice s c acc = function
  | [] -> if s = 0 then [] else [(s, c-10, acc)]
  | x::xs -> choice s c (x::acc) xs @ choice (s+x) (c+10) acc xs
let () =
  Scanf.scanf "%d %d %d %d" @@ fun n a b c ->
  let l = Array.init n (fun _ -> Scanf.scanf " %d" ((+) 0)) |> Array.to_list in
  (choice 0 0 [] l >>= fun (a', wa, l) ->
   choice 0 0 [] l >>= fun (b', wb, l) ->
   choice 0 0 [] l >>= fun (c', wc, l) ->
     [wa + wb + wc + abs (a - a') + abs (b - b') + abs (c - c')])
  |> List.fold_left min 1010101010
  |> Printf.printf "%d\n"

Score : 300 points

Problem Statement

You have N bamboos. The lengths (in centimeters) of these are l_1, l_2, ..., l_N, respectively.

Your objective is to use some of these bamboos (possibly all) to obtain three bamboos of length A, B, C. For that, you can use the following three kinds of magics any number:

Extension Magic: Consumes 1 MP (magic point). Choose one bamboo and increase its length by 1.

Shortening Magic: Consumes 1 MP. Choose one bamboo of length at least 2 and decrease its length by 1.

Composition Magic: Consumes 10 MP. Choose two bamboos and combine them into one bamboo. The length of this new bamboo is equal to the sum of the lengths of the two bamboos combined. (Afterwards, further magics can be used on this bamboo.)

At least how much MP is needed to achieve the objective?

Constraints

3 \leq N \leq 8

1 \leq C < B < A \leq 1000

1 \leq l_i \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B C
l_1
l_2
:
l_N

Output

Print the minimum amount of MP needed to achieve the objective.

Sample Input 1

5 100 90 80
98
40
30
21
80

Sample Output 1

23

We are obtaining three bamboos of lengths 100, 90, 80 from five bamboos 98, 40, 30, 21, 80. We already have a bamboo of length 80, and we can obtain bamboos of lengths 100, 90 by using the magics as follows at the total cost of 23 MP, which is optimal.

Use Extension Magic twice on the bamboo of length 98 to obtain a bamboo of length 100. (MP consumed: 2)

Use Composition Magic on the bamboos of lengths 40, 30 to obtain a bamboo of length 70. (MP consumed: 10)

Use Shortening Magic once on the bamboo of length 21 to obtain a bamboo of length 20. (MP consumed: 1)

Use Composition Magic on the bamboo of length 70 obtained in step 2 and the bamboo of length 20 obtained in step 3 to obtain a bamboo of length 90. (MP consumed: 10)

Sample Input 2

8 100 90 80
100
100
90
90
90
80
80
80

Sample Output 2

0

If we already have all bamboos of the desired lengths, the amount of MP needed is 0. As seen here, we do not necessarily need to use all the bamboos.

Sample Input 3

8 1000 800 100
300
333
400
444
500
555
600
666

Sample Output 3

243


(* q log(max a b) *)
let kInf = 20202020202
let a, b, q = Scanf.scanf " %d %d %d" @@ fun a b c -> a, b, c
let ns = [|a; b|]
let sts = Array.init 2 @@ fun i ->
  let ds = Array.make (ns.(i) + 2) 0 in
  ds.(0) <- -kInf + i; ds.(ns.(i) + 1) <- kInf + i;
  for j = 1 to ns.(i) do ds.(j) <- Scanf.scanf " %d" @@ (+) 0 done;
  ds
let xs = Array.init q @@ fun _ -> Scanf.scanf " %d" @@ (+) 0
let is_ok i key a = a.(i) >= key
let binary_search key a =
  let left, right = ref @@ -1, ref @@ Array.length a in
  while !right - !left > 1 do
    let mid = !left + (!right - !left) / 2 in
    if is_ok mid key a then right := mid
    else left := mid done;
  !right
let f x =
  let si1, ti1 = binary_search x sts.(0), binary_search x sts.(1) in
  let s0, s1, t0, t1 = sts.(0).(si1 - 1), sts.(0).(si1), sts.(1).(ti1 - 1), sts.(1).(ti1) in
  let ans = ref kInf in
  ([s0; s1] |> List.iter @@ fun s ->
    [t0; t1] |> List.iter @@ fun t ->
      ans := min !ans @@ min (abs (x - s)) (abs (x - t)) + abs (t - s));
  Printf.printf "%d\n" !ans
let _ = Array.iter f xs

Score : 400 points

Problem Statement

Along a road running in an east-west direction, there are A shrines and B temples.
The i-th shrine from the west is located at a distance of s_i meters from the west end of the road, and the i-th temple from the west is located at a distance of t_i meters from the west end of the road.

Answer the following Q queries:

Query i (1 \leq i \leq Q): If we start from a point at a distance of x_i meters from the west end of the road and freely travel along the road, what is the minimum distance that needs to be traveled in order to visit one shrine and one temple? (It is allowed to pass by more shrines and temples than required.)

Constraints

1 \leq A, B \leq 10^5

1 \leq Q \leq 10^5

1 \leq s_1 < s_2 < ... < s_A \leq 10^{10}

1 \leq t_1 < t_2 < ... < t_B \leq 10^{10}

1 \leq x_i \leq 10^{10}

s_1, ..., s_A, t_1, ..., t_B, x_1, ..., x_Q are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B Q
s_1
:
s_A
t_1
:
t_B
x_1
:
x_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.

Sample Input 1

2 3 4
100
600
400
900
1000
150
2000
899
799

Sample Output 1

350
1400
301
399

There are two shrines and three temples. The shrines are located at distances of 100, 600 meters from the west end of the road, and the temples are located at distances of 400, 900, 1000 meters from the west end of the road.

Query 1: If we start from a point at a distance of 150 meters from the west end of the road, the optimal move is first to walk 50 meters west to visit a shrine, then to walk 300 meters east to visit a temple.

Query 2: If we start from a point at a distance of 2000 meters from the west end of the road, the optimal move is first to walk 1000 meters west to visit a temple, then to walk 400 meters west to visit a shrine. We will pass by another temple on the way, but it is fine.

Query 3: If we start from a point at a distance of 899 meters from the west end of the road, the optimal move is first to walk 1 meter east to visit a temple, then to walk 300 meters west to visit a shrine.

Query 4: If we start from a point at a distance of 799 meters from the west end of the road, the optimal move is first to walk 199 meters west to visit a shrine, then to walk 200 meters west to visit a temple.

Sample Input 2

1 1 3
1
10000000000
2
9999999999
5000000000

Sample Output 2

10000000000
10000000000
14999999998

The road is quite long, and we may need to travel a distance that does not fit into a 32-bit integer.


(*
 * forall l r (p : forall n, l < n <= r -> bool),
 *   ( exists m,
 *     l < m <= r /\ forall n H, p n H = true <-> m <= n ) ->
 *   { m |
 *     l < m <= r /\ forall n H, p n H = true <-> m <= n }
 *)
let rec lower_bound l r p =
  if r <= 1 + l
  then r
  else let m = (l + r) / 2 in
       if p m
       then lower_bound l m p
       else lower_bound m r p

(*
 * forall l r (p : forall n, l <= n < r -> bool),
 *   ( exists m,
 *     l <= m < r /\ forall n H, p n H = true <-> n <= m ) ->
 *   { m |
 *     l <= m < r /\ forall n H, p n H = true <-> n <= m }
 *)
let rec upper_bound l r p =
  if r <= 1 + l
  then l
  else let m = (l + r) / 2 in
       if p m
       then upper_bound m r p
       else upper_bound l m p

let () = Scanf.scanf "%d %d %d\n" @@ fun a b q ->
  let ss = Array.make (a + 2) 0 in
  let ts = Array.make (b + 2) 0 in
  ss.(0) <- -123456789012345;
  ss.(a + 1) <- 123456789012345;
  for i = 1 to a do
    Scanf.scanf "%d\n" @@ Array.set ss i
  done;
  ts.(0) <- -123456789012345;
  ts.(b + 1) <- 123456789012345;
  for i = 1 to b do
    Scanf.scanf "%d\n" @@ Array.set ts i
  done;
  for i = 1 to q do
    Scanf.scanf "%d\n" @@ fun x ->
      let ls = upper_bound 0 (a + 2) @@ fun i -> ss.(i) <= x in
      let rs = lower_bound (-1) (a + 1) @@ fun i -> x <= ss.(i) in
      let lt = upper_bound 0 (b + 2) @@ fun i -> ts.(i) <= x in
      let rt = lower_bound (-1) (b + 1) @@ fun i -> x <= ts.(i) in
      Printf.printf "%d\n" @@ List.fold_left min max_int
      [ x - min ss.(ls) ts.(lt);
        max ss.(rs) ts.(rt) - x;
        ss.(rs) - x + ss.(rs) - ts.(lt);
        x - ss.(ls) + ts.(rt) - ss.(ls);
        ts.(rt) - x + ts.(rt) - ss.(ls);
        x - ts.(lt) + ss.(rs) - ts.(lt) ]
  done


let divisor a b = if b mod a = 0 then b + a else b - a

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

Score : 100 points

Problem Statement

You are given positive integers A and B.

If A is a divisor of B, print A + B; otherwise, print B - A.

Constraints

All values in input are integers.

1 \leq A \leq B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

If A is a divisor of B, print A + B; otherwise, print B - A.

Sample Input 1

4 12

Sample Output 1

16

As 4 is a divisor of 12, 4 + 12 = 16 should be printed.

Sample Input 2

8 20

Sample Output 2

12

Sample Input 3

1 1

Sample Output 3

2

1 is a divisor of 1.


let n, m = Scanf.scanf " %d %d" @@ fun a b -> a, b
let a_ss = Array.init n @@ fun _ -> Scanf.scanf " %d" @@ fun k -> Array.init k @@ fun _ -> Scanf.scanf " %d" @@ fun a -> a - 1
let cs = Array.make m 0
let f a_s = Array.iter (fun a -> cs.(a) <- cs.(a) + 1) a_s
let _ = Array.iter f a_ss; Array.fold_left (fun ans c -> ans + if c = n then 1 else 0) 0 cs |> Printf.printf "%d\n"

Score : 200 points

Problem Statement

Katsusando loves omelette rice.

Besides, he loves crème brûlée, tenderloin steak and so on, and believes that these foods are all loved by everyone.

To prove that hypothesis, he conducted a survey on M kinds of foods and asked N people whether they like these foods or not.

The i-th person answered that he/she only likes the A_{i1}-th, A_{i2}-th, ..., A_{iK_i}-th food.

Find the number of the foods liked by all the N people.

Constraints

All values in input are integers.

1 \leq N, M \leq 30

1 \leq K_i \leq M

1 \leq A_{ij} \leq M

For each i (1 \leq i \leq N), A_{i1}, A_{i2}, ..., A_{iK_i} are distinct.

Constraints

Input is given from Standard Input in the following format:

N M
K_1 A_{11} A_{12} ... A_{1K_1}
K_2 A_{21} A_{22} ... A_{2K_2}
:
K_N A_{N1} A_{N2} ... A_{NK_N}

Output

Print the number of the foods liked by all the N people.

Sample Input 1

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

Sample Output 1

1

As only the third food is liked by all the three people, 1 should be printed.

Sample Input 2

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

Sample Output 2

0

Katsusando's hypothesis turned out to be wrong.

Sample Input 3

1 30
3 5 10 30

Sample Output 3

3


let _ =
  let n, m = Scanf.scanf "%d %d\n" (fun n m -> n, m) in
  let aa = Array.make n [] in
  for i = 0 to n - 1 do
    let k = Scanf.scanf "%d " (fun k -> k) in
    for j = 1 to k do
      Scanf.scanf (if j <> k then "%d " else "%d\n")
        (fun a -> aa.(i) <- a::aa.(i))
    done
  done;
  let reslist = ref aa.(0) in
  for i = 1 to n - 1 do
    reslist := List.filter (fun e -> List.mem e aa.(i)) !reslist
  done;
  List.length !reslist
  |> print_int


let rec gcd a b = if b = 0 then a else gcd b (a mod b)
let () =
  let n = Scanf.scanf "%d\n" (fun x -> x) in
  let lst = Array.to_list
      (Array.init n (fun _ -> Scanf.scanf "%d " (fun x -> x))) in
  let head = List.hd lst in
  let tail = List.tl lst in
  Printf.printf "%d\n" (List.fold_left gcd head tail)

Score : 300 points

Problem Statement

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

Initially, the health of Monster i is A_i.

Below, a monster with at least 1 health is called alive.

Until there is only one alive monster, the following is repeated:

A random alive monster attacks another random alive monster.

As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.

Find the minimum possible final health of the last monster alive.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible final health of the last monster alive.

Sample Input 1

4
2 10 8 40

Sample Output 1

2

When only the first monster keeps on attacking, the final health of the last monster will be 2, which is minimum.

Sample Input 2

4
5 13 8 1000000000

Sample Output 2

1

Sample Input 3

3
1000000000 1000000000 1000000000

Sample Output 3

1000000000


let solve monsters =
  let min_hp = List.hd monsters in
  let rec inner cur_min =
    let ls = List.filter (fun e -> e mod cur_min <> 0) monsters in
    match ls with
    | [] -> cur_min
    | ls ->
       List.fold_left (fun me e -> min me (e mod cur_min)) max_int ls |> inner
  in inner min_hp

let _ =
  let n = Scanf.scanf "%d\n" (fun n -> n) in
  let monsters = ref [] in
  for i = 0 to n - 1 do
    Scanf.scanf (if i <> n - 1 then "%d " else "%d\n")
      (fun m -> monsters := m::!monsters)
  done;
  List.sort (compare) !monsters
  |> solve |> print_int


let rec fix f x = f (fix f) x;;
Scanf.(Array.(
  let e = make 4 [] in
  init 3 (fun i ->
    scanf " %d %d" @@ fun u v -> (
      let u,v = u-1,v-1 in
      e.(u) <- (v,i) :: e.(u);
      e.(v) <- (u,i) :: e.(v)
    )
  ) |> ignore;
  init 3 (fun i ->
    fix (fun f count v a ->
      if count=3 then (
        if fold_left (&&) true a then
          (print_string "YES"; exit 0)
      ) else (
        List.iter (fun (w,i) ->
          if not a.(i) then (
            a.(i) <- true;
            f (count+1) w a;
            a.(i) <- false;
          )
        ) e.(v)
      )
    ) 0 i @@ make 3 false
  ) |> ignore;
  print_string "NO"
))

Score : 200 points

Problem Statement

There are four towns, numbered 1,2,3 and 4.
Also, there are three roads. The i-th road connects different towns a_i and b_i bidirectionally.
No two roads connect the same pair of towns. Other than these roads, there is no way to travel between these towns, but any town can be reached from any other town using these roads.

Determine if we can visit all the towns by traversing each of the roads exactly once.

Constraints

1 \leq a_i,b_i \leq 4(1\leq i\leq 3)

a_i and b_i are different. (1\leq i\leq 3)

No two roads connect the same pair of towns.

Any town can be reached from any other town using the roads.

Input

Input is given from Standard Input in the following format:

a_1 b_1
a_2 b_2
a_3 b_3

Output

If we can visit all the towns by traversing each of the roads exactly once, print YES; otherwise, print NO.

Sample Input 1

4 2
1 3
2 3

Sample Output 1

YES

We can visit all the towns in the order 1,3,2,4.

Sample Input 2

3 2
2 4
1 2

Sample Output 2

NO

Sample Input 3

2 1
3 2
4 3

Sample Output 3

YES


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

    let m3 a b c = max a (max b c) in

    let rec loop i e1 o2 e3 mx =
        if i = l then sum - mx else
            let ne1 =
                if a.(i) = 0       then max 0 (e1 - 2) else
                if a.(i) = 1       then max 0 (e1 - 1) else
                if a.(i) mod 2 = 1 then a.(i) - 1 + e1 else
                                        a.(i)     + e1
            in
            let no2 =
                if a.(i) = 0       then max 0  (max o2 e1 - 1) else
                if a.(i) mod 2 = 1 then a.(i) + max o2 e1      else
                                        a.(i) + max o2 e1 - 1
            in
            let ne3 =
                if a.(i) = 0       then max 0  (m3 e1 o2 e3 - 2) else
                if a.(i) = 1       then max 0  (m3 e1 o2 e3 - 1) else
                if a.(i) mod 2 = 1 then a.(i) + m3 e1 o2 e3 - 1  else
                                        a.(i) + m3 e1 o2 e3
            in
            let nmx = max (max ne1 no2) (max ne3 mx) in
            loop (i + 1) ne1 no2 ne3 nmx
    in
    loop 0 0 0 0 0 |> Printf.printf "%d\n"
)

Score : 600 points

Problem Statement

Snuke stands on a number line. He has L ears, and he will walk along the line continuously under the following conditions:

He never visits a point with coordinate less than 0, or a point with coordinate greater than L.

He starts walking at a point with integer coordinate, and also finishes walking at a point with integer coordinate.

He only changes direction at a point with integer coordinate.

Each time when Snuke passes a point with coordinate i-0.5, where i is an integer, he put a stone in his i-th ear.

After Snuke finishes walking, Ringo will repeat the following operations in some order so that, for each i, Snuke's i-th ear contains A_i stones:

Put a stone in one of Snuke's ears.

Remove a stone from one of Snuke's ears.

Find the minimum number of operations required when Ringo can freely decide how Snuke walks.

Constraints

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

0 \leq A_i \leq 10^9(1\leq i\leq L)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

L
A_1
:
A_L

Output

Print the minimum number of operations required when Ringo can freely decide how Snuke walks.

Sample Input 1

4
1
0
2
3

Sample Output 1

1

Assume that Snuke walks as follows:

He starts walking at coordinate 3 and finishes walking at coordinate 4, visiting coordinates 3,4,3,2,3,4 in this order.

Then, Snuke's four ears will contain 0,0,2,3 stones, respectively.
Ringo can satisfy the requirement by putting one stone in the first ear.

Sample Input 2

8
2
0
0
2
1
3
4
1

Sample Output 2

3

Sample Input 3

7
314159265
358979323
846264338
327950288
419716939
937510582
0

Sample Output 3

1


let () =
  Scanf.scanf "%d" @@ fun l ->
  let a = Array.init (l+1) @@ fun i ->
    if i = 0 then 0
    else Scanf.scanf " %d" ((+) 0)
  in
  for i = 1 to l do a.(i) <- a.(i) + a.(i-1) done;
  let a x y = a.(y) - a.(x-1) in

  let dpl1, dpl2 = Array.make (l+1) 0, Array.make (l+1) 0 in
  let dpr1, dpr2 = Array.make (l+2) 0, Array.make (l+2) 0 in
  for i = 1 to l do
    dpl1.(i) <- min (a 1 i) @@
      dpl1.(i-1) + if a i i = 0 then 2 else a i i mod 2;
    dpl2.(i) <- min dpl1.(i-1) dpl2.(i-1) + (a i i + 1) mod 2;
  done;
  for i = l downto 1 do
    dpr1.(i) <- min (a i l) @@
      dpr1.(i+1) + if a i i = 0 then 2 else a i i mod 2;
    dpr2.(i) <- min dpr1.(i+1) dpr2.(i+1) + (a i i + 1) mod 2;
  done;

  Array.init (l+1) (fun i ->
    dpl1.(i) + dpr1.(i+1)
    |> min (dpl1.(i) + dpr2.(i+1))
    |> min (dpl2.(i) + dpr1.(i+1)))
  |> Array.fold_left min max_int
  |> Printf.printf "%d\n"


let () =
  Scanf.scanf "%d" @@ fun l ->
  let a = Array.init (l+1) @@ fun i ->
    if i = 0 then 0
    else Scanf.scanf " %d" ((+) 0)
  in
  for i = 1 to l do a.(i) <- a.(i) + a.(i-1) done;

  let dp1 = Array.make (l+1) 0 in
  let dp2 = Array.make (l+2) 0 in
  for i = 1 to l do
    dp1.(i) <- min a.(i) (dp1.(i-1) + (a.(i) - a.(i-1) + 1) mod 2)
  done;
  for i = l downto 1 do
    dp2.(i) <- min (a.(l) - a.(i-1)) @@
      dp2.(i+1) + if a.(i) - a.(i-1) = 0 then 2 else (a.(i) - a.(i-1)) mod 2
  done;

  Array.init (l+1) (fun i -> dp1.(i) + dp2.(i+1))
  |> Array.fold_left min max_int
  |> Printf.printf "%d\n"


let _ =
  let t, x = Scanf.scanf "%f %f\n" (fun t x -> t, x) in
  print_float @@ t /. x

Score : 100 points

Problem Statement

In order to pass the entrance examination tomorrow, Taro has to study for T more hours.

Fortunately, he can leap to World B where time passes X times as fast as it does in our world (World A).

While (X \times t) hours pass in World B, t hours pass in World A.

How many hours will pass in World A while Taro studies for T hours in World B?

Constraints

All values in input are integers.

1 \leq T \leq 100

1 \leq X \leq 100

Input

Input is given from Standard Input in the following format:

T X

Output

Print the number of hours that will pass in World A.

The output will be regarded as correct when its absolute or relative error from the judge's output is at most 10^{-3}.

Sample Input 1

8 3

Sample Output 1

2.6666666667

While Taro studies for eight hours in World B where time passes three times as fast, 2.6666... hours will pass in World A.

Note that an absolute or relative error of at most 10^{-3} is allowed.

Sample Input 2

99 1

Sample Output 2

99.0000000000

Sample Input 3

1 100

Sample Output 3

0.0100000000


Scanf.scanf " %f %f" @@ fun t x ->
  print_float @@ t /. x


let () = Scanf.scanf "%f %f" @@ fun t x -> Printf.printf "%f" @@ t /. x


let () = Scanf.scanf "%d" @@ fun n ->
  let l_ary = Array.init n @@ fun _ -> Scanf.scanf " %d" Fun.id in
  Printf.printf "%s\n" @@
  if 2 * (Array.fold_left max min_int l_ary) < Array.fold_left ( + ) 0 l_ary then "Yes" else "No"

Score : 200 points

Problem Statement

Determine if an N-sided polygon (not necessarily convex) with sides of length L_1, L_2, ..., L_N can be drawn in a two-dimensional plane.

You can use the following theorem:

Theorem: an N-sided polygon satisfying the condition can be drawn if and only if the longest side is strictly shorter than the sum of the lengths of the other N-1 sides.

Constraints

All values in input are integers.

3 \leq N \leq 10

1 \leq L_i \leq 100

Input

Input is given from Standard Input in the following format:

N
L_1 L_2 ... L_N

Output

If an N-sided polygon satisfying the condition can be drawn, print Yes; otherwise, print No.

Sample Input 1

4
3 8 5 1

Sample Output 1

Yes

Since 8 < 9 = 3 + 5 + 1, it follows from the theorem that such a polygon can be drawn on a plane.

Sample Input 2

4
3 8 4 1

Sample Output 2

No

Since 8 \geq 8 = 3 + 4 + 1, it follows from the theorem that such a polygon cannot be drawn on a plane.

Sample Input 3

10
1 8 10 5 8 12 34 100 11 3

Sample Output 3

No


Scanf.scanf "%d %d" (fun n m ->
    let x = Array.init m (fun _ -> Scanf.scanf " %d" (fun x -> x)) in
    Array.sort compare x;
    let xd = Array.init (m - 1) (fun i -> x.(i + 1) - x.(i)) in
    Array.sort compare xd;
    let rec loop i acc =
        if i < max 0 (m - n) then acc else
            loop (i - 1) (acc - xd.(i))
    in
    loop (m - 2) (x.(m - 1) - x.(0)) |> Printf.printf "%d\n"
)

Score : 300 points

Problem Statement

We will play a one-player game using a number line and N pieces.

First, we place each of these pieces at some integer coordinate.

Here, multiple pieces can be placed at the same coordinate.

Our objective is to visit all of the M coordinates X_1, X_2, ..., X_M with these pieces, by repeating the following move:

Move: Choose a piece and let x be its coordinate. Put that piece at coordinate x+1 or x-1.

Note that the coordinates where we initially place the pieces are already regarded as visited.

Find the minimum number of moves required to achieve the objective.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

-10^5 \leq X_i \leq 10^5

X_1, X_2, ..., X_M are all different.

Input

Input is given from Standard Input in the following format:

N M
X_1 X_2 ... X_M

Output

Find the minimum number of moves required to achieve the objective.

Sample Input 1

2 5
10 12 1 2 14

Sample Output 1

5

The objective can be achieved in five moves as follows, and this is the minimum number of moves required.

Initially, put the two pieces at coordinates 1 and 10.

Move the piece at coordinate 1 to 2.

Move the piece at coordinate 10 to 11.

Move the piece at coordinate 11 to 12.

Move the piece at coordinate 12 to 13.

Move the piece at coordinate 13 to 14.

Sample Input 2

3 7
-10 -3 0 9 -100 2 17

Sample Output 2

19

Sample Input 3

100 1
-100000

Sample Output 3

0


Scanf.scanf "%d %d" @@ fun n m ->
let xs = Array.init m (fun _ -> Scanf.scanf " %d" ((+) 0)) in
Array.sort (-) xs;
let ds = Array.init (m-1) (fun i -> xs.(i+1) - xs.(i)) in
Array.sort (-) ds;
Array.sub ds 0 (max 0 @@ m-n) |> Array.fold_left (+) 0
|> Printf.printf "%d\n"


Scanf.scanf " %d %d %d" @@ fun n a b ->
  Printf.printf "%d %d" (min a b) (max (a+b-n) 0)

Score : 100 points

Problem Statement

We conducted a survey on newspaper subscriptions.
More specifically, we asked each of the N respondents the following two questions:

Question 1: Are you subscribing to Newspaper X?

Question 2: Are you subscribing to Newspaper Y?

As the result, A respondents answered "yes" to Question 1, and B respondents answered "yes" to Question 2.

What are the maximum possible number and the minimum possible number of respondents subscribing to both newspapers X and Y?

Write a program to answer this question.

Constraints

1 \leq N \leq 100

0 \leq A \leq N

0 \leq B \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the maximum possible number and the minimum possible number of respondents subscribing to both newspapers, in this order, with a space in between.

Sample Input 1

10 3 5

Sample Output 1

3 0

In this sample, out of the 10 respondents, 3 answered they are subscribing to Newspaper X, and 5 answered they are subscribing to Newspaper Y.

Here, the number of respondents subscribing to both newspapers is at most 3 and at least 0.

Sample Input 2

10 7 5

Sample Output 2

5 2

In this sample, out of the 10 respondents, 7 answered they are subscribing to Newspaper X, and 5 answered they are subscribing to Newspaper Y.

Here, the number of respondents subscribing to both newspapers is at most 5 and at least 2.

Sample Input 3

100 100 100

Sample Output 3

100 100


let n, a, b, c = Scanf.scanf " %d %s %s %s" @@ fun a b c d -> a, b, c, d
let _ = Array.(fold_left (+) 0 @@ init n (fun i -> List.(length @@ sort_uniq compare [a.[i]; b.[i]; c.[i]]) - 1)) |> Printf.printf "%d\n"

Score : 200 points

Problem Statement

You are given three strings A, B and C. Each of these is a string of length N consisting of lowercase English letters.

Our objective is to make all these three strings equal. For that, you can repeatedly perform the following operation:

Operation: Choose one of the strings A, B and C, and specify an integer i between 1 and N (inclusive). Change the i-th character from the beginning of the chosen string to some other lowercase English letter.

What is the minimum number of operations required to achieve the objective?

Constraints

1 \leq N \leq 100

Each of the strings A, B and C is a string of length N.

Each character in each of the strings A, B and C is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

N
A
B
C

Output

Print the minimum number of operations required.

Sample Input 1

4
west
east
wait

Sample Output 1

3

In this sample, initially A = west、B = east、C = wait. We can achieve the objective in the minimum number of operations by performing three operations as follows:

Change the second character in A to a. A is now wast.

Change the first character in B to w. B is now wast.

Change the third character in C to s. C is now wast.

Sample Input 2

9
different
different
different

Sample Output 2

0

If A, B and C are already equal in the beginning, the number of operations required is 0.

Sample Input 3

7
zenkoku
touitsu
program

Sample Output 3

13


let n = Scanf.sscanf (read_line ()) "%d" @@ fun n -> n
let ab = Array.to_list @@ Array.init n @@ fun _ -> Scanf.sscanf (read_line ()) "%d %d" @@ fun a b -> (a, b)
let ab = List.sort (fun (a, b) (c, d) -> (c + d) - (a + b)) ab

type next = T | A

let rec loop sum_t sum_a next rest = match (next, rest) with
  | (_, []) -> sum_t - sum_a
  | (T, (a, _) :: xs) -> loop (sum_t + a) sum_a A xs
  | (A, (_, b) :: xs) -> loop sum_t (sum_a + b) T xs

let () = Printf.printf "%d\n" @@ loop 0 0 T ab

Score : 400 points

Problem Statement

There are N dishes of cuisine placed in front of Takahashi and Aoki.
For convenience, we call these dishes Dish 1, Dish 2, ..., Dish N.

When Takahashi eats Dish i, he earns A_i points of happiness; when Aoki eats Dish i, she earns B_i points of happiness.

Starting from Takahashi, they alternately choose one dish and eat it, until there is no more dish to eat.
Here, both of them choose dishes so that the following value is maximized: "the sum of the happiness he/she will earn in the end" minus "the sum of the happiness the other person will earn in the end".

Find the value: "the sum of the happiness Takahashi earns in the end" minus "the sum of the happiness Aoki earns in the end".

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print the value: "the sum of the happiness Takahashi earns in the end" minus "the sum of the happiness Aoki earns in the end".

Sample Input 1

3
10 10
20 20
30 30

Sample Output 1

20

In this sample, both of them earns 10 points of happiness by eating Dish 1, 20 points by eating Dish 2, and 30 points by eating Dish 3.

In this case, since Takahashi and Aoki have the same "taste", each time they will choose the dish with which they can earn the greatest happiness. Thus, first Takahashi will choose Dish 3, then Aoki will choose Dish 2, and finally Takahashi will choose Dish 1, so the answer is (30 + 10) - 20 = 20.

Sample Input 2

3
20 10
20 20
20 30

Sample Output 2

20

In this sample, Takahashi earns 20 points of happiness by eating any one of the dishes 1, 2 and 3, but Aoki earns 10 points of happiness by eating Dish 1, 20 points by eating Dish 2, and 30 points by eating Dish 3.

In this case, since only Aoki has likes and dislikes, each time they will choose the dish with which Aoki can earn the greatest happiness. Thus, first Takahashi will choose Dish 3, then Aoki will choose Dish 2, and finally Takahashi will choose Dish 1, so the answer is (20 + 20) - 20 = 20.

Sample Input 3

6
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000

Sample Output 3

-2999999997

Note that the answer may not fit into a 32-bit integer.


Scanf.scanf "%d %d" (fun n m ->
    let nei = Array.make n [] in
    let rev = Array.make n 0 in
    for i = 1 to n - 1 + m do
        Scanf.scanf " %d %d" (fun a b ->
            let a = a - 1 in
            let b = b - 1 in
            nei.(a) <- b :: nei.(a);
            rev.(b) <- rev.(b) + 1
        )
    done;
    let rec loop i =
        if rev.(i) = 0 then i else loop (i + 1)
    in
    let root = loop 0 in
    let par = Array.make n 0 in

    let module S = Set.Make (struct type t = int let compare = compare end) in

    let rec loop next = function
        | [] -> if not (S.is_empty next) then loop S.empty (S.elements next)
        | hd :: tl ->
            let rec loop2 next = function
                | [] -> loop next tl
                | x :: xs ->
                        let next = S.add x next in
                        par.(x) <- hd + 1; loop2 next xs
            in
            loop2 next nei.(hd)
    in
    loop S.empty [ root ];
    Array.iter (Printf.printf "%d\n") par
)

Score : 500 points

Problem Statement

There is a rooted tree (see Notes) with N vertices numbered 1 to N.
Each of the vertices, except the root, has a directed edge coming from its parent.
Note that the root may not be Vertex 1.

Takahashi has added M new directed edges to this graph.
Each of these M edges, u \rightarrow v, extends from some vertex u to its descendant v.

You are given the directed graph with N vertices and N-1+M edges after Takahashi added edges.
More specifically, you are given N-1+M pairs of integers, (A_1, B_1), ..., (A_{N-1+M}, B_{N-1+M}), which represent that the i-th edge extends from Vertex A_i to Vertex B_i.

Restore the original rooted tree.

Notes

For "tree" and other related terms in graph theory, see the article in Wikipedia, for example.

Constraints

3 \leq N

1 \leq M

N + M \leq 10^5

1 \leq A_i, B_i \leq N

A_i \neq B_i

If i \neq j, (A_i, B_i) \neq (A_j, B_j).

The graph in input can be obtained by adding M edges satisfying the condition in the problem statement to a rooted tree with N vertices.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_{N-1+M} B_{N-1+M}

Output

Print N lines.
In the i-th line, print 0 if Vertex i is the root of the original tree, and otherwise print the integer representing the parent of Vertex i in the original tree.

Note that it can be shown that the original tree is uniquely determined.

Sample Input 1

3 1
1 2
1 3
2 3

Sample Output 1

0
1
2

The graph in this input is shown below:

It can be seen that this graph is obtained by adding the edge 1 \rightarrow 3 to the rooted tree 1 \rightarrow 2 \rightarrow 3.

Sample Input 2

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

Sample Output 2

6
4
2
0
6
2


Scanf.scanf "%d %d %d" (fun a b c -> Printf.printf "%d\n" @@ (a * b) / 2)

Score : 100 points

Problem Statement

There is a right triangle ABC with ∠ABC=90°.

Given the lengths of the three sides, |AB|,|BC| and |CA|, find the area of the right triangle ABC.

It is guaranteed that the area of the triangle ABC is an integer.

Constraints

1 \leq |AB|,|BC|,|CA| \leq 100

All values in input are integers.

The area of the triangle ABC is an integer.

Input

Input is given from Standard Input in the following format:

|AB| |BC| |CA|

Output

Print the area of the triangle ABC.

Sample Input 1

3 4 5

Sample Output 1

6

This triangle has an area of 6.

Sample Input 2

5 12 13

Sample Output 2

30

This triangle has an area of 30.

Sample Input 3

45 28 53

Sample Output 3

630

This triangle has an area of 630.


open Hashtbl
let h = create 1000000
let s = read_int ()
let f n = if n mod 2 = 0 then n / 2 else 3 * n + 1
let x, i = ref s, ref 1
let _ = while not @@ mem h !x do add h !x 1; x := f !x; incr i done; Printf.printf "%d\n" !i

Score : 200 points

Problem Statement

A sequence a=\{a_1,a_2,a_3,......\} is determined as follows:

The first term s is given as input.

Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd.

a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1.

Find the minimum integer m that satisfies the following condition:

There exists an integer n such that a_m = a_n (m > n).

Constraints

1 \leq s \leq 100

All values in input are integers.

It is guaranteed that all elements in a and the minimum m that satisfies the condition are at most 1000000.

Input

Input is given from Standard Input in the following format:

s

Output

Print the minimum integer m that satisfies the condition.

Sample Input 1

8

Sample Output 1

5

a=\{8,4,2,1,4,2,1,4,2,1,......\}. As a_5=a_2, the answer is 5.

Sample Input 2

7

Sample Output 2

18

a=\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\}.

Sample Input 3

54

Sample Output 3

114


let s = read_int ()
let f n = if n mod 2 = 0 then n / 2 else 3 * n + 1
let cs, p = Array.make 1000001 0, ref s
let _ = cs.(s) <- 1; for i = 2 to 1000000 do p := f !p; if cs.(!p) = 0 then cs.(!p) <- 1 else (Printf.printf "%d\n" i; exit 0) done


let kMax = 1000000

let _ =
  let s = Scanf.scanf "%d" @@ (+) 0 in
  let flags = Array.make (kMax + 10) false in
  flags.(s) <- true;
  let n = ref s in
  for i = 2 to kMax do
    n := if !n mod 2 = 0 then !n / 2 else 3 * !n + 1;
    if flags.(!n) then (Printf.printf "%d\n" i; exit 0);
    flags.(!n) <- true
  done


let f n = if n mod 2 = 0 then n/2 else 3 * n + 1
let rec g n lst cont =
  let m = f n in
  if List.mem m lst then cont+1 else g m (n :: lst) (cont+1)
let () =
  Scanf.scanf "%d\n" (fun s -> g s [] 1)
  |> print_int


let rec fix f x = f (fix f) x;;
Scanf.(Array.(scanf " %d" @@ fun n ->
  let a = make 2000000 false in
  let col n = if n mod 2 = 0 then n/2 else 3*n+1 in
  print_int @@ fix (fun f i x ->
    if a.(x) then i
    else (
      a.(x) <- true;
      f (i+1) (col x)
    )
  ) 1 n))


let rec f i = function
    | 1 -> i+3
    | 2 -> i+3
    | 4 -> i+3
    | n when n mod 2 = 0 ->
        f (i+1) (n / 2)
    | n ->
        f (i+1) (n*3+1)

let () = Scanf.scanf "%d" @@ fun s -> Printf.printf "%d" (f 1 s)


let sub hs b = List.map (fun a -> a - b) hs

let divide hs =
  (*
  Printf.printf "hs@divide: %s\n" (List.fold_left (fun acc h -> (string_of_int h) ^ "; " ^ acc) "" hs);
   *)
  let rec inner left temp min_h = function
  | [] -> temp::left, min_h
  | h::hs when h = 0 -> inner (temp::left) [] 101 hs
  | h::hs -> inner left (h::temp) (min min_h h) hs
  in inner [] [] 101 hs

let rec solve = function
  | [] -> 0
  | hs ->
     match divide hs with
     | [], min_h -> 0
     | hss, min_h when List.length hss = 1 -> min_h + (solve @@ sub hs min_h)
     | hss, _ -> List.fold_left (fun acc hs -> acc + solve hs) 0 hss

let _ =
  let n = Scanf.scanf "%d\n" (fun n -> n) in
  let hs = ref [] in
  for i = 0 to n - 1 do
    Scanf.scanf (if i <> n - 1 then "%d " else "%d\n") (fun h -> hs := h::!hs)
  done;
  solve !hs |> print_int

Score : 300 points

Problem Statement

In a flower bed, there are N flowers, numbered 1,2,......,N. Initially, the heights of all flowers are 0.
You are given a sequence h=\{h_1,h_2,h_3,......\} as input. You would like to change the height of Flower k to h_k for all k (1 \leq k \leq N), by repeating the following "watering" operation:

Specify integers l and r. Increase the height of Flower x by 1 for all x such that l \leq x \leq r.

Find the minimum number of watering operations required to satisfy the condition.

Constraints

1 \leq N \leq 100

0 \leq h_i \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 h_3 ...... h_N

Output

Print the minimum number of watering operations required to satisfy the condition.

Sample Input 1

4
1 2 2 1

Sample Output 1

2

The minimum number of watering operations required is 2.
One way to achieve it is:

Perform the operation with (l,r)=(1,3).

Perform the operation with (l,r)=(2,4).

Sample Input 2

5
3 1 2 3 1

Sample Output 2

5

Sample Input 3

8
4 23 75 0 23 96 50 100

Sample Output 3

221


Scanf.(Array.(scanf " %d" @@ fun n ->
  init n (fun i -> scanf " %d" @@ fun v -> v)
  |> fold_left (fun (sum,prev) v ->
    if prev < v then sum + v - prev,v
    else sum,v
  ) (0,0)
  |> fst |> print_int))


Scanf.scanf " %s" @@ fun s ->
  let n = String.length s in
  for i=0 to n-1 do
    for j=i to n-1 do
      let t = String.(sub s 0 i ^ sub s j (n-j)) in
      print_endline t;
      if t = "keyence" then (
        print_endline "YES"; exit 0)
    done
  done;
  print_endline "NO"

Score : 200 points

Problem Statement

A string is called a KEYENCE string when it can be changed to keyence by removing its contiguous substring (possibly empty) only once.

Given a string S consisting of lowercase English letters, determine if S is a KEYENCE string.

Constraints

The length of S is between 7 and 100 (inclusive).

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S is a KEYENCE string, print YES; otherwise, print NO.

Sample Input 1

keyofscience

Sample Output 1

YES

keyence is an abbreviation of key of science.

Sample Input 2

mpyszsbznf

Sample Output 2

NO

Sample Input 3

ashlfyha

Sample Output 3

NO

Sample Input 4

keyence

Sample Output 4

YES


let n, a, b = Scanf.scanf " %d %d %d" @@ fun a b c -> a, b, c
let ps = Array.init n @@ fun _ -> Scanf.scanf " %d" (+) 0
let cs = Array.make 3 0
let f i = cs.(i) <- cs.(i) + 1
let _ = Array.iter (fun p -> if p <= a then f 0 else if p <= b then f 1 else f 2) ps;
  Array.fold_left min 100 cs |> Printf.printf "%d\n"

Score : 200 points

Problem Statement

You have written N problems to hold programming contests.
The i-th problem will have a score of P_i points if used in a contest.

With these problems, you would like to hold as many contests as possible under the following condition:

A contest has three problems. The first problem has a score not greater than A points, the second has a score between A + 1 and B points (inclusive), and the third has a score not less than B + 1 points.

The same problem should not be used in multiple contests.
At most how many contests can be held?

Constraints

3 \leq N \leq 100

1 \leq P_i \leq 20 (1 \leq i \leq N)

1 \leq A < B < 20

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A B
P_1 P_2 ... P_N

Output

Print the answer.

Sample Input 1

7
5 15
1 10 16 2 7 20 12

Sample Output 1

2

Two contests can be held by putting the first, second, third problems and the fourth, fifth, sixth problems together.

Sample Input 2

8
3 8
5 5 5 10 10 10 15 20

Sample Output 2

0

No contest can be held, because there is no problem with a score of A = 3 or less.

Sample Input 3

3
5 6
5 6 10

Sample Output 3

1


let () =
  Scanf.scanf "%d %d %d" @@ fun n a b ->
  let c = Array.init n (fun _ -> Scanf.scanf " %d" ((+) 0)) in
  let x = Array.fold_left (fun s v -> if v <= a then s+1 else s) 0 c in
  let y = Array.fold_left (fun s v -> if v <= b then s+1 else s) 0 c in
  min x (y-x) |> min (n-y) |> Printf.printf "%d\n"


let () = Scanf.scanf "%d\n%d %d\n" @@ fun n a b ->
  let ps =
    Array.to_list @@
    Array.init n @@ fun _ ->
      Scanf.scanf "%d " @@ fun p -> p in
  Printf.printf "%d\n" @@
    List.fold_left min max_int @@
    List.map List.length
    [ List.filter (fun p -> p <= a) ps;
      List.filter (fun p -> a < p && p <= b) ps;
      List.filter (fun p -> b < p) ps ]


let memoize n f =
  let dp = Hashtbl.create n in
  let rec get x =
    try Hashtbl.find dp x with
    | Not_found ->
        let result = f get x in
        Hashtbl.add dp x result;
        result in
  get

let () = Scanf.scanf "%d %d\n" @@ fun n m ->
  let es = Array.make n [] in
  for i = 1 to m do
    Scanf.scanf "%d %d\n" @@ fun x y ->
      es.(x - 1) <- y - 1 :: es.(x - 1)
  done;
  let dp = memoize n @@ fun dp v ->
    List.fold_right (fun u -> max @@ 1 + dp u) es.(v) 0 in
  Printf.printf "%d\n" @@ Array.fold_left max min_int @@ Array.init n dp

Score : 100 points

Problem Statement

There is a directed graph G with N vertices and M edges.
The vertices are numbered 1, 2, \ldots, N, and for each i (1 \leq i \leq M), the i-th directed edge goes from Vertex x_i to y_i.
G does not contain directed cycles.

Find the length of the longest directed path in G.
Here, the length of a directed path is the number of edges in it.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq x_i, y_i \leq N

All pairs (x_i, y_i) are distinct.

G does not contain directed cycles.

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the length of the longest directed path in G.

Sample Input 1

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

Sample Output 1

3

The red directed path in the following figure is the longest:

Sample Input 2

6 3
2 3
4 5
5 6

Sample Output 2

2

The red directed path in the following figure is the longest:

Sample Input 3

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

Sample Output 3

3

The red directed path in the following figure is one of the longest:


open Printf

let n = int_of_string @@ read_line ()
let a =
  Str.split (Str.regexp " ") @@ read_line ()
  |> List.map int_of_string

let cnt = Array.make 4 0
let dp = Array.make (n+1) @@ Array.make_matrix (0) (0) (-1.0)

let rec calc: int -> int -> int -> float = fun i1 i2 i3 ->
  if dp.(i1).(i2).(i3) <> -1.0 then dp.(i1).(i2).(i3)
  else
    let numer =
      (float n)
      +. (if i1 = 0 then 0. else ((float i1) *. calc (i1-1) i2 i3))
      +. (if i2 = 0 then 0. else ((float i2) *. calc (i1+1) (i2-1) i3))
      +. (if i3 = 0 then 0. else ((float i3) *. calc i1 (i2+1) (i3-1))) in
    let denom = float @@ i1 + i2 + i3 in

    let res = numer /. denom in

    dp.(i1).(i2).(i3) <- res ;
    res

let () =
  List.iter (fun x -> cnt.(x) <- cnt.(x) + 1) a ;
  for z = 0 to n do
        dp.(z) <- Array.make_matrix (n+1) (n+1) (-1.0)
      done;
  dp.(0).(0).(0) <- 0.0 ;
  begin
    calc cnt.(1) cnt.(2) cnt.(3)
    |> string_of_float
    |> print_endline
  end

Score : 100 points

Problem Statement

There are N dishes, numbered 1, 2, \ldots, N.
Initially, for each i (1 \leq i \leq N), Dish i has a_i (1 \leq a_i \leq 3) pieces of sushi on it.

Taro will perform the following operation repeatedly until all the pieces of sushi are eaten:

Roll a die that shows the numbers 1, 2, \ldots, N with equal probabilities, and let i be the outcome. If there are some pieces of sushi on Dish i, eat one of them; if there is none, do nothing.

Find the expected number of times the operation is performed before all the pieces of sushi are eaten.

Constraints

All values in input are integers.

1 \leq N \leq 300

1 \leq a_i \leq 3

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N

Output

Print the expected number of times the operation is performed before all the pieces of sushi are eaten.
The output is considered correct when the relative difference is not greater than 10^{-9}.

Sample Input 1

3
1 1 1

Sample Output 1

5.5

The expected number of operations before the first piece of sushi is eaten, is 1.
After that, the expected number of operations before the second sushi is eaten, is 1.5.
After that, the expected number of operations before the third sushi is eaten, is 3.
Thus, the expected total number of operations is 1 + 1.5 + 3 = 5.5.

Sample Input 2

1
3

Sample Output 2

3

Outputs such as 3.00, 3.000000003 and 2.999999997 will also be accepted.

Sample Input 3

2
1 2

Sample Output 3

4.5

Sample Input 4

10
1 3 2 3 3 2 3 2 1 3

Sample Output 4

54.48064457488221


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

let () =
    let n :: k :: _ = getLI() in
    let inl = getAI() in
    let dp = Array.make (k+1) false in
    rep (k+1) (fun i ->
        Array.iter (fun v ->
            if v+i <= k then
                dp.(v+i) <- dp.(v+i) || not dp.(i);
        ) inl
    );
    puts @@ if dp.(k) then "First" else "Second"

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 rec solve i j t =
  if t then
    if i = j then arr.(i)
    else
      max (solve (succ i) j  false + arr.(i))
        (solve i (pred j) false + arr.(j))
  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)

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.


Scanf.scanf "%d %d %d" (fun a b c ->
    let bc = min b c in
    let o = bc * 2 in
    let c = c - bc in
    let b = b - bc in

    let ac = min a c in
    let o = o + ac in
    let c = c - ac in
    let a = a - ac in
    let o = if c > 0 then o + 1 else o in
    let o = o + b in
    Printf.printf "%d\n" o
)

Score : 200 points

Problem Statement

Takahashi has A untasty cookies containing antidotes, B tasty cookies containing antidotes and C tasty cookies containing poison.

Eating a cookie containing poison results in a stomachache, and eating a cookie containing poison while having a stomachache results in a death.
As he wants to live, he cannot eat one in such a situation.
Eating a cookie containing antidotes while having a stomachache cures it, and there is no other way to cure stomachaches.

Find the maximum number of tasty cookies that Takahashi can eat.

Constraints

0 \leq A,B,C \leq 10^9

A,B and C are integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the maximum number of tasty cookies that Takahashi can eat.

Sample Input 1

3 1 4

Sample Output 1

5

We can eat all tasty cookies, in the following order:

A tasty cookie containing poison

An untasty cookie containing antidotes

A tasty cookie containing poison

A tasty cookie containing antidotes

A tasty cookie containing poison

An untasty cookie containing antidotes

A tasty cookie containing poison

Sample Input 2

5 2 9

Sample Output 2

10

Sample Input 3

8 8 1

Sample Output 3

9


let n = read_line ()
let f x s = let n = ref 0 in String.iter (fun c -> if c = x then incr n) s; !n
let _ = Printf.printf "%d\n" @@ f '2' n

Score : 100 points

Problem Statement

You are given an integer N that has exactly four digits in base ten.
How many times does 2 occur in the base-ten representation of N?

Constraints

1000 \leq N \leq 9999

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

1222

Sample Output 1

3

2 occurs three times in 1222. By the way, this contest is held on December 22 (JST).

Sample Input 2

3456

Sample Output 2

0

Sample Input 3

9592

Sample Output 3

1


(* 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 b * pow b (n - 1)
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


(* log(n) *)
let rec is_divisible n m c =
  if c = 0 then
    true
  else
    if n mod m = 0 then
      is_divisible (n / m) m (c - 1)
    else
      false

(* O(√p * log(p)) *)
let solve n p =
  if n = 1 then
    p
  else (* n >= 2 *)
    let m = int_of_float (floor (sqrt (float_of_int p))) in
    let rec loop g =
      if is_divisible p g n then
        g
      else
        loop (g - 1)
    in
      loop m (* n >= 2 では g <= m (∵ 指数関数の単調増加性より) *)

let _ = Scanf.scanf "%d %d" (fun n p -> solve n p |> string_of_int |> print_endline)


(* O(√n) *)
let lowest_prime_factor n =
  let m = int_of_float (floor (sqrt (float_of_int n))) in
  let rec loop i =
    if i > m then
      n
    else
      if n mod i = 0 then i else loop (i + 1)
  in
    loop 2

(* O(√n * log(n)) *)
let factorize_primes n =
  if n <= 1 then
    []
  else
    let rec loop sofar (p, count) m =
      if m = 1 then
        List.tl (List.rev ((p, count) :: sofar))
      else
        let prime = lowest_prime_factor m in
        if p = prime then
          loop sofar (p, count + 1) (m / p)
        else
          loop ((p, count) :: sofar) (prime, 1) (m / prime)
    in
      loop [] (1, 0) n

let solve n p =
  if p = 1 then
    1
  else
    List.fold_left
      (fun sofar (p, count) ->
        let factor = int_of_float (float_of_int p ** float_of_int (count / n)) in
        sofar * factor)
      1
      (factorize_primes p)

let _ = Scanf.scanf "%d %d" (fun n p -> solve n p |> string_of_int |> print_endline)


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_.(0) <> as_.(1)
      then "second"
      else "first"

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 rec f maxN others lst =
  match lst with
  | [] -> (maxN, others)
  | x :: xs -> if x > maxN then f x (maxN :: others) xs else f maxN (x :: others) xs
let rec sum lst = match lst with
  | [] -> 0
  | x :: xs -> x + sum xs
let () =
  Scanf.scanf "%d\n"
    (fun n -> Array.init n (fun _ -> Scanf.scanf "%d\n" (fun x -> x)))
  |> Array.to_list |> f 0 []
  |> (fun (maxN, others) -> maxN/2 + sum others) |> print_int

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.


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 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 mx = fold_left max 0L a in
  printf "%Ld\n" @@
    (fold_left (+) 0L a - mx/2L)


open Printf
open Scanf

let solve x =
  if List.mem x [3;5;7] then "YES" else "NO"

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

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.


print_endline @@ match read_int () with 3 | 5 | 7 -> "YES" | _ -> "NO"


let () =
  Scanf.scanf "%d\n"
    (fun x -> if x = 3 || x = 5 || x = 7 then "Yes" else "No") |> print_string


let n = read_int ();;
let yes = "YES";;
let no = "NO";;
print_endline ((function 3->yes|5->yes|7->yes|n->no) n)


let s = read_line ()
let ans = ref max_int
let f i = Char.code s.[i] - 48
let _ =
  for i = 0 to String.length s - 3 do ans := min !ans @@ abs @@ f i * 100 + f (i + 1) * 10 + f (i + 2) - 753 done;
  Printf.printf "%d\n" !ans

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.


let n = Scanf.scanf " %d" (+) 0
let a_s = Array.init n @@ fun _ -> Scanf.scanf " %f" (+.) 0.
let avg = Array.fold_left (+.) 0. a_s /. float n
let ans, m = ref 0, ref max_float
let _ = Array.iteri (fun i a -> let d = abs_float (a -. avg) in if d < !m then (ans := i; m := d)) a_s; Printf.printf "%d\n" !ans

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.


let () = Scanf.scanf "%d\n" @@ fun n ->
  let as_ = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun a -> a in
  let sum = Array.fold_left ( + ) 0 as_ in
  Printf.printf "%d\n" @@ Array.fold_left (fun i j ->
    if abs (sum - as_.(i) * n) <= abs (sum - as_.(j) * n) then i else j) 0 @@
  Array.init n @@ fun i -> i


let () = Scanf.scanf "%d\n%d %d\n" @@ fun n t a ->
  let hs = Array.init n @@ fun i -> Scanf.scanf "%d " @@ fun h -> i, h in
  Printf.printf "%d\n" @@ succ @@ fst @@
  Array.fold_left (fun (i, hi) (j, hj) ->
    if abs (1000 * a - 1000 * t + hi * 6) <= abs (1000 * a - 1000 * t + hj * 6)
    then (i, hi)
    else (j, hj)) hs.(0) hs

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 () =
  Scanf.scanf "%d %d" @@ fun n m ->
  let towns = Array.init m (fun i ->
    Scanf.scanf " %d %d" @@ fun p y -> y, i, p) in
  let l6 = Array.make n 1 in
  let answers = Array.make m "" in
  Array.sort compare towns;
  towns |> Array.iter (fun (y,i,p) ->
    answers.(i) <-
      Printf.sprintf "%06d%06d" p l6.(p-1);
    l6.(p-1) <- l6.(p-1) + 1);
  answers |> Array.iter print_endline

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 s = read_line ()
let _ = if String.length s = 2 then print_endline s else Printf.printf "%c%c%c\n" s.[2] s.[1] s.[0]

Score : 100 points

Problem Statement

You are given a string S of length 2 or 3 consisting of lowercase English letters. If the length of the string is 2, print it as is; if the length is 3, print the string after reversing it.

Constraints

The length of S is 2 or 3.

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If the length of S is 2, print S as is; if the length is 3, print S after reversing it.

Sample Input 1

abc

Sample Output 1

cba

As the length of S is 3, we print it after reversing it.

Sample Input 2

ac

Sample Output 2

ac

As the length of S is 2, we print it as is.


Scanf.scanf "%d %d %d" (fun a b k ->
    let rec loop a b r =
        if r = 0 then (if k mod 2 = 0 then Printf.printf "%d %d\n" a b
                                      else Printf.printf "%d %d\n" b a)
        else
            let c = a / 2 in
            loop (b + c) (a / 2 * 2 - c) (r - 1)
    in
    loop a b k
)

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


let a, b, k = Scanf.scanf " %d %d %d" @@ fun a b c -> a, b, c
let ns = [|a; b|]
let _ =
  for i = 0 to k - 1 do
    let r = i mod 2 in
    if ns.(r) mod 2 = 1 then ns.(r) <- ns.(r) - 1; ns.(1 - r) <- ns.(1 - r) + ns.(r) / 2; ns.(r) <- ns.(r) / 2 done;
  Printf.printf "%d %d\n" ns.(0) ns.(1)


Scanf.scanf "%d" (fun n ->
    let dump k =
        let a = Array.make k [] in
        let rec loop_i i cur =
            let rec loop_j j cur =
                if j >= k then cur else (
                    a.(i) <- cur :: a.(i);
                    a.(j) <- cur :: a.(j);
                    loop_j (j + 1) (cur + 1)
                )
            in
            if i = k then () else
                loop_i (i + 1) (loop_j (i + 1) cur)
        in
        loop_i 0 1;
        Array.iter (fun l ->
            let b = Array.of_list l in
            Printf.printf "%d" (Array.length b);
            Array.iter (Printf.printf " %d") b;
            print_newline ()) a
    in

    let rec loop k =
        let k2 = k * (k - 1) / 2 in
        if k2 = n then k else
            if k2 < n then loop (k + 1) else (-1)
    in
    let k = loop 2 in
    if k < 0 then print_endline "No" else (
        Printf.printf "Yes\n%d\n" k;
        dump k
    )
)

Score : 500 points

Problem Statement

You are given an integer N. Determine if there exists a tuple of subsets of \{1,2,...N\}, (S_1,S_2,...,S_k), that satisfies the following conditions:

Each of the integers 1,2,...,N is contained in exactly two of the sets S_1,S_2,...,S_k.

Any two of the sets S_1,S_2,...,S_k have exactly one element in common.

If such a tuple exists, construct one such tuple.

Constraints

1 \leq N \leq 10^5

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If a tuple of subsets of \{1,2,...N\} that satisfies the conditions does not exist, print No.
If such a tuple exists, print Yes first, then print such subsets in the following format:

k
|S_1| S_{1,1} S_{1,2} ... S_{1,|S_1|}
:
|S_k| S_{k,1} S_{k,2} ... S_{k,|S_k|}

where S_i={S_{i,1},S_{i,2},...,S_{i,|S_i|}}.

If there are multiple such tuples, any of them will be accepted.

Sample Input 1

3

Sample Output 1

Yes
3
2 1 2
2 3 1
2 2 3

It can be seen that (S_1,S_2,S_3)=(\{1,2\},\{3,1\},\{2,3\}) satisfies the conditions.

Sample Input 2

4

Sample Output 2

No


let rec f a b = if b = 0 then a else f b (a mod b)
let rec g a b = a / f a b * b
let n, m, s, t, l = Scanf.scanf "%d %d %s %s" @@ fun n m s t -> n, m, s, t, g n m
let n', m' = l / n, l / m
let _ = for k = 0 to (n - 1) / m' do if k * n' < m && s.[k * m'] <> t.[k * n'] then (print_endline "-1"; exit 0) done; Printf.printf "%d\n" l

Score : 300 points

Problem Statement

You are given a string S of length N and another string T of length M.
These strings consist of lowercase English letters.

A string X is called a good string when the following conditions are all met:

Let L be the length of X. L is divisible by both N and M.

Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.

Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.

Determine if there exists a good string. If it exists, find the length of the shortest such string.

Constraints

1 \leq N,M \leq 10^5

S and T consist of lowercase English letters.

|S|=N

|T|=M

Input

Input is given from Standard Input in the following format:

N M
S
T

Output

If a good string does not exist, print -1; if it exists, print the length of the shortest such string.

Sample Input 1

3 2
acp
ae

Sample Output 1

6

For example, the string accept is a good string.
There is no good string shorter than this, so the answer is 6.

Sample Input 2

6 3
abcdef
abc

Sample Output 2

-1

Sample Input 3

15 9
dnsusrayukuaiia
dujrunuma

Sample Output 3

45


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 f a = match a with
1 -> "Hello World"
| _ -> Scanf.scanf "%d %d" (fun a b -> string_of_int (a+b));;
Scanf.scanf "%d" f
|> Printf.printf "%s\n"


let _ =
	let n = Scanf.scanf "%d\n" (fun n -> n) in
    (
    match n with
    | 1 -> "Hello World"
    | 2 ->
    	let a, b = Scanf.scanf "%d\n%d\n" (fun a b -> a, b) in
        string_of_int @@ a + b
    | _ -> failwith "hoge"
    ) |> print_string


Scanf.scanf "%d\n" (fun n ->
  if n = 1 then Printf.printf "Hello World\n" else
  Scanf.scanf "%d\n%d\n" (fun a b ->
    Printf.printf "%d\n" (a+b)
  )
)


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 divisors_all 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 (%$) = Pervasives.(mod)
let () =
	let n,m = get_2_i64 0 in
	let d = m / n in
	for i=i32 d downto 1 do
		if (i32 m) %$ i = 0 then (
			let v = i *$ ((i32 n)-$1) in
			if ((i32 m) -$ v) %$ i = 0 then (
				printf "%d\n" i; exit 0;
			)
		)
	done;

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
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 binary_search ng ok f =
	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 let d = if ng < ok then 1L else -1L
	in f0 ng ok
	(* in f0 (ng-1L*d) (ok+1L*d) *)
let () =
	let n,m = get_2_i64 0 in
	let d = m / n in
	repm 1L d 1L (fun u i ->
		let v = i * (n - 1L) in
		(* printf "%Ld - %Ld: mod %Ld = %Ld\n" m v i ((m-v) mod i); *)
		if (m-v) mod i = 0L then `Ok i else `Ok u
	)
	|> printf "%Ld\n"
	(* binary_search d 0L (fun i ->
		let v = i * (n - 1L) in
		printf "%Ld - %Ld: mod %Ld = %Ld\n" m v i ((m-v) mod i);
		if (m-v) mod i = 0L then true else false
	)
	|> printf "%Ld\n" *)


let () =
  Scanf.scanf "%1d%1d%1d\n"
    (fun a b c ->
       if a = 1 then if b = 1 then if c = 1 then 999 else 991
         else if c = 1 then 919 else 911
       else if b = 1 then if c = 1 then 199 else 191
       else if c = 1 then 119 else 111
    )
  |> print_int

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


print_endline @@ String.map (function '1' -> '9' | '9' -> '1') @@ read_line ()


let n = read_int ()
let _ = Printf.printf "%d\n" @@ (n + 110) / 111 * 111

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


let s = read_line () in
let c = String.get s 0 in
let len = String.length s in
let s = String.make len c in

Printf.printf "%s\n" s


let () = Scanf.scanf "%d" @@ fun n -> Printf.printf "%d\n" @@ n + 110 - (n + 110) mod 111


let n = Scanf.scanf " %d" (+) 0
let vs = Array.init n @@ fun _ -> Scanf.scanf " %d" (+) 0
let css = Array.make_matrix 2 100001 0
let ms = ref [|0, 0; 0, 0|]
let e0, e1, o0, o1 = Array.iteri (fun i v -> let c = css.(i mod 2).(v) + 1 in css.(i mod 2).(v) <- c; !ms.(i mod 2) <- max !ms.(i mod 2) (c, v)) vs;
  List.iter (fun i -> Array.sort (fun x y -> compare y x) css.(i)) [0; 1];
  css.(0).(0), css.(0).(1), css.(1).(0), css.(1).(1)
let _ = Printf.printf "%d\n" @@ if snd !ms.(0) = snd !ms.(1) then n - max (e0 + o1) (e1 + o0) else n - e0 - o0

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 /\/\/\/.


let () =
	Scanf.scanf "%d" (fun n ->
    	let whole_array = Array.init n (fun _ -> Scanf.scanf " %d" (fun x -> x))
        in
        let list = Array.to_list whole_array in
        let (odd_l, even_l) =
        let rec make_l ol el l =
        match l with
        | [] -> (ol, el)
        | x :: [] -> (x :: ol, el)
        | x :: y :: rest -> make_l (x :: ol) (y :: el) rest
        in make_l [] [] list
        in
       (* let rec majority l *)
       (*odd_l, even_lそれぞれについて出現回数の一番大きい数と二番目に大きい数を求める*)
       (*一番大きい数が異なれば単純にそれぞれの編集回数の和が答え。等しければ二番目を考慮*)

       (*以下無関係なコード*)
        let tes_sum = Array.fold_left (+) 0 whole_array in
         Printf.printf "%d" tes_sum)