N, K = map(int, input().split()) A = [int(a) for a in input().split()] ma = A[-1] mi = A[0] B = [a - mi for a in A] C = [a - mi for a in A] print(min(sum(B[:K]) - sum(B[K:]), sum(C[:K]) - sum(C[K:])))