program main
implicit none
integer,allocatable::h1(:)
integer,allocatable::h2(:)
integer::k,n,i,x,y,z

read(*,*) n,k
allocate(h1(n))
allocate(h2(n))

do i=1,n
read(*,*) h1(i)
end do
h2(1)=h1(1)

do i=2,n
h2(i)=0
end do

do i=2,n
do x=1,i
if ((h2(x)-h1(i))<0) then

do y=i,x+1,-1
h2(y)=h2(y-1)
end do
h2(x)=h1(i)
end if
end do
end do
z=h2(1+k-1)-h2(1)
do i=2,n-k+1

if (z>(h2(i+k-1)-h2(i))) then
z=-h2(i+k-1)+h2(i)

end if
end do

write(*,*) z




end program main


