Language: C++

(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
                 (shadow :run)
                 (use-package :fiveam)))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(defmacro nlet (name args &body body)
  (labels ((ensure-list (x) (if (listp x) x (list x))))
    (let ((args (mapcar #'ensure-list args)))
      `(labels ((,name ,(mapcar #'car args) ,@body))
         (,name ,@(mapcar #'cadr args))))))

;; (with-memoizing (:hash-table :test #'equal :key #'cons)
;;   (defun ...))
;; (with-memoizing (:array (10 10 * 10) :initial-element -1 :element-type 'fixnum)
;;   (defun ...))
(defmacro with-memoizing (cache-attribs def-form)
  (let* ((cache-attribs (if (atom cache-attribs) (list cache-attribs) cache-attribs))
         (cache-type (first cache-attribs))
         (dimensions-with-* (when (eql cache-type :array) (second cache-attribs)))
         (dimensions (remove '* dimensions-with-*))
         (rank (length dimensions))
         (rest-attribs (ecase cache-type
                         (:hash-table (cdr cache-attribs))
                         (:array (cddr cache-attribs))))
         (key (prog1 (getf rest-attribs :key) (remf rest-attribs :key)))
         (cache-form (case cache-type
                       (:hash-table `(make-hash-table ,@rest-attribs))
                       (:array `(make-array ',dimensions ,@rest-attribs))))
         (initial-element (when (eql cache-type :array)
                            (assert (member :initial-element rest-attribs))
                            (getf rest-attribs :initial-element))))
    (let ((cache (gensym))
          (value (gensym))
	  (present-p (gensym))
          (name-alias (gensym))
	  (args-lst (gensym))
          (indices (loop repeat rank collect (gensym))))
      (labels ((make-cache-check-form (cache-type args)
                 (case cache-type
                   (:hash-table
                    `(let ((,args-lst (funcall ,(or key #'list) ,@args)))
                       (multiple-value-bind (,value ,present-p)
                           (gethash ,args-lst ,cache)
                         (if ,present-p
                             ,value
                             (setf (gethash ,args-lst ,cache)
                                   (,name-alias ,@args))))))
                   (:array
                    (let ((memoized-args (loop for dimension in dimensions-with-*
                                               for arg in args
                                               unless (eql dimension '*)
                                               collect arg)))
                      (if key
                          `(multiple-value-bind ,indices
                               (funcall ,key ,@memoized-args)
                             (let ((,value (aref ,cache ,@indices)))
                               (if (eql ,initial-element ,value)
                                   (setf (aref ,cache ,@indices)
                                         (,name-alias ,@args))
                                   ,value)))
                          `(let ((,value (aref ,cache ,@memoized-args)))
                             (if (eql ,initial-element ,value)
                                 (setf (aref ,cache ,@memoized-args)
                                       (,name-alias ,@args))
                                 ,value)))))))
               (make-reset-form (cache-type)
                 (case cache-type
                   (:hash-table `(setf ,cache (make-hash-table ,@rest-attribs)))
                   (:array `(prog1 nil
                              (fill (array-storage-vector ,cache) ,initial-element)))))
               (make-reset-name (name)
                 (intern (format nil "RESET-~A" (symbol-name name))))
               (extract-declarations (body)
                 (remove-if-not (lambda (form) (eql 'declare (car form))) body)))
        (ecase (car def-form)
          ((defun)
           (destructuring-bind (_ name args &body body) def-form
             (declare (ignore _))
             `(let ((,cache ,cache-form))
                (defun ,(make-reset-name name) () ,(make-reset-form cache-type))
                (defun ,name ,args
                  (labels ((,name-alias ,args ,@body))
                    (declare (inline ,name-alias))
                    ,@(extract-declarations body)
                    ,(make-cache-check-form cache-type args))))))
          ((nlet)
           (destructuring-bind (_ name bindings &body body) def-form
             (declare (ignore _))
             `(let ((,cache ,cache-form))
                (nlet ,name ,bindings
                      ,(let ((args (mapcar (lambda (x) (if (atom x) x (car x))) bindings)))
                         `(labels ((,name-alias ,args ,@body))
                            (declare (inline ,name-alias))
                            ,@(extract-declarations body)
                            ,(make-cache-check-form cache-type args)))))))
          ((labels flet)
           (destructuring-bind (_ definitions &body labels-body) def-form
             (declare (ignore _))
             (destructuring-bind (name args &body body) (car definitions)
               `(let ((,cache ,cache-form))
                  (,(car def-form)
                   ((,(make-reset-name name) () ,(make-reset-form cache-type))
                    (,name ,args
                           (labels ((,name-alias ,args ,@body))
                             (declare (inline ,name-alias))
                             ,@(extract-declarations body)
                             ,(make-cache-check-form cache-type args)))
                    ,@(cdr definitions))
                   (declare (ignorable #',(make-reset-name name)))
                   ,@labels-body))))))))))

;; (test with-memoizing
;;   (finishes (macroexpand `(with-memoizing (:hash-table :test #'equal)
;;                             (defun add (x y) (+ x y)))))
;;   (finishes (macroexpand `(with-memoizing (:array '(10 10)
;;                                            :element-type 'fixnum
;;                                            :initial-element -1)
;;                             (defun add (x y) (+ x y)))))
;;   (finishes (macroexpand `(with-memoizing (:array '(10 10)
;;                                            :element-type 'fixnum
;;                                            :initial-element -1)
;;                             (labels ((add (x y) (+ x y))
;; 		                     (my-print (x) (print x)))
;; 	                      (add 1 2))))))

(defmacro buffered-read-line (&optional (buffer-size 30) (in '*standard-input*) (terminate-char #\Space))
  "Note that the returned string will be reused."
  (let ((buffer (gensym))
        (character (gensym))
        (idx (gensym)))
    `(let ((,buffer (load-time-value (make-string ,buffer-size
                                                  :element-type 'base-char))))
       (declare (simple-base-string ,buffer))
       (loop for ,character of-type base-char =
                #-swank (code-char (read-byte ,in nil #.(char-code #\Newline)))
                #+swank (read-char ,in nil #\Newline)
             for ,idx from 0
             until (char= ,character #\Newline)
             do (setf (schar ,buffer ,idx) ,character)
             finally (setf (schar ,buffer ,idx) ,terminate-char)
                     (return (values ,buffer ,idx))))))

(defmacro split-ints-and-bind (vars string &body body)
  (let ((pos1 (gensym "POS"))
	(pos2 (gensym "POS"))
	(str (gensym "STR")))
    (labels ((expand (vars &optional (init-pos1 t))
	       (if (null vars)
		   body
		   `((let* ((,pos1 ,(if init-pos1 0 `(1+ ,pos2)))
			    (,pos2 (position #\space ,str :start ,pos1 :test #'char=))
			    (,(car vars) (parse-integer ,str :start ,pos1 :end ,pos2)))
		       ,@(expand (cdr vars) nil))))))
      `(let ((,str ,string))
         (declare (string ,str))
	 ,@(expand vars)))))

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

(defmacro println (obj &optional (stream '*standard-output*))
  `(let ((*read-default-float-format* 'double-float))
     (prog1 (princ ,obj ,stream) (terpri ,stream))))

(defconstant +mod+ 1000000007)

;; Hauptteil

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (graph (make-array n :element-type 'list :initial-element nil))
         (tree (make-array n :element-type 'list :initial-element nil)))
    (dotimes (i (- n 1))
      (split-ints-and-bind (a b c) (buffered-read-line)
        (declare (uint32 a b c))
        (push (cons (- b 1) c) (aref graph (- a 1)))
        (push (cons (- a 1) c) (aref graph (- b 1)))))
    (let ((q (read))
          (k (- (read) 1)))
      (declare (uint32 k q))
      (nlet recurse ((i k) (prev -1))
        (declare (fixnum i prev))
        (loop for (next . cost) of-type (uint32 . uint32) in (aref graph i)
              do (unless (= next prev)
                   (push (cons i cost) (aref tree next))
                   (recurse next i))))
      (with-memoizing (:array (100001) :element-type 'fixnum :initial-element -1)
        (labels ((cost-k (i)
                   (cond ((= i k) 0)
                         ((null (aref tree i)) #.(expt 10 15))
                         (t (loop for (next . cost) of-type (uint32 . uint32) in (aref tree i)
                                  minimize (+ cost (cost-k next))
                                  of-type fixnum)))))
          (dotimes (i q)
            (split-ints-and-bind (x y) (buffered-read-line 20)
              (declare (uint32 x y))
              (println (the fixnum (+ (cost-k (- x 1))
                                      (cost-k (- y 1))))))))))))

#-swank(main)


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:4: error: invalid preprocessing directive #+
    5 |   #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
      |    ^
a.cc:5:32: warning: missing terminating ' character
    5 |   #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
      |                                ^
a.cc:8:2: error: invalid preprocessing directive #+
    8 | #+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)
      |  ^
a.cc:13:25: error: stray '#' in program
   13 |     (let ((args (mapcar #'ensure-list args)))
      |                         ^
a.cc:13:26: warning: missing terminating ' character
   13 |     (let ((args (mapcar #'ensure-list args)))
      |                          ^
a.cc:13:26: error: missing terminating ' character
   13 |     (let ((args (mapcar #'ensure-list args)))
      |                          ^~~~~~~~~~~~~~~~~~~~
a.cc:14:7: error: stray '`' in program
   14 |       `(labels ((,name ,(mapcar #'car args) ,@body))
      |       ^
a.cc:14:33: error: stray '#' in program
   14 |       `(labels ((,name ,(mapcar #'car args) ,@body))
      |                                 ^
a.cc:14:34: warning: missing terminating ' character
   14 |       `(labels ((,name ,(mapcar #'car args) ,@body))
      |                                  ^
a.cc:14:34: error: missing terminating ' character
   14 |       `(labels ((,name ,(mapcar #'car args) ,@body))
      |                                  ^~~~~~~~~~~~~~~~~~~
a.cc:15:18: error: stray '@' in program
   15 |          (,name ,@(mapcar #'cadr args))))))
      |                  ^
a.cc:15:27: error: stray '#' in program
   15 |          (,name ,@(mapcar #'cadr args))))))
      |                           ^
a.cc:15:28: warning: missing terminating ' character
   15 |          (,name ,@(mapcar #'cadr args))))))
      |                            ^
a.cc:15:28: error: missing terminating ' character
   15 |          (,name ,@(mapcar #'cadr args))))))
      |                            ^~~~~~~~~~~~~~~~
a.cc:17:39: error: stray '#' in program
   17 | ;; (with-memoizing (:hash-table :test #'equal :key #'cons)
      |                                       ^
a.cc:17:40: warning: multi-character literal with 12 characters exceeds 'int' size of 4 bytes
   17 | ;; (with-memoizing (:hash-table :test #'equal :key #'cons)
      |                                        ^~~~~~~~~~~~~~~~~~
a.cc:19:75: warning: missing terminating ' character
   19 | ;; (with-memoizing (:array (10 10 * 10) :initial-element -1 :element-type 'fixnum)
      |                                                                           ^
a.cc:19:75: error: missing terminating ' character
   19 | ;; (with-memoizing (:array (10 10 * 10) :initial-element -1 :element-type 'fixnum)
      |                                                                           ^~~~~~~~
a.cc:25:30: warning: missing terminating ' character
   25 |          (dimensions (remove '* dimensions-with-*))
      |                              ^
a.cc:25:30: error: missing terminating ' character
   25 |          (dimensions (remove '* dimensions-with-*))
      |                              ^~~~~~~~~~~~~~~~~~~~~~
a.cc:32:37: error: stray '`' in program
   32 |                        (:hash-table `(make-hash-table ,@rest-attribs))
      |                                     ^
a.cc:32:56: error: stray '@' in program
   32 |                        (:hash-table `(make-hash-table ,@rest-attribs))
      |                                                        ^
a.cc:33:32: error: stray '`' in program
   33 |                        (:array `(make-array ',dimensions ,@rest-attribs))))
      |                                ^
a.cc:33:45: warning: missing terminating ' character
   33 |                        (:array `(make-array ',dimensions ,@rest-attribs))))
      |                                             ^
a.cc:33:45: error: missing terminating ' character
   33 |                        (:array `(make-array ',dimensions ,@rest-attribs))))
      |                                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:46:21: error: stray '`' in program
   46 |                     `(let ((,args-lst (funcall ,(or key #'list) ,@args)))
      |                     ^
a.cc:46:57: error: stray '#' in program
   46 |                     `(let ((,args-lst (funcall ,(or key #'list) ,@args)))
      |                                                         ^
a.cc:46:58: warning: missing terminating ' character
   46 |                     `(let ((,args-lst (funcall ,(or key #'list) ,@args)))
      |                                                          ^
a.cc:46:58: error: missing terminating ' character
   46 |                     `(let ((,args-lst (funcall ,(or key #'list) ,@args)))
      |                                                          ^~~~~~~~~~~~~~~~
a.cc:52:50: error: stray '@' in program
   52 |                                    (,name-alias ,@args))))))
      |                                                  ^
a.cc:56:70: warning: missing terminating ' character
   56 |                                                unless (eql dimension '*)
      |                                                                      ^
a.cc:56:70: error: missing terminating ' character
   56 |                                                unless (eql dimension '*)
      |                                                                      ^~~
a.cc:59:27: error: stray '`' in program
   59 |                           `(multiple-value-bind ,indices
      |                           ^
a.cc:60:47: error: stray '@' in program
   60 |                                (funcall ,key ,@memoized-args)
      |                                               ^
a.cc:61:58: error: stray '@' in program
   61 |                              (let ((,value (aref ,cache ,@indices)))
      |                                                          ^
a.cc:63:56: error: stray '@' in program
   63 |                                    (setf (aref ,cache ,@indices)
      |                                                        ^
a.cc:64:56: error: stray '@' in program
   64 |                                          (,name-alias ,@args))
      |                                                        ^
a.cc:66:27: error: stray '`' in program
   66 |                           `(let ((,value (aref ,cache ,@memoized-args)))
      |                           ^
a.cc:66:56: error: stray '@' in program
   66 |                           `(let ((,value (aref ,cache ,@memoized-args)))
      |                                                        ^
a.cc:68:54: error: stray '@' in program
   68 |                                  (setf (aref ,cache ,@memoized-args)
      |                                                      ^
a.cc:69:54: error: stray '@' in program
   69 |                                        (,name-alias ,@args))
      |                                                      ^
a.cc:73:33: error: stray '`' in program
   73 |                    (:hash-table `(setf ,cache (make-hash-table ,@rest-attribs)))
      |                                 ^
a.cc:73:65: error: stray '@' in program
   73 |                    (:hash-table `(setf ,cache (make-hash-table ,@rest-attribs)))
      |                                                                 ^
a.cc:74:28: error: stray '`' in program
   74 |                    (:array `(prog1 nil
      |                            ^
a.cc:79:53: warning: missing terminating ' character
   79 |                  (remove-if-not (lambda (form) (eql 'declare (car form))) body)))
      |                                                     ^
a.cc:79:53: error: missing terminating ' character
   79 |                  (remove-if-not (lambda (form) (eql 'declare (car form))) body)))
      |                                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:84:14: error: stray '`' in program
   84 |              `(let ((,cache ,cache-form))
      |              ^
a.cc:87:48: error: stray '@' in program
   87 |                   (labels ((,name-alias ,args ,@body))
      |                                                ^
a.cc:89:22: error: stray '@' in program
   89 |                     ,@(extract-declarations body)
      |                      ^
a.cc:94:14: error: stray '`' in program
   94 |              `(let ((,cache ,cache-form))
      |              ^
a.cc:97:26: error: stray '`' in program
   97 |                          `(labels ((,name-alias ,args ,@body))
      |                          ^
a.cc:97:56: error: stray '@' in program
   97 |                          `(labels ((,name-alias ,args ,@body))
      |                                                        ^
a.cc:99:30: error: stray '@' in program
   99 |                             ,@(extract-declarations body)
      |                              ^
a.cc:105:16: error: stray '`' in program
  105 |                `(let ((,cache ,cache-form))
      |                ^
a.cc:109:57: error: stray '@' in program
  109 |                            (labels ((,name-alias ,args ,@body))
      |                                                         ^
a.cc:111:31: error: stray '@' in program
  111 |                              ,@(extract-declarations body)
      |                               ^
a.cc:113:22: error: stray '@' in program
  113 |                     ,@(cdr definitions))
      |                      ^
a.cc:114:40: error: stray '#' in program
  114 |                    (declare (ignorable #',(make-reset-name name)))
      |                                        ^
a.cc:114:41: warning: missing terminating ' character
  114 |                    (declare (ignorable #',(ma