(let* ((s (read-line))
       (k (read))
       (x (make-array (list (length s)) :adjustable t :fill-pointer 0)))
  ;;
  (loop :with d := #\*
        :for c :across s
        :if (char/= c d)
        :do (progn
              (vector-push-extend 1 x)
              (setf d c))
        :else
        :do (incf (aref x (1- (length x)))))
  (cond ((= (length x) 1)
         (format t "~A~%" (floor (* k (aref x 0)) 2)))
        ((char/= (aref s 0) (aref s (1- (length s))))
         (format t "~A~%" (loop :for n :across x
                                :sum (* k (floor n 2)))))
        (t
         (format t "~A~%" (+ (loop :for i :from 1 :to (- (length x) 2)
                                   :sum (* k (floor (aref x i) 2)))
                             (floor (aref x 0) 2)
                             (floor (aref x (1- (length x))) 2)
                             (* (1- k) (floor (+ (aref x 0) (aref x (1- (length x)))) 2)))))))

Score : 300 points

Problem Statement

Given is a string S. Let T be the concatenation of K copies of S.
We can repeatedly perform the following operation: choose a character in T and replace it with a different character.
Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.

Constraints

1 \leq |S| \leq 100

S consists of lowercase English letters.

1 \leq K \leq 10^9

K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the minimum number of operations required.

Sample Input 1

issii
2

Sample Output 1

4

T is issiiissii. For example, we can rewrite it into ispiqisyhi, and now any two adjacent characters are different.

Sample Input 2

qq
81

Sample Output 2

81

Sample Input 3

cooooooooonteeeeeeeeeest
999993333

Sample Output 3

8999939997


(defun cnt-string (s)
  "入力文字列に対し必要な置換操作の回数を返す"
  (let ((cnt 1)
        (okikae 0)
        (hanei nil))
    (dotimes (i (length s))
      (if (> i 0)
          (if (char= (elt s (1- i)) (elt s i))
              (incf cnt)
              (progn
                (setf hanei t)
                (incf okikae (floor cnt 2))
                (setf cnt 1)))))
    (when (> cnt 1) (incf okikae (floor cnt 2)))
    (cond
      ((= (length s) 1) 0)
      (hanei okikae)
      (t (floor cnt 2)))))

(defun same-string (s k)
  "入力文字列の両端が同じ場合"
  (let ((start 0)
        (end 0))
    (do ((l 1 (1+ l)))
        ((or (>= l (length s)) (char/= (elt s (1- l)) (elt s l))) (setf start l)))
    (setf end
          (if (>= start (length s))
              -1
              (do ((j (1- (length s)) (1- j)))
               ((or (< j 1) (char/= (elt s (1- j)) (elt s j)) ) j))))
    (if (= end -1)
        (floor (* k (length s)) 2)
        (+ (floor start 2) (floor (- (length s) end) 2) (* k (cnt-string (subseq s start end))) (* (1- k) (floor (+ start (- (length s) end)) 2))))))

(defun main ()
  (let ((s (string (read)))
        (k (read)))
    (if (= (length s) 1) (format t "~a" (floor k 2)))
    (if (char= (elt s 0) (elt s (1- (length s))))
        (format t "~a" (same-string s k))
        (format t "~a" (* (cnt-string s) k)))))

(main)

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

Submitted source-code file size, in bytes.

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

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


(defun cnt-string (s)
  "入力文字列に対し必要な置換操作の回数を返す"
  (let ((cnt 1)
        (okikae 0)
        (hanei nil))
    (dotimes (i (length s))
      (if (> i 0)
          (if (char= (elt s (1- i)) (elt s i))
              (incf cnt)
              (progn
                (setf hanei t)
                (incf okikae (floor cnt 2))
                (setf cnt 1)))))
    (cond
      ((= (length s) 1) 1)
      (hanei okikae)
      (t (floor cnt 2)))))

(defun same-string (s k)
  "入力文字列の両端が同じ場合"
  (let ((start 0)
        (end 0))
    (do ((l 1 (1+ l)))
        ((or (>= l (length s)) (char/= (elt s (1- l)) (elt s l))) (setf start l)))
    (setf end
          (if (>= start (length s))
              -1
              (do ((j (1- (length s)) (1- j)))
               ((or (< j 1) (char/= (elt s (1- j)) (elt s j)) ) j))))
    (if (= end -1)
        (floor (* k (length s)) 2)
        (+ (floor start 2) (floor (- (length s) end) 2) (* k (cnt-string (subseq s start end))) (* (1- k) (floor (+ start (- (length s) end)) 2))))))

(defun main ()
  (let ((s (string (read)))
        (k (read)))
    (if (= (length s) 1) (format t "~a" (floor k 2)))
    (if (char= (elt s 0) (elt s (1- (length s))))
        (format t "~a" (same-string s k))
        (format t "~a" (* (cnt-string s) k)))))

(main)


    (cond
      ((= (length s) 1) 1)
      (hanei okikae)
      (t (floor cnt 2)))))

(defun same-string (s k)
  "入力文字列の両端が同じ場合"
  (let ((start 0)
        (end 0))
    (do ((l 1 (1+ l)))
        ((or (>= l (length s)) (char/= (elt s (1- l)) (elt s l))) (setf start l)))
    (setf end
          (if (>= start (length s))
              -1
              (do ((j (1- (length s)) (1- j)))
               ((or (< j 1) (char/= (elt s (1- j)) (elt s j)) ) j))))
    (if (= end -1)
        (floor (* k (length s)) 2)
        (+ (floor start 2) (floor (- (length s) end) 2) (* k (cnt-string (subseq s start end))) (* (1- k) (floor (+ start (- (length s) end)) 2))))))

(defun main ()
  (let ((s (string (read)))
        (k (read)))
    (if (= (length s) 1) (format t "~a" (floor k 2)))
    (if (char= (elt s 0) (elt s (1- (length s))))
        (format t "~a" (same-string s k))
        (format t "~a" (* (cnt-string s) k)))))

(main)


(defun cnt-string (s)
  "入力文字列に対し必要な置換操作の回数を返す"
  (let ((cnt 1)
        (okikae 0)
        (hanei nil))
    (dotimes (i (length s))
      (if (> i 0)
          (if (char= (elt s (1- i)) (elt s i))
              (incf cnt)
              (progn
                (setf hanei t)
                (incf okikae (floor cnt 2))
                (setf cnt 1)))))
    (if hanei
        okikae
        (floor cnt 2))))

(defun same-string (s k)
  "入力文字列の両端が同じ場合"
  (let ((start 0)
        (end 0))
    (dotimes (i (length s))
      (if (> i 0)
          (when (char/= (elt s (1- i)) (elt s i)) (setf start i) (return))))
    (do ((l 0 (1+ l)))
        (((>= l (length s)) (setf start l))
         ((char/= (elt s (1- l)) (elt s l)) (setf start l))))
    (setf end
          (if (>= start (length s))
              -1
              (do ((j (1- (length s)) (1- j)))
               ((or (< j 1) (char/= (elt s (1- j)) (elt s j)) ) j))))
    (if (= end -1)
        (floor (length s) 2)
        (+ (floor start 2) (floor (- (length s) end) 2) (* k (cnt-string (subseq s start end))) (* (1- k) (floor (+ start (- (length s) end)) 2))))))

(defun main ()
  (let ((s (string (read)))
        (k (read)))
    (if (= (length s) 1) (format t "~a" (floor k 2)))
    (if (char= (elt s 0) (elt s (1- (length s))))
        (format t "~a" (same-string s k))
        (format t "~a" (* (cnt-string s) k)))))

(main)


(defun cnt-string (s)
  "入力文字列に対し必要な置換操作の回数を返す"
  (let ((cnt 1)
        (okikae 0)
        (hanei nil))
    (dotimes (i (length s))
      (if (> i 0)
          (if (char= (elt s (1- i)) (elt s i))
              (incf cnt)
              (progn
                (setf hanei t)
                (incf okikae (floor cnt 2))
                (setf cnt 1)))))
    (if hanei
        okikae
        (floor cnt 2))))

(defun same-string (s k)
  "入力文字列の両端が同じ場合"
  (let ((start 0)
        (end 0))
    (dotimes (i (length s))
      (if (> i 0)
          (when (char/= (elt s (1- i)) (elt s i)) (setf start i) (return))))
    (setf end
          (if (>= start (length s))
              -1
              (do ((j (1- (length s)) (1- j)))
               ((or (< j 1) (char/= (elt s (1- j)) (elt s j)) ) j))))
    (if (= end -1)
        (floor (length s) 2)
        (+ (floor start 2) (floor (- (length s) end) 2) (* k (cnt-string (subseq s start end))) (* (1- k) (floor (+ start (- (length s) end)) 2))))))

(defun main ()
  (let ((s (string (read)))
        (k (read)))
    (if (= (length s) 1) (format t "~a" (floor k 2)))
    (if (char= (elt s 0) (elt s (1- (length s))))
        (format t "~a" (same-string s k))
        (format t "~a" (* (cnt-string s) k)))))

(main)


(defun cnt-string (s)
  "入力文字列に対し必要な置換操作の回数を返す"
  (let ((cnt 1)
        (okikae 0)
        (hanei nil))
    (dotimes (i (length s))
      (if (> i 0)
          (if (char= (elt s (1- i)) (elt s i))
              (incf cnt)
              (progn
                (setf hanei t)
                (incf okikae (floor cnt 2))
                (setf cnt 1)))))
    (if hanei
        okikae
        (floor cnt 2))))

(defun same-string (s k)
  "入力文字列の両端が同じ場合"
  (let ((start 0)
        (end 0))
    (dotimes (i (length s))
      (if (> i 0)
          (when (char/= (elt s (1- i)) (elt s i)) (setf start i) (return))))
    (setf end
          (if (>= start (length s))
              -1
              (do ((j (1- (length s)) (1- j)))
               ((or (< j 1) (char/= (elt s (1- j)) (elt s j)) ) j))))
    (if (= end -1)
        (floor (length s) 2)
        (+ (floor start 2) (floor (- (length s) end) 2) (* k (cnt-string (subseq s start end))) (* (1- k) (floor (+ start (- (length s) end)) 2))))))

(defun main ()
  (let ((s (string (read)))
        (k (read)))
    (if (= (length s) 1) (format t "~a" 0))
    (if (char= (elt s 0) (elt s (1- (length s))))
        (format t "~a" (same-string s k))
        (format t "~a" (* (cnt-string s) k)))))

(main)


(defparameter *s* (read-line))
(defparameter *k* (read))

(setf *s* (concatenate 'string *s* *s*))

(defun hoge (s cur cum flag)
  (if (>= (1+ cur) (length s))
    cum
    (if (and (equal (char s cur) (char s (1+ cur))) flag)
      (hoge s (1+ cur) (1+ cum) nil)
      (hoge s (1+ cur) cum t))))

(defun piyo (s cur cum)
  (if (>= (1+ cur) (length s))
    cum
    (if (not (equal (char s cur) (char s (1+ cur)))) nil (piyo s (1+ cur) t))))

(if (piyo *s* 0 t)
  (format t "~a~%" (floor (/ (* *k* (length *s*)) 4)))
  (format t "~a~%" (min (floor (/ (* *k* (hoge *s* 0 0 t)) 2)) (floor (/ (* *k* (hoge (reverse *s*) 0 0 t)) 2)))))


(defparameter *s* (read-line))
(defparameter *k* (read))

(setf *s* (concatenate 'string *s* *s*))

(defun hoge (s cur cum flag)
  (if (>= (1+ cur) (length s))
    cum
    (if (and (equal (char s cur) (char s (1+ cur))) flag)
      (hoge s (1+ cur) (1+ cum) nil)
      (hoge s (1+ cur) cum t))))

(defun piyo (s cur cum)
  (if (>= (1+ cur) (length s))
    cum
    (if (not (equal (char s cur) (char s (1+ cur)))) nil (piyo s (1+ cur) t))))

(if (piyo *s* 0 t)
  (format t "~a~%" (floor (/ (* *k* (length *s*)) 2)))
  (format t "~a~%" (min (floor (/ (* *k* (hoge *s* 0 0 t)) 2)) (floor (/ (* *k* (hoge (reverse *s*) 0 0 t)) 2)))))


(defparameter *s* (read-line))
(defparameter *k* (read))

(setf *s* (concatenate 'string *s* *s*))

(defun hoge (s cur cum flag)
  (if (>= (1+ cur) (length s))
    cum
    (if (and (equal (char s cur) (char s (1+ cur))) flag)
      (hoge s (1+ cur) (1+ cum) nil)
      (hoge s (1+ cur) cum t))))

(defun piyo (s cur cum)
  (if (>= (1+ cur) (length s))
    cum
    (if (not (equal (char s cur) (char s (1+ cur)))) nil (piyo s (1+ cur) t))))

(if (or (equal (length *s*) 1) (piyo *s* 0 t))
  (format t "~a~%" (floor (/ (* *k* (length *s*)) 4)))
  (format t "~a~%" (floor (/ (* *k* (hoge *s* 0 0 t)) 2))))


(defparameter *s* (read-line))
(defparameter *k* (read))

(defun hoge (s cur cum flag)
  (if (>= (1+ cur) (length s))
    cum
    (if (and (equal (char s cur) (char s (1+ cur))) flag)
      (hoge s (1+ cur) (1+ cum) nil)
      (hoge s (1+ cur) cum 1))))

(defun huga (s)
  (if (and (equal (char s 0) (char s (1- (length s)))))
    (if (not (equal (char s (- (length s) 2)) (char s (1- (length s))))) 1 0)
    0))

(if (equal (length *s*) 1)
  (format t "~a~%" (floor (/ *k* 2)))
  (format t "~a~%" (+ (* *k* (hoge *s* 0 0 1)) (* (1- *k*) (huga *s*)))))


(defparameter *s* (read-line))
(defparameter *k* (read))

(defun hoge (s cur cum flag)
  (if (>= (1+ cur) (length s))
    cum
    (if (and (equal (char s cur) (char s (1+ cur))) flag)
      (hoge s (1+ cur) (1+ cum) nil)
      (hoge s (1+ cur) cum 1))))

(defun huga (s)
  (if (and (equal (char s 0) (char s (1- (length s)))))
    (if (not (equal (char s (- (length s) 2)) (char s (1- (length s))))) 1 0)
    0))
(format t "~a~%" (+ (* *k* (hoge *s* 0 0 1)) (* (1- *k*) (huga *s*))))


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

;; BEGIN_INSERTED_CONTENTS
(defun map-run-length (function seq &key (test #'eql))
  "Applies FUNCTION to each equal successive element of SEQ. FUNCTION must take
two arguments: the first one receives an element in SEQ and the second one
receives the number of the successive elements equal to the first.

Example: (map-run-length (lambda (x c) (format t \"~D ~D~%\" x c)) #(1 1 1 2 2 1 3))
1 3
2 2
1 1
3 1
"
  (declare (sequence seq)
           (function test function))
  (etypecase seq
    (vector
     (unless (zerop (length seq))
       (let ((prev (aref seq 0))
             (start 0))
         (loop for pos from 1 below (length seq)
               unless (funcall test prev (aref seq pos))
               do (funcall function prev (- pos start))
                  (setf prev (aref seq pos)
                        start pos)
               finally (funcall function prev (- pos start))))))
    (list
     (when (cdr seq)
       (labels ((recur (lst prev count)
                  (declare ((integer 0 #.most-positive-fixnum) count))
                  (cond ((null lst)
                         (funcall function prev count))
                        ((funcall test prev (car lst))
                         (recur (cdr lst) prev (+ 1 count)))
                        (t (funcall function prev count)
                           (recur (cdr lst) (car lst) 1)))))
         (recur (cdr seq) (car seq) 1))))))

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

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

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

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defun main ()
  (let* ((s (read-line))
         (n (length s))
         (k (read)))
    (when (= n (count (aref s 0) s))
      (println (floor (* n k) 2))
      (return-from main))
    (let ((seq (make-array 0 :fill-pointer 0)))
      (map-run-length (lambda (c len)
                        (vector-push-extend (cons c len) seq))
                      s)
      (if (char= (aref s 0) (aref s (- n 1)))
          (let ((sum 0)
                (delta 0))
            (loop for i from 1 below (- (length seq) 1)
                  for (c . len) = (aref seq i)
                  do (incf delta (floor len 2)))
            (incf sum (* delta k))
            (let ((prefix-len (cdr (aref seq 0)))
                  (suffix-len (cdr (aref seq (- (length seq) 1)))))
              (incf sum (floor prefix-len 2))
              (incf sum (floor suffix-len 2))
              (incf sum (* (- k 1) (floor (+ prefix-len suffix-len) 2)))
              (println sum)))
          (let ((delta 0))
            (loop for (c . len) across seq
                  do (incf delta (floor len 2)))
            (println (* delta k)))))))

#-swank (main)

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

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

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

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

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

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

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

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

;; To run: (5am:run! :sample)
#+swank
(it.bese.fiveam:test :sample
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "issii
2
"
    "4
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "qq
81
"
    "81
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "cooooooooonteeeeeeeeeest
999993333
"
    "8999939997
")))


(defvar N (read))
(defvar M (read))

(defvar A (loop repeat N collect (read)))

(defvar discount
  (loop for x in A with lst = nil do
        (loop if (zerop x) return nil do
              (push (ceiling x 2) lst)
              (setf x (floor x 2)))
        finally (return (sort lst #'>))))

(princ (- (reduce #'+ A)
          (loop for x in discount
                for i below M
                sum x)))

Score : 400 points

Problem Statement

Takahashi is going to buy N items one by one.

The price of the i-th item he buys is A_i yen (the currency of Japan).

He has M discount tickets, and he can use any number of them when buying an item.

If Y tickets are used when buying an item priced X yen, he can get the item for \frac{X}{2^Y} (rounded down to the nearest integer) yen.

What is the minimum amount of money required to buy all the items?

Constraints

All values in input are integers.

1 \leq N, M \leq 10^5

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N

Output

Print the minimum amount of money required to buy all the items.

Sample Input 1

3 3
2 13 8

Sample Output 1

9

We can buy all the items for 9 yen, as follows:

Buy the 1-st item for 2 yen without tickets.

Buy the 2-nd item for 3 yen with 2 tickets.

Buy the 3-rd item for 4 yen with 1 ticket.

Sample Input 2

4 4
1 9 3 5

Sample Output 2

6

Sample Input 3

1 100000
1000000000

Sample Output 3

0

We can buy the item priced 1000000000 yen for 0 yen with 100000 tickets.

Sample Input 4

10 1
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 4

9500000000


(let* ((n (read))
       (m (read))
       (lst (loop :repeat n :collect (read))))
  (defun list-int-max (ll)
    (let* ((l 0))
      (loop :for k :from 0 :upto (1- (length ll)) :do(if (< (elt ll l)
                                                            (elt ll k))
                                                         (setf l k)))
      l))
  (defun f ()
    (let* ((x (list-int-max lst)))
      (setf (elt lst x) (floor (elt lst x) 2))))
  (loop :repeat m :do(f))
  (princ (reduce #'+ lst)))


(let* ((n (read))
       (m (read))
       (lst (loop :repeat n :collect (read))))
  (defun list-int-max (ll)
    (let* ((l 0))
      (loop :for k :from 0 :upto (1- (length ll)) :do(if (< (elt ll l)
                                                            (elt ll k))
                                                         (setf l k)))
      l))
  (defun f ()
    (let* ((x (list-int-max lst)))
      (setf (elt lst x) (floor (elt lst x) 2))))
  (loop :repeat m :do(f))
  (princ lst)
  (princ (reduce #'+ lst)))


(let* ((n (read))
       (m (read))
       (a (sort (loop for i from 1 to n collect (read)) #'>)))
  (loop for i from 1 to m
        do (setf a
                 (merge 'list (list (floor (/ (car a) 2))) (cdr a) #'>)))
  (format t "~A~%" (apply #'+ a)))


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

;; BEGIN_INSERTED_CONTENTS
(declaim (inline read-line-into))
(defun read-line-into (buffer-string &key (in *standard-input*) (term-char #\Space))
  "Receives ASCII inputs and returns multiple values: the string and the end
position.

This function calls READ-BYTE to read characters though it calls READ-CHAR
instead on SLIME because SLIME's IO is not bivalent."
  (declare (inline read-byte)
           #-swank (sb-kernel:ansi-stream in)) ; declaring (sb-kernel:ansi-stream in) will be faster
  (loop for c of-type base-char =
           #-swank (code-char (read-byte in nil #.(char-code #\Newline)))
           #+swank (read-char in nil #\Newline)
        for idx from 0
        until (char= c #\Newline)
        do (setf (char buffer-string idx) c)
        finally (when (< idx (length buffer-string))
                  (setf (char buffer-string idx) term-char))
                (return (values buffer-string idx))))

;;;
;;; Rolling hash (32-bit)
;;; NOTE: 32-bit doesn't suffice especially when the strong collision resistance
;;; is required. Better to use 62-bit version instead.
;;;

(defconstant +modulus+ 4294967291)
(defconstant +base+ 2095716802)

(defstruct (rhash (:constructor %make-rhash (cumul powers)))
  (cumul nil :type (simple-array (unsigned-byte 32) (*)))
  (powers nil :type (simple-array (unsigned-byte 32) (*))))

(defun make-rhash (vector)
  (declare #.OPT
           (simple-base-string vector))
  (let* ((size (length vector))
         (cumul (make-array (+ 1 size) :element-type '(unsigned-byte 32)))
         (powers (make-array (+ 1 size) :element-type '(unsigned-byte 32))))
    (setf (aref powers 0) 1)
    (dotimes (i size)
      (setf (aref powers (+ i 1))
            (mod (* (aref powers i) +base+) +modulus+))
      (let ((sum (+ (mod (* (aref cumul i) +base+) +modulus+)
                    (the (unsigned-byte 32) (char-code (aref vector i))))))
        (setf (aref cumul (+ i 1))
              (if (> sum +modulus+)
                  (- sum +modulus+)
                  sum))))
    (%make-rhash cumul powers)))

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

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

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

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (s (make-string n :element-type 'base-char))
         (res 0))
    (declare (uint62 res)
             (uint31 n))
    (read-line-into s)
    (let* ((rhash (make-rhash s))
           (cumul (rhash-cumul rhash))
           (powers (rhash-powers rhash)))
      (dotimes (i1 n)
        (loop for i2 from (+ i1 1) below n
              for len = (min (- i2 i1)
                             (let ((ok 0)
                                   (ng (+ 1 (min (- n i1) (- n i2)))))
                               (declare (uint31 ok ng))
                               (loop
                                 (if (<= (- ng ok) 1)
                                     (return ok)
                                     (let ((mid (ash (+ ng ok) -1)))
                                       (declare (uint31 mid))
                                       (if (= (let ((res (+ (aref cumul (+ i1 mid))
                                                            (- +modulus+ (mod (* (aref cumul i1) (aref powers mid)) +modulus+)))))
                                                (if (> res +modulus+)
                                                    (- res +modulus+)
                                                    res))
                                              (let ((res (+ (aref cumul (+ i2 mid))
                                                            (- +modulus+ (mod (* (aref cumul i2) (aref powers mid)) +modulus+)))))
                                                (if (> res +modulus+)
                                                    (- res +modulus+)
                                                    res)))
                                           (setq ok mid)
                                           (setq ng mid)))))))
              do (setq res (max res len))))
      (println res))))

#-swank (main)

Score : 500 points

Problem Statement

Given is a string S of length N.

Find the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping.

More formally, find the maximum positive integer len such that there exist integers l_1 and l_2 ( 1 \leq l_1, l_2 \leq N - len + 1 ) that satisfy the following:

l_1 + len \leq l_2

S[l_1+i] = S[l_2+i] (i = 0, 1, ..., len - 1)

If there is no such integer len, print 0.

Constraints

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

|S| = N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping. If there is no such non-empty string, print 0 instead.

Sample Input 1

5
ababa

Sample Output 1

2

The strings satisfying the conditions are: a, b, ab, and ba. The maximum length among them is 2, which is the answer.
Note that aba occurs twice in S as contiguous substrings, but there is no pair of integers l_1 and l_2 mentioned in the statement such that l_1 + len \leq l_2.

Sample Input 2

2
xy

Sample Output 2

0

No non-empty string satisfies the conditions.

Sample Input 3

13
strangeorange

Sample Output 3

5


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

;; BEGIN_INSERTED_CONTENTS
(declaim (inline read-line-into))
(defun read-line-into (buffer-string &key (in *standard-input*) (term-char #\Space))
  "Receives ASCII inputs and returns multiple values: the string and the end
position.

This function calls READ-BYTE to read characters though it calls READ-CHAR
instead on SLIME because SLIME's IO is not bivalent."
  (declare (inline read-byte)
           #-swank (sb-kernel:ansi-stream in)) ; declaring (sb-kernel:ansi-stream in) will be faster
  (loop for c of-type base-char =
           #-swank (code-char (read-byte in nil #.(char-code #\Newline)))
           #+swank (read-char in nil #\Newline)
        for idx from 0
        until (char= c #\Newline)
        do (setf (char buffer-string idx) c)
        finally (when (< idx (length buffer-string))
                  (setf (char buffer-string idx) term-char))
                (return (values buffer-string idx))))

;;;
;;; Rolling hash (32-bit)
;;; NOTE: 32-bit doesn't suffice especially when the strong collision resistance
;;; is required. Better to use 62-bit version instead.
;;;

(defconstant +modulus+ 4294967291)
(defconstant +base+ 2095716802)

(defstruct (rhash (:constructor %make-rhash (cumul powers)))
  (cumul nil :type (simple-array (unsigned-byte 32) (*)))
  (powers nil :type (simple-array (unsigned-byte 32) (*))))

(defun make-rhash (vector)
  (declare #.OPT
           (simple-base-string vector))
  (let* ((size (length vector))
         (cumul (make-array (+ 1 size) :element-type '(unsigned-byte 32)))
         (powers (make-array (+ 1 size) :element-type '(unsigned-byte 32))))
    (setf (aref powers 0) 1)
    (dotimes (i size)
      (setf (aref powers (+ i 1))
            (mod (* (aref powers i) +base+) +modulus+))
      (let ((sum (+ (mod (* (aref cumul i) +base+) +modulus+)
                    (the (unsigned-byte 32) (char-code (aref vector i))))))
        (setf (aref cumul (+ i 1))
              (if (> sum +modulus+)
                  (- sum +modulus+)
                  sum))))
    (%make-rhash cumul powers)))

(declaim (ftype (function * (values (unsigned-byte 32) &optional)) rhash-get-lcp))
(defun rhash-get-lcp (rhash start1 start2)
  (declare #.OPT
           ((mod #.most-positive-fixnum) start1 start2))
  (let* ((cumul (rhash-cumul rhash))
         (powers (rhash-powers rhash))
         (max-length (min (- (length cumul) start1 1)
                          (- (length cumul) start2 1))))
    (labels ((bisect (ok ng)
               (declare ((integer 0 #.most-positive-fixnum) ok ng))
               (if (<= (- ng ok) 1)
                   ok
                   (let ((mid (ash (+ ng ok) -1)))
                     (if (= (let ((res (+ (aref cumul (+ start1 mid))
                                          (- +modulus+ (mod (* (aref cumul start1) (aref powers mid)) +modulus+)))))
                              (if (> res +modulus+)
                                  (- res +modulus+)
                                  res))
                            (let ((res (+ (aref cumul (+ start2 mid))
                                          (- +modulus+ (mod (* (aref cumul start2) (aref powers mid)) +modulus+)))))
                              (if (> res +modulus+)
                                  (- res +modulus+)
                                  res)))
                         (bisect mid ng)
                         (bisect ok mid))))))
      (bisect 0 (+ 1 max-length)))))

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

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

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

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (s (make-string n :element-type 'base-char))
         (res 0))
    (declare (uint62 res)
             (uint31 n))
    (read-line-into s)
    (let ((rhash (make-rhash s)))
      (dotimes (i1 n)
        (loop for i2 from (+ i1 1) below n
              for len = (min (- i2 i1) (rhash-get-lcp rhash i1 i2))
              do (setq res (max res len))))
      (println res))))

#-swank (main)


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

;; BEGIN_INSERTED_CONTENTS
;;;
;;; Z-algorithm
;;;

(declaim (inline make-z-array))
(defun make-z-array (vector &key (test #'eql))
  "Returns a vector Z of the same length as VECTOR. An element Z[i] is the
length of the longest substring starting from i which is also a prefix of
VECTOR."
  (declare (vector vector))
  (let* ((size (length vector))
         (base 0)
         (res (make-array size
                          :element-type '(unsigned-byte 16)
                          :initial-element 0)))
    (declare ((integer 0 #.most-positive-fixnum) base))
    (loop for i from 1 below size
          do (if (< (+ i (aref res (- i base)))
                    (+ base (aref res base)))
                 (setf (aref res i)
                       (aref res (- i base)))
                 (let ((delta (max 0 (- (+ base (aref res base)) i))))
                   (declare ((integer 0 #.most-positive-fixnum) delta))
                   (loop while (and (< (+ i delta) size)
                                    (funcall test
                                             (aref vector delta)
                                             (aref vector (+ i delta))))
                         do (incf delta))
                   (setf (aref res i) delta
                         base i))))
    (unless (zerop size)
      (setf (aref res 0) size))
    res))

(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 read-line-into))
(defun read-line-into (buffer-string &key (in *standard-input*) (term-char #\Space))
  "Receives ASCII inputs and returns multiple values: the string and the end
position.

This function calls READ-BYTE to read characters though it calls READ-CHAR
instead on SLIME because SLIME's IO is not bivalent."
  (declare (inline read-byte)
           #-swank (sb-kernel:ansi-stream in))
  (loop for c of-type base-char =
           #-swank (code-char (read-byte in nil #.(char-code #\Newline)))
           #+swank (read-char in nil #\Newline)
        for idx from 0
        until (char= c #\Newline)
        do (setf (char buffer-string idx) c)
        finally (when (< idx (length buffer-string))
                  (setf (char buffer-string idx) term-char))
                (return (values buffer-string idx))))

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

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

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (s (make-string n :element-type 'base-char))
         (res 0))
    (declare (uint62 res)
             (base-string s))
    (read-line-into s)
    (dotimes (start n)
      (let* ((s (subseq s start))
             (z-array (make-z-array s)))
        (declare (simple-base-string s))
        (loop for j from 1 below (length z-array)
              do (setq res (max res (min j (aref z-array j)))))))
    (println res)))

#-swank (main)


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

;; BEGIN_INSERTED_CONTENTS
(declaim (inline displace))
(defun displace (vector &optional (start 0) end)
  "displaced subseq"
  (declare (vector vector)
           ((integer 0 #.array-total-size-limit) start)
           ((or null (integer 0 #.array-total-size-limit)) end))
  (let ((end (or end (length vector))))
    (make-array (- end start)
                :element-type (array-element-type vector)
                :displaced-to vector
                :displaced-index-offset start)))

;;;
;;; Z-algorithm
;;;

(declaim (inline make-z-array))
(defun make-z-array (vector &key (test #'eql))
  "Returns a vector Z of the same length as VECTOR. An element Z[i] is the
length of the longest substring starting from i which is also a prefix of
VECTOR."
  (declare (vector vector))
  (let* ((size (length vector))
         (base 0)
         (res (make-array size
                          :element-type '(integer 0 #.most-positive-fixnum)
                          :initial-element 0)))
    (declare ((integer 0 #.most-positive-fixnum) base))
    (loop for i from 1 below size
          do (if (< (+ i (aref res (- i base)))
                    (+ base (aref res base)))
                 (setf (aref res i)
                       (aref res (- i base)))
                 (let ((delta (max 0 (- (+ base (aref res base)) i))))
                   (declare ((integer 0 #.most-positive-fixnum) delta))
                   (loop while (and (< (+ i delta) size)
                                    (funcall test
                                             (aref vector delta)
                                             (aref vector (+ i delta))))
                         do (incf delta))
                   (setf (aref res i) delta
                         base i))))
    (unless (zerop size)
      (setf (aref res 0) size))
    res))

(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 read-line-into))
(defun read-line-into (buffer-string &key (in *standard-input*) (term-char #\Space))
  "Receives ASCII inputs and returns multiple values: the string and the end
position.

This function calls READ-BYTE to read characters though it calls READ-CHAR
instead on SLIME because SLIME's IO is not bivalent."
  (declare (inline read-byte)
           #-swank (sb-kernel:ansi-stream in))
  (loop for c of-type base-char =
           #-swank (code-char (read-byte in nil #.(char-code #\Newline)))
           #+swank (read-char in nil #\Newline)
        for idx from 0
        until (char= c #\Newline)
        do (setf (char buffer-string idx) c)
        finally (when (< idx (length buffer-string))
                  (setf (char buffer-string idx) term-char))
                (return (values buffer-string idx))))

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

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

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (s (make-string n :element-type 'base-char))
         (res 0))
    (declare (uint62 res)
             (base-string s))
    (read-line-into s)
    (dotimes (start n)
      (let* ((s (displace s start))
             (z-array (make-z-array s)))
        (declare (base-string s))
        (loop for j from 1 below (length z-array)
              do (setq res (max res (min j (aref z-array j)))))))
    (println res)))

#-swank (main)


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

;; BEGIN_INSERTED_CONTENTS
(declaim (inline displace))
(defun displace (vector &optional (start 0) end)
  "displaced subseq"
  (declare (vector vector)
           ((integer 0 #.array-total-size-limit) start)
           ((or null (integer 0 #.array-total-size-limit)) end))
  (let ((end (or end (length vector))))
    (make-array (- end start)
                :element-type (array-element-type vector)
                :displaced-to vector
                :displaced-index-offset start)))

;;;
;;; Z-algorithm
;;;

(declaim (inline make-z-array))
(defun make-z-array (vector &key (test #'eql))
  "Returns a vector Z of the same length as VECTOR. An element Z[i] is the
length of the longest substring starting from i which is also a prefix of
VECTOR."
  (declare (vector vector))
  (let* ((size (length vector))
         (base 0)
         (res (make-array size
                          :element-type '(integer 0 #.most-positive-fixnum)
                          :initial-element 0)))
    (declare ((integer 0 #.most-positive-fixnum) base))
    (loop for i from 1 below size
          do (if (< (+ i (aref res (- i base)))
                    (+ base (aref res base)))
                 (setf (aref res i)
                       (aref res (- i base)))
                 (let ((delta (max 0 (- (+ base (aref res base)) i))))
                   (declare ((integer 0 #.most-positive-fixnum) delta))
                   (loop while (and (< (+ i delta) size)
                                    (funcall test
                                             (aref vector delta)
                                             (aref vector (+ i delta))))
                         do (incf delta))
                   (setf (aref res i) delta))))
    (unless (zerop size)
      (setf (aref res 0) size))
    res))

(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 read-line-into))
(defun read-line-into (buffer-string &key (in *standard-input*) (term-char #\Space))
  "Receives ASCII inputs and returns multiple values: the string and the end
position.

This function calls READ-BYTE to read characters though it calls READ-CHAR
instead on SLIME because SLIME's IO is not bivalent."
  (declare (inline read-byte)
           #-swank (sb-kernel:ansi-stream in))
  (loop for c of-type base-char =
           #-swank (code-char (read-byte in nil #.(char-code #\Newline)))
           #+swank (read-char in nil #\Newline)
        for idx from 0
        until (char= c #\Newline)
        do (setf (char buffer-string idx) c)
        finally (when (< idx (length buffer-string))
                  (setf (char buffer-string idx) term-char))
                (return (values buffer-string idx))))

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

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

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (s (make-string n :element-type 'base-char))
         (res 0))
    (declare (uint62 res)
             (base-string s))
    (read-line-into s)
    (dotimes (start n)
      (let* ((s (subseq s start))
             (z-array (make-z-array s)))
        (declare (simple-base-string s))
        (loop for j from 1 below (length z-array)
              do (setq res (max res (min j (aref z-array j)))))))
    (println res)))

#-swank (main)


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

;; BEGIN_INSERTED_CONTENTS
;;;
;;; Rolling hash (32-bit)
;;; NOTE: 32-bit doesn't suffice especially when the strong collision resistance
;;; is required. Better to use 62-bit version instead.
;;;

(defconstant +modulus+ 4294967291)
(defconstant +base+ 2095716802)

(defstruct (rhash (:constructor %make-rhash (cumul powers)))
  (cumul nil :type (simple-array (unsigned-byte 32) (*)))
  (powers nil :type (simple-array (unsigned-byte 32) (*))))

(declaim (inline make-rhash))
(defun make-rhash (vector &key (key #'char-code))
  (declare (vector vector)
           (function key))
  (let* ((size (length vector))
         (cumul (make-array (+ 1 size) :element-type '(unsigned-byte 32)))
         (powers (make-array (+ 1 size) :element-type '(unsigned-byte 32))))
    (setf (aref powers 0) 1)
    (dotimes (i size)
      (setf (aref powers (+ i 1))
            (mod (* (aref powers i) +base+) +modulus+))
      (let ((sum (+ (mod (* (aref cumul i) +base+) +modulus+)
                    (mod (the fixnum (funcall key (aref vector i))) +modulus+))))
        (setf (aref cumul (+ i 1))
              (if (> sum +modulus+)
                  (- sum +modulus+)
                  sum))))
    (%make-rhash cumul powers)))

(declaim (inline rhash-get-lcp)
         (ftype (function * (values (integer 0 #.most-positive-fixnum) &optional)) rhsah-get-lcp))
(defun rhash-get-lcp (rhash start1 start2)
  (declare #.OPT
           ((mod #.most-positive-fixnum) start1 start2))
  (let ((max-length (min (- (length (rhash-cumul rhash)) start1 1)
                         (- (length (rhash-cumul rhash)) start2 1)))
        (cumul (rhash-cumul rhash))
        (powers (rhash-powers rhash)))
    (declare (optimize (safety 0)))
    (labels ((bisect (ok ng)
               (declare ((unsigned-byte 32) ok ng))
               (if (<= (- ng ok) 1)
                   ok
                   (let ((mid (ash (+ ng ok) -1)))
                     (if (let ((res1 (+ (aref cumul (+ start1 mid))
                                        (- +modulus+ (nth-value 1 (floor (* (aref cumul start1) (aref powers mid)) +modulus+)))))
                               (res2 (+ (aref cumul (+ start2 mid))
                                        (- +modulus+ (nth-value 1 (floor (* (aref cumul start2) (aref powers mid)) +modulus+))))))
                           (= (if (> res1 +modulus+)
                                  (- res1 +modulus+)
                                  res1)
                              (if (> res2 +modulus+)
                                  (- res2 +modulus+)
                                  res2)))
                         (bisect mid ng)
                         (bisect ok mid))))))
      (bisect 0 (+ 1 max-length)))))

(declaim (inline read-line-into))
(defun read-line-into (buffer-string &key (in *standard-input*) (term-char #\Space))
  "Receives ASCII inputs and returns multiple values: the string and the end
position.

This function calls READ-BYTE to read characters though it calls READ-CHAR
instead on SLIME because SLIME's IO is not bivalent."
  (declare (inline read-byte)
           #-swank (sb-kernel:ansi-stream in))
  (loop for c of-type base-char =
           #-swank (code-char (read-byte in nil #.(char-code #\Newline)))
           #+swank (read-char in nil #\Newline)
        for idx from 0
        until (char= c #\Newline)
        do (setf (char buffer-string idx) c)
        finally (when (< idx (length buffer-string))
                  (setf (char buffer-string idx) term-char))
                (return (values buffer-string idx))))

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

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

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

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (s (make-string n :element-type 'base-char))
         (res 0))
    (declare (uint62 res))
    (read-line-into s)
    (let ((rhash (make-rhash s)))
      (dotimes (i1 n)
        (loop for i2 from (+ i1 1) below n
              for len = (min (- i2 i1) (rhash-get-lcp rhash i1 i2))
              do (setq res (max res len))))
      (println res))))

#-swank (main)


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

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

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

(defstruct (rhash (:constructor %make-rhash (mod1 base1 cumul1 powers1 mod2 base2 cumul2 powers2)))
  ;; lower 31-bit value
  (mod1 2147483647 :type (unsigned-byte 31))
  (base1 1059428526 :type (unsigned-byte 31))
  (cumul1 nil :type (simple-array (unsigned-byte 31) (*)))
  (powers1 nil :type (simple-array (unsigned-byte 31) (*)))
  ;; upper 31-bit value
  (mod2 2147483629 :type (unsigned-byte 31))
  (base2 2090066834 :type (unsigned-byte 31))
  (cumul2 nil :type (simple-array (unsigned-byte 31) (*)))
  (powers2 nil :type (simple-array (unsigned-byte 31) (*))))

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

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

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

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

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

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

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

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

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

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

;;;
;;; double-ended queue (ring buffer)
;;;

(define-condition deque-empty-error (simple-error)
  ((queue :initarg :queue :reader deque-empty-error-queue))
  (:report
   (lambda (condition stream)
     (format stream "Attempted to pop empty deque ~W" (deque-empty-error-queue condition)))))

(define-condition deque-full-error (simple-error)
  ((queue :initarg :queue :reader deque-full-error-queue)
   (item :initarg :item :reader deque-full-error-item))
  (:report
   (lambda (condition stream)
     (format stream "Attempted to push item ~W to full deque ~W"
             (deque-full-error-item condition)
             (deque-full-error-queue condition)))))

(define-condition deque-invalid-index-error (simple-error)
  ((queue :initarg :queue :reader deque-invalid-index-error-queue)
   (index :initarg :index :reader deque-invalid-index-error-index))
  (:report
   (lambda (condition stream)
     (format stream "Invalid index ~W for ~W."
             (deque-invalid-index-error-index condition)
             (deque-invalid-index-error-queue condition)))))

;; TODO: detailed documentation
(defmacro define-deque (name &key (element-type 'fixnum))
  "Defines deque for given ELEMENT-TYPE.

constructor: MAKE-<NAME>.
basic operations: <NAME>-PUSH-FRONT, <NAME>-PUSH-BACK, <NAME>-POP-FRONT,
<NAME>-POP-BACK.
accessor: <NAME>-REF.
utilities: <NAME>-EMPTY-P, <NAME>-REINITIALIZE.
"
  (let ((push-front (intern (format nil "~A-PUSH-FRONT" name)))
        (push-back (intern (format nil "~A-PUSH-BACK" name)))
        (pop-front (intern (format nil "~A-POP-FRONT" name)))
        (pop-back (intern (format nil "~A-POP-BACK" name)))
        (empty-p (intern (format nil "~A-EMPTY-P" name)))
        (constructor (intern (format nil "MAKE-~A" name)))
        (reinitializer (intern (format nil "~A-REINITIALIZE" name)))
        (reffer (intern (format nil "~A-REF" name)))
        (data-getter (intern (format nil "~A-DATA" name)))
        (front-getter (intern (format nil "~A-FRONT" name)))
        (count-getter (intern (format nil "~A-COUNT" name))))
    `(progn
       (defstruct (,name (:constructor ,constructor
                             (size &aux
                                   (data (progn
                                           (check-type size (integer 0))
                                           (make-array (+ 1 size) :element-type ',element-type)))))
                         (:copier nil)
                         (:predicate nil))
         (data nil :type (simple-array ,element-type (*)))
         (front 0 :type (integer 0 #.most-positive-fixnum))
         (count 0 :type (integer 0 #.most-positive-fixnum)))

       (declaim (inline ,push-front))
       (defun ,push-front (obj ,name)
         (symbol-macrolet ((front (,front-getter ,name))
                           (count (,count-getter ,name)))
           (let* ((data (,data-getter ,name))
                  (length (length data))
                  (next (- front 1)))
             (declare ((integer -1 #.most-positive-fixnum) next))
             (when (< next 0) (incf next length))
             (when (= (+ 1 count) length)
               (error 'deque-full-error :item obj :queue ,name))
             (setf (aref data front) obj)
             (setq front next)
             (incf count)
             ,name)))

       (declaim (inline ,pop-front))
       (defun ,pop-front (,name)
         (symbol-macrolet ((front (,front-getter ,name))
                           (count (,count-getter ,name)))
           (let* ((data (,data-getter ,name))
                  (length (length data))
                  (next (+ front 1)))
             (declare ((integer 0 #.most-positive-fixnum) next))
             (when (= next length) (decf next length))
             (when (zerop count)
               (error 'deque-empty-error :queue ,name))
             (setq front next)
             (decf count)
             (aref data next))))

       (declaim (inline ,push-back))
       (defun ,push-back (obj ,name)
         (symbol-macrolet ((front (,front-getter ,name))
                           (count (,count-getter ,name)))
           (let* ((data (,data-getter ,name))
                  (length (length data))
                  (pos (+ front count 1)))
             (declare ((integer 0 #.most-positive-fixnum) pos))
             (when (>= pos length) (decf pos length))
             (when (= (+ 1 count) length)
               (error 'deque-full-error :item obj :queue ,name))
             (setf (aref data pos) obj)
             (incf count)
             ,name)))

       (declaim (inline ,pop-back))
       (defun ,pop-back (,name)
         (symbol-macrolet ((front (,front-getter ,name))
                           (count (,count-getter ,name)))
           (let* ((data (,data-getter ,name))
                  (length (length data))
                  (pos (+ front count)))
             (declare ((integer 0 #.most-positive-fixnum) pos))
             (when (>= pos length) (decf pos length))
             (when (zerop count)
               (error 'deque-empty-error :queue ,name))
             (prog1 (aref data pos)
               (decf count)))))

       (declaim (inline ,empty-p))
       (defun ,empty-p (,name)
         (zerop (,count-getter ,name)))

       (declaim (inline ,reinitializer))
       (defun ,reinitializer (,name)
         (setf (,count-getter ,name) 0))

       (declaim (inline ,reffer))
       (defun ,reffer (,name index)
         "Returns the INDEX-th element from the top. (0-based)"
         (declare ((integer 0 #.most-positive-fixnum) index))
         (let* ((data (,data-getter ,name))
                (count (,count-getter ,name))
                (front (,front-getter ,name))
                (length (length data))
                (pos (+ front index 1)))
           (declare ((integer 0 #.most-positive-fixnum) pos))
           (when (>= pos length) (decf pos length))
           (when (>= index count)
             (error 'deque-invalid-index-error :index index :queue ,name))
           (aref data pos))))))

(define-deque deque :element-type fixnum)

(declaim (inline read-line-into))
(defun read-line-into (buffer-string &key (in *standard-input*) (term-char #\Space))
  "Receives ASCII inputs and returns multiple values: the string and the end
position.

This function calls READ-BYTE to read characters though it calls READ-CHAR
instead on SLIME because SLIME's IO is not bivalent."
  (declare (inline read-byte)) ; declaring (sb-kernel:ansi-stream in) will be faster
  (loop for c of-type base-char =
           #-swank (code-char (read-byte in nil #.(char-code #\Newline)))
           #+swank (read-char in nil #\Newline)
        for idx from 0
        until (char= c #\Newline)
        do (setf (char buffer-string idx) c)
        finally (when (< idx (length buffer-string))
                  (setf (char buffer-string idx) term-char))
                (return (values buffer-string idx))))

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

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

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

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (s (make-string n :element-type 'base-char)))
    (read-line-into s)
    (let ((rhash (make-rhash s)))
      (labels ((feasible-p (len)
                 (declare (uint62 len))
                 (let ((q (make-deque (+ len 1)))
                       (table (make-hash-table :test #'eq)))
                   (loop for start from 0 to (- n len)
                         for value = (rhash-query rhash start (+ start len))
                         do (deque-push-back value q)
                            (when (> (deque-count q) len)
                              (setf (gethash (deque-pop-front q) table) t))
                            (when (gethash value table)
                              (return-from feasible-p t))
                         finally (return nil)))))
        (sb-int:named-let bisect ((ok 0) (ng n))
          (if (<= (- ng ok) 1)
              (println ok)
              (let ((mid (ash (+ ok ng) -1)))
                (if (feasible-p mid)
                    (bisect mid ng)
                    (bisect ok mid)))))))))

#-swank (main)

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

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

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

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

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

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

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

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

;; To run: (5am:run! :sample)
#+swank
(it.bese.fiveam:test :sample
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "5
ababa
"
    "2
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "2
xy
"
    "0
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "13
strangeorange
"
    "5
")))


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

;; BEGIN_INSERTED_CONTENTS
;;;
;;; double-ended queue (ring buffer)
;;;

(define-condition deque-empty-error (simple-error)
  ((queue :initarg :queue :reader deque-empty-error-queue))
  (:report
   (lambda (condition stream)
     (format stream "Attempted to pop empty deque ~W" (deque-empty-error-queue condition)))))

(define-condition deque-full-error (simple-error)
  ((queue :initarg :queue :reader deque-full-error-queue)
   (item :initarg :item :reader deque-full-error-item))
  (:report
   (lambda (condition stream)
     (format stream "Attempted to push item ~W to full deque ~W"
             (deque-full-error-item condition)
             (deque-full-error-queue condition)))))

(define-condition deque-invalid-index-error (simple-error)
  ((queue :initarg :queue :reader deque-invalid-index-error-queue)
   (index :initarg :index :reader deque-invalid-index-error-index))
  (:report
   (lambda (condition stream)
     (format stream "Invalid index ~W for ~W."
             (deque-invalid-index-error-index condition)
             (deque-invalid-index-error-queue condition)))))

;; TODO: detailed documentation
(defmacro define-deque (name &key (element-type 'fixnum))
  "Defines deque for given ELEMENT-TYPE.

constructor: MAKE-<NAME>.
basic operations: <NAME>-PUSH-FRONT, <NAME>-PUSH-BACK, <NAME>-POP-FRONT,
<NAME>-POP-BACK.
accessor: <NAME>-REF.
utilities: <NAME>-EMPTY-P, <NAME>-REINITIALIZE.
"
  (let ((push-front (intern (format nil "~A-PUSH-FRONT" name)))
        (push-back (intern (format nil "~A-PUSH-BACK" name)))
        (pop-front (intern (format nil "~A-POP-FRONT" name)))
        (pop-back (intern (format nil "~A-POP-BACK" name)))
        (empty-p (intern (format nil "~A-EMPTY-P" name)))
        (constructor (intern (format nil "MAKE-~A" name)))
        (reinitializer (intern (format nil "~A-REINITIALIZE" name)))
        (reffer (intern (format nil "~A-REF" name)))
        (data-getter (intern (format nil "~A-DATA" name)))
        (front-getter (intern (format nil "~A-FRONT" name)))
        (count-getter (intern (format nil "~A-COUNT" name))))
    `(progn
       (defstruct (,name (:constructor ,constructor
                             (size &aux
                                   (data (progn
                                           (check-type size (integer 0))
                                           (make-array (+ 1 size) :element-type ',element-type)))))
                         (:copier nil)
                         (:predicate nil))
         (data nil :type (simple-array ,element-type (*)))
         (front 0 :type (integer 0 #.most-positive-fixnum))
         (count 0 :type (integer 0 #.most-positive-fixnum)))

       (declaim (inline ,push-front))
       (defun ,push-front (obj ,name)
         (symbol-macrolet ((front (,front-getter ,name))
                           (count (,count-getter ,name)))
           (let* ((data (,data-getter ,name))
                  (length (length data))
                  (next (- front 1)))
             (declare ((integer -1 #.most-positive-fixnum) next))
             (when (< next 0) (incf next length))
             (when (= (+ 1 count) length)
               (error 'deque-full-error :item obj :queue ,name))
             (setf (aref data front) obj)
             (setq front next)
             (incf count)
             ,name)))

       (declaim (inline ,pop-front))
       (defun ,pop-front (,name)
         (symbol-macrolet ((front (,front-getter ,name))
                           (count (,count-getter ,name)))
           (let* ((data (,data-getter ,name))
                  (length (length data))
                  (next (+ front 1)))
             (declare ((integer 0 #.most-positive-fixnum) next))
             (when (= next length) (decf next length))
             (when (zerop count)
               (error 'deque-empty-error :queue ,name))
             (setq front next)
             (decf count)
             (aref data next))))

       (declaim (inline ,push-back))
       (defun ,push-back (obj ,name)
         (symbol-macrolet ((front (,front-getter ,name))
                           (count (,count-getter ,name)))
           (let* ((data (,data-getter ,name))
                  (length (length data))
                  (pos (+ front count 1)))
             (declare ((integer 0 #.most-positive-fixnum) pos))
             (when (>= pos length) (decf pos length))
             (when (= (+ 1 count) length)
               (error 'deque-full-error :item obj :queue ,name))
             (setf (aref data pos) obj)
             (incf count)
             ,name)))

       (declaim (inline ,pop-back))
       (defun ,pop-back (,name)
         (symbol-macrolet ((front (,front-getter ,name))
                           (count (,count-getter ,name)))
           (let* ((data (,data-getter ,name))
                  (length (length data))
                  (pos (+ front count)))
             (declare ((integer 0 #.most-positive-fixnum) pos))
             (when (>= pos length) (decf pos length))
             (when (zerop count)
               (error 'deque-empty-error :queue ,name))
             (prog1 (aref data pos)
               (decf count)))))

       (declaim (inline ,empty-p))
       (defun ,empty-p (,name)
         (zerop (,count-getter ,name)))

       (declaim (inline ,reinitializer))
       (defun ,reinitializer (,name)
         (setf (,count-getter ,name) 0))

       (declaim (inline ,reffer))
       (defun ,reffer (,name index)
         "Returns the INDEX-th element from the top. (0-based)"
         (declare ((integer 0 #.most-positive-fixnum) index))
         (let* ((data (,data-getter ,name))
                (count (,count-getter ,name))
                (front (,front-getter ,name))
                (length (length data))
                (pos (+ front index 1)))
           (declare ((integer 0 #.most-positive-fixnum) pos))
           (when (>= pos length) (decf pos length))
           (when (>= index count)
             (error 'deque-invalid-index-error :index index :queue ,name))
           (aref data pos))))))

(define-deque deque :element-type fixnum)

;;;
;;; Rolling hash (32-bit)
;;; NOTE: 32-bit doesn't suffice especially when the strong collision resistance
;;; is required. Better to use 62-bit version instead.
;;;

(defstruct (rhash (:constructor %make-rhash (modulus base cumul powers)))
  (modulus 4294967291 :type (unsigned-byte 32))
  (base 2095716802 :type (unsigned-byte 32))
  (cumul nil :type (simple-array (unsigned-byte 32) (*)))
  (powers nil :type (simple-array (unsigned-byte 32) (*))))

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

MODULUS := unsigned 32-bit prime number
BASE := 1 | 2 | ... | MODULUS - 1
KEY := function returning FIXNUM"
  (declare (vector vector)
           ((unsigned-byte 32) modulus)
           ((or null (unsigned-byte 32)) base)
           (function key))
  #+sbcl (assert (sb-int:positive-primep modulus))
  (let* ((base (or base (+ 1 (random (- modulus 1)))))
         (size (length vector))
         (cumul (make-array (+ 1 size) :element-type '(unsigned-byte 32)))
         (powers (make-array (+ 1 size) :element-type '(unsigned-byte 32))))
    (assert (<= 1 base (- modulus 1)))
    (setf (aref powers 0) 1)
    (dotimes (i size)
      (setf (aref powers (+ i 1))
            (mod (* (aref powers i) base) modulus))
      (let ((sum (+ (mod (* (aref cumul i) base) modulus)
                    (mod (the fixnum (funcall key (aref vector i))) modulus))))
        (setf (aref cumul (+ i 1))
              (if (> sum modulus)
                  (- sum modulus)
                  sum))))
    (%make-rhash modulus base cumul powers)))

(declaim (inline rhash-query)
         (ftype (function * (values (unsigned-byte 32) &optional)) rhash-query))
(defun rhash-query (rhash l r)
  "Returns the hash value of the interval [L, R)."
  (declare ((integer 0 #.most-positive-fixnum) l r))
  (let ((cumul (rhash-cumul rhash))
        (powers (rhash-powers rhash))
        (modulus (rhash-modulus rhash)))
    (let ((res (+ (aref cumul r)
                  (- modulus (mod (* (aref cumul l) (aref powers (- r l))) modulus)))))
      (if (> res modulus)
          (- res modulus)
          res))))

(declaim (inline rhash-concat))
(defun rhash-concat (rhash hash1 hash2 hash2-length)
  (declare ((unsigned-byte 32) hash1 hash2)
           ((integer 0 #.most-positive-fixnum) hash2-length))
  (let* ((modulus (rhash-modulus rhash)))
    (mod (+ hash2
            (* hash1 (aref (rhash-powers rhash) hash2-length)))
         modulus)))

(declaim (ftype (function * (values (unsigned-byte 32) &optional)) rhash-vector-hash)
         (inline rhash-vector-hash))
(defun rhash-vector-hash (rhash vector &key (key #'char-code))
  "Returns the hash code of VECTOR w.r.t. the moduli and bases of RHASH."
  (declare (vector vector))
  (let* ((mod (rhash-modulus rhash))
         (base (rhash-base rhash))
         (size (length vector))
         (result 0))
    (declare ((unsigned-byte 32) result))
    (dotimes (i size)
      ;; (2^32-1) * (2^32-1) + (2^32-1) < 2^64
      (setq result (mod (+ (* base result)
                           (the (unsigned-byte 32)
                                (mod (the fixnum (funcall key (aref vector i))) mod)))
                        mod)))
    result))

(defun rhash-get-lcp (rhash1 start1 rhash2 start2)
  (declare #.OPT
           ((mod #.most-positive-fixnum) start1 start2))
  (let ((max-length (min (- (length (rhash-cumul rhash1)) start1 1)
                         (- (length (rhash-cumul rhash2)) start2 1))))
    (declare (optimize (safety 0)))
    (labels ((bisect (ok ng)
               (declare ((integer 0 #.most-positive-fixnum) ok ng))
               (if (<= (- ng ok) 1)
                   ok
                   (let ((mid (ash (+ ng ok) -1)))
                     (if (= (rhash-query rhash1 start1 (+ start1 mid))
                            (rhash-query rhash2 start2 (+ start2 mid)))
                         (bisect mid ng)
                         (bisect ok mid))))))
      (bisect 0 (+ 1 max-length)))))

(declaim (inline read-line-into))
(defun read-line-into (buffer-string &key (in *standard-input*) (term-char #\Space))
  "Receives ASCII inputs and returns multiple values: the string and the end
position.

This function calls READ-BYTE to read characters though it calls READ-CHAR
instead on SLIME because SLIME's IO is not bivalent."
  (declare (inline read-byte)) ; declaring (sb-kernel:ansi-stream in) will be faster
  (loop for c of-type base-char =
           #-swank (code-char (read-byte in nil #.(char-code #\Newline)))
           #+swank (read-char in nil #\Newline)
        for idx from 0
        until (char= c #\Newline)
        do (setf (char buffer-string idx) c)
        finally (when (< idx (length buffer-string))
                  (setf (char buffer-string idx) term-char))
                (return (values buffer-string idx))))

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

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

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

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (s (make-string n :element-type 'base-char)))
    (read-line-into s)
    (let ((rhash (make-rhash s 4294967291 :base 2095716802)))
      (labels ((feasible-p (len)
                 (declare (uint62 len))
                 (let ((q (make-deque (+ len 1)))
                       (table (make-hash-table :test #'eq)))
                   (loop for start from 0 to (- n len)
                         for value = (rhash-query rhash start (+ start len))
                         do (deque-push-back value q)
                            (when (> (deque-count q) len)
                              (setf (gethash (deque-pop-front q) table) t))
                            (when (gethash value table)
                              (return-from feasible-p t))
                         finally (return nil)))))
        (sb-int:named-let bisect ((ok 0) (ng n))
          (if (<= (- ng ok) 1)
              (println ok)
              (let ((mid (ash (+ ok ng) -1)))
                (if (feasible-p mid)
                    (bisect mid ng)
                    (bisect ok mid)))))))))

#-swank (main)

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

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

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

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

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

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

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

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

;; To run: (5am:run! :sample)
#+swank
(it.bese.fiveam:test :sample
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "5
ababa
"
    "2
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "2
xy
"
    "0
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "13
strangeorange
"
    "5
")))


(let ((a (read))
      (s (read-line)))
  (format t "~A~%" (if (<= 3200 a) s "red")))

Score : 100 points

Problem Statement

You will be given an integer a and a string s consisting of lowercase English letters as input.

Write a program that prints s if a is not less than 3200 and prints red if a is less than 3200.

Constraints

2800 \leq a < 5000

s is a string of length between 1 and 10 (inclusive).

Each character of s is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

a
s

Output

If a is not less than 3200, print s; if a is less than 3200, print red.

Sample Input 1

3200
pink

Sample Output 1

pink

a = 3200 is not less than 3200, so we print s = pink.

Sample Input 2

3199
pink

Sample Output 2

red

a = 3199 is less than 3200, so we print red.

Sample Input 3

4049
red

Sample Output 3

red

a = 4049 is not less than 3200, so we print s = red.


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

(setq *n* (read))
(setq *ai* (mapcar #'parse-integer (split " " (read-line))))

(print (float (/ 1 (apply #'+ (mapcar #'(lambda (a) (/ 1 a)) *ai*)))))


(defun ans (a s)
  (if (< a 3200)
      "red"
      s))

(format t "~a~%" (ans (read) (read-line)))


(princ (if (< (read) 3200) "red" (read-line)))


(princ (if (< (read) 3200) "red" (read)))


(princ (if (< (read) 3200)) "red" (read))


(let ((a (read))
      (s (read-line)))
  (if (>= a 3200)
      (format t "~A~%" s)
      (format t "red~%")))


(let ((a (read))
      (s (read-line)))
  (if (>= a 3200)
      (print s)
      (print "red")))


(let ((a (read))
      (s (read-line)))
  (princ (if (<= 3200 a) s "red")))


(princ (if (<= 3200 (read)) "red" (read-line)))