import sys input = sys.stdin.readline N,K=map(int,input().split()) A=list(map(int,input().split())) S=[0] for a in A: S.append(S[-1]+a) plus=K minus=N-K def calc(i,j): a=A[i] ANS=S[i+1]-a*(i+1) ANS+=a*(N-i-1)-(S[-1]-S[i+1]) if j+K-1<=i: ANS+=2*(a*K-(S[j+K]-S[j])) elif j>=i: ANS+=2*(S[j+K]-S[j]-a*K) else: ANS+=2*(a*(i-j)-(S[i]-S[j])) ANS+=2*(S[j+K]-S[i]-a*(j+K-i)) return ANS LANS=1<<100 for i in range(N): MIN=0 MAX=N-K while MAX>MIN+5: mid1=MIN+(MAX-MIN)//3 mid2=MIN+(MAX-MIN)//3*2 if calc(i,mid1)