let r = read_int ()
let _ = print_endline (string_of_int (r * r))

Score : 100 points

Problem Statement

Given is an integer r.

How many times is the area of a circle of radius r larger than the area of a circle of radius 1?

It can be proved that the answer is always an integer under the constraints given.

Constraints

1 \leq r \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

r

Output

Print the area of a circle of radius r, divided by the area of a circle of radius 1, as an integer.

Sample Input 1

2

Sample Output 1

4

The area of a circle of radius 2 is 4 times larger than the area of a circle of radius 1.

Note that output must be an integer - for example, 4.0 will not be accepted.

Sample Input 2

100

Sample Output 2

10000


let input = int_of_string(read_line()) in
    print_int(input*input)

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 m = 1000000007
let ( * ) a b = a * b mod m
let rec f n a = if n <= 1 then a else f (n - 1) (a * n)
let rec g b n = if n <= 0 then 1 else let p = g b (n / 2) in p * p * if n mod 2 = 0 then 1 else b
let c n r = if n < r || n < 0 || r < 0 then 0 else f n 1 * g (f r 1) (m - 2) * g (f (n - r) 1) (m - 2)
let _ = Scanf.scanf "%d %d" @@ fun x y -> Printf.printf "%d\n" @@ if (x + y) mod 3 > 0 then 0 else c ((x + y) / 3) (y - (x + y) / 3)

Score : 400 points

Problem Statement

There is a knight - the chess piece - at the origin (0, 0) of a two-dimensional grid.

When the knight is at the square (i, j), it can be moved to either (i+1,j+2) or (i+2, j+1).

In how many ways can the knight reach the square (X, Y)?

Find the number of ways modulo 10^9 + 7.

Constraints

1 \leq X \leq 10^6

1 \leq Y \leq 10^6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print the number of ways for the knight to reach (X, Y) from (0, 0), modulo 10^9 + 7.

Sample Input 1

3 3

Sample Output 1

2

There are two ways: (0,0) \to (1,2) \to (3,3) and (0,0) \to (2,1) \to (3,3).

Sample Input 2

2 2

Sample Output 2

0

The knight cannot reach (2,2).

Sample Input 3

999999 999999

Sample Output 3

151840682

Print the number of ways modulo 10^9 + 7.


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

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

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

let () = Scanf.scanf "%d %d\n" @@ fun x y ->
  Printf.printf "%d\n" @@ if
    2 * x - y < 0 ||
    0 < (2 * x - y) mod 3 ||
    2 * y - x < 0 ||
    0 < (2 * y - x) mod 3
  then 0
  else comb ((x + y) / 3) ((2 * x - y) / 3)


Scanf.sscanf (read_line ()) "%d" (fun a -> let r = (a - 1) / 2 in Printf.printf "%d\n" r)

Score : 100 points

Problem Statement

How many ways are there to choose two distinct positive integers totaling N, disregarding the order?

Constraints

1 \leq N \leq 10^6

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

4

Sample Output 1

1

There is only one way to choose two distinct integers totaling 4: to choose 1 and 3. (Choosing 3 and 1 is not considered different from this.)

Sample Input 2

999999

Sample Output 2

499999


let id x = x

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

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

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

let f (lst, arr) a =
  match arr with
    | [] -> failwith ""
    | x :: xs -> (a, if a = lst then (x + 1) :: xs else 1 :: x :: xs)

let ss =
  let t = List.rev_append
    (if List.hd (List.rev s) = "<" then [0] else [])
    (snd @@ List.fold_left f (List.hd s, [1]) (List.tl s)) in
  if List.hd s = ">" then 0 :: t else t

let rec g = function
  | ([] | _ :: []) -> []
  | x :: y :: xs -> if x > y then x :: (y - 1) :: g xs else (x - 1) :: y :: g xs

let () =
  ss
    |> g
    |> List.map (fun i -> i * (i + 1) / 2)
    |> List.fold_left (+) 0
    |> print_int;
  print_newline ()

Score : 300 points

Problem Statement

Given is a string S of length N-1.
Each character in S is < or >.

A sequence of N non-negative integers, a_1,a_2,\cdots,a_N, is said to be good when the following condition is satisfied for all i (1 \leq i \leq N-1):

If S_i= <: a_i<a_{i+1}

If S_i= >: a_i>a_{i+1}

Find the minimum possible sum of the elements of a good sequence of N non-negative integers.

Constraints

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

S is a string of length N-1 consisting of < and >.

Input

Input is given from Standard Input in the following format:

S

Output

Find the minimum possible sum of the elements of a good sequence of N non-negative integers.

Sample Input 1

<>>

Sample Output 1

3

a=(0,2,1,0) is a good sequence whose sum is 3.
There is no good sequence whose sum is less than 3.

Sample Input 2

<>>><<><<<<<>>><

Sample Output 2

28


Scanf.scanf "%d" (fun n ->
    let l = Array.make n 0 in
    let r = Array.make n 0 in
    for i = 0 to n - 1 do
        Scanf.scanf " %d %d" (fun a b -> l.(i) <- a; r.(i) <- b + 1)
    done;

    let rec loop i p q =
        if i = n then p, q else
            let p = if l.(i) > l.(p) then i else p in
            let q = if r.(i) < r.(q) then i else q in
            loop (i + 1) p q
    in
    let p, q = loop 0 0 0 in

    let result1 =
        let rec loop j acc =
            if j = n then acc + max (r.(q) - l.(p)) 0 else
            if j = p || j = q then loop (j + 1) acc else
                loop (j + 1) (max acc (r.(j) - l.(j)))
        in
        loop 0 0
    in

    Printf.printf "%d\n" @@ (
        let work = Array.init n (fun i -> max (r.(i) - l.(p)) 0, max (r.(q) - l.(i)) 0) in
        Array.sort (fun (a0, b0) (a1, b1) -> compare (a0, -b0) (a1, -b1)) work;
        let s = Array.make (n + 1) 0 in
        let t = Array.make (n + 1) 0 in
        t.(0) <- r.(q) - l.(q);
        s.(n) <- r.(p) - l.(p);
        for i = 1 to n do
            t.(i) <- min t.(i - 1) (snd work.(i - 1));
        done;
        for i = n - 1 downto 0 do
            s.(i) <- min s.(i + 1) (fst work.(i));
        done;

        let rec loop i acc =
            if i = n + 1 then acc else loop (i + 1) (max acc (s.(i) + t.(i)))
        in
        loop 0 result1
    )
)

Score : 600 points

Problem Statement

10^9 contestants, numbered 1 to 10^9, will compete in a competition.
There will be two contests in this competition.

The organizer prepared N problems, numbered 1 to N, to use in these contests.
When Problem i is presented in a contest, it will be solved by all contestants from Contestant L_i to Contestant R_i (inclusive), and will not be solved by any other contestants.

The organizer will use these N problems in the two contests.
Each problem must be used in exactly one of the contests, and each contest must have at least one problem.

The joyfulness of each contest is the number of contestants who will solve all the problems in the contest.
Find the maximum possible total joyfulness of the two contests.

Constraints

2 \leq N \leq 10^5

1 \leq L_i \leq R_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N

Output

Print the maximum possible total joyfulness of the two contests.

Sample Input 1

4
4 7
1 4
5 8
2 5

Sample Output 1

6

The optimal choice is:

Use Problem 1 and 3 in the first contest. Contestant 5, 6, and 7 will solve both of them, so the joyfulness of this contest is 3.

Use Problem 2 and 4 in the second contest. Contestant 2, 3, and 4 will solve both of them, so the joyfulness of this contest is 3.

The total joyfulness of these two contests is 6. We cannot make the total joyfulness greater than 6.

Sample Input 2

4
1 20
2 19
3 18
4 17

Sample Output 2

34

Sample Input 3

10
457835016 996058008
456475528 529149798
455108441 512701454
455817105 523506955
457368248 814532746
455073228 459494089
456651538 774276744
457667152 974637457
457293701 800549465
456580262 636471526

Sample Output 3

540049931


Scanf.scanf "%d" (fun n ->
    let l = Array.make n 0 in
    let r = Array.make n 0 in
    for i = 0 to n - 1 do
        Scanf.scanf " %d %d" (fun a b -> l.(i) <- a; r.(i) <- b + 1)
    done;

    let rec loop i p q =
        if i = n then p, q else
            let p = if l.(i) > l.(p) then i else p in
            let q = if r.(i) < r.(q) then i else q in
            loop (i + 1) p q
    in
    let p, q = loop 0 0 0 in

    let result1 =
        let rec loop j acc =
            if j = n then acc + max (r.(q) - l.(p)) 0 else
            if j = p || j = q then loop (j + 1) acc else
                loop (j + 1) (max acc (r.(j) - l.(j)))
        in
        loop 0 0
    in

    let result2 = (
        let work = Array.init n (fun i -> max (r.(i) - l.(p)) 0, max (r.(q) - l.(i)) 0) in
        Array.sort (fun (a0, b0) (a1, b1) -> compare (-b0, a0) (-b1, a1)) work;
        let s = Array.make (n + 1) 0 in
        let t = Array.make (n + 1) 0 in
        t.(0) <- r.(q) - l.(q);
        s.(n) <- r.(p) - l.(p);
        for i = 1 to n do
            let _, b = work.(i - 1) in
            t.(i) <- min t.(i - 1) b;
        done;
        for i = n - 1 downto 0 do
            let a, _ = work.(i) in
            s.(i) <- min s.(i + 1) a;
        done;

        let rec loop i acc =
            if i = n + 1 then acc else loop (i + 1) (max acc (s.(i) + t.(i)))
        in
        loop 0 result1
    )
    in
    Printf.printf "%d\n" result1
)


let () =
  let rec check num a b =
    if b = 0 then false else if a * b = num then true
    else if a = 1 then check num 9 (b-1)
    else check num (a-1) b in
  Scanf.scanf "%d\n" @@ fun n ->
  Printf.printf "%s\n" (if (check n 9 9) then "Yes" else "No")

Score : 200 points

Problem Statement

Having learned the multiplication table, Takahashi can multiply two integers between 1 and 9 (inclusive) together.

Given an integer N, determine whether N can be represented as the product of two integers between 1 and 9. If it can, print Yes; if it cannot, print No.

Constraints

1 \leq N \leq 100

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N can be represented as the product of two integers between 1 and 9 (inclusive), print Yes; if it cannot, print No.

Sample Input 1

10

Sample Output 1

Yes

10 can be represented as, for example, 2 \times 5.

Sample Input 2

50

Sample Output 2

No

50 cannot be represented as the product of two integers between 1 and 9.

Sample Input 3

81

Sample Output 3

Yes


let scan_int () = Scanf.scanf " %d" (fun x -> x)
let print_int n = Printf.printf "%d\n" n

let rec solve i n ans =
  if i * i > n then ans
  else
    if n mod i = 0 then solve (i+1) n (min ans (i + (n/i) - 2))
    else solve (i+1) n ans

let init_val = int_of_float 1e12

let () =
  let n = scan_int() in
  print_int (solve 1 n init_val)

Score : 300 points

Problem Statement

Takahashi is standing on a multiplication table with infinitely many rows and columns.

The square (i,j) contains the integer i \times j. Initially, Takahashi is standing at (1,1).

In one move, he can move from (i,j) to either (i+1,j) or (i,j+1).

Given an integer N, find the minimum number of moves needed to reach a square that contains N.

Constraints

2 \leq N \leq 10^{12}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the minimum number of moves needed to reach a square that contains the integer N.

Sample Input 1

10

Sample Output 1

5

(2,5) can be reached in five moves. We cannot reach a square that contains 10 in less than five moves.

Sample Input 2

50

Sample Output 2

13

(5, 10) can be reached in 13 moves.

Sample Input 3

10000000019

Sample Output 3

10000000018

Both input and output may be enormous.


let a, b, x = Scanf.scanf " %f %f %f" @@ fun a b c -> a, b, c
let f r = 180. *. r /. acos (-1.) |> Printf.printf "%.10f\n"
let _ = f @@ if x <= b *. a *. a /. 2. then let d = 2. *. x /. (a *. b) in let k = sqrt (b *. b +. d *. d) in acos @@ 2. *. x /. (a *. k) /. b
  else atan2 (2. *. (a *. b -. x /. a)) (a *. a)

Score : 400 points

Problem Statement

Takahashi has a water bottle with the shape of a rectangular prism whose base is a square of side a~\mathrm{cm} and whose height is b~\mathrm{cm}. (The thickness of the bottle can be ignored.)

We will pour x~\mathrm{cm}^3 of water into the bottle, and gradually tilt the bottle around one of the sides of the base.

When will the water be spilled? More formally, find the maximum angle in which we can tilt the bottle without spilling any water.

Constraints

All values in input are integers.

1 \leq a \leq 100

1 \leq b \leq 100

1 \leq x \leq a^2b

Input

Input is given from Standard Input in the following format:

a b x

Output

Print the maximum angle in which we can tilt the bottle without spilling any water, in degrees.
Your output will be judged as correct when the absolute or relative error from the judge's output is at most 10^{-6}.

Sample Input 1

2 2 4

Sample Output 1

45.0000000000

This bottle has a cubic shape, and it is half-full. The water gets spilled when we tilt the bottle more than 45 degrees.

Sample Input 2

12 21 10

Sample Output 2

89.7834636934

This bottle is almost empty. When the water gets spilled, the bottle is nearly horizontal.

Sample Input 3

3 1 8

Sample Output 3

4.2363947991

This bottle is almost full. When the water gets spilled, the bottle is still nearly vertical.


let a, b = Scanf.sscanf (read_line ()) "%d %d" (fun a b -> (a, b))
let h = a - (b * 2)

let () = print_int (if h < 0 then 0 else h)

Score : 100 points

Problem Statement

The window of Takahashi's room has a width of A. There are two curtains hung over the window, each of which has a horizontal length of B. (Vertically, the curtains are long enough to cover the whole window.)

We will close the window so as to minimize the total horizontal length of the uncovered part of the window.
Find the total horizontal length of the uncovered parts of the window then.

Constraints

1 \leq A \leq 100

1 \leq B \leq 100

A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the total horizontal length of the uncovered parts of the window.

Sample Input 1

12 4

Sample Output 1

4

We have a window with a horizontal length of 12, and two curtains, each of length 4, that cover both ends of the window, for example. The uncovered part has a horizontal length of 4.

Sample Input 2

20 15

Sample Output 2

0

If the window is completely covered, print 0.

Sample Input 3

20 30

Sample Output 3

0

Each curtain may be longer than the window.


let _ = Scanf.sscanf (read_line ()) "%d %d" (fun a b ->
  match a - (b * 2) with
  | x when x <= 0 -> print_endline "0"
  | x -> print_endline (string_of_int x)
)


let n = Scanf.scanf " %d" (+) 0
let ds = Array.init n @@ fun _ -> Scanf.scanf " %d" (+) 0
let ans = ref 0
let _ = Array.iteri (fun i d -> for j = i + 1 to n - 1 do ans := !ans + d * ds.(j) done) ds; Printf.printf "%d\n" !ans

Score : 200 points

Problem Statement

It's now the season of TAKOYAKI FESTIVAL!

This year, N takoyaki (a ball-shaped food with a piece of octopus inside) will be served. The deliciousness of the i-th takoyaki is d_i.

As is commonly known, when you eat two takoyaki of deliciousness x and y together, you restore x \times y health points.

There are \frac{N \times (N - 1)}{2} ways to choose two from the N takoyaki served in the festival. For each of these choices, find the health points restored from eating the two takoyaki, then compute the sum of these \frac{N \times (N - 1)}{2} values.

Constraints

All values in input are integers.

2 \leq N \leq 50

0 \leq d_i \leq 100

Input

Input is given from Standard Input in the following format:

N
d_1 d_2 ... d_N

Output

Print the sum of the health points restored from eating two takoyaki over all possible choices of two takoyaki from the N takoyaki served.

Sample Input 1

3
3 1 2

Sample Output 1

11

There are three possible choices:

Eat the first and second takoyaki. You will restore 3 health points.

Eat the second and third takoyaki. You will restore 2 health points.

Eat the first and third takoyaki. You will restore 6 health points.

The sum of these values is 11.

Sample Input 2

7
5 0 7 8 3 3 2

Sample Output 2

312


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


let () = Scanf.scanf "%d\n" @@ fun n ->
  let ans = ref 1 in
  Scanf.scanf "%s" @@ fun s ->
  for i=1 to n-1 do
    if s.[i] <> s.[i-1] then ans := !ans + 1
  done;
  Printf.printf "%d\n" !ans

Score : 300 points

Problem Statement

There are N slimes lining up from left to right. The colors of these slimes will be given as a string S of length N consisting of lowercase English letters. The i-th slime from the left has the color that corresponds to the i-th character of S.

Adjacent slimes with the same color will fuse into one larger slime without changing the color. If there were a slime adjacent to this group of slimes before fusion, that slime is now adjacent to the new larger slime.

Ultimately, how many slimes will be there?

Constraints

1 \leq N \leq 10^5

|S| = N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the final number of slimes.

Sample Input 1

10
aabbbbaaca

Sample Output 1

5

Ultimately, these slimes will fuse into abaca.

Sample Input 2

5
aaaaa

Sample Output 2

1

All the slimes will fuse into one.

Sample Input 3

20
xxzaffeeeeddfkkkkllq

Sample Output 3

10


let () =
  let _ = Scanf.sscanf (input_line stdin) "%d" (fun a -> a) in
  let s = input_line stdin in
  let n = ref 0 in
  let prev_c = ref ' ' in
  let _ = String.iter (fun c -> if !prev_c  <> c then n := !n + 1; prev_c := c) s
  in
  print_int !n;
  print_newline ()


let () =
    let main () =
        let n = int_of_string (read_line ()) in
        let s = read_line () ^ "$" in
        let rec loop i prev acc =
            if i > n then acc else loop (i + 1) s.[i] (acc + if prev = s.[i] then 0 else 1)
        in
        Printf.printf "%d\n" (loop 1 s.[0] 0)
    in
    main ()


let split_string ?(pattern=" ") = Str.split @@ Str.regexp pattern
let n = Scanf.sscanf (read_line ()) "%d" @@ fun n -> n
let ls = read_line () |> split_string |> List.map int_of_string |> List.sort compare |> List.rev |> Array.of_list

let rec loop ai bi l r =
  if r <= l then begin
    let l = ref l in
    while !l < n - 1 && ls.(!l + 1) = ls.(!l) do l := !l + 1 done;
    max 0 @@ !l - bi - 1
  end
  else
    let mid = (l + r) / 2 in
    let a, b, c = ls.(ai), ls.(bi), ls.(mid) in
    if a < b + c && b < c + a && c < a + b then loop ai bi (mid + 1) r
    else loop ai bi l mid

let () =
  let ans = ref 0 in
  for i = 0 to n - 3 do
    for j = i + 1 to n - 2 do
      ans := !ans + loop i j (j + 1) n
    done
  done;
  Printf.printf "%d\n" !ans

Score : 400 points

Problem Statement

Takahashi has N sticks that are distinguishable from each other. The length of the i-th stick is L_i.

He is going to form a triangle using three of these sticks. Let a, b, and c be the lengths of the three sticks used. Here, all of the following conditions must be satisfied:

a < b + c

b < c + a

c < a + b

How many different triangles can be formed? Two triangles are considered different when there is a stick used in only one of them.

Constraints

All values in input are integers.

3 \leq N \leq 2 \times 10^3

1 \leq L_i \leq 10^3

Input

Input is given from Standard Input in the following format:

N
L_1 L_2 ... L_N

Constraints

Print the number of different triangles that can be formed.

Sample Input 1

4
3 4 2 1

Sample Output 1

1

Only one triangle can be formed: the triangle formed by the first, second, and third sticks.

Sample Input 2

3
1 1000 1

Sample Output 2

0

No triangles can be formed.

Sample Input 3

7
218 786 704 233 645 728 389

Sample Output 3

23


let () = Scanf.scanf "%d %d" @@ fun n k ->
  let array = Array.init n @@ fun _ -> Scanf.scanf " %d" Fun.id in
  let ans = Array.fold_left (fun a elt -> if elt >= k then a + 1 else a) 0 array in
  Printf.printf "%d\n" ans

Score : 200 points

Problem Statement

N friends of Takahashi has come to a theme park.

To ride the most popular roller coaster in the park, you must be at least K centimeters tall.

The i-th friend is h_i centimeters tall.

How many of the Takahashi's friends can ride the roller coaster?

Constraints

1 \le N \le 10^5

1 \le K \le 500

1 \le h_i \le 500

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
h_1 h_2 \ldots h_N

Output

Print the number of people among the Takahashi's friends who can ride the roller coaster.

Sample Input 1

4 150
150 140 100 200

Sample Output 1

2

Two of them can ride the roller coaster: the first and fourth friends.

Sample Input 2

1 500
499

Sample Output 2

0

Sample Input 3

5 1
100 200 300 400 500

Sample Output 3

5


let () =
  Scanf.scanf "%d\n" @@ fun n ->
  let a = Array.init n @@ fun i -> Scanf.scanf "%d " @@ fun d -> (i+1,d) in
  let _ = Array.fast_sort (fun (_,x) (_,y) -> compare x y) a in
  for i = 0 to n - 2 do
    print_int (fst a.(i)); print_string " ";
  done;
  print_int (fst a.(n-1)); print_newline ();

Score : 300 points

Problem Statement

Takahashi is a teacher responsible for a class of N students.

The students are given distinct student numbers from 1 to N.

Today, all the students entered the classroom at different times.

According to Takahashi's record, there were A_i students in the classroom when student number i entered the classroom (including student number i).

From these records, reconstruct the order in which the students entered the classroom.

Constraints

1 \le N \le 10^5

1 \le A_i \le N

A_i \neq A_j (i \neq j)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N

Output

Print the student numbers of the students in the order the students entered the classroom.

Sample Input 1

3
2 3 1

Sample Output 1

3 1 2

First, student number 3 entered the classroom.

Then, student number 1 entered the classroom.

Finally, student number 2 entered the classroom.

Sample Input 2

5
1 2 3 4 5

Sample Output 2

1 2 3 4 5

Sample Input 3

8
8 2 7 3 4 5 6 1

Sample Output 3

8 2 4 5 6 7 3 1


let cmp a b = if snd a = snd b then 0 else if snd a > snd b then 1 else -1

let () =
  let n = read_int () in
  let st = Array.init n (fun i -> Scanf.scanf " %d" (fun a -> (i + 1, a))) in
  Array.sort cmp st ;
  Array.iter (fun a -> Printf.printf "%d " (fst a)) st


Scanf.scanf "%d %d" (fun a b ->
    let rec gcd a b = if b = 0 then a else gcd b (a mod b) in

    let rec loop rest i acc =
        if rest = 1 then acc else
            if i * i > rest then acc + 1 else
                if rest mod i > 0 then loop rest (i + 1) acc else
                    let rec loop2 rest =
                        if rest mod i = 0 then loop2 (rest / i) else rest
                    in
                    loop (loop2 rest) (i + 1) (acc + 1)
    in
    loop (gcd a b) 2 1 |> Printf.printf "%d\n"
)

Score : 400 points

Problem Statement

Given are positive integers A and B.

Let us choose some number of positive common divisors of A and B.

Here, any two of the chosen divisors must be coprime.

At most, how many divisors can we choose?

Definition of common divisor

An integer d is said to be a common divisor of integers x and y when d divides both x and y.

Definition of being coprime

Integers x and y are said to be coprime when x and y have no positive common divisors other than 1.

Definition of dividing

An integer x is said to divide another integer y when there exists an integer \alpha such that y = \alpha x.

Constraints

All values in input are integers.

1 \leq A, B \leq 10^{12}

Input

Input is given from Standard Input in the following format:

A B

Output

Print the maximum number of divisors that can be chosen to satisfy the condition.

Sample Input 1

12 18

Sample Output 1

3

12 and 18 have the following positive common divisors: 1, 2, 3, and 6.

1 and 2 are coprime, 2 and 3 are coprime, and 3 and 1 are coprime, so we can choose 1, 2, and 3, which achieve the maximum result.

Sample Input 2

420 660

Sample Output 2

4

Sample Input 3

1 2019

Sample Output 3

1

1 and 2019 have no positive common divisors other than 1.


let filter n =
  match n with
  | 1 ->
      true
  | 2 ->
      true
  | n ->
      if n < 2 || n mod 2 = 0 then false
      else
        let rec f i =
          if float_of_int i > sqrt (float_of_int n) then true
          else if n mod i = 0 then false
          else f (i + 2)
        in
        f 3

let find_all_array f arr =
  let rec iter f i result =
    if i > Array.length arr - 1 then Array.of_list result
    else if f arr.(i) then iter f (i + 1) (arr.(i) :: result)
    else iter f (i + 1) result
  in
  iter f 0 []

let common_div sml lrg =
  let num = Array.init sml (fun i -> i + 1) and acc = ref [] in
  for i = 0 to sml - 1 do
    if sml mod num.(i) = 0 && lrg mod num.(i) = 0 then acc := num.(i) :: !acc
  done ;
  Array.of_list @@ List.rev !acc

let () =
  let a, b = Scanf.scanf "%d %d\n" (fun a b -> (a, b)) in
  let divs = common_div a b in
  print_int @@ Array.length @@ find_all_array filter divs


let factors : int -> (int * int) list = fun n ->
  let rec loop_i n i =
    if i * i > n then
      if n == 1 then [] else [(n, 1)]
    else if n mod i = 0 then
      let (n', c) = loop_j (n/i) i 1
      in
      (i, c) :: loop_i n' (i + 1)
    else
      loop_i n (i + 1)
  and loop_j n i j =
    if n mod i = 0 then
      loop_j (n / i) i (j + 1)
    else
      (n, j)
  in
  loop_i n 2

let rec print_int_list = function
    [] -> ()
  | (x :: xs) ->
     print_int x;
     print_char ' ';
     print_int_list xs

let rec merge = function
    ([], _) -> []
  | (_, []) -> []
  | ((x, cx)::xs, (y,cy)::ys) ->
     if x = y then
       x :: merge (xs, ys)
     else if x < y then
       merge (xs, (y,cy)::ys)
     else
       merge ((x,cx)::xs, ys)

let () =
  let (a, b) = Scanf.sscanf (input_line stdin) "%d %d" (fun a b -> (a, b)) in
  let facta = factors a in
  let factb = factors b in
  let ans_l  = 1 :: merge (facta, factb) in
  let ans = List.length ans_l
  in
  print_int ans;
  print_newline ()


Scanf.scanf "%d %d" (fun n m ->
    let lim = 1 lsl n in
    let sentinel = max_int / 2 in
    let dp = Array.make lim sentinel in
    dp.(0) <- 0;
    for i = 1 to m do
        Scanf.scanf " %d %d" (fun x y ->
            let rec loop i acc =
                if i = y then acc else
                    let c = Scanf.scanf " %d" (fun c -> c) in
                    loop (i + 1) (acc lor (1 lsl (c - 1)))
            in
            let c = loop 0 0 in
            for j = 0 to lim - 1 do
                dp.(j lor c) <- min dp.(j lor c) (dp.(j) + x)
            done
        )
    done;
    Printf.printf "%d\n" @@ if dp.(lim - 1) = sentinel then -1 else dp.(lim - 1)
)

Score : 500 points

Problem Statement

We have N locked treasure boxes, numbered 1 to N.

A shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan), and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each key purchased can be used any number of times.

Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.

Constraints

All values in input are integers.

1 \leq N \leq 12

1 \leq M \leq 10^3

1 \leq a_i \leq 10^5

1 \leq b_i \leq N

1 \leq c_{i1} < c_{i2} < ... < c_{i{b_i}} \leq N

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
c_{11} c_{12} ... c_{1{b_1}}
:
a_M b_M
c_{M1} c_{M2} ... c_{M{b_M}}

Output

Print the minimum cost required to unlock all the treasure boxes.
If it is impossible to unlock all of them, print -1.

Sample Input 1

2 3
10 1
1
15 1
2
30 2
1 2

Sample Output 1

25

We can unlock all the boxes by purchasing the first and second keys, at the cost of 25 yen, which is the minimum cost required.

Sample Input 2

12 1
100000 1
2

Sample Output 2

-1

We cannot unlock all the boxes.

Sample Input 3

4 6
67786 3
1 3 4
3497 1
2
44908 3
2 3 4
2156 3
2 3 4
26230 1
2
86918 1
3

Sample Output 3

69942


let () = Scanf.scanf "%d %d\n" @@ fun n m ->
  let acss = Array.init m @@ fun _ ->
    Scanf.scanf "%d %d\n" @@ fun a b ->
      a,
      Array.fold_left ( lor ) 0 @@
      Array.init b @@ fun _ -> Scanf.scanf "%d " @@ fun c -> 1 lsl (c - 1) in
  let dp = Array.make (1 lsl n) 1145141919 in
  dp.(0) <- 0;
  for i = 0 to (1 lsl n) - 1 do
    Array.iter (fun (a, cs) ->
      dp.(i lor cs) <- min dp.(i lor cs) (a + dp.(i))) acss
  done;
  Printf.printf "%d\n" @@
  if dp.((1 lsl n) - 1) = 1145141919
  then -1
  else dp.((1 lsl n) - 1)


let rec drop n xs =
  if n = 0
  then xs
  else drop (n - 1) (List.tl xs)

exception Exit of int list

let () = Scanf.scanf "%d %d\n" @@ fun n m ->
  let es = Array.make n [] in
  let visited = Array.make n false in
  let trace = Array.make n None in
  for i = 0 to m - 1 do
    Scanf.scanf "%d %d\n" @@ fun a b ->
      es.(a - 1) <- b - 1 :: es.(a - 1)
  done;
  let rec visit vs v =
    match trace.(v) with
    | Some us -> raise (Exit (drop (List.length us) (List.rev vs)))
    | None ->
        if not visited.(v) then begin
          visited.(v) <- true;
          trace.(v) <- Some vs;
          List.iter (fun u -> visit (v :: vs) u) es.(v);
          trace.(v) <- None
        end in
  try
    for v = 0 to n - 1 do
      visit [] v
    done;
    print_endline "-1"
  with Exit vs ->
    Printf.printf "%d\n" (List.length vs);
    List.iter (fun v -> Printf.printf "%d\n" @@ v + 1) vs

Score : 600 points

Problem Statement

Given is a directed graph G with N vertices and M edges.

The vertices are numbered 1 to N, and the i-th edge is directed from Vertex A_i to Vertex B_i.

It is guaranteed that the graph contains no self-loops or multiple edges.

Determine whether there exists an induced subgraph (see Notes) of G such that the in-degree and out-degree of every vertex are both 1. If the answer is yes, show one such subgraph.

Here the null graph is not considered as a subgraph.

Notes

For a directed graph G = (V, E), we call a directed graph G' = (V', E') satisfying the following conditions an induced subgraph of G:

V' is a (non-empty) subset of V.

E' is the set of all the edges in E that have both endpoints in V'.

Constraints

1 \leq N \leq 1000

0 \leq M \leq 2000

1 \leq A_i,B_i \leq N

A_i \neq B_i

All pairs (A_i, B_i) are distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
:
A_M B_M

Output

If there is no induced subgraph of G that satisfies the condition, print -1.
Otherwise, print an induced subgraph of G that satisfies the condition, in the following format:

K
v_1
v_2
:
v_K

This represents the induced subgraph of G with K vertices whose vertex set is \{v_1, v_2, \ldots, v_K\}. (The order of v_1, v_2, \ldots, v_K does not matter.)
If there are multiple subgraphs of G that satisfy the condition, printing any of them is accepted.

Sample Input 1

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

Sample Output 1

3
1
2
4

The induced subgraph of G whose vertex set is \{1, 2, 4\} has the edge set \{(1, 2), (2, 4), (4, 1)\}. The in-degree and out-degree of every vertex in this graph are both 1.

Sample Input 2

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

Sample Output 2

-1

There is no induced subgraph of G that satisfies the condition.

Sample Input 3

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

Sample Output 3

4
2
3
4
5


let (h, w, a, b) = Scanf.sscanf (read_line ()) "%d %d %d %d" @@ fun h w a b -> (h, w, a, b)

let () =
  Array.iter (fun arr ->
    Array.iter print_int arr;
    print_newline ()
  ) @@ Array.init h @@ fun i -> Array.init w @@ fun j ->
    if i < h - a then
      if j < w - b then 1 else 0
    else
      if j < w - b then 0 else 1

Score : 300 points

Problem Statement

We have a square grid with H rows and W columns.
Snuke wants to write 0 or 1 in each of the squares.
Here, all of the following conditions have to be satisfied:

For every row, the smaller of the following is A: the number of 0s contained in the row, and the number of 1s contained in the row. (If these two numbers are equal, “the smaller” should be read as “either”.)

For every column, the smaller of the following is B: the number of 0s contained in the column, and the number of 1s contained in the column.

Determine if these conditions can be satisfied by writing 0 or 1 in each of the squares. If the answer is yes, show one way to fill the squares so that the conditions are satisfied.

Constraints

1 \leq H,W \leq 1000

0 \leq A

2 \times A \leq W

0 \leq B

2 \times B \leq H

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W A B

Output

If the conditions cannot be satisfied by writing 0 or 1 in each of the squares, print -1.

If the conditions can be satisfied, print one way to fill the squares so that the conditions are satisfied, in the following format:

s_{11}s_{12}\cdots s_{1W}
s_{21}s_{22}\cdots s_{2W}
\vdots
s_{H1}s_{H2}\cdots s_{HW}

Here s_{ij} is the digit written in the square at the i-th row from the top and the j-th column from the left in the grid.

If multiple solutions exist, printing any of them will be accepted.

Sample Input 1

3 3 1 1

Sample Output 1

100
010
001

Every row contains two 0s and one 1, so the first condition is satisfied.
Also, every column contains two 0s and one 1, so the second condition is satisfied.

Sample Input 2

1 5 2 0

Sample Output 2

01010


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

let add a s =
  IntMap.add a (1 + try IntMap.find a s with Not_found -> 0) s

let remove a s =
  match IntMap.find a s with
  | exception Not_found -> s
  | 1 -> IntMap.remove a s
  | n -> IntMap.add a (n - 1) s

module SegTree = struct
  module type SemiGroup = sig
    type t
    val op : t -> t -> t
  end

  module Make (S : SemiGroup) : sig
    type t
    type elt

    (* 与えられたリストの要素からなるセグ木を作る *)
    val of_list : elt list -> t
    (* f 0, ... f (n - 1)のn要素からなるセグ木を作る *)
    val init : int -> (int -> elt) -> t
    (*
     * update i f t
     * i番目の要素x_iをf x_iに変更したセグ木を作る
     *)
    val update : int -> (elt -> elt) -> t -> t
    (*
     * query l r t
     * 添字が[l, r)の要素を半群の演算子で畳み込んだ値を求める
     *)
    val query : int -> int -> t -> elt
  end with type elt = S.t = struct
    type elt = S.t

    type body =
      | Leaf of elt
      | Node of elt * body * body
    type t = { size : int; body : body }

    (* セグ木の要素をどのように左右に分配するか
       正整数nについて lsize n + rsize n = n が成り立たなくてはならない *)
    let lsize n = n lsr 1
    let rsize n = (n + 1) lsr 1

    (* セグ木の保持する要素を半群の演算子で畳み込んだもの *)
    let data = function
      | Leaf x
      | Node (x, _, _) -> x

    let mknode l r = Node (S.op (data l) (data r), l, r)

    let rec of_list l = function
      | 1 -> Leaf (List.hd l), List.tl l
      | n ->
          let t1, l = of_list l (lsize n) in
          let t2, l = of_list l (rsize n) in
          mknode t1 t2, l
    let of_list l =
      let n = List.length l in
      assert (0 < n);
      { size = n; body = fst (of_list l n) }

    let rec init i f = function
      | 1 -> Leaf (f i)
      | n -> mknode (init i f (lsize n)) (init (i + lsize n) f (rsize n))
    let init n f =
      assert (1 <= n);
      { size = n; body = init 0 f n }

    let rec update n i f = function
      | Leaf x -> Leaf (f x)
      | Node (_, l, r) ->
          if i < lsize n
          then mknode (update (lsize n) i f l) r
          else mknode l (update (rsize n) (i - lsize n) f r)
    let update i f t =
      assert (0 <= i && i < t.size);
      { t with body = update t.size i f t.body }

    let rec query n l r = function
      | Leaf x -> x
      | Node (x, left, right) ->
          if l = 0 && r = n then x
          else if r <= lsize n then query (lsize n) l r left
          else if lsize n <= l then query (rsize n) (l - lsize n) (r - lsize n) right
          else S.op (query (lsize n) l (lsize n) left) (query (rsize n) 0 (r - lsize n) right)
    let query l r t =
      assert (0 <= l && l < r && r <= t.size);
      query t.size l r t.body
  end
end

module ConjSegTree = SegTree.Make (struct
  type t = bool
  let op = ( && )
end)

let () = Scanf.scanf "%d %d\n" @@ fun n k ->
  let ps = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun p -> p in
  let st =
    ConjSegTree.of_list @@
    Array.to_list @@
    Array.init (n - 1) @@ fun i -> ps.(i) <= ps.(i + 1) in
  let (ex, _, acc) =
    Array.fold_left (fun (ex, s, acc) (p, p', b) ->
      ex || b,
      add p' (remove p s),
      acc +
      if b || fst (IntMap.min_binding s) = p && fst (IntMap.max_binding s) <= p'
      then 0
      else 1)
    (false, Array.fold_right add (Array.sub ps 0 k) IntMap.empty, 1) @@
    Array.init (n - k) @@ fun i ->
      ps.(i), ps.(i + k), ConjSegTree.query (i + 1) (i + k) st in
  Printf.printf "%d\n" @@
  acc + if ex && not (ConjSegTree.query 0 (k - 1) st) then 1 else 0

Score : 700 points

Problem Statement

Snuke has a permutation (P_0,P_1,\cdots,P_{N-1}) of (0,1,\cdots,N-1).

Now, he will perform the following operation exactly once:

Choose K consecutive elements in P and sort them in ascending order.

Find the number of permutations that can be produced as P after the operation.

Constraints

2 \leq N \leq 200000

2 \leq K \leq N

0 \leq P_i \leq N-1

P_0,P_1,\cdots,P_{N-1} are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
P_0 P_1 \cdots P_{N-1}

Output

Print the number of permutations that can be produced as P after the operation.

Sample Input 1

5 3
0 2 1 4 3

Sample Output 1

2

Two permutations can be produced as P after the operation: (0,1,2,4,3) and (0,2,1,3,4).

Sample Input 2

4 4
0 1 2 3

Sample Output 2

1

Sample Input 3

10 4
2 0 1 3 7 5 4 6 8 9

Sample Output 3

6


let weather s =
  if s = "Sunny" then "Cloudy"
  else if s = "Cloudy" then "Rainy"
  else "Sunny"

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

Score: 100 points

Problem Statement

The weather in Takahashi's town changes day by day, in the following cycle: Sunny, Cloudy, Rainy, Sunny, Cloudy, Rainy, ...

Given is a string S representing the weather in the town today. Predict the weather tomorrow.

Constraints

S is Sunny, Cloudy, or Rainy.

Input

Input is given from Standard Input in the following format:

S

Output

Print a string representing the expected weather tomorrow, in the same format in which input is given.

Sample Input 1

Sunny

Sample Output 1

Cloudy

In Takahashi's town, a sunny day is followed by a cloudy day.

Sample Input 2

Rainy

Sample Output 2

Sunny


(*
ocamlfind ocamlopt -package batteries -linkpkg main.ml -o a.out
*)
open Batteries

let n, k, q = Scanf.sscanf (read_line ()) "%d %d %d" ( fun n k q -> n, k, q)
let lst = List.init q ( fun _ -> Scanf.sscanf (read_line ()) "%d" ( fun a -> a))

let score_lst = List.init n (
        fun i ->
                (i + 1, k)
)

let rec f lst score_lst =
        match lst with
                  [] -> score_lst
                | first :: rest ->
                        let nscore_lst = List.map (fun (i, score) ->
                                if i = first then
                                        (i, score)
                                else
                                        (i, score - 1)
                        ) score_lst
                        in
                                f rest nscore_lst

let () =
        List.iter (fun (_, score) ->
                if score > 0 then
                        Printf.printf "Yes\n"
                else
                        Printf.printf "No\n"
        ) (f lst score_lst)

Score: 300 points

Problem Statement

Takahashi has decided to hold fastest-finger-fast quiz games. Kizahashi, who is in charge of making the scoreboard, is struggling to write the program that manages the players' scores in a game, which proceeds as follows.

A game is played by N players, numbered 1 to N. At the beginning of a game, each player has K points.

When a player correctly answers a question, each of the other N-1 players receives minus one (-1) point. There is no other factor that affects the players' scores.

At the end of a game, the players with 0 points or lower are eliminated, and the remaining players survive.

In the last game, the players gave a total of Q correct answers, the i-th of which was given by Player A_i.
For Kizahashi, write a program that determines whether each of the N players survived this game.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq K \leq 10^9

1 \leq Q \leq 10^5

1 \leq A_i \leq N\ (1 \leq i \leq Q)

Input

Input is given from Standard Input in the following format:

N K Q
A_1
A_2
.
.
.
A_Q

Output

Print N lines. The i-th line should contain Yes if Player i survived the game, and No otherwise.

Sample Input 1

6 3 4
3
1
3
2

Sample Output 1

No
No
Yes
No
No
No

In the beginning, the players' scores are (3, 3, 3, 3, 3, 3).

Player 3 correctly answers a question. The players' scores are now (2, 2, 3, 2, 2, 2).

Player 1 correctly answers a question. The players' scores are now (2, 1, 2, 1, 1, 1).

Player 3 correctly answers a question. The players' scores are now (1, 0, 2, 0, 0, 0).

Player 2 correctly answers a question. The players' scores are now (0, 0, 1, -1, -1, -1).

Players 1, 2, 4, 5 and 6, who have 0 points or lower, are eliminated, and Player 3 survives this game.

Sample Input 2

6 5 4
3
1
3
2

Sample Output 2

Yes
Yes
Yes
Yes
Yes
Yes

Sample Input 3

10 13 15
3
1
4
1
5
9
2
6
5
3
5
8
9
7
9

Sample Output 3

No
No
No
No
Yes
No
No
No
Yes
No


let () =
    let main () =
        let n, k, q = Scanf.sscanf (read_line ()) "%d %d %d" (fun a b c -> a, b, c) in
        let p = Array.make n (k - q) in
        for i = 1 to q do
            let a = int_of_string (read_line ()) - 1 in
            p.(a) <- p.(a) + 1
        done;
        Array.iter (fun v -> print_endline (if v > 0 then "Yes" else "No")) p
    in
    main ()


let n = read_int ()
let () = print_int (n*n*n)

Score : 100 points

Problem Statement

Takahashi is going to set a 3-character password.

How many possible passwords are there if each of its characters must be a digit between 1 and N (inclusive)?

Constraints

1 \leq N \leq 9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of possible passwords.

Sample Input 1

2

Sample Output 1

8

There are eight possible passwords: 111, 112, 121, 122, 211, 212, 221, and 222.

Sample Input 2

1

Sample Output 2

1

There is only one possible password if you can only use one kind of character.


open Printf
open Scanf

let solve n = n * n * n

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


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


let f () = Scanf.scanf " %d" (+) 0
let n = f ()
let a_s = Array.init n @@ fun _ -> f ()
let bs = Array.init n @@ fun _ -> f ()
let cs = Array.init (n - 1) @@ fun _ -> f ()
let ans = ref @@ Array.fold_left (+) 0 bs
let _ = for i = 0 to n - 2 do if a_s.(i + 1) = a_s.(i) + 1 then ans := !ans + cs.(a_s.(i) - 1) done; Printf.printf "%d\n" !ans

Score : 200 points

Problem Statement

Takahashi went to an all-you-can-eat buffet with N kinds of dishes and ate all of them (Dish 1, Dish 2, \ldots, Dish N) once.

The i-th dish (1 \leq i \leq N) he ate was Dish A_i.

When he eats Dish i (1 \leq i \leq N), he gains B_i satisfaction points.

Additionally, when he eats Dish i+1 just after eating Dish i (1 \leq i \leq N - 1), he gains C_i more satisfaction points.

Find the sum of the satisfaction points he gained.

Constraints

All values in input are integers.

2 \leq N \leq 20

1 \leq A_i \leq N

A_1, A_2, ..., A_N are all different.

1 \leq B_i \leq 50

1 \leq C_i \leq 50

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
C_1 C_2 ... C_{N-1}

Output

Print the sum of the satisfaction points Takahashi gained, as an integer.

Sample Input 1

3
3 1 2
2 5 4
3 6

Sample Output 1

14

Takahashi gained 14 satisfaction points in total, as follows:

First, he ate Dish 3 and gained 4 satisfaction points.

Next, he ate Dish 1 and gained 2 satisfaction points.

Lastly, he ate Dish 2 and gained 5 + 3 = 8 satisfaction points.

Sample Input 2

4
2 3 4 1
13 5 8 24
45 9 15

Sample Output 2

74

Sample Input 3

2
1 2
50 50
50

Sample Output 3

150


let () = Scanf.scanf "%d\n" @@ fun n ->
  let bb = Array.init (n - 1) @@ fun _ -> Scanf.scanf " %d" Fun.id in
  let ans = ref bb.(0) in
  for i = 1 to n - 2 do
    ans := !ans + min bb.(i) bb.(i - 1)
  done;
  Printf.printf "%d\n" !ans

Score : 300 points

Problem Statement

There is an integer sequence A of length N whose values are unknown.

Given is an integer sequence B of length N-1 which is known to satisfy the following:

B_i \geq \max(A_i, A_{i+1})

Find the maximum possible sum of the elements of A.

Constraints

All values in input are integers.

2 \leq N \leq 100

0 \leq B_i \leq 10^5

Input

Input is given from Standard Input in the following format:

N
B_1 B_2 ... B_{N-1}

Output

Print the maximum possible sum of the elements of A.

Sample Input 1

3
2 5

Sample Output 1

9

A can be, for example, ( 2 , 1 , 5 ), ( -1 , -2 , -3 ), or ( 2 , 2 , 5 ). Among those candidates, A = ( 2 , 2 , 5 ) has the maximum possible sum.

Sample Input 2

2
3

Sample Output 2

6

Sample Input 3

6
0 153 10 10 23

Sample Output 3

53


let h = ref 0
let n, k, t = Scanf.scanf "%d %d %s" @@ fun n k s -> String.iteri (fun i c -> if i > 0 && s.[i - 1] = s.[i] then incr h) s; n, k, min k @@ (n - 1 - !h) / 2
let _ = Printf.printf "%d\n" @@ if k > t then n - 1 else !h + 2 * t

Score : 400 points

Problem Statement

There are N people standing in a queue from west to east.

Given is a string S of length N representing the directions of the people.
The i-th person from the west is facing west if the i-th character of S is L, and east if that character of S is R.

A person is happy if the person in front of him/her is facing the same direction.
If no person is standing in front of a person, however, he/she is not happy.

You can perform the following operation any number of times between 0 and K (inclusive):

Operation: Choose integers l and r such that 1 \leq l \leq r \leq N, and rotate by 180 degrees the part of the queue: the l-th, (l+1)-th, ..., r-th persons. That is, for each i = 0, 1, ..., r-l, the (l + i)-th person from the west will stand the (r - i)-th from the west after the operation, facing east if he/she is facing west now, and vice versa.

What is the maximum possible number of happy people you can have?

Constraints

N is an integer satisfying 1 \leq N \leq 10^5.

K is an integer satisfying 1 \leq K \leq 10^5.

|S| = N

Each character of S is L or R.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of happy people after at most K operations.

Sample Input 1

6 1
LRLRRL

Sample Output 1

3

If we choose (l, r) = (2, 5), we have LLLRLL, where the 2-nd, 3-rd, and 6-th persons from the west are happy.

Sample Input 2

13 3
LRRLRLRRLRLLR

Sample Output 2

9

Sample Input 3

10 1
LLLLLRRRRR

Sample Output 3

9

Sample Input 4

9 2
RRRLRLRLL

Sample Output 4

7


let () = Scanf.scanf "%d %d\n%s" @@ fun n k s ->
  let ss = Array.make n 1 in
  let ls = Array.make n 0 in
  for i = 1 to n - 1 do
    if s.[i - 1] = s.[i]
    then begin
      ss.(i) <- ss.(i - 1);
      ls.(ss.(i)) <- 1 + ls.(ss.(i))
    end else begin
      ss.(i) <- ss.(i - 1) + 1
    end
  done;
  Printf.printf "%d\n" @@
  Array.fold_left ( + ) 0 ls + min (ss.(n - 1) - 1) (2 * k)


open Printf
open Scanf

let solve s1 s2 =
  let f ct i = if String.get s1 i = String.get s2 i then ct + 1 else ct in
  List.fold_left f 0 [0; 1; 2]

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

Score : 100 points

Problem Statement

You will be given a string S of length 3 representing the weather forecast for three days in the past.

The i-th character (1 \leq i \leq 3) of S represents the forecast for the i-th day. S, C, and R stand for sunny, cloudy, and rainy, respectively.

You will also be given a string T of length 3 representing the actual weather on those three days.

The i-th character (1 \leq i \leq 3) of S represents the actual weather on the i-th day. S, C, and R stand for sunny, cloudy, and rainy, respectively.

Print the number of days for which the forecast was correct.

Constraints

S and T are strings of length 3 each.

S and T consist of S, C, and R.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the number of days for which the forecast was correct.

Sample Input 1

CSS
CSR

Sample Output 1

2

For the first day, it was forecast to be cloudy, and it was indeed cloudy.

For the second day, it was forecast to be sunny, and it was indeed sunny.

For the third day, it was forecast to be sunny, but it was rainy.

Thus, the forecast was correct for two days in this case.

Sample Input 2

SSR
SSR

Sample Output 2

3

Sample Input 3

RRR
SSS

Sample Output 3

0


let _ = Scanf.sscanf (read_line()) "%d %d" (fun a b ->
   let rec loop i ans =
    if ans >= b then i else
    loop (i+1) (ans - 1 + a)
  in loop 0 1
) |> Printf.printf "%d\n"

Score : 200 points

Problem Statement

Takahashi's house has only one socket.

Takahashi wants to extend it with some number of power strips, each with A sockets, into B or more empty sockets.

One power strip with A sockets can extend one empty socket into A empty sockets.

Find the minimum number of power strips required.

Constraints

All values in input are integers.

2 \leq A \leq 20

1 \leq B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

Print the minimum number of power strips required.

Sample Input 1

4 10

Sample Output 1

3

3 power strips, each with 4 sockets, extend the socket into 10 empty sockets.

Sample Input 2

8 9

Sample Output 2

2

2 power strips, each with 8 sockets, extend the socket into 15 empty sockets.

Sample Input 3

8 8

Sample Output 3

1


(* Vicfred
 * https://atcoder.jp/contests/abc139/tasks/abc139_c
 * implementation
 * *)
let n = Scanf.scanf "%d\n" ( + ) 0
let hs = Array.init n @@ fun _ -> Scanf.scanf " %d" ( + ) 0
let maxima = ref 0
let main =
    Array.fold_left (fun (m, p) h -> if h <= p then incr maxima else maxima := 0;
    max m !maxima, h) (0, 0) hs |> fst |> Printf.printf "%d\n"

Score : 300 points

Problem Statement

There are N squares arranged in a row from left to right.

The height of the i-th square from the left is H_i.

You will land on a square of your choice, then repeat moving to the adjacent square on the right as long as the height of the next square is not greater than that of the current square.

Find the maximum number of times you can move.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq H_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
H_1 H_2 ... H_N

Output

Print the maximum number of times you can move.

Sample Input 1

5
10 4 8 7 3

Sample Output 1

2

By landing on the third square from the left, you can move to the right twice.

Sample Input 2

7
4 4 5 6 6 5 5

Sample Output 2

3

By landing on the fourth square from the left, you can move to the right three times.

Sample Input 3

4
1 2 3 4

Sample Output 3

0


let () = Scanf.scanf "%d\n" @@ fun n ->
  let ass =
    Array.init n @@ fun _ ->
      Array.init (n - 1) @@ fun _ ->
        Scanf.scanf "%d " @@ fun a -> a - 1 in
  let es = Array.make_matrix n n [] in
  for i = 0 to n - 1 do
    for j = 0 to n - 3 do
      es.(min i ass.(i).(j)).(max i ass.(i).(j)) <-
        (min i ass.(i).(j + 1), max i ass.(i).(j + 1)) ::
        es.(min i ass.(i).(j)).(max i ass.(i).(j))
    done
  done;
  let dp = Array.make_matrix n n (-1) in
  let vs = Array.make_matrix n n false in
  let rec visit acc (i, j) =
    if vs.(i).(j)
    then raise Not_found
    else begin
      vs.(i).(j) <- true;
      let ans = List.fold_left visit ((i, j) :: acc) es.(i).(j) in
      vs.(i).(j) <- false;
      ans
    end in
  try
    let sort =
      Array.fold_left visit [] @@
      Array.init n @@ fun i ->
        (min i ass.(i).(0), max i ass.(i).(0)) in
    let rec update prev (i, j) =
      if dp.(i).(j) <= prev
      then begin
        dp.(i).(j) <- prev + 1;
        List.iter (update dp.(i).(j)) es.(i).(j)
      end in
    List.iter (update 0) sort;
    Printf.printf "%d\n" @@
    Array.fold_left (Array.fold_left max) min_int dp
  with Not_found -> print_endline "-1"

Score : 500 points

Problem Statement

N players will participate in a tennis tournament. We will call them Player 1, Player 2, \ldots, Player N.

The tournament is round-robin format, and there will be N(N-1)/2 matches in total.
Is it possible to schedule these matches so that all of the following conditions are satisfied? If the answer is yes, also find the minimum number of days required.

Each player plays at most one matches in a day.

Each player i (1 \leq i \leq N) plays one match against Player A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1} in this order.

Constraints

3 \leq N \leq 1000

1 \leq A_{i, j} \leq N

A_{i, j} \neq i

A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1} are all different.

Input

Input is given from Standard Input in the following format:

N
A_{1, 1} A_{1, 2} \ldots A_{1, N-1}
A_{2, 1} A_{2, 2} \ldots A_{2, N-1}
:
A_{N, 1} A_{N, 2} \ldots A_{N, N-1}

Output

If it is possible to schedule all the matches so that all of the conditions are satisfied, print the minimum number of days required; if it is impossible, print -1.

Sample Input 1

3
2 3
1 3
1 2

Sample Output 1

3

All the conditions can be satisfied if the matches are scheduled for three days as follows:

Day 1: Player 1 vs Player 2

Day 2: Player 1 vs Player 3

Day 3: Player 2 vs Player 3

This is the minimum number of days required.

Sample Input 2

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

Sample Output 2

4

All the conditions can be satisfied if the matches are scheduled for four days as follows:

Day 1: Player 1 vs Player 2, Player 3 vs Player 4

Day 2: Player 1 vs Player 3

Day 3: Player 1 vs Player 4, Player 2 vs Player 3

Day 4: Player 2 vs Player 4

This is the minimum number of days required.

Sample Input 3

3
2 3
3 1
1 2

Sample Output 3

-1

Any scheduling of the matches violates some condition.


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

let () =
    let split s =
        let rec loop i cur flag acc =
            if i = String.length s then ( if flag then cur :: acc else acc) else (
                match flag, s.[i] with
                | true,  ' ' -> loop (i + 1) "" false (cur :: acc)
                | false, ' ' -> loop (i + 1) "" false acc
                | true,  v   -> loop (i + 1) (Printf.sprintf "%s%c" cur v) true acc
                | false, v   -> loop (i + 1) (Printf.sprintf "%s%c" cur v) true acc
            )
        in
        List.map int_of_string (List.rev (loop 0 "" false []))
    in
    let main () =
        let n = int_of_string (read_line ()) in
        let a = Array.init n (fun _ -> Array.of_list (split (read_line ()))) in
        let add key v l map =
            l.(v) <- l.(v) + 1;
            M.add key (v :: try M.find key map with _ -> []) map
        in
        let canonical (a, b) = if a < b then a * 1024 + b else b * 1024 + a in
        let l = Array.make (1024 * 1024) 0 in
        let rec loop_i i map =
            let rec loop_j j map =
                if j = n - 1 then map else
                    let v = a.(i).(j) in
                    let k = canonical (i + 1, v) in
                    let map =
                        if j = 0 then (
                            if v > i + 1 then add 0 k l map else map
                        ) else (
                            let vv = a.(i).(j - 1) in
                            add (canonical (i + 1, vv)) k l map
                        )
                    in
                    loop_j (j + 1) map
            in
            if i = n then map else loop_i (i + 1) (loop_j 0 map)
        in
        let map = loop_i 0 (M.singleton 0 []) in
        let rec emit day rest check map next arr = function
            | [] -> if rest = 0 then day else
                    if next <> [] then emit (day + 1) rest check map [] (Array.make (n + 1) false) next
                    else (-1)
            | cd :: tl ->
                    let c = cd lsr 10 in
                    let d = cd land 1023 in
                    if arr.(c) || arr.(d) then emit day rest check map (cd :: next) arr tl else
                        (*
                        let () = Printf.printf "day %d: can emit %d,%d\n" day c d in
        *)
                        let () = arr.(c) <- true in
                        let () = arr.(d) <- true in
                        let next = List.fold_left (fun acc cd ->
                            check.(cd) <- check.(cd) - 1;
                            if check.(cd) = 0 then cd :: acc else acc
                        ) next (try M.find cd map with _ -> [])
                        in
                        emit day (rest - 1) check map next arr tl
        in
        let init = List.fold_left (fun acc cd ->
            l.(cd) <- l.(cd) - 1;
            if l.(cd) = 0 then cd :: acc else acc) [] (M.find 0 map)
        in
        (*
        let () = List.iter (fun (c, d) -> Printf.printf "(%d, %d) " c d) init in
        let () = print_newline () in
    *)
        let r = emit 1 (n * (n - 1) / 2) l map [] (Array.make (n + 1) false) init in
        Printf.printf "%d\n" r
    in
    main ()


let () = Scanf.scanf "%d\n" @@ fun n ->
  let ass =
    Array.init n @@ fun _ ->
      Array.init (n - 1) @@ fun _ ->
        Scanf.scanf "%d " @@ fun a -> a - 1 in
  let es = Array.make_matrix n n [] in
  for i = 0 to n - 1 do
    for j = 0 to n - 3 do
      es.(i).(ass.(i).(j)) <-
        (i, ass.(i).(j + 1)) ::
        (ass.(i).(j + 1), i) ::
        es.(i).(ass.(i).(j));
      es.(ass.(i).(j)).(i) <-
        (i, ass.(i).(j + 1)) ::
        (ass.(i).(j + 1), i) :: es.(ass.(i).(j)).(i);
    done
  done;
  let dp = Array.make_matrix n n (-1) in
  let vs = Array.make_matrix n n false in
  let rec visit prev mark i j =
    if vs.(i).(j)
    then raise Not_found
    else if dp.(i).(j) <= prev
    then begin
      vs.(i).(j) <- true;
      dp.(i).(j) <- 1 + prev;
      List.iter (fun (k, l) -> visit dp.(i).(j) mark k l) es.(i).(j);
      vs.(i).(j) <- false
    end in
  try
    for i = 0 to n - 1 do
      visit 0 i i ass.(i).(0)
    done;
    Printf.printf "%d\n" @@
    Array.fold_left (Array.fold_left max) min_int dp
  with Not_found -> print_endline "-1"


open Printf
open Scanf

let read_intlist: unit -> int list = fun () ->
  let sl = Str.split (Str.regexp " ") @@ read_line () in
  List.map (fun s -> sscanf s "%d" (fun x -> x)) sl

let n = sscanf (read_line ()) "%d" (fun x -> x)
let a: int array array = Array.make (n+1) [|0|]
let () = for y = 1 to n do
           a.(y) <- 0 :: (read_intlist ()) @ [0] |> Array.of_list
         done

(*  *)
let ans = ref 0
let inds = Array.make (n+1) 1

let () =
  let bflag = ref true in
  while !bflag do
    let dp = Array.make (n+1) false in
    let flag = ref true in

    for i = 1 to n do
      let x = inds.(i) in
      if not dp.(i) && x < n then begin
        let target = a.(i).(x) in
        if not dp.(target) && a.(target).(inds.(target)) = i then begin
            dp.(i) <- true;
            dp.(target) <- true;

            inds.(i) <- inds.(i) + 1;
            inds.(target) <- inds.(target) + 1;

            if !flag then
              flag := false
          end
        end;
    done;

    if !flag then
      let hoge = List.tl (Array.to_list inds)
                 |> List.for_all (fun e -> e = n) in
      if hoge then
        bflag := false
      else begin
          ans := -1;
          bflag := false
        end
    else
      incr ans
  done

let () = print_endline @@ string_of_int !ans


open Scanf;;
open Printf;;

let rint () = scanf " %d" (fun x -> x);;
let rpoint () = scanf " %f %f" (fun x y -> x, y);;

let () =
  let n = rint () in
  let ps = Array.init n (fun _ -> rpoint ()) in
  Array.sort (fun (a, b) (c, d) -> compare (atan2 b a) (atan2 d c)) ps;
  let ans = ref 0.0 in
  for i = 0 to n - 1 do
    let x = ref 0.0 in
    let y = ref 0.0 in
    for j = 0 to n - 1 do
      x := !x +. fst ps.((i + j) mod n);
      y := !y +. snd ps.((i + j) mod n);
      ans := max !ans (!x *. !x +. !y *. !y);
    done;
  done;
  printf "%.15f\n" (sqrt !ans);

Score: 600 points

Problem Statement

E869120 is initially standing at the origin (0, 0) in a two-dimensional plane.

He has N engines, which can be used as follows:

When E869120 uses the i-th engine, his X- and Y-coordinate change by x_i and y_i, respectively. In other words, if E869120 uses the i-th engine from coordinates (X, Y), he will move to the coordinates (X + x_i, Y + y_i).

E869120 can use these engines in any order, but each engine can be used at most once. He may also choose not to use some of the engines.

He wants to go as far as possible from the origin.
Let (X, Y) be his final coordinates. Find the maximum possible value of \sqrt{X^2 + Y^2}, the distance from the origin.

Constraints

1 \leq N \leq 100

-1 \ 000 \ 000 \leq x_i \leq 1 \ 000 \ 000

-1 \ 000 \ 000 \leq y_i \leq 1 \ 000 \ 000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
: :
x_N y_N

Output

Print the maximum possible final distance from the origin, as a real value.
Your output is considered correct when the relative or absolute error from the true answer is at most 10^{-10}.

Sample Input 1

3
0 10
5 -5
-5 -5

Sample Output 1

10.000000000000000000000000000000000000000000000000

The final distance from the origin can be 10 if we use the engines in one of the following three ways:

Use Engine 1 to move to (0, 10).

Use Engine 2 to move to (5, -5), and then use Engine 3 to move to (0, -10).

Use Engine 3 to move to (-5, -5), and then use Engine 2 to move to (0, -10).

The distance cannot be greater than 10, so the maximum possible distance is 10.

Sample Input 2

5
1 1
1 0
0 1
-1 0
0 -1

Sample Output 2

2.828427124746190097603377448419396157139343750753

The maximum possible final distance is 2 \sqrt{2} = 2.82842....
One of the ways to achieve it is:

Use Engine 1 to move to (1, 1), and then use Engine 2 to move to (2, 1), and finally use Engine 3 to move to (2, 2).

Sample Input 3

5
1 1
2 2
3 3
4 4
5 5

Sample Output 3

21.213203435596425732025330863145471178545078130654

If we use all the engines in the order 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5, we will end up at (15, 15), with the distance 15 \sqrt{2} = 21.2132... from the origin.

Sample Input 4

3
0 0
0 1
1 0

Sample Output 4

1.414213562373095048801688724209698078569671875376

There can be useless engines with (x_i, y_i) = (0, 0).

Sample Input 5

1
90447 91000

Sample Output 5

128303.000000000000000000000000000000000000000000000000

Note that there can be only one engine.

Sample Input 6

2
96000 -72000
-72000 54000

Sample Output 6

120000.000000000000000000000000000000000000000000000000

There can be only two engines, too.

Sample Input 7

10
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
17 18
19 20

Sample Output 7

148.660687473185055226120082139313966514489855137208


let scan_int () = Scanf.scanf " %d" (fun x -> x)
let print_int n = Printf.printf "%d\n" n

let () =
  let m = scan_int() and d = scan_int() in
  let rec loop mi di cnt =
    let d1 = di mod 10 and d10 = di / 10 in
    let cnt =
      if d1 > 1 && d10 > 1 && d1 * d10 = mi then cnt+1
      else cnt in
    if di = d then
      if mi = m then cnt
      else loop (mi+1) 1 cnt
    else loop mi (di+1) cnt in
  print_int (loop 1 1 0)

Score : 200 points

Problem Statement

Today is August 24, one of the five Product Days in a year.

A date m-d (m is the month, d is the date) is called a Product Day when d is a two-digit number, and all of the following conditions are satisfied (here d_{10} is the tens digit of the day and d_1 is the ones digit of the day):

d_1 \geq 2

d_{10} \geq 2

d_1 \times d_{10} = m

Takahashi wants more Product Days, and he made a new calendar called Takahashi Calendar where a year consists of M month from Month 1 to Month M, and each month consists of D days from Day 1 to Day D.

In Takahashi Calendar, how many Product Days does a year have?

Constraints

All values in input are integers.

1 \leq M \leq 100

1 \leq D \leq 99

Input

Input is given from Standard Input in the following format:

M D

Output

Print the number of Product Days in a year in Takahashi Calender.

Sample Input 1

15 40

Sample Output 1

10

There are 10 Product Days in a year, as follows (m-d denotes Month m, Day d):

4-22

6-23

6-32

8-24

9-33

10-25

12-26

12-34

14-27

15-35

Sample Input 2

12 31

Sample Output 2

5

Sample Input 3

1 1

Sample Output 3

0


let m, d = Scanf.scanf " %d %d" @@ fun a b -> a, b
let f m d = d mod 10 >= 2 && d / 10 >= 2 && d / 10 * (d mod 10) = m
let ans = ref 0
let _ = for i = 2 to m do for j = 2 to d do if f i j then incr ans done done; Printf.printf "%d\n" !ans


let scan_int () = Scanf.scanf " %d" (fun x -> x)
let print_int n = Printf.printf "%d\n" n

let modulo = 1000000007
let modmul a b = a * b mod modulo

let () =
  let n = scan_int() in
  let s = read_line() in
  let rec loop i curr ret =
    if i = 2 * n && curr = 0 then ret
    else if i = 2 * n || curr < 0 then 0
    else
      if s.[i] = 'B' then
        if curr mod 2 = 1 then loop (i+1) (curr-1) (modmul ret curr)
        else loop (i+1) (curr+1) ret
      else
        if curr mod 2 = 1 then loop (i+1) (curr+1) ret
        else loop (i+1) (curr-1) (modmul ret curr) in
  let rec calc_factorial i n ret =
    if i > n then ret else calc_factorial (i+1) n (modmul ret i) in
  let a = loop 0 0 1 and b = calc_factorial 1 n 1 in
  print_int (modmul a b)

Score : 500 points

Problem Statement

There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares.

The color of the i-th square from the left is black if the i-th character of S is B, and white if that character is W.

You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa.

Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once.

Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7.

Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different.

Constraints

1 \leq N \leq 10^5

|S| = 2N

Each character of S is B or W.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0.

Sample Input 1

2
BWWB

Sample Output 1

4

There are four ways to make all the squares white, as follows:

Choose Squares 1, 3 in the first operation, and choose Squares 2, 4 in the second operation.

Choose Squares 2, 4 in the first operation, and choose Squares 1, 3 in the second operation.

Choose Squares 1, 4 in the first operation, and choose Squares 2, 3 in the second operation.

Choose Squares 2, 3 in the first operation, and choose Squares 1, 4 in the second operation.

Sample Input 2

4
BWBBWWWB

Sample Output 2

288

Sample Input 3

5
WWWWWWWWWW

Sample Output 3

0


let () = Scanf.scanf "%d" @@ fun n ->
  let aa = Array.init n @@ fun _ -> Scanf.scanf " %d" Fun.id in
  let ans = ref 0.0 in
  for i = 0 to n - 1 do
    ans := !ans +. 1.0 /. float_of_int aa.(i)
  done;
  Printf.printf "%f\n" (1.0 /. !ans)

Score : 200 points

Problem Statement

Given is a sequence of N integers A_1, \ldots, A_N.

Find the (multiplicative) inverse of the sum of the inverses of these numbers, \frac{1}{\frac{1}{A_1} + \ldots + \frac{1}{A_N}}.

Constraints

1 \leq N \leq 100

1 \leq A_i \leq 1000

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N

Output

Print a decimal number (or an integer) representing the value of \frac{1}{\frac{1}{A_1} + \ldots + \frac{1}{A_N}}.

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

Sample Input 1

2
10 30

Sample Output 1

7.5

\frac{1}{\frac{1}{10} + \frac{1}{30}} = \frac{1}{\frac{4}{30}} = \frac{30}{4} = 7.5.

Printing 7.50001, 7.49999, and so on will also be accepted.

Sample Input 2

3
200 200 200

Sample Output 2

66.66666666666667

\frac{1}{\frac{1}{200} + \frac{1}{200} + \frac{1}{200}} = \frac{1}{\frac{3}{200}} = \frac{200}{3} = 66.6666....

Printing 6.66666e+1 and so on will also be accepted.

Sample Input 3

1
1000

Sample Output 3

1000

\frac{1}{\frac{1}{1000}} = 1000.

Printing +1000.0 and so on will also be accepted.


open Printf
open Scanf

let () =
  let n = scanf "%d" (fun x -> x) in
  Array.init n (fun _ -> scanf " %f" (fun x -> x))
  |> Array.map (fun x -> 1.0 /. x)
  |> Array.fold_left (+.) 0.0
  |> (fun x -> 1.0 /. x)
  |> printf "%.15f\n"


let n = Scanf.scanf " %d" (+) 0
let vs = Array.init n @@ fun _ -> Scanf.scanf " %f" (+.) 0.
let _ = Array.sort compare vs; Printf.printf "%.9f\n" @@ Array.fold_left (fun a v -> (a +. v) /. 2.) vs.(0) vs

Score : 300 points

Problem Statement

You have a pot and N ingredients. Each ingredient has a real number parameter called value, and the value of the i-th ingredient (1 \leq i \leq N) is v_i.

When you put two ingredients in the pot, they will vanish and result in the formation of a new ingredient. The value of the new ingredient will be (x + y) / 2 where x and y are the values of the ingredients consumed, and you can put this ingredient again in the pot.

After you compose ingredients in this way N-1 times, you will end up with one ingredient. Find the maximum possible value of this ingredient.

Constraints

2 \leq N \leq 50

1 \leq v_i \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
v_1 v_2 \ldots v_N

Output

Print a decimal number (or an integer) representing the maximum possible value of the last ingredient remaining.

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

Sample Input 1

2
3 4

Sample Output 1

3.5

If you start with two ingredients, the only choice is to put both of them in the pot. The value of the ingredient resulting from the ingredients of values 3 and 4 is (3 + 4) / 2 = 3.5.

Printing 3.50001, 3.49999, and so on will also be accepted.

Sample Input 2

3
500 300 200

Sample Output 2

375

You start with three ingredients this time, and you can choose what to use in the first composition. There are three possible choices:

Use the ingredients of values 500 and 300 to produce an ingredient of value (500 + 300) / 2 = 400. The next composition will use this ingredient and the ingredient of value 200, resulting in an ingredient of value (400 + 200) / 2 = 300.

Use the ingredients of values 500 and 200 to produce an ingredient of value (500 + 200) / 2 = 350. The next composition will use this ingredient and the ingredient of value 300, resulting in an ingredient of value (350 + 300) / 2 = 325.

Use the ingredients of values 300 and 200 to produce an ingredient of value (300 + 200) / 2 = 250. The next composition will use this ingredient and the ingredient of value 500, resulting in an ingredient of value (250 + 500) / 2 = 375.

Thus, the maximum possible value of the last ingredient remaining is 375.

Printing 375.0 and so on will also be accepted.

Sample Input 3

5
138 138 138 138 138

Sample Output 3

138


let ioc c = int_of_char c - int_of_char 'a'

let s = read_line()
let t = read_line()
let n = String.length s
let m = String.length t
let d = Array.init 26 (fun _ -> Array.make (n + 1) ~-1)

let rec f i j s =
  if i = m then s + j
  else
    let k = ioc t.[i] in
    if d.(k).(0) = -1 then -1
    else
      let j = d.(k).(j) in
      if j = -1 then f (i + 1) (d.(k).(0) + 1) (s + n)
      else f (i + 1) (j + 1) s

let () =
  for i = 0 to n - 1 do
    d.(ioc s.[i]).(i) <- i
  done;

  Array.iter (fun d ->
    for i = 0 to n - 1 do
      let j = n - 1 - i in
      if d.(j) = -1 then
        d.(j) <- d.(j + 1)
      done
  ) d;

  d.(ioc s.[0]).(0) <- 0;

  Printf.printf "%d\n" (f 0 0 0)

Score : 500 points

Problem Statement

Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists.

Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s').

Notes

A subsequence of a string a is a string obtained by deleting zero or more characters from a and concatenating the remaining characters without changing the relative order. For example, the subsequences of contest include net, c, and contest.

Constraints

1 \leq |s| \leq 10^5

1 \leq |t| \leq 10^5

s and t consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print -1.

Sample Input 1

contest
son

Sample Output 1

10

t = son is a subsequence of the string contestcon (the first 10 characters in s' = contestcontestcontest...), so i = 10 satisfies the condition.

On the other hand, t is not a subsequence of the string contestco (the first 9 characters in s'), so i = 9 does not satisfy the condition.

Similarly, any integer less than 9 does not satisfy the condition, either. Thus, the minimum integer i satisfying the condition is 10.

Sample Input 2

contest
programming

Sample Output 2

-1

t = programming is not a substring of s' = contestcontestcontest.... Thus, there is no integer i satisfying the condition.

Sample Input 3

contest
sentence

Sample Output 3

33

Note that the answer may not fit into a 32-bit integer type, though we cannot put such a case here.


let test a b =
  Printf.printf "%d\n" (max (max (a+b) (a-b)) (a*b));;

Scanf.sscanf (read_line ()) "%d %d" (fun a b -> test a b);;

Score : 100 points

Problem Statement

We have two integers: A and B.

Print the largest number among A + B, A - B, and A \times B.

Constraints

All values in input are integers.

-100 \leq A,\ B \leq 100

Input

Input is given from Standard Input in the following format:

A B

Output

Print the largest number among A + B, A - B, and A \times B.

Sample Input 1

-13 3

Sample Output 1

-10

The largest number among A + B = -10, A - B = -16, and A \times B = -39 is -10.

Sample Input 2

1 -33

Sample Output 2

34

The largest number among A + B = -32, A - B = 34, and A \times B = -33 is 34.

Sample Input 3

13 3

Sample Output 3

39

The largest number among A + B = 16, A - B = 10, and A \times B = 39 is 39.


open Printf
open Scanf

let solve a b = max (a + b) @@ max (a - b) (a * b)

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


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


type 'a t = Empty | Node of (int * 'a * 'a t * 'a t)

let rec merge q1 q2 =
    match q1, q2 with
    | Empty, _ -> q2
    | _, Empty -> q1
    | Node(n1, v1, l1, r1), Node(n2, v2, l2, r2) ->
        if v1 < v2
        then Node(n1 + n2, v2, q1, merge l2 r2)
        else Node(n1 + n2, v1, merge l1 r1, q2)

let pop q =
    match q with
    | Empty -> failwith "pop"
    | Node(_, _, l, r) ->
        match l, r with
        | Empty, Empty -> Empty
        | Node(_, _, _, _), Empty -> l
        | Node(nl, vl, ll, rl), Node(nr, vr, lr, rr) ->
            if vl < vr
            then Node(nl + nr, vr, l, merge lr rr)
            else Node(nl + nr, vl, merge ll rl, r)
        | _, _ -> failwith "pop"

let rec push x q =
    match q with
    | Empty -> Node(1, x, Empty, Empty)
    | Node(_, v, l, r) ->
        let large = max x v in
        let small = min x v in
        match l, r with
        | Empty, Empty         -> Node(2, large, Node(1, small, Empty, Empty), Empty)
        | Node(n, _, _, _), Empty -> Node(n + 1, large, l, Node(1, small, Empty, Empty))
        | Node(nl, _, _, _), Node(nr, _, _, _) ->
            if nl > nr
            then Node(nl + nr + 2, large, l, push small r)
            else Node(nl + nr + 2, large, push small l, r)
        | _, _ -> failwith "push"

let top q =
    match q with
    | Empty -> failwith "top"
    | Node(_, v, _, _) -> v

let rec insert_while i ws q =
    match ws with
    | w :: ws' when fst w = i ->
        insert_while i ws' (push (snd w) q)
    | _ -> (ws, q)

let rec solve ws m q i sum =
    if i > m then sum
    else
        let (ws', q') = insert_while i ws q in
        if q' = Empty
        then solve ws' m q' (i + 1) sum
        else solve ws' m (pop q') (i + 1) (sum + top q')

let rec input n =
    if n = 0 then []
    else Scanf.scanf "%d %d\n" @@ fun a b ->
        let ws = input (n - 1) in
        (a, b) :: ws

let () =
    Scanf.scanf "%d %d\n" @@ fun n m ->
    let ws = input n in
    let ws' = List.sort (fun a b -> compare (fst a) (fst b)) ws in
    Printf.printf "%d\n" @@ solve ws' m Empty 1 0

Score : 400 points

Problem Statement

There are N one-off jobs available. If you take the i-th job and complete it, you will earn the reward of B_i after A_i days from the day you do it.

You can take and complete at most one of these jobs in a day.

However, you cannot retake a job that you have already done.

Find the maximum total reward that you can earn no later than M days from today.

You can already start working today.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i \leq 10^5

1 \leq B_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the maximum total reward that you can earn no later than M days from today.

Sample Input 1

3 4
4 3
4 1
2 2

Sample Output 1

5

You can earn the total reward of 5 by taking the jobs as follows:

Take and complete the first job today. You will earn the reward of 3 after four days from today.

Take and complete the third job tomorrow. You will earn the reward of 2 after two days from tomorrow, that is, after three days from today.

Sample Input 2

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

Sample Output 2

10

Sample Input 3

1 1
2 1

Sample Output 3

0


let transfer a b c =
  let remain = c - (a - b) in
    if remain > 0 then remain else 0

let a, b, c = Scanf.sscanf (read_line ()) "%d %d %d " (fun a b c -> (a, b, c))
let () = Printf.printf "%d\n" (transfer a b c)

Score : 100 points

Problem Statement

We have two bottles for holding water.

Bottle 1 can hold up to A milliliters of water, and now it contains B milliliters of water.

Bottle 2 contains C milliliters of water.

We will transfer water from Bottle 2 to Bottle 1 as much as possible.

How much amount of water will remain in Bottle 2?

Constraints

All values in input are integers.

1 \leq B \leq A \leq 20

1 \leq C \leq 20

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the integer representing the amount of water, in milliliters, that will remain in Bottle 2.

Sample Input 1

6 4 3

Sample Output 1

1

We will transfer two milliliters of water from Bottle 2 to Bottle 1, and one milliliter of water will remain in Bottle 2.

Sample Input 2

8 3 9

Sample Output 2

4

Sample Input 3

12 3 7

Sample Output 3

0


open Printf
open Scanf

let readint () = scanf " %d" (fun x -> x)

let () =
  let a = readint () and b = readint () and c = readint () in
  printf "%d\n" @@ max 0 (c - (a - b))


let () = Scanf.scanf "%d %d %d" @@ fun a b c ->
    Printf.printf "%d" @@ max 0 (c - a + b)


let rec divisors acc acc' i n =
  match compare n (i * i) with
  | -1 -> List.rev_append acc acc'
  | 0 -> List.rev_append acc (i :: acc')
  | 1 ->
      ( if 0 < n mod i
        then divisors acc acc'
        else divisors (i :: acc) (n / i :: acc') ) (i + 1) n
let divisors = divisors [] [] 1

let () = Scanf.scanf "%d %d\n" @@ fun n k ->
  let as_ = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun a -> a in
  let ms = Array.make n 0 in
  let acc = Array.make (n + 1) 0 in
  let acc' = Array.make (n + 1) 0 in
  Printf.printf "%d\n" @@
  List.fold_left max min_int @@
  List.filter (fun d ->
    for i = 0 to n - 1 do
      ms.(i) <- as_.(i) mod d;
    done;
    Array.sort compare ms;
    for i = 0 to n - 1 do
      acc.(i + 1) <- acc.(i) + ms.(i)
    done;
    for i = n - 1 downto 0 do
      acc'.(i) <- acc'.(i + 1) + d - ms.(i)
    done;
    Array.fold_left ( || ) false @@
    Array.init (n + 1) @@ fun i ->
      acc.(i) = acc'.(i) && acc.(i) <= k) @@
  divisors @@
  Array.fold_left ( + ) 0 as_

Score : 500 points

Problem Statement

We have a sequence of N integers: A_1, A_2, \cdots, A_N.

You can perform the following operation between 0 and K times (inclusive):

Choose two integers i and j such that i \neq j, each between 1 and N (inclusive). Add 1 to A_i and -1 to A_j, possibly producing a negative element.

Compute the maximum possible positive integer that divides every element of A after the operations. Here a positive integer x divides an integer y if and only if there exists an integer z such that y = xz.

Constraints

2 \leq N \leq 500

1 \leq A_i \leq 10^6

0 \leq K \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \cdots A_{N-1} A_{N}

Output

Print the maximum possible positive integer that divides every element of A after the operations.

Sample Input 1

2 3
8 20

Sample Output 1

7

7 will divide every element of A if, for example, we perform the following operation:

Choose i = 2, j = 1. A becomes (7, 21).

We cannot reach the situation where 8 or greater integer divides every element of A.

Sample Input 2

2 10
3 5

Sample Output 2

8

Consider performing the following five operations:

Choose i = 2, j = 1. A becomes (2, 6).

Choose i = 2, j = 1. A becomes (1, 7).

Choose i = 2, j = 1. A becomes (0, 8).

Choose i = 2, j = 1. A becomes (-1, 9).

Choose i = 1, j = 2. A becomes (0, 8).

Then, 0 = 8 \times 0 and 8 = 8 \times 1, so 8 divides every element of A. We cannot reach the situation where 9 or greater integer divides every element of A.

Sample Input 3

4 5
10 1 2 22

Sample Output 3

7

Sample Input 4

8 7
1 7 5 6 8 2 6 5

Sample Output 4

5


let _ = Scanf.sscanf (read_line ()) "%d %d" (fun a b ->
  match a + b with
  | x when x mod 2 = 1 -> print_endline "IMPOSSIBLE"
  | x -> print_endline (string_of_int (x / 2))
)

Score: 100 points

Problem Statement

We have two distinct integers A and B.

Print the integer K such that |A - K| = |B - K|.

If such an integer does not exist, print IMPOSSIBLE instead.

Constraints

All values in input are integers.

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

A and B are distinct.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the integer K satisfying the condition.

If such an integer does not exist, print IMPOSSIBLE instead.

Sample Input 1

2 16

Sample Output 1

9

|2 - 9| = 7 and |16 - 9| = 7, so 9 satisfies the condition.

Sample Input 2

0 3

Sample Output 2

IMPOSSIBLE

No integer satisfies the condition.

Sample Input 3

998244353 99824435

Sample Output 3

549034394


let a, b = Scanf.scanf " %d %d" @@ fun a b -> a, b
let _ = if (a + b) mod 2 = 1 then print_endline "IMPOSSIBLE" else Printf.printf "%d\n" @@ (a + b) / 2


(* Vicfred
 * https://atcoder.jp/contests/abc135/tasks/abc135_b
 * greedy
 * *)
let n = Scanf.scanf "%d\n" ( + ) 0
let ps = Array.init n @@ fun _ -> Scanf.scanf " %d" ( + ) 0
let swaps = Array.fold_left ( + ) 0 @@ Array.init n @@ fun i -> if (i + 1) != ps.(i) then 1 else 0
let _ = Printf.printf "%s\n" (if swaps <= 2 then "YES" else "NO")

Score : 200 points

Problem Statement

We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.

You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.

Print YES if you can sort p in ascending order in this way, and NO otherwise.

Constraints

All values in input are integers.

2 \leq N \leq 50

p is a permutation of {1,\ 2,\ ...,\ N}.

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_N

Output

Print YES if you can sort p in ascending order in the way stated in the problem statement, and NO otherwise.

Sample Input 1

5
5 2 3 4 1

Sample Output 1

YES

You can sort p in ascending order by swapping p_1 and p_5.

Sample Input 2

5
2 4 3 5 1

Sample Output 2

NO

In this case, swapping any two elements does not sort p in ascending order.

Sample Input 3

7
1 2 3 4 5 6 7

Sample Output 3

YES

p is already sorted in ascending order, so no operation is needed.


let () =
    let main () =
        let m = 1_000_000_000 + 7 in
        let (+@) a b = (a + b) mod m in
        let s = read_line () in
        let a = Array.make 13 0 in
        let l = String.length s in
        let rec loop i a =
            if i = l then a else
                let b = Array.make 13 0 in
                let () =
                    if s.[i] = '?' then (
                        for j = 0 to 12 do
                            for k = 0 to 9 do
                                b.((j * 10 + k) mod 13) <- b.((j * 10 + k) mod 13) +@ a.(j)
                            done
                        done
                    ) else (
                        let k = int_of_char s.[i] - int_of_char '0' in
                        for j = 0 to 12 do
                            b.((j * 10 + k) mod 13) <- b.((j * 10 + k) mod 13) +@ a.(j)
                        done
                    )
                in
                loop (i + 1) b
        in
        let a = loop 0 (Array.init 13 (fun i -> if i = 0 then 1 else 0)) in
        Printf.printf "%d\n" a.(5)
    in
    main ()

Score : 400 points

Problem Statement

Given is a string S. Each character in S is either a digit (0, ..., 9) or ?.

Among the integers obtained by replacing each occurrence of ? with a digit, how many have a remainder of 5 when divided by 13? An integer may begin with 0.

Since the answer can be enormous, print the count modulo 10^9+7.

Constraints

S is a string consisting of digits (0, ..., 9) and ?.

1 \leq |S| \leq 10^5

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of integers satisfying the condition, modulo 10^9+7.

Sample Input 1

??2??5

Sample Output 1

768

For example, 482305, 002865, and 972665 satisfy the condition.

Sample Input 2

?44

Sample Output 2

1

Only 044 satisfies the condition.

Sample Input 3

7?4

Sample Output 3

0

We may not be able to produce an integer satisfying the condition.

Sample Input 4

?6?42???8??2??06243????9??3???7258??5??7???????774????4?1??17???9?5?70???76???

Sample Output 4

153716888


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

let ceil_sqrt n =
  let m = floor_sqrt n in
  if n <= m * m then m else m + 1

let () = Scanf.scanf "%d" @@ fun s ->
  let x1 = ceil_sqrt s in
  Printf.printf "0 0 %d %d 1 %d" x1 (x1 * x1 - s) x1

Score : 400 points

Problem Statement

Given is an integer S.
Find a combination of six integers X_1,Y_1,X_2,Y_2,X_3, and Y_3 that satisfies all of the following conditions:

0 \leq X_1,Y_1,X_2,Y_2,X_3,Y_3 \leq 10^9

The area of the triangle in a two-dimensional plane whose vertices are (X_1,Y_1),(X_2,Y_2), and (X_3,Y_3) is S/2.

We can prove that there always exist six integers that satisfy the conditions under the constraints of this problem.

Constraints

1 \leq S \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

S

Output

Print six integers X_1,Y_1,X_2,Y_2,X_3, and Y_3 that satisfy the conditions, in this order, with spaces in between.
If multiple solutions exist, any of them will be accepted.

Sample Input 1

3

Sample Output 1

1 0 2 2 0 1

The area of the triangle in a two-dimensional plane whose vertices are (1,0),(2,2), and (0,1) is 3/2.
Printing 3 0 3 1 0 1 or 1 0 0 1 2 2 will also be accepted.

Sample Input 2

100

Sample Output 2

0 0 10 0 0 10

Sample Input 3

311114770564041497

Sample Output 3

314159265 358979323 846264338 327950288 419716939 937510582


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

let () = Scanf.scanf "%d" @@ fun s ->
  let x1 = lower_bound 0 s @@ fun x1 -> s <= x1 * x1 in
  Printf.printf "0 0 %d %d 1 %d" x1 (x1 * x1 - s) x1


let () =
    let main n =
        let s = n in
        let x1 = 0 in
        let y1 = 0 in
        let sq = int_of_float (ceil (sqrt (float (s)))) in
        let rec loop offs =
            let x2 = sq + offs in
            let y3 = sq - offs in
            let sq2 = x2 * y3 - s in
            if x2 > 1_000_000_000 || sq2 < 0 then failwith "??" else
                let rec loop_2 sq3 count =
                    if count > 400000 || sq3 > sq2 then loop (offs + 1) else
                    if sq2 mod sq3 = 0 then
                        let x3 = sq3 in
                        let y2 = sq2 / sq3 in
                        x1, y1, x2, y2, x3, y3
                    else loop_2 (sq3 + 1) (count + 1)
                in
                if sq2 = 0 then x1, y1, x2, 0, 0, y3 else
                let sq3 = int_of_float (floor (sqrt (float (sq2)))) in
                loop_2 sq3 0
        in
        loop 0
    in
    let x1, y1, x2, y2, x3, y3 = main (int_of_string (read_line ())) in
    Printf.printf "%d %d %d %d %d %d\n" x1 y1 x2 y2 x3 y3


(* Vicfred
 * https://atcoder.jp/contests/abc134/tasks/abc134_a
 * math, implementation
 * *)
Scanf.scanf "%d\n" @@ fun r ->
    Printf.printf "%d\n" @@
    3 * r * r

Score : 100 points

Problem Statement

It is known that the area of a regular dodecagon inscribed in a circle of radius a is 3a^2.

Given an integer r, find the area of a regular dodecagon inscribed in a circle of radius r.

Constraints

1 \leq r \leq 100

r is an integer.

Input

Input is given from Standard Input in the following format:

r

Output

Print an integer representing the area of the regular dodecagon.

Sample Input 1

4

Sample Output 1

48

The area of the regular dodecagon is 3 \times 4^2 = 48.

Sample Input 2

15

Sample Output 2

675

Sample Input 3

80

Sample Output 3

19200


let answer a b =
  let n = a / (b * 2 + 1) in
  let m = a mod (b * 2 + 1) in
    if m == 0 then n else n + 1

let () = Scanf.scanf "%d %d" (fun a b -> print_endline (string_of_int (answer a b)))

Score : 200 points

Problem Statement

There are N apple trees in a row. People say that one of them will bear golden apples.

We want to deploy some number of inspectors so that each of these trees will be inspected.

Each inspector will be deployed under one of the trees. For convenience, we will assign numbers from 1 through N to the trees. An inspector deployed under the i-th tree (1 \leq i \leq N) will inspect the trees with numbers between i-D and i+D (inclusive).

Find the minimum number of inspectors that we need to deploy to achieve the objective.

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq D \leq 20

Input

Input is given from Standard Input in the following format:

N D

Output

Print the minimum number of inspectors that we need to deploy to achieve the objective.

Sample Input 1

6 2

Sample Output 1

2

We can achieve the objective by, for example, placing an inspector under Tree 3 and Tree 4.

Sample Input 2

14 3

Sample Output 2

2

Sample Input 3

20 4

Sample Output 3

3


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

let solve n a =
  let b = Array.make n (-1) in
  let rec go i =
    if i < 1 then ()
    else
    let rec sumPrev s j =
      if j > n then s else sumPrev (s + b.(j-1)) (j + i)
    in
    let tot = sumPrev 0 (2*i) mod 2 in
    (*Printf.printf "%d %d %d\n" i tot a.(i-1);*)
    b.(i-1) <- if tot = a.(i-1) then 0 else 1;
    go (i-1)
  in go n;
  let rec go (k, acc) i =
    if i < 1 then (k, acc)
    else go (if b.(i-1) = 1 then (k+1, i :: acc) else (k, acc)) (i-1)
  in go (0, []) n

let main () =
  let n = readInt () in
  let a = Array.init n (fun _ -> readInt ()) in
  let (k, ans) = solve n a in
  Printf.printf "%d\n" k;
  ans |> List.iter (fun x -> Printf.printf "%d " x);
  print_endline ""

let () = main ()

Score : 400 points

Problem Statement

There are N empty boxes arranged in a row from left to right.
The integer i is written on the i-th box from the left (1 \leq i \leq N).

For each of these boxes, Snuke can choose either to put a ball in it or to put nothing in it.

We say a set of choices to put a ball or not in the boxes is good when the following condition is satisfied:

For every integer i between 1 and N (inclusive), the total number of balls contained in the boxes with multiples of i written on them is congruent to a_i modulo 2.

Does there exist a good set of choices? If the answer is yes, find one good set of choices.

Constraints

All values in input are integers.

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

a_i is 0 or 1.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

If a good set of choices does not exist, print -1.

If a good set of choices exists, print one such set of choices in the following format:

M
b_1 b_2 ... b_M

where M denotes the number of boxes that will contain a ball, and b_1,\ b_2,\ ...,\ b_M are the integers written on these boxes, in any order.

Sample Input 1

3
1 0 0

Sample Output 1

1
1

Consider putting a ball only in the box with 1 written on it.

There are three boxes with multiples of 1 written on them: the boxes with 1, 2, and 3. The total number of balls contained in these boxes is 1.

There is only one box with a multiple of 2 written on it: the box with 2. The total number of balls contained in these boxes is 0.

There is only one box with a multiple of 3 written on it: the box with 3. The total number of balls contained in these boxes is 0.

Thus, the condition is satisfied, so this set of choices is good.

Sample Input 2

5
0 0 0 0 0

Sample Output 2

0

Putting nothing in the boxes can be a good set of choices.


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

let remove a s =
  match IntMap.find a s with
  | exception Not_found -> s
  | 1 -> IntMap.remove a s
  | n -> IntMap.add a (n - 1) s

let add a s =
  IntMap.add a (1 + try IntMap.find a s with Not_found -> 0) s

let () = Scanf.scanf "%d\n" @@ fun n ->
  let as_ =
    Array.fold_right add
      (Array.init n (fun _ ->
        Scanf.scanf "%d " (fun a -> a)))
      IntMap.empty in
  let rec solve s a1 a2 = function
    | 0 -> true
    | n -> IntMap.mem a2 s && solve (remove a2 s) a2 (a1 lxor a2) (n - 1) in
  print_endline @@
  if
    solve as_ (fst (IntMap.min_binding as_)) (fst (IntMap.max_binding as_)) n
  then "Yes"
  else "No"

Score : 300 points

Problem Statement

Snuke has N hats. The i-th hat has an integer a_i written on it.

There are N camels standing in a circle.
Snuke will put one of his hats on each of these camels.

If there exists a way to distribute the hats to the camels such that the following condition is satisfied for every camel, print Yes; otherwise, print No.

The bitwise XOR of the numbers written on the hats on both adjacent camels is equal to the number on the hat on itself.

What is XOR?

The bitwise XOR x_1 \oplus x_2 \oplus \ldots \oplus x_n of n non-negative integers x_1, x_2, \ldots, x_n is defined as follows:

- When x_1 \oplus x_2 \oplus \ldots \oplus x_n is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if the number of integers among x_1, x_2, \ldots, x_n whose binary representations have 1 in the 2^k's place is odd, and 0 if that count is even.

For example, 3 \oplus 5 = 6.

Constraints

All values in input are integers.

3 \leq N \leq 10^{5}

0 \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 answer.

Sample Input 1

3
1 2 3

Sample Output 1

Yes

If we put the hats with 1, 2, and 3 in this order, clockwise, the condition will be satisfied for every camel, so the answer is Yes.

Sample Input 2

4
1 2 4 8

Sample Output 2

No

There is no such way to distribute the hats; the answer is No.


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

let () = Scanf.scanf "%d\n" @@ fun n ->
  let a :: as_ =
    Array.to_list @@
    Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun a -> a in
  print_endline @@
  if
    let s = IntSet.of_list as_ in
    List.exists (fun a' ->
      let rec solve s a1 a2 = function
        | 0 -> a2 = a
        | n -> IntSet.mem a2 s && solve (IntSet.remove a2 s) a2 (a1 lxor a2) (n - 1) in
      solve s a a' (n - 1)) as_
  then "Yes"
  else "No"