from heapq import heappush,heappop,heapify N,K = map(int,input().split()) A = list(map(int,input().split())) if K==1: exit(print(A[0])) ans = 10**18 que = [] S = 0 for i in range(1,N+1): if i*K>N: break tmp = A[i*K-1] if i!=1: t = A[(i-1)*K:i*K-1] t.sort(reverse=True) while t and -que[0] > t[-1]: S += que[0] heappop(que) S += t[-1] heappush(que,-t.pop()) tmp += S ans = min(ans,tmp) S += A[i*K-1] heappush(que,-A[i*K-1]) print(ans)