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

(defstruct (heap (:constructor make-heap
                            (size
                             &key test (element-type t)
                             &aux (data (make-array (1+ size) :element-type element-type)))))
  (data #() :type (simple-array list (*)) :read-only t)
  (test #'< :type function :read-only t)
  (next-position 1 :type (integer 1 #.most-positive-fixnum)))

(declaim (inline heap-push))
(defun heap-push (obj heap)
  (symbol-macrolet ((data (heap-data heap))
                    (test (heap-test heap))
                    (next-position (heap-next-position heap)))
    (labels ((update (pos)
               (unless (= pos 1)
                 (let ((parent-pos (floor pos 2)))
                   (when (funcall test (aref data pos) (aref data parent-pos))
                     (rotatef (aref data pos) (aref data parent-pos))
                     (update parent-pos))))))
      (setf (aref data next-position) obj)
      (update next-position)
      (incf next-position)
      heap)))

(declaim (inline heap-pop))
(defun heap-pop (heap &optional (error t) null-value)
  (symbol-macrolet ((data (heap-data heap))
                    (test (heap-test heap))
                    (next-position (heap-next-position heap)))
    (labels ((update (pos)
               (declare ((integer 1 #.most-positive-fixnum) pos))
               (let* ((child-pos1 (+ pos pos))
                      (child-pos2 (1+ child-pos1)))
                 (when (<= child-pos1 next-position)
                   (if (<= child-pos2 next-position)
                       (if (funcall test (aref data child-pos1) (aref data child-pos2))
                           (unless (funcall test (aref data pos) (aref data child-pos1))
                             (rotatef (aref data pos) (aref data child-pos1))
                             (update child-pos1))
                           (unless (funcall test (aref data pos) (aref data child-pos2))
                             (rotatef (aref data pos) (aref data child-pos2))
                             (update child-pos2)))
                       (unless (funcall test (aref data pos) (aref data child-pos1))
                         (rotatef (aref data pos) (aref data child-pos1))))))))
      (if (= next-position 1)
          (if error
              (error "No element in heap")
              null-value)
          (prog1 (aref data 1)
            (decf next-position)
            (setf (aref data 1) (aref data next-position))
            (update 1))))))

(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))
         (m (read))
         (src (- (read) 1))
         (dest (- (read) 1))
         ;; (to . cost)
         (graph-yen (make-array n :element-type 'list :initial-element nil))
         (graph-snook (make-array n :element-type 'list :initial-element nil))
         (table-yen (make-array n :element-type 'fixnum :initial-element most-positive-fixnum))
         (table-snook (make-array n :element-type 'fixnum :initial-element most-positive-fixnum)))
    (declare (uint32 n m src dest))
    (declare )
    (dotimes (i m)
      (split-ints-and-bind (u v a b) (buffered-read-line 50)
        (declare (uint32 u v a b))
        (push (cons (- v 1) a) (aref graph-yen (- u 1)))
        (push (cons (- u 1) a) (aref graph-yen (- v 1)))
        (push (cons (- v 1) b) (aref graph-snook (- u 1)))
        (push (cons (- u 1) b) (aref graph-snook (- v 1)))))
    (labels ((process (city table graph)
               (let ((pqueue (make-heap 200000
                                        :test (lambda (p1 p2) (< (the fixnum (cdr p1))
                                                                 (the fixnum (cdr p2))))
                                        :element-type '(cons uint32 fixnum)))
                     (visited (make-array n :element-type 'boolean :initial-element nil)))
                 (heap-push (cons city 0) pqueue)
                 (loop for (current . cost) of-type (fixnum . fixnum) = (heap-pop pqueue nil '(-1 . -1))
                       until (= current -1)
                       do (unless (aref visited current)
                            (setf (aref visited current) t)
                            (when (< cost (aref table current))
                              (setf (aref table current) cost))
                            (dolist (neighbor (aref graph current))
                              (heap-push (cons (car neighbor)
                                               (the fixnum (+ cost (the fixnum (cdr neighbor)))))
                                         pqueue)))))))
      (process src table-yen graph-yen)
      (process dest table-snook graph-snook)
      (let ((hub-to-cost (make-array n :element-type 'fixnum))
            (year-to-cost (make-array n :element-type 'fixnum))
            (out (make-string-output-stream :element-type 'base-char)))
        (dotimes (i n)
          (setf (aref hub-to-cost i)
                (- #.(expt 10 15) (the fixnum (+ (aref table-yen i) (aref table-snook i))))))
        (loop for y from (- n 1) downto 0 ; (i-1)-th exchange is closed after i years.
                                        ; i is opened.
              maximize (aref hub-to-cost y) into current-max
              do (setf (aref year-to-cost y) current-max))
        (dotimes (y n (write-string (get-output-stream-string out)))
          (println (aref year-to-cost y) out))))))

#-swank(main)


Measured memory_bytes: 73576