(in-package :cl-user)
(eval-when (:compile-toplevel :load-toplevel :execute)
  (sb-int:defconstant-eqx opt
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0))
    #'equal)
  #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t)
  #-swank (set-dispatch-macro-character
           #\# #\> (lambda (s c p) (declare (ignore c p)) `(values ,(read s nil nil t)))))
#+swank (set-dispatch-macro-character #\# #\> #'cl-debug-print:debug-print-reader)

(macrolet ((def-int (b)
             `(progn (deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))
                     (deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))))
           (defs (&rest bits) `(progn ,@(mapcar (lambda (b) `(def-int ,b)) bits))))
  (defs 2 4 7 8 15 16 31 32 62 63 64))

(defconstant +mod+ 1000000007)

(defmacro dbg (&rest forms)
  #+swank (if (= (length forms) 1)
              `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
              `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

;; BEGIN_INSERTED_CONTENTS
(defpackage :cp/read-fixnum
  (:use :cl)
  (:export #:read-fixnum))
(in-package :cp/read-fixnum)

(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  "NOTE: cannot read -2^62"
  (macrolet ((%read-byte ()
               `(the (unsigned-byte 8)
                     #+swank (char-code (read-char in nil #\Nul))
                     #-swank (sb-impl::ansi-stream-read-byte in nil #.(char-code #\Nul) nil))))
    (let* ((minus nil)
           (result (loop (let ((byte (%read-byte)))
                           (cond ((<= 48 byte 57)
                                  (return (- byte 48)))
                                 ((zerop byte) ; #\Nul
                                  (error "Read EOF or #\Nul."))
                                 ((= byte #.(char-code #\-))
                                  (setq minus t)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte 57)
              (setq result (+ (- byte 48)
                              (* 10 (the (integer 0 #.(floor most-positive-fixnum 10))
                                         result))))
              (return (if minus (- result) result))))))))

;; BEGIN_USE_PACKAGE
(eval-when (:compile-toplevel :load-toplevel :execute)
  (use-package :cp/read-fixnum :cl-user))
(in-package :cl-user)

;;;
;;; Body
;;;

(defun main ()
  (let* ((n (read))
         (q (read))
         (as (make-array n :element-type 'uint31 :initial-element 0))
         (cumul (make-array (+ n 1) :element-type 'uint62 :initial-element 0))
         (cumul2 (make-array (+ n 2) :element-type 'uint62 :initial-element 0)))
    (dotimes (i n)
      (setf (aref as i) (read-fixnum)
            (aref cumul (+ i 1)) (aref as i)
            (aref cumul2 (+ i 1)) (aref as i)))
    (dotimes (i n)
      (incf (aref cumul2 (+ i 2)) (aref cumul2 i))
      (incf (aref cumul (+ i 1)) (aref cumul i)))
    (write-string
     (with-output-to-string (*standard-output* nil :element-type 'base-char)
       (dotimes (_ q)
         (let ((x (read-fixnum)))
           (labels ((calc (l r)
                      (declare (int32 l r))
                      (dbg l r)
                      ;; :large: width is too large
                      ;; :small: width is too small
                      ;; :valid
                      (when (or (< l 0)
                                (and (< r n)
                                     (> (abs (- (aref as l) x))
                                        (abs (- (aref as r) x)))))
                        (return-from calc :large))
                      (when (and (> l 0)
                                 (<= (abs (- (aref as (- l 1)) x))
                                     (abs (- (aref as (- r 1)) x))))
                        (return-from calc :small))
                      :valid))
             (println
              (sb-int:named-let bisect ((small 0) (large n))
                (declare (uint31 small large))
                (when (and (= small 0) (= large 1))
                  (return-from bisect
                    (aref cumul2 n)))
                (let* ((width (ash (+ small large) -1))
                       (res (calc (- n width width) (- n width)))
                       (res-1 (calc (min (- n width) (+ 1 (- n width width)))
                                    (- n width))))
                  (cond ((or (eql :valid res)
                             (eql :valid res-1))
                         (dbg res res-1)
                         (+ (- (aref cumul n) (aref cumul (- n width)))
                            (if (>= (- n width width) 0)
                                (aref cumul2 (- n width width))
                                0)))
                        ((eql res :large)
                         (bisect small width))
                        ((eql res :small)
                         (bisect width large)))))))))))))

#-swank (main)

;;;
;;; Test and benchmark
;;;

#+swank
(defun get-clipbrd ()
  (with-output-to-string (out)
    #+os-windows (run-program "powershell.exe" '("-Command" "Get-Clipboard") :output out :search t)
    #+os-unix (run-program "xsel" '("-b" "-o") :output out :search t)))

#+swank (defparameter *this-pathname* (uiop:current-lisp-file-pathname))
#+swank (defparameter *dat-pathname* (uiop:merge-pathnames* "test.dat" *this-pathname*))

#+swank
(defun run (&optional thing (out *standard-output*))
  "THING := null | string | symbol | pathname

null: run #'MAIN using the text on clipboard as input.
string: run #'MAIN using the string as input.
symbol: alias of FIVEAM:RUN!.
pathname: run #'MAIN using the text file as input."
  (let* ((*standard-output* (or out (make-string-output-stream)))
         (res (etypecase thing
                (null
                 (with-input-from-string (*standard-input* (delete #\Return (get-clipbrd)))
                   (main)))
                (string
                 (with-input-from-string (*standard-input* (delete #\Return thing))
                   (main)))
                (symbol (5am:run! thing))
                (pathname
                 (with-open-file (*standard-input* thing)
                   (main))))))
    (if out res (get-output-stream-string *standard-output*))))

#+swank
(defun gen-dat ()
  (uiop:with-output-file (out *dat-pathname* :if-exists :supersede)
    (format out "")))

#+swank
(defun bench (&optional (out (make-broadcast-stream)))
  (time (run *dat-pathname* out)))

#-swank
(eval-when (:compile-toplevel)
  (when (and (boundp 'sb-c::*compiler-warning-count*)
             (> sb-c::*compiler-warning-count* 0))
    (sb-ext:quit :unix-status 1)))

;; To run: (5am:run! :sample)
#+swank
(it.bese.fiveam:test :sample
  (5am:is
   (equal "31
31
27
23
23
"
          (run "5 5
3 5 7 11 13
1
4
9
10
13
" nil)))
  (5am:is
   (equal "70
60
60
"
          (run "4 3
10 20 30 40
2
34
34
" nil))))


Measured memory_bytes: 30412