(defun solver ()
  (let* ((n (read)) (si (make-array n :fill-pointer 0))
         (temp 0) (maxcounter 0) word)
      (loop repeat n do
            (vector-push (read) si))
      (let* ((m (read)) (ti (make-array m :fill-pointer 0)))
        (loop repeat m do
              (vector-push (read) ti))
        (loop for i from 0 below n do
              (setf word (aref si i))
              (loop for j from 0 below n do
                    (if (equal word (aref si j))
                        (incf temp)))
              (loop for j from 0 below m do
                    (if (equal word (aref ti j))
                        (decf temp)))
              (when (> temp maxcounter) (setf maxcounter temp))
              (setf temp 0))
        (format t "~a~%" maxcounter))))

(solver)

Score : 200 points

Problem Statement

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

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

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

At most how much can he earn on balance?

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

Constraints

N and M are integers.

1 \leq N, M \leq 100

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

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N
M
t_1
t_2
:
t_M

Output

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

Sample Input 1

3
apple
orange
apple
1
grape

Sample Output 1

2

He can earn 2 yen by announcing apple.

Sample Input 2

3
apple
orange
apple
5
apple
apple
apple
apple
apple

Sample Output 2

1

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

Sample Input 3

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

Sample Output 3

0

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

Sample Input 4

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

Sample Output 4

1


(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
                 (shadow :run)
                 (use-package :fiveam)))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
;;;
;;; Maximum bipartite matching by Ford-Fulkerson
;;;
(defun find-matching (graph)
  "Takes adjacency list and returns the maximal bipartite matching. Note that
this function doesn't check if GRAPH is bipartite."
  (declare #.OPT
           ((simple-array list (*)) graph))
  (let* ((n (length graph))
         (checked (make-array n :element-type 'bit :initial-element 0))
         (matching (make-array n :element-type 'fixnum :initial-element -1))
         (res 0))
    (declare ((integer 0 #.most-positive-fixnum) res))
    (labels ((%match (vertex) ; Returns T if VERTEX is matched.
               (setf (aref checked vertex) 1)
               (dolist (candidate (aref graph vertex))
                 (let ((partner-of-candidate (aref matching candidate)))
                   (when (or (= -1 partner-of-candidate)
                             (and (zerop (aref checked partner-of-candidate))
                                  (%match partner-of-candidate)))
                     (setf (aref matching vertex) candidate
                           (aref matching candidate) vertex)
                     (return t))))))
      (dotimes (v n (values matching res))
        (when (= -1 (aref matching v))
          (fill checked 0)
          (when (%match v)
            (incf res)))))))

(defmacro split-ints-bind (vars string &body body)
  (let ((position (gensym))
        (s (gensym)))
    (labels ((expand (vars &optional init-pos)
               (if (null vars)
                   body
                   `((multiple-value-bind (,(car vars) ,position)
                         (parse-integer ,s :start ,(or init-pos position)
                                           :junk-allowed t)
                       ,@(when (null (cdr vars)) `((declare (ignore ,position))))
                       ,@(expand (cdr vars)))))))
      `(let ((,s ,string))
         (declare (string ,s))
         ,@(expand vars 0)))))

(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(defmacro println (obj &optional (stream '*standard-output*))
  `(let ((*read-default-float-format* 'double-float))
     (prog1 (princ ,obj ,stream) (terpri ,stream))))

(defconstant +mod+ 1000000007)

;; Body
(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (red-xs (make-array n :element-type 'uint8))
         (red-ys (make-array n :element-type 'uint8))
         (blue-xs (make-array n :element-type 'uint8))
         (blue-ys (make-array n :element-type 'uint8))
         (graph (make-array (* 2 n) :element-type 'list :initial-element nil)))
    (declare (uint8 n))
    (dotimes (idx n)
      (split-ints-bind (x y) (read-line)
        (setf (aref red-xs idx) x
              (aref red-ys idx) y)))
    (dotimes (idx n)
      (split-ints-bind (x y) (read-line)
        (setf (aref blue-xs idx) x
              (aref blue-ys idx) y)))
    (dotimes (red-idx n)
      (dotimes (blue-idx n)
        (when (and (< (aref red-xs red-idx)
                      (aref blue-xs blue-idx))
                   (< (aref red-ys red-idx)
                      (aref blue-ys blue-idx)))
          (push (+ n blue-idx) (aref graph red-idx))
          (push red-idx (aref graph (+ n blue-idx))))))
    (println (nth-value 1 (find-matching graph)))))

#-swank(main)

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

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

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.


(format t "~A~A~A~%" (subseq (read-line) 0 1) (subseq (read-line) 1 2) (subseq (read-line) 2 3))

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


(princ (loop :for x :from (read) :upto (read) :count(= x (parse-integer (reverse (format nil "~A" x))))))

Score : 200 points

Problem Statement

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

Constraints

10000 \leq A \leq B \leq 99999

All input values are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

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

Sample Input 1

11009 11332

Sample Output 1

4

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

Sample Input 2

31415 92653

Sample Output 2

612


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 ((h (read))
      (w (read))
      (d (read))
      (co (make-array '(90001 2) :element-type 'integer
                                 :initial-element 0
                                 :adjustable t))
      (dif (make-array '(90001) :element-type 'integer
                                :initial-element 0
                                :adjustable t))
      (temp 0)
      q
      l
      r)
  (dotimes (i h)
    (dotimes (j w)
      (setf temp (read))
      (setf (aref co temp 0) i)
      (setf (aref co temp 1) j)))
  (loop for x from 1 upto d
        do (loop for y from 1
              do (setf temp (+ x (* y d)))
                 while (< temp (* h w)) do (setf (aref dif temp) (+ (aref dif (- temp d)) (abs (- (aref co temp 0) (aref co (- temp d) 0))) (abs (- (aref co temp 1) (aref co (- temp d) 1)))))))
  (setf q (read))
  (dotimes (x q)
    (setf l (read))
    (setf r (read))
    (format t "~A~%" (- (aref dif r) (aref dif l)))))

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


(defun get-alice (lst &optional (acc nil))
  (if (null lst)
    acc
    (get-alice (cddr lst) (cons (car lst) acc))))

(defun get-bob (lst &optional (acc nil))
  (get-alice (cdr lst)))

(let* ((n (read))
       (l (sort (loop repeat n
                      collect (read))
                #'>))
       (alice (get-alice l))
       (bob (get-bob l alice)))

  (format t "~A~%" alice)
  (format t "~A~%" bob)

  (format t "~A~%" (- (reduce #'+ alice) (reduce #'+ bob))))

Score: 200 points

Problem Statement

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

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

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

Constraints

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

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

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 a_3 ... a_N

Output

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

Sample Input 1

2
3 1

Sample Output 1

2

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

Sample Input 2

3
2 7 4

Sample Output 2

5

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

Sample Input 3

4
20 18 2 18

Sample Output 3

18


(defun card-game-for-two (n a)
  (let ((alice 0)
        (bob 0))
    (setf a (sort a #'>))
    (dotimes (i n)
      (if (zerop (rem i 2))
          (setf alice (+ alice (aref a i)))
          (setf bob (+ bob (aref a i)))))
    (- alice bob)))

(defun create-data ()
  (let* ((n (read))
         (a (make-array `(,n))))
    (dotimes (i n)
      (setf (aref a i) (read)))
    (values n a)))

(multiple-value-bind (n a) (create-data)
  (format t "~A~%" (card-game-for-two n a)))


(let* ((n (read))
       (a (make-array n :element-type 'integer
                        :adjustable t
                        :fill-pointer 0))
       (ch (make-array n :element-type 'integer
                         :adjustable t
                         :fill-pointer 0))
       (max-num 0)
       pointer
       (point-a 0)
       (point-b 0))
  (dotimes (i n)
    (setf (aref a i) (read))
    (setf (aref ch i) 0))
  (dotimes (i n)
    (setf max-num 0)
    (dotimes (j n)
      (if (and (< max-num (aref a j)) (= 0 (aref ch j))) (progn (setf pointer j)
                                                                (setf max-num (aref a j)))))
    (setf (aref ch pointer) 1)
    (if (= (mod i 2) 0) (setf point-a (+ point-a (aref a pointer))) (setf point-b (+ point-b (aref a pointer)))))
  (format t "~A~%" (- point-a point-b)))


;;; Utils (quoted from https://competitive12.blogspot.com/2020/03/common-lisp.html)

;; Read fixnum

(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  (declare (inline read-byte)
           #-swank (sb-kernel:ansi-stream in))
  (macrolet ((%read-byte ()
               `(the (unsigned-byte 8)
                     #+swank (char-code (read-char in nil #\Nul))
                     #-swank (read-byte in nil 0))))
    (let* ((minus nil)
           (result (loop (let ((byte (%read-byte)))
                           (cond ((<= 48 byte 57)
                                  (return (- byte 48)))
                                 ((zerop byte) ; #\Nul
                                  (error "Read EOF or #\Nul."))
                                 ((= byte #.(char-code #\-))
                                  (setq minus t)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte 57)
              (setq result (+ (- byte 48) (the (integer 0 #.(floor most-positive-fixnum 10)) (* result 10))))
              (return (if minus (- result) result))))))))

;; fast read-line

(defmacro buffered-read-line (&optional (buffer-size 30) (in '*standard-input*) (term-char #\Space))
  (let ((buffer (gensym))
        (character (gensym))
        (idx (gensym)))
    `(let* ((,buffer (load-time-value (make-string ,buffer-size :element-type 'base-char))))
       (declare (simple-base-string ,buffer)
                (inline read-byte))
       (loop for ,character of-type base-char =
             ,(if (member :swank *features*)
                  `(read-char ,in nil #\Newline) ; on SLIME
                `(code-char (read-byte ,in nil #.(char-code #\Newline))))
             for ,idx from 0
             until (char= ,character #\Newline)
             do (setf (schar ,buffer ,idx) ,character)
             finally (when (< ,idx ,buffer-size)
                       (setf (schar ,buffer ,idx) ,term-char))
             (return (values ,buffer ,idx))))))

;;; invoke child process

#-swank
(unless (member :child-sbcl *features*)
  (quit
    :unix-status
    (process-exit-code
      (run-program *runtime-pathname*
                   `("--control-stack-size" "128MB"
                     "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                     "--eval" "(push :child-sbcl *features*)"
                     "--script" ,(namestring *load-pathname*))
                                                                 :output t :error t :input t))))

;-------------------
; Write code here
;-------------------

(defun judge (board)
  (let ((a (make-array 3))
        (b (make-array 3)))
    (setf (aref a 0) 0)
    (setf (aref b 0) (- (aref board 0 0) (aref a 0)))
    (setf (aref b 1) (- (aref board 0 1) (aref a 0)))
    (setf (aref b 2) (- (aref board 0 2) (aref a 0)))
    (setf (aref a 1) (- (aref board 1 1) (aref b 1)))
    (setf (aref a 2) (- (aref board 2 2) (aref b 2)))
    (let ((flag t))
      (dotimes (i 3)
        (dotimes (j 3)
          (when (/= (aref board i j) (+ (aref a i)
                                        (aref b j)))
            (setq flag nil))))
      flag)))

(defun solve (board)
  (if (judge board)
      "Yes"
      "No"))

(defun main ()
  (let ((board (make-array '(3 3))))
    (dotimes (i 3)
      (dotimes (j 3)
        (setf (aref board i j) (read))))
    (princ (solve board))
    (fresh-line)))

(main)

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


(setq a(read))
(setq b(read))
(setq c(read))
(setq d(read))
(setq e(read))
(setq f(read))
(setq g(read))
(setq h(read))
(setq i(read))
(setq x(+ a (* c -151) (* b 150)))
(setq y(+ d (* f -151) (* e 150)))
(setq z(+ g (* i -151) (* h 150)))
(princ(if(= (* y z)(* x z))"Yes""No"))


(defun solve-route (s ps cnt)
  (if (null ps)
    s
    (progn
      (loop
        for p in ps
        do (setf (aref s (first p) (second p)) cnt))
      (solve-route
        s
        (remove-if #'null (loop
          for p in ps
          append (loop for x in '((0 -1) (-1 0) (0 1) (1 0))
                       collect (let* ((npos (list (+ (first p) (first x))
                                                  (+ (second p) (second x))))
                                      (nval (aref s (first npos) (second npos))))
                                 (if nval
                                   (if (< (1+ cnt) nval)
                                      npos))))))
        (1+ cnt)))))

(let* ((h (read))
       (w (read))
       (white-pos 0)
       (s (make-array (list (+ h 2) (+ w 2)) :initial-element nil)))
  (loop for i from 1 to h do
        (loop for j from 1 to w
              for c in (concatenate 'list (read-line)) do
              (if (equal c #\#)
                (setf (aref s i j) -1)
                (progn (incf white-pos)
                       (setf (aref s i j) 9999)))))
  (solve-route s '((1 1)) 0)
  (let* ((ans (- white-pos (aref s h w) 1)))
    (if (<= 0 ans)
      (format t "~a~%" ans)
      (format t "~a~%" -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


;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (sb-int:defconstant-eqx OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0))
    #'equal)
  #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t)
  #-swank (set-dispatch-macro-character
           #\# #\> (lambda (s c p) (declare (ignore c p)) (read s nil nil t))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)
#-swank (disable-debugger) ; for CS Academy

;; BEGIN_INSERTED_CONTENTS
(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  (declare #.OPT)
  (macrolet ((%read-byte ()
               `(the (unsigned-byte 8)
                     #+swank (char-code (read-char in nil #\Nul))
                     #-swank (sb-impl::ansi-stream-read-byte in nil #.(char-code #\Nul) nil))))
    (let* ((minus nil)
           (result (loop (let ((byte (%read-byte)))
                           (cond ((<= 48 byte 57)
                                  (return (- byte 48)))
                                 ((zerop byte) ; #\Nul
                                  (error "Read EOF or #\Nul."))
                                 ((= byte #.(char-code #\-))
                                  (setf minus t)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte 57)
              (setq result (+ (- byte 48)
                              (* 10 (the (integer 0 #.(floor most-positive-fixnum 10)) result))))
              (return (if minus (- result) result))))))))

;;;
;;; Disjoint set by Union-Find algorithm
;;;

(defstruct (disjoint-set
            (:constructor make-disjoint-set
                (size &aux (data (make-array size :element-type 'fixnum :initial-element -1))))
            (:conc-name ds-))
  (data nil :type (simple-array fixnum (*))))

(declaim (ftype (function * (values (mod #.array-total-size-limit) &optional)) ds-root))
(defun ds-root (disjoint-set x)
  "Returns the root of X."
  (declare (optimize (speed 3))
           ((mod #.array-total-size-limit) x))
  (let ((data (ds-data disjoint-set)))
    (if (< (aref data x) 0)
        x
        (setf (aref data x)
              (ds-root disjoint-set (aref data x))))))

(declaim (inline ds-unite!))
(defun ds-unite! (disjoint-set x1 x2)
  "Destructively unites X1 and X2 and returns true iff X1 and X2 become
connected for the first time."
  (let ((root1 (ds-root disjoint-set x1))
        (root2 (ds-root disjoint-set x2)))
    (unless (= root1 root2)
      (let ((data (ds-data disjoint-set)))
        ;; ensure the size of root1 >= the size of root2
        (when (> (aref data root1) (aref data root2))
          (rotatef root1 root2))
        (incf (aref data root1) (aref data root2))
        (setf (aref data root2) root1)))))

(declaim (inline ds-connected-p))
(defun ds-connected-p (disjoint-set x1 x2)
  "Returns true iff X1 and X2 have the same root."
  (= (ds-root disjoint-set x1) (ds-root disjoint-set x2)))

(declaim (inline ds-size))
(defun ds-size (disjoint-set x)
  "Returns the size of the connected component to which X belongs."
  (- (aref (ds-data disjoint-set)
           (ds-root disjoint-set x))))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

;;;
;;; Meldable heap (pairing heap)
;;;
;;; Reference:
;;; https://topcoder.g.hatena.ne.jp/spaghetti_source/20120929/1348886107
;;;

;; Note: An empty heap is NIL.
;; TODO: handle the order of heap independently
(defstruct (pheap (:constructor %make-pheap (key))
                  (:conc-name %pheap-)
                  (:copier nil)
                  (:predicate nil))
  (key nil :type uint62)
  (next nil :type (or null pheap))
  (head nil :type (or null pheap)) ; head of children
  )

(declaim (inline pheap-merge))
(defun pheap-merge (node1 node2 order)
  (cond ((null node1) node2)
        ((null node2) node1)
        (t
         ;; ensure NODE1 < NODE2
         (when (funcall order (%pheap-key node2) (%pheap-key node1))
           (rotatef node1 node2))
         (setf (%pheap-next node2) (%pheap-head node1)
               (%pheap-head node1) node2)
         node1)))

(declaim (inline %pheap-merge-list1 %pheap-merge-list2 %pheap-merge-list3))

;; NOTE: Three implementations are available for MERGE-LIST, each of which has
;; good points and bad points.

;; Implementation 1, naive recursion
;; Pros: fastest on SBCL, no consing
;; Cons: there is a risk of stack exhaustion

(defun %pheap-merge-list1 (node order)
  (labels ((recur (node)
             (when node
               (let* ((a node)
                      (b (%pheap-next node)))
                 (if b
                     (let ((next (%pheap-next b)))
                       (setf (%pheap-next b) nil)
                       (let ((a+b (pheap-merge a b order)))
                         (pheap-merge a+b (recur next) order)))
                     a)))))
    (recur node)))

;; Implementation 2, manual stack by list
;; Pros: stack safe
;; Cons: most consing, 15% slower

(defun %pheap-merge-list2 (node order)
  (let (stack)
    (loop
      (unless node (return))
      (let ((a node)
            b)
        (setf node (%pheap-next node)
              (%pheap-next a) nil)
        (when node
          (setf b node
                node (%pheap-next node)
                (%pheap-next b) nil))
        (push (pheap-merge a b order) stack)))
    (dolist (part stack)
      (setf node (pheap-merge part node order)))
    node))

;; Implementation 3, manual stack by PHEAP
;; Pros: stack safe, no consing
;; Cons: a bit trickey, 5% slower

(defun %pheap-merge-list3 (node order)
  (let ((stack (load-time-value (sb-mop:class-prototype (find-class 'pheap)))))
    (setf (%pheap-next stack) nil)
    (loop
      (unless node (return))
      (let ((a node)
            b)
        (setf node (%pheap-next node)
              (%pheap-next a) nil)
        (when node
          (setf b node
                node (%pheap-next node)
                (%pheap-next b) nil))
        (setf a (pheap-merge a b order)
              (%pheap-next a) (%pheap-next stack)
              (%pheap-next stack) a)))
    (loop
      (unless (%pheap-next stack) (return))
      (let ((next (%pheap-next stack)))
        (setf (%pheap-next stack)
              (%pheap-next (%pheap-next stack)))
        (setf node (pheap-merge next node order))))
    node))

(declaim (inline pheep-peek))
(defun pheap-peek (node)
  (%pheap-key node))

;; Here we adopt clojure-like terms CONJ/DISJ as these are not operations to
;; make use of side effects, unlike PUSH/POP.
(declaim (inline pheap-conj))
(defun pheap-conj (node key order)
  (pheap-merge node (%make-pheap key) order))

(declaim (inline pheap-disj))
(defun pheap-disj (node order)
  (declare (pheap node))
  (%pheap-merge-list3 (%pheap-head node) order))

(defmacro pheap-push (key node order)
  `(setf ,node (pheap-conj ,node ,key ,order)))

(defmacro pheap-pop (node order)
  `(prog1 (pheap-peek ,node)
     (setf ,node (pheap-disj ,node ,order))))

(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(define-binary-heap heap
  :order (lambda (p1 p2)
           (< (the uint62 (pheap-peek p1))
              (the uint62 (pheap-peek p2))))
  :element-type pheap)

(defun main ()
  (let* ((n (read))
         (m (read))
         (as (make-array n :element-type 'uint32))
         (comps (make-array n :element-type '(or null pheap) :initial-element nil))
         (dset (make-disjoint-set n))
         (que nil)
         (comp-n (- n m))
         (res 0))
    (declare (uint32 n m)
             (uint62 res))
    (dotimes (i n)
      (setf (aref as i) (read-fixnum)))
    (dotimes (i m)
      (let ((x (read-fixnum))
            (y (read-fixnum)))
        (ds-unite! dset x y)))
    (when (= comp-n 1)
      (println 0)
      (return-from main))
    (dotimes (i n)
      (let ((root (ds-root dset i)))
        (pheap-push (aref as i) (aref comps root) #'<)))
    #>comps
    (dotimes (i n)
      (let ((comp (aref comps i)))
        (when (aref comps i)
          (let ((top (pheap-pop comp #'<)))
            (incf res top)
            (setf que (pheap-merge que comp #'<))))))
    #>res
    (dotimes (i (- n m 2))
      (unless que
        (write-line "Impossible")
        (return-from main))
      (incf res (pheap-pop que #'<)))
    (println res)))

#-swank (main)

Score : 600 points

Problem Statement

You are given a forest with N vertices and M edges. The vertices are numbered 0 through N-1.
The edges are given in the format (x_i,y_i), which means that Vertex x_i and y_i are connected by an edge.

Each vertex i has a value a_i.
You want to add edges in the given forest so that the forest becomes connected.
To add an edge, you choose two different vertices i and j, then span an edge between i and j.
This operation costs a_i + a_j dollars, and afterward neither Vertex i nor j can be selected again.

Find the minimum total cost required to make the forest connected, or print Impossible if it is impossible.

Constraints

1 ≤ N ≤ 100,000

0 ≤ M ≤ N-1

1 ≤ a_i ≤ 10^9

0 ≤ x_i,y_i ≤ N-1

The given graph is a forest.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M
a_0 a_1 .. a_{N-1}
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the minimum total cost required to make the forest connected, or print Impossible if it is impossible.

Sample Input 1

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

Sample Output 1

7

If we connect vertices 0 and 5, the graph becomes connected, for the cost of 1 + 6 = 7 dollars.

Sample Input 2

5 0
3 1 4 1 5

Sample Output 2

Impossible

We can't make the graph connected.

Sample Input 3

1 0
5

Sample Output 3

0

The graph is already connected, so we do not need to add any edges.


#-swank
(unless (member :child-sbcl *features*)
  (quit
   :unix-status
   (process-exit-code
    (run-program *runtime-pathname*
                 `("--control-stack-size" "256MB"
                   "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                   "--eval" "(push :child-sbcl *features*)"
                   "--script" ,(namestring *load-pathname*))
                 :output t :error t :input t))))

(in-package :cl-user)
(eval-when (:compile-toplevel :load-toplevel :execute)
  (sb-int:defconstant-eqx opt
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0))
    #'equal)
  #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t)
  #-swank (set-dispatch-macro-character
           #\# #\> (lambda (s c p) (declare (ignore c p)) `(values ,(read s nil nil t)))))
#+swank (set-dispatch-macro-character #\# #\> #'cl-debug-print:debug-print-reader)

(macrolet ((def-int (b)
             `(progn (deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))
                     (deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))))
           (defs (&rest bits) `(progn ,@(mapcar (lambda (b) `(def-int ,b)) bits))))
  (defs 2 4 7 8 15 16 31 32 62 63 64))

(defconstant +mod+ 1000000007)

(defmacro dbg (&rest forms)
  #+swank (if (= (length forms) 1)
              `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
              `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

;; BEGIN_INSERTED_CONTENTS
(defpackage :cp/read-fixnum
  (:use :cl)
  (:export #:read-fixnum))
(in-package :cp/read-fixnum)

(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  "NOTE: cannot read -2^62"
  (macrolet ((%read-byte ()
               `(the (unsigned-byte 8)
                     #+swank (char-code (read-char in nil #\Nul))
                     #-swank (sb-impl::ansi-stream-read-byte in nil #.(char-code #\Nul) nil))))
    (let* ((minus nil)
           (result (loop (let ((byte (%read-byte)))
                           (cond ((<= 48 byte 57)
                                  (return (- byte 48)))
                                 ((zerop byte) ; #\Nul
                                  (error "Read EOF or #\Nul."))
                                 ((= byte #.(char-code #\-))
                                  (setq minus t)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte 57)
              (setq result (+ (- byte 48)
                              (* 10 (the (integer 0 #.(floor most-positive-fixnum 10))
                                         result))))
              (return (if minus (- result) result))))))))

(defpackage :cp/shuffle
  (:use :cl)
  (:export #:shuffle!))
(in-package :cp/shuffle)

(declaim (inline shuffle!))
(defun shuffle! (vector &optional (start 0) end)
  "Destructively shuffles VECTOR by Fisher-Yates algorithm."
  (declare (vector vector)
           ((mod #.array-total-size-limit) start)
           ((or null (mod #.array-total-size-limit)) end))
  (loop for i from (- (or end (length vector)) 1) above start
        for j = (+ start (random (- (+ i 1) start)))
        do (rotatef (aref vector i) (aref vector j)))
  vector)

;; BEGIN_USE_PACKAGE
(eval-when (:compile-toplevel :load-toplevel :execute)
  (use-package :cp/shuffle :cl-user))
(eval-when (:compile-toplevel :load-toplevel :execute)
  (use-package :cp/read-fixnum :cl-user))
(in-package :cl-user)

;;;
;;; Body
;;;

(defun solve (root graph)
  (declare #.cl-user::opt
           ((simple-array list (*)) graph))
  (let ((res 1))
    (declare (uint31 res))
    (sb-int:named-let dfs ((v root) (parent -1))
      (let ((deg 0)
            (antenna 0))
        (declare (uint31 deg antenna))
        (dolist (child (aref graph v))
          (unless (= child parent)
            (incf deg)
            (when (dfs child v)
              (incf antenna))))
        (assert (>= deg antenna))
        (let ((delta (max (- deg antenna 1) 0)))
          (incf res delta)
          (> (+ antenna delta) 0))))
    res))

(defun main ()
  (let* ((n (read))
         (graph (make-array n :element-type 'list :initial-element nil))
         (degs (make-array n :element-type 'uint31 :initial-element 0)))
    (dotimes (i (- n 1))
      (let ((a (read-fixnum))
            (b (read-fixnum)))
        (incf (aref degs a))
        (incf (aref degs b))
        (push a (aref graph b))
        (push b (aref graph a))))
    (let ((roots (coerce (loop for i below n
                               when (= 1 (aref degs i))
                               collect i)
                         '(simple-array uint31 (*)))))
      (shuffle! roots)
      (println (loop for i below (min 200 (length roots))
                     for root = (aref roots i)
                     minimize (solve root graph))))))

#-swank (main)

;;;
;;; Test and benchmark
;;;

#+swank
(defun get-clipbrd ()
  (with-output-to-string (out)
    #+os-windows (run-program "powershell.exe" '("-Command" "Get-Clipboard") :output out :search t)
    #+os-unix (run-program "xsel" '("-b" "-o") :output out :search t)))

#+swank (defparameter *this-pathname* (uiop:current-lisp-file-pathname))
#+swank (defparameter *dat-pathname* (uiop:merge-pathnames* "test.dat" *this-pathname*))

#+swank
(defun run (&optional thing (out *standard-output*))
  "THING := null | string | symbol | pathname

null: run #'MAIN using the text on clipboard as input.
string: run #'MAIN using the string as input.
symbol: alias of FIVEAM:RUN!.
pathname: run #'MAIN using the text file as input."
  (let* ((*standard-output* (or out (make-string-output-stream)))
         (res (etypecase thing
                (null
                 (with-input-from-string (*standard-input* (delete #\Return (get-clipbrd)))
                   (main)))
                (string
                 (with-input-from-string (*standard-input* (delete #\Return thing))
                   (main)))
                (symbol (5am:run! thing))
                (pathname
                 (with-open-file (*standard-input* thing)
                   (main))))))
    (if out res (get-output-stream-string *standard-output*))))

#+swank
(defun gen-dat ()
  (uiop:with-output-file (out *dat-pathname* :if-exists :supersede)
    (format out "")))

#+swank
(defun bench (&optional (out (make-broadcast-stream)))
  (time (run *dat-pathname* out)))

#-swank
(eval-when (:compile-toplevel)
  (when (and (boundp 'sb-c::*compiler-warning-count*)
             (> sb-c::*compiler-warning-count* 0))
    (sb-ext:quit :unix-status 1)))

;; To run: (5am:run! :sample)
#+swank
(5am:test :sample
  (5am:is
   (equal "2
"
          (run "5
0 1
0 2
0 3
3 4
" nil)))
  (5am:is
   (equal "1
"
          (run "2
0 1
" nil)))
  (5am:is
   (equal "3
"
          (run "10
2 8
6 0
4 1
7 6
2 3
8 6
6 9
2 4
5 8
" nil))))

Score : 900 points

Problem Statement

We have a tree with N vertices.
The vertices are numbered 0 through N - 1, and the i-th edge (0 ≤ i < N - 1) comnnects Vertex a_i and b_i.
For each pair of vertices u and v (0 ≤ u, v < N), we define the distance d(u, v) as the number of edges in the path u-v.

It is expected that one of the vertices will be invaded by aliens from outer space.
Snuke wants to immediately identify that vertex when the invasion happens.
To do so, he has decided to install an antenna on some vertices.

First, he decides the number of antennas, K (1 ≤ K ≤ N).
Then, he chooses K different vertices, x_0, x_1, ..., x_{K - 1}, on which he installs Antenna 0, 1, ..., K - 1, respectively.
If Vertex v is invaded by aliens, Antenna k (0 ≤ k < K) will output the distance d(x_k, v).
Based on these K outputs, Snuke will identify the vertex that is invaded.
Thus, in order to identify the invaded vertex no matter which one is invaded, the following condition must hold:

For each vertex u (0 ≤ u < N), consider the vector (d(x_0, u), ..., d(x_{K - 1}, u)). These N vectors are distinct.

Find the minumum value of K, the number of antennas, when the condition is satisfied.

Constraints

2 ≤ N ≤ 10^5

0 ≤ a_i, b_i < N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_0 b_0
a_1 b_1
:
a_{N - 2} b_{N - 2}

Output

Print the minumum value of K, the number of antennas, when the condition is satisfied.

Sample Input 1

5
0 1
0 2
0 3
3 4

Sample Output 1

2

For example, install an antenna on Vertex 1 and 3.
Then, the following five vectors are distinct:

(d(1, 0), d(3, 0)) = (1, 1)

(d(1, 1), d(3, 1)) = (0, 2)

(d(1, 2), d(3, 2)) = (2, 2)

(d(1, 3), d(3, 3)) = (2, 0)

(d(1, 4), d(3, 4)) = (3, 1)

Sample Input 2

2
0 1

Sample Output 2

1

For example, install an antenna on Vertex 0.

Sample Input 3

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

Sample Output 3

3

For example, install an antenna on Vertex 0, 4, 9.


(let ((a (read))
      (b (read))
      (c (read)))
  (princ (mod (- a b) c)))

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* ((X (read))
       (A (read))
       (B (read))
       (budget (- X A)))
  (format t "~A~%" (mod budget B)))


#-swank
(unless (member :child-sbcl *features*)
  (quit
   :unix-status
   (process-exit-code
    (run-program *runtime-pathname*
                 `("--control-stack-size" "128MB"
                   "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                   "--eval" "(push :child-sbcl *features*)"
                   "--script" ,(namestring *load-pathname*))
                 :output t :error t :input t))))
(defmacro dp (name args memo-size &body body)
  (let ((memo (gensym)))
    `(let ((,memo (make-array ,memo-size :initial-element nil)))
       (defun ,name ,args
         (or (aref ,memo ,@args)
             (setf (aref ,memo ,@args)
                   ,@body))))))

(defun split (x str)
  (let ((pos (search x str))
        (size (length x)))
    (if pos
        (cons (subseq str 0 pos)
              (split x (subseq str (+ pos size))))
        (list str))))

(defmacro collect-times (time body)
  `(loop repeat ,time collect ,body))

(defun read-times (time)
  (collect-times time (read)))

(defmacro aif (expr then else)
  `(let ((it ,expr))
     (if it ,then ,else)))

(defmacro awhen (expr &rest then)
  `(aif ,expr (progn ,@then) nil))

(defun comulative (function list &key base)
  (if base
      (comulative function (cons base list))
      (do ((lst (cdr list) (cdr lst))
           (acc (list (car list))))
          ((null lst) (reverse acc))
        (push (funcall function (car acc) (car lst)) acc))))

(defun binary-search (function left right
                      &optional
                        (eps 1)
                        (average-function (lambda (x y) (ash (+ x y) -1))))
  (if (<= (abs (- left right)) eps)
      right
      (let ((mid (funcall average-function left right)))
        (if (funcall function mid)
            (binary-search function left mid eps average-function)
            (binary-search function mid right eps average-function)))))

(defun arithmetic-mean (&rest body)
  (/ (apply #'+ body) (length body)))

(defvar +MOD+ (+ (expt 10 9) 7))

(defun mapa-b (fn a b &optional (step 1))
  (do ((i a (+ i step))
       (result nil))
      ((> i b) (nreverse result))
    (push (funcall fn i) result)))

(defun map0-n (fn n)
  (mapa-b fn 0 n))

(defun map1-n (fn n)
  (mapa-b fn 1 n))

(defun read-string (&optional (stream *standard-input*))
  (labels ((is-empty (x)
             (or (char= x #\space) (char= x #\newline))))
    (loop for char = (read-char stream)
          with result
          when (is-empty char)
            do (return (concatenate 'string (nreverse result)))
          do (push char result))))

(defun main (a b &aux (a-sum (comulative #'+ a)) (b-sum (reverse (comulative #'+ (reverse b)))))
  (loop for i in a-sum
        for j in b-sum
        maximize (+ i j)))

(let ((n (read)))
  (princ (main (read-times n) (read-times n))))

Score : 300 points

Problem Statement

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

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

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

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

Constraints

1 \leq N \leq 100

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

Input

Input is given from Standard Input in the following format:

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

Output

Print the maximum number of candies that can be collected.

Sample Input 1

5
3 2 2 4 1
1 2 2 2 1

Sample Output 1

14

The number of collected candies will be maximized when you:

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

Sample Input 2

4
1 1 1 1
1 1 1 1

Sample Output 2

5

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

Sample Input 3

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

Sample Output 3

29

Sample Input 4

1
2
3

Sample Output 4

5


(let ((a (write-to-string (read)))
      (b (write-to-string (read))))

  (format t "~A~%"
          (if (integerp (sqrt (parse-integer (concatenate 'string a b))))
            "Yes"
            "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


(defun sqrtp (x)
  (equal 0.0 (rem (sqrt x) 1)))
(defun joint (x y)
  (if (eq y 100)
      (+ (* x 1000) 100)
      (if (> y 9)
          (+ (* x 100) y)
          (+ (* x 10) y))))
(defun yn (x)
  (if x "Yes" "No"))

(format t (yn (sqrtp (joint (read) (read)))))


(def sqrtp (x)
  (equal 0.0 (rem (sqrt x) 1)))
(def joint (x y)
  (if (> x 9)
      (+ (* 100 x) y)
      (+ (* 10 x) y)))
(def yn (x)
  (if x "Yes" "No"))

(format t (yn (sqrtp (joint x y))))


(defun solver ()
  (let ((ab (parse-integer (format nil "~a~a" (read) (read)))))
    (loop for i from 0 to ab
          when (equalp (expt i 2) ab)
          return (format t "Yes~%")
          finally (format t "No~%"))))

(solver)


(let* ((a (read))
       (b (read))
       (ab (parse-integer (format nil "~A~A" a b)))
       (sab (isqrt ab)))
  (format t (if (= (* sab sab) ab) "Yes" "No")))


;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
                 (shadow :run)
                 (use-package :fiveam)))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  (declare #.OPT)
  (macrolet ((%read-byte ()
               `(the (unsigned-byte 8)
                     #+swank (char-code (read-char in nil #\Nul))
                     #-swank (sb-impl::ansi-stream-read-byte in nil #.(char-code #\Nul) nil))))
    (let* ((minus nil)
           (result (loop (let ((byte (%read-byte)))
                           (cond ((<= 48 byte 57)
                                  (return (- byte 48)))
                                 ((zerop byte) ; #\Nul
                                  ;; (return-from read-fixnum 0)
                                  (error "Read EOF or #\Nul."))
                                 ((= byte #.(char-code #\-))
                                  (setf minus t)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte 57)
              (setq result (+ (- byte 48) (* 10 (the (integer 0 #.(floor most-positive-fixnum 10)) result))))
              (return (if minus (- result) result))))))))

(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

(defconstant +mod+ 1000000007)

;; Body

(declaim (inline get-2dcumul))
(defun get-2dcumul (table y0 x0 y1 x1)
  (+ (- (aref table y1 x1)
        (aref table y0 x1)
        (aref table y1 x0))
     (aref table y0 x0)))

(declaim (inline fast-read-char))
(defun fast-read-char (&optional (in *standard-input*))
  #-swank (declare (inline read-byte)
                   (sb-kernel:ansi-stream in))
  #-swank (code-char (read-byte in))
  #+swank (read-char in))

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (k (read))
         (btable (make-array (list (+ 1 (* 3 k)) (+ 1 (* 3 k)))
                             :element-type 'uint32 :initial-element 0))
         (wtable (make-array (list (+ 1 (* 3 k)) (+ 1 (* 3 k)))
                             :element-type 'uint32 :initial-element 0)))
    (declare (uint32 n k))
    (dotimes (i n)
      (let* ((x (+ 1 (mod (read-fixnum) (* 2 k)))) ;; updated point
             (y (+ 1 (mod (read-fixnum) (* 2 k))))
             (table (if (char= (fast-read-char) #\B) btable wtable)))
        (incf (aref table y x))
        (when (<= (+ x (* 2 k)) (* 3 k))
          (incf (aref table y (+ x (* 2 k))))
          (when (<= (+ y (* 2 k)) (* 3 k))
            (incf (aref table (+ y (* 2 k)) (+ x (* 2 k))))))
        (when (<= (+ y (* 2 k)) (* 3 k))
          (incf (aref table (+ y (* 2 k)) x)))))
    (dotimes (x (+ 1 (* 3 k)))
      (dotimes (y (* 3 k))
        (incf (aref btable (+ y 1) x) (aref btable y x))
        (incf (aref wtable (+ y 1) x) (aref wtable y x))))
    (dotimes (y (+ 1 (* 3 k)))
      (dotimes (x (* 3 k))
        (incf (aref btable y (+ x 1)) (aref btable y x))
        (incf (aref wtable y (+ x 1)) (aref wtable y x))))
    (let ((res 0))
      (declare (uint32 res))
      (dotimes (x k)
        (dotimes (y k)
          (let ((value (+ (get-2dcumul btable y x (+ y k) (+ x k))
                          (get-2dcumul btable (+ y k) (+ x k) (+ y k k) (+ x k k))
                          (get-2dcumul wtable y (+ x k) (+ y k) (+ x k k))
                          (get-2dcumul wtable (+ y k) x (+ y k k) (+ x k)))))
            (setf res (max res value)))))
      (println res))))

#-swank(main)

Score : 500 points

Problem Statement

AtCoDeer is thinking of painting an infinite two-dimensional grid in a checked pattern of side K.
Here, a checked pattern of side K is a pattern where each square is painted black or white so that each connected component of each color is a K × K square.
Below is an example of a checked pattern of side 3:

AtCoDeer has N desires.
The i-th desire is represented by x_i, y_i and c_i.
If c_i is B, it means that he wants to paint the square (x_i,y_i) black; if c_i is W, he wants to paint the square (x_i,y_i) white.
At most how many desires can he satisfy at the same time?

Constraints

1 ≤ N ≤ 10^5

1 ≤ K ≤ 1000

0 ≤ x_i ≤ 10^9

0 ≤ y_i ≤ 10^9

If i ≠ j, then (x_i,y_i) ≠ (x_j,y_j).

c_i is B or W.

N, K, x_i and y_i are integers.

Input

Input is given from Standard Input in the following format:

N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N

Output

Print the maximum number of desires that can be satisfied at the same time.

Sample Input 1

4 3
0 1 W
1 2 W
5 3 B
5 4 B

Sample Output 1

4

He can satisfy all his desires by painting as shown in the example above.

Sample Input 2

2 1000
0 0 B
0 1 W

Sample Output 2

2

Sample Input 3

6 2
1 2 B
2 1 W
2 2 B
1 0 B
0 6 W
4 5 W

Sample Output 3

4


(let* ((l (concatenate 'list (read-line))))

  (format t "~A~%"
          (concatenate 'string (append (concatenate 'list "2018") (subseq l 4)))))

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 ((da (concatenate 'list (read-line))))
  (setf (nth 3 da) #\8)
  (princ da))


(let ((str (read-line)))
  (setf (aref str 3) #\8)
  (write-line str))


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 ((a (read))
      (b (read))
      (s (concatenate 'list (read-line))))

  (defun judge-hyphen (p lst)
    (if (char= (nth p lst) #\-)
      t
      nil))

  (defun judge-length (q r lst)
    (if (= (length lst) (+ q r 1))
      t
      nil))

  (defun judge-unique (lst)
    (if (= (count #\- lst) 1)
      t
      nil))

  (format t "~A~%"
          (if (and (judge-hyphen a s)
                   (judge-length a b s)
                   (judge-unique s))
            "Yes"
            "No")))

Score : 200 points

Problem Statement

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

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

Constraints

1≤A,B≤5

|S|=A+B+1

S consists of - and digits from 0 through 9.

Input

Input is given from Standard Input in the following format:

A B
S

Output

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

Sample Input 1

3 4
269-6650

Sample Output 1

Yes

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

Sample Input 2

1 1
---

Sample Output 2

No

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

Sample Input 3

1 2
7444

Sample Output 3

No


(let* ((a (read))
       (b (read))
       (s (read-line))
       (as (subseq s 0 a))
       (cs (subseq s a (1+ a)))
       (bs (subseq s (1+ a))))
      (if (and (every #'digit-char-p as)
               (equal cs "-")
               (every #'digit-char-p bs))
          (princ "Yes")
          (princ "No")))


;; -*- coding:utf-8 -*-

(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 1))
    #-swank '(optimize (speed 3) (safety 0) (debug 0))))

(defmacro println (obj &optional (stream '*standard-output*))
  `(prog1 (princ ,obj ,stream) (terpri ,stream)))

;; Hauptteil

(deftype uint nil `(integer 0 ,(expt 10 9)))

(defun push-carton (carton rest candidate-rest)
  (if (null rest)
      (if candidate-rest
          (push carton (car candidate-rest))
          (push (list carton) *haystacks*))
      (let ((stack (car rest)))
        (if (and (<= carton (car stack))
                 (or (null candidate-rest)
                     (<= (car stack) (caar candidate-rest))))
            (push-carton carton (cdr rest) rest)
            (push-carton carton (cdr rest) candidate-rest)))))

(defun main ()
  (let ((n (read))
        (*haystacks* nil))
    (declare (special *haystacks*))
    (dotimes (i n)
      (push-carton (read) *haystacks* nil))
    (println (length *haystacks*))))

#-swank(main)

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


(defun f (x y)
  (labels ((inner (n rv)
	     (if (<= n y)
		 (inner (* n 2) (1+ rv))
		 (1- rv))))
    (inner x 1)))

(print (f (read) (read)))


(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
                 (shadow :run)
                 (use-package :fiveam)))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(defmacro println (obj &optional (stream '*standard-output*))
  `(let ((*read-default-float-format* 'double-float))
     (prog1 (princ ,obj ,stream) (terpri ,stream))))

(defconstant +mod+ 1000000007)

;; Body

(defun count-consecutive-chars (s)
  (let ((c0 (aref s 0)))
    (loop for c across s
          while (char= c0 c)
          count t)))

(defun main ()
  (let* ((s (read-line))
         (len (length s)))
    (println
     (max (count-consecutive-chars s)
          (count-consecutive-chars (nreverse s))
          (ceiling len 2)))))

#-swank(main)

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


(defun f (s)
  (loop for ch across s
        until (eq ch #\1)
        count ch))

(let ((x (read-line)))
  (format t "~A~%"
          (- (length x) (f x))))


(defun solve (X Y &optional (i 0))
  (if (> X Y) i
    (solve (* X 2) Y (1+ i))))
(princ (solve (read) (read)))


(define N (read))
(define alledges (make-vector N '()))
(map (lambda (NO-USE)
       (let [(u (- (read) 1))
	     (v (- (read) 1))]
	 (set! (vector-ref alledges u)
	       (cons v (vector-ref alledges u)))
	 (set! (vector-ref alledges v)
	       (cons u (vector-ref alledges v)))))
     (make-list (- N 1)))
(define (remove* ele lst)
  (cond [(null? lst) lst]
	[(= ele (car lst))
	 (remove* ele (cdr lst))]
	[else
	 (cons (car lst)
	       (remove* ele (cdr lst)))]))
(define (get-fewest-paths-sons sons pa)
  (if (null? sons)
      0
      (+ (get-fewest-paths (car sons) pa)
	 (get-fewest-paths-sons (cdr sons) pa))))
(define (get-fewest-paths u pa)
  (let [(edges (remove* pa (vector-ref alledges u)))
	(deg (length (vector-ref alledges u)))]
    (set! (vector-ref alledges u) edges)
    (+ (if (odd? deg) 1/2 0)
       (get-fewest-paths-sons edges u))))
(define (let-paired lst lim)
  (letrec [(vec
	    (list->vector (sort lst >))
	    ;; (list->vector lst)
	    )
	   (len (vector-length vec))
	   (pairpos
	    (lambda (lp rp stack sp)
	      (cond
	       [(= lp len) -1]
	       [(and (< rp lp)
		     (or (null? stack)
			 (> (car stack) lp)))
		(pairpos (+ lp 1) rp stack sp)]
	       [else
		(while (and (<= lp rp)
			    (<= (+ (vector-ref vec lp)
				   (vector-ref vec rp)
				   2)
				lim))
		       (set! stack (cons rp stack))
		       (set! rp (- rp 1)))
		(cond
		 [(and
		   (not (null? stack))
		   (= lp (car stack))
		   (null? (cdr stack)))
		  (vector-ref vec lp)]
		 [(null? stack)
		  (if (and sp
			   (<= (vector-ref vec lp) lim))
		      (let [(con-ret (pairpos (+ lp 1) rp stack #f))]
			(cond
			 [(not con-ret) #f]
			 [(odd? len) (vector-ref vec lp)]
			 [else con-ret]))
		      #f)]
		 [else
		  (when (= lp (car stack))
			(set! stack (cdr stack)))
		  (pairpos (+ lp 1) rp (cdr stack) sp)])])))
	   (res
	    (pairpos 0 (- len 1) '() #t))]
    res))
(define (find* ele lst)
  (cond
   [(null? lst) #f]
   [(eq? ele (car lst)) #t]
   [else (find* ele (cdr lst))]))
(define (check-OK? u pa lim)
  (let* [(edges (vector-ref alledges u))
	 ;; (son-ret (map (lambda (v) (check-OK? v u lim))
	 ;; 	       edges))
	 (son-ret '())]
    (while (and (not (null? edges))
    		(or (null? son-ret)
    		    (car son-ret)))
    	   (set! son-ret
    		 (cons (check-OK? (car edges) u lim)
    		       son-ret))
    	   (set! edges (cdr edges)))
    (if (find* #f son-ret)
    	#f
    	(let [(pair-ret (let-paired son-ret lim))]
    	  (if pair-ret
    	      (+ pair-ret 1)
    	      #f)))))

(define (check-OK?-lr lv rv)
  (if (= (+ lv 1) rv)
      lv
      (let* [(mid (quotient (+ lv rv) 2))
	     ;; (NO-USE (print "check-OK?-lr " (- mid 1)))
	     (check-res (check-OK? 0 -1 (- mid 1)))]
	(if (and check-res
		 (<= check-res (- mid 1)))
	    (check-OK?-lr lv mid)
	    (check-OK?-lr mid rv)))))
(print (get-fewest-paths 0 -1) " " (check-OK?-lr 0 (+ N 1)))

Score : 900 points

Problem Statement

Takahashi has decided to make a Christmas Tree for the Christmas party in AtCoder, Inc.

A Christmas Tree is a tree with N vertices numbered 1 through N and N-1 edges, whose i-th edge (1\leq i\leq N-1) connects Vertex a_i and b_i.

He would like to make one as follows:

Specify two non-negative integers A and B.

Prepare A Christmas Paths whose lengths are at most B. Here, a Christmas Path of length X is a graph with X+1 vertices and X edges such that, if we properly number the vertices 1 through X+1, the i-th edge (1\leq i\leq X) will connect Vertex i and i+1.

Repeat the following operation until he has one connected tree:

Select two vertices x and y that belong to different connected components. Combine x and y into one vertex. More precisely, for each edge (p,y) incident to the vertex y, add the edge (p,x). Then, delete the vertex y and all the edges incident to y.

Properly number the vertices in the tree.

Takahashi would like to find the lexicographically smallest pair (A,B) such that he can make a Christmas Tree, that is, find the smallest A, and find the smallest B under the condition that A is minimized.

Solve this problem for him.

Constraints

2 \leq N \leq 10^5

1 \leq a_i,b_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

For the lexicographically smallest (A,B), print A and B with a space in between.

Sample Input 1

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

Sample Output 1

3 2

We can make a Christmas Tree as shown in the figure below:

Sample Input 2

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

Sample Output 2

2 5

Sample Input 3

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

Sample Output 3

3 4


(let* ((a (read-line))
       (b (read-line)))
  (if (string< (sort a #'char<)
               (sort b #'char>))
      (princ "Yes")
      (princ "No")))

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


(defun check (a b)
  (cond ((and (null a) (null b)) nil)
        ((null a) t)
        ((null b) nil)
        ((char< (car a) (car b)) t)
        ((char= (car a) (car b)) (check (cdr a) (cdr b)))))

(let* ((a (read-line))
       (b (read-line)))
  (if (check (concatenate 'list (sort a #'char<))
             (concatenate 'list (sort b #'char>)))
      (princ "Yes")
      (princ "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


(defun string-sort (k a)
  (map 'string #'code-char (sort (map 'vector #'char-code k) a)))
(if (string< (string-sort (read-line) #'<) (string-sort (read-line) #'>))
    (princ "Yes") (princ "No"))


(let ((a (read-line))
      (b (read-line)))
  (setf a (sort (copy-seq a) #'char<)
        b (sort (copy-seq b) #'char>))
  (format t "~A~%"
          (if (string< a b) "Yes" "No")))


(let* ((a (read-line))
       (b (sort (read-from-string (concatenate 'string "(" (read-line) ")")) #'<))
       (c (remove-duplicates b)))
  (if (null b)
      t
      (princ (apply #'+ (mapcar #'(lambda (x)
                             (let ((co (count x b)))
                               (cond ((plusp (- x co)) co)
                                     ((minusp (- x co)) (- co x))
                                     (t 0))))
                         c)))))

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


;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
                 (shadow :run)
                 (use-package :fiveam)))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

(defconstant +mod+ 1000000007)

;; Body

(defun main ()
  (declare #.OPT)
  (let* ((s (read-line))
         (len (length s))
         (x (read))
         (y (read))
         (hs (make-array len :element-type 'uint16 :fill-pointer 0))
         (vs (make-array len :element-type 'uint16 :fill-pointer 0)))
    (declare ((integer 1 8000) len)
             ((integer -8000 8000) x y))
    (loop with prev-t = -1
          with dest = hs
          for i below (length s)
          when (char= #\T (char s i))
          do (vector-push (- i prev-t 1) dest)
             (setq prev-t i
                   dest (if (eql dest hs) vs hs))
          finally (vector-push (- i prev-t 1) dest))
    (if
     (and
      (let ((dp1 (make-array (+ 1 (* 2 len)) :initial-element 0 :element-type 'bit))
            (dp2 (make-array (+ 1 (* 2 len)) :initial-element 0 :element-type 'bit)))
        (setf (aref dp1 (+ len (aref hs 0))) 1)
        (if (= 1 (length hs))
            (= (aref hs 0) x)
            (loop for h across (subseq hs 1)
                  for dp = dp1 then (if (eql dp dp1) dp2 dp1)
                  for dest-dp = dp2 then (if (eql dest-dp dp1) dp2 dp1)
                  do (fill dest-dp 0)
                     (loop for i of-type uint16 below (length dp)
                           when (= 1 (aref dp i))
                           do (setf (aref dest-dp (+ i h)) 1
                                    (aref dest-dp (- i h)) 1))
                  finally (return (= 1 (aref dest-dp (+ len x)))))))
      (let ((dp1 (make-array (+ 1 (* 2 len)) :initial-element 0 :element-type 'bit))
            (dp2 (make-array (+ 1 (* 2 len)) :initial-element 0 :element-type 'bit)))
        (setf (aref dp1 len) 1)
        (if (zerop (length vs))
            (zerop y)
            (loop for v across vs
                  for dp = dp1 then (if (eql dp dp1) dp2 dp1)
                  for dest-dp = dp2 then (if (eql dest-dp dp1) dp2 dp1)
                  do (fill dest-dp 0)
                     (loop for i of-type uint16 below (length dp)
                           when (= 1 (aref dp i))
                           do (setf (aref dest-dp (+ i v)) 1
                                    (aref dest-dp (- i v)) 1))
                  finally (return (= 1 (aref dest-dp (+ len y))))))))
     (write-line "Yes")
     (write-line "No"))))

#-swank(main)

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


(let* ((n (read))
       (l (loop repeat n
                collect (read))))

  (defun divisible (p)
    (zerop (mod p 2)))

  (defun divided (q)
    (/ q 2))

  (defun recursive-d (lst &optional (cnt 0))
    (if (not (every #'identity (mapcar #'divisible lst)))
        cnt
        (recursive-d (mapcar #'divided lst) (1+ cnt))))

  (format t "~A~%"
          (recursive-d l)))

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


(defun fun ()
  (let ((n (read))
	(x 0)
	(ans 0))
    (dotimes (i n)
      (setf x (logior x (read))))
    (loop
      (if (eq (logand x 1) 1) (return))
      (setf ans (1+ ans))
      (setf x (ash x -1)))
    ans))

(format t "~A~%" (fun))


(defun fun ()
  (let ((n (read))
	(x 0)
	(ans 0))
    (dotimes (i n)
      (setf x (logior x (read))))
    (dotimes (i n)
      (if (eq (logand x 1) 1) (return))
      (setf ans (1+ ans))
      (setf x (ash x -1)))
    ans))

(format t "~A~%" (fun))


(defun fun (n a)
  (let ((flg nil)
	(res 0))
    (loop
       (dotimes (i n)
	 (if (not (zerop (rem (aref a i) 2)))
	     (setf flg t)))
       (if (eq flg t) (return))
       (setf res (+ res 1))
       (dotimes (i n)
	 (setf (aref a i) (/ (aref a i) 2))))
    res))

(defun input ()
  (let* ((n (read))
	 (a (make-array n)))
    (dotimes (i n)
      (setf (aref a i) (read)))
    (values n a)))

(multiple-value-bind (n a) (input)
  (format t "~A~%" (fun n a)))


(defun shift-only (n a)
  (let ((res 0)
	(exist-odd nil))
    (loop
      ;; (dotimes (i n)
      ;; 	(if (not (zerop (rem (aref a i) 2)))
      ;; 	    (setf exist-odd t)))
      (if (oddp (apply #'+ a;; (coerce temp 'list)
		       ))
	  (setf exist-odd t))
      (if (eq exist-odd t) (return))
      (dotimes (i n)
	;; (setf (aref a i) (/ (aref a i) 2))
	((setf (nth a i) (/ (nth a i) 2))))
      (incf res))
    res))

(defun create-data ()
  (let* ((n (read))
	 (a ;; (make-array `(,n))
	    (loop for i to 10 collect i)))
    (dotimes (i n)
      ;; (setf (aref a i) (read))
      (setf (nth a i) (read)))
    (values n a)))

(multiple-value-bind (n a) (create-data)
  (format t "~a~%" (shift-only n a)))


(defun count-div (v)
  (if (zerop v)
      0
      (multiple-value-bind (q r)
        (truncate v 2)
        (if (= 0 r)
            (1+ (count-div q))
            0))))

(let* ((n (read))
       (a (make-array n)))
  (dotimes (i n)
    (setf (aref a i) (read)))
  (format t "~A~%"
          (loop for ai across a
                minimize (count-div ai))))


(defun count-div (v)
  (if (zerop v)
      0
      (multiple-value-bind (q r)
        (truncate v 2)
        (if (= 0 r)
            (1+ (count-div q))
            0))))

(let* ((n (read))
       (a (make-array n)))
  (dotimes (i n)
    (setf (aref a i) (read)))
  (format t "~A~%"
          (loop for ai across a
                with m = 0
                if (< m ai)
                do (setf m ai)
                and minimize
                (count-div ai))))


;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (sb-int:defconstant-eqx OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0))
    #'equal)
  #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t)
  #-swank (set-dispatch-macro-character
           ;; enclose the form with VALUES to avoid being captured by LOOP macro
           #\# #\> (lambda (s c p) (declare (ignore c p)) `(values ,(read s nil nil t)))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)
#-swank (disable-debugger) ; for CS Academy

;; BEGIN_INSERTED_CONTENTS
(declaim (inline hash-keys-to-list))
(defun hash-keys-to-list (hash-table)
  (let ((result nil))
    (maphash (lambda (key _)
               (declare (ignore _))
               (push key result))
             hash-table)
    result))

;; from alexandria
(declaim (inline hash-table-to-alist))
(defun hash-table-to-alist (hash-table)
  (let ((alist nil))
    (maphash (lambda (k v)
               (push (cons k v) alist))
             hash-table)
    alist))

(declaim (inline alist-to-hash-table))
(defun alist-to-hash-table (alist &key (test #'eql))
  (let ((table (make-hash-table :test test)))
    (dolist (pair alist table)
      (setf (gethash (car pair) table) (cdr pair)))))

(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  "NOTE: cannot read -2^62"
  (macrolet ((%read-byte ()
               `(the (unsigned-byte 8)
                     #+swank (char-code (read-char in nil #\Nul))
                     #-swank (sb-impl::ansi-stream-read-byte in nil #.(char-code #\Nul) nil))))
    (let* ((minus nil)
           (result (loop (let ((byte (%read-byte)))
                           (cond ((<= 48 byte 57)
                                  (return (- byte 48)))
                                 ((zerop byte) ; #\Nul
                                  (error "Read EOF or #\Nul."))
                                 ((= byte #.(char-code #\-))
                                  (setf minus t)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte 57)
              (setq result (+ (- byte 48)
                              (* 10 (the (integer 0 #.(floor most-positive-fixnum 10))
                                         result))))
              (return (if minus (- result) result))))))))

(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defun main ()
  (let* ((n (read))
         (k (read))
         (table (make-hash-table)))
    (dotimes (_ n)
      (let ((a (read-fixnum)))
        (if (gethash a table)
            (incf (gethash a table))
            (setf (gethash a table) 1))))
    (let ((alist (sort (hash-table-to-alist table) #'> :key #'cdr)))
      (println
       (reduce #'+
               (subseq alist (min (length alist) k))
               :key #'cdr)))))

#-swank (main)

;;;
;;; Test and benchmark
;;;

#+swank
(defun io-equal (in-string out-string &key (function #'main) (test #'equal))
  "Passes IN-STRING to *STANDARD-INPUT*, executes FUNCTION, and returns true if
the string output to *STANDARD-OUTPUT* is equal to OUT-STRING."
  (labels ((ensure-last-lf (s)
             (if (eql (uiop:last-char s) #\Linefeed)
                 s
                 (uiop:strcat s uiop:+lf+))))
    (funcall test
             (ensure-last-lf out-string)
             (with-output-to-string (out)
               (let ((*standard-output* out))
                 (with-input-from-string (*standard-input* (ensure-last-lf in-string))
                   (funcall function)))))))

#+swank
(defun get-clipbrd ()
  (with-output-to-string (out)
    ;; (run-program "C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe" '("get-clipboard") :output out)
    (run-program "powershell.exe" '("-Command" "Get-Clipboard") :output out :search t)))

#+swank (defparameter *this-pathname* (uiop:current-lisp-file-pathname))
#+swank (defparameter *dat-pathname* (uiop:merge-pathnames* "test.dat" *this-pathname*))

#+swank
(defun run (&optional thing (out *standard-output*))
  "THING := null | string | symbol | pathname

null: run #'MAIN using the text on clipboard as input.
string: run #'MAIN using the string as input.
symbol: alias of FIVEAM:RUN!.
pathname: run #'MAIN using the text file as input."
  (let ((*standard-output* out))
    (etypecase thing
      (null
       (with-input-from-string (*standard-input* (delete #\Return (get-clipbrd)))
         (main)))
      (string
       (with-input-from-string (*standard-input* (delete #\Return thing))
         (main)))
      (symbol (5am:run! thing))
      (pathname
       (with-open-file (*standard-input* thing)
         (main))))))

#+swank
(defun gen-dat ()
  (uiop:with-output-file (out *dat-pathname* :if-exists :supersede)
    (format out "")))

#+swank
(defun bench (&optional (out (make-broadcast-stream)))
  (time (run *dat-pathname* out)))

;; To run: (5am:run! :sample)
#+swank
(it.bese.fiveam:test :sample
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "5 2
1 1 2 2 5
"
    "1
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "4 4
1 1 2 2
"
    "0
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "10 3
5 1 3 2 4 1 1 2 3 4
"
    "3
")))

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


(let((a 0)(b 0)(s(concatenate 'list(read-line))))(loop for c in s do(setq c(parse-integer(string c)))(setq a(+(* a 10)c))(incf b c))(princ(if(=(mod a b)0)"Yes""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