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

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

(in-package :cl-user)

(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 calc-prob))
(defun calc-prob (prev-score new-score temp)
  (handler-bind ((floating-point-overflow (lambda (c) #>c (return-from calc-prob 1d0))))
    (exp (/ (- new-score prev-score) temp))))

(defun initialize-scores (d cs ss state)
  (declare (fixnum d)
           ((simple-array uint8 (*)) cs state)
           ((simple-array uint32 (* *)) ss))
  (let* ((prevs (make-array 26 :element-type 'int32 :initial-element -1))
         (res (make-array 26 :element-type 'int32 :initial-element 0)))
    (dotimes (i d)
      (let ((type (aref state i)))
        (incf (aref res type) (aref ss i type))
        (setf (aref prevs type) i)
        (dotimes (j 26)
          (decf (aref res j) (* (aref cs j) (- i (aref prevs j)))))))
    res))

(defun update-scores! (d cs ss state scores old-type new-type)
  (declare #.OPT
           (uint31 d)
           (fixnum old-type new-type)
           ((simple-array uint8 (*)) cs state)
           ((simple-array uint32 (365 26)) ss)
           ((simple-array int32 (*)) scores))
  (setf (aref scores old-type) 0
        (aref scores new-type) 0)
  (let* ((old-prev -1)
         (new-prev -1))
    (declare (int16 old-prev new-prev))
    (dotimes (i d)
      (let ((type (aref state i)))
        (cond ((= type old-type)
               (incf (aref scores type) (aref ss i type))
               (setq old-prev i)
               (decf (aref scores new-type) (* (aref cs new-type) (- i new-prev))))
              ((= type new-type)
               (incf (aref scores type) (aref ss i type))
               (setq new-prev i)
               (decf (aref scores old-type) (* (aref cs old-type) (- i old-prev))))
              (t (decf (aref scores old-type) (* (aref cs old-type) (- i old-prev)))
                 (decf (aref scores new-type) (* (aref cs new-type) (- i new-prev)))))))
    scores))

(defun solve (d cs ss max-types &optional (time 1.93d0))
  (declare #.OPT
           (double-float time)
           (uint32 d)
           ((simple-array uint8 (*)) cs max-types)
           ((simple-array uint32 (* *)) ss))
  (labels ((calc (scores) (loop for x across scores sum x of-type fixnum)))
    (let* ((state (copy-seq max-types))
           (scores (initialize-scores d cs ss state))
           (score (calc scores))
           (opt-scores (copy-seq scores))
           (opt-score (calc opt-scores))
           (opt-state (copy-seq state))
           (start-temp 1000d0)
           (end-temp 1d0)
           (loop-count 0))
      (declare (fixnum loop-count)
               (fixnum opt-score)
               ((simple-array int32 (*)) scores opt-scores)
               (double-float start-temp end-temp))
      (sb-int:with-progressive-timeout (rem-time :seconds time)
        (loop
          (incf loop-count)
          (let ((rem-time (float (rem-time) 1d0)))
            (when (zerop rem-time)
              (return))
            (let* ((day (random d))
                   (old-type (aref state day))
                   (new-type (random 26))
                   (old-score score))
              (unless (= old-type new-type)
                ;; tmp. update
                (setf (aref state day) new-type)
                (update-scores! d cs ss state scores old-type new-type)
                (let* ((new-score (calc scores))
                       (temp (+ end-temp (/ (* rem-time (- start-temp end-temp)) time)))
                       (prob (calc-prob old-score new-score temp)))
                  (declare (double-float temp))
                  ;; rollback
                  (when (> new-score opt-score)
                    (setq opt-scores (copy-seq scores)
                          opt-score new-score
                          opt-state (copy-seq state)))
                  (if (< (random 1.0) prob)
                      (setq score new-score)
                      (progn
                        (setf (aref state day) old-type)
                        (update-scores! d cs ss state scores new-type old-type)))))))))
      #>loop-count
      ;; (assert (= opt-score (calc (initialize-scores d cs ss opt-state))))
      (values opt-state opt-scores))))

(defun print* (state)
  (write-string
   (with-output-to-string (*standard-output* nil :element-type 'base-char)
     (loop for x across state
           do (println (+ x 1))))))

(defun main ()
  (let* ((d (read))
         (cs (make-array 26 :element-type 'uint8 :initial-element 0))
         (ss (make-array (list d 26) :element-type 'uint32 :initial-element 0))
         (max-types (make-array d :element-type 'uint8 :initial-element 0)))
    (dotimes (i 26)
      (setf (aref cs i) (read-fixnum)))
    (dotimes (i d)
      (dotimes (j 26)
        (setf (aref ss i j) (read-fixnum))))
    ;; #>ss
    (dotimes (i d)
      (let ((max-type 0)
            (max 0))
        (dotimes  (j 26)
          (when (>= (aref ss i j) max)
            (setq max-type j
                  max (aref ss i j))))
        (setf (aref max-types i) max-type)))
    (multiple-value-bind (state scores) (solve d cs ss max-types)
      (print* state)
      (loop for x across scores sum x))))

#-swank (main)

(defun bench ()
  (let ((*random-state* (seed-random-state 0)))
    (loop repeat 5
          sum #>(run *dat-pathname*))))
;;;
;;; 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 "powershell.exe" '("-Command" "Get-Clipboard") :output out :search t)))

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

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

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

#+swank
(defun gen-dat ()
  (uiop:with-output-file (out *dat-pathname* :if-exists :supersede)
    (format out "365~%")
    (dotimes (i 26)
      (println (random 101) out))
    (dotimes (i 365)
      (dotimes (j 26)
        (println (random 20001) out)))))


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

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

(in-package :cl-user)

(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 calc-prob))
(defun calc-prob (prev-score new-score temp)
  (handler-bind ((floating-point-overflow (lambda (c) #>c (return-from calc-prob 1d0))))
    (exp (/ (- new-score prev-score) temp))))

(defun initialize-scores (d cs ss state)
  (declare (fixnum d)
           ((simple-array uint8 (*)) cs state)
           ((simple-array uint32 (* *)) ss))
  (let* ((prevs (make-array 26 :element-type 'int32 :initial-element -1))
         (res (make-array 26 :element-type 'int32 :initial-element 0)))
    (dotimes (i d)
      (let ((type (aref state i)))
        (incf (aref res type) (aref ss i type))
        (setf (aref prevs type) i)
        (dotimes (j 26)
          (decf (aref res j) (* (aref cs j) (- i (aref prevs j)))))))
    res))

(defun update-scores! (d cs ss state scores old-type new-type)
  (declare #.OPT
           (uint31 d)
           (fixnum old-type new-type)
           ((simple-array uint8 (*)) cs state)
           ((simple-array uint32 (* *)) ss)
           ((simple-array int32 (*)) scores))
  (setf (aref scores old-type) 0
        (aref scores new-type) 0)
  (let* ((old-prev -1)
         (new-prev -1))
    (declare (int16 old-prev new-prev))
    (dotimes (i d)
      (let ((type (aref state i)))
        (cond ((= type old-type)
               (incf (aref scores type) (aref ss i type))
               (setq old-prev i)
               (decf (aref scores new-type) (* (aref cs new-type) (- i new-prev))))
              ((= type new-type)
               (incf (aref scores type) (aref ss i type))
               (setq new-prev i)
               (decf (aref scores old-type) (* (aref cs old-type) (- i old-prev))))
              (t (decf (aref scores old-type) (* (aref cs old-type) (- i old-prev)))
                 (decf (aref scores new-type) (* (aref cs new-type) (- i new-prev)))))))
    scores))

(defun solve (d cs ss max-types &optional (time 1.93d0))
  (declare #.OPT
           (double-float time)
           (uint32 d)
           ((simple-array uint8 (*)) cs max-types)
           ((simple-array uint32 (* *)) ss))
  (labels ((calc (scores) (loop for x across scores sum x of-type fixnum)))
    (let* ((state (copy-seq max-types))
           (scores (initialize-scores d cs ss state))
           (opt-scores (copy-seq scores))
           (opt-score (calc opt-scores))
           (opt-state (copy-seq state))
           (start-temp 1000d0)
           (end-temp 1d0)
           (loop-count 0))
      (declare (fixnum loop-count)
               (fixnum opt-score)
               ((simple-array int32 (*)) scores opt-scores)
               (double-float start-temp end-temp))
      (sb-int:with-progressive-timeout (rem-time :seconds time)
        (loop
          (incf loop-count)
          (let ((rem-time (float (rem-time) 1d0)))
            (when (zerop rem-time)
              (return))
            (let* ((day (random d))
                   (old-type (aref state day))
                   (new-type (random 26))
                   (old-score (calc scores)))
              (unless (= old-type new-type)
                ;; tmp. update
                (setf (aref state day) new-type)
                (update-scores! d cs ss state scores old-type new-type)
                (let* ((new-score (calc scores))
                       (temp (+ end-temp (/ (* rem-time (- start-temp end-temp)) time)))
                       (prob (calc-prob old-score new-score temp)))
                  (declare (double-float temp))
                  ;; rollback
                  (when (> new-score opt-score)
                    (setq opt-scores (copy-seq scores)
                          opt-score new-score
                          opt-state (copy-seq state)))
                  (when (>= (random 1.0) prob)
                    (setf (aref state day) old-type)
                    (update-scores! d cs ss state scores new-type old-type))))))))
      #>loop-count
      ;; (assert (= opt-score (calc (initialize-scores d cs ss opt-state))))
      (values opt-state opt-scores))))

(defun print* (state)
  (write-string
   (with-output-to-string (*standard-output* nil :element-type 'base-char)
     (loop for x across state
           do (println (+ x 1))))))

(defun main ()
  (let* ((d (read))
         (cs (make-array 26 :element-type 'uint8 :initial-element 0))
         (ss (make-array (list d 26) :element-type 'uint32 :initial-element 0))
         (max-types (make-array d :element-type 'uint8 :initial-element 0)))
    (dotimes (i 26)
      (setf (aref cs i) (read-fixnum)))
    (dotimes (i d)
      (dotimes (j 26)
        (setf (aref ss i j) (read-fixnum))))
    ;; #>ss
    (dotimes (i d)
      (let ((max-type 0)
            (max 0))
        (dotimes  (j 26)
          (when (>= (aref ss i j) max)
            (setq max-type j
                  max (aref ss i j))))
        (setf (aref max-types i) max-type)))
    (multiple-value-bind (state scores) (solve d cs ss max-types)
      (print* state)
      (loop for x across scores sum x))))

#-swank (main)

(defun bench ()
  (let ((*random-state* (seed-random-state 0)))
    (run *dat-pathname*)))
;;;
;;; 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 "powershell.exe" '("-Command" "Get-Clipboard") :output out :search t)))

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

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

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

#+swank
(defun gen-dat ()
  (uiop:with-output-file (out *dat-pathname* :if-exists :supersede)
    (format out "365~%")
    (dotimes (i 26)
      (println (random 101) out))
    (dotimes (i 365)
      (dotimes (j 26)
        (println (random 20001) out)))))

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

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

Problem Statement

AtCoder currently hosts three types of contests: ABC, ARC, and AGC. As the number of users has grown, in order to meet the needs of more users, AtCoder has decided to increase the number of contests to 26 types, from AAC to AZC. For convenience, we number these 26 types as type 1 through type 26. AtCoder wants to schedule contests for D days so that user satisfaction is as high as possible. For every day, AtCoder will hold exactly one contest, and each contest will end on that day. The satisfaction is calculated as follows.

The satisfaction at the beginning of day 1 is 0. Satisfaction can be negative.

Holding contests increases satisfaction. The amount of increase will vary depending on a variety of factors. Specifically, we know in advance that holding a contest of type i on day d will increase the satisfaction by s_{d,i}.

If a particular type of contest is not held for a while, the satisfaction decreases. Each contest type i has an integer c_i, and at the end of each day d=1,2,...,D, the satisfaction decreases as follows. Let \mathrm{last}(d,i) be the last day before day d (including d) on which a contest of type i was held. If contests of type i have never been held yet, we define \mathrm{last}(d,i)=0. At the end of day d, the satisfaction decreases by \sum _{i=1}^{26}c_i \times (d-\mathrm{last}(d,i)).

Please schedule contests on behalf of AtCoder.
If the satisfaction at the end of day D is S, you will get a score of \max(10^6 + S, 0).
There are 50 test cases, and the score of a submission is the total scores for each test case.
You can make submissions multiple times, and the highest score among your submissions will be your score.

Constraints

D = 365

Each c_i is an integer satisfying 0\leq c_i \leq 100.

Each s_{d,i} is an integer satisfying 0\leq s_{d,i} \leq 20000.

Input

Input is given from Standard Input in the following format:

D
c_1 c_2 \cdots c_{26}
s_{1,1} s_{1,2} \cdots s_{1,26}
\vdots
s_{D,1} s_{D,2} \cdots s_{D,26}

Output

Let t_d (1\leq t_d \leq 26) be the type of the contest that will be held at day d.
Print D integers t_d to Standard Output in the following format:

t_1
t_2
\vdots
t_D

Any output that does not follow the above format may result in 0 pointsWA for that test case.

Input Generation

Each integer c_i and s_{d,i} is generated independently and uniformly at random from the integers in the range described in the problem statement.

Sample Input 1

5
86 90 69 51 2 96 71 47 88 34 45 46 89 34 31 38 97 84 41 80 14 4 50 83 7 82
19771 12979 18912 10432 10544 12928 13403 3047 10527 9740 8100 92 2856 14730 1396 15905 6534 4650 11469 3628 8433 2994 10899 16396 18355 11424
6674 17707 13855 16407 12232 2886 11908 1705 5000 1537 10440 10711 4917 10770 17272 15364 19277 18094 3929 3705 7169 6159 18683 15410 9092 4570
6878 4239 19925 1799 375 9563 3445 5658 19857 11401 6997 6498 19933 3848 2426 2146 19745 16880 17773 18359 3921 14172 16730 11157 5439 256
8633 15862 15303 10749 18499 7792 10317 5901 9395 11433 3514 3959 5202 19850 19469 9790 5653 784 18500 10552 17975 16615 7852 197 8471 7452
19855 17918 7990 10572 4333 438 9140 9104 12622 4985 12319 4028 19922 12132 16259 17476 2976 547 19195 19830 16285 4806 4471 9457 2864 2192

Sample Output 1

1
17
13
14
13

Note that this example is a small one for checking the problem specification. It does not satisfy the constraint D=365 and is never actually given as a test case. The final satisfaction with this output is 79325, so the score is 1079325.

Input generator, score calculator, and visualizer

Beginner's Guide

If you don't know what to do, proceed to problem B or C.

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.


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

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

(in-package :cl-user)

(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 calc-prob))
(defun calc-prob (prev-score new-score temp)
  (handler-bind ((floating-point-overflow (lambda (c) #>c (return-from calc-prob 1d0))))
    (exp (/ (- new-score prev-score) temp))))

(defun initialize-scores (d cs ss state)
  (declare (fixnum d)
           ((simple-array uint8 (*)) cs state)
           ((simple-array uint32 (* *)) ss))
  (let* ((prevs (make-array 26 :element-type 'int32 :initial-element -1))
         (res (make-array 26 :element-type 'int32 :initial-element 0)))
    (dotimes (i d)
      (let ((type (aref state i)))
        (incf (aref res type) (aref ss i type))
        (setf (aref prevs type) i)
        (dotimes (j 26)
          (decf (aref res j) (* (aref cs j) (- i (aref prevs j)))))))
    res))

(defun update-scores! (d cs ss state scores old-type new-type)
  (declare #.OPT
           (fixnum d old-type new-type)
           ((simple-array uint8 (*)) cs state)
           ((simple-array uint32 (* *)) ss)
           ((simple-array int32 (*)) scores))
  (setf (aref scores old-type) 0
        (aref scores new-type) 0)
  (let* ((old-prev -1)
         (new-prev -1))
    (declare (int16 old-prev new-prev))
    (dotimes (i d)
      (let ((type (aref state i)))
        (cond ((= type old-type)
               (incf (aref scores type) (aref ss i type))
               (setq old-prev i)
               (decf (aref scores new-type) (* (aref cs new-type) (- i new-prev))))
              ((= type new-type)
               (incf (aref scores type) (aref ss i type))
               (setq new-prev i)
               (decf (aref scores old-type) (* (aref cs old-type) (- i old-prev))))
              (t (decf (aref scores old-type) (* (aref cs old-type) (- i old-prev)))
                 (decf (aref scores new-type) (* (aref cs new-type) (- i new-prev)))))))
    scores))

(defun solve (d cs ss max-types &optional (time 1.93d0))
  (declare (optimize (speed 3))
           (uint32 d)
           ((simple-array uint8 (*)) cs max-types)
           ((simple-array uint32 (* *)) ss))
  (labels ((calc (scores) (loop for x across scores sum x of-type fixnum)))
    (let* ((state (copy-seq max-types))
           (scores (initialize-scores d cs ss state))
           (opt-scores (copy-seq scores))
           (opt-score (calc opt-scores))
           (opt-state (copy-seq state))
           (start-temp 1000d0)
           (end-temp 1d0)
           (loop-count 0))
      (declare (fixnum loop-count)
               (fixnum opt-score)
               ((simple-array int32 (*)) scores opt-scores)
               (double-float start-temp end-temp))
      (sb-int:with-progressive-timeout (rem-time :seconds time)
        (loop
          (incf loop-count)
          (let ((rem-time (float (rem-time) 1d0)))
            (when (zerop rem-time)
              (return))
            (let* ((day (random d))
                   (old-type (aref state day))
                   (new-type (random 26))
                   (old-score (calc scores)))
              (unless (= old-type new-type)
                ;; tmp. update
                (setf (aref state day) new-type)
                (update-scores! d cs ss state scores old-type new-type)
                (let* ((new-score (calc scores))
                       (temp (+ end-temp (/ (* rem-time (- start-temp end-temp)) time)))
                       (prob (calc-prob old-score new-score temp)))
                  (declare (double-float temp))
                  ;; rollback
                  (when (> new-score opt-score)
                    #>new-score
                    (setq opt-scores (copy-seq scores)
                          opt-score new-score
                          opt-state (copy-seq state)))
                  (when (>= (random 1.0) prob)
                    (setf (aref state day) old-type)
                    (update-scores! d cs ss state scores new-type old-type))))))))
      ;; #>loop-count
      ;; (assert (= opt-score (calc (initialize-scores d cs ss opt-state))))
      (values opt-state opt-scores))))

(defun print* (state)
  (write-string
   (with-output-to-string (*standard-output* nil :element-type 'base-char)
     (loop for x across state
           do (println (+ x 1))))))

(defun main ()
  (let* ((d (read))
         (cs (make-array 26 :element-type 'uint8 :initial-element 0))
         (ss (make-array (list d 26) :element-type 'uint32 :initial-element 0))
         (max-types (make-array d :element-type 'uint8 :initial-element 0)))
    (dotimes (i 26)
      (setf (aref cs i) (read-fixnum)))
    (dotimes (i d)
      (dotimes (j 26)
        (setf (aref ss i j) (read-fixnum))))
    ;; #>ss
    (dotimes (i d)
      (let ((max-type 0)
            (max 0))
        (dotimes  (j 26)
          (when (>= (aref ss i j) max)
            (setq max-type j
                  max (aref ss i j))))
        (setf (aref max-types i) max-type)))
    (multiple-value-bind (state score) (solve d cs ss max-types)
      (print* state)
      score)))

#-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 "powershell.exe" '("-Command" "Get-Clipboard") :output out :search t)))

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

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

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

#+swank
(defun gen-dat ()
  (uiop:with-output-file (out *dat-pathname* :if-exists :supersede)
    (format out "365~%")
    (dotimes (i 26)
      (println (random 101) out))
    (dotimes (i 365)
      (dotimes (j 26)
        (println (random 20001) out)))))

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


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

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

(in-package :cl-user)

(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 calc-prob))
(defun calc-prob (prev-score new-score temp)
  ;; (handler-bind ((floating-point-overflow (lambda (c) (declare )))))
  (exp (/ (- new-score prev-score) temp)))

(defun initialize-scores (d cs ss state)
  (declare (fixnum d)
           ((simple-array uint8 (*)) cs state)
           ((simple-array uint32 (* *)) ss))
  (let* ((prevs (make-array 26 :element-type 'int32 :initial-element -1))
         (res (make-array 26 :element-type 'int32 :initial-element 0)))
    (dotimes (i d)
      (let ((type (aref state i)))
        (incf (aref res type) (aref ss i type))
        (setf (aref prevs type) i)
        (dotimes (j 26)
          (decf (aref res j) (* (aref cs j) (- i (aref prevs j)))))))
    res))

(defun update-scores! (d cs ss state scores old-type new-type)
  (declare #.OPT
           (fixnum d old-type new-type)
           ((simple-array uint8 (*)) cs state)
           ((simple-array uint32 (* *)) ss)
           ((simple-array int32 (*)) scores))
  (setf (aref scores old-type) 0
        (aref scores new-type) 0)
  (let* ((old-prev -1)
         (new-prev -1))
    (declare (int32 old-prev new-prev))
    (dotimes (i d)
      (let ((type (aref state i)))
        (cond ((= type old-type)
               (incf (aref scores type) (aref ss i type))
               (setq old-prev i)
               (decf (aref scores type) (* (aref cs type) (- i old-prev))))
              ((= type new-type)
               (incf (aref scores type) (aref ss i type))
               (setq new-prev i)
               (decf (aref scores type) (* (aref cs type) (- i new-prev)))))))
    scores))

(defun solve (d cs ss max-types &optional (time 1.93))
  (declare (optimize (speed 3))
           (uint32 d)
           ((simple-array uint8 (*)) cs max-types)
           ((simple-array uint32 (* *)) ss))
  (labels ((calc (scores) (loop for x across scores sum x of-type fixnum)))
    (let* ((state (copy-seq max-types))
           (scores (initialize-scores d cs ss state))
           (opt-scores (copy-seq scores))
           (opt-score (calc opt-scores))
           (opt-state (copy-seq state))
           (start-temp 1000d0)
           (end-temp 1d0)
           (loop-count 0))
      (declare (fixnum loop-count)
               (fixnum opt-score)
               ((simple-array int32 (*)) scores opt-scores)
               (double-float start-temp end-temp))

      (sb-int:with-progressive-timeout (rem-time :seconds time)
        (loop (incf loop-count)
              (let ((rem-time (rem-time)))
                (when (zerop rem-time)
                  (return))
                (let* ((day (random d))
                       (old-type (aref state day))
                       (new-type (random 26))
                       (old-score (calc scores)))
                  (unless (= old-type new-type)
                    ;; tmp. update
                    (setf (aref state day) new-type)
                    (update-scores! d cs ss state scores old-type new-type)
                    (let* ((new-score (calc scores))
                           (temp (+ end-temp (/ (* rem-time (- start-temp end-temp)) time)))
                           (prob (calc-prob old-score new-score temp)))
                      (declare (double-float temp))
                      ;; rollback
                      (when (>= (random 1.0) prob)
                        (setf (aref state day) old-type)
                        (update-scores! d cs ss state scores new-type old-type))
                      (when (> new-score opt-score)
                        (setq opt-scores scores
                              opt-score new-score
                              opt-state (copy-seq state)))))))))
      #>loop-count
      ;; (assert (= opt-score (calc opt-scores)))
      (values opt-state opt-scores))))

(defun print* (state)
  (write-string
   (with-output-to-string (*standard-output* nil :element-type 'base-char)
     (loop for x across state
           do (println (+ x 1))))))

(defun main ()
  (let* ((d (read))
         (cs (make-array 26 :element-type 'uint8 :initial-element 0))
         (ss (make-array (list d 26) :element-type 'uint32 :initial-element 0))
         (max-types (make-array d :element-type 'uint8 :initial-element 0)))
    (dotimes (i 26)
      (setf (aref cs i) (read-fixnum)))
    (dotimes (i d)
      (dotimes (j 26)
        (setf (aref ss i j) (read-fixnum))))
    ;; #>ss
    (dotimes (i d)
      (let ((max-type 0)
            (max 0))
        (dotimes  (j 26)
          (when (>= (aref ss i j) max)
            (setq max-type j
                  max (aref ss i j))))
        (setf (aref max-types i) max-type)))
    (multiple-value-bind (state score) (solve d cs ss max-types)
      (print* state)
      score)))

#-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 "powershell.exe" '("-Command" "Get-Clipboard") :output out :search t)))

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

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

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

#+swank
(defun gen-dat ()
  (uiop:with-output-file (out *dat-pathname* :if-exists :supersede)
    (format out "365~%")
    (dotimes (i 26)
      (println (random 101) out))
    (dotimes (i 365)
      (dotimes (j 26)
        (println (random 20001) out)))))

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


(let* ((dmax (read))
       (c (make-array '(27)))
       (s (make-array (list (1+ dmax) 27)))
       (score 0)
       (last (make-array '(27) :initial-element 0)))
  ; read
  (loop :for i :from 1 :to 26
        :do (setf (aref c i) (read)))
  (loop :for d :from 1 :to dmax
        :do (loop :for i :from 1 :to 26
                  :do (setf (aref s d i) (read))))
  ;
  (defun delta (x d)
    (- (aref s d x)
       (loop :for i :from 1 :to 26
             :sum (* (aref c i) (- d (if (= i x) d (aref last i)))))))

  ;;
  (loop :for d :from 1 :to dmax
        :do (let ((cand-c 1)
                  (cand-v (delta 1 d)))
              (loop :for i :from 2 :to 26
                    :do (let ((v (delta i d)))
                          (when (> v cand-v)
                            (setf cand-c i)
                            (setf cand-v v))))
              (setf (aref last cand-c) d)
              (incf score cand-v)
              ;(format t "SCORE: ~A~%" score)
              (format t "~A~%" cand-c)))
  ;(format t "SCORE: ~A~%" score)
  t)


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

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

(in-package :cl-user)

(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 calc-prob))
(defun calc-prob (prev-score new-score temp)
  ;; (handler-bind ((floating-point-overflow (lambda (c) (declare )))))
  (exp (/ (- new-score prev-score) temp)))

(defun eval-score (d cs ss state)
  (declare #.OPT
           (fixnum d)
           ((simple-array uint32 (*)) cs state)
           ((simple-array uint32 (* *)) ss))
  (let ((prevs (make-array 26 :element-type 'int32 :initial-element -1))
        (res 0))
    (declare (fixnum res))
    (dotimes (i d)
      (let ((type (aref state i)))
        (incf res (aref ss i type))
        (setf (aref prevs type) i)
        (dotimes (j 26)
          (decf res (the fixnum (* (aref cs j) (- i (aref prevs j))))))))
    res))

(defun solve (d cs ss max-types &optional (time 1.9))
  (declare (optimize (speed 3))
           (uint32 d)
           ((simple-array uint32 (*)) cs max-types)
           ((simple-array uint32 (* *)) ss))
  (let* ((state (copy-seq max-types))
         (score (eval-score d cs ss state))
         (opt-score score)
         (opt-state (copy-seq state))
         (start-temp 1000d0)
         (end-temp 1d0))
    (declare (fixnum score opt-score)
             (double-float start-temp end-temp))
    (sb-int:with-progressive-timeout (rem-time :seconds time)
      (loop (let ((rem-time (rem-time)))
              (when (zerop rem-time)
                (return))
              (let* ((day (random d))
                     (old-type (aref state day))
                     (new-type (random 26)))
                (setf (aref state day) new-type)
                (let* ((new-score (eval-score d cs ss state))
                       (temp (+ end-temp (/ (* rem-time (- start-temp end-temp)) time)))
                       (prob (calc-prob score new-score temp)))
                  (declare (double-float temp)
                           (fixnum new-score))
                  (if (< (random 1.0) prob)
                      (setq score new-score)
                      (setf (aref state day) old-type))
                  (when (> score opt-score)
                    (setq opt-score #>score
                          opt-state (copy-seq state))))))))
    (values opt-state opt-score)))

(defun print* (state)
  (write-string
   (with-output-to-string (*standard-output* nil :element-type 'base-char)
     (loop for x across state
           do (println (+ x 1))))))

(defun main ()
  (let* ((d (read))
         (cs (make-array 26 :element-type 'uint32 :initial-element 0))
         (ss (make-array (list d 26) :element-type 'uint32 :initial-element 0))
         (max-types (make-array d :element-type 'uint32 :initial-element 0)))
    (dotimes (i 26)
      (setf (aref cs i) (read-fixnum)))
    (dotimes (i d)
      (dotimes (j 26)
        (setf (aref ss i j) (read-fixnum))))
    ;; #>ss
    (dotimes (i d)
      (let ((max-type 0)
            (max 0))
        (dotimes  (j 26)
          (when (>= (aref ss i j) max)
            (setq max-type j
                  max (aref ss i j))))
        (setf (aref max-types i) max-type)))
    (multiple-value-bind (state score) (solve d cs ss max-types)
      (print* state)
      score)))

#-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 "powershell.exe" '("-Command" "Get-Clipboard") :output out :search t)))

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

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

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

#+swank
(defun gen-dat ()
  (uiop:with-output-file (out *dat-pathname* :if-exists :supersede)
    (format out "365~%")
    (dotimes (i 26)
      (println (random 101) out))
    (dotimes (i 365)
      (dotimes (j 26)
        (println (random 20001) out)))))

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


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

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

(in-package :cl-user)

(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 calc-prob))
(defun calc-prob (prev-score new-score temp)
  (exp (/ (- new-score prev-score) temp)))

(defun eval-score (d cs ss state)
  (declare (optimize (speed 3))
           (fixnum d)
           ((simple-array uint32 (*)) cs state)
           ((simple-array uint32 (* *)) ss))
  (let ((prevs (make-array 26 :element-type 'int32 :initial-element -1))
        (res 0))
    (declare (fixnum res))
    (dotimes (i d)
      (let ((type (aref state i)))
        (incf res (aref ss i type))
        (setf (aref prevs type) i)
        (dotimes (j 26)
          (decf res (the fixnum (* (aref cs j) (- i (aref prevs j))))))))
    res))

(defun solve (d cs ss max-types &optional (time 1.9))
  (declare (optimize (speed 3))
           ((simple-array uint32 (*)) cs max-types)
           ((simple-array uint32 (* *)) ss))
  (let* ((state (copy-seq max-types))
         (score (eval-score d cs ss state))
         (opt-score score)
         (opt-state (copy-seq state))
         (start-temp 10000d0)
         (end-temp 10d0))
    (declare (fixnum score opt-score)
             (double-float start-temp end-temp))
    (sb-int:with-progressive-timeout (rem-time :seconds time)
      (loop (let ((rem-time (rem-time)))
              (when (zerop rem-time)
                (return))
              (let* ((day (random d))
                     (old-type (aref state day))
                     (new-type (random 26)))
                (setf (aref state day) new-type)
                (let* ((new-score (eval-score d cs ss state))
                       (temp (+ end-temp (/ (* rem-time (- start-temp end-temp)) time)))
                       (prob (calc-prob score new-score temp)))
                  (declare (double-float temp)
                           (fixnum new-score))
                  (if (< (random 1.0) prob)
                      (setq score new-score)
                      (setf (aref state day) old-type))
                  (when (> score opt-score)
                    (setq opt-score #>score
                          opt-state (copy-seq state))))))))
    opt-state))

(defun print* (state)
  (write-string
   (with-output-to-string (*standard-output* nil :element-type 'base-char)
     (loop for x across state
           do (println (+ x 1))))))

(defun main ()
  (let* ((d (read))
         (cs (make-array 26 :element-type 'uint32 :initial-element 0))
         (ss (make-array (list d 26) :element-type 'uint32 :initial-element 0))
         (max-types (make-array d :element-type 'uint32 :initial-element 0)))
    (dotimes (i 26)
      (setf (aref cs i) (read-fixnum)))
    (dotimes (i d)
      (dotimes (j 26)
        (setf (aref ss i j) (read-fixnum))))
    ;; #>ss
    (dotimes (i d)
      (let ((max-type 0)
            (max 0))
        (dotimes  (j 26)
          (when (>= (aref ss i j) max)
            (setq max-type j
                  max (aref ss i j))))
        (setf (aref max-types i) max-type)))
    (print* (solve d cs ss max-types))))

#-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 "powershell.exe" '("-Command" "Get-Clipboard") :output out :search t)))

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

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

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

#+swank
(defun gen-dat ()
  (uiop:with-output-file (out *dat-pathname* :if-exists :supersede)
    (format out "365~%")
    (dotimes (i 26)
      (println (random 101) out))
    (dotimes (i 365)
      (dotimes (j 26)
        (println (random 20001) out)))))

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


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

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

(in-package :cl-user)

(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 calc-prob))
(defun calc-prob (prev-score new-score temp)
  (exp (/ (- new-score prev-score) temp)))

(defun eval-score (d cs ss state)
  (declare (optimize (speed 3))
           (fixnum d)
           ((simple-array uint32 (*)) cs state)
           ((simple-array uint32 (* *)) ss))
  (let ((prevs (make-array 26 :element-type 'int32 :initial-element -1))
        (res 0))
    (declare (fixnum res))
    (dotimes (i d)
      (let ((type (aref state i)))
        (incf res (aref ss i type))
        (setf (aref prevs i) type)
        (dotimes (j 26)
          (decf res (the fixnum (* (aref cs j) (- i (aref prevs j))))))))
    (max 0 (+ 1000000 res))))

(defun solve (d cs ss max-types &optional (time 1.9))
  (declare (optimize (speed 3))
           ((simple-array uint32 (*)) cs max-types)
           ((simple-array uint32 (* *)) ss))
  (let* ((state (copy-seq max-types))
         (score (eval-score d cs ss state))
         (opt-score score)
         (opt-state (copy-seq state))
         (start-temp 10000d0)
         (end-temp 10d0))
    (declare (fixnum score opt-score)
             (double-float start-temp end-temp))
    (sb-int:with-progressive-timeout (rem-time :seconds time)
      (loop (let ((rem-time (rem-time)))
              (when (zerop rem-time)
                (return))
              (let* ((day (random d))
                     (old-type (aref state day))
                     (new-type (random 26)))
                (setf (aref state day) new-type)
                (let* ((new-score (eval-score d cs ss state))
                       (temp (+ end-temp (/ (* rem-time (- start-temp end-temp)) time)))
                       (prob (calc-prob score new-score temp)))
                  (declare (double-float temp)
                           (fixnum new-score))
                  (if (< (random 1.0) prob)
                      (setq score new-score)
                      (setf (aref state day) old-type))
                  (when (> score opt-score)
                    (setq opt-score score
                          opt-state (copy-seq state))))))))
    opt-state))

(defun print* (state)
  (write-string
   (with-output-to-string (*standard-output* nil :element-type 'base-char)
     (loop for x across state
           do (println (+ x 1))))))

(defun main ()
  (let* ((d (read))
         (cs (make-array 26 :element-type 'uint32 :initial-element 0))
         (ss (make-array (list d 26) :element-type 'uint32 :initial-element 0))
         (max-types (make-array d :element-type 'uint32 :initial-element 0)))
    (dotimes (i 26)
      (setf (aref cs i) (read-fixnum)))
    (dotimes (i d)
      (dotimes (j 26)
        (setf (aref ss i j) (read-fixnum))))
    #>ss
    (dotimes (i d)
      (let ((max-type 0)
            (max 0))
        (dotimes  (j 26)
          (when (>= (aref ss i j) max)
            (setq max-type j
                  max (aref ss i j))))
        (setf (aref max-types i) max-type)))
    (print* (solve d cs ss max-types))))

#-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 "powershell.exe" '("-Command" "Get-Clipboard") :output out :search t)))

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

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

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

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

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


(defun simple (d c s)
  (declare (ignore c))
  (loop :for i :from 1 :to d
        :do (let ((con 1))
              (loop :for j :from 2 :to 26
                    :if (> (aref s i j) (aref s i con))
                    :do (setf con j))
              (format t "~A~%" con))))

(defun main (d c s)
  (simple d c s))

(let* ((d (read))
       (c (make-array '(27)))
       (s (make-array (list (1+ d) 27))))
  ; read
  (loop :for i :from 1 :to 26
        :do (setf (aref c i) (read)))
  (loop :for i :from 1 :to d
        :do (loop :for j :from 1 :to 26
                  :do (setf (aref s i j) (read))))
  (main d c s))


(let* ((n (read))
    (m (read))
    (k (read))
    (a (make-array (+ n 1) :initial-element 0))
    (b (make-array (+ m 1) :initial-element 0))
    (pre m)
    (ans 0))
    (loop for i from 1 to n do
        (incf (aref a i) (+ (read) (aref a (- i 1))))
    )
    (loop for j from 1 to m do
        (incf (aref b j) (+ (read) (aref b (- j 1))))
    )

    (loop for i from 0 to n do
        (progn
        (loop while (and (> (+ (aref a i) (aref b pre)) k) (not (= pre 0))) do
            (decf pre)
        )
        (if (<= (+ (aref a i) (aref b pre)) k)
        (if (< ans (+ i pre))
            (setq ans (+ i pre))
        )
        )
        )
    )
    (princ ans)
)

Score : 300 points

Problem Statement

We have two desks: A and B. Desk A has a vertical stack of N books on it, and Desk B similarly has M books on it.

It takes us A_i minutes to read the i-th book from the top on Desk A (1 \leq i \leq N), and B_i minutes to read the i-th book from the top on Desk B (1 \leq i \leq M).

Consider the following action:

Choose a desk with a book remaining, read the topmost book on that desk, and remove it from the desk.

How many books can we read at most by repeating this action so that it takes us at most K minutes in total? We ignore the time it takes to do anything other than reading.

Constraints

1 \leq N, M \leq 200000

1 \leq K \leq 10^9

1 \leq A_i, B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

Print an integer representing the maximum number of books that can be read.

Sample Input 1

3 4 240
60 90 120
80 150 80 150

Sample Output 1

3

In this case, it takes us 60, 90, 120 minutes to read the 1-st, 2-nd, 3-rd books from the top on Desk A, and 80, 150, 80, 150 minutes to read the 1-st, 2-nd, 3-rd, 4-th books from the top on Desk B, respectively.

We can read three books in 230 minutes, as shown below, and this is the maximum number of books we can read within 240 minutes.

Read the topmost book on Desk A in 60 minutes, and remove that book from the desk.

Read the topmost book on Desk B in 80 minutes, and remove that book from the desk.

Read the topmost book on Desk A in 90 minutes, and remove that book from the desk.

Sample Input 2

3 4 730
60 90 120
80 150 80 150

Sample Output 2

7

Sample Input 3

5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000

Sample Output 3

0

Watch out for integer overflows.


(let* ((n (read))
    (m (read))
    (k (read))
    (a (make-array (+ n 1) :initial-element 0))
    (b (make-array (+ m 1) :initial-element 0))
    (pre m)
    (ans 0))
    (loop for i from 1 to n do
        (incf (aref a i) (+ (read) (aref a (- i 1))))
    )
    (loop for j from 1 to m do
        (incf (aref b j) (+ (read) (aref b (- j 1))))
    )

    (loop for i from 0 to n do
        (progn
        (loop while (and (> (+ (aref a i) (aref b pre)) k) (not (= pre 0))) do
            (decf pre)
        )
        (if (< ans (+ i pre))
            (setq ans (+ i pre))
        )
        )
    )
    (princ ans)
)


(let* ((n (read))
    (m (read))
    (k (read))
    (a (make-array (+ n 1) :initial-element 0))
    (b (make-array (+ m 1) :initial-element 0))
    (pre m)
    (ans 0))
    (loop for i from 1 to n do
        (incf (aref a i) (+ (read) (aref a (- i 1))))
    )
    (loop for j from 1 to m do
        (incf (aref b j) (+ (read) (aref b (- j 1))))
    )

    (loop for i from 0 to n do
        (progn
        (loop while (and (>= (+ (aref a i) (aref b pre)) k) (not (= pre 0))) do
            (decf pre)
        )
        (if (< ans (+ i pre))
            (setq ans (+ i pre))
        )
        )
    )
    (princ ans)
)


(let* ((n (read))
    (m (read))
    (k (read))
    (a (make-array (+ n 1) :initial-element 0))
    (b (make-array (+ m 1) :initial-element 0))
    (pre m)
    (ans 0))
    (loop for i from 1 to n do
        (incf (aref a i) (+ (read) (aref a (- i 1))))
    )
    (loop for j from 1 to m do
        (incf (aref b j) (+ (read) (aref b (- j 1))))
    )

    (loop for i from 0 below (+ n 1) do
        (progn
        (loop while (> (+ (aref a i) (aref b pre)) k) do
            (decf pre)
        )
        (if (< ans (+ i pre))
            (setq ans (+ i pre))
        )
        )
    )
    (princ ans)
)


(let* ((n (read))
       (m (read))
       (k (read))
       (a (make-array `(,(1+ n)) :initial-element 0))
       (b (make-array `(,(1+ m)) :initial-element 0))
       (bestj m)
       (ans 0))
  ; read
  (loop :with acc := 0
        :for i :from 1 :to n
        :do (setf (aref a i) (incf acc (read))))
  (loop :with acc := 0
        :for i :from 1 :to m
        :do (setf (aref b i) (incf acc (read))))
  ;
  (loop :for i :from 0 :to n
        :do (loop :named inner
                  :for j :downfrom bestj :to 0
                  :if (<= (+ (aref a i) (aref b j)) k)
                  :do (progn
                        (setf ans (max ans (+ i j)))
                        (setf bestj j)
                        (return-from inner))))
  (format t "~A~%" ans))


(let* ((n (read))
       (m (read))
       (k (read))
       (a (make-array `(,(1+ n))))
       (b (make-array `(,(1+ m))))
       (amax 0)
       (bmax 0)
       (atop 1)
       (btop 1)
       (alast 0)
       (blast 0)
       (arest 0)
       (brest 0)
       (ans 0))
  ; read
  (loop :for i :from 1 :to n
        :do (setf (aref a i) (read))
        :if (>= k (+ amax (aref a i)))
        :do (progn
              (incf amax (aref a i))
              (incf arest)
              (setf alast i)))
  (loop :for i :from 1 :to m
        :do (setf (aref b i) (read))
        :if (>= k (+ bmax (aref b i)))
        :do (progn
              (incf bmax (aref b i))
              (incf brest)
              (setf blast i)))
  ;
  (loop :named main
        :do (let ((at (if (<= atop n) (aref a atop) nil))
                  (bt (if (<= btop m) (aref b btop) nil)))
              (cond ((and (null at) (null bt))
                     (return-from main))
                    ((or (null bt) (> arest brest) (and at (/= brest 0) (= arest brest) (>= (aref b blast) at)))
                     (if (or (null at) (> at k)) (return-from main))
                     ;(format t "A ~A ~A~%" arest brest)
                     (incf ans)
                     (decf amax at)
                     (decf arest)
                     (decf k at)
                     (loop :for i :downfrom blast :to 1
                           :while (> bmax k)
                           :do (progn
                                 (decf bmax (aref b i))
                                 (decf brest)
                                 (decf blast)))
                     (incf atop))
                    (t
                     (if (or (null bt) (> bt k)) (return-from main))
                     ;(format t "B ~A ~A~%" arest brest)
                     (incf ans)
                     (decf bmax bt)
                     (decf brest)
                     (decf k bt)
                     (loop :for i :downfrom alast :to 1
                           :while (> amax k)
                           :do (progn
                                 (decf amax (aref a i))
                                 (decf arest)
                                 (decf alast)))
                     (incf btop)))))
  (format t "~A~%" ans))


(let* ((n (read))
       (m (read))
       (k (read))
       (a (make-array `(,(1+ n))))
       (b (make-array `(,(1+ m))))
       (amax 0)
       (bmax 0)
       (atop 1)
       (btop 1)
       (alast 0)
       (blast 0)
       (arest 0)
       (brest 0)
       (ans 0))
  ; read
  (loop :for i :from 1 :to n
        :do (setf (aref a i) (read))
        :if (>= k (+ amax (aref a i)))
        :do (progn
              (incf amax (aref a i))
              (incf arest)
              (setf alast i)))
  (loop :for i :from 1 :to m
        :do (setf (aref b i) (read))
        :if (>= k (+ bmax (aref b i)))
        :do (progn
              (incf bmax (aref b i))
              (incf brest)
              (setf blast i)))
  ;
  (loop :named main
        :do (let ((at (if (<= atop n) (aref a atop) nil))
                  (bt (if (<= btop m) (aref b btop) nil)))
              (cond ((and (null at) (null bt))
                     (return-from main))
                    ((or (null bt) (> arest brest) (and at (= arest brest) (>= (aref b blast) at)))
                     (if (or (null at) (> at k)) (return-from main))
                     ;(format t "A ~A ~A~%" arest brest)
                     (incf ans)
                     (decf amax at)
                     (decf arest)
                     (decf k at)
                     (loop :for i :downfrom blast :to 1
                           :while (> bmax k)
                           :do (progn
                                 (decf bmax (aref b i))
                                 (decf brest)
                                 (decf blast)))
                     (incf atop))
                    (t
                     (if (or (null bt) (> bt k)) (return-from main))
                     ;(format t "B ~A ~A~%" arest brest)
                     (incf ans)
                     (decf bmax bt)
                     (decf brest)
                     (decf k bt)
                     (loop :for i :downfrom alast :to 1
                           :while (> amax k)
                           :do (progn
                                 (decf amax (aref a i))
                                 (decf arest)
                                 (decf alast)))
                     (incf btop)))))
  (format t "~A~%" ans))


(let* ((n (read))
       (m (read))
       (k (read))
       (a (make-array `(,(1+ n))))
       (b (make-array `(,(1+ m))))
       (amax 0)
       (bmax 0)
       (atop 1)
       (btop 1)
       (alast 0)
       (blast 0)
       (arest 0)
       (brest 0)
       (ans 0))
  ; read
  (loop :for i :from 1 :to n
        :do (setf (aref a i) (read))
        :if (>= k (+ amax (aref a i)))
        :do (progn
              (incf amax (aref a i))
              (incf arest)
              (setf alast i)))
  (loop :for i :from 1 :to m
        :do (setf (aref b i) (read))
        :if (>= k (+ bmax (aref b i)))
        :do (progn
              (incf bmax (aref b i))
              (incf brest)
              (setf blast i)))
  ;
  (loop :named main
        :do (let ((at (if (<= atop n) (aref a atop) nil))
                  (bt (if (<= btop m) (aref b btop) nil)))
              (cond ((and (null at) (null bt))
                     (return-from main))
                    ((or (null bt) (> arest brest) (and at (= arest brest) (> (aref b blast) at)))
                     (if (or (null at) (> at k)) (return-from main))
                     ;(format t "A ~A ~A~%" arest brest)
                     (incf ans)
                     (decf amax at)
                     (decf arest)
                     (decf k at)
                     (loop :for i :downfrom blast :to 1
                           :while (> bmax k)
                           :do (progn
                                 (decf bmax (aref b i))
                                 (decf brest)
                                 (decf blast)))
                     (incf atop))
                    (t
                     (if (or (null bt) (> bt k)) (return-from main))
                     ;(format t "B ~A ~A~%" arest brest)
                     (incf ans)
                     (decf bmax bt)
                     (decf brest)
                     (decf k bt)
                     (loop :for i :downfrom alast :to 1
                           :while (> amax k)
                           :do (progn
                                 (decf amax (aref a i))
                                 (decf arest)
                                 (decf alast)))
                     (incf btop)))))
  (format t "~A~%" ans))


(let* ((n (read))
       (m (read))
       (k (read))
       (a (make-array `(,(1+ n))))
       (b (make-array `(,(1+ m))))
       (amax 0)
       (bmax 0)
       (atop 1)
       (btop 1)
       (alast 0)
       (blast 0)
       (arest 0)
       (brest 0)
       (ans 0))
  ; read
  (loop :for i :from 1 :to n
        :do (setf (aref a i) (read))
        :if (>= k (+ amax (aref a i)))
        :do (progn
              (incf amax (aref a i))
              (incf arest)
              (setf alast i)))
  (loop :for i :from 1 :to m
        :do (setf (aref b i) (read))
        :if (>= k (+ bmax (aref b i)))
        :do (progn
              (incf bmax (aref b i))
              (incf brest)
              (setf blast i)))
  ;
  (loop :named main
        :do (let ((at (if (<= atop n) (aref a atop) nil))
                  (bt (if (<= btop m) (aref b btop) nil)))
              (cond ((and (null at) (null bt))
                     (return-from main))
                    ((or (null bt) (= 0 bmax) (and at (or (> arest brest) (and (= arest brest) (> (aref b blast) at)))))
                     (if (or (null at) (> at k)) (return-from main))
                     ;(format t "A ~A ~A~%" alast blast)
                     (incf ans)
                     (decf amax at)
                     (decf arest)
                     (decf k at)
                     (loop :for i :downfrom blast :to 1
                           :while (> bmax k)
                           :do (progn
                                 (decf bmax (aref b i))
                                 (decf brest)
                                 (decf blast)))
                     (incf atop))
                    (t
                     (if (or (null bt) (> bt k)) (return-from main))
                     ;(format t "B ~A ~A~%" amax bmax)
                     (incf ans)
                     (decf bmax bt)
                     (decf brest)
                     (decf k bt)
                     (loop :for i :downfrom alast :to 1
                           :while (> amax k)
                           :do (progn
                                 (decf amax (aref a i))
                                 (decf arest)
                                 (decf alast)))
                     (incf btop)))))
  (format t "~A~%" ans))


(let* ((n (read))
       (m (read))
       (k (read))
       (a (make-array `(,(1+ n))))
       (b (make-array `(,(1+ m))))
       (amax 0)
       (bmax 0)
       (atop 1)
       (btop 1)
       (alast 0)
       (blast 0)
       (arest 0)
       (brest 0)
       (ans 0))
  ; read
  (loop :for i :from 1 :to n
        :do (setf (aref a i) (read))
        :if (>= k (+ amax (aref a i)))
        :do (progn
              (incf amax (aref a i))
              (incf arest)
              (setf alast i)))
  (loop :for i :from 1 :to m
        :do (setf (aref b i) (read))
        :if (>= k (+ bmax (aref b i)))
        :do (progn
              (incf bmax (aref b i))
              (incf brest)
              (setf blast i)))
  ;
  (loop :named main
        :do (let ((at (if (<= atop n) (aref a atop) nil))
                  (bt (if (<= btop m) (aref b btop) nil)))
              (cond ((and (null at) (null bt))
                     (return-from main))
                    ((or (null bt) (= 0 bmax) (and at (or (> arest brest) (and (= arest brest) (> bt at)))))
                     (if (or (null at) (> at k)) (return-from main))
                     ;(format t "A ~A ~A~%" alast blast)
                     (incf ans)
                     (decf amax at)
                     (decf arest)
                     (decf k at)
                     (loop :for i :downfrom blast :to 1
                           :while (> bmax k)
                           :do (progn
                                 (decf bmax (aref b i))
                                 (decf brest)
                                 (decf blast)))
                     (incf atop))
                    (t
                     (if (or (null bt) (> bt k)) (return-from main))
                     ;(format t "B ~A ~A~%" amax bmax)
                     (incf ans)
                     (decf bmax bt)
                     (decf brest)
                     (decf k bt)
                     (loop :for i :downfrom alast :to 1
                           :while (> amax k)
                           :do (progn
                                 (decf amax (aref a i))
                                 (decf arest)
                                 (decf alast)))
                     (incf btop)))))
  (format t "~A~%" ans))


(defun number-of-readable-books+rest-time (books time-limit)
  (labels ((rec (books time-limit nbook)
             (declare (optimize (speed 3))
                      (sb-ext:muffle-conditions sb-ext:compiler-note))
             (if (or (endp books)
                     (< time-limit (first books)))
                 (values nbook time-limit)
                 (rec (rest books) (- time-limit (first books)) (1+ nbook)))))
    (rec books time-limit 0)))

(let* ((n (read))
       (m (read))
       (k (read))
       (as (loop :repeat n :collect (read)))
       (bs (loop :repeat m :collect (read))))
  (declare (type (integer 1 200000) n m)
           (type (integer 1 1000000000) k))
  (multiple-value-bind (number-of-readable-books rest-time) (number-of-readable-books+rest-time as k)
    (princ
      (loop :for na :below (1+ number-of-readable-books)
            :maximize (+ na (number-of-readable-books+rest-time bs rest-time))))))


(defun number-of-readable-books+rest-time (books time-limit)
  (labels ((rec (books time-limit nbook)
             (declare (optimize (speed 3))
                      (sb-ext:muffle-conditions sb-ext:compiler-note))
             (if (or (endp books)
                     (< time-limit (first books)))
                 (values nbook time-limit)
                 (rec (rest books) (- time-limit (first books)) (1+ nbook)))))
    (rec books time-limit 0)))

(let* ((n (read))
       (m (read))
       (k (read))
       (as (loop :repeat n :collect (read)))
       (bs (loop :repeat m :collect (read))))
  (declare (type (integer 1 200000) n m)
           (type (integer 1 1000000000) k))
  (multiple-value-bind (number-of-readable-books rest-time) (number-of-readable-books+rest-time as k)
    (loop :for na :below (1+ number-of-readable-books)
          :maximize (+ na (number-of-readable-books+rest-time bs rest-time)))))


(let* ((n (read))
    (m (read))
    (k (read))
    (a (make-array n))
    (b (make-array m))
    (ans_time 0)
    (ans 0))
    (dotimes (i n)
    (setf (aref a i) (read)))
    (dotimes (j m)
    (setf (aref b j) (read))
    )
    (let ((i 0)
        (j 0)
        (hikaku_a 0)
        (hikaku_b 0))
        (loop while (< ans_time k) do
            (if (> n i)
                (setq hikaku_a (aref a i))
                (setq hikaku_a -1)
            )
            (if (> m j)
                (setq hikaku_b (aref b i))
                (setq hikaku_b -1)
            )

            (if (= hikaku_a -1)
                (if (= hikaku_b -1)
                    (return)
                    (if (>= k (+ ans_time hikaku_b))
                        (progn
                        (incf j)
                        (incf ans)
                        (incf ans_time hikaku_b)
                        )
                        (return)
                    )
                )
                (if (= hikaku_b -1)
                    (if (>= k (+ ans_time hikaku_a))
                        (progn
                        (incf i)
                        (incf ans)
                        (incf ans_time hikaku_a)
                        )
                        (return)
                    )
                    (if (>= hikaku_a hikaku_b)
                        (if (>= k (+ ans_time hikaku_b))
                        (progn
                        (incf j)
                        (incf ans)
                        (incf ans_time hikaku_b)
                        )
                        (return)
                        )
                        (if (>= k (+ ans_time hikaku_a))
                        (progn
                        (incf i)
                        (incf ans)
                        (incf ans_time hikaku_a)
                        )
                        (return)
                    )
                    )
                )
            )
        )

    )
    (princ ans)
)


(defun number-of-readable-books+rest-time (books time-limit)
  (labels ((rec (books time-limit nbook)
             (declare (optimize (speed 3)))
             (if (or (endp books)
                     (< time-limit (first books)))
                 (values nbook time-limit)
                 (rec (rest books) (- time-limit (first books)) (1+ nbook)))))
    (rec books time-limit 0)))

(let* ((n (read))
       (m (read))
       (k (read))
       (as (loop :repeat n :collect (read)))
       (bs (loop :repeat m :collect (read))))
  (declare (type (integer 1 200000) n m)
           (type (integer 1 1000000000) k))
  (multiple-value-bind (number-of-readable-books rest-time) (number-of-readable-books+rest-time as k)
    (loop :for na :below (1+ number-of-readable-books)
          :maximize (+ na (number-of-readable-books+rest-time bs rest-time)))))


(let* ((n (read))
    (m (read))
    (k (read))
    (a (make-array n))
    (b (make-array m))
    (ans_time 0)
    (ans 0))
    (dotimes (i n)
    (setf (aref a i) (read)))
    (dotimes (j m)
    (setf (aref b j) (read))
    )
    (let ((i 0)
        (j 0))
        (loop while (< ans_time k) do
        (if (< ans_time k)
            (if (and (> n i) (> m j))
                (if (<= (aref a i) (aref b j))
                    (if (>= k (+ ans_time (aref a i)))
                    (progn
                    (incf ans)
                    (incf ans_time (aref a i))
                    (incf i)
                    )
                    (return))
                    (if (>= k (+ ans_time (aref b j)))
                    (progn
                    (incf ans)
                    (incf ans_time (aref b j))
                    (incf j)
                    )
                    (return))
                )
                (if (> n i)
                    (if (>= k (+ ans_time (aref a i)))
                    (progn
                    (incf ans)
                    (incf ans_time (aref a i))
                    (incf i)
                    )
                    (return))
                    (if (>= k (+ ans_time (aref b j)))
                    (progn
                    (incf ans)
                    (incf ans_time (aref b j))
                    (incf j)
                    )
                    (return))
                )
            )
            (return)
        )
        (if (and (= n i) (= m j))
        (return))
        )

    )

    (princ ans)
)


(let* ((n (read))
       (m (read))
       (k (read))
       (a (loop repeat n collect (read)))
       (b (loop repeat m collect (read))))
  (format t "~A~%"
          (loop while (and (plusp k)
                           (or (and (first a) (>= k (first a)))
                               (and (first b) (>= k (first b)))))
                count 1
                do (cond
                     ((not (first a))
                      (let ((diff (pop b)))
                        (decf k diff)))
                     ((not (first b))
                      (let ((diff (pop a)))
                        (decf k diff)))
                     ((> (first a) (first b))
                      (let ((diff (pop b)))
                        (decf k diff)))
                     (t
                      (let ((diff (pop a)))
                        (decf k diff)))))))


(defun sum-of-divisor (n)
  (loop :for i :from n :downto 1
        :with basis = 1 and prev-div = 1 and sum = 0
        :do (progn
              (unless (= prev-div (floor n i))
                (let ((tmp (floor n i)))
                  (loop :for i :from (+ prev-div 1) :to tmp
                        :do (incf basis i))
                  ;(format t "prev-div: ~A,tmp: ~A~%" prev-div tmp)
                  (setq prev-div tmp)))
              (incf sum (* basis i)))
              ;(format t "index:~A, basis:~A, sum:~A~%" i basis sum))
        :finally (return sum)))

(format t "~A~%" (sum-of-divisor (read)))

Score : 400 points

Problem Statement

For a positive integer X, let f(X) be the number of positive divisors of X.

Given a positive integer N, find \sum_{K=1}^N K\times f(K).

Constraints

1 \leq N \leq 10^7

Input

Input is given from Standard Input in the following format:

N

Output

Print the value \sum_{K=1}^N K\times f(K).

Sample Input 1

4

Sample Output 1

23

We have f(1)=1, f(2)=2, f(3)=2, and f(4)=3, so the answer is 1\times 1 + 2\times 2 + 3\times 2 + 4\times 3 =23.

Sample Input 2

100

Sample Output 2

26879

Sample Input 3

10000000

Sample Output 3

838627288460105

Watch out for overflows.


(let ((n (read))
    (ans 0))
    (loop for i from 1 to n do
        (let ((num (floor (/ n i))))
            (incf ans (floor (/ (* num (+ num 1) i) 2)))
        )
    )
    (princ ans)
)


(let* ((n (read))
       (upper (floor (/ n 2)))
       (ans 0))
  (loop :for i :from 1 :to upper
        :do (let ((num (floor (/ n i))))
              (incf ans (/ (* num (1+ num) i) 2))))
  (format t "~A~%" ans))


(declaim (inline divisor?))
(defun divisor? (n m)
  (zerop (mod m n)))

(let ((n (read)))
  (princ (loop :for k :from 1 :to n
               :sum (* k
                       (1+ (loop :for i :from 1 :to (if (evenp k) (/ k 2) (/ (1- k) 2))
                                 :count (divisor? i k)))))))


(declaim (inline divisor?))
(defun divisor? (n m)
  (zerop (mod m n)))

(let ((n (read)))
  (princ (loop :for k :from 1 :to n
               :sum (* k
                       (loop :for i :from 1 :to k
                             :count (divisor? i k))))))


(let* ((n (read))
    (a (make-array n :initial-element 0))
    (cnt (make-array 1000005 :initial-element 0))
    (ans 0))
    (dotimes (i n)
        (incf (aref a i) (read))
    )
    (loop for x across a do
        (if (not (= (aref cnt x) 0))
            (setf (aref cnt x) 2)
            (loop for i from x by x while (< i 1000005) do
                (incf (aref cnt i))
            )
        )
    )

    (loop for x across a do
        (if (= (aref cnt x) 1)
        (incf ans))
    )
    (princ ans)
)

Score : 400 points

Problem Statement

Given is a number sequence A of length N.

Find the number of integers i \left(1 \leq i \leq N\right) with the following property:

For every integer j \left(1 \leq j \leq N\right) such that i \neq j , A_j does not divide A_i.

Constraints

All values in input are integers.

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

1 \leq A_i \leq 10^6

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the answer.

Sample Input 1

5
24 11 8 3 16

Sample Output 1

3

The integers with the property are 2, 3, and 4.

Sample Input 2

4
5 5 5 5

Sample Output 2

0

Note that there can be multiple equal numbers.

Sample Input 3

10
33 18 45 28 8 19 89 86 2 4

Sample Output 3

5


(let* ((n (read))
       (a (make-array `(,(+ n 1)) :initial-element 0))
       (m 0)
       (dp)
       (c)
       (r 0))
  ;; read
  (loop :for i :from 1 :to n
        :do (setf (aref a i) (read)))
  ;;
  (setf a (sort a #'<))
  (setf m (aref a (1- (length a))))
  (setf dp (make-array `(,(1+ m)) :initial-element t))
  (setf c (make-array `(,(1+ m)) :initial-element nil))
  ;;
  (loop :for i :from 1 :to n
        :do (let ((x (aref a i)))
              (when (and (null (aref c x)) (aref dp x))
                (setf (aref c x) t)
                (loop :for y :from (* x 2) :upto m :by x
                      :do (setf (aref dp y) nil)))))
  ;;
  (loop :for i :from 1 :to n
        :if (and (or (= i n) (/= (aref a i) (aref a (1+ i))))
                 (or (= i 1) (/= (aref a (1- i)) (aref a i)))
                 (aref dp (aref a i)))
        :do (incf r))
  (format t "~A~%" r))