n, k = map(int, input().split()) k = n - k a = list(map(int, input().split())) now = sum(x - a[0] for x in a[:n - k]) - sum(x - a[0] for x in a[n - k: n]) ans = now l = 0 r = n - k for i in range(1, n): x = a[i] now += (a[i] - a[i-1]) * (-l + i-l - (r-i) + n-r) while l < i and r < n and x - a[l] > a[r] - x: now += -2 * (x - a[l]) + 2 * (a[r] - x) l += 1 r += 1 ans = min(ans, now) print(i, ans, l, r, now) print(ans)