Scanf.scanf "%d %d" (fun h w ->
    let s = Array.init h (fun _ -> Scanf.scanf " %s" (fun s -> s)) in
    let rec loop_y y =
        let rec loop_x x =
            if x = w then loop_y (y + 1) else
                if s.(y).[x] = '.' then loop_x (x + 1) else (
                    if x > 0     && s.(y).[x - 1] = '#' ||
                       x < w - 1 && s.(y).[x + 1] = '#' ||
                       y > 0     && s.(y - 1).[x] = '#' ||
                       y < h - 1 && s.(y + 1).[x] = '#' then loop_x (x + 1) else false
                )
        in
        if y = h then true else loop_x 0
    in
    print_endline @@ if loop_y 0 then "Yes" else "No"
)

Score: 300 points

Problem Statement

We have a canvas divided into a grid with H rows and W columns. The square at the i-th row from the top and the j-th column from the left is represented as (i, j).

Initially, all the squares are white. square1001 wants to draw a picture with black paint. His specific objective is to make Square (i, j) black when s_{i, j}= #, and to make Square (i, j) white when s_{i, j}= ..

However, since he is not a good painter, he can only choose two squares that are horizontally or vertically adjacent and paint those squares black, for some number of times (possibly zero). He may choose squares that are already painted black, in which case the color of those squares remain black.

Determine if square1001 can achieve his objective.

Constraints

H is an integer between 1 and 50 (inclusive).

W is an integer between 1 and 50 (inclusive).

For every (i, j) (1 \leq i \leq H, 1 \leq j \leq W), s_{i, j} is # or ..

Input

Input is given from Standard Input in the following format:

H W
s_{1, 1} s_{1, 2} s_{1, 3} ... s_{1, W}
s_{2, 1} s_{2, 2} s_{2, 3} ... s_{2, W}
: :
s_{H, 1} s_{H, 2} s_{H, 3} ... s_{H, W}

Output

If square1001 can achieve his objective, print Yes; if he cannot, print No.

Sample Input 1

3 3
.#.
###
.#.

Sample Output 1

Yes

One possible way to achieve the objective is shown in the figure below. Here, the squares being painted are marked by stars.

Sample Input 2

5 5
#.#.#
.#.#.
#.#.#
.#.#.
#.#.#

Sample Output 2

No

square1001 cannot achieve his objective here.

Sample Input 3

11 11
...#####...
.##.....##.
#..##.##..#
#..##.##..#
#.........#
#...###...#
.#########.
.#.#.#.#.#.
##.#.#.#.##
..##.#.##..
.##..#..##.

Sample Output 3

Yes


(* O(h w) *)
let h, w = Scanf.scanf " %d %d" @@ fun a b -> a, b
let sss = Array.init h @@ fun _ -> Scanf.scanf " %s" @@ fun s -> s
let _ =
  for i = 0 to h - 1 do
    for j = 0 to w - 1 do
      if sss.(i).[j] = '#' then
        let f (dy, dx) =
          let y, x = i + dy, j + dx in
          0 <= y && y < h && 0 <= x && x < w && sss.(y).[x] = '#' in
        if not @@ List.exists f [-1, 0; 0, -1; 1, 0; 0, 1] then (print_endline "No"; exit 0)
    done
  done;
  print_endline "Yes"

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

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


let rec range a b = if a > b then [] else a :: range (a+1) b
let (>>=) x f = List.map f x |> List.concat

let () =
  let h, w = Scanf.scanf "%d %d\n" (fun h w -> h, w) in
  let ss = Array.init h (fun _ ->
               Scanf.scanf "%s\n" (fun x -> x)) in

  (range 0 (h-1) >>= fun i ->
   range 0 (w-1) >>= fun j -> [(i, j)])
  |> List.for_all (fun (i, j) ->
         ss.(i).[j] = '.'
         || List.exists (fun (x, y) ->
                try ss.(x).[y] = '#' with _ -> false)
              [(i-1, j); (i+1, j); (i, j-1); (i, j+1)])
  |> function
    | true -> print_endline "Yes"
    | false -> print_endline "No"


let main () =
  let flag = ref true in
  let a, b = Scanf.scanf "%d %d\n" (fun a b -> a, b) in
  let c = Array.make a "" in
  for i = 0 to a - 1 do
    c.(i) <- Scanf.scanf "%s\n" (fun a -> a)
  done;
  for i = 0 to a - 1 do
    for j = 0 to b - 1 do
      if !flag = true && Char.compare (String.get c.(i) j) '#' = 0 then (
        flag := false;
        if i > 0 then (
          if Char.compare (String.get c.(i - 1) j) '#' = 0 then flag := true
        );
        if i < a - 1 && not !flag then (
          if Char.compare (String.get c.(i + 1) j) '#' = 0 then flag := true
        );
        if j > 0 && not !flag then (
          if Char.compare (String.get c.(i) (j - 1)) '#' = 0 then flag := true
        );
        if j < b - 1 && not !flag then (
          if Char.compare (String.get c.(i) (j + 1)) '#' = 0 then flag := true
        );
      )
    done;
  done;
  Printf.printf "%s\n" (if !flag then "Yes" else "No")

let _ = main ()


Scanf.scanf "%d" (fun n ->
    let arr = Array.init 55556 (fun i -> if i < 2 then 0 else i) in
    let rec loop_i i =
        let rec loop_j j =
            if j <= 55555 then (arr.(j) <- 0; loop_j (j + i)) else loop_i (i + 1)
        in
        if i * i <= 55555 then loop_j (i * i)
    in
    loop_i 2;
    let q = Array.fold_right (fun v acc -> if v = 0 then acc else v :: acc) arr [] in
    let q = Array.of_list q in
    let rec loop rest i =
        if rest > 0 then (
            if q.(i) mod 5 = 1 then (
                Printf.printf "%d " q.(i);
                loop (rest - 1) (i + 1)
            ) else loop rest (i + 1)
        )
    in
    loop n 0;
    print_newline ()
)

Score: 400 points

Problem Statement

Print a sequence a_1, a_2, ..., a_N whose length is N that satisfies the following conditions:

a_i (1 \leq i \leq N) is a prime number at most 55 555.

The values of a_1, a_2, ..., a_N are all different.

In every choice of five different integers from a_1, a_2, ..., a_N, the sum of those integers is a composite number.

If there are multiple such sequences, printing any of them is accepted.

Notes

An integer N not less than 2 is called a prime number if it cannot be divided evenly by any integers except 1 and N, and called a composite number otherwise.

Constraints

N is an integer between 5 and 55 (inclusive).

Input

Input is given from Standard Input in the following format:

N

Output

Print N numbers a_1, a_2, a_3, ..., a_N in a line, with spaces in between.

Sample Input 1

5

Sample Output 1

3 5 7 11 31

Let us see if this output actually satisfies the conditions.

First, 3, 5, 7, 11 and 31 are all different, and all of them are prime numbers.

The only way to choose five among them is to choose all of them, whose sum is a_1+a_2+a_3+a_4+a_5=57, which is a composite number.

There are also other possible outputs, such as 2 3 5 7 13, 11 13 17 19 31 and 7 11 5 31 3.

Sample Input 2

6

Sample Output 2

2 3 5 7 11 13

2, 3, 5, 7, 11, 13 are all different prime numbers.

2+3+5+7+11=28 is a composite number.

2+3+5+7+13=30 is a composite number.

2+3+5+11+13=34 is a composite number.

2+3+7+11+13=36 is a composite number.

2+5+7+11+13=38 is a composite number.

3+5+7+11+13=39 is a composite number.

Thus, the sequence 2 3 5 7 11 13 satisfies the conditions.

Sample Input 3

8

Sample Output 3

2 5 7 13 19 37 67 79


(* エラトステネスのふるい *)
let sieve :
  (* 素数かどうかを判定する自然数の上界 *)
  int ->
  (* 素数ならtrueを返す関数 *)
  (int -> bool)
  = fun n ->
    let a = Array.make n true in
    a.(0) <- false;
    a.(1) <- false;
    for p = 2 to int_of_float @@ ceil @@ sqrt @@ float_of_int n do
      if a.(p) then
        for i = p to (n - 1) / p do
          a.(p * i) <- false
        done
    done;
    Array.get a

let rec take n = function
  | [] -> []
  | x :: xs ->
      if n = 0 then []
      else x :: take (n - 1) xs

let () = Scanf.scanf "%d\n" @@ fun n ->
  let p = sieve 55555 in
  Array.init 55555 (fun i -> i)
  |> Array.to_list
  (*
   * (5i + 1) + (5j + 1) + (5k + 1) + (5l + 1) + (5m + 1)
   * = 5 (i + j + k + l + m + 1)
   *)
  |> List.filter (fun n -> n mod 5 = 1 && p n)
  |> take n
  |> List.iter (Printf.printf "%d ")


open Batteries
let n = read_int ()
let a = Array.init n @@ fun _ -> Scanf.scanf " %d" @@ fun a -> a
let dp = Array.init n (fun _ -> 0)

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

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

let rec i_loop i =
  let rec j_loop j =
    if j >= n then 0 else
    (if dp.(j) - dp.(i) = 0 then 1 + (j_loop @@ j+1) else j_loop @@ j+1)
  in if i >= n-1 then 0 else
  (
    (j_loop @@ i+1) + (i_loop @@ i+1)
  )

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

Score : 200 points

Problem Statement

We have an integer sequence A, whose length is N.

Find the number of the non-empty contiguous subsequences of A whose sums are 0.
Note that we are counting the ways to take out subsequences.
That is, even if the contents of some two subsequences are the same, they are counted individually if they are taken from different positions.

Constraints

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

-10^9 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Find the number of the non-empty contiguous subsequences of A whose sum is 0.

Sample Input 1

6
1 3 -4 2 2 -2

Sample Output 1

3

There are three contiguous subsequences whose sums are 0: (1,3,-4), (-4,2,2) and (2,-2).

Sample Input 2

7
1 -1 1 -1 1 -1 1

Sample Output 2

12

In this case, some subsequences that have the same contents but are taken from different positions are counted individually.
For example, three occurrences of (1, -1) are counted.

Sample Input 3

5
1 -2 3 -4 5

Sample Output 3

0

There are no contiguous subsequences whose sums are 0.


let () =
  Scanf.scanf "%d" @@ fun n ->
  let s = Array.init n (fun _ -> Scanf.scanf " %s" (fun x -> x)) in
  let s = Array.init (2*n-1) (fun i ->
    Array.init (2*n-1) (fun j -> s.(i mod n).[j mod n])) in
  Array.init n (fun i ->
      [(i, 0); (0, i)] |> List.sort_uniq compare |> List.fold_left (fun c (dx, dy) ->
        let sym =
          Array.init (n*n) (fun j ->
            s.(dx + j/n).(dy + j mod n) = s.(dx + j mod n).(dy + j/n))
          |> Array.fold_left (&&) true
        in
        c + if sym then n-i else 0) 0)
  |> Array.fold_left (+) 0
  |> Printf.printf "%d\n"

Score : 500 points

Problem Statement

Snuke has two boards, each divided into a grid with N rows and N columns.
For both of these boards, the square at the i-th row from the top and the j-th column from the left is called Square (i,j).

There is a lowercase English letter written in each square on the first board. The letter written in Square (i,j) is S_{i,j}. On the second board, nothing is written yet.

Snuke will write letters on the second board, as follows:

First, choose two integers A and B ( 0 \leq A, B < N ).

Write one letter in each square on the second board.
Specifically, write the letter written in Square ( i+A, j+B ) on the first board into Square (i,j) on the second board.
Here, the k-th row is also represented as the (N+k)-th row, and the k-th column is also represented as the (N+k)-th column.

After this operation, the second board is called a good board when, for every i and j ( 1 \leq i, j \leq N ), the letter in Square (i,j) and the letter in Square (j,i) are equal.

Find the number of the ways to choose integers A and B ( 0 \leq A, B < N ) such that the second board is a good board.

Constraints

1 \leq N \leq 300

S_{i,j} ( 1 \leq i, j \leq N ) is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

N
S_{1,1}S_{1,2}..S_{1,N}
S_{2,1}S_{2,2}..S_{2,N}
:
S_{N,1}S_{N,2}..S_{N,N}

Output

Print the number of the ways to choose integers A and B ( 0 \leq A, B < N ) such that the second board is a good board.

Sample Input 1

2
ab
ca

Sample Output 1

2

For each pair of A and B, the second board will look as shown below:

The second board is a good board when (A,B) = (0,1) or (A,B) = (1,0), thus the answer is 2.

Sample Input 2

4
aaaa
aaaa
aaaa
aaaa

Sample Output 2

16

Every possible choice of A and B makes the second board good.

Sample Input 3

5
abcde
fghij
klmno
pqrst
uvwxy

Sample Output 3

0

No possible choice of A and B makes the second board good.


let () =
    read_line ()
    |> Str.split (Str.regexp "")
    |> List.filter ((=) "o")
    |> List.length
    |> (( * ) 100)
    |> (( + ) 700)
    |> string_of_int
    |> print_endline

Score : 100 points

Problem Statement

In "Takahashi-ya", a ramen restaurant, a bowl of ramen costs 700 yen (the currency of Japan), plus 100 yen for each kind of topping (boiled egg, sliced pork, green onions).

A customer ordered a bowl of ramen and told which toppings to put on his ramen to a clerk. The clerk took a memo of the order as a string S. S is three characters long, and if the first character in S is o, it means the ramen should be topped with boiled egg; if that character is x, it means the ramen should not be topped with boiled egg. Similarly, the second and third characters in S mean the presence or absence of sliced pork and green onions on top of the ramen.

Write a program that, when S is given, prints the price of the corresponding bowl of ramen.

Constraints

S is a string of length 3.

Each character in S is o or x.

Input

Input is given from Standard Input in the following format:

S

Output

Print the price of the bowl of ramen corresponding to S.

Sample Input 1

oxo

Sample Output 1

900

The price of a ramen topped with two kinds of toppings, boiled egg and green onions, is 700 + 100 \times 2 = 900 yen.

Sample Input 2

ooo

Sample Output 2

1000

The price of a ramen topped with all three kinds of toppings is 700 + 100 \times 3 = 1000 yen.

Sample Input 3

xxx

Sample Output 3

700

The price of a ramen without any toppings is 700 yen.


open Scanf
open Printf

let ($) f x = f x
let string2charlist s = let rec proc i res =
                          try proc (i+1) $ s.[i]::res
                          with Invalid_argument _ -> res
  in List.rev $ proc 0 []

let rec ocaunt = function
    [] -> 0
  | x::xs -> if x=='o' then 1+ocaunt xs else ocaunt xs;;

printf "%d" $ 700 + 100 * ocaunt (string2charlist $ read_line ()) ;;


(* O(n) *)
Scanf.scanf "%d %d" @@ fun n x ->
  let ms = Array.init n @@ fun _ -> Scanf.scanf " %d" @@ (+) 0 in
  let x' = x - Array.fold_left (+) 0 ms in
  n + x' / Array.fold_left min max_int ms |> Printf.printf "%d\n"

Score : 200 points

Problem Statement

Akaki, a patissier, can make N kinds of doughnut using only a certain powder called "Okashi no Moto" (literally "material of pastry", simply called Moto below) as ingredient. These doughnuts are called Doughnut 1, Doughnut 2, ..., Doughnut N. In order to make one Doughnut i (1 ≤ i ≤ N), she needs to consume m_i grams of Moto. She cannot make a non-integer number of doughnuts, such as 0.5 doughnuts.

Now, she has X grams of Moto. She decides to make as many doughnuts as possible for a party tonight. However, since the tastes of the guests differ, she will obey the following condition:

For each of the N kinds of doughnuts, make at least one doughnut of that kind.

At most how many doughnuts can be made here? She does not necessarily need to consume all of her Moto. Also, under the constraints of this problem, it is always possible to obey the condition.

Constraints

2 ≤ N ≤ 100

1 ≤ m_i ≤ 1000

m_1 + m_2 + ... + m_N ≤ X ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
m_1
m_2
:
m_N

Output

Print the maximum number of doughnuts that can be made under the condition.

Sample Input 1

3 1000
120
100
140

Sample Output 1

9

She has 1000 grams of Moto and can make three kinds of doughnuts. If she makes one doughnut for each of the three kinds, she consumes 120 + 100 + 140 = 360 grams of Moto. From the 640 grams of Moto that remains here, she can make additional six Doughnuts 2. This is how she can made a total of nine doughnuts, which is the maximum.

Sample Input 2

4 360
90
90
90
90

Sample Output 2

4

Making one doughnut for each of the four kinds consumes all of her Moto.

Sample Input 3

5 3000
150
130
150
130
110

Sample Output 3

26


open Batteries

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

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

let scan_array n m e conv =
  let arr = Array.make_matrix n m e in
  Enum.Labels.iter (0 --^ n)
    ~f:(fun i -> arr.(i) <- Array.of_list @@ scan_list conv);
  arr

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

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

let (a,b,c,x,y) = scan "%d %d %d %d %d" Tuple.Tuple5.make

let () =
  let n = min x y in
  let m = max x y in
  List.min [2*n*c+a*(x-n)+b*(y-n); a*x+b*y; 2*c*m]
  |> Printf.printf "%d\n"

Score : 300 points

Problem Statement

"Pizza At", a fast food chain, offers three kinds of pizza: "A-pizza", "B-pizza" and "AB-pizza". A-pizza and B-pizza are completely different pizzas, and AB-pizza is one half of A-pizza and one half of B-pizza combined together. The prices of one A-pizza, B-pizza and AB-pizza are A yen, B yen and C yen (yen is the currency of Japan), respectively.

Nakahashi needs to prepare X A-pizzas and Y B-pizzas for a party tonight. He can only obtain these pizzas by directly buying A-pizzas and B-pizzas, or buying two AB-pizzas and then rearrange them into one A-pizza and one B-pizza. At least how much money does he need for this? It is fine to have more pizzas than necessary by rearranging pizzas.

Constraints

1 ≤ A, B, C ≤ 5000

1 ≤ X, Y ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C X Y

Output

Print the minimum amount of money required to prepare X A-pizzas and Y B-pizzas.

Sample Input 1

1500 2000 1600 3 2

Sample Output 1

7900

It is optimal to buy four AB-pizzas and rearrange them into two A-pizzas and two B-pizzas, then buy additional one A-pizza.

Sample Input 2

1500 2000 1900 3 2

Sample Output 2

8500

It is optimal to directly buy three A-pizzas and two B-pizzas.

Sample Input 3

1500 2000 500 90000 100000

Sample Output 3

100000000

It is optimal to buy 200000 AB-pizzas and rearrange them into 100000 A-pizzas and 100000 B-pizzas. We will have 10000 more A-pizzas than necessary, but that is fine.


open Batteries

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

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

let scan_array n m e conv =
  let arr = Array.make_matrix n m e in
  Enum.Labels.iter (0 --^ n)
    ~f:(fun i -> arr.(i) <- Array.of_list @@ scan_list conv);
  arr

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

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

let (a,b,c,x,y) = scan "%d %d %d %d %d" Tuple.Tuple5.make

let () =
  let n = min x y in
  List.min [2*n*c+a*(x-n)+b*(y-n); a*x+b*y]
  |> Printf.printf "%d\n"


let () =
  let a, b, c, x, y = Scanf.scanf "%d %d %d %d %d" (fun a b c x y -> a, b, c, x, y) in
  Array.init 100001 (fun x -> x)
  |> Array.map (fun i -> a *  (max 0 (x-i)) +  b * (max 0 (y-i))  + 2 * c * i)
  |> Array.fold_left min max_int
  |> Printf.printf "%d\n"


Scanf.scanf "%d %d" (fun n c ->
    let xv = Array.init n (fun _ -> Scanf.scanf " %d %d" (fun x v -> x, v)) in
    Array.sort compare xv;
    let as1 = Array.make n 0 in
    let as2 = Array.make n 0 in
    let rec loop i cur mxdiff =
        if i < n then (
            let (x, v) = xv.(i) in
            let cur = cur + v in
            let mxdiff = max mxdiff (cur - x) in
            as1.(i) <- mxdiff;
            loop (i + 1) cur mxdiff
        )
    in
    loop 0 0 0;
    let rec loop i cur mxdiff =
        if i >= 0 then (
            let (x, v) = xv.(i) in
            let cur = cur + v in
            let mxdiff = max mxdiff (cur - (c - x)) in
            as2.(i) <- mxdiff;
            loop (i - 1) cur mxdiff
        )
    in
    loop (n - 1) 0 0;

    let rec loop i cur mx =
        if i = n then mx else
            let (x, v) = xv.(i) in
            let cur = cur + v in
            let cost = cur - x * 2 + (if i = n - 1 then x else as2.(i + 1)) in
            loop (i + 1) cur (max mx cost)
    in
    let a1 = loop 0 0 as2.(0) in

    let rec loop i cur mx =
        if i < 0 then mx else
            let (x, v) = xv.(i) in
            let cur = cur + v in
            let cost = cur - (c - x) * 2 + (if i = 0 then c - x else as1.(i - 1)) in
            loop (i - 1) cur (max mx cost)
    in
    let a2 = loop (n - 1) 0 as1.(n - 1) in
    Printf.printf "%d\n" @@ max a1 a2
)

Score : 500 points

Problem Statement

"Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter.

Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories.

Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter.

Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger.

Constraints

1 ≤ N ≤ 10^5

2 ≤ C ≤ 10^{14}

1 ≤ x_1 < x_2 < ... < x_N < C

1 ≤ v_i ≤ 10^9

All values in input are integers.

Subscores

300 points will be awarded for passing the test set satisfying N ≤ 100.

Input

Input is given from Standard Input in the following format:

N C
x_1 v_1
x_2 v_2
:
x_N v_N

Output

If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c.

Sample Input 1

3 20
2 80
9 120
16 1

Sample Output 1

191

There are three sushi on the counter with a circumference of 20 meters. If he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks seven more meters clockwise, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 9 kilocalories, thus he can take in 191 kilocalories on balance, which is the largest possible value.

Sample Input 2

3 20
2 80
9 1
16 120

Sample Output 2

192

The second and third sushi have been swapped. Again, if he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks six more meters counterclockwise this time, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 8 kilocalories, thus he can take in 192 kilocalories on balance, which is the largest possible value.

Sample Input 3

1 100000000000000
50000000000000 1

Sample Output 3

0

Even though the only sushi is so far that it does not fit into a 32-bit integer, its nutritive value is low, thus he should immediately leave without doing anything.

Sample Input 4

15 10000000000
400000000 1000000000
800000000 1000000000
1900000000 1000000000
2400000000 1000000000
2900000000 1000000000
3300000000 1000000000
3700000000 1000000000
3800000000 1000000000
4000000000 1000000000
4100000000 1000000000
5200000000 1000000000
6600000000 1000000000
8000000000 1000000000
9300000000 1000000000
9700000000 1000000000

Sample Output 4

6500000000

All these sample inputs above are included in the test set for the partial score.


let a, b, c, x, y = Scanf.sscanf (read_line ()) "%d %d %d %d %d" @@ fun a b c x y -> (a, b, c, x, y)

let buy_a n = if c * 2 < a then n * c * 2 else n * a
let buy_b n = if c * 2 < b then n * c * 2 else n * b

let () = Printf.printf "%d\n" @@
  if a + b >= c * 2 then
    2 * c * (min x y) + if x > y then buy_a (x - y) else buy_b (y - x)
  else buy_a x + buy_b y


let f a b x = if a <= x && x <= a+b then "YES" else "NO";;
let () = Scanf.scanf "%d %d %d" f
|> Printf.printf "%s\n"

Score : 100 points

Problem Statement

There are a total of A + B cats and dogs.
Among them, A are known to be cats, but the remaining B are not known to be either cats or dogs.

Determine if it is possible that there are exactly X cats among these A + B animals.

Constraints

1 \leq A \leq 100

1 \leq B \leq 100

1 \leq X \leq 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B X

Output

If it is possible that there are exactly X cats, print YES; if it is impossible, print NO.

Sample Input 1

3 5 4

Sample Output 1

YES

If there are one cat and four dogs among the B = 5 animals, there are X = 4 cats in total.

Sample Input 2

2 2 6

Sample Output 2

NO

Even if all of the B = 2 animals are cats, there are less than X = 6 cats in total.

Sample Input 3

5 3 2

Sample Output 3

NO

Even if all of the B = 3 animals are dogs, there are more than X = 2 cats in total.


let () =
let a, b, x = Scanf.scanf "%d %d %d\n" (fun a b c -> a, b, c) in
Printf.printf "%s\n" (if a <= x && x <= a+b then "YES" else "NO")


let () = Scanf.scanf "%d %d %d" @@ fun a b x ->
  print_endline @@
    if a <= x && x <= a + b then "YES" else "NO"


let n, m, x = Scanf.scanf " %d %d %d" @@ fun a b c -> a, b, c
let a_s = Array.init m @@ fun _ -> Scanf.scanf " %d" @@ (+) 0
let ans = Array.make 2 0
let _ =
  for i = 0 to m - 1 do
    if a_s.(i) < x then ans.(0) <- ans.(0) + 1
    else ans.(1) <- ans.(1) + 1 done;
  min ans.(0) ans.(1) |> Printf.printf "%d\n"

Score : 200 points

Problem Statement

There are N + 1 squares arranged in a row, numbered 0, 1, ..., N from left to right.

Initially, you are in Square X.
You can freely travel between adjacent squares. Your goal is to reach Square 0 or Square N.
However, for each i = 1, 2, ..., M, there is a toll gate in Square A_i, and traveling to Square A_i incurs a cost of 1.
It is guaranteed that there is no toll gate in Square 0, Square X and Square N.

Find the minimum cost incurred before reaching the goal.

Constraints

1 \leq N \leq 100

1 \leq M \leq 100

1 \leq X \leq N - 1

1 \leq A_1 < A_2 < ... < A_M \leq N

A_i \neq X

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M X
A_1 A_2 ... A_M

Output

Print the minimum cost incurred before reaching the goal.

Sample Input 1

5 3 3
1 2 4

Sample Output 1

1

The optimal solution is as follows:

First, travel from Square 3 to Square 4. Here, there is a toll gate in Square 4, so the cost of 1 is incurred.

Then, travel from Square 4 to Square 5. This time, no cost is incurred.

Now, we are in Square 5 and we have reached the goal.

In this case, the total cost incurred is 1.

Sample Input 2

7 3 2
4 5 6

Sample Output 2

0

We may be able to reach the goal at no cost.

Sample Input 3

10 7 5
1 2 3 4 6 8 9

Sample Output 3

3


let () =
let n, m, x = Scanf.scanf "%d %d %d\n" (fun a b c -> a, b, c) in
let lst = Array.to_list (Array.init m (fun _ -> Scanf.scanf "%d " (fun x -> x))) in
let head = List.length (List.filter (fun a -> a < x) lst) in
let tail = List.length (List.filter (fun a -> a > x) lst) in
Printf.printf "%d\n" (min head tail)


let () =
  let n, m, x = Scanf.scanf "%d %d %d" (fun n m x -> n, m, x) in
  let a = Array.init m (fun _ -> Scanf.scanf " %d" (fun x -> x)) in
  let (l, r) =  Array.fold_left (fun (l, r) ai  -> if ai < x then (l+1, r) else (l, r+1)) (0,0)  a in
  Printf.printf "%d\n" (if r < l then r else l)


let () =
  Scanf.scanf "%d" @@ fun n ->
  let a = Array.init n (fun _ -> Scanf.scanf " %d" (fun x -> x)) in
  let a' = Array.init n (fun i -> a.(i)) in
  Array.sort (-) a';
  let m1, m2 = a'.(n/2-1), a'.(n/2) in
  Array.iter (fun e ->
    Printf.printf "%d\n" @@ if e <= m1 then m2 else m1) a

Score : 300 points

Problem Statement

When l is an odd number, the median of l numbers a_1, a_2, ..., a_l is the (\frac{l+1}{2})-th largest value among a_1, a_2, ..., a_l.

You are given N numbers X_1, X_2, ..., X_N, where N is an even number.
For each i = 1, 2, ..., N, let the median of X_1, X_2, ..., X_N excluding X_i, that is, the median of X_1, X_2, ..., X_{i-1}, X_{i+1}, ..., X_N be B_i.

Find B_i for each i = 1, 2, ..., N.

Constraints

2 \leq N \leq 200000

N is even.

1 \leq X_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 ... X_N

Output

Print N lines.
The i-th line should contain B_i.

Sample Input 1

4
2 4 4 3

Sample Output 1

4
3
3
4

Since the median of X_2, X_3, X_4 is 4, B_1 = 4.

Since the median of X_1, X_3, X_4 is 3, B_2 = 3.

Since the median of X_1, X_2, X_4 is 3, B_3 = 3.

Since the median of X_1, X_2, X_3 is 4, B_4 = 4.

Sample Input 2

2
1 2

Sample Output 2

2
1

Sample Input 3

6
5 5 4 4 3 3

Sample Output 3

4
4
4
4
4
4


let () =
  Scanf.scanf "%d" @@ fun n ->
  let a = Array.init n (fun _ -> Scanf.scanf " %d" ((+) 0)) in
  Array.sort (-) a;
  let m = a.(n-1) in
  let r = Array.fold_left (fun r e ->
    if abs (m-2*e) < abs (m-2*r) then e else r) a.(0) a in
  Printf.printf "%d %d\n" m r

Score : 400 points

Problem Statement

Let {\rm comb}(n,r) be the number of ways to choose r objects from among n objects, disregarding order.
From n non-negative integers a_1, a_2, ..., a_n, select two numbers a_i > a_j so that {\rm comb}(a_i,a_j) is maximized.
If there are multiple pairs that maximize the value, any of them is accepted.

Constraints

2 \leq n \leq 10^5

0 \leq a_i \leq 10^9

a_1,a_2,...,a_n are pairwise distinct.

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 a_i and a_j that you selected, with a space in between.

Sample Input 1

5
6 9 4 2 11

Sample Output 1

11 6

\rm{comb}(a_i,a_j) for each possible selection is as follows:

\rm{comb}(4,2)=6

\rm{comb}(6,2)=15

\rm{comb}(6,4)=15

\rm{comb}(9,2)=36

\rm{comb}(9,4)=126

\rm{comb}(9,6)=84

\rm{comb}(11,2)=55

\rm{comb}(11,4)=330

\rm{comb}(11,6)=462

\rm{comb}(11,9)=55

Thus, we should print 11 and 6.

Sample Input 2

2
100 0

Sample Output 2

100 0


let () =
    let s = read_line () |> Str.split (Str.regexp "") in
    let ans = List.fold_left (fun (a, b, c) elt ->
            match elt with
            | "a" -> (a + 1, b, c)
            | "b" -> (a, b + 1, c)
            |  _  -> (a, b, c + 1)
        ) (0, 0, 0) s
    in
    if ans = (1, 1, 1) then print_endline "Yes"
    else print_endline "No"

Score : 100 points

Problem Statement

You are given a string S of length 3 consisting of a, b and c. Determine if S can be obtained by permuting abc.

Constraints

|S|=3

S consists of a, b and c.

Input

Input is given from Standard Input in the following format:

S

Output

If S can be obtained by permuting abc, print Yes; otherwise, print No.

Sample Input 1

bac

Sample Output 1

Yes

Swapping the first and second characters in bac results in abc.

Sample Input 2

bab

Sample Output 2

No

Sample Input 3

abc

Sample Output 3

Yes

Sample Input 4

aaa

Sample Output 4

No


let s = read_line ()
let n = String.length s
let us = Array.make 26 false
let j = ref ~-1
let _ = Char.(String.(iteri (fun i c -> if n < 26 || !j = -1 then us.(code c - code 'a') <- true; if i < n - 1 && s.[i] < s.[i + 1] then j := i) s; if n = 26 && !j = -1 then (print_endline "-1"; exit 0); let m, t = if n < 26 then 0, s else code s.[!j] - code 'a' + 1, sub s 0 !j in for i = m to 25 do if not us.(i) then (Printf.printf "%s%c\n" t (chr (i + code 'a')); exit 0) done))

Score : 300 points

Problem Statement

Gotou just received a dictionary. However, he doesn't recognize the language used in the dictionary. He did some analysis on the dictionary and realizes that the dictionary contains all possible diverse words in lexicographical order.

A word is called diverse if and only if it is a nonempty string of English lowercase letters and all letters in the word are distinct. For example, atcoder, zscoder and agc are diverse words while gotou and connect aren't diverse words.

Given a diverse word S, determine the next word that appears after S in the dictionary, i.e. the lexicographically smallest diverse word that is lexicographically larger than S, or determine that it doesn't exist.

Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Constraints

1 \leq |S| \leq 26

S is a diverse word.

Input

Input is given from Standard Input in the following format:

S

Output

Print the next word that appears after S in the dictionary, or -1 if it doesn't exist.

Sample Input 1

atcoder

Sample Output 1

atcoderb

atcoderb is the lexicographically smallest diverse word that is lexicographically larger than atcoder. Note that atcoderb is lexicographically smaller than b.

Sample Input 2

abc

Sample Output 2

abcd

Sample Input 3

zyxwvutsrqponmlkjihgfedcba

Sample Output 3

-1

This is the lexicographically largest diverse word, so the answer is -1.

Sample Input 4

abcdefghijklmnopqrstuvwzyx

Sample Output 4

abcdefghijklmnopqrstuvx


let rec f c s =
  try let _ = String.index s (char_of_int c) in f (c+1) s
  with _ -> char_of_int c
let _ = Scanf.scanf "%s" @@ fun s ->
  let l = String.length s in
  let rec g i = if s.[i-1] < s.[i] then i-1 else g (i-1) in
  let rec h t c i = if i >= l then c else
    if t >= s.[i] then h t c (i+1) else h t (min c s.[i]) (i+1) in
  if s = "zyxwvutsrqponmlkjihgfedcba" then print_endline "-1"
  else if l < 26 then
    Printf.printf "%s%c\n" s (f 97 s)
  else
    let j = g (l-1) in
    Printf.printf "%s%c\n" (String.sub s 0 j) (h s.[j] 'z' (j+1))


let () =
  let s = read_line () in
  print_endline @@
  if s = "zyxwvutsrqponmlkjihgfedcba" then "-1"
  else if String.length s = 26 then
    Array.init 25 (fun i -> i)
    |> Array.to_list
    |> List.rev
    |> List.find (fun i ->
        s.[i] <> 'z' &&
        Array.init i (fun j -> Char.code s.[j])
        |> Array.to_list
        |> List.for_all (( <> ) (Char.code s.[i] + 1)))
    |> (fun i -> String.sub s 0 i ^ String.make 1 (Char.chr (1 + Char.code s.[i])))
  else "hoge"
    (* Array.init 26 (fun i -> Char.chr (Char.code 'a' + i))
    |> Array.to_list
    |> List.find (fun c ->
        Array.init (String.length s) (fun i -> s.[i])
        |> Array.to_list
        |> List.for_all (( <> ) c))
    |> String.make 1
    |> ( ^ ) s *)


open Scanf
open Printf

let fare a b c d = min a b + min c d
let (a, b, c, d) = (read_int (), read_int (), read_int (), read_int ())

let () = printf "%d\n" (fare a b c d)

Score : 100 points

Problem Statement

You planned a trip using trains and buses.
The train fare will be A yen (the currency of Japan) if you buy ordinary tickets along the way, and B yen if you buy an unlimited ticket.
Similarly, the bus fare will be C yen if you buy ordinary tickets along the way, and D yen if you buy an unlimited ticket.

Find the minimum total fare when the optimal choices are made for trains and buses.

Constraints

1 \leq A \leq 1 000

1 \leq B \leq 1 000

1 \leq C \leq 1 000

1 \leq D \leq 1 000

All input values are integers.

Input

Input is given from Standard Input in the following format:

A
B
C
D

Output

Print the minimum total fare.

Sample Input 1

600
300
220
420

Sample Output 1

520

The train fare will be 600 yen if you buy ordinary tickets, and 300 yen if you buy an unlimited ticket.
Thus, the optimal choice for trains is to buy an unlimited ticket for 300 yen.
On the other hand, the optimal choice for buses is to buy ordinary tickets for 220 yen.

Therefore, the minimum total fare is 300 + 220 = 520 yen.

Sample Input 2

555
555
400
200

Sample Output 2

755

Sample Input 3

549
817
715
603

Sample Output 3

1152


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


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


let () =
  let n = Scanf.scanf "%d " (fun a -> a) in
  let d,x = Scanf.scanf "%d %d " (fun a b -> a,b) in
  let a_lst = Array.to_list @@ Array.init n (fun _ -> Scanf.scanf "%d " (fun a -> a)) in

  let l = List.map (fun a -> 1+ (d-1) / a) a_lst in
  let sum = List.fold_left (+) 0 l in

  Printf.printf "%d\n" @@ sum + x

Score : 200 points

Problem Statement

Some number of chocolate pieces were prepared for a training camp.
The camp had N participants and lasted for D days.
The i-th participant (1 \leq i \leq N) ate one chocolate piece on each of the following days in the camp: the 1-st day, the (A_i + 1)-th day, the (2A_i + 1)-th day, and so on.
As a result, there were X chocolate pieces remaining at the end of the camp. During the camp, nobody except the participants ate chocolate pieces.

Find the number of chocolate pieces prepared at the beginning of the camp.

Constraints

1 \leq N \leq 100

1 \leq D \leq 100

1 \leq X \leq 100

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

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
D X
A_1
A_2
:
A_N

Output

Find the number of chocolate pieces prepared at the beginning of the camp.

Sample Input 1

3
7 1
2
5
10

Sample Output 1

8

The camp has 3 participants and lasts for 7 days.
Each participant eats chocolate pieces as follows:

The first participant eats one chocolate piece on Day 1, 3, 5 and 7, for a total of four.

The second participant eats one chocolate piece on Day 1 and 6, for a total of two.

The third participant eats one chocolate piece only on Day 1, for a total of one.

Since the number of pieces remaining at the end of the camp is one, the number of pieces prepared at the beginning of the camp is 1 + 4 + 2 + 1 = 8.

Sample Input 2

2
8 20
1
10

Sample Output 2

29

Sample Input 3

5
30 44
26
18
81
18
6

Sample Output 3

56


let id x = x
let n = Scanf.scanf "%d\n" id
let d, x = Scanf.scanf "%d %d\n" (fun a b -> a, b)

let eat freq =
  let rec aux day sum =
    if d < day then sum else aux (day + freq) (sum + 1)
  in
  aux 1 0

let solve n =
  let rec aux i sum =
    if i = 0 then sum
    else aux (i - 1) (sum + eat (Scanf.scanf "%d\n" id))
  in
  aux n 0

let () = solve n |> (+) x |> Printf.printf "%d\n"


let n = Scanf.scanf " %d" (+) 0
let a_s = Array.init n @@ fun _ -> Scanf.scanf " %d" (+) 0
let s = Array.fold_left (fun (s, p) a -> s + abs (p - a), a) (abs a_s.(n - 1), 0) a_s |> fst
let f i = if i < 0 || i = n then 0 else a_s.(i)
let g i = s - abs (f (i - 1) - f i) - abs (f i - f (i + 1)) + abs (f (i - 1) - f (i + 1))
let _ = Array.iteri (fun i a -> Printf.printf "%d\n" @@ g i) a_s

Score : 300 points

Problem Statement

There are N sightseeing spots on the x-axis, numbered 1, 2, ..., N.
Spot i is at the point with coordinate A_i.
It costs |a - b| yen (the currency of Japan) to travel from a point with coordinate a to another point with coordinate b along the axis.

You planned a trip along the axis.
In this plan, you first depart from the point with coordinate 0, then visit the N spots in the order they are numbered, and finally return to the point with coordinate 0.

However, something came up just before the trip, and you no longer have enough time to visit all the N spots, so you decided to choose some i and cancel the visit to Spot i.
You will visit the remaining spots as planned in the order they are numbered.
You will also depart from and return to the point with coordinate 0 at the beginning and the end, as planned.

For each i = 1, 2, ..., N, find the total cost of travel during the trip when the visit to Spot i is canceled.

Constraints

2 \leq N \leq 10^5

-5000 \leq A_i \leq 5000 (1 \leq i \leq N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print N lines.
In the i-th line, print the total cost of travel during the trip when the visit to Spot i is canceled.

Sample Input 1

3
3 5 -1

Sample Output 1

12
8
10

Spot 1, 2 and 3 are at the points with coordinates 3, 5 and -1, respectively.
For each i, the course of the trip and the total cost of travel when the visit to Spot i is canceled, are as follows:

For i = 1, the course of the trip is 0 \rightarrow 5 \rightarrow -1 \rightarrow 0 and the total cost of travel is 5 + 6 + 1 = 12 yen.

For i = 2, the course of the trip is 0 \rightarrow 3 \rightarrow -1 \rightarrow 0 and the total cost of travel is 3 + 4 + 1 = 8 yen.

For i = 3, the course of the trip is 0 \rightarrow 3 \rightarrow 5 \rightarrow 0 and the total cost of travel is 3 + 2 + 5 = 10 yen.

Sample Input 2

5
1 1 1 2 0

Sample Output 2

4
4
4
2
4

Sample Input 3

6
-679 -2409 -3258 3095 -3291 -4462

Sample Output 3

21630
21630
19932
8924
21630
19288


let () =
  let a, b, c = Scanf.scanf "%d %d %d " (fun a b c -> a, b, c) in
  print_endline (if (a + b < c) then "No" else "Yes")

Score : 100 points

Problem Statement

An elementary school student Takahashi has come to a variety store.

He has two coins, A-yen and B-yen coins (yen is the currency of Japan), and wants to buy a toy that costs C yen. Can he buy it?

Note that he lives in Takahashi Kingdom, and may have coins that do not exist in Japan.

Constraints

All input values are integers.

1 \leq A, B \leq 500

1 \leq C \leq 1000

Input

Input is given from Standard Input in the following format:

A B C

Output

If Takahashi can buy the toy, print Yes; if he cannot, print No.

Sample Input 1

50 100 120

Sample Output 1

Yes

He has 50 + 100 = 150 yen, so he can buy the 120-yen toy.

Sample Input 2

500 100 1000

Sample Output 2

No

He has 500 + 100 = 600 yen, but he cannot buy the 1000-yen toy.

Sample Input 3

19 123 143

Sample Output 3

No

There are 19-yen and 123-yen coins in Takahashi Kingdom, which are rather hard to use.

Sample Input 4

19 123 142

Sample Output 4

Yes


(* O(k^2) (k = max n m) *)
Scanf.scanf " %d" @@ fun n ->
  let ss = Array.init n @@ fun _ -> Scanf.scanf " %s" @@ fun s -> s in
  let m = Scanf.scanf " %d" @@ (+) 0 in
  let ts = Array.init m @@ fun _ -> Scanf.scanf " %s" @@ fun t -> t in
  let ans = ref 0 in
  for i = 0 to n - 1 do
    let c = ref 0 in
    let s = ss.(i) in
    for j = 0 to n - 1 do
      if s = ss.(j) then incr c
    done;
    for j = 0 to m - 1 do
      if s = ts.(j) then decr c
    done;
    ans := max !ans !c
  done;
  Printf.printf "%d\n" !ans

Score : 200 points

Problem Statement

Takahashi has N blue cards and M red cards.
A string is written on each card. The string written on the i-th blue card is s_i, and the string written on the i-th red card is t_i.

Takahashi will now announce a string, and then check every card. Each time he finds a blue card with the string announced by him, he will earn 1 yen (the currency of Japan); each time he finds a red card with that string, he will lose 1 yen.

Here, we only consider the case where the string announced by Takahashi and the string on the card are exactly the same. For example, if he announces atcoder, he will not earn money even if there are blue cards with atcoderr, atcode, btcoder, and so on. (On the other hand, he will not lose money even if there are red cards with such strings, either.)

At most how much can he earn on balance?

Note that the same string may be written on multiple cards.

Constraints

N and M are integers.

1 \leq N, M \leq 100

s_1, s_2, ..., s_N, t_1, t_2, ..., t_M are all strings of lengths between 1 and 10 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N
M
t_1
t_2
:
t_M

Output

If Takahashi can earn at most X yen on balance, print X.

Sample Input 1

3
apple
orange
apple
1
grape

Sample Output 1

2

He can earn 2 yen by announcing apple.

Sample Input 2

3
apple
orange
apple
5
apple
apple
apple
apple
apple

Sample Output 2

1

If he announces apple, he will lose 3 yen. If he announces orange, he can earn 1 yen.

Sample Input 3

1
voldemort
10
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort

Sample Output 3

0

If he announces voldemort, he will lose 9 yen. If he announces orange, for example, he can avoid losing a yen.

Sample Input 4

6
red
red
blue
yellow
yellow
red
5
red
red
yellow
green
blue

Sample Output 4

1


let a, b = Scanf.scanf " %d %d" @@ fun a b -> a, b
let f n = let s = string_of_int n in s.[0] = s.[4] && s.[1] = s.[3]
let ans = ref 0
let _ = for i = a to b do if f i then incr ans done; Printf.printf "%d\n" !ans

Score : 200 points

Problem Statement

Find the number of palindromic numbers among the integers between A and B (inclusive).
Here, a palindromic number is a positive integer whose string representation in base 10 (without leading zeros) reads the same forward and backward.

Constraints

10000 \leq A \leq B \leq 99999

All input values are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the number of palindromic numbers among the integers between A and B (inclusive).

Sample Input 1

11009 11332

Sample Output 1

4

There are four integers that satisfy the conditions: 11011, 11111, 11211 and 11311.

Sample Input 2

31415 92653

Sample Output 2

612


(* O(b log b) *)
Scanf.scanf "%d %d" @@ fun a b ->
  let string_rev s = let n = String.length s in String.init n @@ fun i -> s.[n - i - 1] in
  let ans = ref 0 in
  for n = a to b do
    let s = string_of_int n in
    if string_rev s = s then incr ans
  done;
  Printf.printf "%d\n" !ans


let () =
    let answer = Scanf.scanf "%d %d" (fun n m ->
        match n, m with
        | 1, 1 -> 1
        | 1, _ -> m - 2
        | _, 1 -> n - 2
        | _, _ -> (n - 2) * (m - 2)
    )
    in Printf.printf "%d\n" answer

Score : 300 points

Problem Statement

There is a grid with infinitely many rows and columns. In this grid, there is a rectangular region with consecutive N rows and M columns, and a card is placed in each square in this region.
The front and back sides of these cards can be distinguished, and initially every card faces up.

We will perform the following operation once for each square contains a card:

For each of the following nine squares, flip the card in it if it exists: the target square itself and the eight squares that shares a corner or a side with the target square.

It can be proved that, whether each card faces up or down after all the operations does not depend on the order the operations are performed.
Find the number of cards that face down after all the operations.

Constraints

1 \leq N,M \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of cards that face down after all the operations.

Sample Input 1

2 2

Sample Output 1

0

We will flip every card in any of the four operations. Thus, after all the operations, all cards face up.

Sample Input 2

1 7

Sample Output 2

5

After all the operations, all cards except at both ends face down.

Sample Input 3

314 1592

Sample Output 3

496080


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

let () = Printf.printf "%d\n" @@
  match m, n with
    | 1, 1 -> 1
    | 1, a | a, 1 -> a - 2
    | n, m -> (n - 2) * (m - 2)


let () = Printf.printf "%d\n" @@ Scanf.scanf "%d %d" (fun n k ->
  Array.init n (( + ) 1)
  |> Array.map (fun b ->
      max 0 (b - k) * (n / b)
    + min (n mod b) (max 0 (n mod b - k + 1)))
  |> Array.fold_left ( + ) 0)

Score : 400 points

Problem Statement

Takahashi had a pair of two positive integers not exceeding N, (a,b), which he has forgotten.
He remembers that the remainder of a divided by b was greater than or equal to K.
Find the number of possible pairs that he may have had.

Constraints

1 \leq N \leq 10^5

0 \leq K \leq N-1

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of possible pairs that he may have had.

Sample Input 1

5 2

Sample Output 1

7

There are seven possible pairs: (2,3),(5,3),(2,4),(3,4),(2,5),(3,5) and (4,5).

Sample Input 2

10 0

Sample Output 2

100

Sample Input 3

31415 9265

Sample Output 3

287927211


let () =
  let n = Scanf.scanf "%d " (fun a -> a) in
  Printf.printf "%d\n" (n/3);

Score : 100 points

Problem Statement

There are N students in a school.

We will divide these students into some groups, and in each group they will discuss some themes.

You think that groups consisting of two or less students cannot have an effective discussion, so you want to have as many groups consisting of three or more students as possible.

Divide the students so that the number of groups consisting of three or more students is maximized.

Constraints

1 \leq N \leq 1000

All input values are integers.

Input

Input is given from Standard Input in the following format:

N

Output

If you can form at most x groups consisting of three or more students, print x.

Sample Input 1

8

Sample Output 1

2

For example, you can form a group of three students and another of five students.

Sample Input 2

2

Sample Output 2

0

Sometimes you cannot form any group consisting of three or more students, regardless of how you divide the students.

Sample Input 3

9

Sample Output 3

3


let () = read_int () / 3 |> Printf.printf "%d\n"


let n = Scanf.scanf " %d" (+) 0
let ss = Array.init n @@ fun _ -> Scanf.scanf " %c" @@ fun c -> c
let _ = Array.iter (fun s -> if s = 'Y' then (print_endline "Four"; exit 0)) ss; print_endline "Three"

Score : 200 points

Problem Statement

In Japan, people make offerings called hina arare, colorful crackers, on March 3.

We have a bag that contains N hina arare. (From here, we call them arare.)

It is known that the bag either contains arare in three colors: pink, white and green, or contains arare in four colors: pink, white, green and yellow.

We have taken out the arare in the bag one by one, and the color of the i-th arare was S_i, where colors are represented as follows - pink: P, white: W, green: G, yellow: Y.

If the number of colors of the arare in the bag was three, print Three; if the number of colors was four, print Four.

Constraints

1 \leq N \leq 100

S_i is P, W, G or Y.

There always exist i, j and k such that S_i=P, S_j=W and S_k=G.

Input

Input is given from Standard Input in the following format:

N
S_1 S_2 ... S_N

Output

If the number of colors of the arare in the bag was three, print Three; if the number of colors was four, print Four.

Sample Input 1

6
G W Y P Y W

Sample Output 1

Four

The bag contained arare in four colors, so you should print Four.

Sample Input 2

9
G W W G P W P G G

Sample Output 2

Three

The bag contained arare in three colors, so you should print Three.

Sample Input 3

8
P Y W G Y W Y Y

Sample Output 3

Four


let n = Scanf.scanf " %d" (+) 0
let ss = Array.to_list @@ Array.init n @@ fun _ -> Scanf.scanf " %c" (=) 'Y'
let _ = print_endline @@ if List.mem true ss then "Four" else "Three"


let n = Scanf.scanf " %d" @@ (+) 0
let ss = Array.init n @@ fun _ -> Scanf.scanf " %c" @@ fun c -> c
let _ =
  for i = 0 to n - 1 do if ss.(i) = 'Y' then (print_endline "Four"; exit 0) done;
  print_endline "Three"


open Batteries;;

let rec combination n r =
  if n = r || r = 0 then 1
  else (combination n (r - 1)) * (n - r + 1) / r
;;

let () =
  let n = read_int () in
  let l = Array.to_list @@
      Array.init n
                 (fun _ -> read_line ()) in
  let lhd = List.map (fun x -> String.get x 0) l in
  let m = List.length @@ List.filter (fun x -> 'M'=x) lhd in
  let a = List.length @@ List.filter (fun x -> 'A'=x) lhd in
  let r = List.length @@ List.filter (fun x -> 'R'=x) lhd in
  let c = List.length @@ List.filter (fun x -> 'C'=x) lhd in
  let h = List.length @@ List.filter (fun x -> 'H'=x) lhd in
  let x = List.length (List.filter (fun x -> x<>0) [m;a;r;c;h]) in
  let ans = m*a*r + m*a*c + m*a*h + m*r*c + m*r*h + m*c*h
            + a*r*c + a*r*h + a*c*h
            + r*c*h in

  let mc = combination m 1 in
  let ac = combination a 1 in
  let rc = combination r 1 in
  let cc = combination c 1 in
  let hc = combination h 1 in
  print_int ans
;;

Score : 300 points

Problem Statement

There are N people. The name of the i-th person is S_i.

We would like to choose three people so that the following conditions are met:

The name of every chosen person begins with M, A, R, C or H.

There are no multiple people whose names begin with the same letter.

How many such ways are there to choose three people, disregarding order?

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

Constraints

1 \leq N \leq 10^5

S_i consists of uppercase English letters.

1 \leq |S_i| \leq 10

S_i \neq S_j (i \neq j)

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If there are x ways to choose three people so that the given conditions are met, print x.

Sample Input 1

5
MASHIKE
RUMOI
OBIRA
HABORO
HOROKANAI

Sample Output 1

2

We can choose three people with the following names:

MASHIKE, RUMOI, HABORO

MASHIKE, RUMOI, HOROKANAI

Thus, we have two ways.

Sample Input 2

4
ZZ
ZZZ
Z
ZZZZZZZZZZ

Sample Output 2

0

Note that there may be no ways to choose three people so that the given conditions are met.

Sample Input 3

5
CHOKUDAI
RNG
MAKOTO
AOKI
RINGO

Sample Output 3

7


Printf.printf "%d\n" @@ Scanf.scanf "%s" @@ fun s ->
  let t = Array.init (String.length s) (fun i -> s.[i]) in
  let x = Array.fold_left (fun z c -> z + (int_of_char c - 48) mod 10) 0 t in
  (int_of_char s.[0] - 49) + (9 * (String.length s - 1)) |> max x

Score : 300 points

Problem Statement

Find the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

Constraints

1\leq N \leq 10^{16}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

Sample Input 1

100

Sample Output 1

18

For example, the sum of the digits in 99 is 18, which turns out to be the maximum value.

Sample Input 2

9995

Sample Output 2

35

For example, the sum of the digits in 9989 is 35, which turns out to be the maximum value.

Sample Input 3

3141592653589793

Sample Output 3

137


let () = Scanf.scanf "%d\n" @@ fun n ->
  let xys = Array.init n @@ fun i ->
    Scanf.scanf "%d %d\n" @@ fun x y -> x, y, i in
  Array.sort compare xys;
  let convex =
    Array.of_list @@
    let h, t =
      match
        Array.fold_left (fun stack (x, y, i) ->
          let rec drop = function
            | ([] | [_]) as stack -> stack
            | ((x1, y1, _) :: (((x2, y2, _) :: _) as rest)) as stack ->
                if 0 < (x1 - x2) * (y - y1) - (y1 - y2) * (x - x1)
                then stack
                else drop rest in (x, y, i) :: drop stack) [] xys
      with
      | [] -> [], []
      | h :: t -> [h], t in
    Array.fold_right (fun (x, y, i) stack ->
      let rec drop = function
        | ([] | [_]) as stack -> stack
        | ((x1, y1, _) :: (((x2, y2, _) :: _) as rest)) as stack ->
            if 0 < (x1 - x2) * (y - y1) - (y1 - y2) * (x - x1)
            then stack
            else drop rest in (x, y, i) :: drop stack) (Array.sub xys 0 (n - 1)) h @ t in
  let convex = Array.sub convex 0 (Array.length convex - 1) in
  let ps = Array.make n 0. in
  for i = 0 to Array.length convex - 1 do
    let (x1, y1, _) = convex.(i) in
    let (x2, y2, j) = convex.((i + 1) mod Array.length convex) in
    let (x3, y3, _) = convex.((i + 2) mod Array.length convex) in
    ps.(j) <-
      0.5 -. acos
        (float_of_int ((x1 - x2) * (x3 - x2) + (y1 - y2) * (y3 - y2)) /.
         sqrt
           ((float_of_int (x1 - x2) ** 2. +. float_of_int (y1 - y2) ** 2.) *.
            (float_of_int (x3 - x2) ** 2. +. float_of_int (y3 - y2) ** 2.)))
      /. (8. *. atan 1.)
  done;
  Array.iter (Printf.printf "%.20f\n") ps

Score : 600 points

Problem Statement

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

Let R=10^{10^{10^{10}}}. Ringo performs the following operation:

Randomly choose a point from the interior of a circle of radius R centered at the origin, and put Snuke there. Snuke will move to the hole with the smallest Euclidean distance from the point, and fall into that hole. If there are multiple such holes, the hole with the smallest index will be chosen.

For every i (1 \leq i \leq N), find the probability that Snuke falls into the i-th hole.

Here, the operation of randomly choosing a point from the interior of a circle of radius R is defined as follows:

Pick two real numbers x and y independently according to uniform distribution on [-R,R].

If x^2+y^2\leq R^2, the point (x,y) is chosen. Otherwise, repeat picking the real numbers x,y until the condition is met.

Constraints

2 \leq N \leq 100

|x_i|,|y_i| \leq 10^6(1\leq i\leq N)

All given points are pairwise distinct.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print N real numbers. The i-th real number must represent the probability that Snuke falls into the i-th hole.

The output will be judged correct when, for all output values, the absolute or relative error is at most 10^{-5}.

Sample Input 1

2
0 0
1 1

Sample Output 1

0.5
0.5

If Ringo put Snuke in the region x+y\leq 1, Snuke will fall into the first hole. The probability of this happening is very close to 0.5.
Otherwise, Snuke will fall into the second hole, the probability of which happening is also very close to 0.5.

Sample Input 2

5
0 0
2 8
4 5
2 6
3 10

Sample Output 2

0.43160120892732328768
0.03480224363653196956
0.13880483535586193855
0.00000000000000000000
0.39479171208028279727


let () =
  let (n, a) = Scanf.scanf "%d %d" (fun x y -> (x, y)) in
  let remainder = n mod 500 in
  let ans = if remainder < a
            then "Yes"
            else "No" in
  Printf.printf "%s\n" ans

Score: 100 points

Problem Statement

E869120 has A 1-yen coins and infinitely many 500-yen coins.

Determine if he can pay exactly N yen using only these coins.

Constraints

N is an integer between 1 and 10000 (inclusive).

A is an integer between 0 and 1000 (inclusive).

Input

Input is given from Standard Input in the following format:

N
A

Output

If E869120 can pay exactly N yen using only his 1-yen and 500-yen coins, print Yes; otherwise, print No.

Sample Input 1

2018
218

Sample Output 1

Yes

We can pay 2018 yen with four 500-yen coins and 18 1-yen coins, so the answer is Yes.

Sample Input 2

2763
0

Sample Output 2

No

When we have no 1-yen coins, we can only pay a multiple of 500 yen using only 500-yen coins. Since 2763 is not a multiple of 500, we cannot pay this amount.

Sample Input 3

37
514

Sample Output 3

Yes


let n = read_int ()
let a = read_int ()
let _ = (if n mod 500 > a then "No" else "Yes") |> print_endline


open Printf
open Scanf

let solve n a =
  let r = n mod 500 in
  if r <= a then "Yes" else "No"

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


let n = Scanf.scanf " %d" (+) 0
let a_s = Array.init n @@ fun _ -> Scanf.scanf " %d" (+) 0
let ss = Array.make 2 0
let _ = Array.sort (fun x y -> y - x) a_s;
  Array.iteri (fun i a -> ss.(i mod 2) <- ss.(i mod 2) + a) a_s;
  Printf.printf "%d\n" @@ ss.(0) - ss.(1)

Score: 200 points

Problem Statement

We have N cards. A number a_i is written on the i-th card.

Alice and Bob will play a game using these cards. In this game, Alice and Bob alternately take one card. Alice goes first.

The game ends when all the cards are taken by the two players, and the score of each player is the sum of the numbers written on the cards he/she has taken. When both players take the optimal strategy to maximize their scores, find Alice's score minus Bob's score.

Constraints

N is an integer between 1 and 100 (inclusive).

a_i \ (1 \leq i \leq N) is an integer between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 a_3 ... a_N

Output

Print Alice's score minus Bob's score when both players take the optimal strategy to maximize their scores.

Sample Input 1

2
3 1

Sample Output 1

2

First, Alice will take the card with 3. Then, Bob will take the card with 1.
The difference of their scores will be 3 - 1 = 2.

Sample Input 2

3
2 7 4

Sample Output 2

5

First, Alice will take the card with 7. Then, Bob will take the card with 4. Lastly, Alice will take the card with 2. The difference of their scores will be 7 - 4 + 2 = 5. The difference of their scores will be 3 - 1 = 2.

Sample Input 3

4
20 18 2 18

Sample Output 3

18


(* O(h w) *)
let h, w = Scanf.scanf " %d %d" @@ fun a b -> a, b
let b = ref 0
let sss = Array.init h @@ fun _ ->
  Array.init w @@ fun _ -> Scanf.scanf " %c" @@ fun c -> if c = '#' then incr b; c
let ds = Array.make_matrix h w @@ -1
let _ =
  let q = Queue.create () in
  ds.(0).(0) <- 0;
  Queue.push (0, 0) q;
  while not @@ Queue.is_empty q do
    let y0, x0 = Queue.pop q in
    let f (dy, dx) =
      let y, x = y0 + dy, x0 + dx in
      if 0 <= y && y < h && 0 <= x && x < w && ds.(y).(x) = -1 && sss.(y).(x) <> '#' then
        (ds.(y).(x) <- ds.(y0).(x0) + 1;
        Queue.push (y, x) q) in
    List.iter f [0, -1; -1, 0; 0, 1; 1, 0]
  done;
  let g = ds.(h - 1).(w - 1) in
  Printf.printf "%d\n" @@ if g = -1 then -1 else h * w - !b - g - 1

Score: 400 points

Problem statement

We have an H \times W grid whose squares are painted black or white. The square at the i-th row from the top and the j-th column from the left is denoted as (i, j).

Snuke would like to play the following game on this grid. At the beginning of the game, there is a character called Kenus at square (1, 1). The player repeatedly moves Kenus up, down, left or right by one square. The game is completed when Kenus reaches square (H, W) passing only white squares.

Before Snuke starts the game, he can change the color of some of the white squares to black. However, he cannot change the color of square (1, 1) and (H, W). Also, changes of color must all be carried out before the beginning of the game.

When the game is completed, Snuke's score will be the number of times he changed the color of a square before the beginning of the game. Find the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed, that is, Kenus can never reach square (H, W) regardless of how Snuke changes the color of the squares.

The color of the squares are given to you as characters s_{i, j}. If square (i, j) is initially painted by white, s_{i, j} is .; if square (i, j) is initially painted by black, s_{i, j} is #.

Constraints

H is an integer between 2 and 50 (inclusive).

W is an integer between 2 and 50 (inclusive).

s_{i, j} is . or # (1 \leq i \leq H, 1 \leq j \leq W).

s_{1, 1} and s_{H, W} are ..

Input

Input is given from Standard Input in the following format:

H W
s_{1, 1}s_{1, 2}s_{1, 3} ... s_{1, W}
s_{2, 1}s_{2, 2}s_{2, 3} ... s_{2, W}
: :
s_{H, 1}s_{H, 2}s_{H, 3} ... s_{H, W}

Output

Print the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed.

Sample Input 1

3 3
..#
#..
...

Sample Output 1

2

The score 2 can be achieved by changing the color of squares as follows:

Sample Input 2

10 37
.....................................
...#...####...####..###...###...###..
..#.#..#...#.##....#...#.#...#.#...#.
..#.#..#...#.#.....#...#.#...#.#...#.
.#...#.#..##.#.....#...#.#.###.#.###.
.#####.####..#.....#...#..##....##...
.#...#.#...#.#.....#...#.#...#.#...#.
.#...#.#...#.##....#...#.#...#.#...#.
.#...#.####...####..###...###...###..
.....................................

Sample Output 2

209


Scanf.scanf "%d %d" (fun x y ->
    Printf.printf "%d\n" @@ if x mod y = 0 then -1 else x
)

Score : 100 points

Problem Statement

You are given positive integers X and Y.
If there exists a positive integer not greater than 10^{18} that is a multiple of X but not a multiple of Y, choose one such integer and print it.
If it does not exist, print -1.

Constraints

1 ≤ X,Y ≤ 10^9

X and Y are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print a positive integer not greater than 10^{18} that is a multiple of X but not a multiple of Y, or print -1 if it does not exist.

Sample Input 1

8 6

Sample Output 1

16

For example, 16 is a multiple of 8 but not a multiple of 6.

Sample Input 2

3 3

Sample Output 2

-1

A multiple of 3 is a multiple of 3.


open Batteries;;

let () =
  let _ = read_line () in
  let la = read_line () |> String.split_on_char ' ' |> List.map int_of_string in
  let lb = read_line () |> String.split_on_char ' ' |> List.map int_of_string in
  let rec diff la lb =
    match la, lb with
    | [], [] -> []
    | hda :: resta, hdb :: restb ->
       (hda - hdb) :: diff resta restb
    | _ -> []
  in
  let l = diff la lb in
  let sum = List.fold_left (+) 0 l in
  let ans = if sum > 0 then "No"
            else "Yes" in
  (* let rec check l ac bc  = *)
  (*   match l with *)
  (*   | [] -> ac,bc *)
  (*   | e :: rest -> *)
  (*      if e<0 then check rest ac (bc+e) *)
  (*      else check rest (ac+e) bc *)
  (* in *)
  (* let b,a= check l 0 0 in *)
  (* let ans = *)
  (*   if a mod b = 0 then "Yes" *)
  (*   else "No" in *)
  print_string ans
;;

Score : 300 points

Problem Statement

You are given two integer sequences of length N: a_1,a_2,..,a_N and b_1,b_2,..,b_N.
Determine if we can repeat the following operation zero or more times so that the sequences a and b become equal.

Operation: Choose two integers i and j (possibly the same) between 1 and N (inclusive), then perform the following two actions simultaneously:

Add 2 to a_i.

Add 1 to b_j.

Constraints

1 ≤ N ≤ 10 000

0 ≤ a_i,b_i ≤ 10^9 (1 ≤ i ≤ N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 .. a_N
b_1 b_2 .. b_N

Output

If we can repeat the operation zero or more times so that the sequences a and b become equal, print Yes; otherwise, print No.

Sample Input 1

3
1 2 3
5 2 2

Sample Output 1

Yes

For example, we can perform three operations as follows to do our job:

First operation: i=1 and j=2. Now we have a = \{3,2,3\}, b = \{5,3,2\}.

Second operation: i=1 and j=2. Now we have a = \{5,2,3\}, b = \{5,4,2\}.

Third operation: i=2 and j=3. Now we have a = \{5,4,3\}, b = \{5,4,3\}.

Sample Input 2

5
3 1 4 1 5
2 7 1 8 2

Sample Output 2

No

Sample Input 3

5
2 7 1 8 2
3 1 4 1 5

Sample Output 3

No


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

Score : 100 points

Problem Statement

You went shopping to buy cakes and donuts with X yen (the currency of Japan).

First, you bought one cake for A yen at a cake shop.
Then, you bought as many donuts as possible for B yen each, at a donut shop.

How much do you have left after shopping?

Constraints

1 \leq A, B \leq 1 000

A + B \leq X \leq 10 000

X, A and B are integers.

Input

Input is given from Standard Input in the following format:

X
A
B

Output

Print the amount you have left after shopping.

Sample Input 1

1234
150
100

Sample Output 1

84

You have 1234 - 150 = 1084 yen left after buying a cake.
With this amount, you can buy 10 donuts, after which you have 84 yen left.

Sample Input 2

1000
108
108

Sample Output 2

28

Sample Input 3

579
123
456

Sample Output 3

0

Sample Input 4

7477
549
593

Sample Output 4

405


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

Score : 300 points

Problem Statement

We have a 2 \times N grid. We will denote the square at the i-th row and j-th column (1 \leq i \leq 2, 1 \leq j \leq N) as (i, j).

You are initially in the top-left square, (1, 1).
You will travel to the bottom-right square, (2, N), by repeatedly moving right or down.

The square (i, j) contains A_{i, j} candies.
You will collect all the candies you visit during the travel.
The top-left and bottom-right squares also contain candies, and you will also collect them.

At most how many candies can you collect when you choose the best way to travel?

Constraints

1 \leq N \leq 100

1 \leq A_{i, j} \leq 100 (1 \leq i \leq 2, 1 \leq j \leq N)

Input

Input is given from Standard Input in the following format:

N
A_{1, 1} A_{1, 2} ... A_{1, N}
A_{2, 1} A_{2, 2} ... A_{2, N}

Output

Print the maximum number of candies that can be collected.

Sample Input 1

5
3 2 2 4 1
1 2 2 2 1

Sample Output 1

14

The number of collected candies will be maximized when you:

move right three times, then move down once, then move right once.

Sample Input 2

4
1 1 1 1
1 1 1 1

Sample Output 2

5

You will always collect the same number of candies, regardless of how you travel.

Sample Input 3

7
3 3 4 5 4 5 3
5 3 4 4 2 3 2

Sample Output 3

29

Sample Input 4

1
2
3

Sample Output 4

5


let get_input () =
    read_line () |> Str.split (Str.regexp " ") |> List.map int_of_string

let () =
    let _ = read_int () in
    let upper = get_input () in
    let lower = get_input () in
    List.fold_left2 (fun (usum, lsum) uelem lelem ->
        (usum + uelem, max (lsum + lelem) (usum + uelem + lelem))
    ) (0, 0) upper lower
    |> (fun (x, y) -> max x y)
    |> string_of_int
    |> print_endline


open Scanf
open Printf

let rec for_iter a b f = if a >= b then () else (f a; for_iter (a+1) b f)
let rec for_iterr a b f = if a <= b then () else (f a; for_iter (a-1) b f)
let rec for_iters a b s f = if (a-b >= 0) <> (s >= 0) then () else (f a; for_iter (a+s) b f)

let read_int () = scanf " %d" (fun x -> x)
let rec read_ints n = if n = 0 then [] else let i = read_int () in i :: read_ints (n-1)
let read_ints_arr n = Array.init n (fun _ -> read_int ())
let read_ints_arr_ref n = Array.init n (fun _ -> ref (read_int ()))

let accum xs =
  let rec f ys ac =
    match ys with
    | [] -> []
    | (y::ys') -> (y+ac) :: f ys' (y+ac)
  in f xs 0

let () =
  let n = read_int () in
  let x0 = read_ints n |> accum in
  let x1 = read_ints n |> List.rev |> accum |> List.rev in
  let ans = List.fold_left2 (fun c a b -> max c (a+b)) 0 x0 x1 in
  printf "%d\n" ans


let cat a b = float_of_string (a ^ b)

let is_square n =
  let root = sqrt n |> floor in n = (root ** 2.)

let p b = (if b then "Yes" else "No") |> print_endline

let () =
  Scanf.scanf "%s %s" cat |> is_square |> p

Score : 200 points

Problem Statement

AtCoDeer the deer has found two positive integers, a and b.
Determine whether the concatenation of a and b in this order is a square number.

Constraints

1 ≤ a,b ≤ 100

a and b are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

If the concatenation of a and b in this order is a square number, print Yes; otherwise, print No.

Sample Input 1

1 21

Sample Output 1

Yes

As 121 = 11 × 11, it is a square number.

Sample Input 2

100 100

Sample Output 2

No

100100 is not a square number.

Sample Input 3

12 10

Sample Output 3

No


open Core

let n = Scanf.scanf "%d" ident

let plans =
  List.init n (fun _ -> Scanf.scanf " %d %d %d" (fun t x y -> (t, x, y)))
  |> List.rev

let reachable np p =
  let nt, nx, ny = np in
  let t, x, y = p in
  let d1 = abs (nx - x) + abs (ny - y) in
  let dt = nt - t in
  if d1 <= dt && d1 mod 2 = dt mod 2 then true else false

let rec solve p = function
  | [] ->
      true
  | np :: rest ->
      if reachable np p then solve np rest else false

let str = if solve (0, 0, 0) plans then "Yes" else "No"

let () = print_endline str

Score : 300 points

Problem Statement

AtCoDeer the deer is going on a trip in a two-dimensional plane.
In his plan, he will depart from point (0, 0) at time 0, then for each i between 1 and N (inclusive), he will visit point (x_i,y_i) at time t_i.

If AtCoDeer is at point (x, y) at time t, he can be at one of the following points at time t+1: (x+1,y), (x-1,y), (x,y+1) and (x,y-1).
Note that he cannot stay at his place.
Determine whether he can carry out his plan.

Constraints

1 ≤ N ≤ 10^5

0 ≤ x_i ≤ 10^5

0 ≤ y_i ≤ 10^5

1 ≤ t_i ≤ 10^5

t_i < t_{i+1} (1 ≤ i ≤ N-1)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
t_1 x_1 y_1
t_2 x_2 y_2
:
t_N x_N y_N

Output

If AtCoDeer can carry out his plan, print Yes; if he cannot, print No.

Sample Input 1

2
3 1 2
6 1 1

Sample Output 1

Yes

For example, he can travel as follows: (0,0), (0,1), (1,1), (1,2), (1,1), (1,0), then (1,1).

Sample Input 2

1
2 100 100

Sample Output 2

No

It is impossible to be at (100,100) two seconds after being at (0,0).

Sample Input 3

2
5 1 1
100 1 1

Sample Output 3

No


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

let ls =
  let rec aux n tmp =
    if n = 0 then List.rev tmp
    else
      let line = Scanf.scanf "%d %d %d\n" (fun t x y -> (t, x, y)) in
      aux (n - 1) (line :: tmp)
  in
  aux n [(0, 0, 0)]

let rec solve = function
  | [] | [_] -> "Yes"
  | pre :: now :: t ->
    let pt, px, py = pre and nt, nx, ny = now in
    let dift = nt - pt and difd = abs (nx - px) + abs (ny - py) in
    if difd <= dift && (dift + difd) mod 2 = 0 then solve (now :: t)
    else "No"

let () = solve ls |> print_endline


let () =
  let q = Scanf.scanf "%d\n" (fun q -> q) in
  let abcds = Array.init q (fun _ -> Scanf.scanf "%d %d %d %d\n" (fun a b c d -> a, b, c, d)) in
  Array.iter (fun (a, b, c, d) ->
    begin match compare a b with
    | -1 ->
        for i = c to d do
          (a + b - i) mod ((b + a) / (a + 1) + 1) < (a + b) / (a + 1)
          |> (function true -> 'B' | false -> 'A')
          |> print_char
        done
    | 0 ->
        for i = c to d do
          i mod 2 = 1
          |> (function true -> 'A' | false -> 'B')
          |> print_char
        done
    | 1 ->
        for i = c to d do
          (i - 1) mod ((a + b) / (b + 1) + 1) < (a + b) / (b + 1)
          |> (function true -> 'A' | false -> 'B')
          |> print_char
        done
    end;
    print_newline ()) abcds

Score : 1100 points

Problem Statement

Let f(A, B), where A and B are positive integers, be the string satisfying the following conditions:

f(A, B) has length A + B;

f(A, B) contains exactly A letters A and exactly B letters B;

The length of the longest substring of f(A, B) consisting of equal letters (ex., AAAAA or BBBB) is as small as possible under the conditions above;

f(A, B) is the lexicographically smallest string satisfying the conditions above.

For example, f(2, 3) = BABAB, and f(6, 4) = AABAABAABB.

Answer Q queries: find the substring of f(A_i, B_i) from position C_i to position D_i (1-based).

Constraints

1 \leq Q \leq 10^3

1 \leq A_i, B_i \leq 5 \times 10^8

1 \leq C_i \leq D_i \leq A_i + B_i

D_i - C_i + 1 \leq 100

All input values are integers.

Partial Score

500 points will be awarded for passing the testset satisfying 1 \leq A_i, B_i \leq 10^3.

Input

Input is given from Standard Input in the following format:

Q
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
:
A_Q B_Q C_Q D_Q

Output

For each query i in order of input, print a line containing the substring of f(A_i, B_i) from position C_i to position D_i (1-based).

Sample Input 1

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

Sample Output 1

BABAB
AABAABAABB
A
BAABA
ABAB


open Printf
open Scanf

let solve s =
  Bytes.set s 3 '8' ; s

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

Score : 100 points

Problem Statement

On some day in January 2018, Takaki is writing a document. The document has a column where the current date is written in yyyy/mm/dd format. For example, January 23, 2018 should be written as 2018/01/23.

After finishing the document, she noticed that she had mistakenly wrote 2017 at the beginning of the date column. Write a program that, when the string that Takaki wrote in the date column, S, is given as input, modifies the first four characters in S to 2018 and prints it.

Constraints

S is a string of length 10.

The first eight characters in S are 2017/01/.

The last two characters in S are digits and represent an integer between 1 and 31 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

Replace the first four characters in S with 2018 and print it.

Sample Input 1

2017/01/07

Sample Output 1

2018/01/07

Sample Input 2

2017/01/31

Sample Output 2

2018/01/31


let () =
  let s = read_line () in
  print_string @@ String.concat "" ["2018"; String.sub s 4 (String.length s - 4); "\n"]


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

Score : 200 points

Problem Statement

An X-layered kagami mochi (X ≥ 1) is a pile of X round mochi (rice cake) stacked vertically where each mochi (except the bottom one) has a smaller diameter than that of the mochi directly below it. For example, if you stack three mochi with diameters of 10, 8 and 6 centimeters from bottom to top in this order, you have a 3-layered kagami mochi; if you put just one mochi, you have a 1-layered kagami mochi.

Lunlun the dachshund has N round mochi, and the diameter of the i-th mochi is d_i centimeters. When we make a kagami mochi using some or all of them, at most how many layers can our kagami mochi have?

Constraints

1 ≤ N ≤ 100

1 ≤ d_i ≤ 100

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
d_1
:
d_N

Output

Print the maximum number of layers in a kagami mochi that can be made.

Sample Input 1

4
10
8
8
6

Sample Output 1

3

If we stack the mochi with diameters of 10, 8 and 6 centimeters from bottom to top in this order, we have a 3-layered kagami mochi, which is the maximum number of layers.

Sample Input 2

3
15
15
15

Sample Output 2

1

When all the mochi have the same diameter, we can only have a 1-layered kagami mochi.

Sample Input 3

7
50
30
50
100
50
80
30

Sample Output 3

4


(* O(n) *)
let list_to_array n a xs =
  let res = Array.make n a in
  List.iteri (fun i x -> res.(i) <- x) xs;
  res

(* O(n * log(n)) (n = |xs|) *)
let unique_info n xs =
  if n <= 0 then
    [], 0
  else
    let sorted_xs = list_to_array n 0 xs in
    Array.sort compare sorted_xs;
    let last = sorted_xs.(n - 1) in
    let sofar, count, _ = Array.fold_right
      (fun x (sofar, count, prev) ->
        if x <> prev then
          x :: sofar, count + 1, x
        else
          sofar, count, x)
      sorted_xs
      ([last], 1, last)
    in
      sofar, count

(* O(n * log(n)) (n = |xs|) *)
let unique_count n xs = snd (unique_info n xs)

(* O(n * log(n)) *)
let solve n ds = unique_count n ds

let fold_int f a n =
  let rec fold f a i n =
    if i >= n then
      a
    else
      fold f (f a i) (i + 1) n
  in
    fold f a 0 n

let read_ints n =
  fold_int
    (fun sofar _ -> read_int () :: sofar)
    []
    n |> List.rev

let _ =
  let n = read_int () in
  let ds = read_ints n in
  solve n ds |> string_of_int |> print_endline


let () =
  Scanf.scanf "%d\n" @@ fun n ->
  Array.init n (fun _ -> Scanf.scanf "%d\n" (fun x -> x))
  |> Array.to_list
  |> List.sort_uniq (-)
  |> List.length
  |> Printf.printf "%d\n"


let m = read_int ()
let () = print_int (48 - m)

Score : 100 points

Problem Statement

How many hours do we have until New Year at M o'clock (24-hour notation) on 30th, December?

Constraints

1≤M≤23

M is an integer.

Input

Input is given from Standard Input in the following format:

M

Output

If we have x hours until New Year at M o'clock on 30th, December, print x.

Sample Input 1

21

Sample Output 1

27

We have 27 hours until New Year at 21 o'clock on 30th, December.

Sample Input 2

12

Sample Output 2

36


let calc h =
    48 - h

let main =
    let x = read_line () |> int_of_string |> calc in
        print_endline (Printf.sprintf "%d" x)


let solve () =
    let m = int_of_string (read_line ())
    in print_int (24 - m + 24); print_newline ()

let _ = solve ()


Scanf.scanf "%d %d %s" (fun a b s ->
    let rec loop i =
        if i = a + b + 1 then true else
        if i = a then (if s.[i] = '-' then loop (i + 1) else false) else
            if s.[i] >= '0' && s.[i] <= '9' then loop (i + 1) else false
    in
    print_endline @@ if loop 0 then "Yes" else "No"
)

Score : 200 points

Problem Statement

The postal code in Atcoder Kingdom is A+B+1 characters long, its (A+1)-th character is a hyphen -, and the other characters are digits from 0 through 9.

You are given a string S. Determine whether it follows the postal code format in Atcoder Kingdom.

Constraints

1≤A,B≤5

|S|=A+B+1

S consists of - and digits from 0 through 9.

Input

Input is given from Standard Input in the following format:

A B
S

Output

Print Yes if S follows the postal code format in AtCoder Kingdom; print No otherwise.

Sample Input 1

3 4
269-6650

Sample Output 1

Yes

The (A+1)-th character of S is -, and the other characters are digits from 0 through 9, so it follows the format.

Sample Input 2

1 1
---

Sample Output 2

No

S contains unnecessary -s other than the (A+1)-th character, so it does not follow the format.

Sample Input 3

1 2
7444

Sample Output 3

No


let q = Scanf.scanf " %d" @@ (+) 0
let rmax = ref 0
let lrs = Array.init q @@ fun _ -> Scanf.scanf " %d %d" @@ fun a b ->
  rmax := max !rmax b; a, b
let is_prime n =
  if n <= 1 then false
  else
    let rec loop i =
      if i * i > n then true
      else if n mod i = 0 then false
      else loop @@ i + 1 in
    loop 2
let sums = Array.make (!rmax + 1) 0
let f n = if n mod 2 = 1 && is_prime n && is_prime @@ (n + 1) / 2 then 1 else 0
let _ =
  for i = 0 to !rmax - 1 do sums.(i + 1) <- sums.(i) + f (i + 1) done;
  let f (l, r) =
    Printf.printf "%d\n" @@ sums.(r) - sums.(l - 1)
  in
  Array.iter f lrs

Score : 400 points

Problem Statement

We say that a odd number N is similar to 2017 when both N and (N+1)/2 are prime.

You are given Q queries.

In the i-th query, given two odd numbers l_i and r_i, find the number of odd numbers x similar to 2017 such that l_i ≤ x ≤ r_i.

Constraints

1≤Q≤10^5

1≤l_i≤r_i≤10^5

l_i and r_i are odd.

All input values are integers.

Input

Input is given from Standard Input in the following format:

Q
l_1 r_1
:
l_Q r_Q

Output

Print Q lines. The i-th line (1≤i≤Q) should contain the response to the i-th query.

Sample Input 1

1
3 7

Sample Output 1

2

3 is similar to 2017, since both 3 and (3+1)/2=2 are prime.

5 is similar to 2017, since both 5 and (5+1)/2=3 are prime.

7 is not similar to 2017, since (7+1)/2=4 is not prime, although 7 is prime.

Thus, the response to the first query should be 2.

Sample Input 2

4
13 13
7 11
7 11
2017 2017

Sample Output 2

1
0
0
1

Note that 2017 is also similar to 2017.

Sample Input 3

6
1 53
13 91
37 55
19 51
73 91
13 49

Sample Output 3

4
4
1
1
1
2


let l, r = Scanf.scanf " %d %d %d %d" @@ fun a b c d -> a + b, c + d
let _ = print_endline @@ if l > r then "Left" else if l = r then "Balanced" else "Right"

Score : 100 points

Problem Statement

A balance scale tips to the left if L>R, where L is the total weight of the masses on the left pan and R is the total weight of the masses on the right pan. Similarly, it balances if L=R, and tips to the right if L<R.

Takahashi placed a mass of weight A and a mass of weight B on the left pan of a balance scale, and placed a mass of weight C and a mass of weight D on the right pan.

Print Left if the balance scale tips to the left; print Balanced if it balances; print Right if it tips to the right.

Constraints

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

All input values are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print Left if the balance scale tips to the left; print Balanced if it balances; print Right if it tips to the right.

Sample Input 1

3 8 7 1

Sample Output 1

Left

The total weight of the masses on the left pan is 11, and the total weight of the masses on the right pan is 8. Since 11>8, we should print Left.

Sample Input 2

3 4 5 2

Sample Output 2

Balanced

The total weight of the masses on the left pan is 7, and the total weight of the masses on the right pan is 7. Since 7=7, we should print Balanced.

Sample Input 3

1 7 6 4

Sample Output 3

Right

The total weight of the masses on the left pan is 8, and the total weight of the masses on the right pan is 10. Since 8<10, we should print Right.


let (a,b,c,d) = Scanf.scanf "%d %d %d %d\n" (fun x y z w -> (x,y,z,w))

let () = let tmp = a + b - c - d in
         if tmp > 0 then print_endline "Left"
         else if tmp = 0 then print_endline "Balanced"
         else print_endline "Right"


let solve x y =
  let rec search n last =
    if last <= y then search (n + 1) (2 * last)
    else n
  in search 0 x

let () = Scanf.scanf "%d %d" solve |> print_int

Score : 300 points

Problem Statement

As a token of his gratitude, Takahashi has decided to give his mother an integer sequence.
The sequence A needs to satisfy the conditions below:

A consists of integers between X and Y (inclusive).

For each 1\leq i \leq |A|-1, A_{i+1} is a multiple of A_i and strictly greater than A_i.

Find the maximum possible length of the sequence.

Constraints

1 \leq X \leq Y \leq 10^{18}

All input values are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print the maximum possible length of the sequence.

Sample Input 1

3 20

Sample Output 1

3

The sequence 3,6,18 satisfies the conditions.

Sample Input 2

25 100

Sample Output 2

3

Sample Input 3

314159265 358979323846264338

Sample Output 3

31


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

let pr x = print_endline (string_of_int x)

let z = (log (float_of_int y)) -. (log (float_of_int x))

let w = (z /. (log 2.0)) +. 1.

let () = w
         |> int_of_float
         |> pr


open Scanf
open Printf

let id x = x

let rec for_iter a b f = if a >= b then [] else let x = f a in x :: for_iter (a+1) b f

let () =
  let s = scanf " %s" id in
  let s_len = String.length s in
  for_iter 0 (s_len-1) (fun i ->
    if s.[i] <> s.[i+1] then max (i+1) (s_len-1-i)
    else s_len)
  |> List.fold_left min s_len
  |> printf "%d\n"

Score : 500 points

Problem Statement

You are given a string S consisting of 0 and 1.
Find the maximum integer K not greater than |S| such that we can turn all the characters of S into 0 by repeating the following operation some number of times.

Choose a contiguous segment [l,r] in S whose length is at least K (that is, r-l+1\geq K must be satisfied). For each integer i such that l\leq i\leq r, do the following: if S_i is 0, replace it with 1; if S_i is 1, replace it with 0.

Constraints

1\leq |S|\leq 10^5

S_i(1\leq i\leq N) is either 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum integer K such that we can turn all the characters of S into 0 by repeating the operation some number of times.

Sample Input 1

010

Sample Output 1

2

We can turn all the characters of S into 0 by the following operations:

Perform the operation on the segment S[1,3] with length 3. S is now 101.

Perform the operation on the segment S[1,2] with length 2. S is now 011.

Perform the operation on the segment S[2,3] with length 2. S is now 000.

Sample Input 2

100000000

Sample Output 2

8

Sample Input 3

00001111

Sample Output 3

4


let pr x = print_endline (string_of_int x)

let explode s =
  let rec exp i l =
    if i < 0 then l else exp (i - 1) (s.[i] :: l) in
  exp (String.length s - 1) []

let solve seq =
  let len = List.length seq in
  let (_,_,res) = List.fold_left (fun (cnt,pre,mv) x ->
                      if x != pre then
                        (cnt+1,x,min mv (max cnt (len - cnt)))
                      else (cnt+1,x,mv)) (0,List.hd seq,len) seq in res

let () = input_line stdin
         |> explode
         |> solve
         |> pr


let () = Scanf.scanf "%d %d" (fun a b -> (a + b) / 2 ) |> Printf.printf "%d\n"

Score : 100 points

Problem Statement

You are given two positive integers a and b.
Let x be the average of a and b.
Print x rounded up to the nearest integer.

Constraints

a and b are integers.

1 \leq a, b \leq 100

Input

Input is given from Standard Input in the following format:

a b

Output

Print x rounded up to the nearest integer.

Sample Input 1

1 3

Sample Output 1

2

The average of 1 and 3 is 2.0, and it will be rounded up to the nearest integer, 2.

Sample Input 2

7 4

Sample Output 2

6

The average of 7 and 4 is 5.5, and it will be rounded up to the nearest integer, 6.

Sample Input 3

5 5

Sample Output 3

5


let f a b = int_of_float ((a +. b) /. 2.0);;
Scanf.scanf "%f %f" f
|> Printf.printf "%d\n"


let s = read_line () in
let t = read_line () in
let sa = Array.init (String.length s) (fun i -> s.[i]) in
let ta = Array.init (String.length t) (fun i -> t.[i]) in
let () = Array.sort (fun a b -> compare a b) sa in
let () = Array.sort (fun a b -> compare b a) ta in
let s = String.init (String.length s) (fun i -> sa.(i)) in
let t = String.init (String.length t) (fun i -> ta.(i)) in
print_endline @@ if s < t then "Yes" else "No"

Score : 200 points

Problem Statement

You are given strings s and t, consisting of lowercase English letters.
You will create a string s' by freely rearranging the characters in s.
You will also create a string t' by freely rearranging the characters in t.
Determine whether it is possible to satisfy s' < t' for the lexicographic order.

Notes

For a string a = a_1 a_2 ... a_N of length N and a string b = b_1 b_2 ... b_M of length M, we say a < b for the lexicographic order if either one of the following two conditions holds true:

N < M and a_1 = b_1, a_2 = b_2, ..., a_N = b_N.

There exists i (1 \leq i \leq N, M) such that a_1 = b_1, a_2 = b_2, ..., a_{i - 1} = b_{i - 1} and a_i < b_i. Here, letters are compared using alphabetical order.

For example, xy < xya and atcoder < atlas.

Constraints

The lengths of s and t are between 1 and 100 (inclusive).

s and t consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

If it is possible to satisfy s' < t', print Yes; if it is not, print No.

Sample Input 1

yx
axy

Sample Output 1

Yes

We can, for example, rearrange yx into xy and axy into yxa. Then, xy < yxa.

Sample Input 2

ratcode
atlas

Sample Output 2

Yes

We can, for example, rearrange ratcode into acdeort and atlas into tslaa. Then, acdeort < tslaa.

Sample Input 3

cd
abc

Sample Output 3

No

No matter how we rearrange cd and abc, we cannot achieve our objective.

Sample Input 4

w
ww

Sample Output 4

Yes

Sample Input 5

zzz
zzz

Sample Output 5

No


let array_of_string str = Array.init (String.length str) (fun i -> str.[i])
let string_of_array arr = String.init (Array.length arr) (fun i -> arr.(i))

let () =
  let s = read_line () |> array_of_string in
  let t = read_line () |> array_of_string in
  Array.sort (fun x y -> int_of_char x - int_of_char y) s;
  Array.sort (fun x y -> int_of_char y - int_of_char x) t;
  print_endline @@
    if string_of_array s < string_of_array t then "Yes" else "No"


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

Score : 300 points

Problem Statement

You are given a sequence of positive integers of length N, a = (a_1, a_2, ..., a_N).
Your objective is to remove some of the elements in a so that a will be a good sequence.

Here, an sequence b is a good sequence when the following condition holds true:

For each element x in b, the value x occurs exactly x times in b.

For example, (3, 3, 3), (4, 2, 4, 1, 4, 2, 4) and () (an empty sequence) are good sequences, while (3, 3, 3, 3) and (2, 4, 1, 4, 2) are not.

Find the minimum number of elements that needs to be removed so that a will be a good sequence.

Constraints

1 \leq N \leq 10^5

a_i is an integer.

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of elements that needs to be removed so that a will be a good sequence.

Sample Input 1

4
3 3 3 3

Sample Output 1

1

We can, for example, remove one occurrence of 3. Then, (3, 3, 3) is a good sequence.

Sample Input 2

5
2 4 1 4 2

Sample Output 2

2

We can, for example, remove two occurrences of 4. Then, (2, 1, 2) is a good sequence.

Sample Input 3

6
1 2 2 3 3 3

Sample Output 3

0

Sample Input 4

1
1000000000

Sample Output 4

1

Remove one occurrence of 10^9. Then, () is a good sequence.

Sample Input 5

8
2 7 1 8 2 8 1 8

Sample Output 5

5


Scanf.scanf "%s %d %d" (fun s x y ->
    let len = String.length s in
    let rec loop i dir first cur xl yl =
        if i = len then (
            if cur > 0 then (
                if first < 0 then cur, xl, yl else
                if dir then first, (cur :: xl), yl
                       else first, xl, (cur :: yl)
            ) else first, xl, yl
        ) else (
            if s.[i] = 'F' then loop (i + 1) dir first (cur + 1) xl yl else
                let first, xl, yl =
                    if cur > 0 then (
                        if first < 0 then cur, xl, yl else
                        if dir then first, (cur :: xl), yl
                               else first, xl, (cur :: yl)
                    ) else max first cur, xl, yl
                in
                loop (i + 1) (not dir) first 0 xl yl
        )
    in
    let fx, xl, yl = loop 0 true (-1) 0 [] [] in
    let judge p l =
        let a = Array.make 8192 false in
        a.(0) <- true;
        List.iter (fun v ->
            for j = 8191 downto v do
                a.(j) <- a.(j) || a.(j - v)
            done
        ) l;
        let sum = List.fold_left (+) 0 l in
        let rec loop i =
            if i = 8192 then false else
                if a.(i) && abs (sum - i * 2) = abs p then true else loop (i + 1)
        in
        loop 0
    in
    print_endline @@ if judge (x - fx) xl && judge y yl then "Yes" else "No"
)

Score : 500 points

Problem Statement

A robot is put at the origin in a two-dimensional plane.
Initially, the robot is facing in the positive x-axis direction.

This robot will be given an instruction sequence s.
s consists of the following two kinds of letters, and will be executed in order from front to back.

F : Move in the current direction by distance 1.

T : Turn 90 degrees, either clockwise or counterclockwise.

The objective of the robot is to be at coordinates (x, y) after all the instructions are executed.
Determine whether this objective is achievable.

Constraints

s consists of F and T.

1 \leq |s| \leq 8 000

x and y are integers.

|x|, |y| \leq |s|

Input

Input is given from Standard Input in the following format:

s
x y

Output

If the objective is achievable, print Yes; if it is not, print No.

Sample Input 1

FTFFTFFF
4 2

Sample Output 1

Yes

The objective can be achieved by, for example, turning counterclockwise in the first T and turning clockwise in the second T.

Sample Input 2

FTFFTFFF
-2 -2

Sample Output 2

Yes

The objective can be achieved by, for example, turning clockwise in the first T and turning clockwise in the second T.

Sample Input 3

FF
1 0

Sample Output 3

No

Sample Input 4

TF
1 0

Sample Output 4

No

Sample Input 5

FFTTFF
0 0

Sample Output 5

Yes

The objective can be achieved by, for example, turning counterclockwise in the first T and turning counterclockwise in the second T.

Sample Input 6

TTTT
1 0

Sample Output 6

No


open Batteries

type rot_t = Left | Down | Right | Up

let rotate_left = function
  | Left -> Down
  | Down -> Right
  | Right -> Up
  | Up -> Left

let rotate_right = function
  | Left -> Up
  | Up -> Right
  | Right -> Down
  | Down -> Left

let move (x, y) = function
  | Left -> (x-1, y)
  | Down -> (x, y-1)
  | Right -> (x+1, y)
  | Up -> (x, y+1)

let solve s x y =
  let instrs = String.to_list s in
  let rec go (ix, iy) rot m is =
    if (abs (x - ix) + abs (y - iy)) > m then false
    else match is with
      | [] -> (ix = x) && (iy = y)
      | 'F' :: rest -> go (move (ix, iy) rot) rot (m-1) rest
      | 'T' :: rest ->
        (go (ix, iy) (rotate_left rot) (m-1) rest)
        || (go (ix, iy) (rotate_right rot) (m-1) rest)
  in
  print_string @@ if go (0, 0) Right (String.length s) instrs then "Yes" else "No"

let () = Scanf.scanf "%s\n%d %d" solve


(* O(n * log(max a_s)) *)
let _ = Scanf.scanf "%d" @@ fun n ->
  let a_s = Array.init n @@ fun _ -> Scanf.scanf " %d" @@ (+) 0 in
  let rec count m =
    for i = 0 to n - 1 do
      if a_s.(i) mod 2 = 1 then (Printf.printf "%d\n" m; exit 0);
      a_s.(i) <- a_s.(i) / 2
    done;
    count (m + 1) in
  count 0 |> Printf.printf "%d\n"

Score : 200 points

Problem Statement

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

Snuke can perform the following operation when all integers on the blackboard are even:

Replace each integer X on the blackboard by X divided by 2.

Find the maximum possible number of operations that Snuke can perform.

Constraints

1 \leq N \leq 200

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible number of operations that Snuke can perform.

Sample Input 1

3
8 12 40

Sample Output 1

2

Initially, [8, 12, 40] are written on the blackboard.
Since all those integers are even, Snuke can perform the operation.

After the operation is performed once, [4, 6, 20] are written on the blackboard.
Since all those integers are again even, he can perform the operation.

After the operation is performed twice, [2, 3, 10] are written on the blackboard.
Now, there is an odd number 3 on the blackboard, so he cannot perform the operation any more.

Thus, Snuke can perform the operation at most twice.

Sample Input 2

4
5 6 8 10

Sample Output 2

0

Since there is an odd number 5 on the blackboard already in the beginning, Snuke cannot perform the operation at all.

Sample Input 3

6
382253568 723152896 37802240 379425024 404894720 471526144

Sample Output 3

8


let rec count_factor_2 x =
  if x mod 2 = 0 then 1 + count_factor_2 (x/2)
  else 0

let () =
  Scanf.scanf "%d\n" @@ fun n ->
  Array.init n (fun _ -> Scanf.scanf "%d " count_factor_2)
  |> Array.fold_left min max_int
  |> Printf.printf "%d\n"