import heapq N, K = [ int(a) for a in input().split(' ') ] alist = [ int(a) for a in input().split(' ') ] def is_valid(num_subl, inds): inds = sorted(inds) prev = -1 for i in range(num_subl): start = max(prev+1, K*(i+1)-1) end = min(start+(num_subl-i-1)*(K-1)+ (N-num_subl*K if i > 0 else 0), N-(num_subl-i-1)-1) if inds[i] not in range(start,end+1): return False prev = inds[i] return True agenda = [] enumed = list(enumerate(alist)) sortedl = sorted(enumed, key=lambda p:p[1]) for num_subl in range(1,N//K+1): if num_subl > 1: if min(agenda) < sum([ p[1] for p in sortedl[:num_subl] ]): break queue = [] heapq.heappush(queue,(sum([ p[1] for p in sortedl[:num_subl] ]), tuple(sortedl))) while queue: sumc, checking = heapq.heappop(queue) if is_valid(num_subl, [ p[0] for p in checking[:num_subl] ]): agenda.append(sumc) break if len(checking)>num_subl: for i in range(num_subl): new_checking = checking[:i]+checking[i+1:] heapq.heappush(queue, (sum([ p[1] for p in new_checking[:num_subl] ]), new_checking)) print(min(agenda))