Language: C++

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


Compiler output:
a.cc:3:6: error: invalid preprocessing directive #+
    3 |     #+swank '(optimize (speed 3) (safety 2))
      |      ^
a.cc:3:13: warning: missing terminating ' character
    3 |     #+swank '(optimize (speed 3) (safety 2))
      |             ^
a.cc:4:6: error: invalid preprocessing directive #-
    4 |     #-swank '(optimize (speed 3) (safety 0) (debug 0))
      |      ^
a.cc:4:13: warning: missing terminating ' character
    4 |     #-swank '(optimize (speed 3) (safety 0) (debug 0))
      |             ^
a.cc:5:6: warning: missing terminating ' character
    5 |     #'equal)
      |      ^
a.cc:5:6: error: invalid preprocessing directive #'equal)
    5 |     #'equal)
      |      ^~~~~~~
a.cc:6:4: error: invalid preprocessing directive #+
    6 |   #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t)
      |    ^
a.cc:6:25: warning: missing terminating ' character
    6 |   #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t)
      |                         ^
a.cc:7:4: error: invalid preprocessing directive #-
    7 |   #-swank (set-dispatch-macro-character
      |    ^
a.cc:9:13: error: invalid preprocessing directive #\
    9 |            #\# #\> (lambda (s c p) (declare (ignore c p)) `(values ,(read s nil nil t)))))
      |             ^
a.cc:10:2: error: invalid preprocessing directive #+
   10 | #+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)
      |  ^
a.cc:11:2: error: invalid preprocessing directive #-
   11 | #-swank (disable-debugger) ; for CS Academy
      |  ^
a.cc:17:12: error: stray '#' in program
   17 |   (declare #.OPT)
      |            ^
a.cc:19:16: error: stray '`' in program
   19 |                `(the (unsigned-byte 8)
      |                ^
a.cc:20:23: error: invalid preprocessing directive #+
   20 |                      #+swank (char-code (read-char in nil #\Nul))
      |                       ^
a.cc:21:23: error: invalid preprocessing directive #-
   21 |                      #-swank (sb-impl::ansi-stream-read-byte in nil #.(char-code #\Nul) nil))))
      |                       ^
a.cc:26:50: error: stray '#' in program
   26 |                                  ((zerop byte) ; #\Nul
      |                                                  ^
a.cc:26:51: error: stray '\' in program
   26 |                                  ((zerop byte) ; #\Nul
      |                                                   ^
a.cc:28:43: error: stray '#' in program
   28 |                                  ((= byte #.(char-code #\-))
      |                                           ^
a.cc:28:56: error: stray '#' in program
   28 |                                  ((= byte #.(char-code #\-))
      |                                                        ^
a.cc:28:57: error: stray '\' in program
   28 |                                  ((= byte #.(char-code #\-))
      |                                                         ^
a.cc:30:28: error: stray '#' in program
   30 |       (declare ((integer 0 #.most-positive-fixnum) result))
      |                            ^
a.cc:35:53: error: stray '#' in program
   35 |                               (* 10 (the (integer 0 #.(floor most-positive-fixnum 10))
      |                                                     ^
a.cc:40:4: error: invalid preprocessing directive #+
   40 |   #+swank
      |    ^
a.cc:42:7: error: stray '`' in program
   42 |       `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      |       ^
a.cc:42:44: warning: missing terminating ' character
   42 |       `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      |                                            ^
a.cc:42:44: error: missing terminating ' character
   42 |       `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      |                                            ^~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:43:7: error: stray '`' in program
   43 |       `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
      |       ^
a.cc:43:44: warning: missing terminating ' character
   43 |       `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
      |                                            ^
a.cc:43:44: error: missing terminating ' character
   43 |       `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
      |                                            ^~~~~~~~~~~~~~~~~~~~~
a.cc:44:4: error: invalid preprocessing directive #-
   44 |   #-swank (declare (ignore forms)))
      |    ^
a.cc:47:3: error: stray '`' in program
   47 |   `(progn
      |   ^
a.cc:48:7: error: stray '@' in program
   48 |      ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
      |       ^
a.cc:48:28: error: stray '`' in program
   48 |      ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
      |                            ^
a.cc:48:75: warning: missing terminating ' character
   48 |      ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
      |                                                                           ^
a.cc:48:75: error: missing terminating ' character
   48 |      ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
      |                                                                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:49:7: error: stray '@' in program
   49 |      ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
      |       ^
a.cc:49:28: error: stray '`' in program
   49 |      ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
      |                            ^
a.cc:49:74: warning: missing terminating ' character
   49 |      ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
      |                                                                          ^
a.cc:49:74: error: missing terminating ' character
   49 |      ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
      |                                                                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:54:38: warning: missing terminating ' character
   54 |   (let ((*read-default-float-format* 'double-float))
      |                                      ^
a.cc:54:38: error: missing terminating ' character
   54 |   (let ((*read-default-float-format* 'double-float))
      |                                      ^~~~~~~~~~~~~~~
a.cc:65:12: error: stray '#' in program
   65 |   (declare #.OPT)
      |            ^
a.cc:68:42: warning: missing terminating ' character
   68 |          (as (make-array n :element-type 'uint31))
      |                                          ^
a.cc:68:42: error: missing terminating ' character
   68 |          (as (make-array n :element-type 'uint31))
      |                                          ^~~~~~~~~
a.cc:69:42: warning: missing terminating ' character
   69 |          (bs (make-array n :element-type 'uint31))
      |                                          ^
a.cc:69:42: error: missing terminating ' character
   69 |          (bs (make-array n :element-type 'uint31))
      |                                          ^~~~~~~~~
a.cc:70:46: warning: missing terminating ' character
   70 |          (dp (make-array 10001 :element-type 'uint62)))
      |                                              ^
a.cc:70:46: error: missing terminating ' character
   70 |          (dp (make-array 10001 :element-type 'uint62)))
      |                                              ^~~~~~~~~~
a.cc:86:2: error: invalid preprocessing directive #-
   86 | #-swank (main)
      |  ^
a.cc:92:2: error: invalid preprocessing directive #+
   92 | #+swank
      |  ^
a.cc:93:54: error: stray '#' in program
   93 | (defun io-equal (in-string out-string &key (function #'main) (test #'equal))
      |                                                      ^
a.cc:93:55: warning: multi-character literal with 13 characters exceeds 'int' size of 4 bytes
   93 | (defun io-equal (in-string out-string &key (function #'main) (test #'equal))
      |                                                       ^~~~~~~~~~~~~~~~~~~~
a.cc:94:3: warning: missing terminating " character
   94 |   "Passes IN-STRING to *STANDARD-INPUT*, executes FUNCTION, and returns true if
      |   ^
a.cc:94:3: error: missing terminating " character
   94 |   "Passes IN-STRING to *STANDARD-INPUT*, executes FUNCTION, and returns true if
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:95:63: warning: missing terminating " character
   95 | the string output to *STANDARD-OUTPUT* is equal to OUT-STRING."
      |                                                               ^
a.cc:95:63: error: missing terminating " character
a.cc:97:42: error: stray '#' in program
   97 |              (if (eql (uiop:last-char s) #\Linefeed)
      |                                          ^
a.cc:97:43: error: stray '\' in program
   97 |              (if (eql (uiop:last-char s) #\Linefeed)
      |                                           ^
a.cc:107:2: error: invalid preprocessing directive #+
  107 | #+swank
      |  ^
a.cc:110:35: warning: missing terminating ' character
  110 |     (run-program "powershell.exe" '("-Command" "Get-Clipboard") :output out :search t)))
      |                                   ^
a.cc:110:35: error: missing terminating ' character
  110 |     (run-program "powershell.exe" '("-Command" "Get-Clipboard") :output out :search t)))
      |                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:112:2: error: invalid preprocessing directive #+
  112 | #+swank (defparameter *this-pathname* (uiop:current-lisp-file-pathname))
      |  ^
a.cc:113:2: error: invalid preprocessing directive #+
  113 | #+swank (defparameter *dat-pathname* (uiop:merge-pathnames* "test.dat" *this-pathname*))
      |  ^
a.cc:115:2: error: invalid preprocessing directive #+
  115 | #+swank
      |  ^
a.cc:117:3: warning: missing terminating " character
  117