program main

  implicit none

  integer(8)    :: n, k
  integer(8),allocatable :: h(:)
  integer(8)    :: i,j
  
  read(*,*) n,k
  allocate( h(n) )
  read(*,*) h
  i = 1 
  call quick_sort( h, i, n ) 
  
  print*, sum( h( i:n-k ) ) 

end program main
 recursive subroutine quick_sort( a, i, j )

  implicit none
  integer(8), intent(in) :: i,j
  integer(8), intent(inout) :: a(i:j)
  integer(8) :: p, pl, k, tmp

  if( i == j ) return
 
  p  = pivot(a,i,j)
 
  if( p /= -1 ) then
    call partition(a(i:j),i,j,a(p),k)
    call quick_sort(a(i:k-p),i,k-1)
    call quick_sort(a(k:j),k,j)
  end if

contains 

subroutine partition( a, i, j, x, l ) 
  
  implicit none
  integer(8), intent(in) :: i,j,x
  integer(8), intent(inout) :: a(i:j)
  integer(8), intent(out) :: l
  integer(8) :: r, t
   
  l = i 
  r = j 

  do while( l<=r )
    do while( l<=j .and. a(l) <  x ) 
      l = l+1
    end do
    do while( r> i .and. a(r) >= x ) 
      r = r-1
    end do
    if(l>r) exit
    t = a(l)
    a(l) = a(r)
    a(r) = t
    l = l+1
    r = r-1
  end do
  
end subroutine partition

function pivot( a, i, j ) result( k )

  implicit none
  integer(8), intent(in) :: i,j
  integer(8), intent(in) :: a(i:j)
  integer(8) :: k

  k = i + 1

  do while( k<=j .and. a(i) == a(k) ) 
    k = k + 1
  end do

  if(k>j) k = -1

  if(a(i)>a(k)) k = i

end function pivot

end subroutine quick_sort 

