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

(declaim (inline split-ints-into-vector))
(defun split-ints-into-vector (string dest-vector &key (offset 0))
  (declare (string string)
           ((simple-array * (*)) dest-vector)
           ((integer 0 #.most-positive-fixnum) offset))
  (loop for idx from offset below (length dest-vector)
        for pos1 = 0 then (1+ pos2)
        for pos2 = (position #\space string :start pos1 :test #'char=)
        do (setf (aref dest-vector idx)
                 (parse-integer string :start pos1 :end pos2))
        finally (return dest-vector)))

(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 (optimize (speed 3)))
  (let* ((n (read))
         (as (make-array n :element-type 'uint32 :initial-element 0))
         (bs (make-array n :element-type 'uint32 :initial-element 0))
         (deltas (make-array n :element-type 'fixnum :initial-element 0)))
    (split-ints-into-vector (read-line) as)
    (split-ints-into-vector (read-line) bs)
    (dotimes (i n)
      (setf (aref deltas i) (- (aref as i) (aref bs i))))
    (setf deltas (sort deltas #'>))
    (if (< (reduce #'+ deltas) 0)
        (println -1)
        (let ((saved-deltas (copy-seq deltas)))
          (nlet recurse ((pos 0) (neg (- n 1)))
            (when (and (< pos neg)
                       (> (aref deltas pos) 0)
                       (< (aref deltas neg) 0))
              (cond ((> (aref deltas pos) (abs (aref deltas neg)))
                     (incf (aref deltas pos) (aref deltas neg))
                     (setf (aref deltas neg) 0)
                     (recurse pos (- neg 1)))
                    ((= (aref deltas pos) (abs (aref deltas neg)))
                     (setf (aref deltas pos) 0
                           (aref deltas neg) 0)
                     (recurse (+ pos 1) (- neg 1)))
                    (t (incf (aref deltas neg) (aref deltas pos))
                       (setf (aref deltas pos) 0)
                       (recurse (+ pos 1) neg)))))
          (println (loop for d1 across deltas
                         for d2 across saved-deltas
                         count (/= d1 d2)))))))

#-swank(main)


Measured memory_bytes: 63076