(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
(defun read-fixnum ()
  (declare #.OPT)
  (macrolet ((%read-byte ()
               `(the (unsigned-byte 8)
                     #+swank (char-code (read-char *standard-input* nil #\Nul))
                     #-swank (sb-impl::ansi-stream-read-byte *standard-input* nil #.(char-code #\Nul) nil))))
    (let* ((minus nil)
           (result (loop (let ((byte (%read-byte)))
                           (cond ((<= 48 byte 57)
                                  (return (- byte 48)))
                                 ((zerop byte) ; #\Nul
                                  (return-from read-fixnum 0))
                                 ((= 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) (the fixnum (* result 10))))
              (return (if minus (- result) result))))))))

(defstruct (heap (:constructor make-heap
                            (size
                             &key test
                             &aux (data (make-array (1+ size) :element-type '(unsigned-byte 32))))))
  (data #() :type (simple-array (unsigned-byte 32) (*)) :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 ((next-position (heap-next-position heap)))
    (let ((data (heap-data heap))
          (test (heap-test heap)))
      (labels ((update (pos)
                 (unless (= pos 1)
                   (let ((parent-pos (ash pos -1)))
                     (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 ((next-position (heap-next-position heap)))
    (let ((data (heap-data heap))
          (test (heap-test 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)))))))

(declaim (inline heap-reinitialize))
(defun heap-reinitialize (heap)
  (setf (heap-next-position heap) 1)
  heap)

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

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

;; Hauptteil

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (2n (+ n n))
         (as (make-array (* 3 n) :element-type 'uint32)))
    (declare (uint31 n))
    (dotimes (i (* 3 n)) (setf (aref as i) (read-fixnum)))
    (let ((sums1 (make-array (+ n 1) :element-type 'fixnum))
          (sums2 (make-array (+ n 1) :element-type 'fixnum))
          (pqueue1 (make-heap n :test #'<))
          (pqueue2 (make-heap n :test #'>)))
      (setf (aref sums1 0) (reduce #'+ as :end n))
      (setf (aref sums2 n) (reduce #'+ as :start 2n :from-end t))
      (dotimes (i n)
        (heap-push (aref as i) pqueue1)
        (heap-push (aref as (+ i 2n)) pqueue2))
      (loop for i from n below 2n
            do (setf (aref sums1 (+ (- i n) 1))
                     (aref sums1 (- i n)))
               (let ((delta (- (aref as i) (heap-peak pqueue1))))
                 (when (> delta 0)
                   (heap-pop pqueue1)
                   (heap-push (aref as i) pqueue1)
                   (incf (aref sums1 (+ (- i n) 1)) delta))))
      (loop for i from (- 2n 1) downto n
            do (setf (aref sums2 (- i n))
                     (aref sums2 (+ (- i n) 1)))
               (let ((delta (- (aref as i) (heap-peak pqueue2))))
                 (when (< delta 0)
                   (heap-pop pqueue2)
                   (heap-push (aref as i) pqueue2)
                   (incf (aref sums2 (- i n)) delta))))
      (println
       (loop for i to n
             maximize (- (aref sums1 i) (aref sums2 i)) of-type fixnum)))))

#-swank(main)


Measured memory_bytes: 37992