(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)

;;; Body

(defparameter *default-deque-size* 100)

(defstruct deque
  (data nil)
  (size nil)
  (head 0)
  (tail 0)
  (count 0))

(defun deque-create (&optional (size *default-deque-size*))
  (make-deque
   :size size
   :data (make-array size)))

(defmethod deque-clear ((d deque))
  (fill (deque-data d) 0)
  (setf (deque-size d) 0)
  (setf (deque-head d) 0)
  (setf (deque-tail d) 0)
  (setf (deque-count d) 0))

;; Subcommand

(declaim (inline deque-empty-p
                 deque-full-p
                 deque-get-prev-index
                 deque-get-next-index))

(defmethod deque-empty-p ((d deque))
  (zerop (deque-count d)))

(defmethod deque-full-p ((d deque))
  (= (deque-count d) (deque-size d)))

(defmethod deque-get-prev-index ((d deque) idx)
  (declare (inline deque-get-next-index))
  (if (zerop idx)
      (1- (deque-size d))
      (1- idx)))

(defmethod deque-get-next-index ((d deque) idx)
  (rem (1+ idx) (deque-size d)))

;;; Main command

(defmethod deque-pushfront ((d deque) item)
  (when (deque-full-p d)
    (error "deque is full"))

  (setf (deque-head d) (deque-get-prev-index d (deque-head d)))
  (setf (aref (deque-data d) (deque-head d)) item)
  (when (deque-empty-p d) ; first insersion
    (setf (deque-tail d) (deque-head d)))
  (incf (deque-count d)))

(defmethod deque-pushback ((d deque) item)
  (when (deque-full-p d)
    (error "deque is full"))

  (setf (deque-tail d) (deque-get-next-index d (deque-tail d)))
  (setf (aref (deque-data d) (deque-tail d)) item)
  (when (deque-empty-p d) ; first insersion
    (setf (deque-head d) (deque-tail d)))
  (incf (deque-count d)))

(defmethod deque-popfront ((d deque))
  (when (deque-empty-p d)
    (error "deque is empty,"))

  (let ((value (aref (deque-data d) (deque-head d))))
    (setf (deque-head d) (deque-get-next-index d (deque-head d)))
    (decf (deque-count d))
    value))

(defmethod deque-popback ((d deque))
  (when (deque-empty-p d)
    (error "deque is empty,"))

  (let ((value (aref (deque-data d) (deque-tail d))))
    (setf (deque-tail d) (deque-get-prev-index d (deque-tail d)))
    (decf (deque-count d))
    value))

(defmethod dref ((d deque) subscripts)
  (let ((arr (deque-data d))
        (size (deque-size d))
        (head (deque-head d)))
    (aref arr (mod (+ subscripts
                      head)
                   size))))

(defparameter *inf* 1000000)

(defparameter *dy-dx* '((1 . 0)
                        (-1 . 0)
                        (0 . 1)
                        (0 . -1)))

(defun bfs (sy sx h w board)
  (declare (fixnum sy sx h w)
           (array board))
  (the fixnum
       (let ((q (deque-create)))
         (deque-pushback q (cons sy sx))
         (setf (aref board sy sx) 0)
         (loop until (deque-empty-p q) do
              (let ((pos (deque-popfront q)))
                (loop for d in *dy-dx* do
                     (let ((ny (+ (first pos) (first d)))
                           (nx (+ (rest pos) (rest d))))
                       (when (and (<= 0 ny (1- h))
                                  (<= 0 nx (1- w))
                                  (= (aref board ny nx) *inf*))
                         (deque-pushback q (cons ny nx))
                         (setf (aref board ny nx)
                               (1+ (aref board (first pos) (rest pos))))))))
            finally
              (let ((cand -1))
                (declare (fixnum cand))
                (dotimes (y h)
                  (dotimes (x w)
                    (when (and (/= (aref board y x) -1)
                               (/= (aref board y x) *inf*))
                      (setf cand (max cand
                                      (aref board y x))))))
                (assert (plusp cand))
                (return cand))))))

(defun solve (h w board)
  (let ((res -1))
    (dotimes (i h)
      (dotimes (j w)
        (when (= (aref board i j) *inf*)
          (setf res (max res
                         (bfs i j h w board))))))
    res))

(defun main ()
  (declare #.OPT)
  (let ((h (read))
        (w (read))
        tmp)
    (let ((board (make-array `(,h ,w) :element-type 'fixnum)))
      (loop for y below h do
           (loop for x below w do
                (setf tmp (read-char))
                (if (char-equal tmp #\.)
                    (setf (aref board y x) *inf*)
                    (setf (aref board y x) -1))))
      (format t "~a~&" (solve h w board)))))

#-swank (main)

Score : 400 points

Problem Statement

Takahashi has a maze, which is a grid of H \times W squares with H horizontal rows and W vertical columns.

The square at the i-th row from the top and the j-th column is a "wall" square if S_{ij} is #, and a "road" square if S_{ij} is ..

From a road square, you can move to a horizontally or vertically adjacent road square.

You cannot move out of the maze, move to a wall square, or move diagonally.

Takahashi will choose a starting square and a goal square, which can be any road squares, and give the maze to Aoki.

Aoki will then travel from the starting square to the goal square, in the minimum number of moves required.

In this situation, find the maximum possible number of moves Aoki has to make.

Constraints

1 \leq H,W \leq 20

S_{ij} is . or #.

S contains at least two occurrences of ..

Any road square can be reached from any road square in zero or more moves.

Input

Input is given from Standard Input in the following format:

H W
S_{11}...S_{1W}
:
S_{H1}...S_{HW}

Output

Print the maximum possible number of moves Aoki has to make.

Sample Input 1

3 3
...
...
...

Sample Output 1

4

If Takahashi chooses the top-left square as the starting square and the bottom-right square as the goal square, Aoki has to make four moves.

Sample Input 2

3 5
...#.
.#.#.
.#...

Sample Output 2

10

If Takahashi chooses the bottom-left square as the starting square and the top-right square as the goal square, Aoki has to make ten moves.


#-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))))

(require 'sb-queue)

(defun get-shortest (h w map start-x start-y)
  (let ((q (sb-queue:make-queue :initial-contents (list (list start-x start-y 0)))))
    (loop until (sb-queue:queue-empty-p q)
          for (x y score) = (sb-queue:dequeue q)
          for next-score = (1+ score)
          do (progn
               (setf (aref map y x) score)
               ;; left
               (when (and (<= 0 (1- x))
                          (zerop (aref map y (1- x)))
                          (or (/= y start-y)
                              (/= (1- x) start-x)))
                 (sb-queue:enqueue (list (1- x) y next-score) q))
               ;; up
               (when (and (<= 0 (1- y))
                          (zerop (aref map (1- y) x))
                          (or (/= (1- y) start-y)
                              (/= x start-x)))
                 (sb-queue:enqueue (list x (1- y) next-score) q))
               ;; right
               (when (and (< (1+ x) w)
                          (zerop (aref map y (1+ x)))
                          (or (/= y start-y)
                              (/= (1+ x) start-x)))
                 (sb-queue:enqueue (list (1+ x) y next-score) q))
               ;; down
               (when (and (< (1+ y) h)
                          (zerop (aref map (1+ y) x))
                          (or (/= (1+ y) start-y)
                              (/= x start-x)))
                 (sb-queue:enqueue (list x (1+ y) next-score) q)))))
  (prog1
      (loop for y from 0 below h
            maximize (loop for x from 0 below w
                           if (or (= x 0)
                                  (= x (1- w))
                                  (= y 0)
                                  (= y (1- h)))
                           maximize (aref map y x)))
    (reset-map h w map)))

(defun solve (h w map)
  (loop for y from 0 below h
        maximize (loop for x from 0 below w
                       if (and (or (= x 0)
                                   (= x (1- w))
                                   (= y 0)
                                   (= y (1- h)))
                               (/= -1 (aref map y x)))
                       maximize (get-shortest h w map x y))))

(defun reset-map (h w map)
  (loop for y from 0 below h
        do (loop for x from 0 below w
                 if (/= -1 (aref map y x))
                 do (setf (aref map y x) 0))))

#-swank
(let* ((h (read))
       (w (read))
       (map (make-array (list h w) :element-type 'fixnum)))
  (loop for y from 0 below h
        do (loop for x from 0 below w
                 for char = (read-char)
                 do (setf (aref map y x)
                          (if (char= char #\.)
                              0
                              -1)))
        do (read-char))
  (format t "~A~%" (solve h w map)))

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

Submitted source-code file size, in bytes.

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

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


;; -*- 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 warshall-floyd!))
(defun warshall-floyd! (matrix)
  (let ((n (array-dimension matrix 0)))
    (dotimes (k n)
      (let ((base-k (array-row-major-index matrix k 0)))
        (dotimes (i n)
          (let ((base-i (array-row-major-index matrix i 0)))
            (dotimes (j n)
              (setf (row-major-aref matrix (+ base-i j))
                    (min (row-major-aref matrix (+ base-i j))
                         (+ (row-major-aref matrix (+ base-i k))
                            (row-major-aref matrix (+ base-k j)))))))))))
  matrix)

(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* ((h (read))
         (w (read))
         (plan (make-array (list h w) :element-type 'bit :initial-element 0))
         (mat (make-array (list (* h w) (* h w)) :element-type 'uint32 :initial-element #xffffffff)))
    (dotimes (i h)
      (let ((line (read-line)))
        (dotimes (j w)
          (when (char= #\# (aref line j))
            (setf (aref plan i j) 1)))))
    (labels ((encode (y x)
               (+ x (* y w)))
             (connect (y1 x1 y2 x2)
               (when (and (<= 0 y2 (- h 1))
                          (<= 0 x2 (- w 1))
                          (zerop (aref plan y2 x2)))
                 (let ((v1 (encode y1 x1))
                       (v2 (encode y2 x2)))
                   (setf (aref mat v1 v2) 1)))))
      (dotimes (i h)
        (dotimes (j w)
          (when (zerop (aref plan i j))
            (setf (aref mat (encode i j) (encode i j)) 0)
            (connect i j (- i 1) j)
            (connect i j (+ i 1) j)
            (connect i j i (- j 1))
            (connect i j i (+ j 1)))))
      (warshall-floyd! mat)
      (let ((res 0))
        (dotimes (y1 h)
          (dotimes (x1 w)
            (dotimes (y2 h)
              (dotimes (x2 w)
                (when (and (zerop (aref plan y1 x1))
                           (zerop (aref plan y2 x2)))
                  (let ((dist (aref mat (encode y1 x1) (encode y2 x2))))
                    (when (< dist #xffffffff)
                      (setq res (max res dist)))))))))
        (println res)))))

#-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 "3 3
...
...
...
"
    "4
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "3 5
...#.
.#.#.
.#...
"
    "10
")))


;; -*- 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
(macrolet ((def (name fname)
             `(define-modify-macro ,name (new-value) ,fname)))
  (def minf min)
  (def maxf max)
  (def mulf *)
  (def divf /)
  (def iorf logior)
  (def xorf logxor)
  (def andf logand))

(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
;;;

;; http://www.ambrsoft.com/trigocalc/circle3d.htm
;; FIXME: more sane handling of degeneracy
(declaim (inline calc-circumcenter))
(defun calc-circumcenter (p1 p2 p3)
  "Returns the center of circumcirlce if it exists, otherwise returns NIL."
  (declare (complex p1 p2 p3))
  (let* ((x1 (realpart p1))
         (y1 (imagpart p1))
         (x2 (realpart p2))
         (y2 (imagpart p2))
         (x3 (realpart p3))
         (y3 (imagpart p3))
         (a (+ (* x1 (- y2 y3))
               (- (* y1 (- x2 x3)))
               (* x2 y3)
               (- (* x3 y2))))
         (b (+ (* (+ (* x1 x1) (* y1 y1)) (- y3 y2))
               (* (+ (* x2 x2) (* y2 y2)) (- y1 y3))
               (* (+ (* x3 x3) (* y3 y3)) (- y2 y1))))
         (c (+ (* (+ (* x1 x1) (* y1 y1)) (- x2 x3))
               (* (+ (* x2 x2) (* y2 y2)) (- x3 x1))
               (* (+ (* x3 x3) (* y3 y3)) (- x1 x2)))))
    (handler-bind ((error (lambda (c)
                            (declare (ignore c))
                            (return-from calc-circumcenter nil))))
      (complex (- (/ b (* 2 a)))
               (- (/ c (* 2 a)))))))

(defun mini-disc-with-2-points (points end q1 q2 eps)
  "Returns the smallest circle that contains points, q1 and q2 (contains q1 and
q2 at the perimeter)."
  (declare (complex q1 q2)
           (double-float eps)
           ((integer 0 #.most-positive-fixnum) end))
  (let* ((center (* 1/2 (+ q1 q2)))
         (radius (abs (- q1 center))))
    (declare (complex center)
             (double-float radius))
    (dotimes (i end)
      (let ((new-point (aref points i)))
        (declare (complex new-point))
        (when (>= (abs (- new-point center)) (+ radius eps))
          (let ((new-center (calc-circumcenter q1 q2 new-point)))
            (setq center new-center
                  radius (abs (- new-point new-center)))))))
    (values center radius)))

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

(defun mini-disc-with-point (points end q eps)
  (declare (complex q)
           (double-float eps))
  (%shuffle! points end)
  (let* ((center (* 1/2 (+ (aref points 0) q)))
         (radius (abs (- q center))))
    (declare (complex center)
             (double-float radius))
    (loop for i from 1 below end
          for new-point of-type complex = (aref points i)
          when (>= (abs (- new-point center)) (+ radius eps))
          do (setf (values center radius)
                   (mini-disc-with-2-points points i (aref points i) q eps)))
    (values center radius)))

(defun calc-smallest-circle (points eps)
  (assert (>= (length points) 1))
  (when (= 1 (length points))
    (return-from calc-smallest-circle
      (values (aref points 0)
              (coerce 0 (type-of (realpart (aref points 0)))))))
  (let* ((points (copy-seq (shuffle! points)))
         (copy (copy-seq points))
         (p0 (aref points 0))
         (p1 (aref points 1))
         (center (* 1/2 (+ p0 p1)))
         (radius (abs (- p0 center))))
    (loop for i from 2 below (length points)
          for new-point = (aref points i)
          when (>= (abs (- new-point center)) (+ radius eps))
          do (setf (values center radius)
                   (mini-disc-with-point copy i new-point eps)))
    (values center radius)))

(defun main ()
  (let* ((n (read))
         (points (make-array n :element-type '(complex double-float))))
    (declare (uint16 n))
    (dotimes (i n)
      (let ((x (float (read) 1d0))
            (y (float (read) 1d0)))
        (setf (aref points i) (complex x y))))
    (println (nth-value 1 (calc-smallest-circle points 1d-9)))))

#-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 "2
0 0
1 0
"
    "0.500000000000000000
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "3
0 0
0 1
1 0
"
    "0.707106781186497524
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "10
10 9
5 9
2 0
0 0
2 7
3 3
2 5
10 0
3 7
1 9
"
    "6.726812023536805158
")))

Score : 600 points

Problem Statement

Given are N points (x_i, y_i) in a two-dimensional plane.

Find the minimum radius of a circle such that all the points are inside or on it.

Constraints

2 \leq N \leq 50

0 \leq x_i \leq 1000

0 \leq y_i \leq 1000

The given N points are all different.

The values in input are all integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the minimum radius of a circle such that all the N points are inside or on it.

Your output will be considered correct if the absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

2
0 0
1 0

Sample Output 1

0.500000000000000000

Both points are contained in the circle centered at (0.5,0) with a radius of 0.5.

Sample Input 2

3
0 0
0 1
1 0

Sample Output 2

0.707106781186497524

Sample Input 3

10
10 9
5 9
2 0
0 0
2 7
3 3
2 5
10 0
3 7
1 9

Sample Output 3

6.726812023536805158

If the absolute or relative error from our answer is at most 10^{-6}, the output will be considered correct.


(defun split (string &key (delimiterp #'(lambda (c) (char= c #\Space))))
  (loop :for beg = (position-if-not delimiterp string)
        :then (position-if-not delimiterp string :start (1+ end))
        :for end = (and beg (position-if delimiterp string :start beg))
        :when beg :collect (subseq string beg end)
        :while end))

(defun main ()
  (let* ((n (parse-integer (read-line)))
         (st (loop for i from 0 below n
                   collect (split (read-line))))
         (x (read-line)))
    (format t "~d~%" (loop for s in st
                           for remain on st
                           do (when (string= x (car s))
                                (return (loop for y in (cdr remain)
                                              summing (parse-integer (cadr y)))))))))

(main)

Score : 200 points

Problem Statement

Niwango created a playlist of N songs.
The title and the duration of the i-th song are s_i and t_i seconds, respectively.
It is guaranteed that s_1,\ldots,s_N are all distinct.

Niwango was doing some work while playing this playlist. (That is, all the songs were played once, in the order they appear in the playlist, without any pause in between.)
However, he fell asleep during his work, and he woke up after all the songs were played.
According to his record, it turned out that he fell asleep at the very end of the song titled X.

Find the duration of time when some song was played while Niwango was asleep.

Constraints

1 \leq N \leq 50

s_i and X are strings of length between 1 and 100 (inclusive) consisting of lowercase English letters.

s_1,\ldots,s_N are distinct.

There exists an integer i such that s_i = X.

1 \leq t_i \leq 1000

t_i is an integer.

Input

Input is given from Standard Input in the following format:

N
s_1 t_1
\vdots
s_{N} t_N
X

Output

Print the answer.

Sample Input 1

3
dwango 2
sixth 5
prelims 25
dwango

Sample Output 1

30

While Niwango was asleep, two songs were played: sixth and prelims.

The answer is the total duration of these songs, 30.

Sample Input 2

1
abcde 1000
abcde

Sample Output 2

0

No songs were played while Niwango was asleep.

In such a case, the total duration of songs is 0.

Sample Input 3

15
ypnxn 279
kgjgwx 464
qquhuwq 327
rxing 549
pmuduhznoaqu 832
dagktgdarveusju 595
wunfagppcoi 200
dhavrncwfw 720
jpcmigg 658
wrczqxycivdqn 639
mcmkkbnjfeod 992
htqvkgkbhtytsz 130
twflegsjz 467
dswxxrxuzzfhkp 989
szfwtzfpnscgue 958
pmuduhznoaqu

Sample Output 3

6348


;; -*- 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
(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))
         (ss (make-array n))
         (ts (make-array n)))
    (dotimes (i n)
      (setf (aref ss i) (read)
            (aref ts i) (read)))
    (let* ((x (read))
           (pos (position x ss)))
      (println (loop for i from (+ pos 1) below n
                     sum (aref ts i))))))

#-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 "3
dwango 2
sixth 5
prelims 25
dwango
"
    "30
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "1
abcde 1000
abcde
"
    "0
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "15
ypnxn 279
kgjgwx 464
qquhuwq 327
rxing 549
pmuduhznoaqu 832
dagktgdarveusju 595
wunfagppcoi 200
dhavrncwfw 720
jpcmigg 658
wrczqxycivdqn 639
mcmkkbnjfeod 992
htqvkgkbhtytsz 130
twflegsjz 467
dswxxrxuzzfhkp 989
szfwtzfpnscgue 958
pmuduhznoaqu
"
    "6348
")))


#-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))))

(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))))

(defun fact (n)
  (if (= n 1)
      1
      (* n (fact (1- n)))))

(defparameter N (parse-integer (read-line)))
(defparameter lst
  (mapcar #'parse-integer (split " " (read-line))))
(defparameter const (+ 7 (expt 10 9)))
(defparameter dists
  (loop for i from 0 below (1- N)
        collect (- (nth (1+ i) lst) (nth i lst))))
(defparameter f (fact (1- N)))
(defparameter coeff
  (loop for i from 1 below N
        collect (apply #'+
                       (loop for j from 1 to i
                             collect (/ 1 j)))))

(defun add-lst (lst1 lst2)
  (if (null lst1)
      0
      (+ (* (car lst1) (car lst2))
         (add-lst (cdr lst1) (cdr lst2)))))

(format t "~A" (mod (* f (add-lst coeff dists)) const))

Score : 600 points

Problem Statement

There are N slimes standing on a number line.
The i-th slime from the left is at position x_i.

It is guaruanteed that 1 \leq x_1 < x_2 < \ldots < x_N \leq 10^{9}.

Niwango will perform N-1 operations. The i-th operation consists of the following procedures:

Choose an integer k between 1 and N-i (inclusive) with equal probability.

Move the k-th slime from the left, to the position of the neighboring slime to the right.

Fuse the two slimes at the same position into one slime.

Find the total distance traveled by the slimes multiplied by (N-1)! (we can show that this value is an integer), modulo (10^{9}+7). If a slime is born by a fuse and that slime moves, we count it as just one slime.

Constraints

2 \leq N \leq 10^{5}

1 \leq x_1 < x_2 < \ldots < x_N \leq 10^{9}

x_i is an integer.

Subtasks

400 points will be awarded for passing the test cases satisfying N \leq 2000.

Input

Input is given from Standard Input in the following format:

N
x_1 x_2 \ldots x_N

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

5

With probability \frac{1}{2}, the leftmost slime is chosen in the first operation, in which case the total distance traveled is 2.

With probability \frac{1}{2}, the middle slime is chosen in the first operation, in which case the total distance traveled is 3.

The answer is the expected total distance traveled, 2.5, multiplied by 2!, which is 5.

Sample Input 2

12
161735902 211047202 430302156 450968417 628894325 707723857 731963982 822804784 880895728 923078537 971407775 982631932

Sample Output 2

750927044

Find the expected value multiplied by (N-1)!, modulo (10^9+7).


(defvar *cache*)

(defun fact (n)
  (loop with result = 1
        for i from 1 to n
        do (setf result
                 (* result i))
        finally (return result)))

(defun solve (n xs)
  (labels ((solve% (n xs)
             (or (gethash xs *cache*)
                 (setf (gethash xs *cache*)
                       (if (= n 1)
                           0
                           (loop with past = ()
                                 for (x y . rest) on xs
                                 while y
                                 summing (/ (+ (- y x)
                                               (solve% (1- n)
                                                       (nconc (reverse past) (cons y rest))))
                                            (1- n))
                                 do (setf past
                                          (cons x past))))))))
    (let ((*cache* (make-hash-table :test 'equal)))
      (rem (* (solve% n xs) (fact (1- n)))
           (+ (expt 10 9) 7)))))

#-swank
(let* ((n (read))
       (xs (loop repeat n collect (read))))
  (format t "~A~%" (solve n xs)))


Score : 100 points

Problem Statement

Takahashi has K 500-yen coins. (Yen is the currency of Japan.)
If these coins add up to X yen or more, print Yes; otherwise, print No.

Constraints

1 \leq K \leq 100

1 \leq X \leq 10^5

Input

Input is given from Standard Input in the following format:

K X

Output

If the coins add up to X yen or more, print Yes; otherwise, print No.

Sample Input 1

2 900

Sample Output 1

Yes

Two 500-yen coins add up to 1000 yen, which is not less than X = 900 yen.

Sample Input 2

1 501

Sample Output 2

No

One 500-yen coin is worth 500 yen, which is less than X = 501 yen.

Sample Input 3

4 2000

Sample Output 3

Yes

Four 500-yen coins add up to 2000 yen, which is not less than X = 2000 yen.


(let ((k (read))
      (x (read)))
  (if (>= (* k 500) x)
      (princ "Yes")
      (princ "No")))


;; -*- 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
(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* ((k (read))
         (x (read)))
    (write-line (if (>= (* 500 k) x)
                    "Yes"
                    "No"))))

#-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 "2 900
"
    "Yes
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "1 501
"
    "No
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "4 2000
"
    "Yes
")))


(defun make-acceptor (string)
  (let ((num 0)
        (pos 0))
  (lambda (c)
    (if (null c)
        num
        (cond ((and (= pos (1- (length string))) (char= c (char string pos)))
               (incf num)
               (setf pos 0))
              ((char= c (char string pos))
               (incf pos))
              (t
               (setf pos 0)))))))

(let ((n (read))
      (x (make-acceptor "ABC")))
  (loop :for i :from 1 :to n
        :for c := (read-char)
        :do (funcall x c))
  (format t "~A~%"  (funcall x nil)))

Score : 200 points

Problem Statement

We have a string S of length N consisting of uppercase English letters.

How many times does ABC occur in S as contiguous subsequences (see Sample Inputs and Outputs)?

Constraints

3 \leq N \leq 50

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print number of occurrences of ABC in S as contiguous subsequences.

Sample Input 1

10
ZABCDBABCQ

Sample Output 1

2

Two contiguous subsequences of S are equal to ABC: the 2-nd through 4-th characters, and the 7-th through 9-th characters.

Sample Input 2

19
THREEONEFOURONEFIVE

Sample Output 2

0

No contiguous subsequences of S are equal to ABC.

Sample Input 3

33
ABCCABCBABCCABACBCBBABCBCBCBCABCB

Sample Output 3

5


(let ((n (read))
      (s (read-line)))
  (format t "~a~%"
          (search "ABC" s)))


(defun split (delimiter target-string)
  (let ((delimiter-position (search delimiter target-string))
        (delimiter-size (length delimiter)))
    (if delimiter-position
        (cons (subseq target-string 0 delimiter-position)
              (split delimiter (subseq target-string (+ delimiter-position delimiter-size))))
        (list target-string))))

(let ((n (read))
      (l (split "ABC" (read-line))))
  (princ (- (length l) 1)))


(defun check_A (x)
  (when (and x (char= (car x) #\A))
			 (check_B (cdr x))))
(defun check_B (x)
  (when (and x (char= (car x) #\B))
			 (check_C (cdr x))))
(defun check_C (x)
  (and x (char= (car x) #\C)))

(defun cnt (x)
  (if x
	(+ (if (check_A x) 1 0) (cnt (cdr x)))
	0))

(defparameter n (read))
(let ((str (concatenate 'list (read-line))))
  (princ (cnt str)))


(defun split (input stack-string output-list)
  (let ((chara (car input)))
    (case chara
          ((nil) (append output-list (list stack-string)))
          (#\space (split (cdr input)
                                  '()
                                  (append output-list (list stack-string))))
          (otherwise (split (cdr input)
                                    (concatenate 'string stack-string (list chara))
                                    output-list)))))

(defun input-to-list (input)
  (mapcar #'parse-integer (split (concatenate 'list input)
                                 '()
                                 '())))

(defun fact (n)
  (labels ((inner (mem n)
                  (if (zerop n)
                      mem
                      (inner (* mem n) (1- n)))
                  ))
          (inner 1 n)))

(defun dec (lst n)
  (+ (* (fact (1- n)) (1- (car lst))) (dec (cdr lst) (1- n)))

(defvar n (parse-integer (read-line)))
(defvar p (input-to-list (read-line)))
(defvar q (input-to-list (read-line)))

(princ (abs (- (dec p n) (dec q n))))

Score : 300 points

Problem Statement

We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).

There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Notes

For two sequences X and Y, X is said to be lexicographically smaller than Y if and only if there exists an integer k such that X_i = Y_i~(1 \leq i < k) and X_k < Y_k.

Constraints

2 \leq N \leq 8

P and Q are permutations of size N.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 ... P_N
Q_1 Q_2 ... Q_N

Output

Print |a - b|.

Sample Input 1

3
1 3 2
3 1 2

Sample Output 1

3

There are 6 permutations of size 3: (1,~2,~3), (1,~3,~2), (2,~1,~3), (2,~3,~1), (3,~1,~2), and (3,~2,~1). Among them, (1,~3,~2) and (3,~1,~2) come 2-nd and 5-th in lexicographical order, so the answer is |2 - 5| = 3.

Sample Input 2

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

Sample Output 2

17517

Sample Input 3

3
1 2 3
1 2 3

Sample Output 3

0


#|
------------------------------------
|               Utils               |
------------------------------------
|#

(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)

#-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))))

(defconstant +mod+ 1000000007)

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~a" b)) () '(signed-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~a" b)) () '(unsigned-byte ,b))) bits)))

(define-int-types 2 4 8 16 32 64)

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1
        (write obj :stream stream)
      (fresh-line stream))))

(defmacro safe-sort (list test &key (key #'identity))
  `(progn
    (declaim (inline sort sb-impl::stable-sort-list))
    (sort (copy-seq ,list) ,test :key ,key)))

(defmacro read-numbers-to-list (size)
  `(loop repeat ,size collect (read)))

(defmacro read-numbers-to-array (size)
  `(make-array ,size :initial-contents :element-type 'fixnum (loop repeat ,size collect (read))))

(defmacro read-characters-to-board (row-size column-size)
  (let ((board (gensym))
        (r (gensym))
        (c (gensym))
        (tmp (gensym)))
    `(let ((,board (make-array '(,row-size ,column-size) :element-type 'character :adjustable nil)))
       (dotimes (,r ,row-size ,board)
         (let ((,tmp (read-line)))
           (dotimes (,c ,column-size)
             (setf (aref ,board ,r ,c) (char ,tmp ,c))))))))

(defun princ-for-each-line (list)
  (format t "~{~a~&~}" list))

(defun unwrap (list)
  (format nil "~{~a~^ ~}" list))

(defmacro with-buffered-stdout (&body body)
  (let ((out (gensym)))
    `(let ((,out (make-string-output-stream :element-type 'base-char)))
       (let ((*standard-output* ,out))
         ,@body)
       (write-string (get-output-stream-string ,out)))))

#|
------------------------------------
|               Body               |
------------------------------------
|#

(defun divide-judge (xs)
  (declare (list xs))
  (cond
    ((every #'oddp xs) t)
    ((every #'evenp xs) (divide-judge (mapcar (lambda (x) (floor x 2)) xs)))
    (t nil)))

(defun solve (m a)
  (let ((xb (mapcar (lambda (x) (floor x 2)) a)))
    (if (not (divide-judge xb))
        0
        (let ((p (reduce #'lcm xb)))
          (ceiling (- m p) (* p 2))))))

(defun main ()
  (declare #.OPT)
  (let ((n (read))
        (m (read)))
    (let ((a (sort (read-numbers-to-list n) #'<)))
      (format t "~a~&" (solve m a)))))

#-swank (main)

Score : 400 points

Problem Statement

Given are a sequence A= {a_1,a_2,......a_N} of N positive even numbers, and an integer M.

Let a semi-common multiple of A be a positive integer X that satisfies the following condition for every k (1 \leq k \leq N):

There exists a non-negative integer p such that X= a_k \times (p+0.5).

Find the number of semi-common multiples of A among the integers between 1 and M (inclusive).

Constraints

1 \leq N \leq 10^5

1 \leq M \leq 10^9

2 \leq a_i \leq 10^9

a_i is an even number.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
a_1 a_2 ... a_N

Output

Print the number of semi-common multiples of A among the integers between 1 and M (inclusive).

Sample Input 1

2 50
6 10

Sample Output 1

2

15 = 6 \times 2.5

15 = 10 \times 1.5

45 = 6 \times 7.5

45 = 10 \times 4.5

Thus, 15 and 45 are semi-common multiples of A. There are no other semi-common multiples of A between 1 and 50, so the answer is 2.

Sample Input 2

3 100
14 22 40

Sample Output 2

0

The answer can be 0.

Sample Input 3

5 1000000000
6 6 2 6 2

Sample Output 3

166666667


#-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))))

(defparameter n (read))
(defparameter m (read))

(let* ((a (loop for i below n collect (read)))
	   (x (reduce (lambda (x y) (let ((res (lcm x (floor y 2))))
								  (if (> res m) (* 10 m) res)))
				  a
				  :initial-value 1)))
  (princ (floor (+ m x) (* x 2))))


#-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))))

(defparameter n (read))
(defparameter m (read))

(let* ((a (loop for i below n collect (read)))
	   (x (reduce (lambda (x y) (let ((res (lcm x (/ y 2))))
								  (if (> res m) (* 10 m) res)))
				  a
				  :initial-value 1)))
  (princ (floor (+ m x) (* x 2))))


;; F - Xor Shift

(defun main ()
  (let* ((N (read))
         (a (loop repeat N collect (read-fixnum)))
         (b (loop repeat N collect (read-fixnum))))
    (loop for kx in (solve N a b)
          do (format t "~{~a~^ ~}~%" kx))))

(defun solve (N a-lst b-lst)
  (let ((a (make-array N :initial-contents a-lst))
        (b (make-array N :initial-contents b-lst))
        (a-xor (make-array N)) ; 直前の要素とのxor (循環)
        (b-xor (make-array (1- N)))) ; 〃 (先頭を除く)
    ; (k,x)が解 ⇔ a-xor[k:]がb-xorと一致, x = b[0] XOR a[k]
    ; a-xor, b-xorを構築
    (loop for i below N
      do (setf (aref a-xor i) (logxor (aref a i) (aref a (mod (1+ i) N)))))
    (loop for i below (1- N)
      do (setf (aref b-xor i) (logxor (aref b i) (aref b (1+ i)))))
    ; a-xor * 2中のb-xorと一致する位置をKMP法で探す
    (loop for k in (kmp-search a-xor b-xor)
          for x = (logxor (aref a (mod k N)) (aref b 0))
          when (< k N)
          collect (list k x))))

(defun kmp-search (text pattern)
  "text * 2中のpatternに一致する位置をすべて返す."
  (let ((n (array-dimension text 0))
        (m (array-dimension pattern 0))
        (pref (compute-prefix-function pattern)))
    (loop with q = 0
          for i from 0 below (* 2 n)
          for c = (aref text (mod i n))
          do (loop until (or (zerop q) (eql (aref pattern q) c))
                   do (setf q (aref pref q))) ; next char does not match
          when (eql (aref pattern q) c)
          do (incf q) end ; next char matches
          when (= q m)
          collect (- i (1- m))
          and do (setf q (aref pref q))))) ; look for the next match

(defun compute-prefix-function (pattern)
  "pref : {1,2,...,m} -> {0,1,...,m-1}
   pref[q] = max {k : k < q and P[:q].endswith(P[:k])}"
  (let* ((m (array-dimension pattern 0))
         (pref (make-array (1+ m) :initial-element 0)))
    (loop with k = 0
          for q from 1 below m
          for c = (aref pattern q)
          do (loop until (or (zerop k) (eql (aref pattern k) c))
                   do (setf k (aref pref k)))
          when (eql (aref pattern k) c)
          do (incf k) end
          do (setf (aref pref (1+ q)) k))
    pref))

(defun read-fixnum (&optional (in *standard-input*))
  "readより速い非負整数専用read (整数間に空白または改行1個に限る)
   cf. https://qiita.com/sansaqua/items/0b6417cb541047e29da4"
  (loop with acc = 0
        for byte = (read-byte in)
        if (<= 48 byte 57)
          do (setf acc (+ (* acc 10) (- byte 48)))
        else return acc))

(main)

Score : 600 points

Problem Statement

Given are two sequences a=\{a_0,\ldots,a_{N-1}\} and b=\{b_0,\ldots,b_{N-1}\} of N non-negative integers each.

Snuke will choose an integer k such that 0 \leq k < N and an integer x not less than 0, to make a new sequence of length N, a'=\{a_0',\ldots,a_{N-1}'\}, as follows:

a_i'= a_{i+k \mod N}\ XOR \ x

Find all pairs (k,x) such that a' will be equal to b.

What is \mbox{ XOR }?

The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows:

When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise.

For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)

Constraints

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

0 \leq a_i,b_i < 2^{30}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_0 a_1 ... a_{N-1}
b_0 b_1 ... b_{N-1}

Output

Print all pairs (k, x) such that a' and b will be equal, using one line for each pair, in ascending order of k (ascending order of x for pairs with the same k).

If there are no such pairs, the output should be empty.

Sample Input 1

3
0 2 1
1 2 3

Sample Output 1

1 3

If (k,x)=(1,3),

a_0'=(a_1\ XOR \ 3)=1

a_1'=(a_2\ XOR \ 3)=2

a_2'=(a_0\ XOR \ 3)=3

and we have a' = b.

Sample Input 2

5
0 0 0 0 0
2 2 2 2 2

Sample Output 2

0 2
1 2
2 2
3 2
4 2

Sample Input 3

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

Sample Output 3

2 2
5 5

Sample Input 4

2
1 2
0 0

Sample Output 4

No pairs may satisfy the condition.


;; -*- 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
(defmacro with-buffered-stdout (&body body)
  "Buffers all outputs to *STANDARD-OUTPUT* in BODY and flushes them to
*STANDARD-OUTPUT* after BODY has been done (without error). Note that only
BASE-CHAR is allowed."
  (let ((out (gensym)))
    `(let ((,out (make-string-output-stream :element-type 'base-char)))
       (let ((*standard-output* ,out))
         ,@body)
       (write-string (get-output-stream-string ,out)))))

(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  "NOTE: cannot read -2^62"
  (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))))))))

;;;
;;; Rolling hash (62-bit)
;;;

;; Reference:
;; https://www.mii.lt/olympiads_in_informatics/pdf/INFOL119.pdf
;; https://ei1333.github.io/luzhiled/snippets/string/rolling-hash.html

(defstruct (rhash (:constructor %make-rhash (cumul1 powers1 cumul2 powers2)))
  ;; lower 31-bit value
  (cumul1 nil :type (simple-array (unsigned-byte 31) (*)))
  (powers1 nil :type (simple-array (unsigned-byte 31) (*)))
  ;; upper 31-bit value
  (cumul2 nil :type (simple-array (unsigned-byte 31) (*)))
  (powers2 nil :type (simple-array (unsigned-byte 31) (*))))

(eval-when (:compile-toplevel :load-toplevel :execute)
  ;; This table consists of pairs of primes less than 2^31 and the random
  ;; primitive roots modulo them larger than 10^9. We randomly choose a pair and
  ;; adopt the prime as modulus and the primitive root as base.
  (declaim ((simple-array (unsigned-byte 31) (100)) *moduli-table* *base-table*))
  (defparameter *moduli-table*
    (make-array 100 :element-type '(unsigned-byte 31)
                    :initial-contents '(2147483647 2147483629 2147483587 2147483579 2147483563 2147483549 2147483543
 2147483497 2147483489 2147483477 2147483423 2147483399 2147483353 2147483323
 2147483269 2147483249 2147483237 2147483179 2147483171 2147483137 2147483123
 2147483077 2147483069 2147483059 2147483053 2147483033 2147483029 2147482951
 2147482949 2147482943 2147482937 2147482921 2147482877 2147482873 2147482867
 2147482859 2147482819 2147482817 2147482811 2147482801 2147482763 2147482739
 2147482697 2147482693 2147482681 2147482663 2147482661 2147482621 2147482591
 2147482583 2147482577 2147482507 2147482501 2147482481 2147482417 2147482409
 2147482367 2147482361 2147482349 2147482343 2147482327 2147482291 2147482273
 2147482237 2147482231 2147482223 2147482121 2147482093 2147482091 2147482081
 2147482063 2147482021 2147481997 2147481967 2147481949 2147481937 2147481907
 2147481901 2147481899 2147481893 2147481883 2147481863 2147481827 2147481811
 2147481797 2147481793 2147481673 2147481629 2147481571 2147481563 2147481529
 2147481509 2147481499 2147481491 2147481487 2147481373 2147481367 2147481359
 2147481353 2147481337)))
  (defparameter *base-table*
    (make-array 100 :element-type '(unsigned-byte 31)
                    :initial-contents '(1059428526 2090066834 1772913519 1695158082 1516083910 1622025757 1248368302
 1894391153 2094976878 1193495823 1783230399 1520742486 1748395380 1703688443
 2138630366 1942049269 2066548889 1890950855 1480056952 1792721876 1092797280
 1204851872 1035383130 1002272185 1319736653 1980774767 1748793187 1866963602
 1200445534 1732959733 1214706585 1957228822 1479411729 1323155655 1052714514
 1989821027 1163834549 1095622874 2087901566 1670886084 1191975321 2091468260
 1429690292 1116037844 1420457779 1937649612 1552519679 1328604092 2090326292
 1397132095 1316705322 1664351025 1391513321 1851038917 1556301575 1928956735
 1764506480 1449537491 2119470570 1793768237 1831208371 1723755364 1643456516
 1993819805 1419297891 1755252963 1775153034 1388979165 2144586633 1501222238
 1872274033 1143076711 1229125474 1483974015 1997206147 1593231852 1632083893
 1601537043 2012194627 1299923971 1566635240 1814404069 1619988648 2072686565
 2014361572 1213868607 1166967329 1009325840 1306167671 1915239658 1223190075
 1821151471 2037700892 1646950698 1517859810 1099233635 1004913731 1653443892
 1782112665 1018916580)))

  (defun %choose-moduli (&optional mod1 mod2 base1 base2)
    "Chooses two appropriate pairs of moduli and bases."
    (declare ((or null (unsigned-byte 31)) mod1 mod2 base1 base2))
    (let* ((rand1 (random (length *moduli-table*)))
           (rand2 (loop (let ((tmp (random (length *moduli-table*))))
                          (unless (= tmp rand1)
                            (return tmp))))))
      (if mod1
          (progn
            #+sbcl (assert (sb-int:positive-primep mod1))
            (setq base1 (or base1 (+ 1 (random (- mod1 1))))))
          (progn
            (setq mod1 (or mod1 (aref *moduli-table* rand1)))
            (if base1
                (assert (<= 1 base1 (- mod1 1)))
                (setq base1 (aref *base-table* rand1)))))
      (if mod2
          (progn
            #+sbcl (assert (sb-int:positive-primep mod2))
            (setq base2 (or base2 (+ 1 (random (- mod2 1))))))
          (progn
            (setq mod2 (or mod2 (aref *moduli-table* rand2)))
            (if base2
                (assert (<= 1 base2 (- mod2 1)))
                (setq base2 (aref *base-table* rand2))))))
    (values mod1 mod2 base1 base2))

  (unless (and (boundp '+rhash-mod1+)
               (boundp '+rhash-mod2+)
               (boundp '+rhash-base1+)
               (boundp '+rhash-base2+))
    (multiple-value-bind (mod1 mod2 base1 base2) (%choose-moduli)
      (defconstant +rhash-mod1+ mod1)
      (defconstant +rhash-mod2+ mod2)
      (defconstant +rhash-base1+ base1)
      (defconstant +rhash-base2+ base2))))

;; KLUDGE: Type derivation of MOD fails in some cases on SBCL. See
;; https://bugs.launchpad.net/sbcl/+bug/1843108
(declaim (inline %mod))
(defun %mod (number divisor)
  (nth-value 1 (floor number divisor)))

(declaim (inline make-rhash))
(defun make-rhash (vector &key (key #'char-code))
  "Returns the table of rolling-hash of VECTOR modulo +RHASH-MOD1+ and
+RHASH-MOD2+. KEY is applied to each element of VECTOR prior to computing the
hash value.

KEY := FUNCTION returning FIXNUM"
  (declare (optimize (speed 3))
           (vector vector)
           (function key))
  (let* ((size (length vector))
         (cumul1 (make-array (+ 1 size) :element-type '(unsigned-byte 31)))
         (powers1 (make-array (+ 1 size) :element-type '(unsigned-byte 31)))
         (cumul2 (make-array (+ 1 size) :element-type '(unsigned-byte 31)))
         (powers2 (make-array (+ 1 size) :element-type '(unsigned-byte 31))))
    (setf (aref powers1 0) 1
          (aref powers2 0) 1)
    (dotimes (i size)
      (setf (aref powers1 (+ i 1))
            (%mod (* (aref powers1 i) +rhash-base1+) +rhash-mod1+)
            (aref powers2 (+ i 1))
            (%mod (* (aref powers2 i) +rhash-base2+) +rhash-mod2+))
      (let ((sum1 (+ (%mod (* +rhash-base1+ (aref cumul1 i)) +rhash-mod1+)
                     (%mod (the fixnum (funcall key (aref vector i))) +rhash-mod1+)))
            (sum2 (+ (%mod (* +rhash-base2+ (aref cumul2 i)) +rhash-mod2+)
                     (%mod (the fixnum (funcall key (aref vector i))) +rhash-mod2+))))
        (setf (aref cumul1 (+ i 1)) (if (>= sum1 +rhash-mod1+)
                                        (- sum1 +rhash-mod1+)
                                        sum1)
              (aref cumul2 (+ i 1)) (if (>= sum2 +rhash-mod2+)
                                        (- sum2 +rhash-mod2+)
                                        sum2))))
    (%make-rhash cumul1 powers1 cumul2 powers2)))

(declaim (ftype (function * (values (unsigned-byte 62) &optional)) rhash-vector-hash)
         (inline rhash-vector-hash))
(defun rhash-vector-hash (vector &key (key #'char-code))
  "Returns the hash code of VECTOR w.r.t. the moduli and bases of RHASH."
  (declare (optimize (speed 3))
           (vector vector)
           (function key))
  (let* ((size (length vector))
         (lower 0)
         (upper 0))
    (declare ((unsigned-byte 31) lower upper))
    (dotimes (i size)
      (setf lower (%mod (+ (* +rhash-base1+ lower)
                          (%mod (the fixnum (funcall key (aref vector i))) +rhash-mod1+))
                       +rhash-mod1+))
      (setf upper (%mod (+ (* +rhash-base2+ upper)
                          (%mod (the fixnum (funcall key (aref vector i))) +rhash-mod2+))
                       +rhash-mod2+)))
    (dpb upper (byte 31 31) lower)))

(declaim (inline rhash-query)
         (ftype (function * (values (unsigned-byte 62) &optional)) rhash-query))
(defun rhash-query (rhash l r)
  "Returns the hash value of the interval [L, R)."
  (declare ((integer 0 #.most-positive-fixnum) l r))
  (assert (<= l r))
  (let ((cumul1 (rhash-cumul1 rhash))
        (powers1 (rhash-powers1 rhash))
        (cumul2 (rhash-cumul2 rhash))
        (powers2 (rhash-powers2 rhash)))
    (let ((lower (+ (aref cumul1 r)
                    (- +rhash-mod1+ (%mod (* (aref cumul1 l) (aref powers1 (- r l))) +rhash-mod1+))))
          (upper (+ (aref cumul2 r)
                    (- +rhash-mod2+ (%mod (* (aref cumul2 l) (aref powers2 (- r l))) +rhash-mod2+)))))
      (let ((lower (if (>= lower +rhash-mod1+) (- lower +rhash-mod1+) lower))
            (upper (if (>= upper +rhash-mod2+) (- upper +rhash-mod2+) upper)))
        (declare ((unsigned-byte 31) lower upper))
        (dpb upper (byte 31 31) lower)))))

(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* ((n (read))
         (as (make-array n :element-type 'uint31))
         (bs (make-array n :element-type 'uint31))
         (adeltas (make-array (* 2 n) :element-type 'uint31))
         (bdeltas (make-array n :element-type 'uint31)))
    (declare (uint31 n))
    (dotimes (i n)
      (setf (aref as i) (read-fixnum)))
    (dotimes (i n)
      (setf (aref bs i) (read-fixnum)))
    (dotimes (i (- n 1))
      (setf (aref adeltas i) (logxor (aref as i) (aref as (+ i 1)))
            (aref adeltas (+ i n)) (aref adeltas i))
      (setf (aref bdeltas i) (logxor (aref bs i) (aref bs (+ i 1)))))
    (setf (aref adeltas (- n 1))
          (logxor (aref as (- n 1)) (aref as 0))
          (aref adeltas (- (* 2 n) 1)) (aref adeltas (- n 1)))
    (setf (aref bdeltas (- n 1)) (logxor (aref bs (- n 1)) (aref bs 0)))
    (let ((ahash (make-rhash adeltas :key #'identity))
          (bhash (rhash-vector-hash bdeltas :key #'identity)))
      (with-buffered-stdout
        (dotimes (i n)
          (when (= (rhash-query ahash i (+ i n)) bhash)
            (format t "~D ~D~%" i (logxor (aref as i) (aref bs 0)))))))))

#-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 "3
0 2 1
1 2 3
"
    "1 3
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "5
0 0 0 0 0
2 2 2 2 2
"
    "0 2
1 2
2 2
3 2
4 2
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "6
0 1 3 7 6 4
1 5 4 6 2 3
"
    "2 2
5 5
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "2
1 2
0 0
"
    "
")))


(let ((s (read))
	(s_t (read)))
	(format t "~(~A~A~)" s_t s)
)

Score : 100 points

Problem Statement

Given are two strings S and T consisting of lowercase English letters. Concatenate T and S in this order, without space in between, and print the resulting string.

Constraints

S and T are strings consisting of lowercase English letters.

The lengths of S and T are between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

S T

Output

Print the resulting string.

Sample Input 1

oder atc

Sample Output 1

atcoder

When S = oder and T = atc, concatenating T and S in this order results in atcoder.

Sample Input 2

humu humu

Sample Output 2

humuhumu


(let ((str (read)))
  (format t "~(~A~A~)" (read) str))


(let* ((s (read-line))
       (pos (position #\Space s)))
  (format t "~A~A~%" (subseq s (1+ pos)) (subseq s 0 pos)))


(let ((a (read))
    (b (read))
    (k (read)))
    (if (> a k)
    (decf a k)
        (if (> (+ a b) k)
        (progn
        (decf b (- k a))
        (setq a 0)
        )
        (progn
        (setq a 0)
        (setq b 0))))
    (format t "~D ~D" a b)
)

Score : 200 points

Problem Statement

Takahashi has A cookies, and Aoki has B cookies.
Takahashi will do the following action K times:

If Takahashi has one or more cookies, eat one of his cookies.

Otherwise, if Aoki has one or more cookies, eat one of Aoki's cookies.

If they both have no cookies, do nothing.

In the end, how many cookies will Takahashi and Aoki have, respectively?

Constraints

0 \leq A \leq 10^{12}

0 \leq B \leq 10^{12}

0 \leq K \leq 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B K

Output

Print the numbers of Takahashi's and Aoki's cookies after K actions.

Sample Input 1

2 3 3

Sample Output 1

0 2

Takahashi will do the following:

He has two cookies, so he eats one of them.

Now he has one cookie left, and he eats it.

Now he has no cookies left, but Aoki has three, so Takahashi eats one of them.

Thus, in the end, Takahashi will have 0 cookies, and Aoki will have 2.

Sample Input 2

500000000000 500000000000 1000000000000

Sample Output 2

0 0

Watch out for overflows.


(defun solve (n l)
  (if (< (car l) n)
      (solve n (remove-if (lambda (x) (zerop (mod x (car l)))) l))
      (car l)))

(defun initialize ()
  (loop :as i
        :below 100002
        :collect (+ i 2)))

(princ (solve (read) (initialize)))

Score: 300 points

Problem Statement

Find the minimum prime number greater than or equal to X.

Notes

A prime number is an integer greater than 1 that cannot be evenly divided by any positive integer except 1 and itself.

For example, 2, 3, and 5 are prime numbers, while 4 and 6 are not.

Constraints

2 \le X \le 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X

Output

Print the minimum prime number greater than or equal to X.

Sample Input 1

20

Sample Output 1

23

The minimum prime number greater than or equal to 20 is 23.

Sample Input 2

2

Sample Output 2

2

X itself can be a prime number.

Sample Input 3

99992

Sample Output 3

100003


(defun solve (n l)
  (if (<= (car l) n)
      (solve n (remove-if (lambda (x) (zerop (mod x (car l)))) l))
      (car l)))

(defun initialize ()
  (loop :as i
        :below 100002
        :collect (+ i 2)))

(princ (solve (read) (initialize)))


;; F - Surrounded Nodes

(defparameter *modulus* (+ (expt 10 9) 7))
(defparameter *2-inv* (/ (1+ *modulus*) 2)) ; 2^-1 (mod *modulus*)

(defun main ()
  (let* ((N (read))  ; 頂点数
         (tree (read-tree N)))
    (princ (solve N tree))
    (fresh-line)))

(defun solve (N tree)
  (let ((expt-2 (make-array N))
        (parent (make-array N :initial-element nil))
        (subtree-size (make-array N :initial-element nil))
        (sum-expt-2-subtree 0))
    ; 1, 2, 4, 8, ..., 2^(N - 1) をexpt-2に保存
    (loop for i below N for e = 1 then (mod (* e 2) *modulus*)
          do (setf (aref expt-2 i) e))
    (labels
      ((dfs (stack)
         (when stack
           (let* ((v (pop stack))
                  (p (aref parent v)) ; vの親
                  (size (aref subtree-size v))) ; vを根とする部分木の頂点数
             (if (aref subtree-size v)
               ; 帰りがけ
               (when p
                 (incf sum-expt-2-subtree (1- (aref expt-2 size)))
                 (incf sum-expt-2-subtree (1- (aref expt-2 (- N size))))
                 (incf (aref subtree-size p) size))
               ; 行きがけ
               (loop
                 initially (push v stack) ; 帰りがけ用に残す
                           (setf (aref subtree-size v) 1)
                 for next in (aref tree v) ; vの隣接頂点
                 unless (eql next (aref parent v)) ; 親以外
                   do (push next stack)
                      (setf (aref parent next) v))))
           (dfs stack))))
      ; sum-expt-2-subtree (2^(部分木の頂点数) - 1の総和) を計算
      (dfs (list 0))

      (loop repeat (1+ N)
            for e = (+ (* N (1- (aref expt-2 (1- N))))
                       (- *modulus* (mod sum-expt-2-subtree *modulus*)))
                then (mod (* e *2-inv*) *modulus*)
            finally (return e)))))

(defun read-tree (N)
  "N頂点の木をreadして作る. 頂点番号は0〜N-1"
  (let ((tree (make-array N))) ; 隣接リスト
    (loop for i below N do (setf (aref tree i) nil))
    (loop repeat (1- N)
      do (let ((a (read-fixnum))
               (b (read-fixnum)))
           (push (1- b) (aref tree (1- a)))
           (push (1- a) (aref tree (1- b)))))
    tree))

(defun read-fixnum (&optional (in *standard-input*))
  "readより速い非負整数専用read (整数間に空白または改行1個に限る)
   cf. https://qiita.com/sansaqua/items/0b6417cb541047e29da4"
  (loop with acc = 0
        for byte = (read-byte in)
        if (<= 48 byte 57)
          do (setf acc (+ (* acc 10) (- byte 48)))
        else return acc))

(main)

Score : 600 points

Problem Statement

Given is a tree T with N vertices. The i-th edge connects Vertex A_i and B_i (1 \leq A_i,B_i \leq N).

Now, each vertex is painted black with probability 1/2 and white with probability 1/2, which is chosen independently from other vertices. Then, let S be the smallest subtree (connected subgraph) of T containing all the vertices painted black. (If no vertex is painted black, S is the empty graph.)

Let the holeyness of S be the number of white vertices contained in S. Find the expected holeyness of S.

Since the answer is a rational number, we ask you to print it \bmod 10^9+7, as described in Notes.

Notes

When you print a rational number, first write it as a fraction \frac{y}{x}, where x, y are integers, and x is not divisible by 10^9 + 7
(under the constraints of the problem, such representation is always possible).

Then, you need to print the only integer z between 0 and 10^9 + 6, inclusive, that satisfies xz \equiv y \pmod{10^9 + 7}.

Constraints

2 \leq N \leq 2 \times 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

Print the expected holeyness of S, \bmod 10^9+7.

Sample Input 1

3
1 2
2 3

Sample Output 1

125000001

If the vertices 1, 2, 3 are painted black, white, black, respectively, the holeyness of S is 1.

Otherwise, the holeyness is 0, so the expected holeyness is 1/8.

Since 8 \times 125000001 \equiv 1 \pmod{10^9+7}, we should print 125000001.

Sample Input 2

4
1 2
2 3
3 4

Sample Output 2

375000003

The expected holeyness is 3/8.

Since 8 \times 375000003 \equiv 3 \pmod{10^9+7}, we should print 375000003.

Sample Input 3

4
1 2
1 3
1 4

Sample Output 3

250000002

The expected holeyness is 1/4.

Sample Input 4

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

Sample Output 4

570312505


(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))))

(defparameter lst (mapcar #'parse-integer (split " " (read-line))))
(defparameter N (car lst))
(defparameter A (cadr lst))
(defparameter B (caddr lst))

(if (oddp (- A B))
    (format t "~A"  (+ 1 (- B A 1) (min (1- A) (- N B))))
    (format t "~A" (floor (/ (- B A) 2))))

Score : 300 points

Problem Statement

2N players are running a competitive table tennis training on N tables numbered from 1 to N.

The training consists of rounds.
In each round, the players form N pairs, one pair per table.
In each pair, competitors play a match against each other.
As a result, one of them wins and the other one loses.

The winner of the match on table X plays on table X-1 in the next round,
except for the winner of the match on table 1 who stays at table 1.

Similarly, the loser of the match on table X plays on table X+1 in the next round,
except for the loser of the match on table N who stays at table N.

Two friends are playing their first round matches on distinct tables A and B.
Let's assume that the friends are strong enough to win or lose any match at will.
What is the smallest number of rounds after which the friends can get to play a match against each other?

Constraints

2 \leq N \leq 10^{18}

1 \leq A < B \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the smallest number of rounds after which the friends can get to play a match against each other.

Sample Input 1

5 2 4

Sample Output 1

1

If the first friend loses their match and the second friend wins their match, they will both move to table 3 and play each other in the next round.

Sample Input 2

5 2 3

Sample Output 2

2

If both friends win two matches in a row, they will both move to table 1.


(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))))

(defparameter lst (mapcar #'parse-integer (split " " (read-line))))
(defparameter N (car lst))
(defparameter A (cadr lst))
(defparemeter B (caddr lst))

(if (oddp (- A B))
    (format t "~A" (apply #'min (list (1- (apply #'max (list A B)))
                                      (- N (apply #'min (list A B))))))
    (format t "~A" (floot (abs (/ (- A B) 2)))))


;; -*- 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
(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))
         (a (read))
         (b (read)))
    (if (evenp (- b a))
        (println (floor (- b a) 2))
        (println
         (min (+ a (floor (- b a 1) 2))
              (+ (+ (- n b) 1)
                 (floor (- n (+ a (+ (- n b) 1))) 2)))))))

#-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 4
"
    "1
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "5 2 3
"
    "2
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "5 2 4
"
    "1
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "5 2 3
"
    "2
")))


;; -*- 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 (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 ()
  (declare (inline sort))
  (let* ((n (read))
         (m (read))
         (v (read))
         (p (read))
         (as (make-array n :element-type 'uint32)))
    (declare (uint31 n m v p))
    (dotimes (i n)
      (setf (aref as i) (read-fixnum)))
    (println
     (let ((as (sort as #'>)))
       (if (<= v p)
           ;; V <= PならターゲットのA_kはA_k+Mにできて、
           ;; p番目に大きいA_{p-1}以上になればOK
           (let ((threshold (aref as (- p 1))))
             (loop for a across as
                   count (>= (+ a m) threshold)))
           ;; V > Pの場合、先頭P-1個には足し続ける(+M)。ターゲットにもたし続ける(+M)
           ;; 残りの(V-P)M票を分散して投票する
           (let ((as (subseq as (- p 1))))
             (labels ((feasible-p (as target sum)
                        (declare (fixnum target sum))
                        ;; sum 相投票量
                        (let ((len (length as)))
                          (loop for i from (- len 1) downto 0
                                for dest = (min (+ (aref as i) m) (+ target m))
                                do (decf sum (- dest (aref as i))))
                          (<= sum 0))))
               ;; 0は絶対OK
               (sb-int:named-let bisect ((ok 0) (ng (length as)))
                 (if (<= (- ng ok) 1)
                     (+ p ok)
                     (let* ((mid (ash (+ ok ng) -1))
                            (excluded-as (remove (aref as mid) as :count 1)))
                       (if (feasible-p excluded-as (aref as mid) (* m (- v p)))
                           (bisect mid ng)
                           (bisect ok mid))))))))))))

#-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 "6 1 2 2
2 1 1 3 0 2
"
    "5
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "6 1 5 2
2 1 1 3 0 2
"
    "3
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "10 4 8 5
7 2 3 6 1 6 5 4 6 5
"
    "8
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "6 1 2 2
2 1 1 3 0 2
"
    "5
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "6 1 5 2
2 1 1 3 0 2
"
    "3
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "10 4 8 5
7 2 3 6 1 6 5 4 6 5
"
    "8
")))

Score : 700 points

Problem Statement

N problems are proposed for an upcoming contest. Problem i has an initial integer score of A_i points.

M judges are about to vote for problems they like. Each judge will choose exactly V problems, independently from the other judges,
and increase the score of each chosen problem by 1.

After all M judges cast their vote, the problems will be sorted in non-increasing order of score, and the first P problems will be chosen for the problemset.
Problems with the same score can be ordered arbitrarily, this order is decided by the chief judge.

How many problems out of the given N have a chance to be chosen for the problemset?

Constraints

2 \le N \le 10^5

1 \le M \le 10^9

1 \le V \le N - 1

1 \le P \le N - 1

0 \le A_i \le 10^9

Input

Input is given from Standard Input in the following format:

N M V P
A_1 A_2 ... A_N

Output

Print the number of problems that have a chance to be chosen for the problemset.

Sample Input 1

6 1 2 2
2 1 1 3 0 2

Sample Output 1

5

If the only judge votes for problems 2 and 5, the scores will be 2 2 1 3 1 2.
The problemset will consist of problem 4 and one of problems 1, 2, or 6.

If the only judge votes for problems 3 and 4, the scores will be 2 1 2 4 0 2.
The problemset will consist of problem 4 and one of problems 1, 3, or 6.

Thus, problems 1, 2, 3, 4, and 6 have a chance to be chosen for the problemset. On the contrary, there is no way for problem 5 to be chosen.

Sample Input 2

6 1 5 2
2 1 1 3 0 2

Sample Output 2

3

Only problems 1, 4, and 6 have a chance to be chosen.

Sample Input 3

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

Sample Output 3

8


;; -*- 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
(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)))
    (when (= n 5)
      (println -1)
      (return-from main))
    (error "Huh?")))

#-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 "6
"
    "aabb..
b..zz.
ba....
.a..aa
..a..b
..a..b
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "2
"
    "-1
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "6
"
    "aabb..
b..zz.
ba....
.a..aa
..a..b
..a..b
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "2
"
    "-1
")))

Score : 900 points

Problem Statement

Let us consider a grid of squares with N rows and N columns. You want to put some domino pieces on this grid.
Each domino piece covers two squares that have a common side. Each square can be covered by at most one piece.

For each row of the grid, let's define its quality as the number of domino pieces that cover at least one square in this row.
We define the quality of each column similarly.

Find a way to put at least one domino piece on the grid so that the quality of every row is equal to the quality of every column,
or determine that such a placement doesn't exist.

Constraints

2 \le N \le 1000

Input

Input is given from Standard Input in the following format:

N

Output

If the required domino placement doesn't exist, print a single integer -1.

Otherwise, output your placement as N strings of N characters each.
If a square is not covered, the corresponding character must be . (a dot).
Otherwise, it must contain a lowercase English letter.
Squares covered by the same domino piece must contain the same letter.
If two squares have a common side but belong to different pieces, they must contain different letters.

Sample Input 1

6

Sample Output 1

aabb..
b..zz.
ba....
.a..aa
..a..b
..a..b

The quality of every row and every column is 2.

Sample Input 2

2

Sample Output 2

-1


(let ((a (read)) (b (read)))
  (if (and (not (eq a 1)) (not (eq b 1)))
	(princ 1)
	(if (and (not (eq a 2)) (not (eq b 2)))
	  (princ 2)
	  (princ 3))))

Score: 100 points

Problem Statement

Takahashi is solving quizzes. He has easily solved all but the last one.

The last quiz has three choices: 1, 2, and 3.

With his supernatural power, Takahashi has found out that the choices A and B are both wrong.

Print the correct choice for this problem.

Constraints

Each of the numbers A and B is 1, 2, or 3.

A and B are different.

Input

Input is given from Standard Input in the following format:

A
B

Output

Print the correct choice.

Sample Input 1

3
1

Sample Output 1

2

When we know 3 and 1 are both wrong, the correct choice is 2.

Sample Input 2

1
2

Sample Output 2

3


(let ((n (read))
    (s-read (read))
    (t-read (read)))

    (dotimes (i n)
        (format t "~(~A~)" (subseq (string s-read) i (+ i 1)))
        (format t "~(~A~)" (subseq (string t-read) i (+ i 1)))
    )
)

Score : 200 points

Problem Statement

Given are strings s and t of length N each, both consisting of lowercase English letters.

Let us form a new string by alternating the characters of S and the characters of T, as follows: the first character of S, the first character of T, the second character of S, the second character of T, ..., the N-th character of S, the N-th character of T. Print this new string.

Constraints

1 \leq N \leq 100

|S| = |T| = N

S and T are strings consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S T

Output

Print the string formed.

Sample Input 1

2
ip cc

Sample Output 1

icpc

Sample Input 2

8
hmhmnknk uuuuuuuu

Sample Output 2

humuhumunukunuku

Sample Input 3

5
aaaaa aaaaa

Sample Output 3

aaaaaaaaaa


(defun gen (str1 str2 &optional s)
  (cond ((equal str1 "") s)
    (t (gen (subseq str1 1) (subseq str2 1)
       (concatenate 'string s (subseq str1 0 1)(subseq str2 0 1)))))
  )

(let* ((n (read))
       (s (read-line))
       (str1 (subseq s 0 n))
       (str2 (subseq s (+ n 1))))
      (format t "~A~%" (gen str1 str2)))a