program answer
  implicit none

  integer :: N, K, sum, i
  integer, allocatable :: p(:)

  read(*,*) N, K
  allocate(p(N))
  read(*,*) p

  call bsort(p)

  do i = 1, K
     sum = sum + p(i)
  end do
  
  write(*,*) sum

  deallocate(p)
  stop
contains
  !bsort
  subroutine bsort(array)
    implicit none
    integer, intent(inout) :: array(:)
    integer :: i, j, n

    n=size(array)
    do i = 1, n
       do j = 1, n-1
          call swapif(array(j),array(j+1))
       end do
    end do
  end subroutine bsort

  subroutine swapif(a,b)
    implicit none
    integer, intent(inout) :: a, b
    integer ::c
    if(a>b) then
       c=a
       a=b
       b=c
    end if
    end subroutine swapif
    
 end program answer

  