program main
  implicit none
  integer :: n, x, a(100), i, j
  read(*, *) n, x
  read(*, *) a(1:n)

  call mergesort(a(1:n), n)

  if (sum(a(1:n)) == x) then
    write(*, "(i0)") n
  else if (sum(a(1:n)) < x) then
    write(*, "(i0)") n - 1
  else
    j = 0
    do i = 1, n
      j = j + a(i)
      if (j > x) then
        write(*, "(i0)") i - 1
        exit
      end if
    end do
  end if
  contains

  subroutine merge_arrays(a, na, b, nb, c, nc)
    ! Merge arrays a and b to c
    implicit none
    integer, intent(in) :: na, nb, nc
    integer, intent(in) :: a(na), b(nb)
    integer, intent(inout) :: c(nc)
    integer :: i, j, k

    if (na + nb /= nc) then
      print *, "subroutine merge_arrays size error"
      stop 161
    end if

    i = 1; j = 1; k = 1
    do while(i <= na .and. j <= nb)
      if (a(i) <= b(j)) then
        c(k) = a(i)
        i = i + 1
      else
        c(k) = b(j)
        j = j + 1
      endif
      k = k + 1
    enddo
    do while (i <= na)
      c(k) = a(i)
      i = i + 1
      k = k + 1
    enddo
  end subroutine merge_arrays

  recursive subroutine mergesort(a, n)
    ! Sort an array a(n)
    implicit none
    integer, intent(in) :: n
    integer, intent(inout) :: a(n)
    integer :: t((n + 1) / 2)
    integer :: na, v

    if (n < 2) then
      return
    else if (n == 2) then
      if (a(1) > a(2)) then
        v = a(1)
        a(1) = a(2)
        a(2) = v
      endif
      return
    endif

    na = (n + 1) / 2
    call mergesort(a, na)
    call mergesort(a(na + 1), n - na)

    if (a(na) > a(na + 1)) then
      t(1:na) = a(1:na)
      call merge_arrays(t, na, a(na + 1:n), n - na, a, n)
    endif
  end subroutine mergesort
end program main
