!
! The below module was made in reference to 
! https://ufcpp.net/study/algorithm/col_heap.html
!
module heappq
  
  implicit none
  integer(8),allocatable :: hp(:)

contains 
subroutine heapinit(n)
  
  integer, intent(in) :: n

  allocate( hp(n) ); hp = 0

end subroutine heapinit

subroutine heappush( newnp, newn )
  
  integer, intent(in)    :: newn
  integer, intent(in)    :: newnp
  integer :: i,ln, tmp
 
  ln = newn
  hp(ln) = newnp
  
  do while ( ln /= 1 )
    i = ln/2
    if ( hp(i) < hp(ln) ) then
      tmp    = hp(ln) 
      hp(ln) = hp(i) 
      hp(i)  = tmp 
    end if
    ln = i
  end do

end subroutine heappush

subroutine heappop( n )
  
  integer :: i,j,tmp
  integer,intent(in) :: n
  
  hp(1) = hp(n)
  
  i = 1 
  do while ( 2*i < n )
    j = 2*i 
    if ( j /= n - 1 .and. hp(j+1) > hp(j) ) j = j + 1
    if ( hp(i) < hp(j) ) then
      tmp    = hp(j) 
      hp(j) = hp(i) 
      hp(i)  = tmp 
    end if
    i = j
  end do

end subroutine heappop
 
end module heappq

program main
  
  use heappq  

  implicit none ! 02;01
  integer :: n, m
  integer, allocatable :: a(:)
  integer :: i,j, tmp, tmpo
  integer(8) :: money
  
  read(*,*) n,m
  allocate( a(n) )
  read(*,*) a
  
  call heapinit(n)
  
  do i = 1, n
    call heappush( a(i), i )
  end do

  do j = 1, m
    tmp = hp(1) / 2 
    call heappop(n)

    call heappush(tmp, n)

  end do

  a = hp 
  money = 0
  do i = 1, n
    money = money + int( a(i), kind = 8 )
  end do
  print*, money

end program main

