let () = Scanf.scanf "%d %d" @@ fun m d ->
  Printf.printf "%d\n" @@
  Array.fold_left (Array.fold_left ( + )) 0 @@
  Array.init m @@ fun i ->
    Array.init d @@ fun j ->
      if
        2 <= (j + 1) mod 10 &&
        2 <= (j + 1) / 10 &&
        ((j + 1) mod 10) * ((j + 1) / 10) = i + 1
      then 1
      else 0

Score : 200 points

Problem Statement

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

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

d_1 \geq 2

d_{10} \geq 2

d_1 \times d_{10} = m

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

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

Constraints

All values in input are integers.

1 \leq M \leq 100

1 \leq D \leq 99

Input

Input is given from Standard Input in the following format:

M D

Output

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

Sample Input 1

15 40

Sample Output 1

10

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

4-22

6-23

6-32

8-24

9-33

10-25

12-26

12-34

14-27

15-35

Sample Input 2

12 31

Sample Output 2

5

Sample Input 3

1 1

Sample Output 3

0


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

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

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

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

Score : 500 points

Problem Statement

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

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

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

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

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

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

Constraints

1 \leq N \leq 10^5

|S| = 2N

Each character of S is B or W.

Input

Input is given from Standard Input in the following format:

N
S

Output

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

Sample Input 1

2
BWWB

Sample Output 1

4

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

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

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

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

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

Sample Input 2

4
BWBBWWWB

Sample Output 2

288

Sample Input 3

5
WWWWWWWWWW

Sample Output 3

0

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.


(* Vicfred
 * https://atcoder.jp/contests/abc138/tasks/abc138_b
 * implementation
 * *)
let n = Scanf.scanf "%d\n" ( + ) 0
let a = Array.init n @@ fun _ -> Scanf.scanf " %f" ( +. ) 0.0
let main = Printf.printf "%.9f\n" @@
((/.) 1.0) @@
Array.fold_left (fun x y -> x +. (1.0 /. y)) 0.0 a

Score : 200 points

Problem Statement

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

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

Constraints

1 \leq N \leq 100

1 \leq A_i \leq 1000

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N

Output

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

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

Sample Input 1

2
10 30

Sample Output 1

7.5

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

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

Sample Input 2

3
200 200 200

Sample Output 2

66.66666666666667

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

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

Sample Input 3

1
1000

Sample Output 3

1000

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

Printing +1000.0 and so on will also be accepted.


(* abc138 b problem *)
open Scanf;;
open Array;;

let () = Scanf.scanf "%d"
           (fun n -> Array.init n (fun _ -> Scanf.scanf " %f" (fun a -> a)))
         |> Array.fold_left (fun x a -> x +. (1.0 /. a)) 0.0
         |> ((/.) 1.0)
         |> print_float
         |> print_newline;;

exit 0;;


(* Vicfred
 * https://atcoder.jp/contests/abc138/tasks/abc138_c
 * sorting, greedy
 * *)

let main =
    Scanf.scanf "%d\n" (fun n ->
        Array.init n (fun _ -> Scanf.scanf " %f" (fun f -> f))
        |> Array.to_list
        |> List.sort compare
        |> (fun arr -> List.fold_left (fun x y -> (x +. y) /. 2.0) (List.hd arr) arr)
        |> Printf.printf "%.9f\n"
)

Score : 300 points

Problem Statement

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

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

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

Constraints

2 \leq N \leq 50

1 \leq v_i \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
v_1 v_2 \ldots v_N

Output

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

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

Sample Input 1

2
3 4

Sample Output 1

3.5

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

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

Sample Input 2

3
500 300 200

Sample Output 2

375

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

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

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

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

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

Printing 375.0 and so on will also be accepted.

Sample Input 3

5
138 138 138 138 138

Sample Output 3

138


let s = read_line ()
let t = read_line ()

let s_len = String.length s
let t_len = String.length t

let ch_pos =
  let ss = s ^ s in
  let len = s_len * 2 in
  let rec f arr i =
    if i = -1 then arr
    else begin
      arr.(i) <- Array.copy arr.(i + 1);
      arr.(i).(int_of_char ss.[i] - int_of_char 'a') <- i;
      f arr (i - 1)
    end
  in
  f (Array.init (len + 1) (fun _ -> Array.make 26 (-1))) (len - 1)

let rec loop sum i pos =
  if i = t_len then
    print_int sum
  else
    let next = ch_pos.(pos).(int_of_char t.[i] - int_of_char 'a') in
    if next = -1 then begin
      print_int (-1); exit 0;
    end else loop (sum + next - pos) (i + 1) (next mod s_len)

let () = loop 1 0 0

Score : 500 points

Problem Statement

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

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

Notes

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

Constraints

1 \leq |s| \leq 10^5

1 \leq |t| \leq 10^5

s and t consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

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

Sample Input 1

contest
son

Sample Output 1

10

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

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

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

Sample Input 2

contest
programming

Sample Output 2

-1

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

Sample Input 3

contest
sentence

Sample Output 3

33

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


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

Score : 100 points

Problem Statement

We have two integers: A and B.

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

Constraints

All values in input are integers.

-100 \leq A,\ B \leq 100

Input

Input is given from Standard Input in the following format:

A B

Output

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

Sample Input 1

-13 3

Sample Output 1

-10

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

Sample Input 2

1 -33

Sample Output 2

34

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

Sample Input 3

13 3

Sample Output 3

39

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


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

let () =
  let a = scan_int() and b = scan_int() in
  print_int (max (a*b) (max (a+b) (a-b)))


open Scanf
open Printf

let rint () = scanf " %d" (fun x -> x)
let stoa s = Array.init (String.length s) (String.get s)
let atos a = String.init (Array.length a) (Array.get a)

let () =
  let a = rint() and b = rint() in
  printf "%d\n" (max (a + b) (max (a - b) (a * b)))


(* Vicfred
 * https://atcoder.jp/contests/abc137/tasks/abc137_b
 * implementation
 * *)

let rec solve i n =
    if i > n then Printf.printf "\n" else
        (Printf.printf "%d " i; solve (i+1) n);;

Scanf.scanf "%d %d" @@ fun k x ->
    solve (x - k + 1) (x + k - 1);;

Score : 200 points

Problem Statement

There are 2000001 stones placed on a number line. The coordinates of these stones are -1000000, -999999, -999998, \ldots, 999999, 1000000.

Among them, some K consecutive stones are painted black, and the others are painted white.

Additionally, we know that the stone at coordinate X is painted black.

Print all coordinates that potentially contain a stone painted black, in ascending order.

Constraints

1 \leq K \leq 100

0 \leq X \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K X

Output

Print all coordinates that potentially contain a stone painted black, in ascending order, with spaces in between.

Sample Input 1

3 7

Sample Output 1

5 6 7 8 9

We know that there are three stones painted black, and the stone at coordinate 7 is painted black. There are three possible cases:

The three stones painted black are placed at coordinates 5, 6, and 7.

The three stones painted black are placed at coordinates 6, 7, and 8.

The three stones painted black are placed at coordinates 7, 8, and 9.

Thus, five coordinates potentially contain a stone painted black: 5, 6, 7, 8, and 9.

Sample Input 2

4 0

Sample Output 2

-3 -2 -1 0 1 2 3

Negative coordinates can also contain a stone painted black.

Sample Input 3

1 100

Sample Output 3

100


let rec make_list n m =
    if n = m then [n] else n :: make_list (n+1) m
let rec string_of_list lst = match lst with
    | [] -> ""
    | f :: r -> string_of_int f ^ " " ^ string_of_list r
let () =
    let k, x = Scanf.scanf "%d %d\n" (fun a b -> a, b) in
    let str = string_of_list (make_list (x-k+1) (x+k-1)) in
    Printf.printf "%s\n" str


open Scanf
open Printf

let (n,m) = sscanf (read_line ()) "%d %d" (fun a b -> (a,b));;
(*
let array = Array.make n (0,0);;
for i = 0 to (n-1) do
  let (x,y) = sscanf (read_line ()) "%d %d" (fun a b -> (a,b)) in
  array.(i) <- (x,y)
done;;
*)

let rec input_list = function
  | 0 -> []
  | x -> sscanf (read_line ()) "%d %d" (fun a b -> (a,b)) :: (input_list (x-1));;

let list = input_list n;;
let sort_list = List.sort (fun a b -> compare (fst a) (fst b)) list;;

module PrioQueue =
struct
  type priority = int
  type 'a queue = Empty | Node of priority * 'a * 'a queue * 'a queue
  let empty = Empty
  let rec insert queue prio elt =
    match queue with
      Empty -> Node(prio, elt, Empty, Empty)
    | Node(p, e, left, right) ->
      (* prio <= p なら小さいほうが優先 prio >= p なら大きい方が優先 *)
      if prio = p then (* prio = p なら eltが小さい方を優先 *)
        if e >= elt
          then Node(prio, elt, insert right p e, left)
          else Node(p, e, insert right prio elt, left)
      else
      if prio > p
        then Node(prio, elt, insert right p e, left)
        else Node(p, e, insert right prio elt, left)
  exception Queue_is_empty
  let rec remove_top = function
      Empty -> raise Queue_is_empty
    | Node(prio, elt, left, Empty) -> left
    | Node(prio, elt, Empty, right) -> right
    | Node(prio, elt, (Node(lprio, lelt, _, _) as left), (Node(rprio, relt, _, _) as right)) ->
      if lprio = rprio then (* prio <= p なら小さいほうが優先 prio >= p なら大きい方が優先 *)
        if relt >= lelt
      then Node(lprio, lelt, remove_top left, right)
      else Node(rprio, relt, left, remove_top right)
      else if lprio > rprio
      then Node(lprio, lelt, remove_top left, right)
      else Node(rprio, relt, left, remove_top right)
  let rec print_queue = function
      Empty -> Printf.printf "Empty\n"
    | Node(prio, elt, left, right) ->
      let _ = Printf.printf "prio = %d, elt = %d \n" prio elt in
      let _ = Printf.printf "left = " in
      let _ = print_queue left in
      let _ = Printf.printf "right = " in
      let _ = print_queue right in ()
  let extract = function
      Empty -> raise Queue_is_empty
    | Node(prio, elt, _, _) as queue -> (prio, elt, remove_top queue)
end;;

open PrioQueue;;

let rec solve_aux list m pq =
  match list with
  | [] -> ([], pq)
  | (x,y) :: rest ->
    if x <= m then
      let new_pq = PrioQueue.insert pq y x in
      solve_aux rest m new_pq
    else
      (list,pq);;

let solve () =
let rec loop rest_day ans candi pq =
  (*let _ = printf "candi = " in
  let _ = List.iter (fun a -> printf "%d / " (fst a))  candi in*)
  if rest_day = m+1 then ans
  else
   (* let _ = printf "rest_day = %d, ans = %d, \n pq = " rest_day ans in*)
    let (rest, new_pq) = solve_aux candi rest_day pq in
    (*let _ = PrioQueue.print_queue new_pq in*)
    match new_pq with
    | Empty ->
      loop (rest_day + 1) ans rest Empty
    | Node(prio, elt, _, _) as queue ->
      loop (rest_day + 1) (ans + prio) rest (PrioQueue.remove_top queue)
in loop 1 0 sort_list Empty;;

let _ = printf "%d\n" (solve ());;

Score : 400 points

Problem Statement

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

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

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

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

You can already start working today.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i \leq 10^5

1 \leq B_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

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

Sample Input 1

3 4
4 3
4 1
2 2

Sample Output 1

5

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

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

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

Sample Input 2

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

Sample Output 2

10

Sample Input 3

1 1
2 1

Sample Output 3

0


open Scanf
open Printf

type heap = Nil | Node of int * heap * heap

let rec merge a b =
  match a, b with
  | a, Nil -> a
  | Nil, b -> b
  | Node (av, al, ar), Node (bv, bl, br) ->
    if av > bv then
      Node (av, (merge ar b), al)
    else
      Node (bv, (merge br a), bl)

let is_empty = function
  | Nil -> true
  | _ -> false

let push a v = merge a (Node (v, Nil, Nil))
let peek = function
  | Nil -> invalid_arg "nil"
  | Node (v, _, _) -> v

let pop = function
  | Nil -> invalid_arg "nil"
  | Node (_, l, r) -> merge l r

let () =
  let n, m = scanf " %d %d" (fun a b -> a, b) in
  let ab = Array.init n (fun _ -> scanf " %d %d" (fun a b -> a, b)) in
  Array.sort compare ab;
  let k = ref 0 in
  let q = ref Nil in
  let ans = ref 0 in
  for i = m - 1 downto 0 do
    while !k < n && i + fst ab.(!k) <= m do
      q := push !q (snd ab.(!k));
      incr k
    done;
    if not (is_empty !q) then begin
      ans := !ans + peek !q;
      q := pop !q
    end;
  done;
  printf "%d\n" !ans


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

Score : 100 points

Problem Statement

We have two bottles for holding water.

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

Bottle 2 contains C milliliters of water.

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

How much amount of water will remain in Bottle 2?

Constraints

All values in input are integers.

1 \leq B \leq A \leq 20

1 \leq C \leq 20

Input

Input is given from Standard Input in the following format:

A B C

Output

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

Sample Input 1

6 4 3

Sample Output 1

1

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

Sample Input 2

8 3 9

Sample Output 2

4

Sample Input 3

12 3 7

Sample Output 3

0


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


let s = read_line ()
let n = String.length s
let eo, ans = Array.(make 2 0, make n 0)
let g a i = a.(i) <- a.(i) + 1
let rec f i p j = if i < n then
  if p = 'R' then if s.[i] = 'R' then (g eo @@ i mod 2; f (i + 1) p j)
    else (ans.(i - 1) <- eo.(1 - i mod 2); ans.(i) <- eo.(i mod 2) + 1; f (i + 1) 'L' i)
  else if s.[i] = 'L' then (g ans @@ j - (i - j) mod 2; f (i + 1) p j) else (eo.(0) <- 0; eo.(1) <- 0; eo.(i mod 2) <- 1; f (i + 1) 'R' j)
let _ = eo.(0) <- 1; f 1 'R' 0; Array.iteri (fun i n -> Printf.printf (if i = 0 then "%d" else " %d") n) ans; print_newline ()

Score : 400 points

Problem Statement

Given is a string S consisting of L and R.

Let N be the length of S. There are N squares arranged from left to right, and the i-th character of S from the left is written on the i-th square from the left.

The character written on the leftmost square is always R, and the character written on the rightmost square is always L.

Initially, one child is standing on each square.

Each child will perform the move below 10^{100} times:

Move one square in the direction specified by the character written in the square on which the child is standing. L denotes left, and R denotes right.

Find the number of children standing on each square after the children performed the moves.

Constraints

S is a string of length between 2 and 10^5 (inclusive).

Each character of S is L or R.

The first and last characters of S are R and L, respectively.

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of children standing on each square after the children performed the moves, in order from left to right.

Sample Input 1

RRLRL

Sample Output 1

0 1 2 1 1

After each child performed one move, the number of children standing on each square is 0, 2, 1, 1, 1 from left to right.

After each child performed two moves, the number of children standing on each square is 0, 1, 2, 1, 1 from left to right.

After each child performed 10^{100} moves, the number of children standing on each square is 0, 1, 2, 1, 1 from left to right.

Sample Input 2

RRLLLLRLRRLL

Sample Output 2

0 3 3 0 0 0 1 1 0 2 2 0

Sample Input 3

RRRLLRLLRRRLLLLL

Sample Output 3

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


open Scanf
open Printf

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

let divisors x =
  let l = ref [] in
  let r = ref [] in
  let y = ref 1 in
  while !y * !y <= x do
    if x mod !y = 0 then begin
      l := !y :: !l;
      if x <> !y * !y then r := (x / !y) :: !r
    end;
    incr y
  done;
  List.rev !l @ !r

let cost n a d =
  let open Array in
  let a = map (fun x -> x mod d) a in
  fast_sort compare a;
  let tot = fold_left (+) 0 a in
  assert (tot mod d = 0);
  let m = tot / d in
  let def = ref 0 in
  for i=n-m to n-1 do def := !def + d - a.(i) done;
  !def

let solve n k a =
  let tot = Array.fold_left (+) 0 a in
  let divs = divisors tot in
  List.fold_left (fun cur d -> if cost n a d <= k then d else cur) 1 divs

let main () =
  let n = readInt () in
  let k = readInt () in
  let a = Array.init n (fun _ -> readInt ()) in
  printf "%d\n" (solve n k a)

let () = main ()

Score : 500 points

Problem Statement

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

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

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

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

Constraints

2 \leq N \leq 500

1 \leq A_i \leq 10^6

0 \leq K \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

2 3
8 20

Sample Output 1

7

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

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

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

Sample Input 2

2 10
3 5

Sample Output 2

8

Consider performing the following five operations:

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

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

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

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

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

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

Sample Input 3

4 5
10 1 2 22

Sample Output 3

7

Sample Input 4

8 7
1 7 5 6 8 2 6 5

Sample Output 4

5


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

Score: 100 points

Problem Statement

We have two distinct integers A and B.

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

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

Constraints

All values in input are integers.

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

A and B are distinct.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the integer K satisfying the condition.

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

Sample Input 1

2 16

Sample Output 1

9

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

Sample Input 2

0 3

Sample Output 2

IMPOSSIBLE

No integer satisfies the condition.

Sample Input 3

998244353 99824435

Sample Output 3

549034394


let answer a b =
  let sa = a - b in
    if (sa mod 2) = 1 then
      "IMPOSSIBLE"
    else
      if sa < 0 then
        sa / 2 * (-1) |> string_of_int
      else
        sa / 2 |> string_of_int

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


let () = Scanf.scanf "%d\n" @@ fun n ->
  let pp = Array.init n @@ fun _ -> Scanf.scanf " %d" @@ fun d -> d - 1 in
  let changed = ref 0 in
  for i = 0 to n - 1 do
    if pp.(i) <> i then changed := !changed + 1
  done;
  Printf.printf "%s\n" @@
    if !changed <= 2 then "YES" else "NO"

Score : 200 points

Problem Statement

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

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

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

Constraints

All values in input are integers.

2 \leq N \leq 50

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

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_N

Output

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

Sample Input 1

5
5 2 3 4 1

Sample Output 1

YES

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

Sample Input 2

5
2 4 3 5 1

Sample Output 2

NO

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

Sample Input 3

7
1 2 3 4 5 6 7

Sample Output 3

YES

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


let () = Scanf.scanf "%d\n" @@ fun n ->
  let ps = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun p -> p in
  print_endline @@ if
    ( >= ) 2 @@
    Array.fold_left ( + ) 0 @@
    Array.init n @@ fun i ->
      if ps.(i) = i + 1 then 0 else 1
  then "YES"
  else "NO"


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

let () =
  let s = read_line () in
  let dp = Array.make_matrix (String.length s + 1) 13 0 in
  dp.(0).(0) <- 1;
  String.iteri (fun i ->
    let go n =
      for j = 0 to 12 do
        dp.(i + 1).((10 * j + n) mod 13) <-
          dp.(i + 1).((10 * j + n) mod 13) +^ dp.(i).(j)
      done in
    function
      | '?' ->
          for i = 0 to 9 do
            go i
          done
      | c -> go (int_of_char c - int_of_char '0')) s;
  Printf.printf "%d\n" dp.(String.length s).(5)

Score : 400 points

Problem Statement

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

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

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

Constraints

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

1 \leq |S| \leq 10^5

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

??2??5

Sample Output 1

768

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

Sample Input 2

?44

Sample Output 2

1

Only 044 satisfies the condition.

Sample Input 3

7?4

Sample Output 3

0

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

Sample Input 4

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

Sample Output 4

153716888


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

Score : 400 points

Problem Statement

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

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

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

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

Constraints

1 \leq S \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

3

Sample Output 1

1 0 2 2 0 1

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

Sample Input 2

100

Sample Output 2

0 0 10 0 0 10

Sample Input 3

311114770564041497

Sample Output 3

314159265 358979323 846264338 327950288 419716939 937510582


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


let () =
    let main () =
        let a = Scanf.sscanf (read_line ()) "%d" (fun a -> a) in
        let n = 3 * a * a in
        Printf.printf "%d\n" n
    in
    main ()

Score : 100 points

Problem Statement

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

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

Constraints

1 \leq r \leq 100

r is an integer.

Input

Input is given from Standard Input in the following format:

r

Output

Print an integer representing the area of the regular dodecagon.

Sample Input 1

4

Sample Output 1

48

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

Sample Input 2

15

Sample Output 2

675

Sample Input 3

80

Sample Output 3

19200


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

Score : 200 points

Problem Statement

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

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

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

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

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq D \leq 20

Input

Input is given from Standard Input in the following format:

N D

Output

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

Sample Input 1

6 2

Sample Output 1

2

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

Sample Input 2

14 3

Sample Output 2

2

Sample Input 3

20 4

Sample Output 3

3


let () = Scanf.scanf "%d\n" @@ fun n ->
  let as_ = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun a -> a in
  let ans = Array.make (n + 1) 0 in
  for i = n downto 1 do
    ans.(i) <- Array.fold_left ( lxor ) as_.(i - 1) @@
      Array.init (n / i - 1) @@ fun j -> as_.(i * (j + 2) - 1)
  done;
  match Array.fold_left ( + ) 0 ans with
  | 0 -> print_endline "0"
  | n ->
      Printf.printf "%d\n" n;
      for i = 1 to n do
        if ans.(i) = 1 then Printf.printf "%d " i
      done;
      print_newline ()

Score : 400 points

Problem Statement

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

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

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

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

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

Constraints

All values in input are integers.

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

a_i is 0 or 1.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

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

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

M
b_1 b_2 ... b_M

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

Sample Input 1

3
1 0 0

Sample Output 1

1
1

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

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

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

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

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

Sample Input 2

5
0 0 0 0 0

Sample Output 2

0

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


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

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

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

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

Score : 300 points

Problem Statement

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

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

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

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

What is XOR?

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

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

For example, 3 \oplus 5 = 6.

Constraints

All values in input are integers.

3 \leq N \leq 10^{5}

0 \leq a_i \leq 10^{9}

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_{N}

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

Yes

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

Sample Input 2

4
1 2 4 8

Sample Output 2

No

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


let () =
    let main () =
        let n = int_of_string (read_line ()) in
        if n <> 1 && n mod 2 = 1 then (
            print_endline "Yes";
            for i = 1 to (n - 1) / 2 do
                Printf.printf "%d %d\n" 1 (2 * i);
                Printf.printf "%d %d\n" (2 * i) (2 * i + 1);
                Printf.printf "%d %d\n" 1 (2 * i + 1 + n);
                Printf.printf "%d %d\n" (2 * i + n) (2 * i + 1 + n);
            done;
            Printf.printf "%d %d\n" 3 (n + 1)
        ) else if n > 3 && (n - 2) land (n - 3) = 0 then (
            print_endline "Yes";
            for i = 1 to (n - 4) / 2 do
                Printf.printf "%d %d\n" 1 (2 * i);
                Printf.printf "%d %d\n" (2 * i) (2 * i + 1);
                Printf.printf "%d %d\n" 1 (2 * i + 1 + n);
                Printf.printf "%d %d\n" (2 * i + n) (2 * i + 1 + n);
            done;
            Printf.printf "%d %d\n" 3 (n + 1);
            Printf.printf "%d %d\n" 3 (n + n - 1);
            Printf.printf "%d %d\n" (n + 2) (n - 2);
            Printf.printf "%d %d\n" (n - 2) (n - 1);
            Printf.printf "%d %d\n" (n - 2) n;
            Printf.printf "%d %d\n" (n + 2) (n + n);
            Printf.printf "%d %d\n" (n + n) (n + n - 2);
        ) else print_endline "No"
    in
    main ()

Score : 700 points

Problem Statement

You are given an integer N. Determine if there exists a tree with 2N vertices numbered 1 to 2N satisfying the following condition, and show one such tree if the answer is yes.

Assume that, for each integer i between 1 and N (inclusive), Vertex i and N+i have the weight i. Then, for each integer i between 1 and N, the bitwise XOR of the weights of the vertices on the path between Vertex i and N+i (including themselves) is i.

Constraints

N is an integer.

1 \leq N \leq 10^{5}

Input

Input is given from Standard Input in the following format:

N

Output

If there exists a tree satisfying the condition in the statement, print Yes; otherwise, print No.
Then, if such a tree exists, print the 2N-1 edges of such a tree in the subsequent 2N-1 lines, in the following format:

a_{1} b_{1}
\vdots
a_{2N-1} b_{2N-1}

Here each pair (a_i, b_i) means that there is an edge connecting Vertex a_i and b_i. The edges may be printed in any order.

Sample Input 1

3

Sample Output 1

Yes
1 2
2 3
3 4
4 5
5 6

The sample output represents the following graph:

Sample Input 2

1

Sample Output 2

No

There is no tree satisfying the condition.


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

Score : 100 points

Problem Statement

N of us are going on a trip, by train or taxi.

The train will cost each of us A yen (the currency of Japan).

The taxi will cost us a total of B yen.

How much is our minimum total travel expense?

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq A \leq 50

1 \leq B \leq 50

Input

Input is given from Standard Input in the following format:

N A B

Output

Print an integer representing the minimum total travel expense.

Sample Input 1

4 2 9

Sample Output 1

8

The train will cost us 4 \times 2 = 8 yen, and the taxi will cost us 9 yen, so the minimum total travel expense is 8 yen.

Sample Input 2

4 2 7

Sample Output 2

7

Sample Input 3

4 2 8

Sample Output 3

8


let is_integer xs ys =
  let dim = Array.length xs in
  let sq_dist = Array.fold_left ( + ) 0 @@
    Array.init dim @@ fun i -> (xs.(i) - ys.(i)) * (xs.(i) - ys.(i)) in
  let check = Array.exists (fun d -> d * d = sq_dist) (Array.init 44 @@ fun i -> (i + 1)) in
  if check then 1 else 0

let () = Scanf.scanf "%d %d\n" @@ fun n d ->
  let points =
    Array.init n @@ fun _ ->
      Array.init d @@ fun _ -> Scanf.scanf " %d" Fun.id in
  let check_boxes =
    Array.init (n - 1) @@ fun i ->
      Array.init (n - 1 - i) @@ fun j -> is_integer points.(i) points.(i + j + 1) in
  let ans =
    Array.fold_left (Array.fold_left ( + )) 0 check_boxes in
  Printf.printf "%d\n" ans

(*
  Array.fold_left: ('a -> 'b -> 'a) -> 'a -> 'b array -> 'a
  1つ目の fold_left は (int -> int array -> int) -> int -> int array array -> int
  2つ目の fold_left は ( + ) を渡しているので ('a -> 'a array -> 'a) になる
  ここで期待するのは int -> int list -> int
*)

Score : 200 points

Problem Statement

There are N points in a D-dimensional space.

The coordinates of the i-th point are (X_{i1}, X_{i2}, ..., X_{iD}).

The distance between two points with coordinates (y_1, y_2, ..., y_D) and (z_1, z_2, ..., z_D) is \sqrt{(y_1 - z_1)^2 + (y_2 - z_2)^2 + ... + (y_D - z_D)^2}.

How many pairs (i, j) (i < j) are there such that the distance between the i-th point and the j-th point is an integer?

Constraints

All values in input are integers.

2 \leq N \leq 10

1 \leq D \leq 10

-20 \leq X_{ij} \leq 20

No two given points have the same coordinates. That is, if i \neq j, there exists k such that X_{ik} \neq X_{jk}.

Input

Input is given from Standard Input in the following format:

N D
X_{11} X_{12} ... X_{1D}
X_{21} X_{22} ... X_{2D}
\vdots
X_{N1} X_{N2} ... X_{ND}

Output

Print the number of pairs (i, j) (i < j) such that the distance between the i-th point and the j-th point is an integer.

Sample Input 1

3 2
1 2
5 5
-2 8

Sample Output 1

1

The number of pairs with an integer distance is one, as follows:

The distance between the first point and the second point is \sqrt{|1-5|^2 + |2-5|^2} = 5, which is an integer.

The distance between the second point and the third point is \sqrt{|5-(-2)|^2 + |5-8|^2} = \sqrt{58}, which is not an integer.

The distance between the third point and the first point is \sqrt{|-2-1|^2+|8-2|^2} = 3\sqrt{5}, which is not an integer.

Sample Input 2

3 4
-3 7 8 2
-12 1 10 2
-2 8 9 3

Sample Output 2

2

Sample Input 3

5 1
1
2
3
4
5

Sample Output 3

10


let is_integer xs ys =
  let dim = Array.length xs in
  let sq_dist = Array.fold_left ( + ) 0 @@
    Array.init dim @@ fun i -> (xs.(i) - ys.(i)) * (xs.(i) - ys.(i)) in
  let check = Array.exists (fun d -> d * d = sq_dist) (Array.init 44 @@ fun i -> (i + 1)) in
  if check then 1 else 0

let () = Scanf.scanf "%d %d\n" @@ fun n d ->
  let points =
    Array.init n @@ fun _ ->
      Array.init d @@ fun _ -> Scanf.scanf " %d" Fun.id in
  let check_boxes =
    Array.init (n - 1) @@ fun i ->
      Array.init (n - i) @@ fun j -> is_integer points.(i) points.(i + j) in
  let ans =
    Array.fold_left (Array.fold_left ( + )) 0 check_boxes in
  Printf.printf "%d\n" ans

(*
  Array.fold_left: ('a -> 'b -> 'a) -> 'a -> 'b array -> 'a
  1つ目の fold_left は (int -> int array -> int) -> int -> int array array -> int
  2つ目の fold_left は ( + ) を渡しているので ('a -> 'a array -> 'a) になる
  ここで期待するのは int -> int list -> int
*)


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

let () = Scanf.scanf "%d %d\n" @@ fun n q ->
  let es = Array.make n [] in
  for i = 0 to n - 2 do
    Scanf.scanf "%d %d %d %d\n" @@ fun a b c d ->
      es.(a - 1) <- (b - 1, c, d) :: es.(a - 1);
      es.(b - 1) <- (a - 1, c, d) :: es.(b - 1)
  done;
  let dist = Array.make n (-1) in
  let count_colors = Array.make n IntMap.empty in
  let acc_colors = Array.make n IntMap.empty in
  let parent = Array.make_matrix 17 n 0 in
  let rec visit c cd d u v =
    if 0 <= dist.(v) then ()
    else begin
      dist.(v) <- d;
      parent.(0).(v) <- u;
      acc_colors.(v) <- cd;
      count_colors.(v) <- c;
      List.iter (fun (u, c', d') ->
        visit
          (IntMap.add c'
            (1 +
              try IntMap.find c' c
              with Not_found -> 0) c)
          (IntMap.add c'
            (d' +
              try IntMap.find c' c
              with Not_found -> 0) c)
          (d + d') v u) es.(v)
    end in
  visit IntMap.empty IntMap.empty 0 0 0;
  for i = 1 to 16 do
    for v = 0 to n - 1 do
      parent.(i).(v) <- parent.(i - 1).(parent.(i - 1).(v))
    done
  done;
  let rec lca u v = function
    | 0 ->
        if parent.(0).(u) = parent.(0).(v)
        then parent.(0).(u)
        else begin
          assert (parent.(1).(u) = parent.(1).(v));
          parent.(1).(u)
        end
    | i ->
        ( if parent.(i).(u) = parent.(i).(v)
          then lca u v
          else lca (parent.(i).(u)) (parent.(i).(u)) ) (i - 1) in
  for i = 0 to q - 1 do
    Scanf.scanf "%d %d %d %d\n" @@ fun x y u v ->
      let w = lca (u - 1) (v - 1) 16 in
      Printf.printf "%d\n" @@
      (dist.(u - 1) - (try IntMap.find x acc_colors.(u - 1) with Not_found -> 0) + y * (try IntMap.find x count_colors.(u - 1) with Not_found -> 0))
      + (dist.(v - 1) - (try IntMap.find x acc_colors.(v - 1) with Not_found -> 0) + y * (try IntMap.find x count_colors.(v - 1) with Not_found -> 0))
      - 2 * (dist.(w) - (try IntMap.find x acc_colors.(w) with Not_found -> 0) + y * (try IntMap.find x count_colors.(w) with Not_found -> 0))
  done

Score : 600 points

Problem Statement

There is a tree with N vertices numbered 1 to N.
The i-th edge in this tree connects Vertex a_i and Vertex b_i, and the color and length of that edge are c_i and d_i, respectively.
Here the color of each edge is represented by an integer between 1 and N-1 (inclusive). The same integer corresponds to the same color, and different integers correspond to different colors.

Answer the following Q queries:

Query j (1 \leq j \leq Q): assuming that the length of every edge whose color is x_j is changed to y_j, find the distance between Vertex u_j and Vertex v_j. (The changes of the lengths of edges do not affect the subsequent queries.)

Constraints

2 \leq N \leq 10^5

1 \leq Q \leq 10^5

1 \leq a_i, b_i \leq N

1 \leq c_i \leq N-1

1 \leq d_i \leq 10^4

1 \leq x_j \leq N-1

1 \leq y_j \leq 10^4

1 \leq u_j < v_j \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 Q
a_1 b_1 c_1 d_1
:
a_{N-1} b_{N-1} c_{N-1} d_{N-1}
x_1 y_1 u_1 v_1
:
x_Q y_Q u_Q v_Q

Output

Print Q lines. The j-th line (1 \leq j \leq Q) should contain the answer to Query j.

Sample Input 1

5 3
1 2 1 10
1 3 2 20
2 4 4 30
5 2 1 40
1 100 1 4
1 100 1 5
3 1000 3 4

Sample Output 1

130
200
60

The graph in this input is as follows:

Here the edges of Color 1 are shown as solid red lines, the edge of Color 2 is shown as a bold green line, and the edge of Color 4 is shown as a blue dashed line.

Query 1: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 4 is 100 + 30 = 130.

Query 2: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 5 is 100 + 100 = 200.

Query 3: Assuming that the length of every edge whose color is 3 is changed to 1000 (there is no such edge), the distance between Vertex 3 and Vertex 4 is 20 + 10 + 30 = 60. Note that the edges of Color 1 now have their original lengths.


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

let () = Scanf.scanf "%d %d\n" @@ fun n d ->
  let xss =
    Array.init n @@ fun _ ->
      Array.init d @@ fun _ -> Scanf.scanf "%d " @@ fun x -> x in
  Printf.printf "%d\n" @@
  Array.fold_left (Array.fold_left ( + )) 0 @@
  Array.init n @@ fun i ->
    Array.init i @@ fun j ->
      let sqd =
        Array.fold_left ( + ) 0 @@
        Array.init d @@ fun k ->
          (xss.(i).(k) - xss.(j).(k)) * (xss.(i).(k) - xss.(j).(k)) in
      let d = floor_sqrt sqd in
      if d * d = sqd then 1 else 0

Score : 100 points

Problem Statement

You are given a 4-character string S consisting of uppercase English letters.
Determine if S consists of exactly two kinds of characters which both appear twice in S.

Constraints

The length of S is 4.

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S consists of exactly two kinds of characters which both appear twice in S, print Yes; otherwise, print No.

Sample Input 1

ASSA

Sample Output 1

Yes

S consists of A and S which both appear twice in S.

Sample Input 2

STOP

Sample Output 2

No

Sample Input 3

FFEE

Sample Output 3

Yes

Sample Input 4

FREE

Sample Output 4

No


let () = Scanf.scanf "%d\n" @@ fun n ->
  let ps = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun p -> p in
  Printf.printf "%d\n" @@
  List.length @@
  List.filter (fun b -> b) @@
  Array.to_list @@
  Array.init (n - 2) @@ fun i ->
    let ps' = [| ps.(i); ps.(i + 1); ps.(i + 2) |] in
    Array.sort compare ps';
    ps'.(1) = ps.(i + 1)

Score : 200 points

Problem Statement

We have a permutation p = {p_1,\ p_2,\ ...,\ p_n} of {1,\ 2,\ ...,\ n}.

Print the number of elements p_i (1 < i < n) that satisfy the following condition:

p_i is the second smallest number among the three numbers p_{i - 1}, p_i, and p_{i + 1}.

Constraints

All values in input are integers.

3 \leq n \leq 20

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

Input

Input is given from Standard Input in the following format:

n
p_1 p_2 ... p_n

Output

Print the number of elements p_i (1 < i < n) that satisfy the condition.

Sample Input 1

5
1 3 5 4 2

Sample Output 1

2

p_2 = 3 is the second smallest number among p_1 = 1, p_2 = 3, and p_3 = 5. Also, p_4 = 4 is the second smallest number among p_3 = 5, p_4 = 4, and p_5 = 2. These two elements satisfy the condition.

Sample Input 2

9
9 6 3 2 5 8 7 4 1

Sample Output 2

5


let n = Scanf.scanf " %d" @@ (+) 0
let ds = Array.init n @@ fun _ -> Scanf.scanf " %d" @@ (+) 0
let _ =
  Array.sort (-) ds;
  ds.(n / 2) - ds.(n / 2 - 1) |> Printf.printf "%d\n"

Score : 300 points

Problem Statement

Takahashi made N problems for competitive programming.
The problems are numbered 1 to N, and the difficulty of Problem i is represented as an integer d_i (the higher, the harder).

He is dividing the problems into two categories by choosing an integer K, as follows:

A problem with difficulty K or higher will be for ARCs.

A problem with difficulty lower than K will be for ABCs.

How many choices of the integer K make the number of problems for ARCs and the number of problems for ABCs the same?

Problem Statement

2 \leq N \leq 10^5

N is an even number.

1 \leq d_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
d_1 d_2 ... d_N

Output

Print the number of choices of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

Sample Input 1

6
9 1 4 4 6 7

Sample Output 1

2

If we choose K=5 or 6, Problem 1, 5, and 6 will be for ARCs, Problem 2, 3, and 4 will be for ABCs, and the objective is achieved.
Thus, the answer is 2.

Sample Input 2

8
9 1 14 5 5 4 4 14

Sample Output 2

0

There may be no choice of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

Sample Input 3

14
99592 10342 29105 78532 83018 11639 92015 77204 30914 21912 34519 80835 100000 1

Sample Output 3

42685


let m = 1000000000 + 7

let rec apb a b =
  if b = 0 then 1
  else
    (a * apb (a - 1) (b - 1))

let rec powab a b =
  if b = 0 then 1
  else
    (a * powab a (b - 1))

let nCr n r =
  (apb n r / apb r r) mod m

let () =
  let (n, k) = Scanf.sscanf (input_line stdin) "%d %d" (fun a b -> (a, b)) in
  let rec loop i =
    if i > k then ()
    else
      if k - i >= 0 && n - k - i + 1 >= 0 then
        let ans = (nCr (k - 1) (k - i) * nCr (n - k + 1) (n - k - i + 1)) mod m in
        (print_int ans;
         print_newline ();
        loop (i + 1))
      else
        (print_endline "0";
         loop (i + 1))
  in loop 1

Score : 400 points

Problem Statement

There are K blue balls and N-K red balls. The balls of the same color cannot be distinguished. Snuke and Takahashi are playing with these balls.

First, Snuke will arrange the N balls in a row from left to right.

Then, Takahashi will collect only the K blue balls. In one move, he can collect any number of consecutive blue balls. He will collect all the blue balls in the fewest moves possible.

How many ways are there for Snuke to arrange the N balls in a row so that Takahashi will need exactly i moves to collect all the blue balls? Compute this number modulo 10^9+7 for each i such that 1 \leq i \leq K.

Constraints

1 \leq K \leq N \leq 2000

Input

Input is given from Standard Input in the following format:

N K

Output

Print K lines. The i-th line (1 \leq i \leq K) should contain the number of ways to arrange the N balls so that Takahashi will need exactly i moves to collect all the blue balls, modulo 10^9+7.

Sample Input 1

5 3

Sample Output 1

3
6
1

There are three ways to arrange the balls so that Takahashi will need exactly one move: (B, B, B, R, R), (R, B, B, B, R), and (R, R, B, B, B). (R and B stands for red and blue, respectively).

There are six ways to arrange the balls so that Takahashi will need exactly two moves: (B, B, R, B, R), (B, B, R, R, B), (R, B, B, R, B), (R, B, R, B, B), (B, R, B, B, R), and (B, R, R, B, B).

There is one way to arrange the balls so that Takahashi will need exactly three moves: (B, R, B, R, B).

Sample Input 2

2000 3

Sample Output 2

1998
3990006
327341989

Be sure to print the numbers of arrangements modulo 10^9+7.


module PQ = Set.Make(struct type t = int * int * int let compare = compare end)
let () =
  Scanf.scanf "%d %d" @@ fun n m ->
  let g = Array.init n (fun _ -> []) in
  for _ = 1 to m do
    Scanf.scanf " %d %d" @@ fun u v ->
    g.(u-1) <- v-1 :: g.(u-1);
  done;
  Scanf.scanf " %d %d" @@ fun s t ->
  let s, t = s-1, t-1 in
  let dist = Array.init 3 (fun _ -> Array.make n (max_int / 3)) in
  let rec loop pq =
    if PQ.is_empty pq then () else
    let d,x,m = PQ.min_elt pq in
    let pq = PQ.remove (d,x,m) pq in
    if d > dist.(m).(x) then loop pq else begin
      let m = (m+1) mod 3 in
      List.fold_left (fun pq y ->
        if d+1 >= dist.(m).(y) then pq else begin
          dist.(m).(y) <- d+1;
          PQ.add (d+1, y, m) pq
        end
      ) pq g.(x) |> loop
    end
  in
  loop PQ.(singleton (0, s, 0));
  if dist.(0).(t) < max_int/3 then Printf.printf "%d\n" (dist.(0).(t) / 3)
  else print_endline "-1"

Score : 500 points

Problem Statement

Ken loves ken-ken-pa (Japanese version of hopscotch). Today, he will play it on a directed graph G.
G consists of N vertices numbered 1 to N, and M edges. The i-th edge points from Vertex u_i to Vertex v_i.

First, Ken stands on Vertex S. He wants to reach Vertex T by repeating ken-ken-pa. In one ken-ken-pa, he does the following exactly three times: follow an edge pointing from the vertex on which he is standing.

Determine if he can reach Vertex T by repeating ken-ken-pa. If the answer is yes, find the minimum number of ken-ken-pa needed to reach Vertex T. Note that visiting Vertex T in the middle of a ken-ken-pa does not count as reaching Vertex T by repeating ken-ken-pa.

Constraints

2 \leq N \leq 10^5

0 \leq M \leq \min(10^5, N (N-1))

1 \leq u_i, v_i \leq N(1 \leq i \leq M)

u_i \neq v_i (1 \leq i \leq M)

If i \neq j, (u_i, v_i) \neq (u_j, v_j).

1 \leq S, T \leq N

S \neq T

Input

Input is given from Standard Input in the following format:

N M
u_1 v_1
:
u_M v_M
S T

Output

If Ken cannot reach Vertex T from Vertex S by repeating ken-ken-pa, print -1.
If he can, print the minimum number of ken-ken-pa needed to reach vertex T.

Sample Input 1

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

Sample Output 1

2

Ken can reach Vertex 3 from Vertex 1 in two ken-ken-pa, as follows: 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 in the first ken-ken-pa, then 4 \rightarrow 1 \rightarrow 2 \rightarrow 3 in the second ken-ken-pa. This is the minimum number of ken-ken-pa needed.

Sample Input 2

3 3
1 2
2 3
3 1
1 2

Sample Output 2

-1

Any number of ken-ken-pa will bring Ken back to Vertex 1, so he cannot reach Vertex 2, though he can pass through it in the middle of a ken-ken-pa.

Sample Input 3

2 0
1 2

Sample Output 3

-1

Vertex S and Vertex T may be disconnected.

Sample Input 4

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

Sample Output 4

2


let i_inp : unit -> int = fun () ->
  int_of_string (input_line stdin)
let () =
  let s = i_inp () in
  let rec loop s n pred =
    if n = 0 then true
    else if s = 0 then false
    else
      let a = s mod 10 in
      if a = pred then false
      else loop (s / 10) (n - 1) a
  in
  if loop s 4 (-1) then
    print_endline "Good"
  else
    print_endline "Bad"

Score : 100 points

Problem Statement

The door of Snuke's laboratory is locked with a security code.

The security code is a 4-digit number. We say the security code is hard to enter when it contains two consecutive digits that are the same.

You are given the current security code S. If S is hard to enter, print Bad; otherwise, print Good.

Constraints

S is a 4-character string consisting of digits.

Input

Input is given from Standard Input in the following format:

S

Output

If S is hard to enter, print Bad; otherwise, print Good.

Sample Input 1

3776

Sample Output 1

Bad

The second and third digits are the same, so 3776 is hard to enter.

Sample Input 2

8080

Sample Output 2

Good

There are no two consecutive digits that are the same, so 8080 is not hard to enter.

Sample Input 3

1333

Sample Output 3

Bad

Sample Input 4

0024

Sample Output 4

Bad


type apple_t = {abs_tst:int; id:int}

let () = Scanf.scanf "%d %d" @@ fun n l ->
  let abs_ary = Array.init n @@ fun i -> {abs_tst = abs (l + i); id = i} in
  let all = n * (l - 1) + (n * (n + 1) / 2) in
  let () = Array.sort compare abs_ary in
  let sub = l + abs_ary.(0).id in
  let ans = all - sub in
  Printf.printf "%d\n" ans

Score : 200 points

Problem Statement

You have N apples, called Apple 1, Apple 2, Apple 3, ..., Apple N. The flavor of Apple i is L+i-1, which can be negative.

You can make an apple pie using one or more of the apples. The flavor of the apple pie will be the sum of the flavors of the apples used.

You planned to make an apple pie using all of the apples, but being hungry tempts you to eat one of them, which can no longer be used to make the apple pie.

You want to make an apple pie that is as similar as possible to the one that you planned to make. Thus, you will choose the apple to eat so that the flavor of the apple pie made of the remaining N-1 apples will have the smallest possible absolute difference from the flavor of the apple pie made of all the N apples.

Find the flavor of the apple pie made of the remaining N-1 apples when you choose the apple to eat as above.

We can prove that this value is uniquely determined.

Constraints

2 \leq N \leq 200

-100 \leq L \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N L

Output

Find the flavor of the apple pie made of the remaining N-1 apples when you optimally choose the apple to eat.

Sample Input 1

5 2

Sample Output 1

18

The flavors of Apple 1, 2, 3, 4, and 5 are 2, 3, 4, 5, and 6, respectively. The optimal choice is to eat Apple 1, so the answer is 3+4+5+6=18.

Sample Input 2

3 -1

Sample Output 2

0

The flavors of Apple 1, 2, and 3 are -1, 0, and 1, respectively. The optimal choice is to eat Apple 2, so the answer is (-1)+1=0.

Sample Input 3

30 -50

Sample Output 3

-1044


let split c s = (
    let res = ref [] in
    let i = ref 0 in
    let n = String.length s in
    while !i < n do
      let j = !i in
      while !i < n && s.[!i] <> c do i := !i + 1 done;
      res := String.sub s j (!i - j) :: !res;
      i := !i + 1;
    done;
    List.rev !res
  );;

let rec range a b = if a > b then [] else a :: range (a + 1) b;;
let read_ints () = read_line () |> split ' ' |> List.map int_of_string;;

let rec gcd x y = if y = 0 then x else gcd y (x mod y);;
let lcm x y = x / gcd x y * y;;

let [a; b; c; d] = read_ints ();;
let a = a - 1;;

let f x c d = x - x / c - x / d + x / lcm c d;;

print_int (f b c d - f a c d);;
print_newline ();;

Score : 300 points

Problem Statement

You are given four integers A, B, C, and D. Find the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Constraints

1\leq A\leq B\leq 10^{18}

1\leq C,D\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Sample Input 1

4 9 2 3

Sample Output 1

2

5 and 7 satisfy the condition.

Sample Input 2

10 40 6 8

Sample Output 2

23

Sample Input 3

314159265358979323 846264338327950288 419716939 937510582

Sample Output 3

532105071133627368


let () =
    let rec gcd a b = if b = 0 then a else gcd b (a mod b) in
    let lcm a b = let g = gcd a b in a / g * b in
    let calc a c d =
        let divisible_by_c = a / c in
        let divisible_by_d = a / d in
        let divisible_by_c_and_d = a / lcm c d in
        a - divisible_by_c - divisible_by_d + divisible_by_c_and_d
    in
    let main () =
        let a, b, c, d = Scanf.sscanf (read_line ()) "%d %d %d %d" (fun a b c d -> a, b, c, d) in
        let s = calc b c d - calc (a - 1) c d in
        Printf.printf "%d\n" s
    in
    main ()


(* O(n log n) *)
let n = Scanf.scanf " %d" @@ (+) 0
let bas = Array.init n @@ fun i -> Scanf.scanf " %d %d" @@ fun a b -> b, a
let _ =
  Array.sort compare bas;
  Array.fold_left (fun t (b, a) -> if a + t > b then (print_endline "No"; exit 0) else a + t) 0 bas |> ignore;
  print_endline "Yes"

Score: 400 points

Problem Statement

Kizahashi, who was appointed as the administrator of ABC at National Problem Workshop in the Kingdom of AtCoder, got too excited and took on too many jobs.

Let the current time be time 0. Kizahashi has N jobs numbered 1 to N.

It takes A_i units of time for Kizahashi to complete Job i. The deadline for Job i is time B_i, and he must complete the job before or at this time.

Kizahashi cannot work on two or more jobs simultaneously, but when he completes a job, he can start working on another immediately.

Can Kizahashi complete all the jobs in time? If he can, print Yes; if he cannot, print No.

Constraints

All values in input are integers.

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

1 \leq A_i, B_i \leq 10^9 (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
.
.
.
A_N B_N

Output

If Kizahashi can complete all the jobs in time, print Yes; if he cannot, print No.

Sample Input 1

5
2 4
1 9
1 8
4 9
3 12

Sample Output 1

Yes

He can complete all the jobs in time by, for example, doing them in the following order:

Do Job 2 from time 0 to 1.

Do Job 1 from time 1 to 3.

Do Job 4 from time 3 to 7.

Do Job 3 from time 7 to 8.

Do Job 5 from time 8 to 11.

Note that it is fine to complete Job 3 exactly at the deadline, time 8.

Sample Input 2

3
334 1000
334 1000
334 1000

Sample Output 2

No

He cannot complete all the jobs in time, no matter what order he does them in.

Sample Input 3

30
384 8895
1725 9791
170 1024
4 11105
2 6
578 1815
702 3352
143 5141
1420 6980
24 1602
849 999
76 7586
85 5570
444 4991
719 11090
470 10708
1137 4547
455 9003
110 9901
15 8578
368 3692
104 1286
3 4
366 12143
7 6649
610 2374
152 7324
4 7042
292 11386
334 5720

Sample Output 3

Yes


let n = read_int ();;
let g = Array.init 200000 (fun _ -> []);;
let visited = Array.make 200000 false;;

for i = 0 to n - 1 do
  let x, y = Scanf.scanf " %d %d" (fun x y -> x - 1, y - 1 + 100000) in
  g.(x) <- y :: g.(x);
  g.(y) <- x :: g.(y);
done

let rec dfs u cl cr =
  if not visited.(u) then (
    if u < 100000 then
      cl := !cl + 1
    else
      cr := !cr + 1;
    visited.(u) <- true;
    List.iter (fun v -> dfs v cl cr) g.(u);
  );;

let ans = ref (-n);;
for i = 0 to 199999 do
  let cl = ref 0 and cr = ref 0 in
  dfs i cl cr;
  ans := !ans + !cl * !cr;
done;;

Printf.printf "%d\n" !ans;;

Score : 600 points

Problem Statement

There are N dots in a two-dimensional plane. The coordinates of the i-th dot are (x_i, y_i).

We will repeat the following operation as long as possible:

Choose four integers a, b, c, d (a \neq c, b \neq d) such that there are dots at exactly three of the positions (a, b), (a, d), (c, b) and (c, d), and add a dot at the remaining position.

We can prove that we can only do this operation a finite number of times. Find the maximum number of times we can do the operation.

Constraints

1 \leq N \leq 10^5

1 \leq x_i, y_i \leq 10^5

If i \neq j, x_i \neq x_j or y_i \neq y_j.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the maximum number of times we can do the operation.

Sample Input 1

3
1 1
5 1
5 5

Sample Output 1

1

By choosing a = 1, b = 1, c = 5, d = 5, we can add a dot at (1, 5). We cannot do the operation any more, so the maximum number of operations is 1.

Sample Input 2

2
10 10
20 20

Sample Output 2

0

There are only two dots, so we cannot do the operation at all.

Sample Input 3

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

Sample Output 3

16

We can do the operation for all choices of the form a = 1, b = 1, c = i, d = j (2 \leq i,j \leq 5), and no more. Thus, the maximum number of operations is 16.


let () = Scanf.scanf "%d %d" @@ fun x a ->
  let ans = if x < a then 0 else 10 in
  Printf.printf "%d\n" ans

Score : 100 points

Problem Statement

X and A are integers between 0 and 9 (inclusive).

If X is less than A, print 0; if X is not less than A, print 10.

Constraints

0 \leq X, A \leq 9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X A

Output

If X is less than A, print 0; if X is not less than A, print 10.

Sample Input 1

3 5

Sample Output 1

0

3 is less than 5, so we should print 0.

Sample Input 2

7 5

Sample Output 2

10

7 is not less than 5, so we should print 10.

Sample Input 3

6 6

Sample Output 3

10

6 is not less than 6, so we should print 10.


(* Vicfred
 * https://atcoder.jp/contests/abc130/tasks/abc130_b
 * implementation
 * *)
Scanf.scanf "%d %d\n" @@ fun n x ->
    Array.init n (fun _ -> Scanf.scanf " %d" ( + ) 0)
    |> Array.fold_left (fun (a, b) c -> (if b + c <= x then a + 1 else a), b + c) (1, 0)
    |> fst
    |> Printf.printf "%d\n"

Score : 200 points

Problem Statement

A ball will bounce along a number line, making N + 1 bounces. It will make the first bounce at coordinate D_1 = 0, and the i-th bounce (2 \leq i \leq N+1) at coordinate D_i = D_{i-1} + L_{i-1}.

How many times will the ball make a bounce where the coordinate is at most X?

Constraints

1 \leq N \leq 100

1 \leq L_i \leq 100

1 \leq X \leq 10000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
L_1 L_2 ... L_{N-1} L_N

Output

Print the number of times the ball will make a bounce where the coordinate is at most X.

Sample Input 1

3 6
3 4 5

Sample Output 1

2

The ball will make a bounce at the coordinates 0, 3, 7 and 12, among which two are less than or equal to 6.

Sample Input 2

4 9
3 3 3 3

Sample Output 2

4

The ball will make a bounce at the coordinates 0, 3, 6, 9 and 12, among which four are less than or equal to 9.


let n, x = Scanf.scanf " %d %d" @@ fun a b -> a, b
let ls = Array.init n @@ fun _ -> Scanf.scanf " %d" (+) 0
let d, i = ref 0, ref 0
let _ = while !d < x do d := !d + ls.(!i); incr i done; Printf.printf "%d\n" @@ !i + if !d = x then 1 else 0


let () = Scanf.scanf "%d %d %d %d" @@ fun w h x y ->
  ( if w = 2 * x && h = 2 * y
    then Printf.printf "%f 1"
    else Printf.printf "%f 0" ) @@ 0.5 *. float_of_int w *. float_of_int h

Score : 300 points

Problem Statement

There is a rectangle in a coordinate plane. The coordinates of the four vertices are (0,0), (W,0), (W,H), and (0,H).
You are given a point (x,y) which is within the rectangle or on its border. We will draw a straight line passing through (x,y) to cut the rectangle into two parts. Find the maximum possible area of the part whose area is not larger than that of the other. Additionally, determine if there are multiple ways to cut the rectangle and achieve that maximum.

Constraints

1 \leq W,H \leq 10^9

0\leq x\leq W

0\leq y\leq H

All values in input are integers.

Input

Input is given from Standard Input in the following format:

W H x y

Output

Print the maximum possible area of the part whose area is not larger than that of the other, followed by 1 if there are multiple ways to cut the rectangle and achieve that maximum, and 0 otherwise.

The area printed will be judged correct when its absolute or relative error is at most 10^{-9}.

Sample Input 1

2 3 1 2

Sample Output 1

3.000000 0

The line x=1 gives the optimal cut, and no other line does.

Sample Input 2

2 2 1 1

Sample Output 2

2.000000 1


let w, h, x, y = Scanf.scanf " %d %d %d %d" @@ fun a b c d -> a, b, c, d
let _ =
  Printf.printf "%.12f " @@ float w *. float h /. 2.;
  print_endline @@ if x * 2 = w && y * 2 = h then "1" else "0"


module Mod = struct
  module type S = sig val modulo : int end
  module Make (S : S) = struct
    open S
    let modulo = modulo
    let (+) a b = (a + b) mod modulo
    let ( * ) a b = (a * b) mod modulo
    let rec ( ** ) a n =
      if n = 0 then 1
      else if n mod 2 = 1 then a * a**(n-1)
      else let t = a ** (n/2) in t * t
    let inv a = a ** (modulo-2)
  end
end
module M = Mod.Make(struct let modulo = 1000000007 end)

let () =
  Scanf.scanf "%d %d" @@ fun n m ->
  let s = Array.init n (fun _ -> Scanf.scanf " %d" ((+) 0)) in
  let t = Array.init m (fun _ -> Scanf.scanf " %d" ((+) 0)) in
  let dp = Array.init (n+1) (fun _ -> Array.make (m+1) 0) in
  for i = 0 to n do
    for j = 0 to m do
      let open M in
      if i = 0 || j = 0 then dp.(i).(j) <- 1 else
      dp.(i).(j) <-
        if s.(i-1) = t.(j-1) then
          dp.(i).(j-1) + dp.(i-1).(j)
        else
          dp.(i).(j-1) + dp.(i-1).(j) + (modulo - dp.(i-1).(j-1));
    done;
  done;
  Printf.printf "%d\n" dp.(n).(m)

Score : 500 points

Problem Statement

You are given two integer sequences S and T of length N and M, respectively, both consisting of integers between 1 and 10^5 (inclusive).

In how many pairs of a subsequence of S and a subsequence of T do the two subsequences are the same in content?

Here the subsequence of A is a sequence obtained by removing zero or more elements from A and concatenating the remaining elements without changing the order.

For both S and T, we distinguish two subsequences if the sets of the indices of the removed elements are different, even if the subsequences are the same in content.

Since the answer can be tremendous, print the number modulo 10^9+7.

Constraints

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

The length of S is N.

The length of T is M.

1 \leq S_i, T_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
S_1 S_2 ... S_{N-1} S_{N}
T_1 T_2 ... T_{M-1} T_{M}

Output

Print the number of pairs of a subsequence of S and a subsequence of T such that the subsequences are the same in content, modulo 10^9+7.

Sample Input 1

2 2
1 3
3 1

Sample Output 1

3

S has four subsequences: (), (1), (3), (1, 3).

T has four subsequences: (), (3), (1), (3, 1).

There are 1 \times 1 pair of subsequences in which the subsequences are both (), 1 \times 1 pair of subsequences in which the subsequences are both (1), and 1 \times 1 pair of subsequences in which the subsequences are both (3), for a total of three pairs.

Sample Input 2

2 2
1 1
1 1

Sample Output 2

6

S has four subsequences: (), (1), (1), (1, 1).

T has four subsequences: (), (1), (1), (1, 1).

There are 1 \times 1 pair of subsequences in which the subsequences are both (), 2 \times 2 pairs of subsequences in which the subsequences are both (1), and 1 \times 1 pair of subsequences in which the subsequences are both (1,1), for a total of six pairs.
Note again that we distinguish two subsequences if the sets of the indices of the removed elements are different, even if the subsequences are the same in content.

Sample Input 3

4 4
3 4 5 6
3 4 5 6

Sample Output 3

16

Sample Input 4

10 9
9 6 5 7 5 9 8 5 6 7
8 6 8 5 5 7 9 9 7

Sample Output 4

191

Sample Input 5

20 20
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Sample Output 5

846527861

Be sure to print the number modulo 10^9+7.


let n, k = Scanf.scanf " %d %d" @@ fun a b -> a, b
let _ = Printf.printf "%d\n" @@ if k = 1 then 0 else n - k

Score : 100 points

Problem Statement

Takahashi is distributing N balls to K persons.

If each person has to receive at least one ball, what is the maximum possible difference in the number of balls received between the person with the most balls and the person with the fewest balls?

Constraints

1 \leq K \leq N \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the maximum possible difference in the number of balls received.

Sample Input 1

3 2

Sample Output 1

1

The only way to distribute three balls to two persons so that each of them receives at least one ball is to give one ball to one person and give two balls to the other person.

Thus, the maximum possible difference in the number of balls received is 1.

Sample Input 2

3 1

Sample Output 2

0

We have no choice but to give three balls to the only person, in which case the difference in the number of balls received is 0.

Sample Input 3

8 5

Sample Output 3

3

For example, if we give 1, 4, 1, 1, 1 balls to the five persons, the number of balls received between the person with the most balls and the person with the fewest balls would be 3, which is the maximum result.


(* O(n log n) *)
let n = Scanf.scanf " %d" @@ (+) 0
let a_s = Array.init n @@ fun _ -> Scanf.scanf " %d" @@ (+) 0
let p = ref @@ -1
let xys = ref []
let _ =
  Array.sort (-) a_s;
  let i = ref 0 in while !p < 0 && !i < n do if a_s.(!i) >= 0 then p := !i; incr i done;
  if !p = -1 then p := n - 1
  else if !p = 0 then p := 1;
  for i = !p to n - 2 do
    xys := (a_s.(0), a_s.(i)) :: !xys;
    a_s.(0) <- a_s.(0) - a_s.(i)
  done;
  for i = 0 to !p - 1 do
    xys := (a_s.(n - 1), a_s.(i)) :: !xys;
    a_s.(n - 1) <- a_s.(n - 1) - a_s.(i)
  done;
  Printf.printf "%d\n" a_s.(n - 1);
  List.rev !xys |> List.iter @@ fun (x, y) -> Printf.printf "%d %d\n" x y

Score : 500 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, written on a blackboard.

We will repeat the following operation N-1 times so that we have only one integer on the blackboard.

Choose two integers x and y on the blackboard and erase these two integers. Then, write a new integer x-y.

Find the maximum possible value of the final integer on the blackboard and a sequence of operations that maximizes the final integer.

Constraints

2 \leq N \leq 10^5

-10^4 \leq A_i \leq 10^4

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

Print the maximum possible value M of the final integer on the blackboard, and a sequence of operations x_i, y_i that maximizes the final integer, in the format below.

Here x_i and y_i represent the integers x and y chosen in the i-th operation, respectively.

If there are multiple sequences of operations that maximize the final integer, any of them will be accepted.

M
x_1 y_1
:
x_{N-1} y_{N-1}

Sample Input 1

3
1 -1 2

Sample Output 1

4
-1 1
2 -2

If we choose x = -1 and y = 1 in the first operation, the set of integers written on the blackboard becomes (-2, 2).

Then, if we choose x = 2 and y = -2 in the second operation, the set of integers written on the blackboard becomes (4).

In this case, we have 4 as the final integer. We cannot end with a greater integer, so the answer is 4.

Sample Input 2

3
1 1 1

Sample Output 2

1
1 1
1 0


let () = Scanf.scanf "%d %d %d" @@ fun p q r ->
    Printf.printf "%d" @@ List.fold_left min max_int [p+q;q+r;r+p]

Score : 100 points

Problem Statement

There are three airports A, B and C, and flights between each pair of airports in both directions.

A one-way flight between airports A and B takes P hours, a one-way flight between airports B and C takes Q hours, and a one-way flight between airports C and A takes R hours.

Consider a route where we start at one of the airports, fly to another airport and then fly to the other airport.

What is the minimum possible sum of the flight times?

Constraints

1 \leq P,Q,R \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

P Q R

Output

Print the minimum possible sum of the flight times.

Sample Input 1

1 3 4

Sample Output 1

4

The sum of the flight times in the route A \rightarrow B \rightarrow C: 1 + 3 = 4 hours

The sum of the flight times in the route A \rightarrow C \rightarrow C: 4 + 3 = 7 hours

The sum of the flight times in the route B \rightarrow A \rightarrow C: 1 + 4 = 5 hours

The sum of the flight times in the route B \rightarrow C \rightarrow A: 3 + 4 = 7 hours

The sum of the flight times in the route C \rightarrow A \rightarrow B: 4 + 1 = 5 hours

The sum of the flight times in the route C \rightarrow B \rightarrow A: 3 + 1 = 4 hours

The minimum of these is 4 hours.

Sample Input 2

3 2 3

Sample Output 2

5


let n, m = Scanf.scanf " %d %d" @@ fun a b -> a, b
let a_s = Array.init m @@ fun _ -> Scanf.scanf " %d" (+) 0
let dp = Array.make (n + 1) 0
let os = Array.make (n + 1) true
let f i v = if os.(i) then dp.(i) <- v
let _ = Array.iter (fun a -> os.(a) <- false) a_s; f 0 1; f 1 1;
  for i = 2 to n do f i @@ (dp.(i - 1) + dp.(i - 2)) mod 1000000007 done;
  Printf.printf "%d\n" dp.(n)

Score : 300 points

Problem Statement

There is a staircase with N steps. Takahashi is now standing at the foot of the stairs, that is, on the 0-th step.
He can climb up one or two steps at a time.

However, the treads of the a_1-th, a_2-th, a_3-th, \ldots, a_M-th steps are broken, so it is dangerous to set foot on those steps.

How many are there to climb up to the top step, that is, the N-th step, without setting foot on the broken steps?
Find the count modulo 1\ 000\ 000\ 007.

Constraints

1 \leq N \leq 10^5

0 \leq M \leq N-1

1 \leq a_1 < a_2 < ... < a_M \leq N-1

Input

Input is given from Standard Input in the following format:

N M
a_1
a_2
.
.
.
a_M

Output

Print the number of ways to climb up the stairs under the condition, modulo 1\ 000\ 000\ 007.

Sample Input 1

6 1
3

Sample Output 1

4

There are four ways to climb up the stairs, as follows:

0 \to 1 \to 2 \to 4 \to 5 \to 6

0 \to 1 \to 2 \to 4 \to 6

0 \to 2 \to 4 \to 5 \to 6

0 \to 2 \to 4 \to 6

Sample Input 2

10 2
4
5

Sample Output 2

0

There may be no way to climb up the stairs without setting foot on the broken steps.

Sample Input 3

100 5
1
23
45
67
89

Sample Output 3

608200469

Be sure to print the count modulo 1\ 000\ 000\ 007.


open List
(* 1行読み込みする関数 *)
(* i_inp : 1行読み込んで整数に変換, f_inp, s_inpもある *)
let inp : unit -> string = fun () ->
	input_line stdin
let s_inp = inp
let i_inp : unit -> int = fun () ->
	int_of_string (inp ())
let f_inp : unit -> float = fun () ->
	float_of_string (inp ())

(* 1行読み込んで、空白で分割する関数 *)
let inp_list : unit -> string list = fun () ->
	Str.split (Str.regexp " ") (inp ())
let s_inp_list : unit -> string list = inp_list
let i_inp_list : unit -> int list = fun () ->
	map (fun x -> int_of_string x) (inp_list ())
let f_inp_list : unit -> float list = fun () ->
  map (fun x -> float_of_string x) (inp_list ())

(* n行読み込む関数 *)
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 () =
  let [n; m] = i_inp_list () in
  let brokens = inp_loop (i_inp) m in
  let memo = Array.make (n + 1) None in
  let rec loop a =
    match memo.(a) with
      None ->
      if mem a brokens then 0
      else
        if a + 2 <= n then
          let x = (loop (a + 1) + loop (a + 2)) mod 1000000007 in
          memo.(a) <- Some x;
          x
        else if a + 1 <= n then
          let x = (loop (a + 1)) mod 1000000007 in
          memo.(a) <- Some x;
          x
        else
          (memo.(a) <- Some 1;
           1)
    | Some x -> x
  in
  print_int (loop 0);
  print_newline ()


(* O(h w) *)
let h, w = Scanf.scanf " %d %d" @@ fun a b -> a, b
let ss = Array.init h @@ fun _ -> Scanf.scanf " %s" @@ fun s -> s
let ys = Array.make_matrix h w 0
let xs = Array.make_matrix h w 0
let ans = ref 0
let _ =
  for y = 0 to h - 1 do
    let c = ref 0 in
    for x = 0 to w - 1 do
      if ss.(y).[x] = '#' then
        (for i = 1 to !c do
          xs.(y).(x - i) <- !c
        done;
        c := 0; xs.(y).(x) <- 0)
      else
        (incr c; xs.(y).(x) <- !c)
    done;
    for i = 1 to !c do
      xs.(y).(w - i) <- !c
    done
  done;
  for x = 0 to w - 1 do
    let c = ref 0 in
    for y = 0 to h - 1 do
      if ss.(y).[x] = '#' then
        (for i = 1 to !c do
          ys.(y - i).(x) <- !c
        done;
        c := 0; ys.(y).(x) <- 0)
      else
        (incr c; ys.(y).(x) <- !c)
    done;
    for i = 1 to !c do
      ys.(h - i).(x) <- !c
    done
  done;
  for y = 0 to h - 1 do
    for x = 0 to w - 1 do
      if ss.(y).[x] <> '#' then
        ans := max !ans @@ ys.(y).(x) + xs.(y).(x) - 1
    done
  done;
  Printf.printf "%d\n" !ans

Score : 400 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns, and there are obstacles on some of the squares.

Snuke is going to choose one of the squares not occupied by an obstacle and place a lamp on it.
The lamp placed on the square will emit straight beams of light in four cardinal directions: up, down, left, and right.
In each direction, the beam will continue traveling until it hits a square occupied by an obstacle or it hits the border of the grid. It will light all the squares on the way, including the square on which the lamp is placed, but not the square occupied by an obstacle.

Snuke wants to maximize the number of squares lighted by the lamp.

You are given H strings S_i (1 \leq i \leq H), each of length W. If the j-th character (1 \leq j \leq W) of S_i is #, there is an obstacle on the square at the i-th row from the top and the j-th column from the left; if that character is ., there is no obstacle on that square.

Find the maximum possible number of squares lighted by the lamp.

Constraints

1 \leq H \leq 2,000

1 \leq W \leq 2,000

S_i is a string of length W consisting of # and ..

. occurs at least once in one of the strings S_i (1 \leq i \leq H).

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the maximum possible number of squares lighted by the lamp.

Sample Input 1

4 6
#..#..
.....#
....#.
#.#...

Sample Output 1

8

If Snuke places the lamp on the square at the second row from the top and the second column from the left, it will light the following squares: the first through fifth squares from the left in the second row, and the first through fourth squares from the top in the second column, for a total of eight squares.

Sample Input 2

8 8
..#...#.
....#...
##......
..###..#
...#..#.
##....#.
#...#...
###.#..#

Sample Output 2

13


let rec fold_for a b v f = if a >= b then v else fold_for (a+1) b (f a v) f

let rec pow ( * ) z a n =
  if n = 0 then z
  else if n mod 2 = 1 then a * (pow ( * ) z a (n-1))
  else let h = pow ( * ) z a (n/2) in h * h

let rec intpow = pow ( * ) 1

let i3 = [| [|1; 0; 0|]; [|0; 1; 0|]; [|0; 0; 1|] |]

let () =
  Scanf.scanf "%d %d %d %d" @@ fun l a b modulo ->

  let (+@) a b = (a + b + modulo) mod modulo in
  let modmul a b = (a * b) mod modulo in
  let modpow = pow modmul 1 in

  let matmul a b =
    let m = Array.length b in
    Array.init (Array.length a) (fun i ->
    Array.init (Array.length b.(0)) (fun j ->
      fold_for 0 m 0 (fun k s -> s +@ modmul a.(i).(k) b.(k).(j))))
  in

  let rec loop i d z =
    if i >= l then z else
    let j = (intpow 10 d - a - 1) / b |> min (l-1) in
    if a + b * j < intpow 10 (d-1) then loop i (d+1) z
    else ((
      let c = j-i+1 in
      let r = [| [| z; a +@ modmul b i; 1 |] |] in
      let m = [| [| modpow 10 d; 0; 0 |]; [| 1; 1; 0 |]; [| 0; b mod modulo; 1 |]; |] in
      let mc = pow matmul i3 m c in
      let r' = matmul r mc in
      loop (j+1) (d+1) r'.(0).(0)
    ))
  in

  loop 0 1 0 |> Printf.printf "%d\n"

Score : 600 points

Problem Statement

There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}.

The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds.

Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M?

Constraints

All values in input are integers.

1 \leq L, A, B < 10^{18}

2 \leq M \leq 10^9

All terms in the arithmetic progression are less than 10^{18}.

Input

Input is given from Standard Input in the following format:

L A B M

Output

Print the remainder when the integer obtained by concatenating the terms is divided by M.

Sample Input 1

5 3 4 10007

Sample Output 1

5563

Our arithmetic progression is 3, 7, 11, 15, 19, so the answer is 37111519 mod 10007, that is, 5563.

Sample Input 2

4 8 1 1000000

Sample Output 2

891011

Sample Input 3

107 10000000000007 1000000000000007 998244353

Sample Output 3

39122908


let n, a, b, c, d, s = Scanf.scanf " %d %d %d %d %d %s" @@ fun a b c d e f -> a, b - 1, c - 1, d - 1, e - 1, f
let s2, s3 = String.sub s a @@ c - a + 1, String.sub s b @@ d - b + 1
let f s = try Str.search_forward (Str.regexp "##") s 0 |> ignore; false with _ -> true
let s4 = String.sub s (b - 1) @@ min (n - b + 1) @@ d - b + 3
let _ = print_endline @@
  if f s2 && f s3 then
    if d < c then
      try Str.search_forward (Str.regexp "\\.\\.\\.") s4 0 |> ignore; "Yes" with _ -> "No"
    else "Yes"
  else "No"

Score : 400 points

Problem Statement

There are N squares arranged in a row, numbered 1, 2, ..., N from left to right.
You are given a string S of length N consisting of . and #. If the i-th character of S is #, Square i contains a rock; if the i-th character of S is ., Square i is empty.

In the beginning, Snuke stands on Square A, and Fnuke stands on Square B.

You can repeat the following operation any number of times:

Choose Snuke or Fnuke, and make him jump one or two squares to the right. The destination must be one of the squares, and it must not contain a rock or the other person.

You want to repeat this operation so that Snuke will stand on Square C and Fnuke will stand on Square D.

Determine whether this is possible.

Constraints

4 \leq N \leq 200\ 000

S is a string of length N consisting of . and #.

1 \leq A, B, C, D \leq N

Square A, B, C and D do not contain a rock.

A, B, C and D are all different.

A < B

A < C

B < D

Input

Input is given from Standard Input in the following format:

N A B C D
S

Output

Print Yes if the objective is achievable, and No if it is not.

Sample Input 1

7 1 3 6 7
.#..#..

Sample Output 1

Yes

The objective is achievable by, for example, moving the two persons as follows. (A and B represent Snuke and Fnuke, respectively.)

A#B.#..

A#.B#..

.#AB#..

.#A.#B.

.#.A#B.

.#.A#.B

.#..#AB

Sample Input 2

7 1 3 7 6
.#..#..

Sample Output 2

No

Sample Input 3

15 1 3 15 13
...#.#...#.#...

Sample Output 3

Yes


let n = read_int ()
let _ = Printf.printf "%d\n" (180*(n-2))

Score : 100 points

Problem Statement

Given an integer N not less than 3, find the sum of the interior angles of a regular polygon with N sides.

Print the answer in degrees, but do not print units.

Constraints

3 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print an integer representing the sum of the interior angles of a regular polygon with N sides.

Sample Input 1

3

Sample Output 1

180

The sum of the interior angles of a regular triangle is 180 degrees.

Sample Input 2

100

Sample Output 2

17640


let f s x = let n = ref 0 in String.iter (fun c -> if c = x then incr n) s; !n
let _ = print_endline @@ if f (read_line ()) 'x' > 7 then "NO" else "YES"

Score : 200 points

Problem Statement

Takahashi is competing in a sumo tournament.
The tournament lasts for 15 days, during which he performs in one match per day.
If he wins 8 or more matches, he can also participate in the next tournament.

The matches for the first k days have finished.
You are given the results of Takahashi's matches as a string S consisting of o and x.
If the i-th character in S is o, it means that Takahashi won the match on the i-th day; if that character is x, it means that Takahashi lost the match on the i-th day.

Print YES if there is a possibility that Takahashi can participate in the next tournament, and print NO if there is no such possibility.

Constraints

1 \leq k \leq 15

S is a string of length k consisting of o and x.

Input

Input is given from Standard Input in the following format:

S

Output

Print YES if there is a possibility that Takahashi can participate in the next tournament, and print NO otherwise.

Sample Input 1

oxoxoxoxoxoxox

Sample Output 1

YES

Takahashi has 7 wins and 7 losses before the last match. If he wins that match, he will have 8 wins.

Sample Input 2

xxxxxxxx

Sample Output 2

NO


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

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

let rec sum = function | [] -> 0 | x :: xs -> x + sum xs
let sum = sum c - List.hd c

let rec find_index i arr f =
  if Array.length arr = i then -1
  else if f arr.(i) then i
  else find_index (i + 1) arr f
let find_index = find_index 0 ab

let s = Array.make n false
let ans = Array.make n 0

let rec loop = function
  | [] -> ()
  | x :: xs ->
    let idx = find_index (fun (a, b) -> a + b > 0 && (s.(a) || s.(b))) in
    let (a, b) = ab.(idx) in
    ab.(idx) <- (-1, -1);
    if s.(a) then begin
      s.(b) <- true; ans.(b) <- x
    end
    else begin
      s.(a) <- true; ans.(a) <- x
    end;
    loop xs

let () =
  match c with
    | x :: [] -> print_endline "0"
    | x :: y :: xs ->
      let (a, b) = ab.(0) in
      ab.(0) <- (-1, -1);
      ans.(a) <- x; s.(a) <- true;
      ans.(b) <- y; s.(b) <- true;
      loop xs;
      Printf.printf "%d\n" sum;
      print_endline @@ String.concat " " @@ Array.to_list @@ Array.map string_of_int ans;
    | _ -> failwith ""

Score : 500 points

Problem Statement

You are given a tree with N vertices 1,2,\ldots,N, and positive integers c_1,c_2,\ldots,c_N.
The i-th edge in the tree (1 \leq i \leq N-1) connects Vertex a_i and Vertex b_i.

We will write a positive integer on each vertex in T and calculate our score as follows:

On each edge, write the smaller of the integers written on the two endpoints.

Let our score be the sum of the integers written on all the edges.

Find the maximum possible score when we write each of c_1,c_2,\ldots,c_N on one vertex in T, and show one way to achieve it. If an integer occurs multiple times in c_1,c_2,\ldots,c_N, we must use it that number of times.

Constraints

1 \leq N \leq 10000

1 \leq a_i,b_i \leq N

1 \leq c_i \leq 10^5

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}
c_1 \ldots c_N

Output

Use the following format:

M
d_1 \ldots d_N

where M is the maximum possible score, and d_i is the integer to write on Vertex i.
d_1,d_2,\ldots,d_N must be a permutation of c_1,c_2,\ldots,c_N.
If there are multiple ways to achieve the maximum score, any of them will be accepted.

Sample Input 1

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

Sample Output 1

10
1 2 3 4 5

If we write 1,2,3,4,5 on Vertex 1,2,3,4,5, respectively, the integers written on the four edges will be 1,2,3,4, for the score of 10. This is the maximum possible score.

Sample Input 2

5
1 2
1 3
1 4
1 5
3141 59 26 53 59

Sample Output 2

197
59 26 3141 59 53

c_1,c_2,\ldots,c_N may not be pairwise distinct.


let () =
    let main () =
        let a, p = Scanf.sscanf (read_line ()) "%d %d" (fun a p -> a, p) in
        Printf.printf "%d\n" ((a * 3 + p) / 2)
    in
    main ()

Score : 100 points

Problem Statement

We have A apples and P pieces of apple.

We can cut an apple into three pieces of apple, and make one apple pie by simmering two pieces of apple in a pan.

Find the maximum number of apple pies we can make with what we have now.

Constraints

All values in input are integers.

0 \leq A, P \leq 100

Input

Input is given from Standard Input in the following format:

A P

Output

Print the maximum number of apple pies we can make with what we have.

Sample Input 1

1 3

Sample Output 1

3

We can first make one apple pie by simmering two of the three pieces of apple. Then, we can make two more by simmering the remaining piece and three more pieces obtained by cutting the whole apple.

Sample Input 2

0 1

Sample Output 2

0

We cannot make an apple pie in this case, unfortunately.

Sample Input 3

32 21

Sample Output 3

58


let n = Scanf.scanf " %d" (+) 0
let sps = Array.init n @@ fun i -> Scanf.scanf " %s %d" @@ fun a b -> a, b, i + 1
let f (s, p, i) (s2, p2, i2) = if s = s2 then compare p2 p else compare s s2
let _ = Array.sort f sps; Array.iter (fun (_, _, i) -> Printf.printf "%d\n" i) sps

Score : 200 points

Problem Statement

You have decided to write a book introducing good restaurants.
There are N restaurants that you want to introduce: Restaurant 1, Restaurant 2, ..., Restaurant N. Restaurant i is in city S_i, and your assessment score of that restaurant on a 100-point scale is P_i.
No two restaurants have the same score.

You want to introduce the restaurants in the following order:

The restaurants are arranged in lexicographical order of the names of their cities.

If there are multiple restaurants in the same city, they are arranged in descending order of score.

Print the identification numbers of the restaurants in the order they are introduced in the book.

Constraints

1 ≤ N ≤ 100

S is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.

0 ≤ P_i ≤ 100

P_i is an integer.

P_i ≠ P_j (1 ≤ i < j ≤ N)

Input

Input is given from Standard Input in the following format:

N
S_1 P_1
:
S_N P_N

Output

Print N lines. The i-th line (1 ≤ i ≤ N) should contain the identification number of the restaurant that is introduced i-th in the book.

Sample Input 1

6
khabarovsk 20
moscow 10
kazan 50
kazan 35
moscow 60
khabarovsk 40

Sample Output 1

3
4
6
1
5
2

The lexicographical order of the names of the three cities is kazan < khabarovsk < moscow. For each of these cities, the restaurants in it are introduced in descending order of score. Thus, the restaurants are introduced in the order 3,4,6,1,5,2.

Sample Input 2

10
yakutsk 10
yakutsk 20
yakutsk 30
yakutsk 40
yakutsk 50
yakutsk 60
yakutsk 70
yakutsk 80
yakutsk 90
yakutsk 100

Sample Output 2

10
9
8
7
6
5
4
3
2
1