from collections import deque
n, m = map(int, input().split())
cost = list( map(int, input().split()) )# dp(i) 表示前i个烽火台进行选择,且最后一个烽火台点燃的所有合法方案中,最小开销的数值
# dp(i) = cost(i) + min(dp(i-1), dp(i-2), ... dp(i-m)) 其实dp(i)就依赖于前面长度为m
# 滑动窗口中的最小值
dp = [0] * nque = deque()
for idx, val in enumerate(cost):if idx < m:dp[idx] = cost[idx]else:dp[idx] = que[0][1] + cost[idx]while len(que) > 0 and idx - que[0][0] >= m:que.popleft()while len(que) > 0 and dp[idx] <= que[-1][1]:que.pop()que.append( (idx, dp[idx]) )print( min(dp[-m:]) )