program main

  implicit none

  integer    :: n, k
  integer,allocatable :: p(:)
  real(8)   ,allocatable :: sp(:)
  real(8)   ,allocatable :: h(:)
  real(8)   ,allocatable :: sh(:)
  integer :: i, j 
  real(8) :: rj
  
  read(*,*) n, k
  allocate( p(n) )
  allocate( h(n) )
  read(*,*) p

  allocate( sp(maxval( p )) )
  j = 0
  do i = 1, maxval( p )
    j = j + i
    sp(i) = j
  end do

  do i = 1, n
    h(i) = sp(p(i)) / p(i) 
  end do

  allocate( sh(n-k+1) )
  rj = sum( h(1:k) )
  do i = 1, n-k+1
    sh(i) = rj
    rj = rj + h(i+k) - h(i) 
  end do
  
  call quicksort1( sh, 1, n-k+1 ) 

  print*, sh( n-k+1 )

end program main

recursive subroutine quicksort1(a, first, last)
  implicit none
  real(8),intent(inout)::a(*)
  integer,intent(in)::first,last
  !
  ! Original
  ! https://gist.github.com/t-nissie/479f0f16966925fa29ea
  !
  real(8)::x,t
  integer::i,j

  x = a((first+last)/2)
  i = first
  j = last
  do
     do while (a(i) < x)
        i=i+1
     end do
     do while (x < a(j))
        j=j-1
     end do
     if (i >= j) exit
     t = a(i)
     a(i) = a(j)
     a(j) = t
     i=i+1
     j=j-1
  end do
  if (first < i-1) call quicksort1(a, first, i-1)
  if (j+1 < last)  call quicksort1(a, j+1, last)

  return
end subroutine quicksort1
