from heapq import heappop, heappush
n, k = map(int, input().strip().split())
costs = list(map(int, input().strip().split()))
ans = 0
heap = []
for i in range(n - 1, -1, -1):
    ans += costs[i]
    heappush(heap, -costs[i])
    if i % k == 0 and i > 0 and heap:
        ans += heappop(heap)
print(ans)