program EDPCB
    implicit none
    integer(8)::N,K,i,j
    integer(8),allocatable,dimension(:)::H,dp
    read(5,*)N,K
    allocate(H(N))
    allocate(dp(N))
    read(5,*)(H(i),i=1,N)

    dp=0

    do j=2,N
        do i=1,K
            if(j>i)then
                if(i==1)then
                    dp(j)=dp(j-1)+abs(H(j)-H(j-1))
                else
                    dp(j)=min(dp(j),dp(j-i)+abs(H(j)-H(j-i)))
                end if
            else
                exit
            end if
        end do
    end do

    print'(i0)',dp(N)
end program EDPCB