      module sortMod
        implicit none
        
        contains
        
        subroutine bSort( a ,kk )
          implicit none
          integer(16) :: a(:)
          integer(16) :: n,i,k,buffer,kk
          
          n = size(a)
          if( n==1 ) return
          if( kk==n ) kk = kk -1
          
          do i = 1,kk
            do k = i+1,n
              if( a(i)<a(k) )then
                buffer = a(i)
                a(i) = a(k)
                a(k) = buffer
              end if
            end do
          end do
        end subroutine
        
        impure recursive function qSort( arr ) result( after )
          !ref
          !https://t.doujin-constant.net/
          !post/143374686394/fortranをより使いこなしたquick-sort
          implicit none
          integer(16),intent(in) :: arr(:)
          !integer(16) :: after( size(arr) )
          integer(16),allocatable :: after(:)
          
          if( size(arr)>1 )then
            after = [ qSort( pack( arr(:),arr(:)> arr(1))), &
                      pack( arr(:),arr(:)==arr(1) ), &
                      qSort( pack( arr(:),arr(:)<arr(1))) ]
          else
            after = arr
          end if
        end function qSort
        
      end module
      
      PROGRAM piyo
      use sortMod
      IMPLICIT NONE
      integer(16) :: n,k
      integer(16),allocatable :: h(:),sh(:)
      
      read*,n,k
      allocate(h(n),sh(n))
      read*,h(:)
      
      !call bsort( h,k )
      sh = qSort( h )
      
      if( k/=0 )then
        if(k>n) k = n
        sh(1:k) = 0
      end if
      
      !test
    !  print*,qSort( [1_16,2_16, 5_16, 3_16, 2_16] )
    !  print*,qSort( [1_16,2_16] )
      print*,sum(sh)
      
      
      
      END PROGRAM