let eqs a b o =
  let a, b, o = if o < 0 then b, a, -o else a, b, o in
  let rec f i =
    if i+o >= String.length a || i >= String.length b then true
    else (a.[i+o] = '?' || b.[i] = '?' || a.[i+o] = b.[i]) && f (i+1)
  in f 0

let max3 a b = max (max a b)
let min3 a b = min (min a b)

let () =
  Scanf.scanf "%s %s %s" @@ fun a b c ->
  let la, lb, lc = String.(length a, length b, length c) in
  let ab = Array.init 4001 (fun i -> eqs a b (i-2000)) in
  let bc = Array.init 4001 (fun i -> eqs b c (i-2000)) in
  let ac = Array.init 4001 (fun i -> eqs a c (i-2000)) in

  let ans = ref 6000 in
  for i = -4000 to 4000 do
    if la - i < !ans && i < (!ans - lb) && (abs i > 2000 || ab.(i+2000)) then
      for j = -4000 to 4000 do
        if (la - j < !ans) && j < (!ans - lc) &&
           (abs j > 2000 || ac.(j+2000)) &&
           (abs (j-i) > 2000 || bc.(j-i+2000))
        then
          ans := min !ans (max3 la (i+lb) (j+lc) - min3 0 i j)
      done;
  done;
  Printf.printf "%d\n" !ans

Score : 500 points

Problem Statement

Snuke has a string s.
From this string, Anuke, Bnuke, and Cnuke obtained strings a, b, and c, respectively, as follows:

Choose a non-empty (contiguous) substring of s (possibly s itself). Then, replace some characters (possibly all or none) in it with ?s.

For example, if s is mississippi, we can choose the substring ssissip and replace its 1-st and 3-rd characters with ? to obtain ?s?ssip.

You are given the strings a, b, and c.
Find the minimum possible length of s.

Constraints

1 \leq |a|, |b|, |c| \leq 2000

a, b, and c consists of lowercase English letters and ?s.

Input

Input is given from Standard Input in the following format:

a
b
c

Output

Print the minimum possible length of s.

Sample Input 1

a?c
der
cod

Sample Output 1

7

For example, s could be atcoder.

Sample Input 2

atcoder
atcoder
???????

Sample Output 2

7

a, b, and c may not be distinct.


let () =
  let s = Scanf.scanf "%s\n" @@ fun s -> s in
  let ans = ref true in
  for i = 0 to String.length s - 1 do
    if (i mod 2 = 0 && s.[i] = 'h') || (i mod 2 = 1 && s.[i] = 'i')
    then ans := !ans && true
    else ans := !ans && false
  done;
  Printf.printf "%s\n" (if !ans then "Yes" else "No")

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

Score : 100 points

Problem Statement

A Hitachi string is a concatenation of one or more copies of the string hi.

For example, hi and hihi are Hitachi strings, while ha and hii are not.

Given a string S, determine whether S is a Hitachi string.

Constraints

The length of S is between 1 and 10 (inclusive).

S is a string consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S is a Hitachi string, print Yes; otherwise, print No.

Sample Input 1

hihi

Sample Output 1

Yes

hihi is the concatenation of two copies of hi, so it is a Hitachi string.

Sample Input 2

hi

Sample Output 2

Yes

Sample Input 3

ha

Sample Output 3

No

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 s = read_line () in
let n = String.length s in
let rec loop i =
    if i = n then true else (
        match i mod 2, s.[i] with
        | 0, 'h'
        | 1, 'i' -> loop (i + 1)
        | _ -> false
    )
in
(if loop 0 then "Yes" else "No") |> print_endline


let s = read_line () in
Printf.printf "%s\n"
  (if s.[0] <> s.[1] || s.[1] <> s.[2] || s.[2] <> s.[0] then "Yes"
   else "No")

Score : 100 points

Problem Statement

In AtCoder City, there are three stations numbered 1, 2, and 3.

Each of these stations is operated by one of the two railway companies, A and B. A string S of length 3 represents which company operates each station. If S_i is A, Company A operates Station i; if S_i is B, Company B operates Station i.

To improve the transportation condition, for each pair of a station operated by Company A and one operated by Company B, there will be a bus service connecting them.

Determine if there is a pair of stations that will be connected by a bus service.

Constraints

Each character of S is A or B.

|S| = 3

Input

Input is given from Standard Input in the following format:

S

Output

If there is a pair of stations that will be connected by a bus service, print Yes; otherwise, print No.

Sample Input 1

ABA

Sample Output 1

Yes

Company A operates Station 1 and 3, while Company B operates Station 2.

There will be a bus service between Station 1 and 2, and between Station 2 and 3, so print Yes.

Sample Input 2

BBA

Sample Output 2

Yes

Company B operates Station 1 and 2, while Company A operates Station 3.

There will be a bus service between Station 1 and 3, and between Station 2 and 3, so print Yes.

Sample Input 3

BBB

Sample Output 3

No

Company B operates all the stations. Thus, there will be no bus service, so print No.


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

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

let ablue =
        let amari = n mod (a + b) in
                if amari < a then
                        amari
                else
                        a

let () =
        (
                let div = n / (a + b) in
                let sblue = a * div in
                        sblue + ablue
        ) |> Printf.printf "%d\n"

Score : 200 points

Problem Statement

Takahashi has many red balls and blue balls. Now, he will place them in a row.

Initially, there is no ball placed.

Takahashi, who is very patient, will do the following operation 10^{100} times:

Place A blue balls at the end of the row of balls already placed. Then, place B red balls at the end of the row.

How many blue balls will be there among the first N balls in the row of balls made this way?

Constraints

1 \leq N \leq 10^{18}

A, B \geq 0

0 < A + B \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the number of blue balls that will be there among the first N balls in the row of balls.

Sample Input 1

8 3 4

Sample Output 1

4

Let b denote a blue ball, and r denote a red ball. The first eight balls in the row will be bbbrrrrb, among which there are four blue balls.

Sample Input 2

8 0 4

Sample Output 2

0

He placed only red balls from the beginning.

Sample Input 3

6 2 4

Sample Output 3

2

Among bbrrrr, there are two blue balls.


open Batteries
let a = read_line()
    |> String.split_on_char ' '
    |> List.map int_of_string

let n = (List.nth a 0) mod ((List.nth a 1) + (List.nth a 2))
let amari = if n > List.nth a 1 then n else List.nth a 1

let _ = (((List.nth a 0) / ((List.nth a 1) + (List.nth a 2))) * (List.nth a 1) + amari) |> string_of_int |> print_endline


let () = Scanf.scanf "%d %d" @@ fun a b ->
  Printf.printf "%d\n" @@
  try
    List.find (fun i -> 2 * i / 25 = a && i / 10 = b) @@
    Array.to_list @@
    Array.init 20000 @@ fun i -> i
  with Not_found -> -1

Score : 300 points

Problem Statement

Find the price of a product before tax such that, when the consumption tax rate is 8 percent and 10 percent, the amount of consumption tax levied on it is A yen and B yen, respectively. (Yen is the currency of Japan.)

Here, the price before tax must be a positive integer, and the amount of consumption tax is rounded down to the nearest integer.

If multiple prices satisfy the condition, print the lowest such price; if no price satisfies the condition, print -1.

Constraints

1 \leq A \leq B \leq 100

A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

If there is a price that satisfies the condition, print an integer representing the lowest such price; otherwise, print -1.

Sample Input 1

2 2

Sample Output 1

25

If the price of a product before tax is 25 yen, the amount of consumption tax levied on it is:

When the consumption tax rate is 8 percent: \lfloor 25 \times 0.08 \rfloor = \lfloor 2 \rfloor = 2 yen.

When the consumption tax rate is 10 percent: \lfloor 25 \times 0.1 \rfloor = \lfloor 2.5 \rfloor = 2 yen.

Thus, the price of 25 yen satisfies the condition. There are other possible prices, such as 26 yen, but print the minimum such price, 25.

Sample Input 2

8 10

Sample Output 2

100

If the price of a product before tax is 100 yen, the amount of consumption tax levied on it is:

When the consumption tax rate is 8 percent: \lfloor 100 \times 0.08 \rfloor = 8 yen.

When the consumption tax rate is 10 percent: \lfloor 100 \times 0.1 \rfloor = 10 yen.

Sample Input 3

19 99

Sample Output 3

-1

There is no price before tax satisfying this condition, so print -1.


Scanf.scanf "%d %d" (fun a b ->
    let rec loop i =
        if i > 1000 then -1 else
        if i * 8 / 100 = a && i / 10 = b then i else
            loop (i + 1)
    in
    loop 1 |> Printf.printf "%d\n"
)


open Batteries
let s = read_line ()
let q = read_int ()

let string_rev s =
  let len = String.length s in
  String.init len (fun i -> s.[len - 1 - i])

let add_p result line_list reversed =
    match List.nth line_list 1 with
    | "1" -> if not reversed then (List.nth line_list 2) ^ result else result ^ (List.nth line_list 2)
    | _ -> if not reversed then result ^ (List.nth line_list 2) else (List.nth line_list 2) ^ result

let reverse p =
    match p with
    | true -> false
    | false -> true

let rec ans r s reversed =
    match s with
    | 0 -> if not reversed then r else string_rev r
    | _ -> match read_line() with
           | x -> match x.[0] with
                  | '1' -> ans r (s-1) (reverse reversed)
                  | _ -> ans (add_p r (String.split_on_char ' ' x) reversed) (s-1) reversed

let _ = print_endline (ans s q false)

Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

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

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy


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

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 () = Scanf.scanf "%d %d\n%s" @@ fun n p s ->
  let ( +^ ) x y = (x + y) mod p in
  let neg x = (p - x) mod p in
  let ( *^ ) x y = (x * y) mod p in
  let inv x = power ( *^ ) 1 x (p - 2) in
  let acc = Array.make (n + 1) 0 in
  let radix = Array.make (n + 1) 1 in
  for i = 0 to n - 1 do
    radix.(i + 1) <- 10 *^ radix.(i);
    acc.(i + 1) <- 10 *^ acc.(i) +^ (Char.code s.[i] - Char.code '0');
  done;
  let ms = Array.make (n + 2) IntMap.empty in
  for i = n downto 0 do
    ms.(i) <- IntMap.add acc.(i) (1 + try IntMap.find acc.(i) ms.(i + 1) with Not_found -> 0) ms.(i + 1)
  done;
  Printf.printf "%d\n" @@
  Array.fold_left ( + ) 0 @@
  Array.init (n + 1) @@ fun i ->
    try IntMap.find (neg (acc.(i) *^ inv radix.(i))) ms.(i + 1) with Not_found -> 0

Score : 500 points

Problem Statement

Takahashi has a string S of length N consisting of digits from 0 through 9.

He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten.

Here substrings starting with a 0 also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers.

Compute this count to help Takahashi.

Constraints

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

S consists of digits.

|S| = N

2 \leq P \leq 10000

P is a prime number.

Input

Input is given from Standard Input in the following format:

N P
S

Output

Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten.

Sample Input 1

4 3
3543

Sample Output 1

6

Here S = 3543. There are ten non-empty (contiguous) substrings of S:

3: divisible by 3.

35: not divisible by 3.

354: divisible by 3.

3543: divisible by 3.

5: not divisible by 3.

54: divisible by 3.

543: divisible by 3.

4: not divisible by 3.

43: not divisible by 3.

3: divisible by 3.

Six of these are divisible by 3, so print 6.

Sample Input 2

4 2
2020

Sample Output 2

10

Here S = 2020. There are ten non-empty (contiguous) substrings of S, all of which are divisible by 2, so print 10.

Note that substrings beginning with a 0 also count.

Sample Input 3

20 11
33883322005544116655

Sample Output 3

68


Scanf.sscanf (read_line ()) "%d %d" (fun n p ->
    let s = read_line () in
    let dp = Array.make_matrix 2 p 0 in
    let rec loop i acc =
        if i = n then acc else (
            let cur = i mod 2 in
            let next = 1 - cur in
            let c = int_of_char s.[i] - int_of_char '0' in
            for j = 0 to p - 1 do dp.(next).(j) <- 0 done;
            for j = 0 to p - 1 do
                let v = (j * 10 + c) mod p in
                dp.(next).(v) <- dp.(next).(v) + dp.(cur).(j)
            done;
            dp.(next).(c mod p) <- dp.(next).(c mod p) + 1;
            loop (i + 1) (acc + dp.(next).(0))
        )
    in
    loop 0 0 |> Printf.printf "%d\n"
)


open Batteries
open BatPrintf

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

let scan_list cnv =
  read_line ()
  |> String.split_on_char ' '
  |> List.map cnv

let scan_listn n cnv =
  (0 --^ n) |> Enum.map (fun _ -> (cnv % read_line) ())

let bsearch_ge arr a =
  let l = Array.length arr in
  match Array.bsearch Int.ord arr a with
  | `All_lower -> l
  | `All_bigger -> 0
  | `Just_after n -> l - n -1
  | `At n -> l - n
  | `Empty -> 0

let rec zip xs ys =
  match xs, ys with
  | [], _ -> []
  | _, [] -> []
  | x::xs, y::ys ->
    (x, y) :: zip xs ys

let square x = x * x

let dbg n = Printf.eprintf "%s\n" @@ dump n; n

let n = scan "%d" identity

let () =
  succ n / 2
  |> printf "%d"

Score : 100 points

Problem Statement

Takahashi wants to print a document with N pages double-sided, where two pages of data can be printed on one sheet of paper.

At least how many sheets of paper does he need?

Constraints

N is an integer.

1 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

5

Sample Output 1

3

By printing the 1-st, 2-nd pages on the 1-st sheet, 3-rd and 4-th pages on the 2-nd sheet, and 5-th page on the 3-rd sheet, we can print all the data on 3 sheets of paper.

Sample Input 2

2

Sample Output 2

1

Sample Input 3

100

Sample Output 3

50


let () =
  let n = read_int () in
  let num_of_paper = (n + 1) / 2 in
  print_int num_of_paper


open Array
let () =
  let arr = List.concat (to_list (init 3 @@ fun _ -> Scanf.scanf "%d %d %d\n" @@ fun a b c -> [a; b; c])) in
  let n = read_int () in
  let check = Array.make_matrix 3 3 false in
  for i = 0 to n-1 do
    Scanf.scanf "%d\n" @@ fun x ->
    if List.mem x arr then List.iteri (fun i a -> if a = x then check.(i / 3).(i mod 3) <- true) arr
  done;
  let ans = ref false in
  for i = 0 to 2 do
    if check.(i).(0) && check.(i).(1) && check.(i).(2) then ans := true (* 横 *)
    else if check.(0).(i) && check.(1).(i) && check.(2).(i) then ans := true (* たて *)
  done;
  if check.(0).(0) && check.(1).(1) && check.(2).(2) then ans := true;
  if check.(0).(2) && check.(1).(1) && check.(2).(0) then ans := true;
  print_endline (if !ans then "Yes" else "No")

Score : 200 points

Problem Statement

We have a bingo card with a 3\times3 grid. The square at the i-th row from the top and the j-th column from the left contains the number A_{i, j}.

The MC will choose N numbers, b_1, b_2, \cdots, b_N. If our bingo sheet contains some of those numbers, we will mark them on our sheet.

Determine whether we will have a bingo when the N numbers are chosen, that is, the sheet will contain three marked numbers in a row, column, or diagonal.

Constraints

All values in input are integers.

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

A_{i_1, j_1} \neq A_{i_2, j_2} ((i_1, j_1) \neq (i_2, j_2))

1 \leq N \leq 10

1 \leq b_i \leq 100

b_i \neq b_j (i \neq j)

Input

Input is given from Standard Input in the following format:

A_{1, 1} A_{1, 2} A_{1, 3}
A_{2, 1} A_{2, 2} A_{2, 3}
A_{3, 1} A_{3, 2} A_{3, 3}
N
b_1
\vdots
b_N

Output

If we will have a bingo, print Yes; otherwise, print No.

Sample Input 1

84 97 66
79 89 11
61 59 7
7
89
7
87
79
24
84
30

Sample Output 1

Yes

We will mark A_{1, 1}, A_{2, 1}, A_{2, 2}, A_{3, 3}, and complete the diagonal from the top-left to the bottom-right.

Sample Input 2

41 7 46
26 89 2
78 92 8
5
6
45
16
57
17

Sample Output 2

No

We will mark nothing.

Sample Input 3

60 88 34
92 41 43
65 73 48
10
60
43
88
11
48
73
65
41
92
34

Sample Output 3

Yes

We will mark all the squares.


(* 重複なし順列 *)
let rec perm n ls =
  if n = 0 then [[]]
  else flatmap (fun x -> List.map (fun y -> x :: y)
                                  (perm (n - 1) (remove x ls)))
               ls
and remove x ls = List.filter (fun y -> x <> y) ls
and flatten = function
  [] -> []
| x::xs -> x @ (flatten xs)
and flatmap func ls = flatten (List.map func ls)

let t = Scanf.sscanf (read_line ()) "%d %d %d" (fun a b c -> [a; b; c])
let c = Scanf.sscanf (read_line ()) "%d %d %d" (fun a b c -> [a; b; c])
let b = Scanf.sscanf (read_line ()) "%d %d %d" (fun a b c -> [a; b; c])

let bingo_list = [t; c; b;
    [List.hd t; List.nth c 1; List.nth b 2];
    [List.nth t 2; List.nth c 1; List.hd b];

    [List.hd t; List.hd c; List.hd b];
    [List.nth t 1; List.nth c 1; List.nth b 1];
    [List.nth t 2; List.nth c 2; List.nth b 2];
    ]

let n = read_int ()

let rec read_lines_to_int_list count =
    match count with
    | 0 -> []
    | _ -> read_int () :: read_lines_to_int_list (count-1)

let value_list = read_lines_to_int_list n
let all_perms = perm 3 value_list

let rec ans lst = match lst with
     [] -> false
    | first :: rest -> if List.exists (fun x -> x = first) all_perms then true else ans rest

let () = if ans bingo_list then print_endline "Yes" else print_endline "No"


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

Scanf.scanf "%d %d %d" (fun n m k ->
    let friend = Array.make n S.empty in
    let block = Array.make n S.empty in
    for i = 1 to m do
        Scanf.scanf " %d %d" (fun a b ->
            let a = a - 1 in let b = b - 1 in
            friend.(a) <- S.add b friend.(a);
            friend.(b) <- S.add a friend.(b);
        )
    done;
    for i = 1 to k do
        Scanf.scanf " %d %d" (fun c d ->
            let c = c - 1 in let d = d - 1 in
            block.(c) <- S.add d block.(c);
            block.(d) <- S.add c block.(d);
        )
    done;
    let clust = Array.make n (-1) in
    let cm    = Array.make n S.empty in
    let rec loop i cl =
        if i < n then (
            let cl = if clust.(i) <> -1 then cl else (
                let rec collect acc next = function
                    | [] -> if S.is_empty next then acc else collect acc S.empty (S.elements next)
                    | hd :: tl ->
                            let acc = S.add hd acc in
                            let next = S.union (S.diff friend.(hd) acc) next in
                            collect acc next tl
                in
                let acc = collect S.empty (S.singleton i) [] in
                S.iter (fun v -> clust.(v) <- cl) acc;
                cm.(cl) <- acc;
                cl + 1
            )
            in
            let c = S.cardinal (S.diff (S.diff cm.(clust.(i)) block.(i)) friend.(i)) in
            let () = Printf.printf "%d " (c - 1) in
            loop (i + 1) cl
        )
    in
    loop 0 0
)

Score : 400 points

Problem Statement

An SNS has N users - User 1, User 2, \cdots, User N.

Between these N users, there are some relationships - M friendships and K blockships.

For each i = 1, 2, \cdots, M, there is a bidirectional friendship between User A_i and User B_i.

For each i = 1, 2, \cdots, K, there is a bidirectional blockship between User C_i and User D_i.

We define User a to be a friend candidate for User b when all of the following four conditions are satisfied:

a \neq b.

There is not a friendship between User a and User b.

There is not a blockship between User a and User b.

There exists a sequence c_0, c_1, c_2, \cdots, c_L consisting of integers between 1 and N (inclusive) such that c_0 = a, c_L = b, and there is a friendship between User c_i and c_{i+1} for each i = 0, 1, \cdots, L - 1.

For each user i = 1, 2, ... N, how many friend candidates does it have?

Constraints

All values in input are integers.

2 ≤ N ≤ 10^5

0 \leq M \leq 10^5

0 \leq K \leq 10^5

1 \leq A_i, B_i \leq N

A_i \neq B_i

1 \leq C_i, D_i \leq N

C_i \neq D_i

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

(A_i, B_i) \neq (B_j, A_j)

(C_i, D_i) \neq (C_j, D_j) (i \neq j)

(C_i, D_i) \neq (D_j, C_j)

(A_i, B_i) \neq (C_j, D_j)

(A_i, B_i) \neq (D_j, C_j)

Input

Input is given from Standard Input in the following format:

N M K
A_1 B_1
\vdots
A_M B_M
C_1 D_1
\vdots
C_K D_K

Output

Print the answers in order, with space in between.

Sample Input 1

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

Sample Output 1

0 1 0 1

There is a friendship between User 2 and 3, and between 3 and 4. Also, there is no friendship or blockship between User 2 and 4. Thus, User 4 is a friend candidate for User 2.

However, neither User 1 or 3 is a friend candidate for User 2, so User 2 has one friend candidate.

Sample Input 2

5 10 0
1 2
1 3
1 4
1 5
3 2
2 4
2 5
4 3
5 3
4 5

Sample Output 2

0 0 0 0 0

Everyone is a friend of everyone else and has no friend candidate.

Sample Input 3

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

Sample Output 3

1 3 5 4 3 3 3 3 1 0


module S = Set.Make (struct type t = int let compare = compare end);;
let () =
    let main () =
        let n = read_line () |> int_of_string in
        let s = read_line () in
        let letter = Array.make 26 S.empty in
        for i = 0 to n - 1 do
            let code = int_of_char s.[i] - 97 in
            letter.(code) <- S.add i letter.(code)
        done;
        let q = read_int () in
        for i = 1 to q do
            Scanf.scanf " %d %d %s" (fun command a b ->
                if command = 1 then (
                    let a = a - 1 in
                    let c = int_of_char s.[a] - 97 in
                    letter.(c) <- S.remove a letter.(c);
                    let c = int_of_char b.[0] - 97 in
                    letter.(c) <- S.add a letter.(c)
                ) else (
                    let a = a - 1 in
                    let r = int_of_string b - 1 in
                    let rec loop i acc =
                        if i = 26 then acc else
                            let _,_,s = S.split (a - 1) letter.(i) in
                            let s,_,_ = S.split (r + 1) s in
                            let acc = if S.is_empty s then acc else acc + 1 in
                            loop (i + 1) acc
                    in
                    loop 0 0 |> Printf.printf "%d\n"
                )
            )
        done
    in
    main ()

Score : 500 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters.

Process Q queries of the following two types:

Type 1: change the i_q-th character of S to c_q. (Do nothing if the i_q-th character is already c_q.)

Type 2: answer the number of different characters occurring in the substring of S between the l_q-th and r_q-th characters (inclusive).

Constraints

N, Q, i_q, l_q, and r_q are integers.

S is a string consisting of lowercase English letters.

c_q is a lowercase English letter.

1 \leq N \leq 500000

1 \leq Q \leq 20000

|S| = N

1 \leq i_q \leq N

1 \leq l_q \leq r_q \leq N

There is at least one query of type 2 in each testcase.

Input

Input is given from Standard Input in the following format:

N
S
Q
Query_1
\vdots
Query_Q

Here, Query_i in the 4-th through (Q+3)-th lines is one of the following:

1 i_q c_q

2 l_q r_q

Output

For each query of type 2, print a line containing the answer.

Sample Input 1

7
abcdbbd
6
2 3 6
1 5 z
2 1 1
1 4 a
1 7 d
2 1 7

Sample Output 1

3
1
5

In the first query, cdbb contains three kinds of letters: b , c , and d, so we print 3.

In the second query, S is modified to abcdzbd.

In the third query, a contains one kind of letter: a, so we print 1.

In the fourth query, S is modified to abcazbd.

In the fifth query, S does not change and is still abcazbd.

In the sixth query, abcazbd contains five kinds of letters: a, b, c, d, and z, so we print 5.


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 ST = SegTree.Make (struct
  type t = int
  let op = ( lor )
end)

let popcnt x =
  let x = (x land 0x5555555555555555) + ((x lsr 1) land 0x5555555555555555) in
  let x = (x land 0x3333333333333333) + ((x lsr 2) land 0x3333333333333333) in
  let x = (x land 0x0f0f0f0f0f0f0f0f) + ((x lsr 4) land 0x0f0f0f0f0f0f0f0f) in
  let x = (x land 0x00ff00ff00ff00ff) + ((x lsr 8) land 0x00ff00ff00ff00ff) in
  let x = (x land 0x0000ffff0000ffff) + ((x lsr 16) land 0x0000ffff0000ffff) in
  (x land 0x00000000ffffffff) + ((x lsr 32) land 0x00000000ffffffff)

let () = Scanf.scanf "%d\n%s\n%d\n" @@ fun n s q ->
  let segtree = ref @@ ST.init n @@ fun i ->
    1 lsl (Char.code s.[i] - Char.code 'a') in
  for i = 0 to q - 1 do
    Scanf.scanf "%d " @@ function
      | 1 -> Scanf.scanf "%d %c\n" @@ fun i c ->
          segtree := ST.update (i - 1) (fun _ -> 1 lsl (Char.code c - Char.code 'a')) !segtree
      | 2 -> Scanf.scanf "%d %d\n" @@ fun l r ->
          Printf.printf "%d\n" @@ popcnt @@ ST.query (l - 1) r !segtree
  done


open Printf
open Scanf

let solve n r =
  if n >= 10 then r
  else r + 100 * (10 - n)

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

Score : 100 points

Problem Statement

Takahashi is a member of a programming competition site, ButCoder.

Each member of ButCoder is assigned two values: Inner Rating and Displayed Rating.

The Displayed Rating of a member is equal to their Inner Rating if the member has participated in 10 or more contests. Otherwise, the Displayed Rating will be their Inner Rating minus 100 \times (10 - K) when the member has participated in K contests.

Takahashi has participated in N contests, and his Displayed Rating is R. Find his Inner Rating.

Constraints

All values in input are integers.

1 \leq N \leq 100

0 \leq R \leq 4111

Input

Input is given from Standard Input in the following format:

N R

Output

Print his Inner Rating.

Sample Input 1

2 2919

Sample Output 1

3719

Takahashi has participated in 2 contests, which is less than 10, so his Displayed Rating is his Inner Rating minus 100 \times (10 - 2) = 800.

Thus, Takahashi's Inner Rating is 2919 + 800 = 3719.

Sample Input 2

22 3051

Sample Output 2

3051


let n, k = Scanf.scanf " %d %d" @@ fun a b -> a, b
let rec f p = if p > n then 1 else 1 + f (p * k)
let _ = Printf.printf "%d\n" @@ f k

Score : 200 points

Problem Statement

Given is an integer N. Find the number of digits that N has in base K.

Notes

For information on base-K representation, see Positional notation - Wikipedia.

Constraints

All values in input are integers.

1 \leq N \leq 10^9

2 \leq K \leq 10

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of digits that N has in base K.

Sample Input 1

11 2

Sample Output 1

4

In binary, 11 is represented as 1011.

Sample Input 2

1010101 10

Sample Output 2

7

Sample Input 3

314159265 3

Sample Output 3

18


let () = Scanf.scanf "%d %d" @@ fun n k ->
  Printf.printf "%d\n" @@ succ @@ int_of_float @@ log (float_of_int n) /. log (float_of_int k)


open Batteries
open BatPrintf

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

let scan_list cnv =
  read_line ()
  |> String.split_on_char ' '
  |> List.map cnv

let scan_listn n cnv =
  (0 --^ n) |> Enum.map (fun _ -> (cnv % read_line) ())

let bsearch_ge arr a =
  let l = Array.length arr in
  match Array.bsearch Int.ord arr a with
  | `All_lower -> l
  | `All_bigger -> 0
  | `Just_after n -> l - n -1
  | `At n -> l - n
  | `Empty -> 0

let rec zip xs ys =
  match xs, ys with
  | [], _ -> []
  | _, [] -> []
  | x::xs, y::ys ->
    (x, y) :: zip xs ys

let square x = x * x

let n = scan "%d" identity
let xs = scan_list Int.of_string

let () =
  let half = (List.sum xs) / n in
  let half2 = (List.sum xs) / n + 1 in
  let n = List.sum @@ List.map (fun n -> square @@ n - half) xs in
  let m = List.sum @@ List.map (fun n -> square @@ n - half2) xs in
  printf "%d" @@ min n m

Score : 300 points

Problem Statement

There are N people living on a number line.

The i-th person lives at coordinate X_i.

You are going to hold a meeting that all N people have to attend.

The meeting can be held at any integer coordinate. If you choose to hold the meeting at coordinate P, the i-th person will spend (X_i - P)^2 points of stamina to attend the meeting.

Find the minimum total points of stamina the N people have to spend.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq X_i \leq 100

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 ... X_N

Output

Print the minimum total stamina the N people have to spend.

Sample Input 1

2
1 4

Sample Output 1

5

Assume the meeting is held at coordinate 2. In this case, the first person will spend (1 - 2)^2 points of stamina, and the second person will spend (4 - 2)^2 = 4 points of stamina, for a total of 5 points of stamina. This is the minimum total stamina that the 2 people have to spend.

Note that you can hold the meeting only at an integer coordinate.

Sample Input 2

7
14 14 2 13 56 2 37

Sample Output 2

2354


open Batteries
open BatPrintf

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

let scan_list cnv =
  read_line ()
  |> String.split_on_char ' '
  |> List.map cnv

let scan_listn n cnv =
  (0 --^ n) |> Enum.map (fun _ -> (cnv % read_line) ())

let bsearch_ge arr a =
  let l = Array.length arr in
  match Array.bsearch Int.ord arr a with
  | `All_lower -> l
  | `All_bigger -> 0
  | `Just_after n -> l - n -1
  | `At n -> l - n
  | `Empty -> 0

let rec zip xs ys =
  match xs, ys with
  | [], _ -> []
  | _, [] -> []
  | x::xs, y::ys ->
    (x, y) :: zip xs ys

let square x = x * x

let dbg n = Printf.printf "%s\n" @@ dump n; n

module Comb = struct
  let prime = 1_000_000_000+7

  let ( * ) a b = a * b mod prime
  let (-) a b = (a+prime - b) mod prime

  let rec pow n m =
    if m = 0 then 1
    else if m mod 2 = 0 then pow (n*n mod prime) (m/2)
    else n * pow (n*n mod prime) (m/2) mod prime

  let fact n =
    let rec aux m ret =
      if m = 0 then ret
      else aux (m-1) @@ (m*ret) mod prime in
    aux n 1

  let comb n k =
    let x = Enum.fold ( * ) 1 (n-k+1 -- n) in
    x * (pow (fact k) (prime - 2))
end

let (n, a, b) = scan "%d %d %d" Tuple.Tuple3.make

let () =
  let open Comb in
  pow 2 n
  |> (fun x -> (-) x 1)
  |> (fun x -> (-) x @@ comb n a)
  |> (fun x -> (-) x @@ comb n b)
  |> printf "%d\n"

Score : 400 points

Problem Statement

Akari has n kinds of flowers, one of each kind.

She is going to choose one or more of these flowers to make a bouquet.

However, she hates two numbers a and b, so the number of flowers in the bouquet cannot be a or b.

How many different bouquets are there that Akari can make?

Find the count modulo (10^9 + 7).

Here, two bouquets are considered different when there is a flower that is used in one of the bouquets but not in the other bouquet.

Constraints

All values in input are integers.

2 \leq n \leq 10^9

1 \leq a < b \leq \textrm{min}(n, 2 \times 10^5)

Input

Input is given from Standard Input in the following format:

n a b

Output

Print the number of bouquets that Akari can make, modulo (10^9 + 7). (If there are no such bouquets, print 0.)

Sample Input 1

4 1 3

Sample Output 1

7

In this case, Akari can choose 2 or 4 flowers to make the bouquet.

There are 6 ways to choose 2 out of the 4 flowers, and 1 way to choose 4, so there are a total of 7 different bouquets that Akari can make.

Sample Input 2

1000000000 141421 173205

Sample Output 2

34076506

Print the count modulo (10^9 + 7).


open Batteries
open BatPrintf

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

let scan_list cnv =
  read_line ()
  |> String.split_on_char ' '
  |> List.map cnv

let scan_listn n cnv =
  (0 --^ n) |> Enum.map (fun _ -> (cnv % read_line) ())

let bsearch_ge arr a =
  let l = Array.length arr in
  match Array.bsearch Int.ord arr a with
  | `All_lower -> l
  | `All_bigger -> 0
  | `Just_after n -> l - n -1
  | `At n -> l - n
  | `Empty -> 0

let rec zip xs ys =
  match xs, ys with
  | [], _ -> []
  | _, [] -> []
  | x::xs, y::ys ->
    (x, y) :: zip xs ys

let square x = x * x

let dbg n =
  Printf.printf "%s\n" @@ dump n; n

module CombMemo = struct
  let prime = 1_000_000_000+7
  let size = 2*1_00_000

  let memo = let arr = Array.make size None in
    arr.(0) <- Some 1;
    arr

  let ( * ) a b = a * b mod prime
  let (-) a b = (a+prime - b) mod prime

  let rec pow n m =
    if m = 0 then 1
    else if m mod 2 = 0 then pow (n*n) (m/2)
    else n * pow (n*n) (m/2)

  let rec fact n =
    match memo.(n) with
    | None ->
      let m = n * fact (n-1) in
      memo.(n) <- Some m;
      m
    | Some a -> a

  let comb n k =
    fact n * (pow (fact k) (prime-2)) * (pow (fact (n - k)) (prime-2))
end

module Comb = struct
  let prime = 1_000_000_000+7

  let ( * ) a b = a * b mod prime
  let (-) a b = (a+prime - b) mod prime

  let rec pow n m =
    if m = 0 then 1
    else if m mod 2 = 0 then pow (n*n mod prime) (m/2)
    else n * pow (n*n mod prime) (m/2) mod prime

  let fact n =
    let rec aux m ret =
      if m = 0 then ret
      else aux (m-1) @@ (m*ret) mod prime in
    aux n 1

  let comb n k =
    let x = Enum.fold ( * ) 1 (n-k+1 -- n) in
    x * (pow (fact k) (prime - 2))
end

let (n, a, b) = scan "%d %d %d" Tuple.Tuple3.make

let () =
  let open Comb in
  pow 2 n
  |> (fun x -> (-) x 1)
  |> (fun x -> (-) x @@ comb n a)
  |> (fun x -> (-) x @@ comb n b)
  |> printf "%d\n"


Scanf.scanf "%d %d" (fun n k ->
    let m = 1_000_000_007 in

    let extgcd x y =
        let rec eg r0 r1 a0 a1 b0 b1 =
            if r1 = 0 then a0, b0 else
                let q1 = r0 / r1 in
                eg r1 (r0 mod r1) a1 (a0 - q1 * a1) b1 (b0 - q1 * b1)
        in
        eg x y 1 0 0 1
    in

    let inv x y = (* 1/x in mod y *)
        let c, d = extgcd x y in
        if c < 0 then c + y else c
    in
    let ( +@) a b = (a + b) mod m in
    let ( -@) a b = (((a - b) mod m) + m) mod m in
    let ( *@) a b = ((a mod m) * (b mod m)) mod m in
    let ( /@) a b = a *@ inv b m in
    let rec loop i r1 r2 a b acc =
        if i > n || i > k then acc else (
            let acc = acc +@ r1 *@ r2 in
            let r1 = r1 *@ a /@ b in
            let r2 = r2 *@ (a - 1) /@ b in
            let a = a - 1 in
            let b = b + 1 in
            loop (i + 1) r1 r2 a b acc
        )
    in
    loop 0 1 1 n 1 0 |> Printf.printf "%d\n"
)

Score : 500 points

Problem Statement

There is a building with n rooms, numbered 1 to n.

We can move from any room to any other room in the building.

Let us call the following event a move: a person in some room i goes to another room j~ (i \neq j).

Initially, there was one person in each room in the building.

After that, we know that there were exactly k moves happened up to now.

We are interested in the number of people in each of the n rooms now. How many combinations of numbers of people in the n rooms are possible?

Find the count modulo (10^9 + 7).

Constraints

All values in input are integers.

3 \leq n \leq 2 \times 10^5

2 \leq k \leq 10^9

Input

Input is given from Standard Input in the following format:

n k

Output

Print the number of possible combinations of numbers of people in the n rooms now, modulo (10^9 + 7).

Sample Input 1

3 2

Sample Output 1

10

Let c_1, c_2, and c_3 be the number of people in Room 1, 2, and 3 now, respectively. There are 10 possible combination of (c_1, c_2, c_3):

(0, 0, 3)

(0, 1, 2)

(0, 2, 1)

(0, 3, 0)

(1, 0, 2)

(1, 1, 1)

(1, 2, 0)

(2, 0, 1)

(2, 1, 0)

(3, 0, 0)

For example, (c_1, c_2, c_3) will be (0, 1, 2) if the person in Room 1 goes to Room 2 and then one of the persons in Room 2 goes to Room 3.

Sample Input 2

200000 1000000000

Sample Output 2

607923868

Print the count modulo (10^9 + 7).

Sample Input 3

15 6

Sample Output 3

22583772


open Batteries
open BatPrintf

let scan fmt f =
  BatScanf.sscanf (read_line ()) fmt f

let scan_list cnv =
  read_line ()
  |> BatString.split_on_char ' '
  |> List.map cnv

let rec zip xs ys =
  match xs, ys with
  | [], _ -> []
  | _, [] -> []
  | x::xs, y::ys ->
    (x, y) :: zip xs ys

let (a,b,c)= scan "%d %d %d" @@ BatTuple.Tuple3.make

let () =
  (if (a != b &&  b != c && b != c) || ( a == b && b == c && c == a) then
     "No"
   else
     "Yes")
  |> printf "%s\n"

Score: 100 points

Problem Statement

A triple of numbers is said to be poor when two of those numbers are equal but the other number is different from those two numbers.

You will be given three integers A, B, and C. If this triple is poor, print Yes; otherwise, print No.

Constraints

A, B, and C are all integers between 1 and 9 (inclusive).

Input

Input is given from Standard Input in the following format:

A B C

Output

If the given triple is poor, print Yes; otherwise, print No.

Sample Input 1

5 7 5

Sample Output 1

Yes

A and C are equal, but B is different from those two numbers, so this triple is poor.

Sample Input 2

4 4 4

Sample Output 2

No

A, B, and C are all equal, so this triple is not poor.

Sample Input 3

4 9 6

Sample Output 3

No

Sample Input 4

3 3 4

Sample Output 4

Yes


let () =
	let n = Scanf.scanf "%d\n" (fun x -> x) in
    let lst = Array.to_list (Array.init n (fun _ -> Scanf.scanf "%d " (fun x -> x))) in
    Printf.printf
    	(if (List.for_all (fun x -> (x mod 2 = 1) || ((x mod 2 = 0) && (x mod 3 = 0 || x mod 5 = 0))) lst)
         then "APPROVED" else "DENIED")

Score: 200 points

Problem Statement

You are an immigration officer in the Kingdom of AtCoder. The document carried by an immigrant has some number of integers written on it, and you need to check whether they meet certain criteria.

According to the regulation, the immigrant should be allowed entry to the kingdom if and only if the following condition is satisfied:

All even numbers written on the document are divisible by 3 or 5.

If the immigrant should be allowed entry according to the regulation, output APPROVED; otherwise, print DENIED.

Notes

The condition in the statement can be rephrased as "If x is an even number written on the document, x is divisible by 3 or 5".
Here "if" and "or" are logical terms.

Constraints

All values in input are integers.

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 \dots A_N

Output

If the immigrant should be allowed entry according to the regulation, print APPROVED; otherwise, print DENIED.

Sample Input 1

5
6 7 9 10 31

Sample Output 1

APPROVED

The even numbers written on the document are 6 and 10.

All of them are divisible by 3 or 5, so the immigrant should be allowed entry.

Sample Input 2

3
28 27 24

Sample Output 2

DENIED

28 violates the condition, so the immigrant should not be allowed entry.


open Batteries
open BatPrintf

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

let scan_list cnv =
  read_line ()
  |> String.split_on_char ' '
  |> List.map cnv

let scan_listn n cnv =
  (0 --^ n) /@ (fun _ -> (cnv % read_line) ())

let rec zip xs ys =
  match xs, ys with
  | [], _ -> []
  | _, [] -> []
  | x::xs, y::ys ->
    (x, y) :: zip xs ys

let n = scan "%d" identity
let s = scan_listn n identity

let () =
  let assoc =
    fold (fun assoc k -> List.modify_opt k
             (function
               | None -> Some 0
               | Some n -> Some (n + 1)) assoc) [] s in
  let m = List.fold_left (fun m x -> if m < (Tuple.Tuple2.second x) then (Tuple.Tuple2.second x) else m) 0 assoc in
  List.filter (fun x -> (Tuple.Tuple2.second x) = m ) assoc
  |> List.map Tuple.Tuple2.first
  |> List.sort String.compare
  |> String.concat "\n"
  |> printf "%s\n"

Score: 300 points

Problem Statement

We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.

Print all strings that are written on the most number of votes, in lexicographical order.

Constraints

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

S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.

The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

Print all strings in question in lexicographical order.

Sample Input 1

7
beat
vet
beet
bed
vet
bet
beet

Sample Output 1

beet
vet

beet and vet are written on two sheets each, while beat, bed, and bet are written on one vote each. Thus, we should print the strings beet and vet.

Sample Input 2

8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo

Sample Output 2

buffalo

Sample Input 3

7
bass
bass
kick
kick
bass
kick
kick

Sample Output 3

kick

Sample Input 4

4
ushi
tapu
nichia
kun

Sample Output 4

kun
nichia
tapu
ushi


let st = Scanf.sscanf (read_line ()) "%s %s" (fun s t -> [s;t])
let ab = Scanf.sscanf (read_line ()) "%d %d" (fun a b -> [a;b])
let u = read_line ()
let _ =
  match u with
  | u when u = (List.hd st) -> Printf.printf "%d %d\n" ((List.hd ab) - 1) (List.nth ab 1)
  | _ -> Printf.printf "%d %d\n" (List.hd ab) ((List.nth ab 1) - 1)

Score : 100 points

Problem Statement

We have A balls with the string S written on each of them and B balls with the string T written on each of them.

From these balls, Takahashi chooses one with the string U written on it and throws it away.

Find the number of balls with the string S and balls with the string T that we have now.

Constraints

S, T, and U are strings consisting of lowercase English letters.

The lengths of S and T are each between 1 and 10 (inclusive).

S \not= T

S=U or T=U.

1 \leq A,B \leq 10

A and B are integers.

Input

Input is given from Standard Input in the following format:

S T
A B
U

Output

Print the answer, with space in between.

Sample Input 1

red blue
3 4
red

Sample Output 1

2 4

Takahashi chose a ball with red written on it and threw it away.
Now we have two balls with the string S and four balls with the string T.

Sample Input 2

red blue
5 5
blue

Sample Output 2

5 4

Takahashi chose a ball with blue written on it and threw it away.
Now we have five balls with the string S and four balls with the string T.


let () =
  Scanf.scanf "%d\n" @@ fun n ->
  let lst = Array.to_list (Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun d -> d) in
  let ans = (List.length lst) = List.length (List.sort_uniq compare lst) in
  Printf.printf "%s\n" (if ans then "YES" else "NO")

Score : 300 points

Problem Statement

Given is a sequence of integers A_1, A_2, ..., A_N.
If its elements are pairwise distinct, print YES; otherwise, print NO.

Constraints

2 ≤ N ≤ 200000

1 ≤ A_i ≤ 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

If the elements of the sequence are pairwise distinct, print YES; otherwise, print NO.

Sample Input 1

5
2 6 1 4 5

Sample Output 1

YES

The elements are pairwise distinct.

Sample Input 2

6
4 1 3 1 6 2

Sample Output 2

NO

The second and fourth elements are identical.

Sample Input 3

2
10000000 10000000

Sample Output 3

NO


let split_string ?(pattern=" ") = Str.split @@ Str.regexp pattern
let (n, k) = Scanf.sscanf (read_line ()) "%d %d" @@ fun n k -> (n, k)

let p = read_line () |> split_string |> List.map int_of_string |> Array.of_list
  |> Array.map (fun i -> float (i * (i + 1)) /. float (i * 2))

let rec loop i s m =
  if i = n then Printf.printf "%f\n" m
  else
    let s = s +. p.(i) -. if i < k then 0. else p.(i - k) in
    loop (i + 1) s (max s m)

let () = loop 0 0. 0.

Score : 400 points

Problem Statement

We have N dice arranged in a line from left to right. The i-th die from the left shows p_i numbers from 1 to p_i with equal probability when thrown.

We will choose K adjacent dice, throw each of them independently, and compute the sum of the numbers shown. Find the maximum possible value of the expected value of this sum.

Constraints

1 ≤ K ≤ N ≤ 200000

1 ≤ p_i ≤ 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
p_1 ... p_N

Output

Print the maximum possible value of the expected value of the sum of the numbers shown.

Your output will be considered correct when its absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

5 3
1 2 2 4 5

Sample Output 1

7.000000000000

When we throw the third, fourth, and fifth dice from the left, the expected value of the sum of the numbers shown is 7. This is the maximum value we can achieve.

Sample Input 2

4 1
6 6 6 6

Sample Output 2

3.500000000000

Regardless of which die we choose, the expected value of the number shown is 3.5.

Sample Input 3

10 4
17 13 13 12 15 20 10 13 17 11

Sample Output 3

32.000000000000


Scanf.scanf "%d %d %d %d" (fun r1 c1 r2 c2 ->
    let m = 1_000_000_007 in

    let extgcd x y =
        let rec eg r0 r1 a0 a1 b0 b1 =
            if r1 = 0 then a0 else
                let q1 = r0 / r1 in
                eg r1 (r0 mod r1) a1 (a0 - q1 * a1) b1 (b0 - q1 * b1)
        in
        eg x y 1 0 0 1
    in
    let inv x y = (* 1/x in mod y *)
        let c = extgcd x y in
        if c < 0 then c + y else c
    in
    let ( */) a b = ((a mod m) * (b mod m) ) mod m in
    let ( //) a b = a */ inv b m in
    let ( +/) a b = (a + b) mod m in
    let ( -/) a b = (((a - b) mod m) + m) mod m in

    let wholesum r c =
        let rec loop cur acc a b i =
            if i > c then acc else loop (cur */ a // b) (acc + cur) (a + 1) (b + 1) (i + 1)
        in
        loop (r + 1) 0 (r + 2) 2 0
    in
    let ans = wholesum r2 c2 -/ wholesum (r1 - 1) c2 -/ wholesum r2 (c1 - 1) +/ wholesum (r1 - 1) (c1 - 1) in
    Printf.printf "%d\n" ans
)

Score : 600 points

Problem Statement

Snuke is standing on a two-dimensional plane. In one operation, he can move by 1 in the positive x-direction, or move by 1 in the positive y-direction.

Let us define a function f(r, c) as follows:

f(r,c) := (The number of paths from the point (0, 0) to the point (r, c) that Snuke can trace by repeating the operation above)

Given are integers r_1, r_2, c_1, and c_2.
Find the sum of f(i, j) over all pair of integers (i, j) such that r_1 ≤ i ≤ r_2 and c_1 ≤ j ≤ c_2, and compute this value modulo (10^9+7).

Constraints

1 ≤ r_1 ≤ r_2 ≤ 10^6

1 ≤ c_1 ≤ c_2 ≤ 10^6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

r_1 c_1 r_2 c_2

Output

Print the sum of f(i, j) modulo (10^9+7).

Sample Input 1

1 1 2 2

Sample Output 1

14

For example, there are two paths from the point (0, 0) to the point (1, 1): (0,0) → (0,1) → (1,1) and (0,0) → (1,0) → (1,1), so f(1,1)=2.

Similarly, f(1,2)=3, f(2,1)=3, and f(2,2)=6. Thus, the sum is 14.

Sample Input 2

314 159 2653 589

Sample Output 2

602215194


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

Score : 100 points

Problem Statement

Serval is fighting with a monster.

The health of the monster is H.

In one attack, Serval can decrease the monster's health by A.
There is no other way to decrease the monster's health.

Serval wins when the monster's health becomes 0 or below.

Find the number of attacks Serval needs to make before winning.

Constraints

1 \leq H \leq 10^4

1 \leq A \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H A

Output

Print the number of attacks Serval needs to make before winning.

Sample Input 1

10 4

Sample Output 1

3

After one attack, the monster's health will be 6.

After two attacks, the monster's health will be 2.

After three attacks, the monster's health will be -2.

Thus, Serval needs to make three attacks to win.

Sample Input 2

1 10000

Sample Output 2

1

Sample Input 3

10000 1

Sample Output 3

10000


Scanf.scanf "%d %d" (fun h n ->
    let a = Array.init n (fun _ -> Scanf.scanf " %d" (fun a -> a)) in
    let s = Array.fold_left (+) 0 a in
    (if s >= h then "Yes" else "No") |> print_endline
)

Score : 200 points

Problem Statement

Raccoon is fighting with a monster.

The health of the monster is H.

Raccoon can use N kinds of special moves. Using the i-th move decreases the monster's health by A_i.
There is no other way to decrease the monster's health.

Raccoon wins when the monster's health becomes 0 or below.

If Raccoon can win without using the same move twice or more, print Yes; otherwise, print No.

Constraints

1 \leq H \leq 10^9

1 \leq N \leq 10^5

1 \leq A_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H N
A_1 A_2 ... A_N

Output

If Raccoon can win without using the same move twice or more, print Yes; otherwise, print No.

Sample Input 1

10 3
4 5 6

Sample Output 1

Yes

The monster's health will become 0 or below after, for example, using the second and third moves.

Sample Input 2

20 3
4 5 6

Sample Output 2

No

Sample Input 3

210 5
31 41 59 26 53

Sample Output 3

Yes

Sample Input 4

211 5
31 41 59 26 53

Sample Output 4

No


open Batteries
let n, k = Scanf.sscanf (read_line ()) "%d %d" (fun n k -> n, k)
let h = read_line () |> String.split_on_char ' ' |> List.map int_of_string

let rec loop lst i sum =
  match lst with
  | [] -> sum
  | first :: rest ->
    if i <> 0 then min (loop rest (i-1) sum) (loop rest i (sum+first))
    else loop rest i (sum+first)

let _ = Printf.printf "%d\n" @@ loop h k 0

Score : 300 points

Problem Statement

Fennec is fighting with N monsters.

The health of the i-th monster is H_i.

Fennec can do the following two actions:

Attack: Fennec chooses one monster. That monster's health will decrease by 1.

Special Move: Fennec chooses one monster. That monster's health will become 0.

There is no way other than Attack and Special Move to decrease the monsters' health.

Fennec wins when all the monsters' healths become 0 or below.

Find the minimum number of times Fennec needs to do Attack (not counting Special Move) before winning when she can use Special Move at most K times.

Constraints

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

0 \leq K \leq 2 \times 10^5

1 \leq H_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
H_1 ... H_N

Output

Print the minimum number of times Fennec needs to do Attack (not counting Special Move) before winning.

Sample Input 1

3 1
4 1 5

Sample Output 1

5

By using Special Move on the third monster, and doing Attack four times on the first monster and once on the second monster, Fennec can win with five Attacks.

Sample Input 2

8 9
7 9 3 2 3 8 4 6

Sample Output 2

0

She can use Special Move on all the monsters.

Sample Input 3

3 0
1000000000 1000000000 1000000000

Sample Output 3

3000000000

Watch out for overflow.


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

let h = Scanf.sscanf (read_line ()) "%d" (
        fun h ->
                h
)

let i = ref 0

let rec f h =
        if h = 1 then
                1
        else
                2 * (f (h / 2)) + 1

let () =
        f h
        |> Printf.printf "%d\n"

Score : 400 points

Problem Statement

Caracal is fighting with a monster.

The health of the monster is H.

Caracal can attack by choosing one monster. When a monster is attacked, depending on that monster's health, the following happens:

If the monster's health is 1, it drops to 0.

If the monster's health, X, is greater than 1, that monster disappears. Then, two new monsters appear, each with the health of \lfloor X/2 \rfloor.

(\lfloor r \rfloor denotes the greatest integer not exceeding r.)

Caracal wins when the healths of all existing monsters become 0 or below.

Find the minimum number of attacks Caracal needs to make before winning.

Constraints

1 \leq H \leq 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H

Output

Find the minimum number of attacks Caracal needs to make before winning.

Sample Input 1

2

Sample Output 1

3

When Caracal attacks the initial monster, it disappears, and two monsters appear, each with the health of 1.

Then, Caracal can attack each of these new monsters once and win with a total of three attacks.

Sample Input 2

4

Sample Output 2

7

Sample Input 3

1000000000000

Sample Output 3

1099511627775


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

let () =
  let ans = Array.make (h + 1) max_int in
  ans.(0) <- 0;
  for i = 1 to h do
    Array.iter (fun (a, b) -> ans.(i) <- min ans.(i) (ans.(max 0 (i - a)) + b)) ab
  done;
  Printf.printf "%d\n" ans.(h)

Score : 500 points

Problem Statement

Ibis is fighting with a monster.

The health of the monster is H.

Ibis can cast N kinds of spells. Casting the i-th spell decreases the monster's health by A_i, at the cost of B_i Magic Points.

The same spell can be cast multiple times. There is no way other than spells to decrease the monster's health.

Ibis wins when the health of the monster becomes 0 or below.

Find the minimum total Magic Points that have to be consumed before winning.

Constraints

1 \leq H \leq 10^4

1 \leq N \leq 10^3

1 \leq A_i \leq 10^4

1 \leq B_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H N
A_1 B_1
:
A_N B_N

Output

Print the minimum total Magic Points that have to be consumed before winning.

Sample Input 1

9 3
8 3
4 2
2 1

Sample Output 1

4

First, let us cast the first spell to decrease the monster's health by 8, at the cost of 3 Magic Points. The monster's health is now 1.

Then, cast the third spell to decrease the monster's health by 2, at the cost of 1 Magic Point. The monster's health is now -1.

In this way, we can win at the total cost of 4 Magic Points.

Sample Input 2

100 6
1 1
2 3
3 9
4 27
5 81
6 243

Sample Output 2

100

It is optimal to cast the first spell 100 times.

Sample Input 3

9999 10
540 7550
691 9680
700 9790
510 7150
415 5818
551 7712
587 8227
619 8671
588 8228
176 2461

Sample Output 3

139815


open Printf
open Scanf

let solve n m = if n = m then "Yes" else "No"

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

Score : 100 points

Problem Statement

Takahashi is participating in a programming contest, AXC001. He has just submitted his code to Problem A.

The problem has N test cases, all of which must be passed to get an AC verdict.

Takahashi's submission has passed M cases out of the N test cases.

Determine whether Takahashi's submission gets an AC.

Constraints

1 \leq N \leq 100

0 \leq M \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

If Takahashi's submission gets an AC, print Yes; otherwise, print No.

Sample Input 1

3 3

Sample Output 1

Yes

All three test cases have been passed, so his submission gets an AC.

Sample Input 2

3 2

Sample Output 2

No

Only two out of the three test cases have been passed, so his submission does not get an AC.

Sample Input 3

1 1

Sample Output 3

Yes


let () = Scanf.scanf "%d %d" @@ fun n m ->
  print_endline @@ if n = m then "Yes" else "No"


Scanf.scanf "%d %d" (fun a b ->
    let digits = "0123456789" in
    let sa = String.make a digits.[b] in
    let sb = String.make b digits.[a] in
    print_endline (min sa sb)
)

Score : 200 points

Problem Statement

Given are 1-digit positive integers a and b. Consider these two strings: the concatenation of b copies of the digit a, and the concatenation of a copies of the digit b. Which of these is lexicographically smaller?

Constraints

1 \leq a \leq 9

1 \leq b \leq 9

a and b are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

Print the lexicographically smaller of the two strings. (If the two strings are equal, print one of them.)

Sample Input 1

4 3

Sample Output 1

3333

We have two strings 444 and 3333. Between them, 3333 is the lexicographically smaller.

Sample Input 2

7 7

Sample Output 2

7777777


let f a b = String.make b (Char.(chr @@ code '0' + a))
let _ = Scanf.scanf "%d %d" @@ fun a b -> print_endline @@ min (f a b) (f b a)


let () =
  Scanf.scanf "%d\n" @@ fun n ->
  let prr = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun d -> d in
  let p = Array.fold_left (fun (minN, ans) x -> if minN >= x then (x, ans+1) else (minN, ans)) (prr.(0), 0) prr in
  Printf.printf "%d\n" (snd p)

Score : 300 points

Problem Statement

Given is a permutation P_1, \ldots, P_N of 1, \ldots, N.
Find the number of integers i (1 \leq i \leq N) that satisfy the following condition:

For any integer j (1 \leq j \leq i), P_i \leq P_j.

Constraints

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

P_1, \ldots, P_N is a permutation of 1, \ldots, N.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 ... P_N

Output

Print the number of integers i that satisfy the condition.

Sample Input 1

5
4 2 5 1 3

Sample Output 1

3

i=1, 2, and 4 satisfy the condition, but i=3 does not - for example, P_i > P_j holds for j = 1.

Similarly, i=5 does not satisfy the condition, either. Thus, there are three integers that satisfy the condition.

Sample Input 2

4
4 3 2 1

Sample Output 2

4

All integers i (1 \leq i \leq N) satisfy the condition.

Sample Input 3

6
1 2 3 4 5 6

Sample Output 3

1

Only i=1 satisfies the condition.

Sample Input 4

8
5 7 4 2 6 8 1 3

Sample Output 4

4

Sample Input 5

1
1

Sample Output 5

1


open Printf
open Scanf

let id x = x

let solve n =
  let ar = Array.init n @@ fun _ -> scanf "%d " id in
  let f (ct, mn) x = if mn > x then (ct + 1, x) else (ct, mn) in
  fst @@ Array.fold_left f (0, 300000) ar

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


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

let f a b =
  if a = 0 then 0
  else begin
    let res = ref 0 in
    for i = 1 to n do
      let t = (int_of_char (string_of_int i).[0]) - 48 in
      if t = a && i mod 10 = b then res := !res + 1
    done;
    !res
  end

let () =
  let ans = ref 0 in
  for i = 0 to 9 do
    for j = 0 to 9 do
      ans := !ans + f i j * f j i
    done
  done;
  Printf.printf "%d\n" !ans

Score : 400 points

Problem Statement

Given is a positive integer N.

Find the number of pairs (A, B) of positive integers not greater than N that satisfy the following condition:

When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B.

Constraints

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

25

Sample Output 1

17

The following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21), (21,12), (22,2), and (22,22).

Sample Input 2

1

Sample Output 2

1

Sample Input 3

100

Sample Output 3

108

Sample Input 4

2020

Sample Output 4

40812

Sample Input 5

200000

Sample Output 5

400000008


let () =
    let main () =
        let n = int_of_string (read_line ()) in
        let xl = Array.init n (fun _ ->
            Scanf.scanf " %d %d" (fun x l -> x, l))
        in
        let l = Array.fold_left (fun acc (x, l) -> (x - l, x + l) :: acc) [] xl in
        let l = List.sort compare l in
        let k = Array.make (n + 1) 1_000_000_001 in
        let () = k.(0) <- -1_000_000_001 in
        let rec loop m = function
            | [] -> m
            | (left, right) :: tl ->
                let rec loop2 m mj =
                    if mj < 0 then m else
                        let m = if k.(mj) <= left then (
                            let () = k.(mj + 1) <- min right k.(mj + 1) in
                            max m (mj + 1)
                        ) else m
                        in
                        loop2 m (mj - 1)
                in
                let m = loop2 m m in
                loop m tl
        in
        loop 0 l |> Printf.printf "%d\n"
    in
    main ()

Score : 200 points

Problem Statement

In a factory, there are N robots placed on a number line.
Robot i is placed at coordinate X_i and can extend its arms of length L_i in both directions, positive and negative.

We want to remove zero or more robots so that the movable ranges of arms of no two remaining robots intersect.
Here, for each i (1 \leq i \leq N), the movable range of arms of Robot i is the part of the number line between the coordinates X_i - L_i and X_i + L_i, excluding the endpoints.

Find the maximum number of robots that we can keep.

Constraints

1 \leq N \leq 100,000

0 \leq X_i \leq 10^9 (1 \leq i \leq N)

1 \leq L_i \leq 10^9 (1 \leq i \leq N)

If i \neq j, X_i \neq X_j.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 L_1
X_2 L_2
\vdots
X_N L_N

Output

Print the maximum number of robots that we can keep.

Sample Input 1

4
2 4
4 3
9 3
100 5

Sample Output 1

3

By removing Robot 2, we can keep the other three robots.

Sample Input 2

2
8 20
1 10

Sample Output 2

1

Sample Input 3

5
10 1
2 1
4 1
6 1
8 1

Sample Output 3

5


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

let () =
  Array.concat [Array.make k s; Array.make (n - k) (if s = 1000000000 then 1 else s + 1)]
  |> Array.map string_of_int
  |> Array.to_list
  |> String.concat " "
  |> print_endline

Score : 400 points

Problem Statement

Given are three integers N, K, and S.

Find a sequence A_1, A_2, ..., A_N of N integers between 1 and 10^9 (inclusive) that satisfies the condition below.
We can prove that, under the conditions in Constraints, such a sequence always exists.

There are exactly K pairs (l, r) of integers such that 1 \leq l \leq r \leq N and A_l + A_{l + 1} + \cdots + A_r = S.

Constraints

1 \leq N \leq 10^5

0 \leq K \leq N

1 \leq S \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K S

Output

Print a sequence satisfying the condition, in the following format:

A_1 A_2 ... A_N

Sample Input 1

4 2 3

Sample Output 1

1 2 3 4

Two pairs (l, r) = (1, 2) and (3, 3) satisfy the condition in the statement.

Sample Input 2

5 3 100

Sample Output 2

50 50 50 30 70


Scanf.scanf "%d %d %d" @@ fun n k s -> Array.(append (make k s) @@ make (n - k) (s + if s = 1000000000 then -1 else 1) |> to_list |> List.map string_of_int |> String.concat " " |> print_endline)


let c = (read_line ()).[0] |> Char.code
let _ = print_char (Char.chr (c+1))

Score : 100 points

Problem Statement

Given is a lowercase English letter C that is not z. Print the letter that follows C in alphabetical order.

Constraints

C is a lowercase English letter that is not z.

Input

Input is given from Standard Input in the following format:

C

Output

Print the letter that follows C in alphabetical order.

Sample Input 1

a

Sample Output 1

b

a is followed by b.

Sample Input 2

y

Sample Output 2

z

y is followed by z.


let () = Scanf.scanf "%d %d\n" @@ fun n m ->
  let pss = Array.init m @@ fun _ -> Scanf.scanf "%d %s\n" @@ fun p s -> p, s in
  let ac = Array.make n false in
  let wa = Array.make n 0 in
  Array.iter (fun (p, s) ->
    if not ac.(p - 1) then begin
      if s = "AC"
      then ac.(p - 1) <- true
      else wa.(p - 1) <- 1 + wa.(p - 1)
    end) pss;
  Printf.printf "%d %d\n"
  (Array.fold_right (fun b -> ( + ) @@ if b then 1 else 0) ac 0) @@
  Array.fold_left ( + ) 0 @@
  Array.init n @@ fun i -> if ac.(i) then wa.(i) else 0

Score : 300 points

Problem Statement

Takahashi participated in a contest on AtCoder.

The contest had N problems.

Takahashi made M submissions during the contest.

The i-th submission was made for the p_i-th problem and received the verdict S_i (AC or WA).

The number of Takahashi's correct answers is the number of problems on which he received an AC once or more.

The number of Takahashi's penalties is the sum of the following count for the problems on which he received an AC once or more: the number of WAs received before receiving an AC for the first time on that problem.

Find the numbers of Takahashi's correct answers and penalties.

Constraints

N, M, and p_i are integers.

1 \leq N \leq 10^5

0 \leq M \leq 10^5

1 \leq p_i \leq N

S_i is AC or WA.

Input

Input is given from Standard Input in the following format:

N M
p_1 S_1
:
p_M S_M

Output

Print the number of Takahashi's correct answers and the number of Takahashi's penalties.

Sample Input 1

2 5
1 WA
1 AC
2 WA
2 AC
2 WA

Sample Output 1

2 2

In his second submission, he received an AC on the first problem for the first time. Before this, he received one WA on this problem.

In his fourth submission, he received an AC on the second problem for the first time. Before this, he received one WA on this problem.

Thus, he has two correct answers and two penalties.

Sample Input 2

100000 3
7777 AC
7777 AC
7777 AC

Sample Output 2

1 0

Note that it is pointless to get an AC more than once on the same problem.

Sample Input 3

6 0

Sample Output 3

0 0


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

let () =
    let main () =
        let h, w = Scanf.sscanf (read_line ()) "%d %d" (fun h w -> h, w) in
        let s = Array.init h (fun _ -> read_line ()) in
        let dfs x y =
            let m = Array.make_matrix h w (-1) in
            let rec loop depth next = function
                | [] -> if S.is_empty next then depth else loop (depth + 1) S.empty (S.elements next)
                | (xx, yy) :: tl ->
                        let () = m.(yy).(xx) <- depth in
                        let next = if xx > 0     && m.(yy).(xx - 1) < 0 && s.(yy).[xx-1] = '.' then S.add (xx-1, yy) next else next in
                        let next = if xx < w - 1 && m.(yy).(xx + 1) < 0 && s.(yy).[xx+1] = '.' then S.add (xx+1, yy) next else next in
                        let next = if yy > 0     && m.(yy - 1).(xx) < 0 && s.(yy-1).[xx] = '.' then S.add (xx, yy-1) next else next in
                        let next = if yy < h - 1 && m.(yy + 1).(xx) < 0 && s.(yy+1).[xx] = '.' then S.add (xx, yy+1) next else next in
                        loop depth next tl
            in
            if s.(y).[x] = '.' then loop 0 S.empty [ x, y ] else 0
        in
        let rec loop_h y acc =
            let rec loop_w x acc =
                if x = w then acc else loop_w (x + 1) (max acc (dfs x y))
            in
            if y = h then acc else loop_h (y + 1) (loop_w 0 acc)
        in
        loop_h 0 0 |> Printf.printf "%d\n"
    in
    main ()

Score : 400 points

Problem Statement

Takahashi has a maze, which is a grid of H \times W squares with H horizontal rows and W vertical columns.

The square at the i-th row from the top and the j-th column is a "wall" square if S_{ij} is #, and a "road" square if S_{ij} is ..

From a road square, you can move to a horizontally or vertically adjacent road square.

You cannot move out of the maze, move to a wall square, or move diagonally.

Takahashi will choose a starting square and a goal square, which can be any road squares, and give the maze to Aoki.

Aoki will then travel from the starting square to the goal square, in the minimum number of moves required.

In this situation, find the maximum possible number of moves Aoki has to make.

Constraints

1 \leq H,W \leq 20

S_{ij} is . or #.

S contains at least two occurrences of ..

Any road square can be reached from any road square in zero or more moves.

Input

Input is given from Standard Input in the following format:

H W
S_{11}...S_{1W}
:
S_{H1}...S_{HW}

Output

Print the maximum possible number of moves Aoki has to make.

Sample Input 1

3 3
...
...
...

Sample Output 1

4

If Takahashi chooses the top-left square as the starting square and the bottom-right square as the goal square, Aoki has to make four moves.

Sample Input 2

3 5
...#.
.#.#.
.#...

Sample Output 2

10

If Takahashi chooses the bottom-left square as the starting square and the top-right square as the goal square, Aoki has to make ten moves.


let () = Scanf.scanf "%d\n" @@ fun n ->
  let sts = Array.init n @@ fun _ -> Scanf.scanf "%s %d\n" @@ fun s t -> s, t in
  Scanf.scanf "%s\n" @@ fun x ->
    Printf.printf "%d\n" @@
    snd @@
    List.find (fun (s, _) -> s = x) @@
    snd @@
    Array.fold_right (fun (s, t) (sum, acc) ->
      (sum + t, (s, sum) :: acc)) sts (0, [])

Score : 200 points

Problem Statement

Niwango created a playlist of N songs.
The title and the duration of the i-th song are s_i and t_i seconds, respectively.
It is guaranteed that s_1,\ldots,s_N are all distinct.

Niwango was doing some work while playing this playlist. (That is, all the songs were played once, in the order they appear in the playlist, without any pause in between.)
However, he fell asleep during his work, and he woke up after all the songs were played.
According to his record, it turned out that he fell asleep at the very end of the song titled X.

Find the duration of time when some song was played while Niwango was asleep.

Constraints

1 \leq N \leq 50

s_i and X are strings of length between 1 and 100 (inclusive) consisting of lowercase English letters.

s_1,\ldots,s_N are distinct.

There exists an integer i such that s_i = X.

1 \leq t_i \leq 1000

t_i is an integer.

Input

Input is given from Standard Input in the following format:

N
s_1 t_1
\vdots
s_{N} t_N
X

Output

Print the answer.

Sample Input 1

3
dwango 2
sixth 5
prelims 25
dwango

Sample Output 1

30

While Niwango was asleep, two songs were played: sixth and prelims.

The answer is the total duration of these songs, 30.

Sample Input 2

1
abcde 1000
abcde

Sample Output 2

0

No songs were played while Niwango was asleep.

In such a case, the total duration of songs is 0.

Sample Input 3

15
ypnxn 279
kgjgwx 464
qquhuwq 327
rxing 549
pmuduhznoaqu 832
dagktgdarveusju 595
wunfagppcoi 200
dhavrncwfw 720
jpcmigg 658
wrczqxycivdqn 639
mcmkkbnjfeod 992
htqvkgkbhtytsz 130
twflegsjz 467
dswxxrxuzzfhkp 989
szfwtzfpnscgue 958
pmuduhznoaqu

Sample Output 3

6348


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

let result = if 500 * k >= x then "Yes" else "No"

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

Score : 100 points

Problem Statement

Takahashi has K 500-yen coins. (Yen is the currency of Japan.)
If these coins add up to X yen or more, print Yes; otherwise, print No.

Constraints

1 \leq K \leq 100

1 \leq X \leq 10^5

Input

Input is given from Standard Input in the following format:

K X

Output

If the coins add up to X yen or more, print Yes; otherwise, print No.

Sample Input 1

2 900

Sample Output 1

Yes

Two 500-yen coins add up to 1000 yen, which is not less than X = 900 yen.

Sample Input 2

1 501

Sample Output 2

No

One 500-yen coin is worth 500 yen, which is less than X = 501 yen.

Sample Input 3

4 2000

Sample Output 3

Yes

Four 500-yen coins add up to 2000 yen, which is not less than X = 2000 yen.


let count_ABC str sz =
  let rec iter_count pos rest =
    if pos >= sz - 2
    then rest
    else
      if String.sub str pos 3 = "ABC"
      then iter_count (pos+1) (rest+1)
      else iter_count (pos+1) rest
  in iter_count 0 0

let () = Scanf.scanf "%d\n %s" @@ fun n s -> Printf.printf "%d\n" @@ count_ABC s n

Score : 200 points

Problem Statement

We have a string S of length N consisting of uppercase English letters.

How many times does ABC occur in S as contiguous subsequences (see Sample Inputs and Outputs)?

Constraints

3 \leq N \leq 50

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print number of occurrences of ABC in S as contiguous subsequences.

Sample Input 1

10
ZABCDBABCQ

Sample Output 1

2

Two contiguous subsequences of S are equal to ABC: the 2-nd through 4-th characters, and the 7-th through 9-th characters.

Sample Input 2

19
THREEONEFOURONEFIVE

Sample Output 2

0

No contiguous subsequences of S are equal to ABC.

Sample Input 3

33
ABCCABCBABCCABACBCBBABCBCBCBCABCB

Sample Output 3

5


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

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

let id = identity

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

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

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

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

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

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

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

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

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

let zip l m =
  let n = min (List.length l) (List.length m) in
  List.combine (List.take n l) (List.take n m)

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

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

let (n,m) = scan "%d %d" Tuple2.make
let ls = scan_list ~sep:' ' Int.of_string

let rec gcd a b =
  if b = 0 then a
  else gcd b (a mod b)

let () =
  let ns = ListL.map ls ~f:(fun x ->
      let rec div2 n = if n mod 2 = 0 then succ @@ div2 (n/2) else 0 in
      div2 x) in
  let ms = ListL.map ls ~f:(fun x -> x / 2) in
  List.reduce (fun a b -> a*b / gcd a b) ms
  |> (fun x ->
      if List.for_all ((=) @@ List.hd ns) ns then (m/x + 1) / 2
      else 0)
  |> Printf.printf "%d\n"

Score : 400 points

Problem Statement

Given are a sequence A= {a_1,a_2,......a_N} of N positive even numbers, and an integer M.

Let a semi-common multiple of A be a positive integer X that satisfies the following condition for every k (1 \leq k \leq N):

There exists a non-negative integer p such that X= a_k \times (p+0.5).

Find the number of semi-common multiples of A among the integers between 1 and M (inclusive).

Constraints

1 \leq N \leq 10^5

1 \leq M \leq 10^9

2 \leq a_i \leq 10^9

a_i is an even number.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
a_1 a_2 ... a_N

Output

Print the number of semi-common multiples of A among the integers between 1 and M (inclusive).

Sample Input 1

2 50
6 10

Sample Output 1

2

15 = 6 \times 2.5

15 = 10 \times 1.5

45 = 6 \times 7.5

45 = 10 \times 4.5

Thus, 15 and 45 are semi-common multiples of A. There are no other semi-common multiples of A between 1 and 50, so the answer is 2.

Sample Input 2

3 100
14 22 40

Sample Output 2

0

The answer can be 0.

Sample Input 3

5 1000000000
6 6 2 6 2

Sample Output 3

166666667


let () =
    let rec gcd a b = if b = 0 then a else gcd b (a mod b) in
    let lcm a b = a / gcd a b * b in
    let main () =
        let n, m = Scanf.scanf "%d %d" (fun n m -> n, m) in
        let a = Array.init n (fun _ -> Scanf.scanf " %d" (fun p -> p)) in
        let rec loop i acc =
            if i = n then acc else
                if a.(i) mod 4 = 0 then m + 1 else
                    loop (i + 1) (lcm acc (a.(i) / 2))
        in
        let l = loop 1 (a.(0) / 2) in
        (m / l + 1) / 2 |> Printf.printf "%d\n"
    in
    main ()


let () = Scanf.scanf "%s %s" @@ fun s t -> Printf.printf "%s\n" @@ t ^ s

Score : 100 points

Problem Statement

Given are two strings S and T consisting of lowercase English letters. Concatenate T and S in this order, without space in between, and print the resulting string.

Constraints

S and T are strings consisting of lowercase English letters.

The lengths of S and T are between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

S T

Output

Print the resulting string.

Sample Input 1

oder atc

Sample Output 1

atcoder

When S = oder and T = atc, concatenating T and S in this order results in atcoder.

Sample Input 2

humu humu

Sample Output 2

humuhumu


let _ = Scanf.sscanf (read_line ()) "%s %s" (fun a b -> print_endline (b ^ a))


let () =
	let s, t = Scanf.scanf "%s %s\n" (fun a b -> a, b) in
    Printf.printf "%s\n" (t ^ s)


let () =
  Scanf.scanf "%d %d %d\n" @@ fun a b k ->
  let (la, lb) =
    if k <= a then (a-k,b)
    else if k <= a + b then (0,b-(k-a))
    else (0,0) in
  Printf.printf "%d %d\n" la lb

Score : 200 points

Problem Statement

Takahashi has A cookies, and Aoki has B cookies.
Takahashi will do the following action K times:

If Takahashi has one or more cookies, eat one of his cookies.

Otherwise, if Aoki has one or more cookies, eat one of Aoki's cookies.

If they both have no cookies, do nothing.

In the end, how many cookies will Takahashi and Aoki have, respectively?

Constraints

0 \leq A \leq 10^{12}

0 \leq B \leq 10^{12}

0 \leq K \leq 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B K

Output

Print the numbers of Takahashi's and Aoki's cookies after K actions.

Sample Input 1

2 3 3

Sample Output 1

0 2

Takahashi will do the following:

He has two cookies, so he eats one of them.

Now he has one cookie left, and he eats it.

Now he has no cookies left, but Aoki has three, so Takahashi eats one of them.

Thus, in the end, Takahashi will have 0 cookies, and Aoki will have 2.

Sample Input 2

500000000000 500000000000 1000000000000

Sample Output 2

0 0

Watch out for overflows.


let x = read_int ()
let f n = n > 1 && let rec f i = i * i > n || n mod i > 0 && f (i + 1) in f 2
let _ = for i = x to 100003 do if f i then (Printf.printf "%d\n" i; exit 0) done

Score: 300 points

Problem Statement

Find the minimum prime number greater than or equal to X.

Notes

A prime number is an integer greater than 1 that cannot be evenly divided by any positive integer except 1 and itself.

For example, 2, 3, and 5 are prime numbers, while 4 and 6 are not.

Constraints

2 \le X \le 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X

Output

Print the minimum prime number greater than or equal to X.

Sample Input 1

20

Sample Output 1

23

The minimum prime number greater than or equal to 20 is 23.

Sample Input 2

2

Sample Output 2

2

X itself can be a prime number.

Sample Input 3

99992

Sample Output 3

100003


let k, ss = Scanf.scanf "%d %d %d %d %d %s" @@ fun n k r s p t -> k, Array.init n (fun i -> match t.[i] with 'r' -> p | 's' -> r | _ -> s)
let _ = Array.(iteri (fun i s -> if i >= k && s = ss.(i - k) then ss.(i) <- 0) ss; fold_left (+) 0 ss) |> Printf.printf "%d\n"

Score : 400 points

Problem Statement

At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows:

The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.)

Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss):

R points for winning with Rock;

S points for winning with Scissors;

P points for winning with Paper.

However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.)

Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands.

The information Takahashi obtained is given as a string T. If the i-th character of T (1 \leq i \leq N) is r, the machine will play Rock in the i-th round. Similarly, p and s stand for Paper and Scissors, respectively.

What is the maximum total score earned in the game by adequately choosing the hand to play in each round?

Notes

In this problem, Rock Paper Scissors can be thought of as a two-player game, in which each player simultaneously forms Rock, Paper, or Scissors with a hand.

If a player chooses Rock and the other chooses Scissors, the player choosing Rock wins;

if a player chooses Scissors and the other chooses Paper, the player choosing Scissors wins;

if a player chooses Paper and the other chooses Rock, the player choosing Paper wins;

if both players play the same hand, it is a draw.

Constraints

2 \leq N \leq 10^5

1 \leq K \leq N-1

1 \leq R,S,P \leq 10^4

N,K,R,S, and P are all integers.

|T| = N

T consists of r, p, and s.

Input

Input is given from Standard Input in the following format:

N K
R S P
T

Output

Print the maximum total score earned in the game.

Sample Input 1

5 2
8 7 6
rsrpr

Sample Output 1

27

The machine will play {Rock, Scissors, Rock, Paper, Rock}.

We can, for example, play {Paper, Rock, Rock, Scissors, Paper} against it to earn 27 points.
We cannot earn more points, so the answer is 27.

Sample Input 2

7 1
100 10 1
ssssppr

Sample Output 2

211

Sample Input 3

30 5
325 234 123
rspsspspsrpspsppprpsprpssprpsr

Sample Output 3

4996


let () = Scanf.scanf "%d %d %d %d\n" @@ fun n m v p ->
  let as_ = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun a -> a in
  Array.sort (fun a a' -> compare a' a) as_;
  let acc = Array.make (n + 1) 0 in
  for i = 0 to n - 1 do
    acc.(i + 1) <- as_.(i) + acc.(i)
  done;
  Printf.printf "%d\n" @@
  Array.fold_left ( + ) 0 @@
  Array.init n @@ fun i ->
    if
      as_.(p - 1) <= as_.(i) ||
      as_.(p - 1) <= as_.(i) + m &&
      acc.(i) - acc.(p - 1)
        <= min (i - p + 1) (n - v) * m + as_.(i) * (i - p + 1)
    then 1
    else 0

Score : 700 points

Problem Statement

N problems are proposed for an upcoming contest. Problem i has an initial integer score of A_i points.

M judges are about to vote for problems they like. Each judge will choose exactly V problems, independently from the other judges,
and increase the score of each chosen problem by 1.

After all M judges cast their vote, the problems will be sorted in non-increasing order of score, and the first P problems will be chosen for the problemset.
Problems with the same score can be ordered arbitrarily, this order is decided by the chief judge.

How many problems out of the given N have a chance to be chosen for the problemset?

Constraints

2 \le N \le 10^5

1 \le M \le 10^9

1 \le V \le N - 1

1 \le P \le N - 1

0 \le A_i \le 10^9

Input

Input is given from Standard Input in the following format:

N M V P
A_1 A_2 ... A_N

Output

Print the number of problems that have a chance to be chosen for the problemset.

Sample Input 1

6 1 2 2
2 1 1 3 0 2

Sample Output 1

5

If the only judge votes for problems 2 and 5, the scores will be 2 2 1 3 1 2.
The problemset will consist of problem 4 and one of problems 1, 2, or 6.

If the only judge votes for problems 3 and 4, the scores will be 2 1 2 4 0 2.
The problemset will consist of problem 4 and one of problems 1, 3, or 6.

Thus, problems 1, 2, 3, 4, and 6 have a chance to be chosen for the problemset. On the contrary, there is no way for problem 5 to be chosen.

Sample Input 2

6 1 5 2
2 1 1 3 0 2

Sample Output 2

3

Only problems 1, 4, and 6 have a chance to be chosen.

Sample Input 3

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

Sample Output 3

8


open Printf
open Scanf

let solve a b = 6 - a - b

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

Score: 100 points

Problem Statement

Takahashi is solving quizzes. He has easily solved all but the last one.

The last quiz has three choices: 1, 2, and 3.

With his supernatural power, Takahashi has found out that the choices A and B are both wrong.

Print the correct choice for this problem.

Constraints

Each of the numbers A and B is 1, 2, or 3.

A and B are different.

Input

Input is given from Standard Input in the following format:

A
B

Output

Print the correct choice.

Sample Input 1

3
1

Sample Output 1

2

When we know 3 and 1 are both wrong, the correct choice is 2.

Sample Input 2

1
2

Sample Output 2

3


let id x = x

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

let split = Str.split (Str.regexp "")

let solve =
  List.combine (split s) (split t)
    |> List.map (fun (x, y) -> x ^ y)
    |> String.concat ""
    |> print_endline

Score : 200 points

Problem Statement

Given are strings s and t of length N each, both consisting of lowercase English letters.

Let us form a new string by alternating the characters of S and the characters of T, as follows: the first character of S, the first character of T, the second character of S, the second character of T, ..., the N-th character of S, the N-th character of T. Print this new string.

Constraints

1 \leq N \leq 100

|S| = |T| = N

S and T are strings consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S T

Output

Print the string formed.

Sample Input 1

2
ip cc

Sample Output 1

icpc

Sample Input 2

8
hmhmnknk uuuuuuuu

Sample Output 2

humuhumunukunuku

Sample Input 3

5
aaaaa aaaaa

Sample Output 3

aaaaaaaaaa


let n, st = Scanf.scanf " %d %s %s" @@ fun a b c -> a, [|b; c|]
let _ = String.init (2 * n) (fun i -> st.(i mod 2).[i / 2]) |> print_endline


let rec gcd a b = if b = 0 then a else gcd b (a mod b)
let _ = Printf.printf "%d\n" (Scanf.scanf "%d %d" (fun a b -> a / gcd a b * b))

Score : 300 points

Problem Statement

Takahashi is organizing a party.

At the party, each guest will receive one or more snack pieces.

Takahashi predicts that the number of guests at this party will be A or B.

Find the minimum number of pieces that can be evenly distributed to the guests in both of the cases predicted.

We assume that a piece cannot be divided and distributed to multiple guests.

Constraints

1 \leq A, B \leq 10^5

A \neq B

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the minimum number of pieces that can be evenly distributed to the guests in both of the cases with A guests and B guests.

Sample Input 1

2 3

Sample Output 1

6

When we have six snack pieces, each guest can take three pieces if we have two guests, and each guest can take two if we have three guests.

Sample Input 2

123 456

Sample Output 2

18696

Sample Input 3

100000 99999

Sample Output 3

9999900000


let () =
    let rec gcd a b = if b = 0 then a else gcd b (a mod b) in
    let lcm a b = a / gcd a b * b in
    let main () =
        let a, b = Scanf.scanf "%d %d" (fun a b -> a, b) in
        Printf.printf "%d\n" (lcm a b)
    in
    main ()


let id x = x

let repeat f =
  let rec loop lst = function
    | 0 -> List.rev lst
    | n -> loop (f () :: lst) (n-1) in
  loop []

let n = Scanf.scanf "%d\n" id

let a = repeat (fun () -> Scanf.scanf " %d" id) n

let () =
  let ans = List.fold_left (fun v i -> if i = (v + 1) then v + 1 else v) 0 a in
  print_int @@ if ans = 0 then -1 else n - ans

Score : 400 points

Problem Statement

We have N bricks arranged in a row from left to right.

The i-th brick from the left (1 \leq i \leq N) has an integer a_i written on it.

Among them, you can break at most N-1 bricks of your choice.

Let us say there are K bricks remaining. Snuke will be satisfied if, for each integer i (1 \leq i \leq K), the i-th of those brick from the left has the integer i written on it.

Find the minimum number of bricks you need to break to satisfy Snuke's desire. If his desire is unsatisfiable, print -1 instead.

Constraints

All values in input are integers.

1 \leq N \leq 200000

1 \leq a_i \leq N

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of bricks that need to be broken to satisfy Snuke's desire, or print -1 if his desire is unsatisfiable.

Sample Input 1

3
2 1 2

Sample Output 1

1

If we break the leftmost brick, the remaining bricks have integers 1 and 2 written on them from left to right, in which case Snuke will be satisfied.

Sample Input 2

3
2 2 2

Sample Output 2

-1

In this case, there is no way to break some of the bricks to satisfy Snuke's desire.

Sample Input 3

10
3 1 4 1 5 9 2 6 5 3

Sample Output 3

7

Sample Input 4

1
1

Sample Output 4

0

There may be no need to break the bricks at all.


let n, ans = read_int (), ref 0
let a_s = Array.init n @@ fun _ -> Scanf.scanf " %d" (+) 0
let _ = Array.fold_left (fun c a -> if a = c then c + 1 else (incr ans; c)) 1 a_s
let _ = Printf.printf "%d\n" @@ if !ans = n then -1 else !ans


let () =
    let main () =
        let n = int_of_string (read_line ()) in
        let zeros = if n mod 2 = 1 then 0 else (
            let rec loop i t acc =
                if i > n then acc else
                    loop (i * 5) (t + 0) (acc + t * (n / i))
            in
            loop 10 1 0
        )
        in
        Printf.printf "%d\n" zeros
    in
    main ()

Score : 500 points

Problem Statement

For an integer n not less than 0, let us define f(n) as follows:

f(n) = 1 (if n < 2)

f(n) = n f(n-2) (if n \geq 2)

Given is an integer N. Find the number of trailing zeros in the decimal notation of f(N).

Constraints

0 \leq N \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of trailing zeros in the decimal notation of f(N).

Sample Input 1

12

Sample Output 1

1

f(12) = 12 × 10 × 8 × 6 × 4 × 2 = 46080, which has one trailing zero.

Sample Input 2

5

Sample Output 2

0

f(5) = 5 × 3 × 1 = 15, which has no trailing zeros.

Sample Input 3

1000000000000000000

Sample Output 3

124999999999999995


let _ = Scanf.sscanf (read_line ()) "%d %d %d" (fun a b c ->
  match a + b + c with
  | x when x >= 22 -> print_endline "bust"
  | _ -> print_endline "win"
)

Score : 100 points

Problem Statement

Given are three integers A_1, A_2, and A_3.

If A_1+A_2+A_3 is greater than or equal to 22, print bust; otherwise, print win.

Constraints

1 \leq A_i \leq 13 \ \ (i=1,2,3)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A_1 A_2 A_3

Output

If A_1+A_2+A_3 is greater than or equal to 22, print bust; otherwise, print win.

Sample Input 1

5 7 9

Sample Output 1

win

5+7+9=21, so print win.

Sample Input 2

13 7 2

Sample Output 2

bust

13+7+2=22, so print bust.


let s = read_line () in
let rec loop a b acc =
    if a > b then acc else loop (a + 1) (b - 1) (if s.[a] != s.[b] then acc + 1 else acc)
in
Printf.printf "%d\n" (loop 0 (String.length s - 1) 0)

Score : 200 points

Problem Statement

Takahashi loves palindromes. Non-palindromic strings are unacceptable to him. Each time he hugs a string, he can change one of its characters to any character of his choice.

Given is a string S. Find the minimum number of hugs needed to make S palindromic.

Constraints

S is a string consisting of lowercase English letters.

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

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum number of hugs needed to make S palindromic.

Sample Input 1

redcoder

Sample Output 1

1

For example, we can change the fourth character to o and get a palindrome redooder.

Sample Input 2

vvvvvv

Sample Output 2

0

We might need no hugs at all.

Sample Input 3

abcdabc

Sample Output 3

2