program atcoder
    implicit none
    integer(8) :: i,j,n,maxw
    integer(8),allocatable,dimension(:,:) :: dp
    integer(8),allocatable,dimension(:) :: v,w
    read*,n,maxw
    allocate(dp(0:n,0:maxw))
    allocate(v(n))
    allocate(w(n))

    dp = 0
    do i = 1,n
        read*,w(i),v(i)
    enddo

    do i = 0,n-1
        do j = 0,maxw
            if( j<w(i+1) )then
                dp(i+1,j) = dp(i,j)
            else
                dp(i+1,j) = max( dp(i,j) , dp(i,j-w(i+1))+v(i+1) )
            endif
        enddo
    enddo
    write(*,'(I0)')dp(n,maxw)

    stop 
contains
end program atcoder
