let n = Scanf.scanf " %d" (+) 0
let xs = Array.init n @@ fun i -> Scanf.scanf " %d" @@ fun x -> x, i
let rs = Array.make n 0
let _ = Array.(sort compare xs; iteri (fun j (_, i) -> rs.(i) <- j) xs; iter (fun j -> Printf.printf "%d\n" @@ fst @@ if n / 2 - 1 < j then xs.(n / 2 - 1) else xs.(n / 2)) rs)

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 s = Array.init 3 @@ fun _ -> Scanf.scanf " %c" @@ fun c -> c
let _ =
  Array.sort compare s;
  print_endline @@ if s = [|'a'; 'b'; 'c'|] then "Yes" else "No"

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

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

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 a, b, k = Scanf.scanf " %d %d %d" @@ fun a b c -> a, b, c
let l, r = max a @@ b - k + 1, min b @@ a + k - 1
let _ =
  if l <= r then for i = a to b do Printf.printf "%d\n" i done
  else
    (for i = a to r do Printf.printf "%d\n" i done;
    for i = l to b do Printf.printf "%d\n" i done)

Score : 200 points

Problem Statement

Print all the integers that satisfies the following in ascending order:

Among the integers between A and B (inclusive), it is either within the K smallest integers or within the K largest integers.

Constraints

1 \leq A \leq B \leq 10^9

1 \leq K \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B K

Output

Print all the integers that satisfies the condition above in ascending order.

Sample Input 1

3 8 2

Sample Output 1

3
4
7
8

3 is the first smallest integer among the integers between 3 and 8.

4 is the second smallest integer among the integers between 3 and 8.

7 is the second largest integer among the integers between 3 and 8.

8 is the first largest integer among the integers between 3 and 8.

Sample Input 2

4 8 3

Sample Output 2

4
5
6
7
8

Sample Input 3

2 9 100

Sample Output 3

2
3
4
5
6
7
8
9


let () = Scanf.scanf "%d %d %d" @@ fun a b k ->
  if b - a <= 2 * k - 2 then
    for i = a to b do
      Printf.printf "%d\n" i
    done
  else begin
    for i = a to a + k - 1 do
      Printf.printf "%d\n" i
    done;
    for i = b - k + 1 to b do
      Printf.printf "%d\n" i
    done
  end


(* O(1) *)
let a, b, c = Scanf.scanf " %d %d %d" @@ fun a b c -> a, b, c
let m = max a @@ max b c
let ec = List.filter (fun n -> n mod 2 = 0) [a; b; c] |> List.length
let x = m + if (ec = 1 || ec = 2) && m mod 2 = 2 - ec then 1 else 0
let _ = Printf.printf "%d\n" @@ (3 * x - (a + b + c)) / 2

Score : 300 points

Problem Statement

You are given three integers A, B and C. Find the minimum number of operations required to make A, B and C all equal by repeatedly performing the following two kinds of operations in any order:

Choose two among A, B and C, then increase both by 1.

Choose one among A, B and C, then increase it by 2.

It can be proved that we can always make A, B and C all equal by repeatedly performing these operations.

Constraints

0 \leq A,B,C \leq 50

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the minimum number of operations required to make A, B and C all equal.

Sample Input 1

2 5 4

Sample Output 1

2

We can make A, B and C all equal by the following operations:

Increase A and C by 1. Now, A, B, C are 3, 5, 5, respectively.

Increase A by 2. Now, A, B, C are 5, 5, 5, respectively.

Sample Input 2

2 6 3

Sample Output 2

5

Sample Input 3

31 41 5

Sample Output 3

23


let sqrt_int x = int_of_float (sqrt (float_of_int x))

let () =
  Scanf.scanf "%d" @@ fun q ->
  Array.init q begin fun _ -> Scanf.scanf " %d %d" @@ fun a b ->
    let a, b = min a b, max a b in
    let mul, hmul = a * b - 1, sqrt_int (a * b - 1) in
    let rec search l r =
      if l+1 >= r then l else
      let m = (l + r) / 2 in
      if mul / m > hmul then search m r else search l m
    in
    let c = search a (a*b+1) - a |> max 0 in
    (a-1) + min (b-1) (if hmul <> 0 && mul/hmul = a then hmul-1 else hmul) + c
  end |> Array.iter (Printf.printf "%d\n")

Score : 700 points

Problem Statement

10^{10^{10}} participants, including Takahashi, competed in two programming contests.
In each contest, all participants had distinct ranks from first through 10^{10^{10}}-th.

The score of a participant is the product of his/her ranks in the two contests.

Process the following Q queries:

In the i-th query, you are given two positive integers A_i and B_i. Assuming that Takahashi was ranked A_i-th in the first contest and B_i-th in the second contest, find the maximum possible number of participants whose scores are smaller than Takahashi's.

Constraints

1 \leq Q \leq 100

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
A_1 B_1
:
A_Q B_Q

Output

For each query, print the maximum possible number of participants whose scores are smaller than Takahashi's.

Sample Input 1

8
1 4
10 5
3 3
4 11
8 9
22 40
8 36
314159265 358979323

Sample Output 1

1
12
4
11
14
57
31
671644785

Let us denote a participant who was ranked x-th in the first contest and y-th in the second contest as (x,y).

In the first query, (2,1) is a possible candidate of a participant whose score is smaller than Takahashi's. There are never two or more participants whose scores are smaller than Takahashi's, so we should print 1.


let () =
  let n = Scanf.scanf "%d\n" (fun n -> n) in
  let abs = Array.init n (fun _ -> Scanf.scanf "%d %d\n" (fun a b -> a, b)) in
  Printf.printf "%d\n" @@
  if List.for_all (fun (a, b) -> a = b) (Array.to_list abs) then 0
  else
  Array.fold_left (fun acc (a, b) ->
    if b < a then acc + a - b
    else acc + a) 0 abs

Score : 700 points

Problem Statement

You are given sequences A and B consisting of non-negative integers.
The lengths of both A and B are N, and the sums of the elements in A and B are equal.
The i-th element in A is A_i, and the i-th element in B is B_i.

Tozan and Gezan repeats the following sequence of operations:

If A and B are equal sequences, terminate the process.

Otherwise, first Tozan chooses a positive element in A and decrease it by 1.

Then, Gezan chooses a positive element in B and decrease it by 1.

Then, give one candy to Takahashi, their pet.

Tozan wants the number of candies given to Takahashi until the process is terminated to be as large as possible, while Gezan wants it to be as small as possible.
Find the number of candies given to Takahashi when both of them perform the operations optimally.

Constraints

1 \leq N \leq 2 × 10^5

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

The sums of the elements in A and B are equal.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print the number of candies given to Takahashi when both Tozan and Gezan perform the operations optimally.

Sample Input 1

2
1 2
3 2

Sample Output 1

2

When both Tozan and Gezan perform the operations optimally, the process will proceed as follows:

Tozan decreases A_1 by 1.

Gezan decreases B_1 by 1.

One candy is given to Takahashi.

Tozan decreases A_2 by 1.

Gezan decreases B_1 by 1.

One candy is given to Takahashi.

As A and B are equal, the process is terminated.

Sample Input 2

3
8 3
0 1
4 8

Sample Output 2

9

Sample Input 3

1
1 1

Sample Output 3

0


let s, f = read_line (), Printf.printf "%s%c\n"
let n = String.length s
let _ = String.(if n < 26 then Char.(for i = code 'a' to code 'z' do try index s (chr i) |> ignore with _ -> f s (chr i); exit 0 done) else let j = ref ~-1 in iteri (fun i c -> if i > 0 && s.[i - 1] < c then j := i - 1) s; if !j < 0 then print_endline "-1" else let m = ref 'z' in for i = !j + 1 to n - 1 do if s.[i] > s.[!j] then m := min !m s.[i] done; f (sub s 0 !j) !m)

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 s = read_line ()
let n, fs, i = Array.(String.length s, make 128 false, ref 97)
let f s n = Printf.printf "%s%c\n" s (Char.chr n); exit 0
let _ = String.(Char.(iter (fun c -> fs.(code c) <- true) s; if n < 26 then (while fs.(!i) do incr i done; f s !i);
  for j = n - 1 downto 0 do i := code s.[j]; while !i < 123 && fs.(!i) do incr i done; if !i < 123 then f (sub s 0 j) !i; fs.(code s.[j]) <- false done; print_endline "-1"))


let s = read_line ()
let n, fs, is, i = Array.(String.length s, make 26 false, make 26 0, ref 0)
let f s n = Printf.printf "%s%c\n" s (Char.chr (n + 97)); exit 0
let _ = String.(Char.(iter (fun c -> let n = code c - 97 in fs.(n) <- true; is.(!i) <- n; incr i) s;
  if n < 26 then (i := 0; while fs.(!i) do incr i done; f s !i);
  for j = n - 1 downto 0 do i := code s.[j] - 96; while !i < 26 && fs.(!i) do incr i done; if !i < 26 then f (sub s 0 j) !i; fs.(is.(j)) <- false done; print_endline "-1"))


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


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

    let rec check2 prev i sum =
        if i > 30000 then true else
            if (i + sum) mod 2 = 0 &&
               (i + sum) mod 3 = 0 &&
               gcd i sum <> 1 then (Printf.printf "%d %d\n" prev i; false)
            else check2 prev (i + 1) sum
    in

    let rec check i sum =
        if i mod 6 = 5 then check (i + 1) sum else
            if check2 i (i + 1) (sum + i) then check (i + 1) sum
    in

    let rec loop i acc sum =
        if acc = n - 2 then check i sum else
        if i mod 2 = 0 || i mod 3 = 0 then (
            let () = Printf.printf "%d " i in
            let acc = acc + 1 in
            let sum = sum + i in
            loop (i + 1) acc sum
        ) else loop (i + 1) acc sum
    in
    loop 2 0 0
)

Score : 600 points

Problem Statement

Nagase is a top student in high school. One day, she's analyzing some properties of special sets of positive integers.

She thinks that a set S = \{a_{1}, a_{2}, ..., a_{N}\} of distinct positive integers is called special if for all 1 \leq i \leq N, the gcd (greatest common divisor) of a_{i} and the sum of the remaining elements of S is not 1.

Nagase wants to find a special set of size N. However, this task is too easy, so she decided to ramp up the difficulty. Nagase challenges you to find a special set of size N such that the gcd of all elements are 1 and the elements of the set does not exceed 30000.

Constraints

3 \leq N \leq 20000

Input

Input is given from Standard Input in the following format:

N

Output

Output N space-separated integers, denoting the elements of the set S. S must satisfy the following conditions :

The elements must be distinct positive integers not exceeding 30000.

The gcd of all elements of S is 1, i.e. there does not exist an integer d > 1 that divides all elements of S.

S is a special set.

If there are multiple solutions, you may output any of them. The elements of S may be printed in any order. It is guaranteed that at least one solution exist under the given contraints.

Sample Input 1

3

Sample Output 1

2 5 63

\{2, 5, 63\} is special because gcd(2, 5 + 63) = 2, gcd(5, 2 + 63) = 5, gcd(63, 2 + 5) = 7. Also, gcd(2, 5, 63) = 1. Thus, this set satisfies all the criteria.

Note that \{2, 4, 6\} is not a valid solution because gcd(2, 4, 6) = 2 > 1.

Sample Input 2

4

Sample Output 2

2 5 20 63

\{2, 5, 20, 63\} is special because gcd(2, 5 + 20 + 63) = 2, gcd(5, 2 + 20 + 63) = 5, gcd(20, 2 + 5 + 63) = 10, gcd(63, 2 + 5 + 20) = 9. Also, gcd(2, 5, 20, 63) = 1. Thus, this set satisfies all the criteria.


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

let () = Scanf.scanf "%d" @@ function
  | 3 -> print_endline "2 5 63\n"
  | 4 -> print_endline "2 5 20 63\n"
  | 5 -> print_endline "2 5 20 30 63\n"
  | n ->
      let s =
        IntSet.of_list @@
        Array.to_list @@
        Array.init n @@ fun i ->
          i / 4 * 6 + [| 2; 3; 4; 6 |].(i mod 4) in
      let maximum = IntSet.max_elt s in
      IntSet.iter (Printf.printf "%d ")
        begin match IntSet.fold (fun x y -> (x + y) mod 6) s 0 with
        | 0 -> s
        | 2 -> IntSet.add (maximum - maximum mod 6 + 6) @@ IntSet.remove 8 s
        | 3 -> IntSet.add (maximum - maximum mod 6 + 6) @@ IntSet.remove 9 s
        | 5 -> IntSet.add (maximum - maximum mod 6 + if maximum mod 6 < 4 then 4 else 10) @@ IntSet.remove 9 s
        end;
      print_newline ()


let () =
  let rec makelst k x d =
    if k * x + 1 > d then [] else k * x + 1 :: makelst (k+1) x d in
  Scanf.scanf "%d\n%d %d\n" @@ fun n d x ->
  let arr = Array.init n @@ fun _ ->
  	Scanf.scanf "%d\n" @@ fun a -> List.length (makelst 0 a d) in
  let num = Array.fold_left (+) 0 arr in
  Printf.printf "%d\n" (x+num)

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 () =
  let n = Scanf.scanf "%d " (fun a -> a) in
  let arr = Array.init n (fun _ -> Scanf.scanf "%d " (fun a -> a)) in
  let arr = Array.append arr [|0|] in
  let a_l = Array.to_list arr in

  let cum f init l =
    let rec aux prev res = function
      | [] -> res
      | hd :: tl ->
        let acc = f prev hd in
        aux hd (acc::res) tl
    in
    aux init [] l
  in

  let l = cum (fun x y -> abs (y-x)) 0 a_l in
  let ans = List.fold_left (+) 0 l in

  let rec aux res = function
    | [] -> res
    | hd1 :: hd2 :: hd3 :: tl ->
      let acc = abs (hd3-hd1) - (abs (hd3-hd2) + abs(hd2-hd1)) in
      aux (acc::res) (hd2::hd3::tl)
    | _ -> res
  in

  let l = aux [] (0::a_l) in
  let l = List.rev l in
  List.iter (fun x -> Printf.printf "%d\n" @@ ans+x) l;

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\n" (fun a b c -> a, b, c) in
Printf.printf "%s\n" (if a + b >= c then "Yes" else "No")

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


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


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


let rec list_init n f = if n <= 0 then [] else let x = f () in x :: list_init (n-1) f
let rec remove x = function [] -> [] | (y::ys') -> if x = y then ys' else y :: remove x ys'
let step (acc, zs) (x,y) =
  match List.filter (fun (a,b) -> a < x && b < y) zs with
  | []     -> (acc,   zs)
  | (z::_) -> (acc+1, remove z zs)
let () =
  Scanf.scanf "%d" (fun n ->
  let a = list_init n (fun _ -> Scanf.scanf " %d %d" (fun x y -> x, y))
  |> List.sort (fun (_,b) (_,d) -> d - b) in
  list_init n (fun _ -> Scanf.scanf " %d %d" (fun x y -> x, y))
  |> List.sort compare |> List.fold_left step (0, a)) |> fst |> Printf.printf "%d\n"

Score : 400 points

Problem Statement

On a two-dimensional plane, there are N red points and N blue points.
The coordinates of the i-th red point are (a_i, b_i), and the coordinates of the i-th blue point are (c_i, d_i).

A red point and a blue point can form a friendly pair when, the x-coordinate of the red point is smaller than that of the blue point, and the y-coordinate of the red point is also smaller than that of the blue point.

At most how many friendly pairs can you form? Note that a point cannot belong to multiple pairs.

Constraints

All input values are integers.

1 \leq N \leq 100

0 \leq a_i, b_i, c_i, d_i < 2N

a_1, a_2, ..., a_N, c_1, c_2, ..., c_N are all different.

b_1, b_2, ..., b_N, d_1, d_2, ..., d_N are all different.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
a_2 b_2
:
a_N b_N
c_1 d_1
c_2 d_2
:
c_N d_N

Output

Print the maximum number of friendly pairs.

Sample Input 1

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

Sample Output 1

2

For example, you can pair (2, 0) and (4, 2), then (3, 1) and (5, 5).

Sample Input 2

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

Sample Output 2

2

For example, you can pair (0, 0) and (2, 3), then (1, 1) and (3, 4).

Sample Input 3

2
2 2
3 3
0 0
1 1

Sample Output 3

0

It is possible that no pair can be formed.

Sample Input 4

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

Sample Output 4

5

Sample Input 5

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

Sample Output 5

4


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

let rec loop ans reds = function
  | [] -> ans
  | (c, d) :: bs ->
      let ts = List.filter (fun (b, a)-> a < c && b < d) reds in
      if List.length ts > 0 then
        let red = ts |> List.sort compare |> List.rev |> List.hd in
        loop (ans + 1) (List.filter ((<>) red) reds) bs
      else
        loop ans reds bs

let () = Printf.printf "%d\n" @@ loop 0 ab (cd |> List.sort compare)


open Batteries
open Char
let arr = String.explode (read_line ())
let brr = String.explode (read_line ())
let crr = String.explode (read_line ())
let ans = escaped (List.nth arr 0) ^ escaped (List.nth brr 1) ^ escaped (List.nth crr 2)
let () = print_endline ans

Score : 100 points

Problem Statement

We have a 3×3 square grid, where each square contains a lowercase English letters.
The letter in the square at the i-th row from the top and j-th column from the left is c_{ij}.

Print the string of length 3 that can be obtained by concatenating the letters in the squares on the diagonal connecting the top-left and bottom-right corner of the grid, from the top-left to bottom-right.

Constraints

Input consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

c_{11}c_{12}c_{13}
c_{21}c_{22}c_{23}
c_{31}c_{32}c_{33}

Output

Print the string of length 3 that can be obtained by concatenating the letters on the diagonal connecting the top-left and bottom-right corner of the grid, from the top-left to bottom-right.

Sample Input 1

ant
obe
rec

Sample Output 1

abc

The letters in the squares on the diagonal connecting the top-left and bottom-right corner of the grid are a, b and c from top-right to bottom-left. Concatenate these letters and print abc.

Sample Input 2

edu
cat
ion

Sample Output 2

ean


let n, m = Scanf.scanf " %d %d" @@ fun a b -> a, b
let _ = Printf.printf "%d\n" @@ if n * m = 1 then 1 else if n = 1 || m = 1 then n * m - 2 else (n - 2) * (m - 2)

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 n, m = Scanf.scanf "%d %d" (fun x y -> x, y)
let () =
  abs((n * m) - (2 * n) - (2 * m) + 4)
  |> Printf.printf "%d\n"


let n = read_int ()
let () = print_int (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 id x = x

type v = T | F

let rec slv = function
	| 0 -> T
	| n -> if (Scanf.scanf "%c" id) = 'Y' then F else slv (n-1)

let () =
	let n = Scanf.scanf "%d\n" id in
	match slv n with
	| F -> print_string "Four"
	| T -> print_string "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 () =
  let chk = Array.make 4 0 in
  Scanf.scanf "%d" (fun n -> Array.init n (fun _ -> Scanf.scanf " %c" (String.index "PWGY")))
  |> Array.iter (fun x -> chk.(x) <- 1);
  Array.fold_left (+) 0 chk |> (fun x -> if x = 3 then "Three" else "Four")
  |> print_endline


open Scanf
open Printf

let flip f x y = f y x

let rec range a b = if a >= b then [] else a :: range (a+1) b

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 rec for_iter a b f = if a >= b then [] else let x = f a in x :: for_iter (a+1) b f
let rec for_iter_ a b f = if a >= b then () else (f a; for_iter_ (a+1) b f)

let list_of_string str = range 0 (String.length str) |> List.map (fun i -> str.[i])
let array_of_string str = Array.init (String.length str) (fun i -> str.[i])

let z c = if c = 'P' then 0 else if c = 'W' then 1 else if c = 'G' then 2 else 3

let () =
  let chk = Array.make 4 0 in
  scanf "%d" (fun n -> Array.init n (fun _ -> scanf " %c" z))
  |> Array.iter (fun x -> chk.(x) <- 1);
  Array.fold_left (+) 0 chk |> (fun x -> if x <= 3 then "Three" else "Four")
  |> print_endline


let h, w, d, ps, ts = Array.(Scanf.scanf "%d %d %d" @@ fun h w d -> h, w, d, make (h * w) (0, 0), make (h * w) 0)
let lrs = Scanf.(for i = 0 to h - 1 do for j = 0 to w - 1 do ps.(scanf " %d" pred) <- i, j done done; scanf " %d" @@ fun q -> Array.init q @@ fun _ -> scanf " %d %d" @@ fun a b -> a - 1, b - 1)
let f a b = let (i, j), (k, l) = ps.(a), ps.(b) in abs (i - k) + abs (j - l)
let _ = for i = d to h * w - 1 do ts.(i) <- ts.(i - d) + f (i - d) i done; Array.iter (fun (l, r) -> Printf.printf "%d\n" @@ ts.(r) - ts.(l)) lrs

Score : 400 points

Problem Statement

We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).

The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.

You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.

You now have to take Q practical tests of your ability as a magical girl.

The i-th test will be conducted as follows:

Initially, a piece is placed on the square where the integer L_i is written.

Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.

Here, it is guaranteed that R_i-L_i is a multiple of D.

For each test, find the sum of magic points consumed during that test.

Constraints

1 \leq H,W \leq 300

1 \leq D \leq H×W

1 \leq A_{i,j} \leq H×W

A_{i,j} \neq A_{x,y} ((i,j) \neq (x,y))

1 \leq Q \leq 10^5

1 \leq L_i \leq R_i \leq H×W

(R_i-L_i) is a multiple of D.

Input

Input is given from Standard Input in the following format:

H W D
A_{1,1} A_{1,2} ... A_{1,W}
:
A_{H,1} A_{H,2} ... A_{H,W}
Q
L_1 R_1
:
L_Q R_Q

Output

For each test, print the sum of magic points consumed during that test.

Output should be in the order the tests are conducted.

Sample Input 1

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

Sample Output 1

5

4 is written in Square (1,2).

6 is written in Square (3,3).

8 is written in Square (3,1).

Thus, the sum of magic points consumed during the first test is (|3-1|+|3-2|)+(|3-3|+|1-3|)=5.

Sample Input 2

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

Sample Output 2

0
0

Note that there may be a test where the piece is not moved at all, and there may be multiple identical tests.

Sample Input 3

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

Sample Output 3

0
5
0


Printf.printf "%d\n" @@ Scanf.scanf "%s" @@ fun s ->
  (int_of_char s.[0] - 49) + (9 * (String.length s - 1))

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 init = function
  | [] -> raise (Failure "init")
  | x :: xs ->
      List.rev @@
      fst @@
      List.fold_left (fun (tr, x) y -> (x :: tr, y)) ([], x) xs

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 rec drop_until_ccw x y = 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_until_ccw x y rest in
  let convex =
    Array.of_list @@
    init @@
    let h, t =
      match
        Array.fold_left (fun stack (x, y, i) ->
          (x, y, i) :: drop_until_ccw x y stack) [] xys
      with
      | [] -> [], []
      | h :: t -> [h], t in
    Array.fold_right (fun (x, y, i) stack ->
      (x, y, i) :: drop_until_ccw x y stack) (Array.sub xys 0 (n - 1)) h @ t 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 pi x = 2.0 *. acos 0.0 *. x
let () =
  Scanf.scanf "%d" @@ fun n ->
  let a = Array.init n (fun i -> Scanf.scanf " %f %f" (fun x y -> i,x,y)) in
  Array.init n (fun i ->
    let _, x, y = a.(i) in
    let f o pr = Array.fold_left (fun (l, u) (j, x', y') ->
      if i = j then l, u else
      let rad = mod_float (o +. pi 1.0 +. atan2 (y' -. y) (x' -. x)) (pi 2.0) in
      max l (rad -. pi 0.5), min u (rad +. pi 0.5)
    ) pr a in
    let l0, u0 = f 0.0 (pi 0.5, pi 1.5) in
    let l1, u1 = f (pi 1.0) (pi 0.5, pi 1.5) in
    ( max 0.0 (u0-.l0) +. max 0.0 (u1-.l1) ) /. pi 2.0
  ) |> Array.iter (Printf.printf "%.10f\n")


open Scanf
open Printf

let coin n a = if n mod 500 <= a then "Yes" else "No"

let a, n = (read_int (), read_int ())
let () = printf "%s\n" @@ coin n a

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 result = if a >= (n mod 500) then "Yes" else "No"

let () = print_string result


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

let _ =
  let n = read_int () in
  let a = read_int () in
  solve n a |> print_endline


(*ABC 088 A *)
let () =
  Scanf.scanf "%d" (fun n ->
      Scanf.scanf " %d" (fun a ->
          match n mod 500 <= a with true -> "Yes" | false -> "No" )
      |> print_endline )


let () = Scanf.scanf "%d %d" (fun n a -> if n mod 500 <= a then "Yes" else "No") |> print_endline


open Scanf
open Printf

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

let () =
  let n = read_int ()
  and a = read_int () in
  printf (if n mod 500 <= a then "Yes\n" else "No\n")


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

let is_const_list l =
	match l with
		| [] -> false
		| e :: es -> List.for_all ((=) e) es

let diff l1 l2 = List.map2 (-) l1 l2

let () =
	let row1 = read_int_list () in
	let row2 = read_int_list () in
	let row3 = read_int_list () in
	if is_const_list (diff row1 row2) && is_const_list (diff row2 row3) then
		print_endline "Yes"
	else
		print_endline "No"

Score: 300 points

Problem Statement

We have a 3 \times 3 grid. A number c_{i, j} is written in the square (i, j), where (i, j) denotes the square at the i-th row from the top and the j-th column from the left.

According to Takahashi, there are six integers a_1, a_2, a_3, b_1, b_2, b_3 whose values are fixed, and the number written in the square (i, j) is equal to a_i + b_j.

Determine if he is correct.

Constraints

c_{i, j} \ (1 \leq i \leq 3, 1 \leq j \leq 3) is an integer between 0 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

c_{1,1} c_{1,2} c_{1,3}
c_{2,1} c_{2,2} c_{2,3}
c_{3,1} c_{3,2} c_{3,3}

Output

If Takahashi's statement is correct, print Yes; otherwise, print No.

Sample Input 1

1 0 1
2 1 2
1 0 1

Sample Output 1

Yes

Takahashi is correct, since there are possible sets of integers such as: a_1=0,a_2=1,a_3=0,b_1=1,b_2=0,b_3=1.

Sample Input 2

2 2 2
2 1 2
2 2 2

Sample Output 2

No

Takahashi is incorrect in this case.

Sample Input 3

0 8 8
0 8 8
0 8 8

Sample Output 3

Yes

Sample Input 4

1 8 6
2 9 7
0 7 7

Sample Output 4

No


open Scanf
open Printf
open List

let step = [(1, 0); (0, 1); (-1, 0); (0, -1)]

let flip f x y = f y x

let inf = 101010101010

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

let rec read_ints n =
  if n = 0 then []
  else
    let a = scanf " %d" (fun x -> x) in
    a :: read_ints (n-1)

let () =
  let (h :: w :: _) = read_ints 2 in
  let num_white = ref 0 in
  let field = Array.init h (fun _ ->
    let row = scanf " %s" (fun s -> s) in
    flip String.iter row (
      fun c -> if c = '.' then num_white := !num_white + 1 else ()
    );
    row) in

  let min_dist = Array.init h (fun _ -> Array.init w (fun _ -> ref inf)) in
  min_dist.(0).(0) := 0;

  let queue = Queue.create () in
  Queue.push (0, 0) queue;
  while not (Queue.is_empty queue) do
    let x, y = Queue.pop queue in
    flip List.iter step (fun (dx, dy) ->
      let nx, ny = x + dx, y + dy in
      if nx < 0 || h <= nx || ny < 0 || w <= ny || String.get (field.(nx)) ny = '#'
      then ()
      else if !(min_dist.(nx).(ny)) = inf then (
        min_dist.(nx).(ny) := !(min_dist.(x).(y)) + 1;
        Queue.push (nx, ny) queue
      )
    )
  done;
  let ans = !(min_dist.(h-1).(w-1)) in
  printf "%d\n" (if ans = inf then -1 else !num_white - ans - 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


let () =
  let (x,y) = Scanf.scanf "%d %d\n" (fun x y -> x,y) in
  let y'=int_of_float (sqrt (float_of_int y)) in
  let rec min_yp i =
    if i > x || i > y' then 2
    else if y mod i <> 0 then i
    else min_yp i+1
  in
  let check  =
    if x mod y = 0 then -1
    else if x>y then x
    else x * (min_yp 2)
  in
  let ans = check in
  print_int ans;

;;

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


open Printf
open Scanf

let solve x a b = (x - a) mod b

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

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 f x a b = (mod) (x-a) b;;
let () = Scanf.scanf "%d %d %d" f
|> Printf.printf "%d\n"


let () =
  let x = Scanf.scanf "%d " (fun a -> a) in
  let a = Scanf.scanf "%d " (fun a -> a) in
  let b = Scanf.scanf "%d " (fun a -> a) in

  Printf.printf "%d\n" @@ (x - a) mod b


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


let a, b = Scanf.scanf " %s %s" @@ fun a b -> a, b
let n = int_of_string @@ a ^ b
let i = ref 1
let _ = while !i * !i <= 100100 do if !i * !i = n then (print_endline "Yes"; exit 0); incr i done; print_endline "No"

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


(* O(n) (n = max {a, b}) *)
Scanf.scanf "%s %s" @@ fun a b ->
  let m = a ^ b |> int_of_string in
  let rec find i =
    if i * i = m then "Yes"
    else if i * i > m then "No"
    else find (i + 1) in
  find 1 |> print_endline


let () = Scanf.scanf "%d\n" @@ fun n ->
  Array.init n (fun _ ->
    Scanf.scanf "%d %d %d\n" @@ fun t x y -> t, x, y)
  |> Array.fold_left (fun (acc, txy') txy -> ((txy', txy) :: acc, txy)) ([], (0, 0, 0))
  |> fst
  |> List.for_all (fun ((t, x, y), (t', x', y')) ->
      let d = abs (x - x') + abs (y - y') in
      let dt = t' - t in
      d <= dt && (d - dt) mod 2 = 0)
  |> (function true -> "Yes" | false -> "No")
  |> print_endline

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 s = read_line ()
let _ = Printf.printf "2018/01/%c%c\n" s.[8] s.[9]

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 input () =
    let n  = int_of_string (read_line ())
    in let rec ds i result = if i >= n
        then result
        else let input = int_of_string (read_line ())
             in  ds (i+1) (input::result)
    in List.rev (ds 0 [])

let remove_duplicate (x::xs) =
    let rec remove_duplicate' prev l =
        match l with
        | []    -> []
        | x::xs -> if x == prev
            then remove_duplicate' x xs
            else x :: (remove_duplicate' x xs)
    in x :: (remove_duplicate' x xs)

let _ = List.sort (Pervasives.compare) (input ())
     |> remove_duplicate
     |> List.fold_left (+) 0
     |> print_int
     |> print_newline

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


Scanf.scanf "%d" (fun m -> Printf.printf "%d\n" @@ 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 m = read_int ()
let _ = (24 - m) + 24 |> Printf.printf "%d\n"


open Int64;;

let n = read_int () in

print_endline(to_string (of_int (48-n)))


let calc_ a b s =
    match BatString.split_on_char '-' s with
    | [x; y] -> String.length x = a && String.length y = b
    | _ -> false

let calc a b s =
    let f v i c = v && (if i = a then c = '-' else c <> '-') in
    BatString.fold_lefti f true s

let main =
    let [a; b] = read_line () |> BatString.split_on_char ' ' |> List.map int_of_string in
    let s = read_line () in
    let v = (calc a b s) in
    print_endline (if v 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


open Scanf
open Printf

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 rec for_all_iter a b f = if a >= b then true else f a && for_all_iter (a+1) b f

let () =
  let a = read_int () in let b = read_int () in
  let s = scanf " %s" (fun s -> s) in
  let check1 = for_all_iter 0 a (fun i -> '0' <= s.[i] && s.[i] <= '9') in
  let check2 = s.[a] = '-' in
  let check3 = for_all_iter (a+1) (a+b+1) (fun i -> '0' <= s.[i] && s.[i] <= '9') in
  printf "%s\n" (if check1 && check2 && check3 then "Yes" else "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 primes n =
  if n <= 1 then [|false; false|]
  else
    let flags = Array.make (n + 1) true in
    flags.(0) <- false; flags.(1) <- false;
    let i = ref 2 in
    while !i * !i <= n do
      if flags.(!i) then
        (let j = ref @@ !i + !i in
        while !j <= n do flags.(!j) <- false; j := !j + !i done);
      incr i
    done;
    flags
let sums = Array.make (!rmax + 1) 0
let ps = primes !rmax
let f n = if n mod 2 = 1 && ps.(n) && ps.((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 g (l, r) = Printf.printf "%d\n" @@ sums.(r) - sums.(l - 1) in
  Array.iter g 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 a, b, c, d = Scanf.scanf "%d %d %d %d\n" @@ fun a b c d -> a, b, c, d
let ans =
	if a + b > c + d then "Left" else if a + b = c + d then "Balanced"
	else "Right"
let () = print_endline ans

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 () =
  let a,b,c,d= Scanf.scanf "%d %d %d %d " (fun a b c d -> a,b,c,d) in
  Printf.printf "%s\n"
    (if a+b>c+d then
       "Left"
     else (
       if a+b=c+d then
         "Balanced"
       else
         "Right"
     )
    );


let solve x y =
  let rec aux x' c = if x' > y then c else aux (x' * 2) (c + 1) in
  aux x 0

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

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


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 (fun i ->
    if s.[i] = '1' then max (min (i+1) (s_len-i)) (max i (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 max (a : int) (b : int) =
  if a > b then a else b

let min (a : int) (b : int) =
  if a < b then a else b

let fold f init s =
  let rec iter i res =
    if i < 0 then res else iter (i - 1) (f res s.[i]) in
  iter (String.length s - 1) init

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

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


open Core

type input = {a: int; b: int}

let i = Scanf.scanf "%d %d" (fun a b -> {a; b})

let mean = (i.a + i.b) / 2 + (i.a + i.b) mod 2

let () = Printf.printf "%d\n" mean

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 () =
  Scanf.scanf "%d %d" (fun a b ->
      let m = (a + b) / 2 in
      (match (a + b) mod 2 with 0 -> m | 1 -> m + 1) |> Printf.printf "%d\n" )


let f g s = let a = String.(Array.init (length s) @@ get s) in Array.(sort g a; String.init (length a) @@ get a)
let s = f compare @@ read_line ()
let t = f (fun x y -> compare y x) @@ read_line ()
let _ = 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 rec chars_of_string = function
  | "" -> []
  | str ->
      String.get str 0 :: chars_of_string (String.sub str 1 (String.length str - 1))

let rec string_of_chars = function
  | [] -> ""
  | c :: tail -> String.make 1 (Char.chr c) ^ string_of_chars tail

(* 比較関数を元に文字列中の文字を並び替える *)
let sort_chars f s =
  string_of_chars @@
    List.sort f @@
      List.map Char.code @@ chars_of_string s

(* 辞書順で最小の文字列に変換 *)
let to_min = sort_chars (-)

(* 辞書順で最大の文字列に変換 *)
let to_max = sort_chars (fun a b -> b - a)

let () =
  let (s, t) = Scanf.scanf "%s\n%s" (fun s t -> (to_min s, to_max t)) in
    print_string @@ if String.compare s t < 0 then "Yes" else "No"


let () =
  let s = read_line () in
  let t = read_line () in
  let sort s f =
    let arr = Array.init (String.length s) (fun i -> String.make 1 s.[i]) in
    Array.sort f arr ;
    Array.fold_left (fun x y -> x ^ y) "" arr
  in
  let s = sort s compare in
  let t = sort t (fun x y -> compare y x) in
  (match s < t with true -> "Yes" | false -> "No") |> print_endline


let id x = x
let n = Scanf.scanf "%d\n" id
let h = Hashtbl.create n
let ans = ref 0

let () =
  for i = 1 to n do
    let a = Scanf.scanf "%d " id in
    let v = try Hashtbl.find h a with _ -> 0 in
    Hashtbl.replace h a (v + 1)
  done ;
  Hashtbl.iter
    (fun k v ->
      if v < k then ans := !ans + v else if v > k then ans := !ans + (v - k))
    h ;
  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


open Batteries

module Counter = Map.Make(Int)

let solve xs =
  let update = function
    | None -> Some 1
    | Some i -> Some (i + 1)
  in let rec count counter = function
    | [] -> counter
    | h :: t -> Counter.modify_opt h update counter
  in let count_dict = count Counter.empty xs
  in let n_reduce i =
    let ni = Counter.find i count_dict in
    if i < ni then ni - i
    else if i = ni then 0
    else ni
  in BatEnum.map n_reduce (Counter.keys count_dict) |> BatEnum.sum |> print_int

let () =
  let _ = read_int () in
  let xs = read_line () |> String.split_on_char ' ' |> List.map int_of_string in
  solve xs


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 = function
    | [] -> (ix = x) && (iy = y)
    | 'F' :: rest ->
      if (abs (x - ix) + abs (y - iy)) > m then false
      else 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

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 = function
    | [] -> (ix = x) && (iy = y)
    | 'F' :: rest -> go (move (ix, iy) rot) rot rest
    | 'T' :: rest ->
      (go (ix, iy) (rotate_left rot) rest) || (go (ix, iy) (rotate_right rot) rest)
  in
  print_string @@ if go (0, 0) Right instrs then "Yes" else "No"

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


let input_int_array n =
  Array.init n (fun _ -> Scanf.scanf "%d " (fun v -> v))

let () =
  let n = read_int ()
  in let ar = input_int_array n
  in let min_k = Array.fold_left (fun u v ->
    let rec f0 p =
      if p mod 2 = 1 then 0 else (1 + f0 (p/2))
    in let k = f0 v
    in min u k
  ) max_int ar
  in min_k |> string_of_int |> print_endline

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


open Hashtbl

let id x = x
let n = Scanf.scanf "%d " id
let h = create n
let k = Scanf.scanf "%d\n" id
let ans = ref 0

let find_min () =
  fold
    (fun k v tmp ->
       let k', v' = tmp in
       if v < v' then (k, v) else tmp)
    h (0, max_int)

let inith () =
  let rec f i =
    if i <= n then
      let a = Scanf.scanf "%d " id in
      let v = try find h a with _ -> 0 in
      replace h a (v + 1) ;
      f (i + 1)
  in
  f 1

let () =
  inith () ;
  let len = ref (length h) in
  while k < !len do
    let v = find_min () in
    remove h (fst v) ;
    ans := !ans + snd v
  done ;
  Printf.printf "%d\n" !ans

Score : 300 points

Problem Statement

Takahashi has N balls. Initially, an integer A_i is written on the i-th ball.

He would like to rewrite the integer on some balls so that there are at most K different integers written on the N balls.

Find the minimum number of balls that Takahashi needs to rewrite the integers on them.

Constraints

1 \leq K \leq N \leq 200000

1 \leq A_i \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N

Output

Print the minimum number of balls that Takahashi needs to rewrite the integers on them.

Sample Input 1

5 2
1 1 2 2 5

Sample Output 1

1

For example, if we rewrite the integer on the fifth ball to 2, there are two different integers written on the balls: 1 and 2.
On the other hand, it is not possible to rewrite the integers on zero balls so that there are at most two different integers written on the balls, so we should print 1.

Sample Input 2

4 4
1 1 2 2

Sample Output 2

0

Already in the beginning, there are two different integers written on the balls, so we do not need to rewrite anything.

Sample Input 3

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

Sample Output 3

3


open Hashtbl

let id x = x
let n = Scanf.scanf "%d " id
let h = create n
let k = Scanf.scanf "%d\n" id
let ans = ref 0

let find_min () =
  fold
    (fun k v tmp ->
       let k', v' = tmp in
       if v < v' then (k, v) else tmp)
    h (0, max_int)

let inith () =
  let rec f i =
    if i <= n then
      let a = Scanf.scanf "%d " id in
      let v = try find h a with _ -> 0 in
      replace h a (v + 1) ;
      f (i + 1)
  in
  f 1

let () =
  inith () ;
  while k < length h do
    let v = find_min () in
    remove h (fst v) ;
    ans := !ans + snd v
  done ;
  Printf.printf "%d\n" !ans


open Scanf
open Printf

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 rec take n xs_ = match xs_ with
  | (x::xs) when n > 0 -> x :: take (n-1) xs
  | _ -> []

let group_len xs_ =
  let rec f c x ys =
    match ys with
    | [] -> [c]
    | (y::ys') -> if x = y then f (c+1) x ys' else c :: f 1 y ys' in
  match xs_ with
    | [] -> []
    | (x::xs) -> f 1 x xs

let () =
  let n = read_int () in let k = read_int () in
  let ls = read_ints n |> List.sort (-) |> group_len |> List.sort (-) in
  take (List.length ls - k) ls |> List.fold_left (+) 0 |> printf "%d\n"


module A = Array
module C = Char
module I = Int64
module L = List
module Q = Queue

let pf = Printf.printf
let sf = Scanf.scanf
let ssf = Scanf.sscanf

let read_int () = sf "%d " (fun x -> x)
let read_float () = sf "%f " (fun x -> x)
let read_str () = sf "%s " (fun x -> x)
let read_array n read () = A.init n (fun _ -> read ())
let err s = raise (Failure s)

let range s t = A.init (t - s) @@ (+) s
let foreach fold_f init s t map_f =
  range s t |> A.map map_f |> A.fold_left fold_f init

let inf = int_of_float 1e18
let eps = 1e-11

module S = struct
  include String
  let of_array a = String.init (A.length a) (A.get a)
  let to_array s = A.init (String.length s) (String.get s)
end;;

let () =
  let n = read_int () in
  let a = read_int () in
  let b = read_int () in
  let res = min (n * a) b in
  pf "%d\n" res

Score : 100 points

Problem Statement

You are parking at a parking lot. You can choose from the following two fee plans:

Plan 1: The fee will be A×T yen (the currency of Japan) when you park for T hours.

Plan 2: The fee will be B yen, regardless of the duration.

Find the minimum fee when you park for N hours.

Constraints

1≤N≤20

1≤A≤100

1≤B≤2000

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

When the minimum fee is x yen, print the value of x.

Sample Input 1

7 17 120

Sample Output 1

119

If you choose Plan 1, the fee will be 7×17=119 yen.

If you choose Plan 2, the fee will be 120 yen.

Thus, the minimum fee is 119 yen.

Sample Input 2

5 20 100

Sample Output 2

100

The fee might be the same in the two plans.

Sample Input 3

6 18 100

Sample Output 3

100


let () =
  Scanf.scanf "%d\n" @@ fun n ->
  let len = String.length (string_of_int n) in
  let rec sum n i len =
    let jyu = int_of_float (10. ** float_of_int (i-1)) in
    if i > len then 0 else (n / jyu) mod (jyu*10) + sum n (i+1) len in
  Printf.printf "%s\n" (if n mod (sum n 1 len) = 0 then "Yes" else "No")

Score : 200 points

Problem Statement

An integer X is called a Harshad number if X is divisible by f(X), where f(X) is the sum of the digits in X when written in base 10.

Given an integer N, determine whether it is a Harshad number.

Constraints

1?N?10^8

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print Yes if N is a Harshad number; print No otherwise.

Sample Input 1

12

Sample Output 1

Yes

f(12)=1+2=3. Since 12 is divisible by 3, 12 is a Harshad number.

Sample Input 2

57

Sample Output 2

No

f(57)=5+7=12. Since 57 is not divisible by 12, 12 is not a Harshad number.

Sample Input 3

148

Sample Output 3

No


(* O(|n|) *)
let n = read_line () in
let ds = Array.init (String.length n) @@ fun i -> int_of_char n.[i] - 48 in
print_endline @@ if int_of_string n mod Array.fold_left (+) 0 ds = 0 then "Yes" else "No"


(* O(log n) *)
Scanf.scanf "%d" @@ fun n ->
  let rec sum acc m =
    if m <= 0 then acc else sum (acc + m mod 10) @@ m / 10 in
  print_endline @@ if n mod sum 0 n = 0 then "Yes" else "No"


let rec f = function 0 -> 0 | n -> n mod 10 + f (n / 10)

let () = Scanf.scanf "%d" @@ fun x ->
  print_endline @@ if x mod f x = 0 then "Yes" else "No"


let () =
  let n, c = Scanf.scanf "%d %d\n" (fun n c -> n, c) in
  let cs = Array.make c []
  in
  for i = 0 to n - 1 do
    Scanf.scanf "%d %d %d\n" (fun s t c ->
      cs.(c - 1) <- (s, t) :: cs.(c - 1))
  done;
  let channels = Array.make 200003 0 in
  begin Array.map (fun sts ->
    match List.sort compare sts with
    | [] -> []
    | (s, t) :: sts ->
        List.fold_left (fun (s, t, acc) (s', t') ->
          if t = s' then (s, t', acc)
          else (s', t', (s, t) :: acc)) (s, t, []) sts
        |> (fun (s, t, acc) -> (s, t) :: acc)) cs
  |> Array.to_list
  |> List.concat
  |> List.iter (fun (s, t) ->
      channels.(2 * s - 1) <- channels.(2 * s - 1) + 1;
      channels.(2 * t) <- channels.(2 * t) - 1)
  end;
  for i = 1 to Array.length channels - 1 do
    channels.(i) <- channels.(i) + channels.(i - 1)
  done;
  Printf.printf "%d\n" (Array.fold_left max 0 channels)

Score : 400 points

Problem Statement

Joisino is planning to record N TV programs with recorders.

The TV can receive C channels numbered 1 through C.

The i-th program that she wants to record will be broadcast from time s_i to time t_i (including time s_i but not t_i) on Channel c_i.

Here, there will never be more than one program that are broadcast on the same channel at the same time.

When the recorder is recording a channel from time S to time T (including time S but not T), it cannot record other channels from time S-0.5 to time T (including time S-0.5 but not T).

Find the minimum number of recorders required to record the channels so that all the N programs are completely recorded.

Constraints

1≤N≤10^5

1≤C≤30

1≤s_i<t_i≤10^5

1≤c_i≤C

If c_i=c_j and i≠j, either t_i≤s_j or s_i≥t_j.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N C
s_1 t_1 c_1
:
s_N t_N c_N

Output

When the minimum required number of recorders is x, print the value of x.

Sample Input 1

3 2
1 7 2
7 8 1
8 12 1

Sample Output 1

2

Two recorders can record all the programs, for example, as follows:

With the first recorder, record Channel 2 from time 1 to time 7. The first program will be recorded. Note that this recorder will be unable to record other channels from time 0.5 to time 7.

With the second recorder, record Channel 1 from time 7 to time 12. The second and third programs will be recorded. Note that this recorder will be unable to record other channels from time 6.5 to time 12.

Sample Input 2

3 4
1 3 2
3 4 4
1 4 3

Sample Output 2

3

There may be a channel where there is no program to record.

Sample Input 3

9 4
56 60 4
33 37 2
89 90 3
32 43 1
67 68 3
49 51 3
31 32 3
70 71 1
11 12 3

Sample Output 3

2


open Batteries
let () =
  let n = Scanf.scanf "%d " (fun a -> a) in

  let rec digits base n res =
    if n = 0 then res
    else digits base (n/base) ((n mod base)::res)
  in

  let digits_l = digits 10 n [] in

  let rec aux prev cnt = function
      [] -> "No"
    | hd :: tl ->
      if prev = hd then
        let cnt = cnt + 1 in
        if cnt = 3 then
          "Yes"
        else
          aux hd (cnt+1) tl
      else
        aux hd 0 tl
  in

  Printf.printf "%s\n" @@
  aux (-1) 0 digits_l

Score : 100 points

Problem Statement

We call a 4-digit integer with three or more consecutive same digits, such as 1118, good.

You are given a 4-digit integer N. Answer the question: Is N good?

Constraints

1000 ≤ N ≤ 9999

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N is good, print Yes; otherwise, print No.

Sample Input 1

1118

Sample Output 1

Yes

N is good, since it contains three consecutive 1.

Sample Input 2

7777

Sample Output 2

Yes

An integer is also good when all the digits are the same.

Sample Input 3

1234

Sample Output 3

No


let solve s =
  if s.[0] = s.[1] && s.[1] = s.[2] || s.[1] = s.[2] && s.[2] = s.[3] then
    "Yes"
  else "No"

let () = read_line () |> solve |> print_endline


let n = read_int ();;
let rec h n m l = if n = 1 then l else h (n-1) l (m+l);;
print_int (h n 2 1)

Score : 200 points

Problem Statement

It is November 18 now in Japan. By the way, 11 and 18 are adjacent Lucas numbers.

You are given an integer N. Find the N-th Lucas number.

Here, the i-th Lucas number L_i is defined as follows:

L_0=2

L_1=1

L_i=L_{i-1}+L_{i-2} (i≥2)

Constraints

1≤N≤86

It is guaranteed that the answer is less than 10^{18}.

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the N-th Lucas number.

Sample Input 1

5

Sample Output 1

11

L_0=2

L_1=1

L_2=L_0+L_1=3

L_3=L_1+L_2=4

L_4=L_2+L_3=7

L_5=L_3+L_4=11

Thus, the 5-th Lucas number is 11.

Sample Input 2

86

Sample Output 2

939587134549734843


let rec lucas = function
  | 0 -> (2, 1)
  | n ->
      let (l_n_2, l_n_1) = lucas (n - 1) in
      (l_n_1, l_n_1 + l_n_2)

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


Scanf.scanf "%d" (fun abcd ->
    let a = abcd            / 1000 in
    let b = (abcd mod 1000) /  100 in
    let c = (abcd mod  100) /   10 in
    let d = (abcd mod   10)        in
    if a + b + c + d = 7 then Printf.printf "%d+%d+%d+%d=7\n" a b c d else
    if a + b + c - d = 7 then Printf.printf "%d+%d+%d-%d=7\n" a b c d else
    if a + b - c + d = 7 then Printf.printf "%d+%d-%d+%d=7\n" a b c d else
    if a + b - c - d = 7 then Printf.printf "%d+%d-%d-%d=7\n" a b c d else
    if a - b + c + d = 7 then Printf.printf "%d-%d+%d+%d=7\n" a b c d else
    if a - b + c - d = 7 then Printf.printf "%d-%d+%d-%d=7\n" a b c d else
    if a - b - c + d = 7 then Printf.printf "%d-%d-%d+%d=7\n" a b c d else
    if a - b - c - d = 7 then Printf.printf "%d-%d-%d-%d=7\n" a b c d else ()
)

Score : 300 points

Problem Statement

Sitting in a station waiting room, Joisino is gazing at her train ticket.

The ticket is numbered with four digits A, B, C and D in this order, each between 0 and 9 (inclusive).

In the formula A op1 B op2 C op3 D = 7, replace each of the symbols op1, op2 and op3 with + or - so that the formula holds.

The given input guarantees that there is a solution. If there are multiple solutions, any of them will be accepted.

Constraints

0≤A,B,C,D≤9

All input values are integers.

It is guaranteed that there is a solution.

Input

Input is given from Standard Input in the following format:

ABCD

Output

Print the formula you made, including the part =7.

Use the signs + and -.

Do not print a space between a digit and a sign.

Sample Input 1

1222

Sample Output 1

1+2+2+2=7

This is the only valid solution.

Sample Input 2

0290

Sample Output 2

0-2+9+0=7

0 - 2 + 9 - 0 = 7 is also a valid solution.

Sample Input 3

3242

Sample Output 3

3+2+4-2=7


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

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

let id = identity

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

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

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

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

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

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

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

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

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

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

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

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

let (h, w) = scan "%d %d" Tuple2.make
let mat = scan_matrix ~sep:' ' 10 10 0 Int.of_string
let mat2 = scan_matrix ~sep:' ' h w 0 Int.of_string

let () =
  ListL.iter (0++^10) ~f:(fun k ->
      ListL.iter (0++^10) ~f:(fun i ->
          ListL.iter (0++^10) ~f:(fun j ->
              mat.(i).(j) <- min mat.(i).(j) (mat.(i).(k)+mat.(k).(j))
            )
        ));
  ArrayL.map mat2 ~f:(fun a ->
      ArrayL.map a ~f:(fun v ->
          if v = -1 then 0
          else mat.(v).(1)
        )
      |> Array.sum
    )
  |> Array.sum
  |> Printf.printf "%d\n"

Score : 400 points

Problem Statement

Joisino the magical girl has decided to turn every single digit that exists on this world into 1.

Rewriting a digit i with j (0≤i,j≤9) costs c_{i,j} MP (Magic Points).

She is now standing before a wall. The wall is divided into HW squares in H rows and W columns, and at least one square contains a digit between 0 and 9 (inclusive).

You are given A_{i,j} that describes the square at the i-th row from the top and j-th column from the left, as follows:

If A_{i,j}≠-1, the square contains a digit A_{i,j}.

If A_{i,j}=-1, the square does not contain a digit.

Find the minimum total amount of MP required to turn every digit on this wall into 1 in the end.

Constraints

1≤H,W≤200

1≤c_{i,j}≤10^3 (i≠j)

c_{i,j}=0 (i=j)

-1≤A_{i,j}≤9

All input values are integers.

There is at least one digit on the wall.

Input

Input is given from Standard Input in the following format:

H W
c_{0,0} ... c_{0,9}
:
c_{9,0} ... c_{9,9}
A_{1,1} ... A_{1,W}
:
A_{H,1} ... A_{H,W}

Output

Print the minimum total amount of MP required to turn every digit on the wall into 1 in the end.

Sample Input 1

2 4
0 9 9 9 9 9 9 9 9 9
9 0 9 9 9 9 9 9 9 9
9 9 0 9 9 9 9 9 9 9
9 9 9 0 9 9 9 9 9 9
9 9 9 9 0 9 9 9 9 2
9 9 9 9 9 0 9 9 9 9
9 9 9 9 9 9 0 9 9 9
9 9 9 9 9 9 9 0 9 9
9 9 9 9 2 9 9 9 0 9
9 2 9 9 9 9 9 9 9 0
-1 -1 -1 -1
8 1 1 8

Sample Output 1

12

To turn a single 8 into 1, it is optimal to first turn 8 into 4, then turn 4 into 9, and finally turn 9 into 1, costing 6 MP.

The wall contains two 8s, so the minimum total MP required is 6×2=12.

Sample Input 2

5 5
0 999 999 999 999 999 999 999 999 999
999 0 999 999 999 999 999 999 999 999
999 999 0 999 999 999 999 999 999 999
999 999 999 0 999 999 999 999 999 999
999 999 999 999 0 999 999 999 999 999
999 999 999 999 999 0 999 999 999 999
999 999 999 999 999 999 0 999 999 999
999 999 999 999 999 999 999 0 999 999
999 999 999 999 999 999 999 999 0 999
999 999 999 999 999 999 999 999 999 0
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

Sample Output 2

0

Note that she may not need to change any digit.

Sample Input 3

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

Sample Output 3

47


Scanf.scanf "%c %c" @@ fun x y -> print_endline @@ if x = y then "=" else if x < y then "<" else ">"

Score : 100 points

Problem Statement

In programming, hexadecimal notation is often used.

In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters A, B, C, D, E and F are used to represent the values 10, 11, 12, 13, 14 and 15, respectively.

In this problem, you are given two letters X and Y. Each X and Y is A, B, C, D, E or F.

When X and Y are seen as hexadecimal numbers, which is larger?

Constraints

Each X and Y is A, B, C, D, E or F.

Input

Input is given from Standard Input in the following format:

X Y

Output

If X is smaller, print <; if Y is smaller, print >; if they are equal, print =.

Sample Input 1

A B

Sample Output 1

<

10 < 11.

Sample Input 2

E C

Sample Output 2

>

14 > 12.

Sample Input 3

F F

Sample Output 3

=

15 = 15.


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

let ans = (x-z)/(y+z)

    let _ =  Printf.printf "%d\n" ans
;;


(* Vicfred
 * https://atcoder.jp/contests/abc078/tasks/abc078_b
 * math
 * *)
Scanf.scanf "%d %d %d\n" @@ fun x y z -> Printf.printf "%d\n" @@ (x - z) / (y + z)

Score : 200 points

Problem Statement

We have a long seat of width X centimeters.
There are many people who wants to sit here. A person sitting on the seat will always occupy an interval of length Y centimeters.

We would like to seat as many people as possible, but they are all very shy, and there must be a gap of length at least Z centimeters between two people, and between the end of the seat and a person.

At most how many people can sit on the seat?

Constraints

All input values are integers.

1 \leq X, Y, Z \leq 10^5

Y+2Z \leq X

Input

Input is given from Standard Input in the following format:

X Y Z

Output

Print the answer.

Sample Input 1

13 3 1

Sample Output 1

3

There is just enough room for three, as shown below:

Figure

Sample Input 2

12 3 1

Sample Output 2

2

Sample Input 3

100000 1 1

Sample Output 3

49999

Sample Input 4

64146 123 456

Sample Output 4

110

Sample Input 5

64145 123 456

Sample Output 5

109


Scanf.scanf " %d %d %d" @@ fun x y z -> Printf.printf "%d\n" @@ (x - z) / (y + z)


Scanf.scanf "%d %d %d" @@ fun x y z -> Printf.printf "%d\n" @@ (x - z) / (y + z)


open Batteries
let () =
  let x,y = Scanf.scanf "%s %s " (fun a b -> a,b) in
  Printf.printf "%s\n" @@
  if x > y then ">" else if x < y then "<" else "="


let () = Scanf.scanf "%d %d %d" (fun x y z -> (x-z)/(y+z)) |> Printf.printf "%d\n"


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

Score : 700 points

Problem Statement

We have N gemstones labeled 1 through N.

You can perform the following operation any number of times (possibly zero).

Select a positive integer x, and smash all the gems labeled with multiples of x.

Then, for each i, if the gem labeled i remains without getting smashed, you will receive a_i yen (the currency of Japan).
However, a_i may be negative, in which case you will be charged money.

By optimally performing the operation, how much yen can you earn?

Constraints

All input values are integers.

1 \leq N \leq 100

|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 amount of money that can be earned.

Sample Input 1

6
1 2 -6 4 5 3

Sample Output 1

12

It is optimal to smash Gem 3 and 6.

Sample Input 2

6
100 -100 -100 -100 100 -100

Sample Output 2

200

Sample Input 3

5
-1 -2 -3 -4 -5

Sample Output 3

0

It is optimal to smash all the gems.

Sample Input 4

2
-1000 100000

Sample Output 4

99000


open Printf
open Scanf

let solve s1 s2 =
  if s1.[0] = s2.[2] && s1.[1] = s2.[1] && s1.[2] = s2.[0] then "YES" else "NO"

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

Score : 100 points

Problem Statement

You are given a grid with 2 rows and 3 columns of squares.
The color of the square at the i-th row and j-th column is represented by the character C_{ij}.

Write a program that prints YES if this grid remains the same when rotated 180 degrees, and prints NO otherwise.

Constraints

C_{i,j}(1 \leq i \leq 2, 1 \leq j \leq 3) is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

C_{11}C_{12}C_{13}
C_{21}C_{22}C_{23}

Output

Print YES if this grid remains the same when rotated 180 degrees; print NO otherwise.

Sample Input 1

pot
top

Sample Output 1

YES

This grid remains the same when rotated 180 degrees.

Sample Input 2

tab
bet

Sample Output 2

NO

This grid does not remain the same when rotated 180 degrees.

Sample Input 3

eye
eel

Sample Output 3

NO


let () =
  let c1s = read_line () in
  let c2s = read_line () in
  Array.init 3 (fun i -> c1s.[i] = c2s.[2 - i])
  |> Array.fold_left ( && ) true
  |> (function true -> "YES" | false -> "NO")
  |> print_endline