(defun isEasy (n c)
  (or
   (and (evenp n) (not (string= c #\R)))
   (and (oddp n) (not (string= c #\L)))
   )
  )
(defun notEasyList (str)
 (loop
  with x = 0
  for c across str
  when (not (isEasy (incf x) c))
  collect x
 )
)
(defun isEasyTapDance (str)
  (if (null (notEasyList str))
      "Yes"
      "No"
  )
)
(princ (isEasyTapDance (string (read))))


(defun splitat (c line)
  (labels ((rec (line acc)
		(let ((pos (position-if (lambda(cc) (char= c cc)) line)))
		  (if pos
		    (rec (subseq line (1+ pos)) (cons (subseq line 0 pos) acc))
		    (nreverse (cons line acc))))))
    (rec line nil)))
(defun f(n k q lst)
  (mapcar (lambda(a) (if (<= a 0) "No" "Yes")) (loop for i from 1 to n collect (+ (nth (1- i) lst) (- k q)))))
(defun g (n k q lst)
  (f n k q (reducer n lst)))
(defun start()
  (let* ((nkq (mapcar #'parse-integer (splitat #\space (read-line nil nil))))
	 (n (car nkq))
	 (k (cadr nkq))
	 (q (caddr nkq))
	 (lst nil))
    (dotimes (i q)
      (push (parse-integer (read-line nil nil)) lst))
    (format t "~{~A~%~}" (g n k q lst))))
(defun reducer(n lst)
  (labels ((rec(lst acc)
	     (if (null lst)
	       acc
	       (progn
		 (incf (nth (1- (car lst)) acc))
		 (rec (cdr lst) acc)))))
    (rec lst (loop for i from 1 to n collect 0))))
(start)

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

Score: 300 points

Problem Statement

Takahashi has decided to hold fastest-finger-fast quiz games. Kizahashi, who is in charge of making the scoreboard, is struggling to write the program that manages the players' scores in a game, which proceeds as follows.

A game is played by N players, numbered 1 to N. At the beginning of a game, each player has K points.

When a player correctly answers a question, each of the other N-1 players receives minus one (-1) point. There is no other factor that affects the players' scores.

At the end of a game, the players with 0 points or lower are eliminated, and the remaining players survive.

In the last game, the players gave a total of Q correct answers, the i-th of which was given by Player A_i.
For Kizahashi, write a program that determines whether each of the N players survived this game.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq K \leq 10^9

1 \leq Q \leq 10^5

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

Input

Input is given from Standard Input in the following format:

N K Q
A_1
A_2
.
.
.
A_Q

Output

Print N lines. The i-th line should contain Yes if Player i survived the game, and No otherwise.

Sample Input 1

6 3 4
3
1
3
2

Sample Output 1

No
No
Yes
No
No
No

In the beginning, the players' scores are (3, 3, 3, 3, 3, 3).

Player 3 correctly answers a question. The players' scores are now (2, 2, 3, 2, 2, 2).

Player 1 correctly answers a question. The players' scores are now (2, 1, 2, 1, 1, 1).

Player 3 correctly answers a question. The players' scores are now (1, 0, 2, 0, 0, 0).

Player 2 correctly answers a question. The players' scores are now (0, 0, 1, -1, -1, -1).

Players 1, 2, 4, 5 and 6, who have 0 points or lower, are eliminated, and Player 3 survives this game.

Sample Input 2

6 5 4
3
1
3
2

Sample Output 2

Yes
Yes
Yes
Yes
Yes
Yes

Sample Input 3

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

Sample Output 3

No
No
No
No
Yes
No
No
No
Yes
No

Submitted source-code file size, in bytes.

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

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


(defun splitat (c line)
  (labels ((rec (line acc)
				(let ((pos (position-if (lambda(cc) (char= c cc)) line)))
				  (if pos
					  (rec (subseq line (1+ pos)) (cons (subseq line 0 pos) acc))
					(nreverse (cons line acc))))))
	(rec line nil)))
(defun f(n k q lst)
  (mapcar (lambda(a) (if (<= a 0) "No" "Yes")) (loop for i from 1 to n collect (+ (nth (1- i) lst) (- k q)))))
(defun g (n k q lst)
  (f n k q (loop for i from 1 to n collect (length (remove-if-not (lambda(a) (= a i)) lst)))))
(let* ((nkq (mapcar #'parse-integer (splitat #\space (read-line nil nil))))
       (n (car nkq))
       (k (cadr nkq))
       (q (caddr nkq))
      (lst nil))
  (dotimes (i q)
    (push (parse-integer (read-line nil nil)) lst))
  (format t "~{~A~%~}" (g n k q lst)))


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

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

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

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

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

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defun main ()
  (let* ((n (read))
         (k (read))
         (q (read))
         (dp (make-array n :element-type 'fixnum :initial-element (- k q))))
    (dotimes (i q)
      (let ((a (- (read-fixnum) 1)))
        (incf (aref dp a))))
    (dotimes (i n)
      (write-line
       (if (> (aref dp i) 0)
           "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:/msys64/usr/bin/cat.exe" '("/dev/clipboard") :output out)))

#+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 3 4
3
1
3
2
"
    "No
No
Yes
No
No
No
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "6 5 4
3
1
3
2
"
    "Yes
Yes
Yes
Yes
Yes
Yes
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "10 13 15
3
1
4
1
5
9
2
6
5
3
5
8
9
7
9
"
    "No
No
No
No
Yes
No
No
No
Yes
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
           #\# #\> (lambda (s c p) (declare (ignore c p)) (read s nil nil t))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)
#-swank (disable-debugger) ; for CS Academy

;; BEGIN_INSERTED_CONTENTS

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

;; Treap accessible by index (O(log(n))).
;; Virtually it works like std::set of C++ or TreeSet of Java.

;; Note:
;; - You shouldn't insert duplicate keys into a treap unless you know what you
;; are doing.
;; - You cannot rely on the side effect when you call any destructive operations
;; on a treap. Always use the returned value.
;; - An empty treap is NIL.

(defstruct (treap (:constructor %make-treap (key priority &key left right (count 1)))
                  (:copier nil)
                  (:conc-name %treap-))
  (key 0 :type uint62)
  (priority 0 :type (integer 0 #.most-positive-fixnum))
  (count 0 :type (integer 0 #.most-positive-fixnum))
  (left nil :type (or null treap))
  (right nil :type (or null treap)))

(declaim (inline treap-count))
(defun treap-count (treap)
  "Returns the size of the (nullable) TREAP."
  (declare ((or null treap) treap))
  (if (null treap)
      0
      (%treap-count treap)))

(declaim (inline update-count))
(defun update-count (treap)
  (declare (treap treap))
  (setf (%treap-count treap)
        (+ 1
           (treap-count (%treap-left treap))
           (treap-count (%treap-right treap)))))

(declaim (inline treap-split)
         (ftype (function * (values (or null treap) (or null treap) &optional)) treap-split))
(defun treap-split (key treap)
  "Destructively splits the TREAP with reference to KEY and returns two treaps,
the smaller sub-treap (< KEY) and the larger one (>= KEY)."
  (declare #.OPT
           (uint62 key)
           ((or null treap) treap))
  (labels ((recur (treap)
             (cond ((null treap)
                    (values nil nil))
                   ((< (%treap-key treap) key)
                    (multiple-value-bind (left right) (recur (%treap-right treap))
                      (setf (%treap-right treap) left)
                      (update-count treap)
                      (values treap right)))
                   (t
                    (multiple-value-bind (left right) (recur (%treap-left treap))
                      (setf (%treap-left treap) right)
                      (update-count treap)
                      (values left treap))))))
    (recur treap)))

(defun treap-insert (key treap)
  "Destructively inserts KEY into TREAP and returns the resultant treap."
  (declare #.OPT
           (uint62 key)
           ((or null treap) treap))
  (let ((node (%make-treap key (random most-positive-fixnum))))
    (labels ((recur (treap)
               (declare (treap node))
               (cond ((null treap) node)
                     ((> (%treap-priority node) (%treap-priority treap))
                      (setf (values (%treap-left node) (%treap-right node))
                            (treap-split (%treap-key node) treap))
                      (update-count node)
                      node)
                     (t
                      (if (< (%treap-key node) (%treap-key treap))
                          (setf (%treap-left treap)
                                (recur (%treap-left treap)))
                          (setf (%treap-right treap)
                                (recur (%treap-right treap))))
                      (update-count treap)
                      treap))))
      (recur treap))))

(defun treap-merge (left right)
  "Destructively concatenates two treaps. Assumes that all keys of LEFT are
smaller (or larger, depending on the order) than those of RIGHT.

Note that this `merge' is different from CL:MERGE and rather close to
CL:CONCATENATE. (TREAP-UNITE is the analogue of the former.)"
  (declare #.OPT
           ((or null treap) left right))
  (cond ((null left) right)
        ((null right) left)
        ((> (%treap-priority left) (%treap-priority right))
         (setf (%treap-right left)
               (treap-merge (%treap-right left) right))
         (update-count left)
         left)
        (t
         (setf (%treap-left right)
               (treap-merge left (%treap-left right)))
         (update-count right)
         right)))

(defun treap-delete (key treap)
  "Destructively deletes the KEY in TREAP and returns the resultant treap."
  (declare #.OPT
           (uint62 key)
           ((or null treap) treap))
  (labels ((recur (treap)
             (cond ((null treap) nil)
                   ((< key (%treap-key treap))
                    (setf (%treap-left treap) (recur (%treap-left treap)))
                    (update-count treap)
                    treap)
                   ((< (%treap-key treap) key)
                    (setf (%treap-right treap) (recur (%treap-right treap)))
                    (update-count treap)
                    treap)
                   (t
                    (treap-merge (%treap-left treap) (%treap-right treap))))))
    (declare (ftype (function * (values (or null treap) &optional)) recur))
    (recur treap)))

(defun treap-map (function treap)
  "Successively applies FUNCTION to TREAP[0], ..., TREAP[SIZE-1]. FUNCTION must
take one argument."
  (declare (function function))
  (when treap
    (treap-map function (%treap-left treap))
    (funcall function (%treap-key treap))
    (treap-map function (%treap-right treap))))

(defmethod print-object ((object treap) stream)
  (print-unreadable-object (object stream :type t)
    (let ((init t))
      (treap-map (lambda (key)
                   (if init
                       (setq init nil)
                       (write-char #\  stream))
                   (write key :stream stream))
                 object))))

(defun treap-ref (treap index)
  "Index access"
  (declare #.OPT
           ((or null treap) treap)
           ((integer 0 #.most-positive-fixnum) index))
  (labels ((%ref (treap index)
             (declare (optimize (speed 3) (safety 0))
                      ((integer 0 #.most-positive-fixnum) index))
             (let ((left-count (treap-count (%treap-left treap))))
               (cond ((< index left-count)
                      (%ref (%treap-left treap) index))
                     ((> index left-count)
                      (%ref (%treap-right treap) (- index left-count 1)))
                     (t (%treap-key treap))))))
    (%ref treap index)))

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

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

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

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defun main ()
  (declare #.OPT
           (inline sort))
  (sb-int:with-progressive-timeout (get-remaining-time :seconds 1.75d0)
    (let* ((n (read))
           (as (make-array n :element-type 'uint62)))
      (dotimes (i n)
        (setf (aref as i) (read-fixnum)))
      (setf as (sort as #'>))
      (let ((redxor 0)
            (bluexor 0)
            (rtreap nil)
            (btreap nil)
            (res 0)
            (max-temp (float (sb-int:power-of-two-ceiling (reduce #'max as)) 1d0)))
        (declare (uint62 redxor bluexor res)
                 (double-float max-temp))
        (dotimes (i n)
          (if (evenp i)
              (setq rtreap (treap-insert i rtreap)
                    redxor (logxor redxor (aref as i)))
              (setq btreap (treap-insert i btreap)
                    bluexor (logxor bluexor (aref as i)))))
        (dotimes (_ most-positive-fixnum)
          (let ((remaining-time (get-remaining-time)))
            (when (eq 0 (get-remaining-time))
              (println res)
              (return-from main))
            (let* ((rem (/ remaining-time 1.8d0))
                   (temp (+ 1d0 (* max-temp rem))))
              ;; (dbg rtreap btreap)
              (dotimes (_ 5000)
                (let ((r (random 3)))
                  (cond ((= 0 r)
                         (when (and rtreap btreap)
                           (let* ((rpos (random (treap-count rtreap)))
                                  (bpos (random (treap-count btreap))))
                             (let* ((ridx (treap-ref rtreap rpos))
                                    (bidx (treap-ref btreap bpos))
                                    (rval (aref as ridx))
                                    (bval (aref as bidx))
                                    (old-val (+ redxor bluexor))
                                    (new-redxor (logxor redxor rval bval))
                                    (new-bluexor (logxor bluexor rval bval))
                                    (new-val (+ new-redxor new-bluexor))
                                    (prob (exp (/ (- new-val old-val) temp))))
                               (when (< (random 1d0) prob)
                                 (setq res (max res new-val))
                                 (setq redxor new-redxor
                                       bluexor new-bluexor
                                       rtreap (treap-delete ridx rtreap)
                                       btreap (treap-delete bidx btreap))
                                 (setq rtreap (treap-insert bidx rtreap)
                                       btreap (treap-insert ridx btreap)))))))
                        ((= 1 r)
                         (when (> (treap-count rtreap) 1)
                           (let* ((rpos (random (treap-count rtreap))))
                             (let* ((ridx (treap-ref rtreap rpos))
                                    (rval (aref as ridx))
                                    (old-val (+ redxor bluexor))
                                    (new-redxor (logxor redxor rval))
                                    (new-bluexor (logxor bluexor rval))
                                    (new-val (+ new-redxor new-bluexor))
                                    (prob (exp (/ (- new-val old-val) temp))))
                               (declare (uint32 ridx))
                               (when (< (random 1d0) prob)
                                 (setq res (max res new-val))
                                 (setq redxor new-redxor
                                       bluexor new-bluexor
                                       rtreap (treap-delete ridx rtreap))
                                 (setq btreap (treap-insert ridx btreap)))))))
                        (t
                         (when (> (treap-count btreap) 1)
                           (let* ((bpos (random (treap-count btreap))))
                             (let* ((bidx (treap-ref btreap bpos))
                                    (bval (aref as bidx))
                                    (old-val (+ redxor bluexor))
                                    (new-redxor (logxor redxor bval))
                                    (new-bluexor (logxor bluexor bval))
                                    (new-val (+ new-redxor new-bluexor))
                                    (prob (exp (/ (- new-val old-val) temp))))
                               (declare (uint32 bidx))
                               (when (< (random 1d0) prob)
                                 (setq res (max res new-val))
                                 (setq redxor new-redxor
                                       bluexor new-bluexor
                                       btreap (treap-delete bidx btreap))
                                 (setq rtreap (treap-insert bidx rtreap)))))))))))))))))

#-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:/msys64/usr/bin/cat.exe" '("/dev/clipboard") :output out)))

#+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 6 5
"
    "12
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "4
23 36 66 65
"
    "188
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "20
1008288677408720767 539403903321871999 1044301017184589821 215886900497862655 504277496111605629 972104334925272829 792625803473366909 972333547668684797 467386965442856573 755861732751878143 1151846447448561405 467257771752201853 683930041385277311 432010719984459389 319104378117934975 611451291444233983 647509226592964607 251832107792119421 827811265410084479 864032478037725181
"
    "2012721721873704572
")))

Score: 600 points

Problem Statement

We have N non-negative integers: A_1, A_2, ..., A_N.

Consider painting at least one and at most N-1 integers among them in red, and painting the rest in blue.

Let the beauty of the painting be the \mbox{XOR} of the integers painted in red, plus the \mbox{XOR} of the integers painted in blue.

Find the maximum possible beauty of the painting.

What is \mbox{XOR}?

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

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

For example, 3 \oplus 5 = 6.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

0 \leq A_i < 2^{60}\ (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible beauty of the painting.

Sample Input 1

3
3 6 5

Sample Output 1

12

If we paint 3, 6, 5 in blue, red, blue, respectively, the beauty will be (6) + (3 \oplus 5) = 12.

There is no way to paint the integers resulting in greater beauty than 12, so the answer is 12.

Sample Input 2

4
23 36 66 65

Sample Output 2

188

Sample Input 3

20
1008288677408720767 539403903321871999 1044301017184589821 215886900497862655 504277496111605629 972104334925272829 792625803473366909 972333547668684797 467386965442856573 755861732751878143 1151846447448561405 467257771752201853 683930041385277311 432010719984459389 319104378117934975 611451291444233983 647509226592964607 251832107792119421 827811265410084479 864032478037725181

Sample Output 3

2012721721873704572

A_i and the answer may not fit into a 32-bit integer type.


Score : 100 points

Problem Statement

Takahashi is going to set a 3-character password.

How many possible passwords are there if each of its characters must be a digit between 1 and N (inclusive)?

Constraints

1 \leq N \leq 9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of possible passwords.

Sample Input 1

2

Sample Output 1

8

There are eight possible passwords: 111, 112, 121, 122, 211, 212, 221, and 222.

Sample Input 2

1

Sample Output 2

1

There is only one possible password if you can only use one kind of character.


(let* ((n (read))
       (lst (loop :repeat n :collect (read)))
       (lst-t (mapcar (lambda (a b) (= a (1- b))) lst (cdr lst)))
       (lst-a (loop :repeat n :collect (read)))
       (lst-b (loop :repeat (1- n) :collect (read))))
  (princ (+ (reduce #'+ lst-a)
            (loop :for k :from 0 :upto (1- n) :sum (if (nth k lst-t)
                                                       (nth (1- (nth k lst)) lst-b)
                                                       0)))))

Score : 200 points

Problem Statement

Takahashi went to an all-you-can-eat buffet with N kinds of dishes and ate all of them (Dish 1, Dish 2, \ldots, Dish N) once.

The i-th dish (1 \leq i \leq N) he ate was Dish A_i.

When he eats Dish i (1 \leq i \leq N), he gains B_i satisfaction points.

Additionally, when he eats Dish i+1 just after eating Dish i (1 \leq i \leq N - 1), he gains C_i more satisfaction points.

Find the sum of the satisfaction points he gained.

Constraints

All values in input are integers.

2 \leq N \leq 20

1 \leq A_i \leq N

A_1, A_2, ..., A_N are all different.

1 \leq B_i \leq 50

1 \leq C_i \leq 50

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
C_1 C_2 ... C_{N-1}

Output

Print the sum of the satisfaction points Takahashi gained, as an integer.

Sample Input 1

3
3 1 2
2 5 4
3 6

Sample Output 1

14

Takahashi gained 14 satisfaction points in total, as follows:

First, he ate Dish 3 and gained 4 satisfaction points.

Next, he ate Dish 1 and gained 2 satisfaction points.

Lastly, he ate Dish 2 and gained 5 + 3 = 8 satisfaction points.

Sample Input 2

4
2 3 4 1
13 5 8 24
45 9 15

Sample Output 2

74

Sample Input 3

2
1 2
50 50
50

Sample Output 3

150


(let* ((n (read))
       (lst (loop :repeat (1- n) :collect (read)))
       (lst-a (cons (car lst) lst))
       (lst-b (concatenate 'list lst (last lst)))
       (lst-t (concatenate 'list '(nil) (mapcar #'<= lst (cdr lst)) '(T))))
  (princ (reduce #'+ (mapcar (lambda (a b c)
                               (if a b c)) lst-t lst-a lst-b) )))

Score : 300 points

Problem Statement

There is an integer sequence A of length N whose values are unknown.

Given is an integer sequence B of length N-1 which is known to satisfy the following:

B_i \geq \max(A_i, A_{i+1})

Find the maximum possible sum of the elements of A.

Constraints

All values in input are integers.

2 \leq N \leq 100

0 \leq B_i \leq 10^5

Input

Input is given from Standard Input in the following format:

N
B_1 B_2 ... B_{N-1}

Output

Print the maximum possible sum of the elements of A.

Sample Input 1

3
2 5

Sample Output 1

9

A can be, for example, ( 2 , 1 , 5 ), ( -1 , -2 , -3 ), or ( 2 , 2 , 5 ). Among those candidates, A = ( 2 , 2 , 5 ) has the maximum possible sum.

Sample Input 2

2
3

Sample Output 2

6

Sample Input 3

6
0 153 10 10 23

Sample Output 3

53


(defun count-happy-people (s)
  (labels ((inner (s &optional (cnt 0))
             (cond
               ((null (cdr s)) cnt)
               ((char-equal
                 (first s)
                 (second s))
                (inner (rest s) (1+ cnt)))
               (t
                (inner (rest s) cnt)))))
    (inner s)))

(defun solve (n k s)
  (let ((start (count-happy-people s)))
    (min (+ start (* k 2))
         (1- n))))

(defun main ()
  (let ((n (read))
        (k (read))
        (s (concatenate 'list (read-line))))
    (format t "~a~%" (solve n k s))))

(main)

Score : 400 points

Problem Statement

There are N people standing in a queue from west to east.

Given is a string S of length N representing the directions of the people.
The i-th person from the west is facing west if the i-th character of S is L, and east if that character of S is R.

A person is happy if the person in front of him/her is facing the same direction.
If no person is standing in front of a person, however, he/she is not happy.

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

Operation: Choose integers l and r such that 1 \leq l \leq r \leq N, and rotate by 180 degrees the part of the queue: the l-th, (l+1)-th, ..., r-th persons. That is, for each i = 0, 1, ..., r-l, the (l + i)-th person from the west will stand the (r - i)-th from the west after the operation, facing east if he/she is facing west now, and vice versa.

What is the maximum possible number of happy people you can have?

Constraints

N is an integer satisfying 1 \leq N \leq 10^5.

K is an integer satisfying 1 \leq K \leq 10^5.

|S| = N

Each character of S is L or R.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of happy people after at most K operations.

Sample Input 1

6 1
LRLRRL

Sample Output 1

3

If we choose (l, r) = (2, 5), we have LLLRLL, where the 2-nd, 3-rd, and 6-th persons from the west are happy.

Sample Input 2

13 3
LRRLRLRRLRLLR

Sample Output 2

9

Sample Input 3

10 1
LLLLLRRRRR

Sample Output 3

9

Sample Input 4

9 2
RRRLRLRLL

Sample Output 4

7


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

;; BEGIN_INSERTED_CONTENTS

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

;;;
;;; Implicit treap
;;; (treap with implicit key)
;;;

(defconstant +op-identity+ 0
  "identity element w.r.t. OP")

(defstruct (itreap (:constructor %make-itreap (value &key left right (count 1) (accumulator value)))
                  (:copier nil)
                  (:conc-name %itreap-))
  (value +op-identity+ :type fixnum)
  (accumulator +op-identity+ :type fixnum)
  (count 1 :type (integer 0 #.most-positive-fixnum)) ; size of (sub)treap
  (left nil :type (or null itreap))
  (right nil :type (or null itreap)))

(declaim (inline itreap-count))
(defun itreap-count (itreap)
  "Returns the length of ITREAP."
  (declare ((or null itreap) itreap))
  (if itreap
      (%itreap-count itreap)
      0))

(declaim (inline itreap-accumulator))
(defun itreap-accumulator (itreap)
  "Returns the sum (w.r.t. OP) of the whole ITREAP:
ITREAP[0]+ITREAP[1]+...+ITREAP[SIZE-1]."
  (declare ((or null itreap) itreap))
  (if itreap
      (%itreap-accumulator itreap)
      +op-identity+))

(declaim (inline update-count))
(defun update-count (itreap)
  (declare (itreap itreap))
  (setf (%itreap-count itreap)
        (+ 1
           (itreap-count (%itreap-left itreap))
           (itreap-count (%itreap-right itreap)))))

(declaim (inline update-accumulator))
(defun update-accumulator (itreap)
  (declare (itreap itreap))
  (setf (%itreap-accumulator itreap)
        (if (%itreap-left itreap)
            (if (%itreap-right itreap)
                (max (%itreap-accumulator (%itreap-left itreap))
                     (%itreap-value itreap)
                     (%itreap-accumulator (%itreap-right itreap)))
                (max (%itreap-accumulator (%itreap-left itreap))
                     (%itreap-value itreap)))
            (if (%itreap-right itreap)
                (max (%itreap-value itreap)
                     (%itreap-accumulator (%itreap-right itreap)))
                (%itreap-value itreap)))))

(declaim (inline force-up))
(defun force-up (itreap)
  "Propagates up the information from children."
  (declare (itreap itreap))
  (update-count itreap)
  (update-accumulator itreap))

(defun make-itreap (vector)
  (declare #.OPT ((simple-array uint31 (*)) vector))
  (let ((size (length vector)))
    (labels ((build (l r)
               (declare ((integer 0 #.most-positive-fixnum) l r))
               (if (= l r)
                   nil
                   (let* ((mid (ash (+ l r) -1))
                          (node (%make-itreap (aref vector mid))))
                     (setf (%itreap-left node) (build l mid))
                     (setf (%itreap-right node) (build (+ mid 1) r))
                     (force-up node)
                     node))))
      (build 0 size))))

(declaim (inline itreap-query))
(defun itreap-query (itreap l r)
  "Queries the `sum' (w.r.t. OP) of the interval [L, R)."
  (declare #.OPT
           ((integer 0 #.most-positive-fixnum) l r))
  (labels
      ((recur (itreap l r)
         (declare ((integer 0 #.most-positive-fixnum) l r)
                  (values fixnum))
         (unless itreap
           (return-from recur +op-identity+))
         (if (and (zerop l) (= r (%itreap-count itreap)))
             (%itreap-accumulator itreap)
             (let ((left-count (itreap-count (%itreap-left itreap))))
               (if (<= l left-count)
                   (if (< left-count r)
                       ;; LEFT-COUNT is in [L, R)
                       (max (recur (%itreap-left itreap) l (min r left-count))
                            (%itreap-value itreap)
                            (recur (%itreap-right itreap) 0 (- r left-count 1)))
                       ;; LEFT-COUNT is in [R, END)
                       (recur (%itreap-left itreap) l (min r left-count)))
                   ;; LEFT-COUNT is in [0, L)
                   (recur (%itreap-right itreap) (- l left-count 1) (- r left-count 1)))))))
    (recur itreap l r)))

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

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

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

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (ps (make-array 100000 :element-type 'uint31)))
    (declare (uint31 n))
    (dotimes (i n)
      (setf (aref ps i) (read-fixnum)))
    (let ((itreap (make-itreap ps)))
      (let ((res 0))
        (declare ((integer 0 #.most-positive-fixnum) res))
        (dotimes (i n)
          (let* ((l (sb-int:named-let bisect ((ok -1) (ng i))
                      (declare (int32 ok ng))
                      (if (<= (- ng ok) 1)
                          ok
                          (let* ((mid (ash (+ ok ng) -1))
                                 (val (itreap-query itreap mid i)))
                            (declare (fixnum val))
                            (if (> val (aref ps i))
                                (bisect mid ng)
                                (bisect ok mid))))))
                 (r+1 (sb-int:named-let bisect ((ng i) (ok (+ n 1)))
                        (declare (int32 ng ok))
                        (if (<= (- ok ng) 1)
                            ok
                            (let* ((mid (ash (+ ok ng) -1))
                                   (val (itreap-query itreap i mid)))
                              (declare (fixnum val))
                              (if (> val (aref ps i))
                                  (bisect ng mid)
                                  (bisect mid ok))))))
                 (r (- r+1 1)))
            (when (>= l 0)
              (let* ((ll (sb-int:named-let bisect ((ok -1) (ng l))
                           (declare (int32 ok ng))
                           (if (<= (- ng ok) 1)
                               ok
                               (let* ((mid (ash (+ ok ng) -1))
                                      (val (itreap-query itreap mid l)))
                                 (declare (fixnum val))
                                 (if (> val (aref ps i))
                                     (bisect mid ng)
                                     (bisect ok mid)))))))
                (incf res (* (- r i) (- l ll) (aref ps i)))))
            (when (< r n)
              (let* ((rr+1 (sb-int:named-let bisect ((ng (+ r 1)) (ok (+ n 1)))
                             (declare (int32 ng ok))
                             (if (<= (- ok ng) 1)
                                 ok
                                 (let* ((mid (ash (+ ok ng) -1))
                                        (val (itreap-query itreap r+1 mid)))
                                   (declare (fixnum val))
                                   (if (> val (aref ps i))
                                       (bisect ng mid)
                                       (bisect mid ok))))))
                     (rr (- rr+1 1)))
                (incf res (* (- i l) (- rr r) (aref ps i)))))))
        (println res)))))

#-swank (main)

Score: 500 points

Problem Statement

Given is a permutation P of \{1, 2, \ldots, N\}.

For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest value among P_L, P_{L+1}, \ldots, P_R.

Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.

Constraints

2 \le N \le 10^5

1 \le P_i \le N

P_i \neq P_j (i \neq j)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 \ldots P_N

Output

Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.

Sample Input 1

3
2 3 1

Sample Output 1

5

X_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.

Sample Input 2

5
1 2 3 4 5

Sample Output 2

30

Sample Input 3

8
8 2 7 3 4 5 6 1

Sample Output 3

136


(let ((s (read-line))
    (ts (read-line))
    (ans 0))
    (dotimes (i 3)
    (if (char= (char s i) (char ts i))
    (incf ans))
    )
    (princ ans)
)

Score : 100 points

Problem Statement

You will be given a string S of length 3 representing the weather forecast for three days in the past.

The i-th character (1 \leq i \leq 3) of S represents the forecast for the i-th day. S, C, and R stand for sunny, cloudy, and rainy, respectively.

You will also be given a string T of length 3 representing the actual weather on those three days.

The i-th character (1 \leq i \leq 3) of S represents the actual weather on the i-th day. S, C, and R stand for sunny, cloudy, and rainy, respectively.

Print the number of days for which the forecast was correct.

Constraints

S and T are strings of length 3 each.

S and T consist of S, C, and R.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the number of days for which the forecast was correct.

Sample Input 1

CSS
CSR

Sample Output 1

2

For the first day, it was forecast to be cloudy, and it was indeed cloudy.

For the second day, it was forecast to be sunny, and it was indeed sunny.

For the third day, it was forecast to be sunny, but it was rainy.

Thus, the forecast was correct for two days in this case.

Sample Input 2

SSR
SSR

Sample Output 2

3

Sample Input 3

RRR
SSS

Sample Output 3

0


(let* ((n (read-line))
       (m (read-line)))
  (princ (loop :for k :from 0 :upto 2 :count (char-equal (aref n k) (aref m k)))))


(let ((a (read))
               (b (read)))
           (print
           (if (> a b)
               1
               (multiple-value-bind (a rem)
                   (floor b a)
                 (if (zerop rem) a (1+ a))))))

Score : 200 points

Problem Statement

Takahashi's house has only one socket.

Takahashi wants to extend it with some number of power strips, each with A sockets, into B or more empty sockets.

One power strip with A sockets can extend one empty socket into A empty sockets.

Find the minimum number of power strips required.

Constraints

All values in input are integers.

2 \leq A \leq 20

1 \leq B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

Print the minimum number of power strips required.

Sample Input 1

4 10

Sample Output 1

3

3 power strips, each with 4 sockets, extend the socket into 10 empty sockets.

Sample Input 2

8 9

Sample Output 2

2

2 power strips, each with 8 sockets, extend the socket into 15 empty sockets.

Sample Input 3

8 8

Sample Output 3

1


(let* ((n (read))
       (lst (loop :repeat n :collect (read)))
       (lst-a (mapcar #'< lst (cdr lst)))
       (mem 0)
       (ans '()))
  (loop :for k :in lst-a :do (push (if k (prog1 mem (setf mem 0)) (progn (incf mem) 0)) ans) :finally (push mem ans))
  (princ (apply #'max ans)))

Score : 300 points

Problem Statement

There are N squares arranged in a row from left to right.

The height of the i-th square from the left is H_i.

You will land on a square of your choice, then repeat moving to the adjacent square on the right as long as the height of the next square is not greater than that of the current square.

Find the maximum number of times you can move.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq H_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
H_1 H_2 ... H_N

Output

Print the maximum number of times you can move.

Sample Input 1

5
10 4 8 7 3

Sample Output 1

2

By landing on the third square from the left, you can move to the right twice.

Sample Input 2

7
4 4 5 6 6 5 5

Sample Output 2

3

By landing on the fourth square from the left, you can move to the right three times.

Sample Input 3

4
1 2 3 4

Sample Output 3

0


Score : 400 points

Problem Statement

For an integer N, we will choose a permutation \{P_1, P_2, ..., P_N\} of \{1, 2, ..., N\}.

Then, for each i=1,2,...,N, let M_i be the remainder when i is divided by P_i.

Find the maximum possible value of M_1 + M_2 + \cdots + M_N.

Constraints

N is an integer satisfying 1 \leq N \leq 10^9.

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum possible value of M_1 + M_2 + \cdots + M_N.

Sample Input 1

2

Sample Output 1

1

When the permutation \{P_1, P_2\} = \{2, 1\} is chosen, M_1 + M_2 = 1 + 0 = 1.

Sample Input 2

13

Sample Output 2

78

Sample Input 3

1

Sample Output 3

0


(defmacro dotimes-unroll ((var count size &optional result) &body body)
  "DOTIMES macro with loop-unrolling by SIZE."
  (let ((whole (gensym))
        (_count (gensym)))
    (check-type var symbol)
    (check-type size (integer 1 #.most-positive-fixnum))
    `(block ,whole
       (let ((,_count ,count)
             (,var 0))
         (declare ((integer 0 #.most-positive-fixnum) ,var ,_count))
         (loop
           (when (> ,var (- ,_count ,size))
             (do ((,var ,var (1+ ,var)))
                 ((>= ,var ,_count)
                  (return-from ,whole ,result))
               ,@body))
           ,@(loop for i from 0 below size
                   collect `(locally ,@body (setq ,var (+ ,var 1)))))))))

(defun main ()
  (declare (optimize (speed 3) (safety 0)))
  (let* ((n (read))
         (res 0))
    (declare ((unsigned-byte 62) res)
             (uint32 n))
    (dotimes-unroll (i n 20)
      (incf res i))
    (princ res)
    (terpri)))

#-swank (main)


(defun check-and-pick (a n &optional (c 0))
  (if (= (count nil a) n)
      c
      (let (checked)
        (loop for i from 1 to n
              when (not (member i checked))
              do (let ((e (car (nth (1- i) a))))
                   (when (and e (not (member e checked)) (= (car (nth (1- e) a)) i))
                     (push i checked)
                     (push e checked)
                     (setf (nth (1- i) a) (cdr (nth (1- i) a)))
                     (setf (nth (1- e) a) (cdr (nth (1- e) a))))))
        (if checked
            (check-and-pick a n (1+ c))
            -1))))

(defun main ()
  (let ((n (read))
        (a))
    (setf a
          (loop for i from 1 to n
                collect (loop for j from 2 to n
                              collect (read))))
    (format t "~A~%" (check-and-pick a n))))
(main)

Score : 500 points

Problem Statement

N players will participate in a tennis tournament. We will call them Player 1, Player 2, \ldots, Player N.

The tournament is round-robin format, and there will be N(N-1)/2 matches in total.
Is it possible to schedule these matches so that all of the following conditions are satisfied? If the answer is yes, also find the minimum number of days required.

Each player plays at most one matches in a day.

Each player i (1 \leq i \leq N) plays one match against Player A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1} in this order.

Constraints

3 \leq N \leq 1000

1 \leq A_{i, j} \leq N

A_{i, j} \neq i

A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1} are all different.

Input

Input is given from Standard Input in the following format:

N
A_{1, 1} A_{1, 2} \ldots A_{1, N-1}
A_{2, 1} A_{2, 2} \ldots A_{2, N-1}
:
A_{N, 1} A_{N, 2} \ldots A_{N, N-1}

Output

If it is possible to schedule all the matches so that all of the conditions are satisfied, print the minimum number of days required; if it is impossible, print -1.

Sample Input 1

3
2 3
1 3
1 2

Sample Output 1

3

All the conditions can be satisfied if the matches are scheduled for three days as follows:

Day 1: Player 1 vs Player 2

Day 2: Player 1 vs Player 3

Day 3: Player 2 vs Player 3

This is the minimum number of days required.

Sample Input 2

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

Sample Output 2

4

All the conditions can be satisfied if the matches are scheduled for four days as follows:

Day 1: Player 1 vs Player 2, Player 3 vs Player 4

Day 2: Player 1 vs Player 3

Day 3: Player 1 vs Player 4, Player 2 vs Player 3

Day 4: Player 2 vs Player 4

This is the minimum number of days required.

Sample Input 3

3
2 3
3 1
1 2

Sample Output 3

-1

Any scheduling of the matches violates some condition.


#-swank
(unless (member :child-sbcl *features*)
  (quit
   :unix-status
   (process-exit-code
    (run-program *runtime-pathname*
                 `("--control-stack-size" "256MB"
                   "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                   "--eval" "(push :child-sbcl *features*)"
                   "--script" ,(namestring *load-pathname*))
                 :output t :error t :input t))))
;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (sb-int:defconstant-eqx OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0))
    #'equal)
  #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t)
  #-swank (set-dispatch-macro-character
           #\# #\> (lambda (s c p) (declare (ignore c p)) (read s nil (values) t))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)
#-swank (disable-debugger) ; for CS Academy

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

;;;
;;; Topological sort
;;;

(define-condition cycle-detected-error (simple-error)
  ((graph :initarg :graph :reader cycle-detected-error-graph)
   (vertex :initarg :vertex :reader cycle-detected-error-vertex))
  (:report
   (lambda (condition stream)
     (format stream "Detected a cycle containing ~A in ~A."
             (cycle-detected-error-vertex condition)
             (cycle-detected-error-graph condition)))))

(declaim (ftype (function * (values (simple-array (integer 0 #.most-positive-fixnum) (*)) &optional))
                topological-sort))
(defun topological-sort (graph)
  "Returns a topologically sorted array of all the vertex in GRAPH. This
function signals CYCLE-DETECTED-ERROR when it detects a cycle.

GRAPH := vector of adjacency lists."
  (declare #.OPT
           ((simple-array list (*)) graph))
  (let* ((n (length graph))
         (tmp-marked (make-array n :element-type 'bit :initial-element 0))
         (marked (make-array n :element-type 'bit :initial-element 0))
         (result (make-array n :element-type '(integer 0 #.most-positive-fixnum)))
         (index (- n 1)))
    (declare (fixnum index))
    (labels ((visit (v)
               (when (= 0 (aref marked v))
                 (when (= 1 (aref tmp-marked v))
                   (error 'cycle-detected-error :graph graph :vertex v))
                 (setf (aref tmp-marked v) 1)
                 (dolist (next (aref graph v))
                   (visit next))
                 (setf (aref marked v) 1)
                 (setf (aref result index) v)
                 (decf index))))
      (dotimes (v n result)
        (when (= 0 (aref marked v))
          (visit v))))))

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

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

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

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(declaim (inline encode))
(defun encode (x y)
  (when (> x y)
    (rotatef x y))
  (dpb y (byte 10 10) x))

(defconstant +max+ (ash 1 20))

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (as (make-array (list n (- n 1)) :element-type 'uint32))
         (graph (make-array +max+ :element-type 'list :initial-element nil)))
    (declare (uint16 n))
    (dotimes (i n)
      (dotimes (j (- n 1))
        (let ((a (- (read-fixnum) 1)))
          (setf (aref as i j) a))))
    (dotimes (i n)
      (dotimes (j (- n 2))
        (let* ((opponent1 (aref as i j))
               (opponent2 (aref as i (+ j 1)))
               (v1 (encode i opponent1))
               (v2 (encode i opponent2)))
          (push v2 (aref graph v1)))))
    (handler-bind ((cycle-detected-error
                     (lambda (c) (println -1) (return-from main))))
      (let ((seq (topological-sort graph))
            (dp (make-array +max+ :element-type 'uint32 :initial-element 1)))
        (dotimes (i +max+)
          (let ((v (aref seq i)))
            (dolist (neighbor (aref graph v))
              (setf (aref dp neighbor)
                    (max (aref dp neighbor)
                         (+ 1 (aref dp v)))))))
        (println (reduce #'max dp))))))

#-swank (main)


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

;; BEGIN_INSERTED_CONTENTS
;;;
;;; ARRAY-ELEMENT-TYPE is not constant-folded on SBCL version earlier than
;;; 1.5.0. See
;;; https://github.com/sbcl/sbcl/commit/9f0d12e7ab961828931d01c0b2a76a5885ad35d2
;;;

(eval-when (:compile-toplevel :load-toplevel :execute)
  (sb-c:deftransform array-element-type ((array))
    (let ((type (sb-c::lvar-type array)))
      (flet ((element-type (type)
               (and (sb-c::array-type-p type)
                    (sb-int:neq (sb-kernel::array-type-specialized-element-type type) sb-kernel:*wild-type*)
                    (sb-kernel:type-specifier (sb-kernel::array-type-specialized-element-type type)))))
        (cond ((let ((type (element-type type)))
                 (and type
                      `',type)))
              ((sb-kernel:union-type-p type)
               (let (result)
                 (loop for type in (sb-kernel:union-type-types type)
                       for et = (element-type type)
                       unless (and et
                                   (if result
                                       (equal result et)
                                       (setf result et)))
                       do (sb-c::give-up-ir1-transform))
                 `',result))
              ((sb-kernel:intersection-type-p type)
               (loop for type in (sb-kernel:intersection-type-types type)
                     for et = (element-type type)
                     when et
                     return `',et
                     finally (sb-c::give-up-ir1-transform)))
              (t
               (sb-c::give-up-ir1-transform)))))))

;;;
;;; 2D convex hull of points (Monotone Chain Algorithm)
;;; Complexity: O(nlog(n))
;;;

(declaim (inline make-convex-hull!))
(defun make-convex-hull! (points &optional (eps 0))
  "Returns the vector of the vertices of the convex hull, which are sorted in
the anticlockwise direction around the perimeter. This function sorts POINTS as
a side effect.

If EPS is non-negative, three vertices in a straight line are excluded (when the
calculation error is within EPS, of course); they are allowed if EPS is
negative.

POINTS := vector of complex number"
  (declare (inline sort)
           (vector points))
  ;; FIXME: the returned vector may contain duplicate vertices in a degenerative
  ;; case: E.g. (make-convex-hull! #(#c(1 2) #c(1 2) #c(1 2) #c(1 2)) ) |->
  ;; #(#C(1 2) #C(1 2))
  (macrolet ((outer (p1 p2) ; outer product
               `(let ((c1 ,p1)
                      (c2 ,p2))
                  (- (* (realpart c1) (imagpart c2))
                     (* (imagpart c1) (realpart c2))))))
    (when (<= (length points) 1)
      (return-from make-convex-hull! (copy-seq points)))
    (let* ((n (length points))
           (end 0)
           (res (make-array (* n 2) :element-type (array-element-type points)))
           (points (sort points (lambda (p1 p2)
                                  (if (= (realpart p1) (realpart p2))
                                      (< (imagpart p1) (imagpart p2))
                                      (< (realpart p1) (realpart p2)))))))
      (declare (fixnum end))
      (do ((i 0 (+ i 1)))
          ((= i n))
        (loop (if (and (> end 1)
                       (<= (outer (- (aref res (- end 1)) (aref res (- end 2)))
                                  (- (aref points i) (aref res (- end 1))))
                           eps))
                  (decf end)
                  (return)))
        (setf (aref res end) (aref points i))
        (incf end))
      (let ((tmp-end end))
        (do ((i (- n 2) (- i 1)))
            ((< i 0))
          (loop (if (and (> end tmp-end)
                         (<= (outer (- (aref res (- end 1)) (aref res (- end 2)))
                                    (- (aref points i) (aref res (- end 1))))
                             eps))
                    (decf end)
                    (return)))
          (setf (aref res end) (aref points i))
          (incf end)))
      (adjust-array res (- end 1)))))

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

(defconstant +eps+ 1d-9)

(defun main ()
  (let* ((n (read))
         (vecs (make-array n :element-type '(complex double-float)))
         (dp (make-array 1 :element-type '(complex double-float) :initial-element #c(0d0 0d0))))
    (dotimes (i n)
      (let* ((x (float (read) 1d0))
             (y (float (read) 1d0)))
        (setf (aref vecs i) (complex x y))))
    (dotimes (i n)
      (let* ((v (aref vecs i))
             (len (length dp))
             (translation (adjust-array dp (* 2 len))))
        (declare ((simple-array (complex double-float) (*)) translation)
                 (uint32 len))
        (dotimes (i len)
          (setf (aref translation (+ i len))
                (+ (aref translation i) v)))
        (setq dp (make-convex-hull! translation +eps+))))
    (println (reduce #'max dp :key #'abs))))

#-swank (main)

Score: 600 points

Problem Statement

E869120 is initially standing at the origin (0, 0) in a two-dimensional plane.

He has N engines, which can be used as follows:

When E869120 uses the i-th engine, his X- and Y-coordinate change by x_i and y_i, respectively. In other words, if E869120 uses the i-th engine from coordinates (X, Y), he will move to the coordinates (X + x_i, Y + y_i).

E869120 can use these engines in any order, but each engine can be used at most once. He may also choose not to use some of the engines.

He wants to go as far as possible from the origin.
Let (X, Y) be his final coordinates. Find the maximum possible value of \sqrt{X^2 + Y^2}, the distance from the origin.

Constraints

1 \leq N \leq 100

-1 \ 000 \ 000 \leq x_i \leq 1 \ 000 \ 000

-1 \ 000 \ 000 \leq y_i \leq 1 \ 000 \ 000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
: :
x_N y_N

Output

Print the maximum possible final distance from the origin, as a real value.
Your output is considered correct when the relative or absolute error from the true answer is at most 10^{-10}.

Sample Input 1

3
0 10
5 -5
-5 -5

Sample Output 1

10.000000000000000000000000000000000000000000000000

The final distance from the origin can be 10 if we use the engines in one of the following three ways:

Use Engine 1 to move to (0, 10).

Use Engine 2 to move to (5, -5), and then use Engine 3 to move to (0, -10).

Use Engine 3 to move to (-5, -5), and then use Engine 2 to move to (0, -10).

The distance cannot be greater than 10, so the maximum possible distance is 10.

Sample Input 2

5
1 1
1 0
0 1
-1 0
0 -1

Sample Output 2

2.828427124746190097603377448419396157139343750753

The maximum possible final distance is 2 \sqrt{2} = 2.82842....
One of the ways to achieve it is:

Use Engine 1 to move to (1, 1), and then use Engine 2 to move to (2, 1), and finally use Engine 3 to move to (2, 2).

Sample Input 3

5
1 1
2 2
3 3
4 4
5 5

Sample Output 3

21.213203435596425732025330863145471178545078130654

If we use all the engines in the order 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5, we will end up at (15, 15), with the distance 15 \sqrt{2} = 21.2132... from the origin.

Sample Input 4

3
0 0
0 1
1 0

Sample Output 4

1.414213562373095048801688724209698078569671875376

There can be useless engines with (x_i, y_i) = (0, 0).

Sample Input 5

1
90447 91000

Sample Output 5

128303.000000000000000000000000000000000000000000000000

Note that there can be only one engine.

Sample Input 6

2
96000 -72000
-72000 54000

Sample Output 6

120000.000000000000000000000000000000000000000000000000

There can be only two engines, too.

Sample Input 7

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

Sample Output 7

148.660687473185055226120082139313966514489855137208


;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (sb-int:defconstant-eqx OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0))
    #'equal)
  #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t)
  #-swank (set-dispatch-macro-character
           #\# #\> (lambda (s c p) (declare (ignore c p)) (read s nil (values) 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 solve (n xs ys)
  (let ((phases (make-array n :element-type 'double-float))
        (ords (make-array n :element-type 'uint32)))
    (dotimes (i n)
      (setf (aref ords i) i)
      (setf (aref phases i) (atan (aref ys i) (aref xs i))))
    (setf ords (sort ords (lambda (i j)
                            (< (aref phases i) (aref phases j)))))
    (let ((res 0d0)
          (ords (concatenate '(simple-array uint32 (*)) ords ords)))
      (labels ((calc-dist (x y)
                 (sqrt (+ (* x x) (* y y))))
               (calc (rot)
                 (dotimes (base n)
                   (let* ((base-v (aref ords base))
                          (base-phase (aref phases base-v))
                          (xsum 0d0)
                          (ysum 0d0))
                     (loop for i from base below (+ base n)
                           for new-v = (aref ords i)
                           for new-phase = (let ((p (aref phases new-v)))
                                             (if (> base-phase p)
                                                 (+ p (* 2 pi))
                                                 p))
                           while (<= (- new-phase base-phase) rot)
                           do ;; (dbg base-phase new-phase)
                              (incf xsum (aref xs new-v))
                              (incf ysum (aref ys new-v)))
                     (setq res (max res (calc-dist xsum ysum)))))))
        (calc #.(* 1/2 pi))
        (calc pi)
        res))))

(defun main ()
  (let* ((n (read))
         (xs (make-array n :element-type 'double-float))
         (ys (make-array n :element-type 'double-float)))
    (dotimes (i n)
      (let* ((x (float (read) 1d0))
             (y (float (read) 1d0)))
        (setf (aref xs i) x
              (aref ys i) y)))
    (let ((res 0))
      (setq res (max res (solve n xs ys)))
      (dotimes (i n) (setf (aref xs i) (- (aref xs i))))
      (setq res (max res (solve n xs ys)))
      (println res))))

#-swank (main)

;; For Test
#+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:/msys64/usr/bin/cat.exe" '("/dev/clipboard") :output out)))

#+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 10
5 -5
-5 -5
"
    "10.000000000000000000000000000000000000000000000000
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "5
1 1
1 0
0 1
-1 0
0 -1
"
    "2.828427124746190097603377448419396157139343750753
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "5
1 1
2 2
3 3
4 4
5 5
"
    "21.213203435596425732025330863145471178545078130654
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "3
0 0
0 1
1 0
"
    "1.414213562373095048801688724209698078569671875376
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "1
90447 91000
"
    "128303.000000000000000000000000000000000000000000000000
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "2
96000 -72000
-72000 54000
"
    "120000.000000000000000000000000000000000000000000000000
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "10
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
17 18
19 20
"
    "148.660687473185055226120082139313966514489855137208
")))


;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (ql:quickload '(:cl-debug-print :fiveam))
  #-swank (set-dispatch-macro-character #\# #\> (lambda (s c p) (declare (ignore c p)) (read s nil (values) 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))
         (x 0d0))
    (dotimes (i n)
      (let ((a (read)))
        (incf x (/ 1d0 a))))
    (println (/ 1d0 x))))

#-swank (main)

;; For Test
#+swank
(defun io-equal (in-string out-string &key (target #'main) (test #'equal))
  "Passes IN-STRING to *STANDARD-INPUT*, executes TARGET, 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 target)))))))

#+swank
(defun get-clipbrd ()
  (with-output-to-string (out)
    (run-program "C:/msys64/usr/bin/cat.exe" '("/dev/clipboard") :output out)))

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

Score : 200 points

Problem Statement

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

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

Constraints

1 \leq N \leq 100

1 \leq A_i \leq 1000

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N

Output

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

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

Sample Input 1

2
10 30

Sample Output 1

7.5

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

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

Sample Input 2

3
200 200 200

Sample Output 2

66.66666666666667

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

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

Sample Input 3

1
1000

Sample Output 3

1000

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

Printing +1000.0 and so on will also be accepted.


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

;; BEGIN_INSERTED_CONTENTS
;;;
;;; 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 (mod1 base1 cumul1 powers1 mod2 base2 cumul2 powers2)))
  ;; lower 31-bit value
  (mod1 2147483647 :type (unsigned-byte 31))
  (base1 1059428526 :type (unsigned-byte 31))
  (cumul1 nil :type (simple-array (unsigned-byte 31) (*)))
  (powers1 nil :type (simple-array (unsigned-byte 31) (*)))
  ;; upper 31-bit value
  (mod2 2147483629 :type (unsigned-byte 31))
  (base2 2090066834 :type (unsigned-byte 31))
  (cumul2 nil :type (simple-array (unsigned-byte 31) (*)))
  (powers2 nil :type (simple-array (unsigned-byte 31) (*))))

;; 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 (mod1 mod2 base1 base2 rhash)
  "Chooses two appropriate pairs of moduli and bases."
  (declare ((or null (unsigned-byte 31)) mod1 mod2 base1 base2))
  (when rhash
    (return-from %choose-moduli
      (values (rhash-mod1 rhash)
              (rhash-mod2 rhash)
              (rhash-base1 rhash)
              (rhash-base2 rhash))))
  (let* ((rand1 (random (length *moduli-table*)))
         (rand2 (loop (let ((tmp (random (length *moduli-table*))))
                        (unless (= tmp rand1)
                          (return tmp))))))
    (if mod1
        (progn
          (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
          (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))

(defun make-rhash (vector &key (key #'char-code) mod1 mod2 base1 base2 rhash)
  "Returns the table of rolling-hash of VECTOR modulo MOD1 and MOD2. KEY is
applied to each element of VECTOR prior to computing the hash value. If moduli
and bases are NIL, this function randomly chooses them. If RHASH is specified,
the same moduli and bases as RHASH is adopted.

MOD[1|2] := NIL | unsigned 31-bit prime number
BASE1 := NIL | 1 | 2 | ... | MOD1 - 1
BASE2 := NIL | 1 | 2 | ... | MOD2 - 1
KEY := FUNCTION returning FIXNUM
RHASH := NIL | RHASH"
  (declare (optimize (speed 3))
           (vector vector)
           ((or null (unsigned-byte 31)) mod1 mod2 base1 base2)
           (function key))
  (multiple-value-bind (mod1 mod2 base1 base2) (%choose-moduli mod1 mod2 base1 base2 rhash)
    (declare ((unsigned-byte 31) mod1 mod2 base1 base2))
    (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) base1) mod1)
              (aref powers2 (+ i 1))
              (mod (* (aref powers2 i) base2) mod2))
        (let ((sum1 (+ (mod (* base1 (aref cumul1 i)) mod1)
                       (mod (the fixnum (funcall key (aref vector i))) mod1)))
              (sum2 (+ (mod (* base2 (aref cumul2 i)) mod2)
                       (mod (the fixnum (funcall key (aref vector i))) mod2))))
          (setf (aref cumul1 (+ i 1)) (if (> sum1 mod1)
                                          (- sum1 mod1)
                                          sum1)
                (aref cumul2 (+ i 1)) (if (> sum2 mod2)
                                          (- sum2 mod2)
                                          sum2))))
      (%make-rhash mod1 base1 cumul1 powers1 mod2 base2 cumul2 powers2))))

(declaim (ftype (function * (values (unsigned-byte 62) &optional)) rhash-vector-hash)
         (inline rhash-vector-hash))
(defun rhash-vector-hash (rhash 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* ((mod1 (rhash-mod1 rhash))
         (mod2 (rhash-mod2 rhash))
         (base1 (rhash-base1 rhash))
         (base2 (rhash-base2 rhash))
         (size (length vector))
         (lower 0)
         (upper 0))
    (declare ((unsigned-byte 31) lower upper))
    (dotimes (i size)
      (setf lower (mod (+ (* base1 lower)
                          (mod (the fixnum (funcall key (aref vector i))) mod1))
                       mod1))
      (setf upper (mod (+ (* base2 upper)
                          (mod (the fixnum (funcall key (aref vector i))) mod2))
                       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))
        (mod1 (rhash-mod1 rhash))
        (cumul2 (rhash-cumul2 rhash))
        (powers2 (rhash-powers2 rhash))
        (mod2 (rhash-mod2 rhash)))
    (let ((lower (+ (aref cumul1 r)
                    (- mod1 (mod (* (aref cumul1 l) (aref powers1 (- r l))) mod1))))
          (upper (+ (aref cumul2 r)
                    (- mod2 (mod (* (aref cumul2 l) (aref powers2 (- r l))) mod2)))))
      (let ((lower (if (> lower mod1) (- lower mod1) lower))
            (upper (if (> upper mod2) (- upper mod2) upper)))
        (declare ((unsigned-byte 31) lower upper))
        (dpb upper (byte 31 31) lower)))))

(declaim (inline rhash-concat))
(defun rhash-concat (rhash hash1 hash2 length2)
  "Returns the hash value of the concatenated sequence.

HASH1 := hash value of the first sequence
HASH2 := hash value of the second sequence
LENGTH2 := length of the second sequence."
  (declare ((unsigned-byte 62) hash1 hash2)
           ((integer 0 #.most-positive-fixnum) length2))
  (let* ((hash1-lower (ldb (byte 31 0) hash1))
         (hash1-upper (ldb (byte 31 31) hash1))
         (hash2-lower (ldb (byte 31 0) hash2))
         (hash2-upper (ldb (byte 31 31) hash2))
         (mod1 (rhash-mod1 rhash))
         (mod2 (rhash-mod2 rhash))
         (res-lower (mod (+ hash2-lower
                            (* hash1-lower
                               (aref (rhash-powers1 rhash) length2)))
                         mod1))
         (res-upper (mod (+ hash2-upper
                            (* hash1-upper
                               (aref (rhash-powers2 rhash) length2)))
                         mod2)))
    (declare ((unsigned-byte 31) res-lower res-upper))
    (dpb res-upper (byte 31 31) res-lower)))

(declaim (ftype (function * (values (integer 0 #.most-positive-fixnum) &optional)) rhash-get-lcp))
(defun rhash-get-lcp (rhash1 start1 rhash2 start2)
  "Returns the length of the longest common prefix of two suffixes which begin
at START1 and START2."
  (declare (optimize (speed 3))
           ((mod #.most-positive-fixnum) start1 start2))
  (assert (and (= (rhash-mod1 rhash1) (rhash-mod1 rhash2))
               (= (rhash-mod2 rhash1) (rhash-mod2 rhash2))))
  (assert (and (< start1 (length (rhash-cumul1 rhash1)))
               (< start2 (length (rhash-cumul1 rhash2)))))
  (let ((max-length (min (- (length (rhash-cumul1 rhash1)) start1 1)
                         (- (length (rhash-cumul1 rhash2)) start2 1))))
    (declare (optimize (safety 0)))
    (labels ((bisect (ok ng)
               (declare ((integer 0 #.most-positive-fixnum) ok ng))
               (if (<= (- ng ok) 1)
                   ok
                   (let ((mid (ash (+ ng ok) -1)))
                     (if (= (rhash-query rhash1 start1 (+ start1 mid))
                            (rhash-query rhash2 start2 (+ start2 mid)))
                         (bisect mid ng)
                         (bisect ok mid))))))
      (bisect 0 (+ 1 max-length)))))

(defun map-prefix-hash (rhash vector function &key (start 0) end)
  "Applies FUNCTION to the hash value of each prefix of VECTOR (in ascending
order, including null prefix)."
  (declare (vector vector)
           (function function)
           ((integer 0 #.most-positive-fixnum) start)
           ((or null (integer 0 #.most-positive-fixnum)) end))
  (let* ((end (or end (length vector)))
         (mod1 (rhash-mod1 rhash))
         (mod2 (rhash-mod2 rhash))
         (base1 (rhash-base1 rhash))
         (base2 (rhash-base2 rhash))
         (lower 0)
         (upper 0))
    (loop for i from start below end
          do (funcall function (dpb upper (byte 31 31) lower))
             (setq lower (mod (* lower base1) mod1)
                   upper (mod (* upper base2) mod2)))))

(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* ((s (read-line))
         (target (read-line))
         (slen (length s))
         (table (make-array (list 26 slen) :element-type 'int32 :initial-element -1)))
    (declare (simple-string s target))
    (dotimes (i slen)
      (let ((c (- (char-code (aref s i)) 97)))
        (loop for j from i downto 0
              while (= (aref table c j) -1)
              do (setf (aref table c j) i))))
    (dbg table)
    (let ((spos 0)
          (count 1))
      (dotimes (i (length target))
        (let* ((c (- (char-code (aref target i)) 97))
               (next-pos (aref table c spos)))
          (dbg c next-pos)
          (when (= -1 next-pos)
            (setq next-pos (aref table c 0))
            (incf count)
            (when (= -1 next-pos)
              (println -1)
              (return-from main)))
          (setq spos next-pos)))
      (let ((res (+ 1 spos (* slen (- count 1)))))
        (println res)))))

#-swank (main)

Score : 500 points

Problem Statement

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

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

Notes

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

Constraints

1 \leq |s| \leq 10^5

1 \leq |t| \leq 10^5

s and t consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

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

Sample Input 1

contest
son

Sample Output 1

10

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

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

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

Sample Input 2

contest
programming

Sample Output 2

-1

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

Sample Input 3

contest
sentence

Sample Output 3

33

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