n, k = map(int, input().split())
a = list(map(int, input().split()))
f = [[], []]
for it in range(2):
s = [[0, 0]]
x = 0
for i in range(n):
while len(s) > 1 and a[i] <= a[s[-1][0]]:
x -= s[-1][1]
s.pop()
dx = (i - s[-1][0]) * a[i]
x += dx
f[it].append(dx + s[-1][1])
s.append([i, dx + s[-1][1]])
a = list(reversed(a))
print(min([f[0][i] + f[1][n - (i + k)] for i in range(n - k + 1)]))