def f(L,R,x): return -g(0,n,x) + 2*g(L,R,x) def g(i,j,x): return h(i,x)*(1 if i <= x else -1) + h(j,x)*(-1 if j <= x else 1) def h(j,x): return a[x]*(x-j) - (s[j]-s[x]) n,k = map(int,input().split()) *a, = map(int,input().split()) s = a + [0] for i in range(n)[::-1]: s[i] += s[i+1] L = 0 ans = 1<<60 for i in range(n): while L+k < n and a[i]-a[L] > a[L+k]-a[i]: L += 1 v = f(L,L+k,i) ans = min(v,ans) print(ans)