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

(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) (key #'identity))
  (declare (string string)
           (function key)
           ((array * (*)) dest-vector)
           ((integer 0 #.most-positive-fixnum) offset))
  (loop with position = 0
        for idx from offset below (length dest-vector)
        do (setf (values (aref dest-vector idx) position)
                 (parse-integer string :start position :junk-allowed t))
           (setf (aref dest-vector idx) (funcall key (aref dest-vector idx)))
        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))))

(defconstant +mod+ 1000000007)

;; Hauptteil
(defun integer-to-bits (integer)
  (labels ((integer-to-list (int &optional accum)
             (cond ((> int 0)
                    (multiple-value-bind (i r) (truncate int 2)
                      (integer-to-list i (push r accum))))
                   ((null accum) (push 0 accum))
                   (t accum))))
    (coerce (integer-to-list integer) 'bit-vector)))

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (k (read))
         (as (make-array n :element-type 'uint62))
         (max-digit 40) ; 39-th ... 0-th
         (zeros (make-array max-digit :element-type 'uint32)))
    (declare ((integer 0 #.(expt 10 5)) n)
             ((integer 0 #.(expt 10 12)) k))
    (split-ints-into-vector (read-line) as)
    (dotimes (pos max-digit)
      (loop for i below n
            count (= 0 (ldb (byte 1 pos) (aref as i))) into c
            finally (setf (aref zeros pos) c)))
    (println
     (nlet recurse ((pos (- max-digit 1)) (limited t))
       (declare (values uint62))
       (if (= -1 pos)
           0
           (let ((c (aref zeros pos)))
             (declare ((integer 0 #.(expt 10 5)) c))
             (if limited
                 (if (zerop (ldb (byte 1 pos) k))
                     (+ (* (- n c) (expt 2 pos))
                        (recurse (- pos 1) t))
                     (max (+ (* (- n c) (expt 2 pos))
                             (recurse (- pos 1) nil))
                          (+ (* c (expt 2 pos))
                             (recurse (- pos 1) t))))
                 (if (> c (- n c))
                     ;; choose 1
                     (+ (* c (expt 2 pos))
                        (recurse (- pos 1) nil))
                     (+ (* (- n c) (expt 2 pos))
                        (recurse (- pos 1) nil))))))))))

#-swank(main)


Measured memory_bytes: 34792