(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
(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  (declare #.OPT)
  (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
                                  ;; (return-from read-fixnum 0)
                                  (error "Read EOF or #\Nul."))
                                 ((= 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))))))))

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

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

;; Body

;; dp[i][j][k] = Maximal value for k items in {0, ..., i-1} with total weight <= j
;; Let w'_i be the weight of the item i offset by w_0 (=: w').

;; dp[i+1][j][k] max=  dp[i][j][k]
;; dp[i+1][j+w_i][k+1] max= dp[i][j][k]+v_i
;; k<=i

;; When we choose k items, the possible upper limit of the total weight is
;; W-k*w'. However, we have only to look up the weight <= Σw'_i:
;; i.e. min(W-k*w', Σw_i). Therefore the range of the weight in the DP table
;; needs to be at most 300.
(defun main ()
  (let* ((n (read))
         (w (read))
         (ws (make-array n :element-type 'uint31 :initial-element 0))
         (vs (make-array n :element-type 'uint31))
         (w-offset 0))
    (declare ((integer 0 100) n)
             (uint31 w w-offset))
    (dotimes (i n)
      (if (zerop i)
          (setf w-offset (read-fixnum))
          (setf (aref ws i) (- (read-fixnum) w-offset)))
      (setf (aref vs i) (read-fixnum)))
    (let* ((virtual-w (reduce #'+ ws))
           (dp (make-array (list (1+ n) (1+ virtual-w) (1+ n))
                           :element-type 'uint62
                           :initial-element 0)))
      (declare (uint31 virtual-w))
      (dotimes (i n)
        (dotimes (j (1+ virtual-w))
          (dotimes (k (1+ i))
            (setf (aref dp (+ i 1) j k)
                  (max (aref dp (+ i 1) j k) (aref dp i j k)))
            (let ((next-w (+ j (aref ws i))))
              (when (<= next-w virtual-w)
                (setf (aref dp (+ i 1) next-w (+ k 1) )
                      (max (aref dp (+ i 1) next-w (+ k 1))
                           (+ (aref dp i j k) (aref vs i)))))))))
      (println
       (loop for k to n
             for possible-weight = (min virtual-w (- w (* k w-offset)))
             while (>= possible-weight 0)
             maximize (aref dp n possible-weight k))))))

#-swank(main)


Measured memory_bytes: 27368