Scanf.scanf "%d %d %d" (fun n m k ->
    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 mm = 998244353 in
    let ( +@) a b = (a + b) mod mm in
    let ( *@) a b = (a * b) mod mm in
    let ( /@) a b = a *@ inv b mm in

    let modpow a b =
        let rec loop r bit acc =
            if bit = 0 then acc else
                loop (r *@ r) (bit lsr 1) (if bit land 1 = 0 then acc else acc *@ r)
        in
        loop a b 1
    in

    let rec loop cur over single acc =
        let acc = acc +@ cur in
        if single > k then acc else
            let cur = cur *@ over /@ (single *@ (m - 1)) in
            loop cur (over - 1) (single + 1) acc
    in
    (if m = 1 && k = n - 1 then 1 else
        loop (m *@ modpow (m - 1) (n - 1)) (n - 1) 1 0) |> Printf.printf "%d\n"
)


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

let m = 998244353
let ( +^ ) x y = (x + y) mod m
let ( *^ ) x y = (x * y) mod m
let inv = Fun.flip (power ( *^ ) 1) (m - 2)

let () = Scanf.scanf "%d %d %d" @@ fun n m k ->
  let comb = Array.make n 1 in
  for i = 0 to n - 2 do
    comb.(i + 1) <- comb.(i) *^ (n - 1 - i) *^ inv (i + 1)
  done;
  Printf.printf "%d\n" @@
  List.fold_left ( +^ ) 0 @@
  List.init (k + 1) @@ fun k ->
    power ( *^ ) (m *^ comb.(k)) (m - 1) (n - 1 - k)

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

Score : 500 points

Problem Statement

There are N blocks arranged in a row. Let us paint these blocks.

We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways.

Find the number of ways to paint the blocks under the following conditions:

For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors.

There may be at most K pairs of adjacent blocks that are painted in the same color.

Since the count may be enormous, print it modulo 998244353.

Constraints

All values in input are integers.

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

0 \leq K \leq N - 1

Input

Input is given from Standard Input in the following format:

N M K

Output

Print the answer.

Sample Input 1

3 2 1

Sample Output 1

6

The following ways to paint the blocks satisfy the conditions: 112, 121, 122, 211, 212, and 221. Here, digits represent the colors of the blocks.

Sample Input 2

100 100 0

Sample Output 2

73074801

Sample Input 3

60522 114575 7559

Sample Output 3

479519525

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 parse s =
  Array.init (String.length s) (fun i -> s.[i])
  |> Array.fold_left (fun (p, b) c ->
      if c = '(' then (p+1, b) else (p-1, min b (p-1))) (0, 0)

let () =
  Scanf.scanf "%d" @@ fun n ->
  let s = Array.init n (fun _ -> Scanf.scanf " %s" parse) in
  Array.sort (fun (a, b) (c, d) -> d - b) s;
  let tp, tb =
    Array.fold_left (fun (tp, tb) (p, b) ->
      (tp + p, min tb (tp + b))) (0, 0) s in

  let ans = if tp = 0 && tb >= 0 then "Yes" else "No" in
  print_endline ans

Score : 600 points

Problem Statement

A bracket sequence is a string that is one of the following:

An empty string;

The concatenation of (, A, and ) in this order, for some bracket sequence A ;

The concatenation of A and B in this order, for some non-empty bracket sequences A and B /

Given are N strings S_i. Can a bracket sequence be formed by concatenating all the N strings in some order?

Constraints

1 \leq N \leq 10^6

The total length of the strings S_i is at most 10^6.

S_i is a non-empty string consisting of ( and ).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If a bracket sequence can be formed by concatenating all the N strings in some order, print Yes; otherwise, print No.

Sample Input 1

2
)
(()

Sample Output 1

Yes

Concatenating (() and ) in this order forms a bracket sequence.

Sample Input 2

2
)(
()

Sample Output 2

No

Sample Input 3

4
((()))
((((((
))))))
()()()

Sample Output 3

Yes

Sample Input 4

3
(((
)
)

Sample Output 4

No


open Array
let () =
  let rec make_list n = if n = 0 then [0] else n :: make_list (n-1) in
  let n,m = Scanf.scanf "%d %d\n" @@ fun a b -> a, b in
  let h = init n @@ fun _ -> Scanf.scanf "%d " @@ fun d -> d in
  let arr = make n 0 in
  for i = 1 to m do
    Scanf.scanf "%d %d\n" @@ fun x y ->
    let a = x-1 in let b = y-1 in
    arr.(a) <- (max h.(b) arr.(a)); arr.(b) <- (max h.(a) arr.(b))
  done;
  print_int
    (List.fold_left (fun i v -> if h.(v) > arr.(v) then i+1 else i) 0 (make_list (n-1)))

Score : 300 points

Problem Statement

There are N observatories in AtCoder Hill, called Obs. 1, Obs. 2, ..., Obs. N. The elevation of Obs. i is H_i.
There are also M roads, each connecting two different observatories. Road j connects Obs. A_j and Obs. B_j.

Obs. i is said to be good when its elevation is higher than those of all observatories that can be reached from Obs. i using just one road.
Note that Obs. i is also good when no observatory can be reached from Obs. i using just one road.

How many good observatories are there?

Constraints

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq H_i \leq 10^9

1 \leq A_i,B_i \leq N

A_i \neq B_i

Multiple roads may connect the same pair of observatories.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
H_1 H_2 ... H_N
A_1 B_1
A_2 B_2
:
A_M B_M

Output

Print the number of good observatories.

Sample Input 1

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

Sample Output 1

2

From Obs. 1, you can reach Obs. 3 using just one road. The elevation of Obs. 1 is not higher than that of Obs. 3, so Obs. 1 is not good.

From Obs. 2, you can reach Obs. 3 and 4 using just one road. The elevation of Obs. 2 is not higher than that of Obs. 3, so Obs. 2 is not good.

From Obs. 3, you can reach Obs. 1 and 2 using just one road. The elevation of Obs. 3 is higher than those of Obs. 1 and 2, so Obs. 3 is good.

From Obs. 4, you can reach Obs. 2 using just one road. The elevation of Obs. 4 is higher than that of Obs. 2, so Obs. 4 is good.

Thus, the good observatories are Obs. 3 and 4, so there are two good observatories.

Sample Input 2

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

Sample Output 2

3


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

let pow n a = Array.fold_left ( * ) 1 @@ Array.make a n

let () =
  for a = -200 to 200 do
    for b = -200 to 200 do
      if pow a 5 - pow b 5 = x then begin
        Printf.printf "%d %d\n" a b;
        exit 0
      end
    done
  done

Score : 400 points

Problem Statement

Give a pair of integers (A, B) such that A^5-B^5 = X.
It is guaranteed that there exists such a pair for the given integer X.

Constraints

1 \leq X \leq 10^9

X is an integer.

There exists a pair of integers (A, B) satisfying the condition in Problem Statement.

Input

Input is given from Standard Input in the following format:

X

Output

Print A and B, with space in between.
If there are multiple pairs of integers (A, B) satisfying the condition, you may print any of them.

A B

Sample Input 1

33

Sample Output 1

2 -1

For A=2 and B=-1, A^5-B^5 = 33.

Sample Input 2

1

Sample Output 2

0 -1


open Batteries
let x = read_int ()

let rec loop a =
  let rec loop2 b =
    if b >= 1000 then (0, 0) else
    if ((a * a * a * a * a) - (b*b*b*b*b)) = x then (a, b) else loop2 (b+1)
  in if a >= 1000 then (0, 0) else (
  let a_, b_ = loop2 (-1000) in
  if a_ = 0 && b_ = 0 then loop (a+1) else (a_, b_)
)
let a, b = loop (-1000)
let _ = Printf.printf "%d %d\n" a b


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

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

let id = identity

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

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

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

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

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

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

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

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

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

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

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

let (n,a,b,c) = scan "%d %d %d %d" Tuple4.make
let coms = scan_lines n "%s" id

open Option.Infix
open Option.Monad

let rec solve1 a b c= function
  | [] -> Some []
  | com::rest ->
    match com with
    | "AB" ->
      if a = 0 && b = 0 then None
      else if a = 1 then
        solve1 (pred a) (succ b) c rest >>= fun ret ->
        return ("B" :: ret)
      else
        solve1 (succ a) (pred b) c rest >>= fun ret ->
        return ("A" :: ret)
    | "BC" ->
      if b = 0 && c = 0 then None
      else if b = 1 then
        solve1 a (pred b) (succ c) rest >>= fun ret ->
        return ("C" :: ret)
      else
        solve1 a (succ b) (pred c) rest >>= fun ret ->
        return ("B" :: ret)
    | "AC" ->
      if a = 0 && c = 0 then None
      else if c = 1 then
        solve1 (succ a) b (pred c) rest >>= fun ret ->
        return ("A" :: ret)
      else solve1 (pred a) b (succ c) rest >>= fun ret ->
        return ("C" :: ret)
    | _ ->  None

let rec solven a b c = function
  | [] -> Some []
  | com :: rest ->
    begin
      match com with
      | "AB" ->
        if a > b then
          solven (pred a) (succ b) c rest >>= fun ret ->
          return ("B"::ret)
        else
          solven (succ a) (pred b) c rest >>= fun ret ->
          return ("A"::ret)
      | "BC" ->
        if b > c then
          solven a (pred b) (succ c) rest >>= fun ret ->
          return ("C"::ret)
        else
          solven a (succ b) (pred c) rest >>= fun ret ->
          return ("B"::ret)
      | "AC" ->
        if a > c then
          solven (pred a) b (succ c) rest >>= fun ret ->
          return ("C"::ret)
        else
          solven (succ a) b (pred c) rest >>= fun ret ->
          return ("A"::ret)
      | _ -> None
    end

let rec solve2 a b c = function
  | [] -> Some []
  | com :: rest ->
    begin
      match com with
      | "AB" ->
        if a = 1 && b = 1 && List.at_opt rest 0 |> Option.is_some then
          let next = List.at rest 0 in
          match next with
          | "AB" -> solve2 (pred a) (succ b) c rest >>= fun ret ->
            return ("B"::ret)
          | "AC" -> solve2 (succ a) (pred b) c rest >>= fun ret ->
            return ("A"::ret)
          | "BC" -> solve2 (pred a) (succ b) c rest >>= fun ret ->
            return ("B"::ret)
          | _ -> None
        else if a = 0 then
          solve2 (succ a) (pred b) c rest >>= fun ret ->
          return ("A" :: ret)
        else
          solve2 (pred a) (succ b) c rest >>= fun ret ->
          return ("B" :: ret)
      | "BC" ->
        if b = 1 && c= 1 && List.at_opt rest 0 |> Option.is_some then
          let next = List.at rest 0 in
          match next with
          | "AB" -> solve2 a (succ b) (pred c) rest >>= fun ret ->
            return ("B" :: ret)
          | "BC" -> solve2 a (pred b) (succ c) rest >>= fun ret ->
            return ("C"::ret)
          | "AC" -> solve2 a (pred b) (succ c) rest >>= fun ret ->
            return ("C"::ret)
          | _ -> None
        else if b = 0 then
          solve2 a (succ b) (pred c) rest >>= fun ret ->
          return ("B" :: ret)
        else
          solve2 a (pred b) (succ c) rest >>= fun ret ->
          return ("C" :: ret)
      | "AC" ->
        if c = 1 && a = 1 && List.at_opt rest 0 |> Option.is_some then
          let next = List.at rest 0 in
          match next with
          | "AB" -> solve2 (succ a) b (pred c) rest >>= fun ret ->
            return ("A" :: ret)
          | "BC" -> solve2 (pred a) b (succ c) rest >>= fun ret ->
            return ("C"::ret)
          | "AC" -> solve2 (succ a) b (pred c) rest >>= fun ret ->
            return ("A"::ret)
          | _ -> None
        else if c = 0 then
          solve2 (pred a) b (succ c) rest >>= fun ret ->
          return ("C" :: ret)
        else
          solve2 (succ a) b (pred c) rest >>= fun ret ->
          return ("A" :: ret)
      | _ -> None
    end

let () =
  (match List.first coms with
   | "AB" when a = 0 && b = 0 -> None
   | "BC" when b = 0 && c = 0 -> None
   | "AC" when a = 0 && c = 0 -> None
   | _ ->
     if a + b + c = 1 then
       solve1 a b c coms
     else if a + b + c = 2 then
       solve2 a b c coms
     else
       solven a b c coms)
  |> Option.map_default (fun ans -> "Yes\n" ^ String.join "\n" ans) "No"
  |> Printf.printf "%s\n"

Score : 600 points

Problem Statement

There is a game that involves three variables, denoted A, B, and C.

As the game progresses, there will be N events where you are asked to make a choice.
Each of these choices is represented by a string s_i. If s_i is AB, you must add 1 to A or B then subtract 1 from the other; if s_i is AC, you must add 1 to A or C then subtract 1 from the other; if s_i is BC, you must add 1 to B or C then subtract 1 from the other.

After each choice, none of A, B, and C should be negative.

Determine whether it is possible to make N choices under this condition. If it is possible, also give one such way to make the choices.

Constraints

1 \leq N \leq 10^5

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

N, A, B, C are integers.

s_i is AB, AC, or BC.

Input

Input is given from Standard Input in the following format:

N A B C
s_1
s_2
:
s_N

Output

If it is possible to make N choices under the condition, print Yes; otherwise, print No.

Also, in the former case, show one such way to make the choices in the subsequent N lines. The (i+1)-th line should contain the name of the variable (A, B, or C) to which you add 1 in the i-th choice.

Sample Input 1

2 1 3 0
AB
AC

Sample Output 1

Yes
A
C

You can successfully make two choices, as follows:

In the first choice, add 1 to A and subtract 1 from B. A becomes 2, and B becomes 2.

In the second choice, add 1 to C and subtract 1 from A. C becomes 1, and A becomes 1.

Sample Input 2

3 1 0 0
AB
BC
AB

Sample Output 2

No

Sample Input 3

1 0 9 0
AC

Sample Output 3

No

Sample Input 4

8 6 9 1
AC
BC
AB
BC
AC
BC
AB
AB

Sample Output 4

Yes
C
B
B
C
C
B
A
A


let () = Scanf.scanf "%d\n %d %d" @@ fun k a b ->
  print_endline @@
  try
    for i = a to b do
      if i mod k = 0 then raise Not_found else ()
    done;
    "NG"
  with Not_found -> "OK"

Score: 100 points

Problem Statement

Takahashi the Jumbo will practice golf.

His objective is to get a carry distance that is a multiple of K, while he can only make a carry distance of between A and B (inclusive).

If he can achieve the objective, print OK; if he cannot, print NG.

Constraints

All values in input are integers.

1 \leq A \leq B \leq 1000

1 \leq K \leq 1000

Input

Input is given from Standard Input in the following format:

K
A B

Output

If he can achieve the objective, print OK; if he cannot, print NG.

Sample Input 1

7
500 600

Sample Output 1

OK

Among the multiples of 7, for example, 567 lies between 500 and 600.

Sample Input 2

4
5 7

Sample Output 2

NG

No multiple of 4 lies between 5 and 7.

Sample Input 3

1
11 11

Sample Output 3

OK


let k = read_int ()
let a, b = Scanf.scanf "%d %d\n" @@ fun a b -> a, b
let ans = if a mod k = 0 || a/k*k+k <= b || b mod k = 0 then "OK" else "NG"
let () = print_endline ans


let k = read_int ()
let a, b = Scanf.scanf "%d %d\n" @@ fun a b -> a, b
let ans = if k = 1 then "OK" else if b/k - a/k > 0 then "OK" else "NG"
let () = print_endline ans


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

let a = Array.make q 0
let b = Array.make q 0
let c = Array.make q 0
let d = Array.make q 0

let rec f i =
  if i < q then (
      Scanf.scanf "%d %d %d %d\n" (fun w x y z ->
      a.(i) <- w - 1;
      b.(i) <- x - 1;
      c.(i) <- y;
      d.(i) <- z
    );
    f (i + 1)
  )

let rec g i j e ans =
  if j = n then (
      let rec h i ans =
        if i = q then ans
        else h (i + 1) (ans + if e.(b.(i)) - e.(a.(i)) = c.(i) then d.(i) else 0)
      in h 0 0
  ) else (
      let rec h i ans =
        if i = m + 1 then ans
        else (
            e.(j) <- i;
            h (i + 1) (max ans (g i (j + 1) e ans))
        )
      in h i 0
  )

let () =
  f 0;
  let ans = g 1 0 (Array.make n 0) 0 in
  Printf.printf "%d\n" ans

Score : 300 points

Problem Statement

Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i , c_i , d_i ).

Consider a sequence A satisfying the following conditions:

A is a sequence of N positive integers.

1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.

Let us define a score of this sequence as follows:

The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)

Find the maximum possible score of A.

Constraints

All values in input are integers.

2 ≤ N ≤ 10

1 \leq M \leq 10

1 \leq Q \leq 50

1 \leq a_i < b_i \leq N ( i = 1, 2, ..., Q )

0 \leq c_i \leq M - 1 ( i = 1, 2, ..., Q )

(a_i, b_i, c_i) \neq (a_j, b_j, c_j) (where i \neq j)

1 \leq d_i \leq 10^5 ( i = 1, 2, ..., Q )

Input

Input is given from Standard Input in the following format:

N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q

Output

Print the maximum possible score of A.

Sample Input 1

3 4 3
1 3 3 100
1 2 2 10
2 3 2 10

Sample Output 1

110

When A = \{1, 3, 4\}, its score is 110. Under these conditions, no sequence has a score greater than 110, so the answer is 110.

Sample Input 2

4 6 10
2 4 1 86568
1 4 0 90629
2 3 0 90310
3 4 1 29211
3 4 3 78537
3 4 2 8580
1 2 1 96263
1 4 2 2156
1 2 0 94325
1 4 3 94328

Sample Output 2

357500

Sample Input 3

10 10 1
1 10 9 1

Sample Output 3

1


let tuple3 a b c = (a, b, c)
;;

let tuple4 a b c d = (a, b, c, d)
;;

let rec f n m list a_list min =
  if Array.length a_list == n
  then
    list
    |> List.filter_map
      (fun (a, b, c, d) -> if a_list.(b-1) - a_list.(a-1) == c then Option.some d else Option.none)
    |> List.fold_left (+) 0
  else
    List.init (m - min + 1) ((+) min)
    |> List.map (fun min -> f n m list (Array.append a_list [|min|]) min)
    |> List.fold_left max 0
;;

let () =
  let n, m, q = Scanf.scanf "%d %d %d\n" tuple3 in
  let list = List.init q (fun _ -> Scanf.scanf "%d %d %d %d\n" tuple4) in
  let res = f n m list [||] 1 in
  Printf.printf "%d\n" res
;;


let tuple3 a b c = (a, b, c)
;;

let tuple4 a b c d = (a, b, c, d)
;;

let rec f n m list a_list min =
  if Array.length a_list == n
  then
    list
    |> List.filter_map
      (fun (a, b, c, d) -> if a_list.(b-1) - a_list.(a-1) == c then Option.some d else Option.none)
    |> List.fold_left (+) 0
  else
    List.init (m - min + 1) ((+) min)
    |> List.map (fun min -> f n m list (Array.append a_list [|min|]) min)
    |> List.fold_left max 0
;;

let () =
  let n, m, q = Scanf.scanf "%d %d %d" tuple3 in
  let list = List.init q (fun _ -> Scanf.scanf "%d %d %d %d\n" tuple4) in
  let res = f n m list [||] 1 in
  Printf.printf "%d\n" res
;;


let () = Scanf.scanf "%d %d %d\n" @@ fun n m q ->
  let abcds = Array.init q @@ fun _ ->
    Scanf.scanf "%d %d %d %d\n" @@ fun a b c d -> a, b, c, d in
  let ass =
    List.init n (Fun.const ())
    |> Fun.flip List.fold_left [(m, [])] (fun nass () ->
        List.concat @@
        Fun.flip List.map nass @@ fun (n, as_) ->
          List.init n @@ fun a -> (a + 1, a + 1 :: as_))
    |> List.map snd
    |> List.map Array.of_list in
  Printf.printf "%d\n" @@
  List.fold_left (fun m as_ ->
    max m @@
    Array.fold_right (fun (a, b, c, d) -> ( + ) @@
      if as_.(b - 1) - as_.(a - 1) = c then d else 0) abcds 0) min_int ass


Scanf.scanf "%d %d %d" (fun a b n ->
  let x = min (b - 1) n in
  let ans = a * x / b - x / b * a in
  Printf.printf "%d\n" ans
)

Score : 400 points

Problem Statement

Given are integers A, B, and N.

Find the maximum possible value of floor(Ax/B) - A × floor(x/B) for a non-negative integer x not greater than N.

Here floor(t) denotes the greatest integer not greater than the real number t.

Constraints

1 ≤ A ≤ 10^{6}

1 ≤ B ≤ 10^{12}

1 ≤ N ≤ 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B N

Output

Print the maximum possible value of floor(Ax/B) - A × floor(x/B) for a non-negative integer x not greater than N, as an integer.

Sample Input 1

5 7 4

Sample Output 1

2

When x=3, floor(Ax/B)-A×floor(x/B) = floor(15/7) - 5×floor(3/7) = 2. This is the maximum value possible.

Sample Input 2

11 10 9

Sample Output 2

9


let tuple3 a b c = (a, b, c)
;;

let tuple4 a b c d = (a, b, c, d)
;;

let rec f a_list min n m list =
  if Array.length a_list == n
  then
    list
    |> List.filter_map
      (fun (a, b, c, d) -> if a_list.(b-1) - a_list.(a-1) == c then Option.some d else Option.none)
    |> List.fold_left (+) 0
  else
    List.init (m - min + 1) ((+) min)
    |> List.map (fun min -> f (Array.append a_list [|min|]) min n m list)
    |> List.fold_left max 0
;;

let () =
  let n, m, q = Scanf.scanf "%d %d %d\n" tuple3 in
  let list = List.init q (fun _ -> Scanf.scanf "%d %d %d %d\n" tuple4) in
  let res = f [||] 1 n m list in
  Printf.printf "%d\n" res
;;


let () = Scanf.scanf "%d %d %d" @@ fun a b n ->
  Printf.printf "%d\n" @@
  if b - 1 <= n
  then a * (b - 1) / b
  else a * n / b


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

Score : 300 points

Problem Statement

You drew lottery N times. In the i-th draw, you got an item of the kind represented by a string S_i.

How many kinds of items did you get?

Constraints

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

S_i consists of lowercase English letters and has a length between 1 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

Print the number of kinds of items you got.

Sample Input 1

3
apple
orange
apple

Sample Output 1

2

You got two kinds of items: apple and orange.

Sample Input 2

5
grape
grape
grape
grape
grape

Sample Output 2

1

Sample Input 3

4
aaaa
a
aaa
aa

Sample Output 3

4


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 []

module ModOp = struct
  let prime = 1_000_000_007
  let (-) a b = (a + prime - b) mod prime
  let (+) a b = (a + b) mod prime
  let ( * ) a b = (a * b) mod prime

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

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 atoi c = Char.code c - Char.code '0'

let rec powerset = function
  | [] -> [[]]
  | hd::tl ->
    let pws =  powerset tl in
    pws @ ListL.map pws ~f:(fun pw -> hd::pw)

let permutations l =
  let rec interleave x = function
    | [] -> [[x]]
    | (hd::tl) as l ->
      (x::l) :: (interleave x tl |> ListL.map ~f:(fun l -> hd::l)) in
  let rec aux = function
    | [] -> [[]]
    | a::rest ->
      aux rest |> ListL.map ~f:(interleave a) |> List.concat in
  aux 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 arr = scan_array ~sep:' ' Int.of_string

let () =
  if n < Array.sum arr then Printf.printf "-1\n"
  else Printf.printf "%d\n" @@ n - Array.sum arr

Score : 200 points

Problem Statement

Takahashi has N days of summer vacation.

His teacher gave him M summer assignments. It will take A_i days for him to do the i-th assignment.

He cannot do multiple assignments on the same day, or hang out on a day he does an assignment.

What is the maximum number of days Takahashi can hang out during the vacation if he finishes all the assignments during this vacation?

If Takahashi cannot finish all the assignments during the vacation, print -1 instead.

Constraints

1 \leq N \leq 10^6

1 \leq M \leq 10^4

1 \leq A_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 ... A_M

Output

Print the maximum number of days Takahashi can hang out during the vacation, or -1.

Sample Input 1

41 2
5 6

Sample Output 1

30

For example, he can do the first assignment on the first 5 days, hang out on the next 30 days, and do the second assignment on the last 6 days of the vacation. In this way, he can safely spend 30 days hanging out.

Sample Input 2

10 2
5 6

Sample Output 2

-1

He cannot finish his assignments.

Sample Input 3

11 2
5 6

Sample Output 3

0

He can finish his assignments, but he will have no time to hang out.

Sample Input 4

314 15
9 26 5 35 8 9 79 3 23 8 46 2 6 43 3

Sample Output 4

9


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 []

module ModOp = struct
  let prime = 1_000_000_007
  let (-) a b = (a + prime - b) mod prime
  let (+) a b = (a + b) mod prime
  let ( * ) a b = (a * b) mod prime

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

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 atoi c = Char.code c - Char.code '0'

let rec powerset = function
  | [] -> [[]]
  | hd::tl ->
    let pws =  powerset tl in
    pws @ ListL.map pws ~f:(fun pw -> hd::pw)

let permutations l =
  let rec interleave x = function
    | [] -> [[x]]
    | (hd::tl) as l ->
      (x::l) :: (interleave x tl |> ListL.map ~f:(fun l -> hd::l)) in
  let rec aux = function
    | [] -> [[]]
    | a::rest ->
      aux rest |> ListL.map ~f:(interleave a) |> List.concat in
  aux 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 = scan "%d" id
let a = scan_array ~sep:' ' Int.of_string

let () =
  let h = Hashtbl.create n in
  ArrayL.iter a ~f:(fun i ->
      Hashtbl.modify_def 0 (pred i) succ h);
  ListL.iter (0++^n) ~f:(fun i ->
      Printf.printf "%d" @@ Hashtbl.find_default h i 0
    )

Score : 300 points

Problem Statement

A company has N members, who are assigned ID numbers 1, ..., N.

Every member, except the member numbered 1, has exactly one immediate boss with a smaller ID number.

When a person X is the immediate boss of a person Y, the person Y is said to be an immediate subordinate of the person X.

You are given the information that the immediate boss of the member numbered i is the member numbered A_i. For each member, find how many immediate subordinates it has.

Constraints

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

1 \leq A_i < i

Input

Input is given from Standard Input in the following format:

N
A_2 ... A_N

Output

For each of the members numbered 1, 2, ..., N, print the number of immediate subordinates it has, in its own line.

Sample Input 1

5
1 1 2 2

Sample Output 1

2
2
0
0
0

The member numbered 1 has two immediate subordinates: the members numbered 2 and 3.

The member numbered 2 has two immediate subordinates: the members numbered 4 and 5.

The members numbered 3, 4, and 5 do not have immediate subordinates.

Sample Input 2

10
1 1 1 1 1 1 1 1 1

Sample Output 2

9
0
0
0
0
0
0
0
0
0

Sample Input 3

7
1 2 3 4 5 6

Sample Output 3

1
1
1
1
1
1
0


Scanf.scanf "%d" (fun n ->
    let a = Array.make (n + 1) 0 in
    for i = 2 to n do
        Scanf.scanf " %d" (fun j -> a.(j) <- a.(j) + 1)
    done;
    for i = 1 to n do
        Printf.printf "%d\n" a.(i)
    done
)


Scanf.scanf "%d %d" (fun n k ->
    let m = 1_000_000_000 + 7 in
    let (+@) a b = (a + b) mod m in
    let rec loop i acc =
        if i > n + 1 then acc else
            let mi = i * (i - 1) / 2 in
            let ma = n * i - mi in
            loop (i + 1) (acc +@ (ma - mi + 1))
    in
    loop k 0 |> Printf.printf "%d\n"
)


open Base
open Stdio

(* Core.read_line is deprecated *)
let read_line () =
    (* Out_channel.(flush stdout); *)
    In_channel.(input_line_exn stdin)

let mapt2 ~f (a, b) = (f a, f b)

let n = Int.of_string @@ read_line ()

let sums = Array.create ~len:(n+1) 0
let neg_res = Array.create ~len:(n+1) 0L
let g_hd = Array.create ~len:(n+1) 0
let g_nx = Array.create ~len:(2*n) 0
let g_to = Array.create ~len:(2*n) 0
let g_len = ref 0
let g_add u v =
    g_len := !g_len + 1;
    g_nx.(!g_len) <- g_hd.(u);
    g_to.(!g_len) <- v;
    g_hd.(u) <- !g_len

let colors = read_line () |> String.split ~on:' ' |> List.map ~f:Int.of_string
    |> List.cons 0 |> Array.of_list

let rec dfs ?(cur=1) ?(par=0) () =
    let memo = sums.(colors.(par)) in
    let rec loop e acc = if e = 0 then acc else
        let ch = g_to.(e) in
        let ret = if ch = par then 0 else dfs ~cur:ch ~par:cur () in
        loop g_nx.(e) (ret + acc)
    in
    let tree_size = loop (g_hd.(cur)) 0 in

    sums.(colors.(cur)) <- sums.(colors.(cur)) + 1;
    let component_size = Int64.of_int @@ tree_size - (sums.(colors.(par)) - memo) in
    neg_res.(colors.(par)) <- Int64.(neg_res.(colors.(par)) + component_size * (component_size + 1L) / 2L);
    sums.(colors.(par)) <- tree_size + memo;
    tree_size

let _ =
    for i = 0 to n - 2 do
        let a, b = read_line () |> String.lsplit2_exn ~on:' ' |> mapt2 ~f:Int.of_string in
        g_add a b;
        g_add b a
    done

let _ =
    ignore (dfs ());
    for i = 1 to n do
        let component_size = Int64.of_int @@ n - sums.(i) in
        let n64 = Int64.of_int n in
        let res = Int64.(n64 * (n64 + 1L) / 2L - component_size * (component_size + 1L) / 2L - neg_res.(i)) in
        printf "%Ld\n" res
    done

Score : 600 points

Problem Statement

We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i.
Additionally, each vertex is painted in a color, and the color of Vertex i is c_i. Here, the color of each vertex is represented by an integer between 1 and N (inclusive). The same integer corresponds to the same color; different integers correspond to different colors.

For each k=1, 2, ..., N, solve the following problem:

Find the number of simple paths that visit a vertex painted in the color k one or more times.

Note: The simple paths from Vertex u to v and from v to u are not distinguished.

Constraints

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

1 \leq c_i \leq N

1 \leq a_i,b_i \leq N

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
c_1 c_2 ... c_N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

Print the answers for k = 1, 2, ..., N in order, each in its own line.

Sample Input 1

3
1 2 1
1 2
2 3

Sample Output 1

5
4
0

Let P_{i,j} denote the simple path connecting Vertex i and j.

There are 5 simple paths that visit a vertex painted in the color 1 one or more times:

P_{1,1}\,,\,
P_{1,2}\,,\,
P_{1,3}\,,\,
P_{2,3}\,,\,
P_{3,3}

There are 4 simple paths that visit a vertex painted in the color 2 one or more times:

P_{1,2}\,,\,
P_{1,3}\,,\,
P_{2,2}\,,\,
P_{2,3}

There are no simple paths that visit a vertex painted in the color 3 one or more times.

Sample Input 2

1
1

Sample Output 2

1

Sample Input 3

2
1 2
1 2

Sample Output 3

2
2

Sample Input 4

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

Sample Output 4

5
8
10
5
5

Sample Input 5

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

Sample Output 5

18
15
0
14
23
0
23
0


(* unihernandez22
 * https://atcoder.jp/contests/abc162/tasks/abc162_b
 * implementation
 * *)

Printf.printf "%d\n" @@
Scanf.scanf "%d\n" @@ fun n ->
    let ans = ref 0 in
    for i = 1 to n do
        if (i mod 3 != 0) && (i mod 5 != 0) then
            ans := !ans + i;
    done;
    !ans

Score : 200 points

Problem Statement

Let us define the FizzBuzz sequence a_1,a_2,... as follows:

If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.

If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.

If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.

If none of the above holds, a_i=i.

Find the sum of all numbers among the first N terms of the FizzBuzz sequence.

Constraints

1 \leq N \leq 10^6

Input

Input is given from Standard Input in the following format:

N

Output

Print the sum of all numbers among the first N terms of the FizzBuzz sequence.

Sample Input 1

15

Sample Output 1

60

The first 15 terms of the FizzBuzz sequence are:

1,2,\mbox{Fizz},4,\mbox{Buzz},\mbox{Fizz},7,8,\mbox{Fizz},\mbox{Buzz},11,\mbox{Fizz},13,14,\mbox{FizzBuzz}

Among them, numbers are 1,2,4,7,8,11,13,14, and the sum of them is 60.

Sample Input 2

1000000

Sample Output 2

266666333332

Watch out for overflow.


open Batteries
let n = read_int()
let rec loop i sum =
  if i > n then sum else
  if i mod 5 = 0 || i mod 3 = 0 then loop (i+1) sum else
  loop (i+1) (sum+i)

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


let rec check n k =
  if n <= k && n <= abs (n - k) then n
  else if n <= k && n > abs (n - k) then abs (n - k)
  else
    let md = n mod k in
    check md k

let () =
  let n, k = Scanf.scanf "%d %d\n" (fun a b -> (a, b)) in
  print_int @@ check n k

Score : 300 points

Problem Statement

Given any integer x, Aoki can do the operation below.

Operation: Replace x with the absolute difference of x and K.

You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times.

Constraints

0 ≤ N ≤ 10^{18}

1 ≤ K ≤ 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the minimum possible value taken by N after Aoki does the operation zero or more times.

Sample Input 1

7 4

Sample Output 1

1

Initially, N=7.

After one operation, N becomes |7-4| = 3.

After two operations, N becomes |3-4| = 1, which is the minimum value taken by N.

Sample Input 2

2 6

Sample Output 2

2

N=2 after zero operations is the minimum.

Sample Input 3

1000000000000000000 1

Sample Output 3

0


let rec solve n k candidates =
  if k < n
  then List.nth candidates k
  else
    let n', candidates' =
      List.fold_left (fun (n', candidates') x ->
        match x mod 10 with
        | 0 -> (n' + 2, 10 * x + 1 :: 10 * x :: candidates')
        | 9 -> (n' + 2, 10 * x + 9 :: 10 * x + 8 :: candidates')
        | _ -> (n' + 3, 10 * x + x mod 10 + 1 :: 10 * x + x mod 10 :: 10 * x + x mod 10 - 1 :: candidates')) (0, []) candidates in
    solve n' (k - n) @@ List.rev candidates'
let solve k = solve 9 k [1; 2; 3; 4; 5; 6; 7; 8; 9]

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

Score : 400 points

Problem Statement

A positive integer X is said to be a lunlun number if and only if the following condition is satisfied:

In the base ten representation of X (without leading zeros), for every pair of two adjacent digits, the absolute difference of those digits is at most 1.

For example, 1234, 1, and 334 are lunlun numbers, while none of 31415, 119, or 13579 is.

You are given a positive integer K. Find the K-th smallest lunlun number.

Constraints

1 \leq K \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K

Output

Print the answer.

Sample Input 1

15

Sample Output 1

23

We will list the 15 smallest lunlun numbers in ascending order:

1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
21,
22,
23.

Thus, the answer is 23.

Sample Input 2

1

Sample Output 2

1

Sample Input 3

13

Sample Output 3

21

Sample Input 4

100000

Sample Output 4

3234566667

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


open Printf
open Scanf

let solve s =
  if s.[2] = s.[3] && s.[4] = s.[5] then "Yes" else "No"

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

Score : 100 points

Problem Statement

A string of length 6 consisting of lowercase English letters is said to be coffee-like if and only if its 3-rd and 4-th characters are equal and its 5-th and 6-th characters are also equal.

Given a string S, determine whether it is coffee-like.

Constraints

S is a string of length 6 consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S is coffee-like, print Yes; otherwise, print No.

Sample Input 1

sippuu

Sample Output 1

Yes

In sippuu, the 3-rd and 4-th characters are equal, and the 5-th and 6-th characters are also equal.

Sample Input 2

iphone

Sample Output 2

No

Sample Input 3

coffee

Sample Output 3

Yes


let hisHappiness money =
	let hapiness500 = money / 500 * 1000 in
	let hapiness5 = (money mod 1000) / 5 * 5 in
	hapiness500 + hapiness5 ;;
let () = read_line() |> int_of_string |> hisHappiness |> string_of_int |> print_string

Score : 200 points

Problem Statement

Takahashi loves gold coins. He gains 1000 happiness points for each 500-yen coin he has and gains 5 happiness points for each 5-yen coin he has. (Yen is the currency of Japan.)

Takahashi has X yen. If he exchanges his money so that he will gain the most happiness points, how many happiness points will he earn?

(We assume that there are six kinds of coins available: 500-yen, 100-yen, 50-yen, 10-yen, 5-yen, and 1-yen coins.)

Constraints

0 \leq X \leq 10^9

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the maximum number of happiness points that can be earned.

Sample Input 1

1024

Sample Output 1

2020

By exchanging his money so that he gets two 500-yen coins and four 5-yen coins, he gains 2020 happiness points, which is the maximum number of happiness points that can be earned.

Sample Input 2

0

Sample Output 2

0

He is penniless - or yenless.

Sample Input 3

1000000000

Sample Output 3

2000000000

He is a billionaire - in yen.


open Printf
open Scanf

let solve x =
  let fhy = x / 500 in
  let fy = (x - fhy * 500) / 5 in
  fhy * 1000 + fy * 5

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


open Batteries
let add ary value =
  Array.append ary [|value|]

let get_graph n =
  Array.init n (fun a -> [])

(* 頂点数と辺数 *)
let n, x, y = Scanf.sscanf (read_line ()) "%d %d %d" (fun n x y ->
    n, x, y
  )
let graph = get_graph n

let _ =
  let rec loop i =
    if i < n then
      (if i = 0 then
        (graph.(i) <- graph.(i) @ [(i+1)])
        else if i = n-1 then (graph.(i) <- graph.(i) @ [(i-1)]) else graph.(i) <- graph.(i) @ [(i+1); (i-1)];
      loop (i+1))
    else ()
  in loop 0;
  graph.(x-1) <- graph.(x-1) @ [y-1];
  graph.(y-1) <- graph.(y-1) @ [x-1]

let get_nagasa x =
(* 初期条件 (頂点 x を初期ノードとする) *)
(* BFS のためのデータ構造 *)
  let dist = Array.init n (fun a -> -1) in
  let que = Queue.create () in
  let _ =
    dist.(x) <- 0;
    Queue.push x que in
  let rec loop () =
    (* BFS 開始 (キューが空になるまで探索を行う) *)
    if Queue.is_empty que = false then
    (* キューから先頭頂点を取り出す *)
    let v = Queue.take que in
    (* v から辿れる頂点をすべて調べる *)
    let rec search lst =
      match lst with
      | [] -> ()
      | first :: rest ->
        (* すでに発見済みの頂点は探索しない *)
        if dist.(first) = -1 then
          (* 新たな白色頂点 first について距離情報を更新してキューに追加する *)
          (dist.(first) <- (dist.(v) + 1);
          Queue.push first que;
          search rest)
        else search rest
    in search graph.(v);
    loop () else ()
  in let rec get k =
    if k = n then [] else dist.(k) :: get (k+1)
  in loop (); get 0

let all =
  let rec loop i =
    if i = n then [] else (get_nagasa i) @ (loop (i+1))
  in loop 0

let count_list lst target =
  let rec loop l =
    match l with
    | [] -> 0
    | first :: rest -> if first = target then 1 + loop rest else loop rest
  in loop lst

let _ =
  for i = 1 to n-1 do
    Printf.printf "%d\n" @@ (count_list all i / 2)
  done

Score : 400 points

Problem Statement

We have an undirected graph G with N vertices numbered 1 to N and N edges as follows:

For each i=1,2,...,N-1, there is an edge between Vertex i and Vertex i+1.

There is an edge between Vertex X and Vertex Y.

For each k=1,2,...,N-1, solve the problem below:

Find the number of pairs of integers (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j in G is k.

Constraints

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

1 \leq X,Y \leq N

X+1 < Y

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X Y

Output

For each k=1, 2, ..., N-1 in this order, print a line containing the answer to the problem.

Sample Input 1

5 2 4

Sample Output 1

5
4
1
0

The graph in this input is as follows:

There are five pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 1: (1,2)\,,(2,3)\,,(2,4)\,,(3,4)\,,(4,5).

There are four pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 2: (1,3)\,,(1,4)\,,(2,5)\,,(3,5).

There is one pair (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 3: (1,5).

There are no pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 4.

Sample Input 2

3 1 3

Sample Output 2

3
0

The graph in this input is as follows:

Sample Input 3

7 3 7

Sample Output 3

7
8
4
2
0
0

Sample Input 4

10 4 8

Sample Output 4

10
12
10
8
4
1
0
0
0


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

Score : 100 points

Problem Statement

We have N+M balls, each of which has an integer written on it.

It is known that:

The numbers written on N of the balls are even.

The numbers written on M of the balls are odd.

Find the number of ways to choose two of the N+M balls (disregarding order) so that the sum of the numbers written on them is even.

It can be shown that this count does not depend on the actual values written on the balls.

Constraints

0 \leq N,M \leq 100

2 \leq N+M

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the answer.

Sample Input 1

2 1

Sample Output 1

1

For example, let us assume that the numbers written on the three balls are 1,2,4.

If we choose the two balls with 1 and 2, the sum is odd;

If we choose the two balls with 1 and 4, the sum is odd;

If we choose the two balls with 2 and 4, the sum is even.

Thus, the answer is 1.

Sample Input 2

4 3

Sample Output 2

9

Sample Input 3

1 1

Sample Output 3

0

Sample Input 4

13 3

Sample Output 4

81

Sample Input 5

0 3

Sample Output 5

3


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


open Batteries
let cut st str = String.init (String.length str - st) (fun a -> String.get str (a+st))
let get num str = String.init num (fun a -> String.get str a)
let s = read_line ()
let n = String.length s
let n_1 = get ((n-1)/2) s
let n_2 = cut ((n+3)/2-1) s
let half_n1 = get ((String.length n_1)/2) n_1
let end_n1 = cut ((String.length n_1)/2+1) n_1
let half_n2 = get ((String.length n_2)/2) n_2
let end_n2 = cut ((String.length n_2)/2+1) n_2

let _ = (if half_n1 = end_n1 && half_n2 = end_n2 && n_1 = n_2 then "Yes" else "No") |> print_endline

Score : 200 points

Problem Statement

A string S of an odd length is said to be a strong palindrome if and only if all of the following conditions are satisfied:

S is a palindrome.

Let N be the length of S. The string formed by the 1-st through ((N-1)/2)-th characters of S is a palindrome.

The string consisting of the (N+3)/2-st through N-th characters of S is a palindrome.

Determine whether S is a strong palindrome.

Constraints

S consists of lowercase English letters.

The length of S is an odd number between 3 and 99 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

If S is a strong palindrome, print Yes;
otherwise, print No.

Sample Input 1

akasaka

Sample Output 1

Yes

S is akasaka.

The string formed by the 1-st through the 3-rd characters is aka.

The string formed by the 5-th through the 7-th characters is aka.
All of these are palindromes, so S is a strong palindrome.

Sample Input 2

level

Sample Output 2

No

Sample Input 3

atcoder

Sample Output 3

No


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 = if n <= m then  List.range n `To m else []
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 []

module ModOp = struct
  let prime = 1_000_000_007
  let (-) a b = (a + prime - b) mod prime
  let (+) a b = (a + b) mod prime
  let ( * ) a b = (a * b) mod prime

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

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 atoi c = Char.code c - Char.code '0'
let itoa n = Char.chr (n + Char.code '0')

let rec powerset = function
  | [] -> [[]]
  | hd::tl ->
    let pws =  powerset tl in
    pws @ ListL.map pws ~f:(fun pw -> hd::pw)

let permutations l =
  let rec interleave x = function
    | [] -> [[x]]
    | (hd::tl) as l ->
      (x::l) :: (interleave x tl |> ListL.map ~f:(fun l -> hd::l)) in
  let rec aux = function
    | [] -> [[]]
    | a::rest ->
      aux rest |> ListL.map ~f:(interleave a) |> List.concat in
  aux 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 l = scan "%f" id
let () =
  let i = l/.3.0 in
  Printf.printf "%f\n" @@ Float.pow i 3.0

Score : 300 points

Problem Statement

Given is a positive integer L.
Find the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.

Constraints

1 ≤ L ≤ 1000

L is an integer.

Input

Input is given from Standard Input in the following format:

L

Output

Print the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.
Your output is considered correct if its absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

3

Sample Output 1

1.000000000000

For example, a rectangular cuboid whose dimensions are 0.8, 1, and 1.2 has a volume of 0.96.

On the other hand, if the dimensions are 1, 1, and 1, the volume of the rectangular cuboid is 1, which is greater.

Sample Input 2

999

Sample Output 2

36926037.000000000000


let _ = Printf.printf "%f\n" ((read_float () /. 3.) ** 3.)


let l = read_int ()
let s = float_of_int (l/3)
let _ = (if l mod 3 = 0 then s ** 3. else
        if l mod 3 = 1 then s *. (s +. 0.5) *. (s +. 0.5) else
        (s +. 1.) *. (s +. 0.5) *. (s +. 0.5)) |> Printf.printf "%f\n"


let split_string ?(pattern="") = Str.split @@ Str.regexp pattern
let n = Scanf.sscanf (read_line ()) "%d" @@ fun n -> n
let a = read_line () |> split_string ~pattern:" " |> List.map int_of_string

let cnt = a
  |> List.sort compare
  |> List.fold_left (fun acc n ->
    match acc with
      | [] -> [(n, 1)]
      | (v, cnt) as t :: xs -> if v = n then (v, cnt + 1) :: xs else (n, 1) :: t :: xs
  ) []

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

let map = List.fold_left (fun map (k, v) -> IntMap.add k v map) IntMap.empty cnt

let sum = List.fold_left (fun sum (_, cnt) -> sum + cnt * (cnt - 1) / 2) 0 cnt

let () = List.iter(fun i ->
  Printf.printf "%d\n" @@ sum - IntMap.find i map + 1
) a

Score : 400 points

Problem Statement

We have N balls. The i-th ball has an integer A_i written on it.

For each k=1, 2, ..., N, solve the following problem and print the answer.

Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.

Constraints

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

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For each k=1,2,...,N, print a line containing the answer.

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.

From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5


let calc a b c =
  let concat s ls t lt =
    let rec f i =
      let rec g j =
        if i+j >= ls || j >= lt then true
        else (s (i+j) = '?' || t j = '?' || s (i+j) = t j) && g (j+1)
      in
      if g 0 then i else f (i+1)
    in f 0
  in
  let la, lb, lc = String.(length a, length b, length c) in
  let abi = concat (fun i -> a.[i]) la (fun i -> b.[i]) lb in
  let lab = max la (abi + lb) in
  let abci = concat (fun i -> if i < abi then a.[i] else b.[i-abi]) lab (fun i -> c.[i]) lc in
  max lab (abci + lc)

let () =
  Scanf.scanf "%s %s %s" @@ fun a b c ->
    [calc a b c; calc a c b; calc b a c; calc b c a; calc c a b; calc c b a]
    |> List.fold_left min 10000 |> Printf.printf "%d\n"

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 () = Scanf.scanf "%d %d %d\n" @@ fun a b m ->
  let as_ = Array.init a @@ fun _ -> Scanf.scanf "%d%c" @@ fun a _ -> a in
  let bs = Array.init b @@ fun _ -> Scanf.scanf "%d%c" @@ fun b _ -> b in
  let xycs = Array.init m @@ fun _ -> Scanf.scanf "%d %d %d\n" @@ fun x y c -> x, y, c in
  Printf.printf "%d\n" @@
  Array.fold_right (fun (x, y, c) -> min @@ as_.(x - 1) + bs.(y - 1) - c) xycs @@
  Array.fold_left min max_int as_ + Array.fold_left min max_int bs

Score : 200 points

Problem Statement

You are visiting a large electronics store to buy a refrigerator and a microwave.

The store sells A kinds of refrigerators and B kinds of microwaves. The i-th refrigerator ( 1 \le i \le A ) is sold at a_i yen (the currency of Japan), and the j-th microwave ( 1 \le j \le B ) is sold at b_j yen.

You have M discount tickets. With the i-th ticket ( 1 \le i \le M ), you can get a discount of c_i yen from the total price when buying the x_i-th refrigerator and the y_i-th microwave together. Only one ticket can be used at a time.

You are planning to buy one refrigerator and one microwave. Find the minimum amount of money required.

Constraints

All values in input are integers.

1 \le A \le 10^5

1 \le B \le 10^5

1 \le M \le 10^5

1 \le a_i , b_i , c_i \le 10^5

1 \le x_i \le A

1 \le y_i \le B

c_i \le a_{x_i} + b_{y_i}

Input

Input is given from Standard Input in the following format:

A B M
a_1 a_2 ... a_A
b_1 b_2 ... b_B
x_1 y_1 c_1
\vdots
x_M y_M c_M

Output

Print the answer.

Sample Input 1

2 3 1
3 3
3 3 3
1 2 1

Sample Output 1

5

With the ticket, you can get the 1-st refrigerator and the 2-nd microwave for 3+3-1=5 yen.

Sample Input 2

1 1 2
10
10
1 1 5
1 1 10

Sample Output 2

10

Note that you cannot use more than one ticket at a time.

Sample Input 3

2 2 1
3 5
3 5
2 2 2

Sample Output 3

6

You can get the 1-st refrigerator and the 1-st microwave for 6 yen, which is the minimum amount to pay in this case.
Note that using a ticket is optional.


let s = read_line () in
print_endline (if s = "AAA" || s = "BBB" then "No" else "Yes")

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.


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

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.


let () =
  let ans = ref 0 in
  Scanf.scanf "%d %d\n" @@ fun a b ->
  for y = 1 to 1010 do
    if y * 8 / 100 = a && y / 10 = b then ans := y
  done;
  print_int (if !ans = 0 then -1 else !ans)

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.


let split_string ?(pattern="") = Str.split @@ Str.regexp pattern
let s = read_line () |> split_string
let q = Scanf.sscanf (read_line ()) "%d" @@ fun q -> q
let qq = Array.init q @@ fun _ -> read_line () |> split_string ~pattern:" "

let (s, t) = Array.fold_left (fun (s, t) -> function
  | "1" :: [] -> (t, s)
  | "2" :: f :: c :: [] -> if f = "1" then (c :: s, t) else (s, c :: t)
  | _ -> failwith ""
) (s, []) qq

let () =
  List.iter print_string s;
  List.iter print_string (List.rev t);
  print_newline ()

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


Scanf.sscanf (read_line ()) "%d %d" (fun n p ->
    let s = read_line () in
    if p = 2 || p = 5 then (
        let rec loop i d acc =
            if i < 0 then acc else (
                let c = int_of_char s.[i] - int_of_char '0' in
                let d = if c mod p = 0 then d + 1 else d in
                let acc = acc + d in
                loop (i - 1) d acc
            )
        in
        loop (n - 1) 0 0 |> Printf.printf "%d\n"
    ) else (
        let dp = Array.make p 0 in
        dp.(0) <- 1;
        let rec loop i mul prev acc =
            if i < 0 then acc else
                let c = int_of_char s.[i] - int_of_char '0' in
                let d = (c * mul + prev) mod p in
                let () = dp.(d) <- dp.(d) + 1 in
                loop (i - 1) ((mul * 10) mod p) d (acc + dp.(d) - 1)
        in
        loop (n - 1) 1 0 0 |> Printf.printf "%d\n"
    )
)

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


let () =
  let num_card =
    Array.init 3 @@ fun _ ->
      Array.init 3 @@ fun _ ->
        Scanf.scanf " %d" @@ Fun.id in
  let mark = Array.make_matrix 3 3 false in
  Scanf.scanf "%d\n" @@ fun n ->
    for i = 0 to n - 1 do
      Scanf.scanf "%d\n" @@ fun b ->
        for i = 0 to 2 do
          for j = 0 to 2 do
            if num_card.(i).(j) = b then mark.(i).(j) <- true
          done
        done
    done;
    print_endline @@
    if
      mark.(0) = [| true; true; true |] ||
      mark.(1) = [| true; true; true |] ||
      mark.(2) = [| true; true; true |] ||
      Array.init 3 (fun i -> mark.(i).(0)) = [| true; true; true |] ||
      Array.init 3 (fun i -> mark.(i).(1)) = [| true; true; true |] ||
      Array.init 3 (fun i -> mark.(i).(2)) = [| true; true; true |] ||
      Array.init 3 (fun i -> mark.(i).(i)) = [| true; true; true |] ||
      Array.init 3 (fun i -> mark.(2-i).(i)) = [| true; true; true |]
    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 inp_matrix : 'a array -> int -> int -> unit = fun a m n ->
  a.(0).(0) <- Scanf.scanf "%d" (fun x -> x);
  for i = 0 to m - 1 do
    for j = 0 to n - 1 do
      if (i <> 0 || j <> 0) then
        a.(i).(j) <- Scanf.scanf " %d" (fun x -> x)
    done
  done;
  let _ = Scanf.scanf "%c" (fun x -> x) in
  ()

let rec inp_loop : (unit -> 'a) -> int -> 'a list = fun inp_fun n ->
  if n = 0 then []
  else
    let x = inp_fun () in
    x :: inp_loop inp_fun (n - 1)

let hole a m n k =
  for i = 0 to m - 1 do
    for j = 0 to n - 1 do
      if a.(i).(j) == k then
        a.(i).(j) <- (-1)
    done
  done

let calc_ans a =
  let ans = ref "No" in
  for i = 0 to 2 do
    if (a.(i).(0) == a.(i).(1) && a.(i).(0) == a.(i).(2)) then ans := "Yes"
  done;
  for j = 0 to 2 do
    if (a.(0).(j) == a.(1).(j) && a.(0).(j) == a.(2).(j)) then ans := "Yes"
  done;
  if (a.(0).(0) == a.(1).(1) && a.(2).(2) == a.(1).(1) || a.(0).(2) == a.(1).(1) && a.(0).(2) == a.(2).(0)) then ans := "Yes";
  !ans

let () =
  let a = Array.make_matrix 3 3 0 in
  let () = inp_matrix a 3 3 in
  let n = Scanf.sscanf (input_line stdin) "%d" (fun a -> a) in
  let l = inp_loop (fun () -> Scanf.sscanf (input_line stdin) "%d" (fun a -> a)) n in
  List.iter  (hole a 3 3) l;
  let ans = calc_ans a
  in
  print_endline ans


let inp_matrix : 'a array -> int -> int -> unit = fun a m n ->
  for i = 0 to m - 1 do
    for j = 0 to n - 1 do
      a.(i).(j) <- Scanf.scanf " %d" (fun x -> x)
    done
  done

let rec inp_loop : (unit -> 'a) -> int -> 'a list = fun inp_fun n ->
  if n = 0 then []
  else
    let x = inp_fun () in
    x :: inp_loop inp_fun (n - 1)

let hole a m n k =
  for i = 0 to m - 1 do
    for j = 0 to n - 1 do
      if a.(i).(j) == k then
        a.(i).(j) <- (-1)
    done
  done

let calc_ans a =
  let ans = ref "No" in
  for i = 0 to 2 do
    if (a.(i).(0) == a.(i).(1) && a.(i).(0) == a.(i).(2)) then ans := "Yes"
  done;
  for j = 0 to 2 do
    if (a.(0).(j) == a.(1).(j) && a.(0).(j) == a.(2).(j)) then ans := "Yes"
  done;
  if (a.(0).(0) == a.(1).(1) && a.(2).(2) == a.(1).(1) || a.(0).(2) == a.(1).(1) && a.(0).(2) == a.(2).(0)) then ans := "Yes";
  !ans

let () =
  let a = Array.make_matrix 3 3 0 in
  let () = inp_matrix a 3 3 in
  let n = Scanf.sscanf (input_line stdin) "%d" (fun a -> a) in
  let l = inp_loop (fun () -> Scanf.sscanf (input_line stdin) "%d" (fun a -> a)) n in
  List.iter  (hole a 3 3) l;
  let ans = calc_ans a
  in
  print_endline ans


let check_bingo board hits =
  (* ビンゴ対象となる配列のインデックス *)
  let check_list = [[0;1;2]; [3;4;5]; [6;7;8]; [0;3;6]; [1;4;7]; [2;5;8]; [0;4;8]; [2;4;6]] in
  (* インデックスを値に変換 *)
  let rec get_cell board l cell_values =
    match l with
    | [] -> cell_values
    | x::xs ->
       get_cell board xs ((Array.get board x)::cell_values)
  in
  (* ある列/行/斜めがビンゴかチェック *)
  let is_bingo l hits = List.for_all (fun x -> (List.mem x hits)) l in
  let rec check_bingo_sub board l =
    match l with
    | [] -> "No"
    | x::xs ->
       let check_cell_list = get_cell board x [] in
       let result = is_bingo check_cell_list hits in
       if result then "Yes" else check_bingo_sub board xs
  in
  check_bingo_sub board check_list

(* 盤面入力 *)
let rec input_boards n board =
  match n with
  | 0 -> Array.of_list (List.rev board)
  | m ->
     let new_board = Scanf.scanf "%d %d %d\n" (fun a b c -> c :: b :: a :: board) in
     input_boards (m-1) new_board

(* 当たりの入力 *)
let rec input_hits n hits =
  match n with
  | 0 -> hits
  | m ->
     let input = read_int () in
     input_hits (m-1) (input::hits)

let _ =
  let board = input_boards 3 [] in
  let n_hit = read_int () in
  let hits  = input_hits n_hit [] in
  let ans = check_bingo board hits in
  print_endline ans


let () =
    let main () =
        let ar = Array.make 101 1024 in
        for i = 0 to 8 do
            Scanf.scanf " %d" (fun a -> ar.(a) <- 1 lsl i)
        done;
        let n = Scanf.scanf " %d" (fun n -> n) in
        let rec loop i map =
            if i = 0 then map else
                let b = Scanf.scanf " %d" (fun b -> b) in
                loop (i - 1) (map lor ar.(b))
        in
        let m = loop n 0 in
        let l = [
            (1 lsl 0) lor (1 lsl 1) lor (1 lsl 2) ;
            (1 lsl 3) lor (1 lsl 4) lor (1 lsl 5) ;
            (1 lsl 6) lor (1 lsl 7) lor (1 lsl 8) ;
            (1 lsl 0) lor (1 lsl 3) lor (1 lsl 6) ;
            (1 lsl 1) lor (1 lsl 4) lor (1 lsl 7) ;
            (1 lsl 2) lor (1 lsl 5) lor (1 lsl 8) ;
            (1 lsl 0) lor (1 lsl 4) lor (1 lsl 8) ;
            (1 lsl 2) lor (1 lsl 4) lor (1 lsl 6)
        ]
        in
        print_endline (if List.exists (fun a -> a land m = a) l then "Yes" else "No")
    in
    main ()


let () =
    let main () =
        let ar = Array.make 101 1024 in
        for i = 0 to 8 do
            Scanf.scanf " %d" (fun a -> ar.(a) <- i)
        done;
        let n = Scanf.scanf " %d" (fun n -> n) in
        let rec loop i map =
            if i = 0 then map else
                let b = Scanf.scanf " %d" (fun b -> b) in
                loop (i - 1) (map lor ar.(b))
        in
        let m = loop n 0 in
        let l = [
            (1 lsl 0) lor (1 lsl 1) lor (1 lsl 2) ;
            (1 lsl 3) lor (1 lsl 4) lor (1 lsl 5) ;
            (1 lsl 6) lor (1 lsl 7) lor (1 lsl 8) ;
            (1 lsl 0) lor (1 lsl 3) lor (1 lsl 6) ;
            (1 lsl 1) lor (1 lsl 4) lor (1 lsl 7) ;
            (1 lsl 2) lor (1 lsl 5) lor (1 lsl 8) ;
            (1 lsl 0) lor (1 lsl 4) lor (1 lsl 8) ;
            (1 lsl 2) lor (1 lsl 4) lor (1 lsl 6)
        ]
        in
        print_endline (if List.exists (fun a -> a land m = a) l then "Yes" else "No")
    in
    main ()


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 [] 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) <- b :: friend.(a);
            friend.(b) <- 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 [||] 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 next = [] then acc else (collect acc [] next)
                    | hd :: tl ->
                            let acc = hd :: acc in
                            let next = List.fold_left (fun next v -> if clust.(v) <> -1 then next else (clust.(v) <- cl; v :: next)) next friend.(hd) in
                            collect acc next tl
                in
                clust.(i) <- cl;
                let acc = collect [] [i] [] in
                cm.(cl) <- Array.of_list acc;
                cl + 1
            )
            in
            let b = Array.fold_left (fun acc v -> if S.mem v block.(i) then acc + 1 else acc) 0 cm.(clust.(i)) in

            (* let c = S.cardinal (S.diff (S.diff cm.(clust.(i)) block.(i)) friend.(i)) in *)
            let c = Array.length cm.(clust.(i)) - List.length (friend.(i)) - b 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
        let ss = Array.init n (fun i ->
            let code = int_of_char s.[i] - 97 in
            letter.(code) <- S.add (i + 1) letter.(code);
            code
        )
        in
        let q = read_line () |> int_of_string in
        for i = 1 to q do
            Scanf.sscanf (read_line ()) "%d %d %s" (fun command a b ->
                if command = 1 then (
                    let c = ss.(a - 1) in
                    letter.(c) <- S.remove a letter.(c);
                    let c = int_of_char b.[0] - 97 in
                    letter.(c) <- S.add a letter.(c);
                    ss.(a - 1) <- c;
                ) else (
                    let r = int_of_string b 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 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 + 1) letter.(code)
        done;
        let q = read_int () in
        for i = 1 to q do
            Scanf.sscanf (read_line ()) "%d %d %s" (fun command a b ->
                if command = 1 then (
                    let c = int_of_char s.[a - 1] - 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 r = int_of_string b 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 ()


let () = Scanf.scanf "%d %d" @@ fun n r ->
  Printf.printf "%d\n" @@
  if 10 <= n
  then r
  else r + 100 * (10 - 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


Scanf.scanf "%d %d" (fun n k ->
    let rec loop i acc =
        if i = 0 then acc else loop (i / k) (acc + 1)
    in
    loop n 0 |> Printf.printf "%d\n"
)

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


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 n = scan "%d" identity
let xs = scan_list Int.of_string

let () =
  let half = (List.sum xs) / 2 in
  let half2 = (1+List.sum xs) /2 in
  let n = List.sum @@ List.map (fun n -> let k = n - half in k*k) xs in
  let m = List.sum @@ List.map (fun n -> let k = n - half2 in k*k) 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


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


let poor a b c =
  if a = b && a = c then "No"
  else if a = b && a <> c then "Yes"
  else if a = c && a <> b then "Yes"
  else if b = c && b <> a then "Yes"
  else "No"

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

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 () = Scanf.scanf "%d %d %d" @@ fun a b c -> Printf.printf "%s\n" @@
  if a = b && a <> c
  then "Yes"
  else if a = c && a <> b
  then "Yes"
  else if b = c && a <> b
  then "Yes"
  else "No"


open Printf
open Scanf

let solve a b c =
  let n = List.length @@ List.filter (fun x -> x = 0) [a-b; b-c; c-a] in
  if n = 1 then "Yes" else "No"

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


let () =
	let a, b, c = Scanf.scanf "%d %d %d\n" (fun a b c -> a, b, c) in
    Printf.printf
    	(if a = b && a <> c then "Yes"
         else if a = c && a <> b then "Yes"
         else if b = c && a <> b then "Yes"
         else "No")


let _ = print_endline (String.make (String.length @@ read_line ()) 'x')

Score : 200 points

Problem Statement

Given is a string S. Replace every character in S with x and print the result.

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

Replace every character in S with x and print the result.

Sample Input 1

sardine

Sample Output 1

xxxxxxx

Replacing every character in S with x results in xxxxxxx.

Sample Input 2

xxxx

Sample Output 2

xxxx

Sample Input 3

gone

Sample Output 3

xxxx


open Printf
open Scanf

let solve s = String.map (fun _ -> 'x') s

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


open Batteries
let n = read_int ()
let a = read_line () |> String.split_on_char ' ' |> List.sort compare

let _ =
  let rec loop lst =
    match lst with
    | [] -> "YES"
    | first :: rest -> if first = List.hd rest then "NO" else loop rest
  in loop a |> print_endline

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


open Batteries
let n, k = Scanf.sscanf (read_line()) "%d %d" (fun n k ->n,k)
let dp = Array.init n @@ fun a -> 0.
let ex i = (1. +. (float_of_int i)) /. 2.

let a = Array.init n @@ fun _ -> Scanf.scanf " %d" @@ fun a -> ex a

let rec loop i =
  if i >= n then () else
  (dp.(i) <- dp.(i-1) +. a.(i); loop (i+1))

let _ = dp.(0) <- a.(0); loop 1

let rec loop1 j =
  if j + k >= n then min_float else
  max (dp.(j+k) -. dp.(j)) (loop1 @@ j+1)

let _ = Printf.printf "%.12f\n" @@ loop1 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


open Batteries
open Printf

let scan fmt f =
  Scanf.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 (n, k) = scan "%d %d" BatTuple.Tuple2.make
let ps = scan_list float_of_string

let exp n = n *. (n +. 1.0) /. 2.0 /. n

let hold_max (m, p) (x, y) =
  let n = p -. x +. y in
  (BatFloat.max m n, n)

let () =
  let sums = List.map exp ps in
  let init = BatList.fsum @@ BatList.take k sums in
  BatList.fold_left hold_max (init, init) (zip sums @@ BatList.drop k sums)
  |> BatTuple.Tuple2.first
  |> printf "%f\n"


open Batteries
open BatPrintf
open BatBigarray

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 n = scan "%s" identity
let k = scan "%d" identity

let () =
  let len = String.length n in
  let dp = Array3.create Int C_layout 2 (len+1) len in
  Array3.set dp 0 0 0 1;
  let open BatEnum in
  iter (fun i ->
      let d = int_of_string @@ String.of_char @@ String.get n (i-1) in
      iter (fun j ->
          Array3.set dp 1 i j @@ (Array3.get dp 1 (i - 1) j) * 9 +
                                 (if j > 0 then
                                    Array3.get dp 1 (i - 1) (j - 1)
                                  else 0) +
                                 if d > 0 then
                                   (d - 1) * (Array3.get dp 0 (i - 1) j) + (if j > 0 then Array3.get dp 0 (i - 1) (j - 1) else 0)
                                 else
                                   0;
          Array3.set dp 0 i j @@
          if d > 0 then
            Array3.get dp 0 (i-1) j
          else if j > 0 then
            Array3.get dp 0 (i-1) (j-1)
          else
            0
        ) (0 --^ len);
    ) (1 -- len);
  printf "%d\n" @@ Array3.get dp 1 len (len - k) + Array3.get dp 0 len (len - k)

Score : 500 points

Problem Statement

Find the number of integers between 1 and N (inclusive) that contains exactly K non-zero digits when written in base ten.

Constraints

1 \leq N < 10^{100}

1 \leq K \leq 3

Input

Input is given from Standard Input in the following format:

N
K

Output

Print the count.

Sample Input 1

100
1

Sample Output 1

19

The following 19 integers satisfy the condition:

1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90,100

Sample Input 2

25
2

Sample Output 2

14

The following 14 integers satisfy the condition:

11,12,13,14,15,16,17,18,19,21,22,23,24,25

Sample Input 3

314159
2

Sample Output 3

937

Sample Input 4

9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
3

Sample Output 4

117879300


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

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 Printf
open Scanf

module L =
  struct
    include List

    let rec drop n = function
        [] -> []
      | h :: t when n > 0 -> drop (n - 1) t
      | l -> l

    let init n f =
      let rec loop x =
        if x = n then []
        else let y = f x in y :: loop (x + 1)
      in loop 0
  end

let id x = x

let flip f x y = f y x

let solve n k =
  L.fold_left (+) 0 @@ L.drop k @@ L.fast_sort (flip compare) @@ L.init n (fun _ -> scanf "%d " id)

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

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.


let () = Scanf.scanf "%c %c" @@ fun a b -> Printf.printf "%s\n" @@
  if a <= b
  then String.make (int_of_char b - 48) a
  else String.make (int_of_char a - 48) b

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


open Printf
open Scanf

let solve a b = String.make (max a b) @@ Char.chr @@ 48 + (min a b)

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