Problem:
Score : 400 points

Problem Statement

You have N items and a bag of strength W.
The i-th item has a weight of w_i and a value of v_i.

You will select some of the items and put them in the bag.
Here, the total weight of the selected items needs to be at most W.

Your objective is to maximize the total value of the selected items.

Constraints

1 ≤ N ≤ 100

1 ≤ W ≤ 10^9

1 ≤ w_i ≤ 10^9

For each i = 2,3,...,N, w_1 ≤ w_i ≤ w_1 + 3.

1 ≤ v_i ≤ 10^7

W, each w_i and v_i are integers.

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible total value of the selected items.

Sample Input 1

4 6
2 1
3 4
4 10
3 4

Sample Output 1

11

The first and third items should be selected.

Sample Input 2

4 6
2 1
3 7
4 10
3 6

Sample Output 2

13

The second and fourth items should be selected.

Sample Input 3

4 10
1 100
1 100
1 100
1 100

Sample Output 3

400

You can take everything.

Sample Input 4

4 1
10 100
10 100
10 100
10 100

Sample Output 4

0

You can take nothing.

Solution in Common Lisp (SBCL 1.1.14) (judge verdict: Accepted)

(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 ()
  (declare #.OPT)
  (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)