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 Fortran (gfortran v4.8.4) (judge verdict: Accepted)

program ARC073D
    implicit none
    integer(8)::N,W,i,j,k,l,a,b,c,d,Vall(100,4),wi,w1,vi,vcount(4),ans,wall,v
    read*,N,W
    vcount=0
    Vall=0
    ans=0
    do i=1,N
        read*,wi,vi
        if(i==1)w1=wi
        vcount(wi-w1+1)=vcount(wi-w1+1)+1
        Vall(vcount(wi-w1+1),wi-w1+1)=vi
    end do

    do i=1,4
        call heapsort(100,Vall(:,i))
    end do

    do i=0,vcount(1)
        do j=0,vcount(2)
            do k=0,vcount(3)
                do l=0,vcount(4)
                    wall=i*w1+j*(w1+1)+k*(w1+2)+l*(w1+3)
                    if(wall<=W)then
                        v=0
                        do a=0,i-1
                            v=v+Vall(100-a,1)
                        end do

                        do b=0,j-1
                            v=v+Vall(100-b,2)
                        end do

                        do c=0,k-1
                            v=v+Vall(100-c,3)
                        end do

                        do d=0,l-1
                            v=v+Vall(100-d,4)
                        end do
                        ans=max(ans,v)
                    end if
                end do
            end do
        end do
    end do

    print'(i0)',ans
end program ARC073D

subroutine heapsort(n,array)
    implicit none
    integer,intent(in) :: n
    integer(8),intent(inout) :: array(1:n)
   
    integer(8) ::i,k,j,l
    integer(8) :: t
   
    if(n.eq.1)return
  
    l=n/2+1
    k=n
    do while(k.ne.1)
       if(l.gt.1)then
          l=l-1
          t=array(L)
       else
          t=array(k)
          array(k)=array(1)
          k=k-1
          if(k.eq.1) then
             array(1)=t
             exit
          endif
       endif
       i=l
       j=l+l
       do while(j.le.k)
          if(j.lt.k)then
             if(array(j).lt.array(j+1))j=j+1
          endif
          if (t.lt.array(j))then
             array(i)=array(j)
             i=j
             j=j+j
          else
             j=k+1
          endif
       enddo
       array(i)=t
    enddo
  
    return
  end subroutine heapsort