program sample
implicit none

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

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

do i = 1, N
if(p(i) >= p(i+1))then
p(i)=p(i+1)
endif
end do

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

write(*,*) sum

stop


end program sample