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 = 0 if i!=1: t = A[(i-1)*K:i*K] t.sort(reverse=True) tmp += t.pop() while t and -que[0] > t[-1]: S += que[0] heappop(que) S += t[-1] heappush(que,-t.pop()) else: tmp = A[i*K-1] tmp += S ans = min(ans,tmp) S += A[i*K-1] heappush(que,-A[i*K-1]) print(ans)