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

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

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

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

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

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defun main ()
  (let* ((input (read-line))
         (len (length input))
         (s (make-array (+ len 1) :element-type 'uint8))
         ;; 0: 繰り上げないで払う 1: 繰り上げて払う
         (dp (make-array (list (+ len 2) 2) :element-type 'uint62
                                            :initial-element most-positive-fixnum)))
    (declare (simple-string input))
    (loop for i below len
          for c across input
          for d = (- (char-code c) 48)
          do (setf (aref s (- len i 1)) d))
    (setf (aref dp 0 0) 0)
    (dotimes (x (+ len 1))
      (let ((d (aref s x)))
        ;; 繰り上げていない状態 -> 繰り上げていない状態
        (minf (aref dp (+ x 1) 0)
              (+ (aref dp x 0) d))
        ;; 繰り上げていない状態 -> 繰り上げた状態
        (minf (aref dp (+ x 1) 1)
              (+ (aref dp x 0) (- 10 d)))
        ;; 繰り上げた状態 -> 繰り上げていない状態
        (minf (aref dp (+ x 1) 0)
              (+ (aref dp x 1) d 1))
        ;; 繰り上げた状態 -> 繰り上げた状態
        (minf (aref dp (+ x 1) 1)
              (+ (aref dp x 1) (- 10 (+ d 1))))))
    (println (min (aref dp (+ len 1) 0)
                  (aref dp (+ len 1) 1)))
    ;; #>s
    ;; (let ((carry nil)
    ;;       (res 0))
    ;;   (dotimes (i (length s))
    ;;     (let ((d (aref s i)))
    ;;       #>d
    ;;       (when carry
    ;;         (incf d)
    ;;         (setq carry nil))
    ;;       (ecase d
    ;;         (0)
    ;;         (1 (incf res 1))
    ;;         (2 (incf res 2))
    ;;         (3 (incf res 3))
    ;;         (4 (incf res 4))
    ;;         (5 (incf res 5))
    ;;         (6 (incf res 4) (setq carry t))
    ;;         (7 (incf res 3) (setq carry t))
    ;;         (8 (incf res 2) (setq carry t))
    ;;         (9 (incf res 1) (setq carry t))
    ;;         (10 (setq carry t))))
    ;;     #>res)
    ;;   (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 "36
"
    "8
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "91
"
    "3
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170
"
    "243
")))

Score: 500 points

Problem Statement

In the Kingdom of AtCoder, only banknotes are used as currency. There are 10^{100}+1 kinds of banknotes, with the values of 1, 10, 10^2, 10^3, \dots, 10^{(10^{100})}. You have come shopping at a mall and are now buying a takoyaki machine with a value of N. (Takoyaki is the name of a Japanese snack.)

To make the payment, you will choose some amount of money which is at least N and give it to the clerk. Then, the clerk gives you back the change, which is the amount of money you give minus N.

What will be the minimum possible number of total banknotes used by you and the clerk, when both choose the combination of banknotes to minimize this count?

Assume that you have sufficient numbers of banknotes, and so does the clerk.

Constraints

N is an integer between 1 and 10^{1,000,000} (inclusive).

Input

Input is given from Standard Input in the following format:

N

Output

Print the minimum possible number of total banknotes used by you and the clerk.

Sample Input 1

36

Sample Output 1

8

If you give four banknotes of value 10 each, and the clerk gives you back four banknotes of value 1 each, a total of eight banknotes are used.

The payment cannot be made with less than eight banknotes in total, so the answer is 8.

Sample Input 2

91

Sample Output 2

3

If you give two banknotes of value 100, 1, and the clerk gives you back one banknote of value 10, a total of three banknotes are used.

Sample Input 3

314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170

Sample Output 3

243


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

(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 (inline bisect-left))
(defun bisect-left (target value &key (start 0) end (order #'<) (key #'identity))
  "TARGET := vector | function (taking an integer argument)
ORDER := strict order

Analogue of lower_bound() of C++ or bisect_left() of Python: Returns the
smallest index (or input) i that fulfills TARGET[i] >= VALUE, where '>=' is the
complement of ORDER. In other words, this function returns the leftmost index at
which VALUE can be inserted with keeping the order. Therefore, TARGET must be
monotonically non-decreasing with respect to ORDER.

- This function returns END if VALUE exceeds TARGET[END-1].
- The range [START, END) is half-open.
- END must be explicitly specified if TARGET is function.
- KEY is applied to each element of TARGET before comparison."
  (declare (function key order)
           (integer start)
           ((or null integer) end))
  (macrolet
      ((frob (accessor &optional declaration)
         `(progn
            (assert (<= start end))
            (if (= start end) end
                (labels
                    ((%bisect-left (left ok)
                       ;; TARGET[OK] >= VALUE always holds (assuming
                       ;; TARGET[END] = +infinity)
                       ,@(when declaration (list declaration))
                       (let ((mid (ash (+ left ok) -1)))
                         (if (= mid left)
                             (if (funcall order (funcall key (,accessor target left)) value)
                                 ok
                                 left)
                             (if (funcall order (funcall key (,accessor target mid)) value)
                                 (%bisect-left mid ok)
                                 (%bisect-left left mid))))))
                  (%bisect-left start end))))))
    (etypecase target
      (vector
       (let ((end (or end (length target))))
         (frob aref (declare ((integer 0 #.most-positive-fixnum) left ok)))))
      (function
       (assert end () "Requires END argument if TARGET is a function.")
       (frob funcall)))))

(declaim (inline bisect-right))
(defun bisect-right (target value &key (start 0) end (order #'<) (key #'identity))
  "TARGET := vector | function (taking an integer argument)
ORDER := strict order

Analogue of upper_bound() of C++ or bisect_right() of Python: Returns the
smallest index (or input) i that fulfills TARGET[i] > VALUE. In other words,
this function returns the rightmost index at which VALUE can be inserted with
keeping the order. Therefore, TARGET must be monotonically non-decreasing with
respect to ORDER.

- This function returns END if VALUE >= TARGET[END-1].
- The range [START, END) is half-open.
- END must be explicitly specified if TARGET is function.
- KEY is applied to each element of TARGET before comparison."
  (declare (function key order)
           (integer start)
           ((or null integer) end))
  (macrolet
      ((frob (accessor &optional declaration)
         `(progn
            (assert (<= start end))
            (if (= start end)
                end
                (labels
                    ((%bisect-right (left ok)
                       ;; TARGET[OK] > VALUE always holds (assuming
                       ;; TARGET[END] = +infinity)
                       ,@(when declaration (list declaration))
                       (let ((mid (ash (+ left ok) -1)))
                         (if (= mid left)
                             (if (funcall order value (funcall key (,accessor target left)))
                                 left
                                 ok)
                             (if (funcall order value (funcall key (,accessor target mid)))
                                 (%bisect-right left mid)
                                 (%bisect-right mid ok))))))

                  (%bisect-right start end))))))
    (etypecase target
      (vector
       (let ((end (or end (length target))))
         (frob aref (declare ((integer 0 #.most-positive-fixnum) left ok)))))
      (function
       (assert end () "Requires END argument if TARGET is a function.")
       (frob funcall)))))

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

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

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

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

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (m (read))
         (as (make-array n :element-type 'uint32))
         (bs (make-array n :element-type 'bit))
         (bits (make-array (+ n 1) :element-type 'bit))
         ;; vertex . edge-index
         (graph (make-array (+ n 1) :element-type 'list :initial-element nil))
         (visited (make-array (+ n 1) :element-type 'bit :initial-element 0))
         (res (make-array m :element-type 'bit :initial-element 0)))
    (dotimes (i n)
      (let ((a (read-fixnum))
            (b (read-fixnum)))
        (setf (aref as i) a
              (aref bs i) b)))
    (parallel-sort! as #'< bs)
    (dotimes (i (+ n 1))
      (cond ((= i 0)
             (setf (aref bits i) (aref bs i)))
            ((= i n)
             (setf (aref bits i) (aref bs (- n 1))))
            (t (setf (aref bits i)
                     (logxor (aref bs i) (aref bs (- i 1)))))))
    (dotimes (i m)
      (let* ((l (read-fixnum))
             (r (read-fixnum))
             (l-comped (bisect-left as l))
             (r-comped (bisect-right as r)))
        (push (cons l-comped i) (aref graph r-comped))
        (push (cons r-comped i) (aref graph l-comped))))
    (labels ((dfs (v)
               (setf (aref visited v) 1)
               (let ((xor (aref bits v)))
                 (declare (bit xor))
                 (dolist (node (aref graph v))
                   (let ((child (car node))
                         (edge-index (cdr node)))
                     (when (zerop (aref visited child))
                       (let ((child-xor (dfs child)))
                         (when (= 1 child-xor)
                           (setf (aref res edge-index) 1))
                         (xorf xor child-xor)))))
                 xor)))
      (dotimes (v (+ n 1))
        (when (zerop (aref visited v))
          (unless (zerop (dfs v))
            (println -1)
            (return-from main))))
      (let ((out (make-string-output-stream :element-type 'base-char)))
        (loop with init = t
              with count of-type uint31 = 0
              for i below m
              when (= 1 (aref res i))
              do (if init
                     (setq init nil)
                     (write-char #\  out))
                 (incf count)
                 (write (+ i 1) :stream out)
              finally (println count)
                      (write-line (get-output-stream-string out)))))))

#-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 "3 4
5 1
10 1
8 0
1 10
4 5
6 7
8 9
"
    "2
1 4
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "4 2
2 0
3 1
5 1
7 0
1 4
4 7
"
    "-1
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "3 2
5 0
10 0
8 0
6 9
66 99
"
    "0
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "12 20
536130100 1
150049660 1
79245447 1
132551741 0
89484841 1
328129089 0
623467741 0
248785745 0
421631475 0
498966877 0
43768791 1
112237273 0
21499042 142460201
58176487 384985131
88563042 144788076
120198276 497115965
134867387 563350571
211946499 458996604
233934566 297258009
335674184 555985828
414601661 520203502
101135608 501051309
90972258 300372385
255474956 630621190
436210625 517850028
145652401 192476406
377607297 520655694
244404406 304034433
112237273 359737255
392593015 463983307
150586788 504362212
54772353 83124235
"
    "5
1 7 8 9 11
")))

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

Score: 600 points

Problem Statement

After being invaded by the Kingdom of AlDebaran, bombs are planted throughout our country, AtCoder Kingdom.

Fortunately, our military team called ABC has managed to obtain a device that is a part of the system controlling the bombs.

There are N bombs, numbered 1 to N, planted in our country. Bomb i is planted at the coordinate A_i. It is currently activated if B_i=1, and deactivated if B_i=0.

The device has M cords numbered 1 to M. If we cut Cord j, the states of all the bombs planted between the coordinates L_j and R_j (inclusive) will be switched - from activated to deactivated, and vice versa.

Determine whether it is possible to deactivate all the bombs at the same time. If the answer is yes, output a set of cords that should be cut.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

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

A_i are pairwise distinct.

B_i is 0 or 1. (1 \leq i \leq N)

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

1 \leq L_j \leq R_j \leq 10^9\ (1 \leq j \leq M)

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_N B_N
L_1 R_1
:
L_M R_M

Output

If it is impossible to deactivate all the bombs at the same time, print -1. If it is possible to do so, print a set of cords that should be cut, as follows:

k
c_1 c_2 \dots c_k

Here, k is the number of cords (possibly 0), and c_1, c_2, \dots, c_k represent the cords that should be cut. 1 \leq c_1 < c_2 < \dots < c_k \leq M must hold.

Sample Input 1

3 4
5 1
10 1
8 0
1 10
4 5
6 7
8 9

Sample Output 1

2
1 4

There are two activated bombs at the coordinates 5, 10, and one deactivated bomb at the coordinate 8.

Cutting Cord 1 switches the states of all the bombs planted between the coordinates 1 and 10, that is, all of the three bombs.

Cutting Cord 4 switches the states of all the bombs planted between the coordinates 8 and 9, that is, Bomb 3.

Thus, we can deactivate all the bombs by cutting Cord 1 and Cord 4.

Sample Input 2

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

Sample Output 2

-1

Cutting any set of cords will not deactivate all the bombs at the same time.

Sample Input 3

3 2
5 0
10 0
8 0
6 9
66 99

Sample Output 3

0

All the bombs are already deactivated, so we do not need to cut any cord.

Sample Input 4

12 20
536130100 1
150049660 1
79245447 1
132551741 0
89484841 1
328129089 0
623467741 0
248785745 0
421631475 0
498966877 0
43768791 1
112237273 0
21499042 142460201
58176487 384985131
88563042 144788076
120198276 497115965
134867387 563350571
211946499 458996604
233934566 297258009
335674184 555985828
414601661 520203502
101135608 501051309
90972258 300372385
255474956 630621190
436210625 517850028
145652401 192476406
377607297 520655694
244404406 304034433
112237273 359737255
392593015 463983307
150586788 504362212
54772353 83124235

Sample Output 4

5
1 7 8 9 11

If there are multiple sets of cords that deactivate all the bombs when cut, any of them can be printed.

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 read-string () ;スペース区切りstring入力
  (let* ((str (read-line)))
    (labels ((instring (instring-str rt)
               (let* ((pos (position #\Space instring-str :test #'char=)))
                 (if pos
                     (instring (subseq instring-str (1+ pos))
                               (cons (subseq instring-str 0 pos) rt))
                     (cons instring-str rt)))))
      (reverse (instring str nil)))))
(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))))

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 ((a (read))
      (b (read))
      (c (read))
      (d (read))
      (e (read)))
  (if (eq a e)
    (progn
      (princ (1- c))
      (princ #\space)
      (princ d))
    (progn
      (princ c)
      (princ #\space)
      (princ (1- d)))))


(let* ((n (read))
       (as (make-array n :element-type 'fixnum)))
  (loop for i from 0 below n
        do (setf (aref as i) (read)))
  (setf as (sort as #'>))
  (princ (loop for i from 1 below n
               do (when (= (aref as i) (aref as (- i 1)))
                    (return "NO"))
               finally (return "YES")))
  (terpri))

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


(let* ((n (read))
       (k (read))
       (p (make-array (list (1+ n))))
       (s (make-array (list (1+ n)) :initial-element 0))
       (ans 0))
  (loop :for i :from 1 :to k
        :do (let ((x (read)))
              (setf (aref p i) x)
              (incf (aref s k) (aref p i))))
  (setf ans (aref s k))
  (loop :for i :from (1+ k) :to n
        :do (let ((x (read)))
              (setf (aref p i) x)
              (setf (aref s i) (+ (aref s (1- i)) (aref p i) (- (aref p (- i k)))))
              (setf ans (max ans (aref s i)))))
  (format t "~F~%" (/ (+ ans k) 2)))

Score : 400 points

Problem Statement

We have N dice arranged in a line from left to right. The i-th die from the left shows p_i numbers from 1 to p_i with equal probability when thrown.

We will choose K adjacent dice, throw each of them independently, and compute the sum of the numbers shown. Find the maximum possible value of the expected value of this sum.

Constraints

1 ≤ K ≤ N ≤ 200000

1 ≤ p_i ≤ 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
p_1 ... p_N

Output

Print the maximum possible value of the expected value of the sum of the numbers shown.

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

Sample Input 1

5 3
1 2 2 4 5

Sample Output 1

7.000000000000

When we throw the third, fourth, and fifth dice from the left, the expected value of the sum of the numbers shown is 7. This is the maximum value we can achieve.

Sample Input 2

4 1
6 6 6 6

Sample Output 2

3.500000000000

Regardless of which die we choose, the expected value of the number shown is 3.5.

Sample Input 3

10 4
17 13 13 12 15 20 10 13 17 11

Sample Output 3

32.000000000000


(defun solve (n k p)
  (/ (if (= n k)
       (+ (loop for i from 0 below k
                sum (aref p i))
          k)
       (loop with cur = (+ (loop for i from 0 below k
                                 sum (aref p i))
                           k)
             for i from k below n
             maximizing (setf cur (+ (- cur (aref p (- i k)))
                                     (aref p i)))))
     2.0d0))

#-swank
(let* ((n (read))
       (k (read))
       (p (make-array n :element-type 'fixnum))
       (*read-default-float-format* 'double-float))
  (loop for i from 0 below n
        do (setf (aref p i) (read)))
  (format t "~A~%" (solve n k p)))


;; F - Many Many Paths

(defparameter *modulus* (+ (expt 10 9) 7))
(defparameter *max-rc* (expt 10 6))

(defun main ()
  (let ((r1 (read))
        (c1 (read))
        (r2 (read))
        (c2 (read)))
    (princ (solve r1 c1 r2 c2))))

(defun solve (r1 c1 r2 c2)
  ; f(r,c) == choose(r+c, r)
  ; sum_{r=0}^{r2} sum_{c=0}^{c2} f(r,c) == f(r+1, c+1) - 1
  (labels ((f (r c) (choose (+ r c) r)))
    (mod+ (f (1+ r2) (1+ c2))
          (- (f (1+ r2) c1))
          (- (f r1 (1+ c2)))
          (f r1 c1))))

;; modulo addition/product/division

(defun mod+ (&rest rest)
  (mod (apply #'+ rest) *modulus*))

(defun mod* (&rest rest)
  (mod (apply #'* rest) *modulus*))

(defun mod/ (num &rest denom)
  (apply #'mod* (cons num (mapcar #'mod-inverse denom))))

(defun bit-sequence (v)
  "(bit-sequence 13) => (1 1 0 1)"
  (nreverse
    (loop for x = v then (ash x -1) while (plusp x)
          collect (logand x 1))))

(let ((modulus-2-bits (bit-sequence (- *modulus* 2))))
  ; Mが素数のとき，a^(M-2) ≡ a^(-1) mod M (フェルマーの小定理より)
  (defun mod-inverse (a)
    (loop for b in modulus-2-bits
          for e = a then (mod* e e (if (zerop b) 1 a))
          finally (return e))))

(let* ((max-n (+ *max-rc* *max-rc* 2))
       (fact (make-array (1+ max-n)))) ; 階乗
  (setf (aref fact 0) 1) ; 0! = 1
  (loop for i from 1 to max-n
     do (setf (aref fact i) (mod* (aref fact (1- i)) i)))
  (defun choose (n k)
    (mod/ (aref fact n) (aref fact k) (aref fact (- n k)))))

(main)

Score : 600 points

Problem Statement

Snuke is standing on a two-dimensional plane. In one operation, he can move by 1 in the positive x-direction, or move by 1 in the positive y-direction.

Let us define a function f(r, c) as follows:

f(r,c) := (The number of paths from the point (0, 0) to the point (r, c) that Snuke can trace by repeating the operation above)

Given are integers r_1, r_2, c_1, and c_2.
Find the sum of f(i, j) over all pair of integers (i, j) such that r_1 ≤ i ≤ r_2 and c_1 ≤ j ≤ c_2, and compute this value modulo (10^9+7).

Constraints

1 ≤ r_1 ≤ r_2 ≤ 10^6

1 ≤ c_1 ≤ c_2 ≤ 10^6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

r_1 c_1 r_2 c_2

Output

Print the sum of f(i, j) modulo (10^9+7).

Sample Input 1

1 1 2 2

Sample Output 1

14

For example, there are two paths from the point (0, 0) to the point (1, 1): (0,0) → (0,1) → (1,1) and (0,0) → (1,0) → (1,1), so f(1,1)=2.

Similarly, f(1,2)=3, f(2,1)=3, and f(2,2)=6. Thus, the sum is 14.

Sample Input 2

314 159 2653 589

Sample Output 2

602215194


(defparameter const (+ 7 (expt 10 9)))
(defparameter r1 (read))
(defparameter c1 (read))
(defparameter r2 (read))
(defparameter c2 (read))

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

(defun comb (r c)
  (/ (fact (+ r c)) (fact r) (fact c)))

(format t "~A" (loop with count = 0
                     for r from r1 to r2
                     do (loop for c from c1 to c2
                              do (incf count (mod (comb r c)
                                                  const))
                                 (setf count (mod count const)))
                        (setf count (mod count const))
                     finally (return (mod count const))))


(let ((h (read))
      (a (read)))
  (format t "~A~%" (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 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 (mapcar #'parse-integer (splitat #\space (read-line nil nil))))
  (format t "~A~%" (if (f (car line0) line1) "Yes" "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


(let* ((h (read))
       (n (read))
       (lst (loop :repeat n :sum (read))))
  (if (<= h lst) (princ "Yes") (princ "No")))


(defvar N (read))
(defvar K (read))

(princ (reduce #'+ (nthcdr K (sort (loop :repeat N :collect (read)) #'>))))

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.


(defun solve (array e-num spa &optional (pos 0) (result 0))
  (cond ((and (= spa 0) (= e-num 0))
         result)
        ((= spa 0)
         (solve array (1- e-num) spa (1+ pos) (+ result (aref array pos))))
        (t
         (solve array (1- e-num) (1- spa) (1+ pos) result))))

(let* ((Enemy-Numbers (read))
       (Special-Atack (read))
       (Enemy-list (make-array Enemy-Numbers :initial-element 0)))
  (dotimes (x Enemy-Numbers)
    (setf (aref Enemy-list x) (read)))
  (sort Enemy-list #'>)
  (print (if (>= Special-Atack Enemy-Numbers)
             0
             (solve Enemy-list Enemy-Numbers Special-Atack))))


(let* ((n (read))
       (k (read))
       (hs (make-array n :element-type 'integer)))
  (loop for i from 0 below n do (setf (aref hs i) (read)))
  (setf hs (sort hs #'>=))
  (format t "~a~%" (loop for i from k below n
                         summing (aref hs i))))


(let* ((n (read)))
  (princ (loop :for k :from 0 :upto (1- (length (format nil "~b" n))) :sum (expt 2 k))))

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


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

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

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

(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+ most-positive-fixnum)
(defun main ()
  (declare #.OPT)
  (let* ((h (read))
         (n (read))
         (as (make-array n :element-type 'uint31))
         (bs (make-array n :element-type 'uint31))
         (dp (make-array 10001 :element-type 'uint62)))
    (declare (uint31 h n))
    (dotimes (i n)
      (setf (aref as i) (read-fixnum)
            (aref bs i) (read-fixnum)))
    (dotimes (x (+ n 1))
      (dotimes (y (+ h 1))
        (setf (aref dp y)
              (cond ((zerop y) 0)
                    ((zerop x) +inf+)
                    (t (let ((a (aref as (- x 1)))
                             (b (aref bs (- x 1))))
                         (min (+ (aref dp (max 0 (- y a))) b)
                              (aref dp y))))))))
    (println (aref dp h))))

#-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 "9 3
8 3
4 2
2 1
"
    "4
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "100 6
1 1
2 3
3 9
4 27
5 81
6 243
"
    "100
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "9999 10
540 7550
691 9680
700 9790
510 7150
415 5818
551 7712
587 8227
619 8671
588 8228
176 2461
"
    "139815
")))

Score : 500 points

Problem Statement

Ibis is fighting with a monster.

The health of the monster is H.

Ibis can cast N kinds of spells. Casting the i-th spell decreases the monster's health by A_i, at the cost of B_i Magic Points.

The same spell can be cast multiple times. There is no way other than spells to decrease the monster's health.

Ibis wins when the health of the monster becomes 0 or below.

Find the minimum total Magic Points that have to be consumed before winning.

Constraints

1 \leq H \leq 10^4

1 \leq N \leq 10^3

1 \leq A_i \leq 10^4

1 \leq B_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 B_1
:
A_N B_N

Output

Print the minimum total Magic Points that have to be consumed before winning.

Sample Input 1

9 3
8 3
4 2
2 1

Sample Output 1

4

First, let us cast the first spell to decrease the monster's health by 8, at the cost of 3 Magic Points. The monster's health is now 1.

Then, cast the third spell to decrease the monster's health by 2, at the cost of 1 Magic Point. The monster's health is now -1.

In this way, we can win at the total cost of 4 Magic Points.

Sample Input 2

100 6
1 1
2 3
3 9
4 27
5 81
6 243

Sample Output 2

100

It is optimal to cast the first spell 100 times.

Sample Input 3

9999 10
540 7550
691 9680
700 9790
510 7150
415 5818
551 7712
587 8227
619 8671
588 8228
176 2461

Sample Output 3

139815


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

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

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

(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+ most-positive-fixnum)
(defun main ()
  (declare #.OPT)
  (let* ((h (read))
         (n (read))
         (as (make-array n :element-type 'uint31))
         (bs (make-array n :element-type 'uint31))
         (dp (make-array '(1001 10001) :element-type 'uint62)))
    (declare (uint31 h n))
    (dotimes (i n)
      (setf (aref as i) (read-fixnum)
            (aref bs i) (read-fixnum)))
    (dotimes (x (+ n 1))
      (dotimes (y (+ h 1))
        (setf (aref dp x y)
              (cond ((zerop y) 0)
                    ((zerop x) +inf+)
                    (t (let ((a (aref as (- x 1)))
                             (b (aref bs (- x 1))))
                         (min (+ (aref dp x (max 0 (- y a))) b)
                              (+ (aref dp (- x 1) (max 0 (- y a))) b)
                              (aref dp (- x 1) y))))))))
    (println (aref dp n h))))

#-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 "9 3
8 3
4 2
2 1
"
    "4
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "100 6
1 1
2 3
3 9
4 27
5 81
6 243
"
    "100
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "9999 10
540 7550
691 9680
700 9790
510 7150
415 5818
551 7712
587 8227
619 8671
588 8228
176 2461
"
    "139815
")))


(defun acorwa (a b)
  (if (= a b)
      "Yes"
      "No"
      )
  )
(princ (acorwa (read) (read)))

Score : 100 points

Problem Statement

Takahashi is participating in a programming contest, AXC001. He has just submitted his code to Problem A.

The problem has N test cases, all of which must be passed to get an AC verdict.

Takahashi's submission has passed M cases out of the N test cases.

Determine whether Takahashi's submission gets an AC.

Constraints

1 \leq N \leq 100

0 \leq M \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

If Takahashi's submission gets an AC, print Yes; otherwise, print No.

Sample Input 1

3 3

Sample Output 1

Yes

All three test cases have been passed, so his submission gets an AC.

Sample Input 2

3 2

Sample Output 2

No

Only two out of the three test cases have been passed, so his submission does not get an AC.

Sample Input 3

1 1

Sample Output 3

Yes


;; B - Comparing Strings

(let ((a (read))
      (b (read)))
  (format t "~{~a~}~%" (loop repeat (max a b) collect (min a b))))

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


(defmacro put-ans (n v)
  `(princ (coerce (make-array ,n :initial-element ,v) 'string)))

(let* ((a (read))
       (b (read))
       (a-char (char (write-to-string a) 0))
       (b-char (char (write-to-string b) 0)))
  (if (> a b)
      (put-ans a b-char)
      (put-ans b a-char)))


(defun solve (ps)
  (loop with tmp = -100000
        with count = 0
        for p in ps
        if (<= p current-min)
        do (setf current-min p)
           (incf count)
        finally (return count)))

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

(let* ((n (read))
       (ps (loop repeat n collect (read))))
  (format t "~A~%" (answer n ps)))

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


(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(lst)
  (labels((rec(i acc)
            (if (= i (length lst))
              acc
              (if (every (lambda (a) (<= (nth i lst) a)) (subseq lst 0 i))
                (rec (1+ i) (1+ acc))
                (rec (1+ i) acc)))))
    (rec 0 0)))
(compile 'f)
(let* ((line0 (read-line nil nil))
       (line (mapcar #'parse-integer (splitat #\space (read-line nil nil)))))
  (format t "~A~%" (f line)))


(let* ((n (read)))
  (princ (loop :for k :from 1 :upto n
               :sum (loop :for j :from 1 :upto n
                          :count (and (= (floor k (expt 10 (floor (log k 10))))
                                         (mod j 10))
                                      (= (floor j (expt 10 (floor (log j 10))))
                                         (mod k 10)))))))

Score : 400 points

Problem Statement

Given is a positive integer N.

Find the number of pairs (A, B) of positive integers not greater than N that satisfy the following condition:

When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B.

Constraints

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

25

Sample Output 1

17

The following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21), (21,12), (22,2), and (22,22).

Sample Input 2

1

Sample Output 2

1

Sample Input 3

100

Sample Output 3

108

Sample Input 4

2020

Sample Output 4

40812

Sample Input 5

200000

Sample Output 5

400000008


(defparameter N (read))
(defparameter matrix
  (make-array '(9 9)))

(defun convert (num)
  (let* ((s-num (write-to-string num))
         (a (aref s-num 0))
         (b (aref s-num (1- (length s-num)))))
        (cons (digit-char-p a)
              (digit-char-p b))))

(defun calc-matrix (num M)
  (loop for i from 1 to num
        if (not (zerop (mod i 10)))
        do (let ((pair (convert i)))
                (incf (aref M
                            (1- (car pair))
                            (1- (cdr pair)))))
        finally (return M)))

(defun answer (M)
  (loop with a = 0
   		for i from 0 below 9
        do (loop for j from 0 below 9
                 do (setf a (+ a (* (aref M i j)
                                    (aref M j i)))))
        finally (return a)))

(format t "~A" (answer (calc-matrix N matrix)))


;; -*- 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
;;;
;;; 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 mod-inverse (a)
  (declare #.OPT
           (uint31 a))
  (let ((a (mod a +mod+))
        (b +mod+)
        (u 1)
        (v 0))
    (declare (int32 a b u v))
    (loop until (zerop b)
          for quot = (floor a b)
          do (decf a (the fixnum (* quot b)))
             (rotatef a b)
             (decf u (the fixnum (* quot v)))
             (rotatef u v))
    (setq u (mod u +mod+))
    (if (< u 0)
        (+ u +mod+)
        u)))

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (pdata (make-prime-data 10000))
         (as (make-array n :element-type 'uint31))
         (lcm-table (make-hash-table :size 1000 :test #'eq)))
    (dotimes (i n)
      (setf (aref as i) (read-fixnum))
      (dolist (node (factorize (aref as i) pdata))
        (let ((prime (car node))
              (exp (cdr node)))
          (declare (uint31 prime exp))
          (if (gethash prime lcm-table)
              (maxf (the uint31 (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 (the uint31 (mod-inverse 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
")))

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


#-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)
  (list (- (car x) (cadr x))
        (1- (+ (car x) (cadr x)))))

(defun check (lst)
  (let ((counter 0)
        (tmp -100000000))
       (loop for i from 0 below N
             do (if (< tmp (aref lst i 0))
                    (progn
                      (incf counter)
                      (setf tmp (aref lst i 1))))
             finally (return counter))))

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

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

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


(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)
  (list (- (car x) (cadr x))
        (+ (car x) (cadr x) 1)))

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

(defun check (lst count)
  (loop for i from 1 below N
        do (if (> (aref lst (1- i) 1)
                  (aref lst i 0))
               (incf count))
        finally (return count)))

(format t "~A" (check lst 1))


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

(defun convert (x)
  (list (- (car x) (cadr x))
        (+ (car x) (cadr x) 1)))

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

(defun check (lst count)
  (loop for i from 1 below N
        do (if (> (aref lst (1- i) 1)
                  (aref lst i 0))
               (incf count))
        finally (return count)))

(format t "~A" (check lst 1))


;; -*- 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 println-sequence))
(defun println-sequence (sequence &key (out *standard-output*) (key #'identity))
  (let ((init t))
    (sequence:dosequence (x sequence)
      (if init
          (setq init nil)
          (write-char #\  out))
      (princ (funcall key x) out))
    (terpri out)))

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

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

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

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defun main ()
  (let* ((n (read))
         (k (read))
         (s (read))
         (res (make-array n :element-type 'uint31)))
    (if (zerop k)
        (if (= s #.(expt 10 9))
            (fill res 1)
            (fill res #.(expt 10 9)))
        (if (= s #.(expt 10 9))
            (dotimes (i n)
              (if (< i k)
                  (setf (aref res i) #.(expt 10 9))
                  (setf (aref res i) 1)))
            (dotimes (i n)
              (if (< i k)
                  (setf (aref res i) s)
                  (setf (aref res i) #.(expt 10 9))))))
    (println-sequence 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 "4 2 3
"
    "1 2 3 4
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "5 3 100
"
    "50 50 50 30 70
")))

Score : 400 points

Problem Statement

Given are three integers N, K, and S.

Find a sequence A_1, A_2, ..., A_N of N integers between 1 and 10^9 (inclusive) that satisfies the condition below.
We can prove that, under the conditions in Constraints, such a sequence always exists.

There are exactly K pairs (l, r) of integers such that 1 \leq l \leq r \leq N and A_l + A_{l + 1} + \cdots + A_r = S.

Constraints

1 \leq N \leq 10^5

0 \leq K \leq N

1 \leq S \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K S

Output

Print a sequence satisfying the condition, in the following format:

A_1 A_2 ... A_N

Sample Input 1

4 2 3

Sample Output 1

1 2 3 4

Two pairs (l, r) = (1, 2) and (3, 3) satisfy the condition in the statement.

Sample Input 2

5 3 100

Sample Output 2

50 50 50 30 70


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

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

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

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

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

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

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defun main ()
  (declare (inline sort))
  (let* ((n (read))
         (m (read))
         (ds (make-array n :element-type 'uint31))
         ;; d(u) <= d(v) for (u v . idx)
         (edges (make-array m :element-type 'list))
         (graph (make-array n :element-type 'list :initial-element nil))
         (cols (make-array n :element-type 'int32 :initial-element -1))
         (weights (make-array m :element-type 'int32 :initial-element -1)))
    (dotimes (i n)
      (setf (aref ds i) (read-fixnum)))
    (dotimes (i m)
      (let ((u (- (read-fixnum) 1))
            (v (- (read-fixnum) 1)))
        (when (> (aref ds u) (aref ds v))
          (rotatef u v))
        (setf (aref edges i) (list u v i))
        (push u (aref graph v))
        (push v (aref graph u))))
    (setq edges
          (sort edges
                (lambda (e1 e2)
                  (let ((max-d1 (aref ds (second e1)))
                        (max-d2 (aref ds (second e2))))
                    (or (< max-d1 max-d2)
                        (and (= max-d1 max-d2)
                             (< (the uint31 (third e1))
                                (the uint31 (third e2)))))))))
    #>graph
    (unless (loop
              for v below n
              always (loop
                       for neighbor in (aref graph v)
                       thereis (>= (aref ds v) (aref ds neighbor))))
      (println -1)
      (return-from main))
    (dotimes (i m)
      ;; d(u) <= d(v)
      (destructuring-bind (u v idx) (aref edges i)
        (cond ((= -1 (aref cols u) (aref cols v))
               (assert (= (aref ds u) (aref ds v)))
               (setf (aref cols u) 0
                     (aref cols v) 1)
               (setf (aref weights idx) (aref ds v)))
              ((= -1 (aref cols u))
               (setf (aref cols u) (logxor 1 (aref cols v)))
               (setf (aref weights idx) (aref ds v)))
              ((= -1 (aref cols v))
               (setf (aref cols v) (logxor 1 (aref cols u)))
               (setf (aref weights idx) (aref ds v)))
              (t
               (setf (aref weights idx) #.(expt 10 9))))))
    (with-buffered-stdout
      (sb-int:dovector (col cols (terpri))
        (write-char (if (zerop col) #\W #\B)))
      (dotimes (i m)
        (println (aref weights i))))))

#-swank (main)

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

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

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

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

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

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

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

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

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

Score : 900 points

Problem Statement

We have a connected undirected graph with N vertices and M edges.
Edge i in this graph (1 \leq i \leq M) connects Vertex U_i and Vertex V_i bidirectionally.
We are additionally given N integers D_1, D_2, ..., D_N.

Determine whether the conditions below can be satisfied by assigning a color - white or black - to each vertex and an integer weight between 1 and 10^9 (inclusive) to each edge in this graph.
If the answer is yes, find one such assignment of colors and integers, too.

There is at least one vertex assigned white and at least one vertex assigned black.

For each vertex v (1 \leq v \leq N), the following holds.

The minimum cost to travel from Vertex v to a vertex whose color assigned is different from that of Vertex v by traversing the edges is equal to D_v.

Here, the cost of traversing the edges is the sum of the weights of the edges traversed.

Constraints

2 \leq N \leq 100,000

1 \leq M \leq 200,000

1 \leq D_i \leq 10^9

1 \leq U_i, V_i \leq N

The given graph is connected and has no self-loops or multiple edges.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
D_1 D_2 ... D_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M

Output

If there is no assignment satisfying the conditions, print a single line containing -1.

If such an assignment exists, print one such assignment in the following format:

S
C_1
C_2
\vdots
C_M

Here,

the first line should contain the string S of length N. Its i-th character (1 \leq i \leq N) should be W if Vertex i is assigned white and B if it is assigned black.

The (i + 1)-th line (1 \leq i \leq M) should contain the integer weight C_i assigned to Edge i.

Sample Input 1

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

Sample Output 1

BWWBB
4
3
1
5
2

Assume that we assign the colors and integers as the sample output, and let us consider Vertex 5, for example. To travel from Vertex 5, which is assigned black, to a vertex that is assigned white with the minimum cost, we should make these moves: Vertex 5 \to Vertex 4 \to Vertex 2. The total cost of these moves is 7, which satisfies the condition. We can also verify that the condition is satisfied for other vertices.

Sample Input 2

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

Sample Output 2

-1

Sample Input 3

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

Sample Output 3

BBBW
1
1
1
2
1
1


(let* ((n (read))
       (ac (make-hash-table :size n))
       (wa (make-hash-table :size n))
       (m (read))
       (ac-num 0)
       (wa-num 0))
  (loop :for i :from 1 :to m
        :for p := (read)
        :for s := (read-line)
        :unless (gethash p ac)
        :do (if (string= s "AC")
                (setf (gethash p ac) 1)
                (if (gethash p wa)
                    (incf (gethash p wa))
                    (setf (gethash p wa) 1))))
  (maphash (lambda (key val)
             (incf ac-num val)
             (incf wa-num (if (gethash key wa) (gethash key wa) 0)))
           ac)
  (format t "~A ~A~%" ac-num wa-num))

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


(let* ((n (read))
       (c (make-hash-table :size n))
       (m (read))
       (ac 0)
       (wa 0))
  (loop :for i :from 1 :to m
        :for p := (read)
        :for s := (read-line)
        :unless (gethash p c)
        :do (cond ((string= s "AC")
                   (setf (gethash p c) t)
                   (incf ac))
                  (t
                   (incf wa))))
  (format t "~A ~A~%" ac wa))


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

(defvar ac 0)
(defvar wa 0)

(defvar ac-array (make-array N :initial-element 0))
(defvar wa-array (make-array N :initial-element 0))

(defun calc ()
  (let ((No (1- (read)))
        (Ans (read)))
    (cond ((string= Ans "AC")
           (when (zerop (aref ac-array No))
             (setq wa (+ wa (aref wa-array No)))
             (incf (aref ac-array No))
             (incf ac)))
          ((string= Ans "WA")
           (when (zerop (aref ac-array No))
             (incf (aref wa-array No)))))
    (decf M)
    (if (= M 0)
        (format t "~D ~D~%" ac wa)
        (calc))))

(calc)


(defun read-string () ;スペース区切りstring入力
  (let* ((str (read-line)))
    (labels ((instring (instring-str rt)
               (let* ((pos (position #\Space instring-str :test #'char=)))
                 (if pos
                     (instring (subseq instring-str (1+ pos))
                               (cons (subseq instring-str 0 pos) rt))
                     (cons instring-str rt)))))
      (reverse (instring str nil)))))

(let* ((n (read))
       (m (read))
       (lst (mapcar (lambda (k) (list (parse-integer (first k))
                                            (second k)))
                          (loop :repeat m :collect (read-string))))
       (arr (make-array (list (1+ n) 2) :initial-element 0 :element-type 'fixnum)))
  (mapcar (lambda (k) (if (string= (second k) "AC")
                          (setf (aref arr (first k) 1) 1)
                          (if (= (aref arr (first k) 1) 0)
                              (incf (aref arr (first k) 0))))) lst)
  (format t "~A ~A" (loop :for k :from 1 :upto n :count (= (aref arr k 1) 1))
          (loop :for k :from 1 :upto n :sum (if (= (aref arr k 1) 1) (aref arr k 0) 0))))


(let* ((n (read))
       (m (read))
       (lst (mapcar (lambda (k) (list (parse-integer (first k))
                                            (second k)))
                          (loop :repeat m :collect (read-string))))
       (arr (make-array (list (1+ n) 2) :initial-element 0 :element-type 'fixnum)))
  (mapcar (lambda (k) (if (string= (second k) "AC")
                          (setf (aref arr (first k) 1) 1)
                          (if (= (aref arr (first k) 1) 0)
                              (incf (aref arr (first k) 0))))) lst)
  (format t "~A ~A" (loop :for k :from 1 :upto n :count (= (aref arr k 1) 1))
          (loop :for k :from 1 :upto n :sum (aref arr k 0))))