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

(defun heap-peak (heap &optional (error t) null-value)
  (if (= 1 (heap-next-position heap))
      (if error
          (error "No element in heap")
          null-value)
      (aref (heap-data heap) 1)))

;; For test
;; (eval-when (:compile-toplevel :load-toplevel :execute)
;;   (ql:quickload :fiveam)
;;   (use-package :fiveam))

;; (test heap-test
;;   (let ((h (make-heap 20)))
;;     (finishes (dolist (o (list 7 18 22 15 27 9 11))
;;                 (heap-push o h)))
;;     (is (= 7 (heap-peak h)))
;;     (is (equal '(7 9 11 15 18 22 27)
;;                (loop repeat 7 collect (heap-pop h))))
;;     (signals error (heap-pop h))
;;     (is (eql 'eof (heap-pop h nil 'eof)))
;;     (is (eql 'eof (heap-peak h nil 'eof))))
;;   (is (typep (heap-data (make-heap 10 :element-type 'fixnum))
;;              '(simple-array fixnum (*)))))

;; (run! 'heap-test)

(defun bench (&optional (size 2000000))
  (declare (optimize (speed 3)))
  (let* ((heap (make-heap size :element-type 'fixnum))
         (seed (seed-random-state 0)))
    (time (dotimes (i size)
            (heap-push (random most-positive-fixnum seed) heap)))
    (time (dotimes (i size)
            (heap-pop heap)))))

(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 ()
  (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))
         (yen-table (make-array n :element-type 'fixnum :initial-element most-positive-fixnum))
         (snook-table (make-array n :element-type 'fixnum :initial-element most-positive-fixnum)))
    (dotimes (i m)
      (split-ints-and-bind (u v a b) (buffered-read-line 50)
        (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)))))
    (let ((pqueue (make-heap 200000 :test (lambda (p1 p2) (< (cdr p1) (cdr p2)))
                                    :element-type '(cons uint32 fixnum)))
          (visited (make-array n :element-type 'boolean :initial-element nil)))
      (heap-push (cons src 0) pqueue)
      (loop for (current . cost) = (heap-pop pqueue nil '(-1 . -1))
            until (= current -1)
            do (unless (aref visited current)
                 (setf (aref visited current) t)
                 (when (< cost (aref yen-table current))
                   (setf (aref yen-table current) cost))
                 (dolist (neighbor (aref graph-yen current))
                   (heap-push (cons (car neighbor) (+ cost (cdr neighbor))) pqueue)))))
    (let ((pqueue (make-heap 200000 :test (lambda (p1 p2) (< (cdr p1) (cdr p2)))
                                    :element-type '(cons uint32 fixnum)))
          (visited (make-array n :element-type 'boolean :initial-element nil)))
      (heap-push (cons dest 0) pqueue)
      (loop for (current . cost) = (heap-pop pqueue nil '(-1 . -1))
            until (= current -1)
            do (unless (aref visited current)
                 (setf (aref visited current) t)
                 (when (< cost (aref snook-table current))
                   (setf (aref snook-table current) cost))
                 (dolist (neighbor (aref graph-snook current))
                   (heap-push (cons (car neighbor) (+ cost (cdr neighbor))) pqueue)))))
    (let ((hub-to-cost (make-array n :element-type 'fixnum))
          (year-to-cost (make-array n :element-type 'fixnum)))
      (dotimes (i n)
        (setf (aref hub-to-cost i)
              (- #.(expt 10 15) (+ (aref yen-table i) (aref snook-table 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)
        (println (aref year-to-cost y))))))

#-swank(main)


Measured memory_bytes: 79072