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

;; BEGIN_INSERTED_CONTENTS
;;;
;;; Arithmetic operations with static modulus
;;;

;; FIXME: Currently MOD* and MOD+ doesn't apply MOD when the number of
;; parameters is one.
(defmacro define-mod-operations (divisor)
  `(progn
     (defun mod* (&rest args)
       (reduce (lambda (x y) (mod (* x y) ,divisor)) args))

     (sb-c:define-source-transform mod* (&rest args)
       (if (null args)
           1
           (reduce (lambda (x y) `(mod (* ,x ,y) ,',divisor)) args)))

     (defun mod+ (&rest args)
       (reduce (lambda (x y) (mod (+ x y) ,divisor)) args))

     (sb-c:define-source-transform mod+ (&rest args)
       (if (null args)
           0
           (reduce (lambda (x y) `(mod (+ ,x ,y) ,',divisor)) args)))

     (define-modify-macro incfmod (delta)
       (lambda (x y) (mod (+ x y) ,divisor)))

     (define-modify-macro decfmod (delta)
       (lambda (x y) (mod (- x y) ,divisor)))

     (define-modify-macro mulfmod (multiplier)
       (lambda (x y) (mod (* x y) ,divisor)))))

;;;
;;; Memoization macro
;;;

;;
;; Basic usage:
;;
;; (with-cache (:hash-table :test #'equal :key #'cons)
;;   (defun add (a b)
;;     (+ a b)))
;; This function caches the returned values for already passed combinations of
;; arguments. In this case ADD stores the key (CONS A B) and the returned value
;; to a hash-table when (ADD A B) is evaluated for the first time. ADD returns
;; the stored value when it is called with the same arguments (w.r.t. EQUAL)
;; again.
;;
;; The storage for cache can be hash-table or array. Let's see an example for
;; array:
;; (with-cache (:array (10 20 30) :initial-element -1 :element-type 'fixnum)
;;   (defun foo (a b c) ... ))
;; This form stores the value of FOO in an array created by (make-array (list 10
;; 20 30) :initial-element -1 :element-type 'fixnum). Note that INITIAL-ELEMENT
;; must always be given here as it is used as the flag expressing `not yet
;; stored'. (Therefore INITIAL-ELEMENT should be a value FOO never takes.)
;;
;; If you want to ignore some arguments, you can put `*' in dimensions:
;; (with-cache (:array (10 10 * 10) :initial-element -1)
;;   (defun foo (a b c d) ...)) ; then C is ignored when querying or storing cache
;;
;; Available definition forms in WITH-CACHE are DEFUN, LABELS, FLET, and
;; SB-INT:NAMED-LET.
;;
;; You can trace the memoized function by :TRACE option:
;; (with-cache (:array (10 10) :initial-element -1 :trace t)
;;   (defun foo (x y) ...))
;; Then FOO is traced as with CL:TRACE.
;;

;; TODO & NOTE: Currently a memoized function is not enclosed with a block of
;; the function name.

;; FIXME: *RECURSION-DEPTH* should be included within the macro.
(declaim (type (integer 0 #.most-positive-fixnum) *recursion-depth*))
(defparameter *recursion-depth* 0)

(eval-when (:compile-toplevel :load-toplevel :execute)
  (defun %enclose-with-trace (fname args form)
    (let ((value (gensym)))
      `(progn
         (format t "~&~A~A: (~A ~{~A~^ ~}) =>"
                 (make-string *recursion-depth*
                              :element-type 'base-char
                              :initial-element #\ )
                 *recursion-depth*
                 ',fname
                 (list ,@args))
         (let ((,value (let ((*recursion-depth* (1+ *recursion-depth*)))
                         ,form)))
           (format t "~&~A~A: (~A ~{~A~^ ~}) => ~A"
                   (make-string *recursion-depth*
                                :element-type 'base-char
                                :initial-element #\ )
                   *recursion-depth*
                   ',fname
                   (list ,@args)
                   ,value)
           ,value))))

  (defun %extract-declarations (body)
    (remove-if-not (lambda (form) (and (consp form) (eql 'declare (car form))))
                   body))

  (defun %parse-cache-form (cache-specifier)
    (let ((cache-type (car cache-specifier))
          (cache-attribs (cdr cache-specifier)))
      (assert (member cache-type '(:hash-table :array)))
      (let* ((dims-with-* (when (eql cache-type :array) (first cache-attribs)))
             (dims (remove '* dims-with-*))
             (rank (length dims))
             (rest-attribs (ecase cache-type
                             (:hash-table cache-attribs)
                             (:array (cdr cache-attribs))))
             (key (prog1 (getf rest-attribs :key) (remf rest-attribs :key)))
             (trace-p (prog1 (getf rest-attribs :trace) (remf rest-attribs :trace)))
             (cache-form (case cache-type
                           (:hash-table `(make-hash-table ,@rest-attribs))
                           (:array `(make-array (list ,@dims) ,@rest-attribs))))
             (initial-element (when (eql cache-type :array)
                                (assert (member :initial-element rest-attribs))
                                (getf rest-attribs :initial-element))))
        (let ((cache (gensym "CACHE"))
              (value (gensym))
	      (present-p (gensym))
              (name-alias (gensym))
	      (args-lst (gensym))
              (indices (loop repeat rank collect (gensym))))
          (labels
              ((make-cache-querier (cache-type name args)
                 (let ((res (case cache-type
                              (:hash-table
                               `(let ((,args-lst (funcall ,(or key '#'list) ,@args)))
                                  (multiple-value-bind (,value ,present-p)
                                      (gethash ,args-lst ,cache)
                                    (if ,present-p
                                        ,value
                                        (setf (gethash ,args-lst ,cache)
                                              (,name-alias ,@args))))))
                              (:array
                               (assert (= (length args) (length dims-with-*)))
                               (let ((memoized-args (loop for dimension in dims-with-*
                                                          for arg in args
                                                          unless (eql dimension '*)
                                                          collect arg)))
                                 (if key
                                     `(multiple-value-bind ,indices
                                          (funcall ,key ,@memoized-args)
                                        (let ((,value (aref ,cache ,@indices)))
                                          (if (eql ,initial-element ,value)
                                              (setf (aref ,cache ,@indices)
                                                    (,name-alias ,@args))
                                              ,value)))
                                     `(let ((,value (aref ,cache ,@memoized-args)))
                                        (if (eql ,initial-element ,value)
                                            (setf (aref ,cache ,@memoized-args)
                                                  (,name-alias ,@args))
                                            ,value))))))))
                   (if trace-p
                       (%enclose-with-trace name args res)
                       res)))
               (make-reset-form (cache-type)
                 (case cache-type
                   (:hash-table `(setf ,cache (make-hash-table ,@rest-attribs)))
                   (:array `(prog1 nil
                              ;; TODO: portable fill
                              (fill (array-storage-vector ,cache) ,initial-element)))))
               (make-reset-name (name)
                 (intern (format nil "RESET-~A" (symbol-name name)))))
            (values cache cache-form cache-type name-alias
                    #'make-reset-name
                    #'make-reset-form
                    #'make-cache-querier)))))))

(defmacro with-cache ((cache-type &rest cache-attribs) def-form)
  "CACHE-TYPE := :HASH-TABLE | :ARRAY.
DEF-FORM := definition form with DEFUN, LABELS, FLET, or SB-INT:NAMED-LET."
  (multiple-value-bind (cache-symbol cache-form cache-type name-alias
                        make-reset-name make-reset-form
                        make-cache-querier)
      (%parse-cache-form (cons cache-type cache-attribs))
    (ecase (car def-form)
      ((defun)
       (destructuring-bind (_ name args &body body) def-form
         (declare (ignore _))
         `(let ((,cache-symbol ,cache-form))
            (defun ,(funcall make-reset-name name) ()
              ,(funcall make-reset-form cache-type))
            (defun ,name ,args
              ,@(%extract-declarations body)
              (labels ((,name-alias ,args ,@body))
                (declare (inline ,name-alias))
                ,(funcall make-cache-querier cache-type name args))))))
      ((labels flet)
       (destructuring-bind (_ definitions &body labels-body) def-form
         (declare (ignore _))
         (destructuring-bind (name args &body body) (car definitions)
           `(let ((,cache-symbol ,cache-form))
              (,(car def-form)
               ((,(funcall make-reset-name name) ()
                 ,(funcall make-reset-form cache-type))
                (,name ,args
                       ,@(%extract-declarations body)
                       (labels ((,name-alias ,args ,@body))
                         (declare (inline ,name-alias))
                         ,(funcall make-cache-querier cache-type name args)))
                ,@(cdr definitions))
               (declare (ignorable #',(funcall make-reset-name name)))
               ,@labels-body)))))
      ((nlet #+sbcl sb-int:named-let)
       (destructuring-bind (_ name bindings &body body) def-form
         (declare (ignore _))
         `(let ((,cache-symbol ,cache-form))
            (,(car def-form) ,name ,bindings
             ,@(%extract-declarations body)
             ,(let ((args (mapcar (lambda (x) (if (atom x) x (car x))) bindings)))
                `(labels ((,name-alias ,args ,@body))
                   (declare (inline ,name-alias))
                   ,(funcall make-cache-querier cache-type name args))))))))))

(defmacro with-caches (cache-specs def-form)
  "DEF-FORM := definition form by LABELS or FLET.

 (with-caches (cache-spec1 cache-spec2)
   (labels ((f (x) ...) (g (y) ...))))
is equivalent to the line up of
 (with-cache cache-spec1 (labels ((f (x) ...))))
and
 (with-cache cache-spec2 (labels ((g (y) ...))))

This macro will be useful to do mutual recursion between memoized local
functions."
  (assert (member (car def-form) '(labels flet)))
  (let (cache-symbol-list cache-form-list cache-type-list name-alias-list make-reset-name-list make-reset-form-list make-cache-querier-list)
    (dolist (cache-spec (reverse cache-specs))
      (multiple-value-bind (cache-symbol cache-form cache-type name-alias
                            make-reset-name make-reset-form make-cache-querier)
          (%parse-cache-form cache-spec)
        (push cache-symbol cache-symbol-list)
        (push cache-form cache-form-list)
        (push cache-type cache-type-list)
        (push name-alias name-alias-list)
        (push make-reset-name make-reset-name-list)
        (push make-reset-form make-reset-form-list)
        (push make-cache-querier make-cache-querier-list)))
    (labels ((def-name (def) (first def))
             (def-args (def) (second def))
             (def-body (def) (cddr def)))
      (destructuring-bind (_ definitions &body labels-body) def-form
        (declare (ignore _))
        `(let ,(loop for cache-symbol in cache-symbol-list
                     for cache-form in cache-form-list
                     collect `(,cache-symbol ,cache-form))
           (,(car def-form)
            (,@(loop for def in definitions
                     for cache-type in cache-type-list
                     for make-reset-name in make-reset-name-list
                     for make-reset-form in make-reset-form-list
                     collect `(,(funcall make-reset-name (def-name def)) ()
                               ,(funcall make-reset-form cache-type)))
             ,@(loop for def in definitions
                     for cache-type in cache-type-list
                     for name-alias in name-alias-list
                     for make-cache-querier in make-cache-querier-list
                     collect `(,(def-name def) ,(def-args def)
                               ,@(%extract-declarations (def-body def))
                               (labels ((,name-alias ,(def-args def) ,@(def-body def)))
                                 (declare (inline ,name-alias))
                                 ,(funcall make-cache-querier cache-type (def-name def) (def-args def))))))
            (declare (ignorable ,@(loop for def in definitions
                                        for make-reset-name in make-reset-name-list
                                        collect `#',(funcall make-reset-name
                                                             (def-name def)))))
            ,@labels-body))))))

;;;
;;; Sort multiple vectors
;;;

;; Note: Not randomized; the worst case time complexity is O(n^2).

(declaim (inline %median3))
(defun %median3 (x y z order)
  (if (funcall order x y)
      (if (funcall order y z)
          y
          (if (funcall order z x)
              x
              z))
      (if (funcall order z y)
          y
          (if (funcall order x z)
              x
              z))))

(defun parallel-sort! (vector order &rest vectors)
  "Destructively sorts VECTOR w.r.t. ORDER and applies the same permutation to
all the vectors in VECTORS."
  (declare (vector vector))
  (labels
      ((recur (left right)
         (when (< left right)
           (let* ((l left)
                  (r right)
                  (pivot (%median3 (aref vector l)
                                   (aref vector (ash (+ l r) -1))
                                   (aref vector r)
                                   order)))
             (declare ((integer 0 #.most-positive-fixnum) l r))
             (loop (loop while (funcall order (aref vector l) pivot)
                         do (incf l 1))
                   (loop while (funcall order pivot (aref vector r))
                         do (decf r 1))
                   (when (>= l r)
                     (return))
                   (rotatef (aref vector l) (aref vector r))
                   (dolist (v vectors)
                     (rotatef (aref v l) (aref v r)))
                   (incf l 1)
                   (decf r 1))
             (recur left (- l 1))
             (recur (+ r 1) right)))))
    (recur 0 (- (length vector) 1))
    vector))

#+sbcl
(sb-c:define-source-transform parallel-sort! (vector order &rest vectors)
  (let ((vec (gensym))
        (vecs (loop for _ in vectors collect (gensym))))
    `(let ((,vec ,vector)
           ,@(loop for v in vectors
                   for sym in vecs
                   collect `(,sym ,v)))
       (labels
           ((recur (left right)
              (when (< left right)
                (let* ((l left)
                       (r right)
                       (pivot (%median3 (aref ,vec l)
                                        (aref ,vec (ash (+ l r) -1))
                                        (aref ,vec r)
                                        ,order)))
                  (declare ((integer 0 #.most-positive-fixnum) l r))
                  (loop (loop while (funcall ,order (aref ,vec l) pivot)
                              do (incf l 1))
                        (loop while (funcall ,order pivot (aref ,vec r))
                              do (decf r 1))
                        (when (>= l r)
                          (return))
                        (rotatef (aref ,vec l) (aref ,vec r))
                        ,@(loop for sym in vecs
                                collect `(rotatef (aref ,sym l) (aref ,sym r)))
                        (incf l 1)
                        (decf r 1))
                  (recur left (- l 1))
                  (recur (+ r 1) right)))))
         (recur 0 (- (length ,vec) 1))
         ,vec))))

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

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

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

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

(defconstant +mod+ 998244353)

;;;
;;; Body
;;;

(defconstant +inf+ most-positive-fixnum)
(define-mod-operations +mod+)

(defun main ()
  (let* ((n (read))
         (xs (make-array (+ n 1) :element-type 'fixnum :initial-element +inf+))
         (ds (make-array n :element-type 'uint32)))
    (declare (uint31 n))
    (dotimes (i n)
      (let ((x (read-fixnum))
            (d (read-fixnum)))
        (setf (aref xs i) x
              (aref ds i) d)))
    (parallel-sort! xs #'< ds)
    (dbg xs ds)
    (with-caches ((:array (200001) :element-type 'uint32 :initial-element #xffffffff)
                  (:array (200001) :element-type 'uint32 :initial-element #xffffffff))
      (labels ((get-farthest (v)
                 (let ((x (aref xs v))
                       (d (aref ds v))
                       (next-x (aref xs (+ v 1))))
                   (loop (when (>= next-x (+ x d))
                           (return v))
                         (setq v (get-farthest (+ v 1))
                               next-x (aref xs (+ v 1))))))
               (dp (v)
                 (let ((res 1)
                       (sup (get-farthest v))
                       (next (+ v 1)))
                   (declare (uint31 res))
                   (loop (when (> next sup)
                           (return))
                         (mulfmod res (dp next))
                         (setq next (+ 1 (get-farthest next))))
                   (mod+ res 1))))
        (let ((res 1)
              (pos 0))
          (declare (uint31 res))
          (loop (when (>= pos n)
                  (return))
                (mulfmod res (dp pos))
                (setq pos (+ 1 (get-farthest pos))))
          (println res))))))

#-swank (main)

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

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

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

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

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

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

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

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

;; To run: (5am:run! :sample)
#+swank
(it.bese.fiveam:test :sample
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "2
1 5
3 3
"
    "3
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "3
6 5
-1 10
3 3
"
    "5
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "4
7 10
-10 3
4 3
-4 3
"
    "16
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "20
-8 1
26 4
0 5
9 1
19 4
22 20
28 27
11 8
-3 20
-25 17
10 4
-18 27
24 28
-11 19
2 27
-2 18
-1 12
-24 29
31 29
29 7
"
    "110
")))

Score : 600 points

Problem Statement

There are N robots numbered 1 to N placed on a number line. Robot i is placed at coordinate X_i. When activated, it will travel the distance of D_i in the positive direction, and then it will be removed from the number line. All the robots move at the same speed, and their sizes are ignorable.

Takahashi, who is a mischievous boy, can do the following operation any number of times (possibly zero) as long as there is a robot remaining on the number line.

Choose a robot and activate it. This operation cannot be done when there is a robot moving.

While Robot i is moving, if it touches another robot j that is remaining in the range [X_i, X_i + D_i) on the number line, Robot j also gets activated and starts moving. This process is repeated recursively.

How many possible sets of robots remaining on the number line are there after Takahashi does the operation some number of times? Compute this count modulo 998244353, since it can be enormous.

Constraints

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

-10^9 \leq X_i \leq 10^9

1 \leq D_i \leq 10^9

X_i \neq X_j (i \neq j)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 D_1
:
X_N D_N

Output

Print the number of possible sets of robots remaining on the number line, modulo 998244353.

Sample Input 1

2
1 5
3 3

Sample Output 1

3

There are three possible sets of robots remaining on the number line: \{1, 2\}, \{1\}, and \{\}.

These can be achieved as follows:

If Takahashi activates nothing, the robots \{1, 2\} will remain.

If Takahashi activates Robot 1, it will activate Robot 2 while moving, after which there will be no robots on the number line. This state can also be reached by activating Robot 2 and then Robot 1.

If Takahashi activates Robot 2 and finishes doing the operation, the robot \{1\} will remain.

Sample Input 2

3
6 5
-1 10
3 3

Sample Output 2

5

There are five possible sets of robots remaining on the number line: \{1, 2, 3\}, \{1, 2\}, \{2\}, \{2, 3\}, and \{\}.

Sample Input 3

4
7 10
-10 3
4 3
-4 3

Sample Output 3

16

None of the robots influences others.

Sample Input 4

20
-8 1
26 4
0 5
9 1
19 4
22 20
28 27
11 8
-3 20
-25 17
10 4
-18 27
24 28
-11 19
2 27
-2 18
-1 12
-24 29
31 29
29 7

Sample Output 4

110

Remember to print the count modulo 998244353.


(defun solve (N)
  (ceiling N 2))

(let ((N (read-line)))
  (format t "~A~%" (solve (N))))

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

Score : 100 points

Problem Statement

Takahashi wants to print a document with N pages double-sided, where two pages of data can be printed on one sheet of paper.

At least how many sheets of paper does he need?

Constraints

N is an integer.

1 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

5

Sample Output 1

3

By printing the 1-st, 2-nd pages on the 1-st sheet, 3-rd and 4-th pages on the 2-nd sheet, and 5-th page on the 3-rd sheet, we can print all the data on 3 sheets of paper.

Sample Input 2

2

Sample Output 2

1

Sample Input 3

100

Sample Output 3

50

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 duplex-printing ()
  (let ((n (read)))
    (multiple-value-bind (ans x) (floor n 2)
      (format t "~D~%" (+ ans x)))))

(duplex-printing)


(defun d (n k acc)
  (if (= n 0)
      (1- acc)
      (d (- n (mod n (expt k acc))) k (1+ acc))))

(let ((n (read))
      (k (read)))
  (format t "~A~%" (d n k 1)))

Score : 200 points

Problem Statement

Given is an integer N. Find the number of digits that N has in base K.

Notes

For information on base-K representation, see Positional notation - Wikipedia.

Constraints

All values in input are integers.

1 \leq N \leq 10^9

2 \leq K \leq 10

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of digits that N has in base K.

Sample Input 1

11 2

Sample Output 1

4

In binary, 11 is represented as 1011.

Sample Input 2

1010101 10

Sample Output 2

7

Sample Input 3

314159265 3

Sample Output 3

18


(defun solve (n k)
  (let ((*print-base* k))
    (length (prin1-to-string n))))

#-swank
(let* ((n (read))
       (k (read)))
  (format t "~A~%" (solve n k)))


(defun solve (n k)
  (do ((i n (floor i k))
       (j 0 (1+ j)))
      ((= i 0) j)))

#-swank
(let* ((n (read))
       (k (read)))
  (format t "~A~%" (solve n k)))


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

(defparameter *hash-memo* (make-hash-table :test #'equal))

(defmacro dp (&body func)
  `(let* ((f (quote func))
          (num (gethash f *hash-memo*)))
     (if num
       num
       (setf (gethash f *hash-memo*) ,func))))

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

(defun func (a b)
  (if (zerop a)
    0
    (+ 1 (func (floor a b) b))))

(princ (func (read) (read)))


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

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

(defparameter *factorial-dp* (make-array 400001 :initial-element nil))

(defparameter *mod* (+ (expt 10 9) 7))

(defun factorial (x)
  (if (<= x 1) 1
    (if (aref *factorial-dp* x)
      (aref *factorial-dp* x)
      (setf (aref *factorial-dp* x) (mod* x (factorial (1- x)))))))

(defun mod+ (&rest body)
  (reduce (lambda (a b) (mod (+ a b) *mod*)) body))
(defun mod- (&rest body)
  (reduce (lambda (a b) (mod (- a b) *mod*)) body))
(defun mod* (&rest body)
  (reduce (lambda (a b) (mod (* a b) *mod*)) body))
(defun modpow (x y)
  (if (zerop y) 1
    (mod* (if (oddp y) x 1)
          (modpow (mod* x x) (ash y -1)))))
(defun modinv (x)
  (modpow x (- *mod* 2)))
(defun mod/ (&rest body)
  (reduce (lambda (a b) (mod* a (modinv b))) body))

(defun combination (a b)
  (mod/ (factorial a) (factorial b) (factorial (- a b))))

(let* ((n (read))
       (k (read))
       (ans (combination (1- (* n 2)) n)))
  (loop for zeros from (1+ k) below n
        do
        (setf ans (mod- ans
                        (mod*
                          (combination n zeros)
                          (combination (1- n) (- n zeros 1))))))
  (princ ans))

Score : 500 points

Problem Statement

There is a building with n rooms, numbered 1 to n.

We can move from any room to any other room in the building.

Let us call the following event a move: a person in some room i goes to another room j~ (i \neq j).

Initially, there was one person in each room in the building.

After that, we know that there were exactly k moves happened up to now.

We are interested in the number of people in each of the n rooms now. How many combinations of numbers of people in the n rooms are possible?

Find the count modulo (10^9 + 7).

Constraints

All values in input are integers.

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

2 \leq k \leq 10^9

Input

Input is given from Standard Input in the following format:

n k

Output

Print the number of possible combinations of numbers of people in the n rooms now, modulo (10^9 + 7).

Sample Input 1

3 2

Sample Output 1

10

Let c_1, c_2, and c_3 be the number of people in Room 1, 2, and 3 now, respectively. There are 10 possible combination of (c_1, c_2, c_3):

(0, 0, 3)

(0, 1, 2)

(0, 2, 1)

(0, 3, 0)

(1, 0, 2)

(1, 1, 1)

(1, 2, 0)

(2, 0, 1)

(2, 1, 0)

(3, 0, 0)

For example, (c_1, c_2, c_3) will be (0, 1, 2) if the person in Room 1 goes to Room 2 and then one of the persons in Room 2 goes to Room 3.

Sample Input 2

200000 1000000000

Sample Output 2

607923868

Print the count modulo (10^9 + 7).

Sample Input 3

15 6

Sample Output 3

22583772


(defun main ()
  (let* ((a (read)) (b (read)) (c (read)) (cnt 0))
    (when (= a b) (incf cnt))
    (when (= a c) (incf cnt))
    (when (= b c) (incf cnt))
    (write-line (if (= cnt 2) "Yes" "No"))))

Score: 100 points

Problem Statement

A triple of numbers is said to be poor when two of those numbers are equal but the other number is different from those two numbers.

You will be given three integers A, B, and C. If this triple is poor, print Yes; otherwise, print No.

Constraints

A, B, and C are all integers between 1 and 9 (inclusive).

Input

Input is given from Standard Input in the following format:

A B C

Output

If the given triple is poor, print Yes; otherwise, print No.

Sample Input 1

5 7 5

Sample Output 1

Yes

A and C are equal, but B is different from those two numbers, so this triple is poor.

Sample Input 2

4 4 4

Sample Output 2

No

A, B, and C are all equal, so this triple is not poor.

Sample Input 3

4 9 6

Sample Output 3

No

Sample Input 4

3 3 4

Sample Output 4

Yes


(let* ((a (read))
       (b (read))
       (c (read))
       (result (length (remove-duplicats (list a b c)))))
  (princ (if (or (= result 1) (= result 3))
             "No"
             "Yes")))


(let ((result (length (remove-duplicates (list (read) (read) (read)) :test #'=))))
  (princ (if (or (= result 1) (= result 2))
             "Yes"
             "No")))


(defun splitat (c line)
  (labels ((rec (line acc)
				(let ((pos (position-if (lambda(cc) (char= c cc)) line)))
				  (if pos
					  (rec (subseq line (1+ pos)) (cons (subseq line 0 pos) acc))
					(nreverse (cons line acc))))))
	(rec line nil)))

(defun f(a b c)
  (if (or (and (= a b)
               (not (= b c)))
          (and (= b c)
               (not (= a b)))
          (and (= a c)
               (not (= b c)))
          )
    "Yes"
    "No"))
(let* ((line (mapcar #'parse-integer (splitat #\space (read-line nil nil)))))
  (format t "~A~%" (f (car line) (cadr line) (caddr line))))


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

(defun is-approved-paper (n)
  (or (oddp n)
      (zerop (mod n 3))
      (zerop (mod n 5))))

(defun my-and (l)
  (and (car l)
       (if (cdr l)
           (my-and (cdr l))
           T)))

(defun is-approved (l)
  (if (my-and l)
      "APPROVED"
      "DENIED"))

(let* ((n (read))
      (papers-str (read-line))
      (papers (mapcar #'parse-integer (split " " papers-str))))
  (princ (is-approved (mapcar #'is-approved-paper papers))))

Score: 200 points

Problem Statement

You are an immigration officer in the Kingdom of AtCoder. The document carried by an immigrant has some number of integers written on it, and you need to check whether they meet certain criteria.

According to the regulation, the immigrant should be allowed entry to the kingdom if and only if the following condition is satisfied:

All even numbers written on the document are divisible by 3 or 5.

If the immigrant should be allowed entry according to the regulation, output APPROVED; otherwise, print DENIED.

Notes

The condition in the statement can be rephrased as "If x is an even number written on the document, x is divisible by 3 or 5".
Here "if" and "or" are logical terms.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq A_i \leq 1000

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

If the immigrant should be allowed entry according to the regulation, print APPROVED; otherwise, print DENIED.

Sample Input 1

5
6 7 9 10 31

Sample Output 1

APPROVED

The even numbers written on the document are 6 and 10.

All of them are divisible by 3 or 5, so the immigrant should be allowed entry.

Sample Input 2

3
28 27 24

Sample Output 2

DENIED

28 violates the condition, so the immigrant should not be allowed entry.


(defvar alist nil)

(let ((ht (make-hash-table)))
     (mapc (lambda (x)
                   (if #1=(gethash x ht)
                       (incf #1#)
                       (setf #1# 1) ))
           (loop :repeat (read) :collect (read)) )
     (maphash (lambda (k v) (push (cons k v) alist)) ht)
     ;(princ alist)
     )

(defvar alist-max
  (reduce (lambda (x y) (max x (cdr y)))
          alist :initial-value 0) )

(setq alist (remove-if-not
             (lambda (x) (= (cdr x) alist-max))
             alist))

(setq alist
      (sort (mapcar
             (lambda (x) (string-downcase (car x)))
             alist)
            #'string<))

(format t "~{~A~^~%~}" alist)

Score: 300 points

Problem Statement

We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.

Print all strings that are written on the most number of votes, in lexicographical order.

Constraints

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

S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.

The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

Print all strings in question in lexicographical order.

Sample Input 1

7
beat
vet
beet
bed
vet
bet
beet

Sample Output 1

beet
vet

beet and vet are written on two sheets each, while beat, bed, and bet are written on one vote each. Thus, we should print the strings beet and vet.

Sample Input 2

8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo

Sample Output 2

buffalo

Sample Input 3

7
bass
bass
kick
kick
bass
kick
kick

Sample Output 3

kick

Sample Input 4

4
ushi
tapu
nichia
kun

Sample Output 4

kun
nichia
tapu
ushi


(defun extr (in &optional (out nil) (m (cdar in)))
  (if (/= m (cdar in)) out
    (extr (cdr in) (cons (caar in) out) m)))

(defun read-record(N ht)
  (dotimes (x N)
    (let ((str (read)))
      (let ((h (gethash str ht)))
	(setf (gethash str ht) (if h (1+ h) 1))))))

(defun ht2alist (table)
  (let ((alist nil))
    (maphash (lambda (k v) (push (cons k v) alist))
	     table)
    alist))

(let ((ht (make-hash-table :test #'equal)) alist)
  (read-record (read) ht)
  (setq alist (extr (sort (ht2alist ht) #'> :key #'cdr)))
  (format t "~{~A~^~%~}" (mapcar 'string-downcase (sort alist #'string<))))


(defun cul-max-cnt (names)
  (let ((max-cnt 0)
        (cur-cnt 0)
        (last-name ""))
    (mapc (lambda (name)
            (if (string= name last-name)
                (incf cur-cnt)
                (progn
                  (setf last-name name)
                  (setf max-cnt (max max-cnt (setf cur-cnt 1)))))
            (setf max-cnt (max max-cnt cur-cnt)))
          names)
    max-cnt))

(defun collect-answer (names max-cnt)
  (let ((cur-cnt 0)
        (last-name "")
        (answers (list)))
    (mapc (lambda (name)
            (if (string= name last-name)
                (incf cur-cnt)
                (progn
                  (setf cur-cnt 1)
                  (setf last-name name)))
            (when (= max-cnt cur-cnt)
              (push name answers)))
          names)
    answers))

(let* ((n (read))
       (names  (sort (loop repeat n collect (read-line)) #'string<))
       (answers (sort (collect-answer names (cul-max-cnt names)) #'string<)))
  (loop for i in answers do
        (format t "~a~%" i)))


(let* ((n (parse-integer (read-line nil nil)))
       (ht (make-hash-table :test #'equal))
       (maxvalue 1))
  (dotimes (i n)
    (let ((line (read-line nil nil)))
      (if (gethash line ht)
        (let ((newvalue (incf (gethash line ht))))
          (when (< maxvalue newvalue)
            (setq maxvalue newvalue)))
        (setf (gethash line ht) 1))))
  (let ((result nil))
    (maphash (lambda(key val)
               (when (= val maxvalue)
                 (setq result (cons key result)))) ht)
    (sort result #'string<)
    (mapcar (lambda(a) (format t "~A~%" a)) result)))


(defun splitat (c line)
  (labels ((rec (line acc)
				(let ((pos (position-if (lambda(cc) (char= c cc)) line)))
				  (if pos
					  (rec (subseq line (1+ pos)) (cons (subseq line 0 pos) acc))
					(nreverse (cons line acc))))))
	(rec line nil)))

(let* ((n (parse-integer (read-line nil nil)))
       (ht (make-hash-table :test #'equal))
       (maxvalue 0))
  (dotimes (i n)
    (let ((line (read-line nil nil)))
      (if (gethash line ht)
        (let ((newvalue (incf (gethash line ht))))
          (when (< maxvalue newvalue)
            (setq maxvalue newvalue)))
        (setf (gethash line ht) 1))))
  (maphash (lambda(key val)
             (when (= val maxvalue)
               (format t "~A~%" key))) ht))


    (let ((x (read))
          (y (read))
          (a (read))
          (b (read))
          (u (read)))
      (if (eq u x)
          (format t "~d ~d~%" (- a 1) b)
          (format t "~d ~d~%" a (- b 1))))

Score : 100 points

Problem Statement

We have A balls with the string S written on each of them and B balls with the string T written on each of them.

From these balls, Takahashi chooses one with the string U written on it and throws it away.

Find the number of balls with the string S and balls with the string T that we have now.

Constraints

S, T, and U are strings consisting of lowercase English letters.

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

S \not= T

S=U or T=U.

1 \leq A,B \leq 10

A and B are integers.

Input

Input is given from Standard Input in the following format:

S T
A B
U

Output

Print the answer, with space in between.

Sample Input 1

red blue
3 4
red

Sample Output 1

2 4

Takahashi chose a ball with red written on it and threw it away.
Now we have two balls with the string S and four balls with the string T.

Sample Input 2

red blue
5 5
blue

Sample Output 2

5 4

Takahashi chose a ball with blue written on it and threw it away.
Now we have five balls with the string S and four balls with the string T.


(let* ((n (read-string))
       (m (cons (read) (read)))
       (f (read-line)))
  (format t "~A ~A" (if (string= f (first n))
                        (1- (car m)) (car m))
          (if (string= f (second n))
              (1- (cdr m)) (cdr m))))


(defun splitat (c line)
  (labels ((rec (line acc)
				(let ((pos (position-if (lambda(cc) (char= c cc)) line)))
				  (if pos
					  (rec (subseq line (1+ pos)) (cons (subseq line 0 pos) acc))
					(nreverse (cons line acc))))))
	(rec line nil)))

(defun f(h a)
  (ceiling (/ h a)))
(let* ((line0 (splitat #\space (read-line nil nil)))
       (s (car line0))
       (tt (cadr line0))
       (line1 (mapcar #'parse-integer (splitat #\space (read-line nil nil))))
       (a (car line1))
       (b (cadr line1))
       (u (read-line nil nil))
       )
  (format t "~A ~A~%" (if (string= u s) (1- a) a) (if (string= u tt) (1- b) b)))


(defun solve (balls1 balls2 a b u)
  (if (equal u balls1)
      (list (1- a) b)
      (list a (1- b))))

#-swank
(let* ((balls (read-line))
       (a (read))
       (b (read))
       (u (read-line))
       (balls1 (subseq balls 0 (position #\Space balls)))
       (balls2 (subseq balls (1+ (position #\Space balls)))))
  (format t "~{~A~^ ~}~%" (solve balls1 balls2 a b u)))


(format t "~V@{~A~:*~}" (length (read-line)) "x")

Score : 200 points

Problem Statement

Given is a string S. Replace every character in S with x and print the result.

Constraints

S is a string consisting of lowercase English letters.

The length of S is between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

Replace every character in S with x and print the result.

Sample Input 1

sardine

Sample Output 1

xxxxxxx

Replacing every character in S with x results in xxxxxxx.

Sample Input 2

xxxx

Sample Output 2

xxxx

Sample Input 3

gone

Sample Output 3

xxxx


(let* ((n (read))
       (m (remove-duplicates (loop :repeat n :collect (read)))))
  (if (= (length m) n)
      (princ "YES")
      (princ "NO")))

Score : 300 points

Problem Statement

Given is a sequence of integers A_1, A_2, ..., A_N.
If its elements are pairwise distinct, print YES; otherwise, print NO.

Constraints

2 ≤ N ≤ 200000

1 ≤ A_i ≤ 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

If the elements of the sequence are pairwise distinct, print YES; otherwise, print NO.

Sample Input 1

5
2 6 1 4 5

Sample Output 1

YES

The elements are pairwise distinct.

Sample Input 2

6
4 1 3 1 6 2

Sample Output 2

NO

The second and fourth elements are identical.

Sample Input 3

2
10000000 10000000

Sample Output 3

NO


(defun solve (n a)
  (let ((hash (make-hash-table)))
    (loop for x in a
          if (gethash x hash)
          do (return "NO")
          else
          do (setf (gethash x hash) t)
          finally (return "YES"))))

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


(let ((h (read))
      (a (read)))
     (princ
      (ceiling h a)))

Score : 100 points

Problem Statement

Serval is fighting with a monster.

The health of the monster is H.

In one attack, Serval can decrease the monster's health by A.
There is no other way to decrease the monster's health.

Serval wins when the monster's health becomes 0 or below.

Find the number of attacks Serval needs to make before winning.

Constraints

1 \leq H \leq 10^4

1 \leq A \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H A

Output

Print the number of attacks Serval needs to make before winning.

Sample Input 1

10 4

Sample Output 1

3

After one attack, the monster's health will be 6.

After two attacks, the monster's health will be 2.

After three attacks, the monster's health will be -2.

Thus, Serval needs to make three attacks to win.

Sample Input 2

1 10000

Sample Output 2

1

Sample Input 3

10000 1

Sample Output 3

10000


(defun read-and-sum (n)
  (loop :repeat n
        :sum (read)))

(let* ((enemy-hp (read))
       (n (read))
       (deathblow-damages (read-and-sum n)))
  (if (<= enemy-hp (apply #'+ deathblow-damages))
      (princ "Yes")
      (princ "No")))

Score : 200 points

Problem Statement

Raccoon is fighting with a monster.

The health of the monster is H.

Raccoon can use N kinds of special moves. Using the i-th move decreases the monster's health by A_i.
There is no other way to decrease the monster's health.

Raccoon wins when the monster's health becomes 0 or below.

If Raccoon can win without using the same move twice or more, print Yes; otherwise, print No.

Constraints

1 \leq H \leq 10^9

1 \leq N \leq 10^5

1 \leq A_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H N
A_1 A_2 ... A_N

Output

If Raccoon can win without using the same move twice or more, print Yes; otherwise, print No.

Sample Input 1

10 3
4 5 6

Sample Output 1

Yes

The monster's health will become 0 or below after, for example, using the second and third moves.

Sample Input 2

20 3
4 5 6

Sample Output 2

No

Sample Input 3

210 5
31 41 59 26 53

Sample Output 3

Yes

Sample Input 4

211 5
31 41 59 26 53

Sample Output 4

No


(defun splitat (c line)
  (labels ((rec (line acc)
				(let ((pos (position-if (lambda(cc) (char= c cc)) line)))
				  (if pos
					  (rec (subseq line (1+ pos)) (cons (subseq line 0 pos) acc))
					(nreverse (cons line acc))))))
	(rec line nil)))

(defun f(h ai)
  (<= h (reduce #'+ ai)))
(let* ((line0 (mapcar #'read-from-string (splitat #\space (read-line nil nil))))
      (line2 (mapcar #'read-from-string (splitat #\space (read-line nil nil))))
      (line1 (make-array (length line2) :initial-contents line2)))
  (format t "~A~%" (if (f (car line0) line1) "Yes" "No")))


(defun splitat (c line)
  (labels ((rec (line acc)
				(let ((pos (position-if (lambda(cc) (char= c cc)) line)))
				  (if pos
					  (rec (subseq line (1+ pos)) (cons (subseq line 0 pos) acc))
					(nreverse (cons line acc))))))
	(rec line nil)))

(defun f(h ai)
  (<= h (reduce #'+ ai)))
(let ((line0 (mapcar #'read-from-string (splitat #\space (read-line nil nil))))
      (line1 (map 'array #'identity (mapcar #'read-from-string (splitat #\space (read-line nil nil))))))
  (format t "~A~%" (if (f (car line0) line1) "Yes" "No")))


(defun splitat (c line)
  (labels ((rec (line acc)
				(let ((pos (position-if (lambda(cc) (char= c cc)) line)))
				  (if pos
					  (rec (subseq line (1+ pos)) (cons (subseq line 0 pos) acc))
					(nreverse (cons line acc))))))
	(rec line nil)))

(defun f(h ai)
  (<= h (reduce #'+ ai)))
(let ((line0 (mapcar #'parse-integer (splitat #\space (read-line nil nil))))
      (line1 (map 'array #'identity (mapcar #'parse-integer (splitat #\space (read-line nil nil))))))
  (format t "~A~%" (if (f (car line0) line1) "Yes" "No")))


(defun create-data ()
  (let ((n (read))
        (k (read))
        (h))
    (dotimes (i n)
      (push (read) h))
    (sort (copy-list h) #'>)
    (values n k h)))

(defun fennec-vs-monster (n k h)
  (dotimes (i k)
    (pop h)
    (if (null h)
        (return)))
  (loop for i in h
        sum i))

(multiple-value-bind (n k h) (create-data)
  (format t "~D~%" (fennec-vs-monster n k h)))

Score : 300 points

Problem Statement

Fennec is fighting with N monsters.

The health of the i-th monster is H_i.

Fennec can do the following two actions:

Attack: Fennec chooses one monster. That monster's health will decrease by 1.

Special Move: Fennec chooses one monster. That monster's health will become 0.

There is no way other than Attack and Special Move to decrease the monsters' health.

Fennec wins when all the monsters' healths become 0 or below.

Find the minimum number of times Fennec needs to do Attack (not counting Special Move) before winning when she can use Special Move at most K times.

Constraints

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

0 \leq K \leq 2 \times 10^5

1 \leq H_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
H_1 ... H_N

Output

Print the minimum number of times Fennec needs to do Attack (not counting Special Move) before winning.

Sample Input 1

3 1
4 1 5

Sample Output 1

5

By using Special Move on the third monster, and doing Attack four times on the first monster and once on the second monster, Fennec can win with five Attacks.

Sample Input 2

8 9
7 9 3 2 3 8 4 6

Sample Output 2

0

She can use Special Move on all the monsters.

Sample Input 3

3 0
1000000000 1000000000 1000000000

Sample Output 3

3000000000

Watch out for overflow.


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

(let* ((n (read))
	   (k (read))
	   (h (concatenate 'vector (sort (loop repeat n collect (read)) #'<))))
  (princ (loop for i below (- n k) sum (aref h i))))


(defun solve (h)
  (if (= h 1)
      1
      (1+ (* 2 (solve (floor h 2))))))

#-swank
(let* ((h (read)))
  (format t "~A~%" (solve h)))

Score : 400 points

Problem Statement

Caracal is fighting with a monster.

The health of the monster is H.

Caracal can attack by choosing one monster. When a monster is attacked, depending on that monster's health, the following happens:

If the monster's health is 1, it drops to 0.

If the monster's health, X, is greater than 1, that monster disappears. Then, two new monsters appear, each with the health of \lfloor X/2 \rfloor.

(\lfloor r \rfloor denotes the greatest integer not exceeding r.)

Caracal wins when the healths of all existing monsters become 0 or below.

Find the minimum number of attacks Caracal needs to make before winning.

Constraints

1 \leq H \leq 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H

Output

Find the minimum number of attacks Caracal needs to make before winning.

Sample Input 1

2

Sample Output 1

3

When Caracal attacks the initial monster, it disappears, and two monsters appear, each with the health of 1.

Then, Caracal can attack each of these new monsters once and win with a total of three attacks.

Sample Input 2

4

Sample Output 2

7

Sample Input 3

1000000000000

Sample Output 3

1099511627775


(defun comparing-strings (a b)
  (if (<= a b)
      (dotimes (i b)
        (prin1 a))
      (dotimes (i a)
        (prin1 b))))

(format t "~A~%" (comparing-strings (read) (read)))

Score : 200 points

Problem Statement

Given are 1-digit positive integers a and b. Consider these two strings: the concatenation of b copies of the digit a, and the concatenation of a copies of the digit b. Which of these is lexicographically smaller?

Constraints

1 \leq a \leq 9

1 \leq b \leq 9

a and b are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

Print the lexicographically smaller of the two strings. (If the two strings are equal, print one of them.)

Sample Input 1

4 3

Sample Output 1

3333

We have two strings 444 and 3333. Between them, 3333 is the lexicographically smaller.

Sample Input 2

7 7

Sample Output 2

7777777


(let* ((ls (read-from-string (concatenate 'string "(" (read-line) ")")))
       (mx (apply #'max ls))
       (mn (apply #'min ls)))
  (princ (make-string mx :initial-element (char (prin1-to-string mn) 0))))


(let ((a (read))
      (b (read)))
     (loop repeat (max a b)
           do (format t "~A" (min a b))))


(defun low-elements (n p)
  (let ((ans 0)
        (min n))
    (dotimes (i n)
      (when (<= (nth i p) min)
        (setf min (nth i p))
        (incf ans)))
    ans))

(defun create-data ()
  (let ((n (read))
        (p))
    (dotimes (i n)
      (push (read) p))
    (values n p)))

(multiple-value-bind (n p) (create-data)
  (format t "~D~%" (low-elements n p)))

Score : 300 points

Problem Statement

Given is a permutation P_1, \ldots, P_N of 1, \ldots, N.
Find the number of integers i (1 \leq i \leq N) that satisfy the following condition:

For any integer j (1 \leq j \leq i), P_i \leq P_j.

Constraints

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

P_1, \ldots, P_N is a permutation of 1, \ldots, N.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 ... P_N

Output

Print the number of integers i that satisfy the condition.

Sample Input 1

5
4 2 5 1 3

Sample Output 1

3

i=1, 2, and 4 satisfy the condition, but i=3 does not - for example, P_i > P_j holds for j = 1.

Similarly, i=5 does not satisfy the condition, either. Thus, there are three integers that satisfy the condition.

Sample Input 2

4
4 3 2 1

Sample Output 2

4

All integers i (1 \leq i \leq N) satisfy the condition.

Sample Input 3

6
1 2 3 4 5 6

Sample Output 3

1

Only i=1 satisfies the condition.

Sample Input 4

8
5 7 4 2 6 8 1 3

Sample Output 4

4

Sample Input 5

1
1

Sample Output 5

1


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

(defparameter N (read))
(defparameter lst
  (make-array N
              :initial-contents
              (loop repeat N collect (read))))

(defun drop (lst)
  (loop with c = 1
        with tmp = (aref lst 0)
        for i from 1 below N
        do (if (> (aref lst (1- i))
                  (aref lst i))
               (progn
                 (incf c)
                 (setf tmp (aref lst i))))
        finally (return c)))

(defun answer (n lst)
  (if (= n 1)
      1
      (drop lst)))

(format t "~A" (answer N lst))


(defun get-input-array (n)
  (let ((rst (make-array n)))
    (dotimes (i n)
      (setf (aref rst i) (read)))
    rst))

(defun main (n)
  (let ((rst 0)
        (temp 0)
        (input-array (get-input-array n)))
    (loop for x from 0 to (- n 1)
          for num = (aref input-array x)
          if (or (= temp 0) (>= temp num))
          do (loop for y from 0 to x
                   if (< (aref input-array y) num) do (return)
                   finally (incf rst))
          if (< temp num) do (setf temp num)
          )
    rst))

(format t "~A~%" (main (read)))


;; E - Flatten

(defun main ()
  (let* ((n (read))
         (a (loop repeat n collect (read))))
    (princ (solve a))))

(defparameter *modulus* (+ (expt 10 9) 7))

(defun solve (a)
  (mod (* (apply #'lcm a) (loop for x in a sum (/ 1 x))) *modulus*))

(main)

Score : 500 points

Problem Statement

Given are N positive integers A_1,...,A_N.

Consider positive integers B_1, ..., B_N that satisfy the following condition.

Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds.

Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N.

Since the answer can be enormous, print the sum modulo (10^9 +7).

Constraints

1 \leq N \leq 10^4

1 \leq A_i \leq 10^6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7).

Sample Input 1

3
2 3 4

Sample Output 1

13

Let B_1=6, B_2=4, and B_3=3, and the condition will be satisfied.

Sample Input 2

5
12 12 12 12 12

Sample Output 2

5

We can let all B_i be 1.

Sample Input 3

3
1000000 999999 999998

Sample Output 3

996989508

Print the sum modulo (10^9+7).


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

;; BEGIN_INSERTED_CONTENTS
;;;
;;; Binomial coefficient with mod
;;; build: O(n)
;;; query: O(1)
;;;

;; TODO: non-global handling

(defconstant +binom-size+ 1000001)
(defconstant +binom-mod+ #.(+ (expt 10 9) 7))

(declaim ((simple-array (unsigned-byte 31) (*)) *fact* *fact-inv* *inv*))
(defparameter *fact* (make-array +binom-size+ :element-type '(unsigned-byte 31))
  "table of factorials")
(defparameter *fact-inv* (make-array +binom-size+ :element-type '(unsigned-byte 31))
  "table of inverses of factorials")
(defparameter *inv* (make-array +binom-size+ :element-type '(unsigned-byte 31))
  "table of inverses of non-negative integers")

(defun initialize-binom ()
  (declare (optimize (speed 3) (safety 0)))
  (setf (aref *fact* 0) 1
        (aref *fact* 1) 1
        (aref *fact-inv* 0) 1
        (aref *fact-inv* 1) 1
        (aref *inv* 1) 1)
  (loop for i from 2 below +binom-size+
        do (setf (aref *fact* i) (mod (* i (aref *fact* (- i 1))) +binom-mod+)
                 (aref *inv* i) (- +binom-mod+
                                   (mod (* (aref *inv* (rem +binom-mod+ i))
                                           (floor +binom-mod+ i))
                                        +binom-mod+))
                 (aref *fact-inv* i) (mod (* (aref *inv* i)
                                             (aref *fact-inv* (- i 1)))
                                          +binom-mod+))))

(initialize-binom)

;;;
;;; Arithmetic operations with static modulus
;;;

;; FIXME: Currently MOD* and MOD+ doesn't apply MOD when the number of
;; parameters is one.
(defmacro define-mod-operations (divisor)
  `(progn
     (defun mod* (&rest args)
       (reduce (lambda (x y) (mod (* x y) ,divisor)) args))

     (sb-c:define-source-transform mod* (&rest args)
       (if (null args)
           1
           (reduce (lambda (x y) `(mod (* ,x ,y) ,',divisor)) args)))

     (defun mod+ (&rest args)
       (reduce (lambda (x y) (mod (+ x y) ,divisor)) args))

     (sb-c:define-source-transform mod+ (&rest args)
       (if (null args)
           0
           (reduce (lambda (x y) `(mod (+ ,x ,y) ,',divisor)) args)))

     (define-modify-macro incfmod (delta)
       (lambda (x y) (mod (+ x y) ,divisor)))

     (define-modify-macro decfmod (delta)
       (lambda (x y) (mod (- x y) ,divisor)))

     (define-modify-macro mulfmod (multiplier)
       (lambda (x y) (mod (* x y) ,divisor)))))

(macrolet ((def (name fname)
             `(define-modify-macro ,name (new-value) ,fname)))
  (def minf min)
  (def maxf max)
  (def mulf *)
  (def divf /)
  (def iorf logior)
  (def xorf logxor)
  (def andf logand))

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

(declaim (ftype (function * (values simple-bit-vector &optional)) make-prime-table))
(defun make-prime-table (sup)
  "Returns a simple-bit-vector of length SUP, whose (0-based) i-th bit is 1 if i
is prime and 0 otherwise.

Example: (make-prime-table 10) => #*0011010100"
  (declare (optimize (speed 3) (safety 0)))
  (check-type sup (integer 2 (#.array-total-size-limit)))
  (let ((table (make-array sup :element-type 'bit :initial-element 0))
        (sup/64 (ceiling sup 64)))
    ;; special treatment for p = 2
    (dotimes (i sup/64)
      (setf (sb-kernel:%vector-raw-bits table i) #xAAAAAAAAAAAAAAAA))
    (setf (sbit table 1) 0
          (sbit table 2) 1)
    ;; p >= 3
    (loop for p from 3 to (+ 1 (isqrt (- sup 1))) by 2
          when (= 1 (sbit table p))
          do (loop for composite from (* p p) below sup by p
                   do (setf (sbit table composite) 0)))
    table))

;; FIXME: Currently the element type of the resultant vector is (UNSIGNED-BYTE 62).
(declaim (ftype (function * (values (simple-array (integer 0 #.most-positive-fixnum) (*))
                                    simple-bit-vector
                                    &optional))
                make-prime-sequence))
(defun make-prime-sequence (sup)
  "Returns the ascending sequence of primes smaller than SUP."
  (declare (optimize (speed 3) (safety 0)))
  (check-type sup (integer 2 (#.array-total-size-limit)))
  (let ((table (make-prime-table sup)))
    (let* ((length (count 1 table))
           (result (make-array length :element-type '(integer 0 #.most-positive-fixnum)))
           (index 0))
      (declare ((integer 0 #.most-positive-fixnum) length))
      (loop for x below sup
            when (= 1 (sbit table x))
            do (setf (aref result index) x)
               (incf index))
      (values result table))))

(defstruct (prime-data (:constructor %make-prime-data (seq table)))
  (seq nil :type (simple-array (integer 0 #.most-positive-fixnum) (*)))
  (table nil :type simple-bit-vector))

(defun make-prime-data (sup)
  (multiple-value-call #'%make-prime-data (make-prime-sequence sup)))

(declaim (inline factorize)
         (ftype (function * (values list &optional)) factorize))
(defun factorize (x prime-data)
  "Returns the associative list of prime factors of X, which is composed
of (<prime> . <exponent>). E.g. (factorize 100 <prime-table>) => '((2 . 2) (5
. 5)).

- Any numbers beyond the range of PRIME-DATA are regarded as prime.
- The returned list is in descending order w.r.t. prime factors."
  (declare (integer x))
  (setq x (abs x))
  (when (<= x 1)
    (return-from factorize nil))
  (let ((prime-seq (prime-data-seq prime-data))
        result)
    (loop for prime of-type unsigned-byte across prime-seq
          do (when (= x 1)
               (return-from factorize result))
             (loop for exponent of-type (integer 0 #.most-positive-fixnum) from 0
                   do (multiple-value-bind (quot rem) (floor x prime)
                        (if (zerop rem)
                            (setf x quot)
                            (progn
                              (when (> exponent 0)
                                (push (cons prime exponent) result))
                              (loop-finish))))))
    (if (= x 1)
        result
        (cons (cons x 1) result))))

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

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

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

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(define-mod-operations +mod+)
(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (pdata (make-prime-data 10000))
         (as (make-array n :element-type 'uint31))
         (lcm-table (make-hash-table)))
    (dotimes (i n)
      (setf (aref as i) (read-fixnum))
      (dolist (node (factorize (aref as i) pdata))
        (let ((prime (car node))
              (exp (cdr node)))
          (if (gethash prime lcm-table)
              (maxf (gethash prime lcm-table) exp)
              (setf (gethash prime lcm-table) exp)))))
    (let ((lcm 1))
      (declare (uint31 lcm))
      (maphash (lambda (prime exp)
                 (declare (uint31 prime exp))
                 (dotimes (_ exp)
                   (mulfmod lcm prime)))
               lcm-table)
      (let ((res 0))
        (declare (uint62 res))
        (loop for a across as
              do (incf res (mod* lcm (aref *inv* a))))
        (println (mod res +mod+))))))

#-swank (main)

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

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

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

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

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

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

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

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

;; To run: (5am:run! :sample)
#+swank
(it.bese.fiveam:test :sample
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "3
2 3 4
"
    "13
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "5
12 12 12 12 12
"
    "5
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "3
1000000 999999 999998
"
    "996989508
")))


(defparameter N (read))
(defparameter lst
  (loop repeat N collect (read)))
(defparameter const (+ 7 (expt 10 9)))

(defun calc (lst)
  (let ((l (apply #'lcm lst)))
       (mod (apply #'+
                   (mapcar #'(lambda (x) (mod (/ l x) const))
                           lst))
            const)))

(format t "~A" (calc lst))


;; F - Tree and Constraints

(defun main ()
  (let* ((n (read))
         (ab (loop repeat (1- n) collect (cons (read) (read))))
         (m (read))
         (uv (loop repeat m collect (cons (read) (read)))))
    (princ (solve n ab m uv))))

(defun make-adjacency-list (n edges)
  (let ((adj (make-array n :initial-element nil)))
    (loop for i from 0
          for edge in edges
          for a = (1- (car edge))
          for b = (1- (cdr edge))
          do (setf (aref adj a) (cons (cons b i) (aref adj a)))
             (setf (aref adj b) (cons (cons a i) (aref adj b))))
    adj))

(defun find-paths (m uv n adj)
  (labels ((dfs (u v parent)
             (if (eql u v)
                 (empty-set (1- n))
                 (loop for (a . i) in (aref adj u)
                       for p = (and (not (eql a parent))
                                    (dfs a v u))
                       when p do (set-add p i)
                                 (return p)))))
    (loop for (u . v) in uv
          collect (dfs (1- u) (1- v) nil))))

(defun coloring (n paths union sign)
  (if paths
      (+ (coloring n (cdr paths) union sign)
         (coloring n (cdr paths) (set-union union (car paths)) (- sign)))
      (* sign (ash 1 (- (1- n) (count-set-elem union))))))

(defun solve (n ab m uv)
  (let* ((adj (make-adjacency-list n ab))
         (paths (find-paths m uv n adj)))
    (coloring n paths (empty-set (1- n)) 1)))

;; set operations

(defun empty-set (n-elem)
  (make-array n-elem :element-type 'bit :initial-element 0))

(defun set-add (set elem)
  (setf (aref set elem) 1))

(defun set-union (set1 set2)
  (loop with result = (make-array (array-dimension set1 0) :element-type 'bit)
        for b1 across set1
        for b2 across set2
        for i from 0
        do (setf (aref result i) (logior b1 b2))
        finally (return result)))

(defun count-set-elem (set)
  (count 1 set))

(main)

Score : 600 points

Problem Statement

We have a tree with N vertices numbered 1 to N.
The i-th edge in this tree connects Vertex a_i and Vertex b_i.

Consider painting each of these edges white or black. There are 2^{N-1} such ways to paint the edges. Among them, how many satisfy all of the following M restrictions?

The i-th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black.

Constraints

2 \leq N \leq 50

1 \leq a_i,b_i \leq N

The graph given in input is a tree.

1 \leq M \leq \min(20,\frac{N(N-1)}{2})

1 \leq u_i < v_i \leq N

If i \not= j, either u_i \not=u_j or v_i\not=v_j

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}
M
u_1 v_1
:
u_M v_M

Output

Print the number of ways to paint the edges that satisfy all of the M conditions.

Sample Input 1

3
1 2
2 3
1
1 3

Sample Output 1

3

The tree in this input is shown below:

All of the M restrictions will be satisfied if Edge 1 and 2 are respectively painted (white, black), (black, white), or (black, black), so the answer is 3.

Sample Input 2

2
1 2
1
1 2

Sample Output 2

1

The tree in this input is shown below:

All of the M restrictions will be satisfied only if Edge 1 is painted black, so the answer is 1.

Sample Input 3

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

Sample Output 3

9

The tree in this input is shown below:

Sample Input 4

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

Sample Output 4

62

The tree in this input is shown below:


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

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

(defun convert (x r)
  (list (- x r) (+ x r)))

(defparameter N (parse-integer (read-line)))
(defparameter robots
  (make-array (list N 2)
              :initial-contents
              (sort (loop for i from 0 below N
                          collect (apply #'convert
                                         (mapcar #'parse-integer
                                                 (split " " (read-line)))))
        #'< :key #'car)))

(defun hoge (lst)
  (loop with count = N
        for i from 1 below N
        do (if (> (aref lst (1- i) 1)
                  (aref lst i 0))
               (progn (decf count)
                      (incf i)))
        finally (return count)))

(format t "~A" (hoge robots))

Score : 200 points

Problem Statement

In a factory, there are N robots placed on a number line.
Robot i is placed at coordinate X_i and can extend its arms of length L_i in both directions, positive and negative.

We want to remove zero or more robots so that the movable ranges of arms of no two remaining robots intersect.
Here, for each i (1 \leq i \leq N), the movable range of arms of Robot i is the part of the number line between the coordinates X_i - L_i and X_i + L_i, excluding the endpoints.

Find the maximum number of robots that we can keep.

Constraints

1 \leq N \leq 100,000

0 \leq X_i \leq 10^9 (1 \leq i \leq N)

1 \leq L_i \leq 10^9 (1 \leq i \leq N)

If i \neq j, X_i \neq X_j.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 L_1
X_2 L_2
\vdots
X_N L_N

Output

Print the maximum number of robots that we can keep.

Sample Input 1

4
2 4
4 3
9 3
100 5

Sample Output 1

3

By removing Robot 2, we can keep the other three robots.

Sample Input 2

2
8 20
1 10

Sample Output 2

1

Sample Input 3

5
10 1
2 1
4 1
6 1
8 1

Sample Output 3

5


(let ((c (read-char)))
  (format t "~A~%" (code-char (1+ (char-code c)))))

Score : 100 points

Problem Statement

Given is a lowercase English letter C that is not z. Print the letter that follows C in alphabetical order.

Constraints

C is a lowercase English letter that is not z.

Input

Input is given from Standard Input in the following format:

C

Output

Print the letter that follows C in alphabetical order.

Sample Input 1

a

Sample Output 1

b

a is followed by b.

Sample Input 2

y

Sample Output 2

z

y is followed by z.


(defun next-alphabet ()
  (let ((n (read-char))
        (i))
    (setf i (char-code n))
    (format t "~c~%" (code-char (1+ i)))))

(next-alphabet)


(let* ((n (read))
    (m (read))
    (a (make-array (list n 2) :initial-element 0))
    (ac 0)
    (wa 0))

    (loop for i below m do
        (let ((p (- (read) 1))
            (s (read)))
            (if (string= s "WA")
                (if (= (aref a p 1) 0)
                    (incf (aref a p 0)))
                (incf (aref a p 1)))))

    (loop for i below n do
        (if (>= (aref a i 1) 1)
            (progn
                (incf ac)
                (if (>= (aref a i 0) 1)
                    (incf wa (aref a i 0))))))
    (format t "~D ~D~%" ac wa)
)

Score : 300 points

Problem Statement

Takahashi participated in a contest on AtCoder.

The contest had N problems.

Takahashi made M submissions during the contest.

The i-th submission was made for the p_i-th problem and received the verdict S_i (AC or WA).

The number of Takahashi's correct answers is the number of problems on which he received an AC once or more.

The number of Takahashi's penalties is the sum of the following count for the problems on which he received an AC once or more: the number of WAs received before receiving an AC for the first time on that problem.

Find the numbers of Takahashi's correct answers and penalties.

Constraints

N, M, and p_i are integers.

1 \leq N \leq 10^5

0 \leq M \leq 10^5

1 \leq p_i \leq N

S_i is AC or WA.

Input

Input is given from Standard Input in the following format:

N M
p_1 S_1
:
p_M S_M

Output

Print the number of Takahashi's correct answers and the number of Takahashi's penalties.

Sample Input 1

2 5
1 WA
1 AC
2 WA
2 AC
2 WA

Sample Output 1

2 2

In his second submission, he received an AC on the first problem for the first time. Before this, he received one WA on this problem.

In his fourth submission, he received an AC on the second problem for the first time. Before this, he received one WA on this problem.

Thus, he has two correct answers and two penalties.

Sample Input 2

100000 3
7777 AC
7777 AC
7777 AC

Sample Output 2

1 0

Note that it is pointless to get an AC more than once on the same problem.

Sample Input 3

6 0

Sample Output 3

0 0


(defun create-data ()
  (let* ((n (read))
         (m (read))
         (p (make-array (1+ n) :initial-element 0))
         (s (make-array (1+ n) :initial-element 0))
         (buff1)
         (buff2))
    (dotimes (i m)
      (setf buff1 (read))
      (setf buff2 (read))
      (if (and (string-equal buff2 "AC")(= 0 (aref p buff1)))
          (incf (aref p buff1))
          (if (and (string-equal buff2 "WA") (= 0 (aref p buff1)))
              (incf (aref s buff1)))))
    (dotimes (i n)
      (if (and (= 0 (aref p i)) (/= 0 (aref s i)))
          (setf (aref s i) 0)))
    (values n p s)))

(defun welcome-to-atcoder (n p s)
  (let ((ans1 0)
        (ans2 0))
    (dotimes (i (1+ n))
      (setf ans1 (+ ans1 (aref p i)))
      (setf ans2 (+ ans2 (aref s i))))
    (values ans1 ans2)))

(multiple-value-bind (ans1 ans2)(multiple-value-bind (n p s) (create-data)
  (welcome-to-atcoder n p s))
  (format t "~D ~D~%" ans1 ans2))


(defun create-data ()
  (let* ((n (read))
         (m (read))
         (p (make-array (1+ n) :initial-element 0))
         (s (make-array (1+ n) :initial-element 0))
         (buff1)
         (buff2))
    (dotimes (i m)
      (setf buff1 (read))
      (setf buff2 (read))
      (if (and (string-equal buff2 "AC")(= 0 (aref p buff1)))
          (incf (aref p buff1))
          (if (and (string-equal buff2 "WA") (= 0 (aref p buff1)))
              (incf (aref s buff1)))))
    (dotimes (i m)
      (if (and (= 0 (aref p i)) (/= 0 (aref s i)))
          (setf (aref s i) 0)))
    (values n p s)))

(defun welcome-to-atcoder (n p s)
  (let ((ans1 0)
        (ans2 0))
    (dotimes (i (1+ n))
      (setf ans1 (+ ans1 (aref p i)))
      (setf ans2 (+ ans2 (aref s i))))
    (values ans1 ans2)))

(multiple-value-bind (ans1 ans2)(multiple-value-bind (n p s) (create-data)
  (welcome-to-atcoder n p s))
  (format t "~D ~D~%" ans1 ans2))


(defun create-data ()
  (let* ((n (read))
         (m (read))
         (p (make-array (1+ n) :initial-element 0))
         (s (make-array (1+ n) :initial-element 0))
         (buff1)
         (buff2))
    (dotimes (i m)
      (setf buff1 (read))
      (setf buff2 (read))
      (if (and (string-equal buff2 "AC")(= 0 (aref p buff1)))
          (incf (aref p buff1))
          (if (and (string-equal buff2 "WA") (= 0 (aref p buff1)))
              (incf (aref s buff1)))))
    (values n p s)))

(defun welcome-to-atcoder (n p s)
  (let ((ans1 0)
        (ans2 0))
    (dotimes (i (1+ n))
      (setf ans1 (+ ans1 (aref p i)))
      (setf ans2 (+ ans2 (aref s i))))
    (values ans1 ans2)))

(multiple-value-bind (ans1 ans2)(multiple-value-bind (n p s) (create-data)
  (welcome-to-atcoder n p s))
  (format t "~D ~D~%" ans1 ans2))


;; C - Welcome to AtCoder

(defun main ()
  (let* ((n (read)) ; 問題数
         (m (read)) ; 提出数
         (p-s (loop repeat m collect
                    (cons (read-fixnum) (read))))) ; 問題番号と結果
    (format t "~{~a~^ ~}~%" (solve n m p-s))))

(defun solve (n m p-s-lst)
  "正答数とペナルティ数のリストを返す"
  (let ((wa-count (make-array n :initial-element 0)) ; 各問の誤答数
        (ac-p     (make-array n :initial-element nil))) ; 正答したか
    (loop for p-s in p-s-lst
          for p = (1- (car p-s))
          for s = (cdr p-s)
          when (and (eq s 'WA) (not (aref ac-p p)))
            do (incf (aref wa-count p)) end
          when (eq s 'AC)
            do (setf (aref ac-p p) t) end)
    (list (loop for a across ac-p count a)
          (loop for w across wa-count sum w))))

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

(main)


(defvar N (read))
(defvar M (read))
(defvar ac 0)
(defvar wa 0)
(defvar exist-ac-count (list nil))
(defvar exist-wa-count (list nil))

(defun calc ()
  (let ((No (read))
        (Ans (read)))
    (cond ((string= Ans "AC")
           (unless (find No exist-ac-count)
             (when (find No exist-wa-count)
               (incf wa))
             (incf ac)
             (push No exist-ac-count)))
          ((string= Ans "WA")
           (unless (find No exist-wa-count)
             (push No exist-wa-count))
           (when (find No exist-ac-count)
             (incf wa))))
    (decf M)
    (if (= M 0)
        (format t "~D ~D~%" ac wa)
        (calc))))

(if (= M 0)
    (format t "0 0~%")
    (calc))


(defun calc (l max-count)
  (let ((No (1- (read)))
        (Ans (read)))
    (cond ((= max-count 1)
           (let ((AC 0)
                 (WA 0))
             (dolist (x l)
               (when (> (car x) 0)
                 (if (> (cdr x) 0)
                     (progn
                       (incf AC)
                       (incf WA))
                     (incf AC))))
             (return-from calc (cons AC WA))))
          ((string= Ans "AC")
           (incf (car (nth No l))))
          ((string= Ans "WA")
           (incf (cdr (nth No l)))))
    (calc l (decf max-count))))

(let ((N (read))
      (M (read)))
  (if (= M 0)
      (format t "0 0~%")
      (let* ((ans (loop repeat N
                        collect (cons 0 0)))
             (result (calc ans M)))
        (format t "~A ~A~%" (car result) (cdr result)))))


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

;; BEGIN_INSERTED_CONTENTS
;;;
;;; Queue with singly linked list
;;;

(defstruct (queue (:constructor make-queue
                      (&optional list &aux (tail (last list)))))
  (list nil :type list)
  (tail nil :type (or null (cons t null))))

(declaim (inline enqueue))
(defun enqueue (obj queue)
  "Pushes OBJ to the end of QUEUE."
  (symbol-macrolet ((list (queue-list queue))
                    (tail (queue-tail queue)))
    (if (null list)
        (setf tail (list obj)
              list tail)
        (setf (cdr tail) (list obj)
              tail (cdr tail))))
  queue)

(declaim (inline dequeue))
(defun dequeue (queue)
  "Removes and returns the element at the front of QUEUE. Returns NIL if QUEUE
is empty."
  (pop (queue-list queue)))

(declaim (inline queue-empty-p))
(defun queue-empty-p (queue)
  (null (queue-list queue)))

(declaim (inline queue-peek))
(defun queue-peek (queue)
  (car (queue-list queue)))

(declaim (inline enqueue-front))
(defun enqueue-front (obj queue)
  "Pushes OBJ to the front of QUEUE."
  (symbol-macrolet ((list (queue-list queue))
                    (tail (queue-tail queue)))
    (if (null list)
        (setf tail (list obj)
              list tail)
        (push obj list))
    queue))

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

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

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

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defconstant +inf+ #xffffffff)

(defun calc-longest (plan init-y init-x)
  (declare #.OPT
           (uint8 init-y init-x)
           ((simple-array bit (* *)) plan))
  (destructuring-bind (h w) (array-dimensions plan)
    (declare (uint8 h w))
    (let ((que (make-queue))
          (dists (make-array (list h w) :element-type 'uint32 :initial-element +inf+)))
      (labels ((visit (y x new-dist)
                 (when (and (<= 0 y (- h 1))
                            (<= 0 x (- w 1))
                            (= +inf+ (aref dists y x))
                            (zerop (aref plan y x)))
                   (setf (aref dists y x) new-dist)
                   (enqueue (cons y x) que))))
        (visit init-y init-x 0)
        (loop until (queue-empty-p que)
              for (y . x) = (dequeue que)
              for dist = (aref dists y x)
              do (visit (- y 1) x (+ 1 dist))
                 (visit (+ y 1) x (+ 1 dist))
                 (visit y (- x 1) (+ 1 dist))
                 (visit y (+ x 1) (+ 1 dist)))
        (loop for d across (array-storage-vector dists)
              when (< d +inf+)
              maximize d)))))

(defun main ()
  (let* ((h (read))
         (w (read))
         (plan (make-array (list h w) :element-type 'bit :initial-element 0)))
    (declare (uint8 h w))
    (dotimes (i h)
      (let ((line (read-line)))
        (dotimes (j w)
          (when (char= #\# (aref line j))
            (setf (aref plan i j) 1)))))
    (let ((res 0))
      (dotimes (y h)
        (dotimes (x w)
          (when (zerop (aref plan y x))
            (setq res (max res (calc-longest plan y x))))))
      (println res))))

#-swank (main)

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

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

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

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

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

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

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

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

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

Score : 400 points

Problem Statement

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

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

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

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

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

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

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

Constraints

1 \leq H,W \leq 20

S_{ij} is . or #.

S contains at least two occurrences of ..

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

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3 3
...
...
...

Sample Output 1

4

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

Sample Input 2

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

Sample Output 2

10

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


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

;; BEGIN_INSERTED_CONTENTS
(declaim (inline warshall-floyd!))
(defun warshall-floyd! (matrix)
  (let ((n (array-dimension matrix 0)))
    (dotimes (k n)
      (let ((base-k (array-row-major-index matrix k 0)))
        (dotimes (i n)
          (let ((base-i (array-row-major-index matrix i 0)))
            (dotimes (j n)
              (setf (row-major-aref matrix (+ base-i j))
                    (min (row-major-aref matrix (+ base-i j))
                         (+ (row-major-aref matrix (+ base-i k))
                            (row-major-aref matrix (+ base-k j)))))))))))
  matrix)

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

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

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

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defconstant +inf+ #xffffffff)
(defun main ()
  (declare #.OPT)
  (let* ((h (read))
         (w (read))
         (plan (make-array (list h w) :element-type 'bit :initial-element 0))
         (mat (make-array (list (* h w) (* h w))
                          :element-type 'uint32
                          :initial-element +inf+)))
    (declare (uint8 h w))
    (dotimes (i h)
      (let ((line (read-line)))
        (dotimes (j w)
          (when (char= #\# (aref line j))
            (setf (aref plan i j) 1)))))
    (labels ((encode (y x)
               (+ x (* y w)))
             (connect (y1 x1 y2 x2)
               (when (and (<= 0 y2 (- h 1))
                          (<= 0 x2 (- w 1))
                          (zerop (aref plan y2 x2)))
                 (let ((v1 (encode y1 x1))
                       (v2 (encode y2 x2)))
                   (setf (aref mat v1 v2) 1)))))
      (dotimes (i h)
        (dotimes (j w)
          (when (zerop (aref plan i j))
            (setf (aref mat (encode i j) (encode i j)) 0)
            (connect i j (- i 1) j)
            (connect i j (+ i 1) j)
            (connect i j i (- j 1))
            (connect i j i (+ j 1)))))
      (warshall-floyd! mat)
      (let ((res 0))
        (declare (uint31 res))
        (dotimes (y1 h)
          (dotimes (x1 w)
            (dotimes (y2 h)
              (dotimes (x2 w)
                (when (and (zerop (aref plan y1 x1))
                           (zerop (aref plan y2 x2)))
                  (let ((dist (aref mat (encode y1 x1) (encode y2 x2))))
                    (when (< dist +inf+)
                      (setq res (max res dist)))))))))
        (println res)))))

#-swank (main)

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

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

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

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

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

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

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

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

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