from heapq import * import sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def main(): n, m = MI() ww = LI() hp = [] for i in range(n): heappush(hp, (ww[i - 1] + ww[i], i)) diff=n-m used=[False]*n while diff: s,i=heappop(hp) if used[i] or s!=ww[i-1]+ww[i]:continue used[i]=True ww[i-1]=ww[i]=0 heappush(hp,(ww[i-2]+ww[i-1],i-1)) heappush(hp,(ww[i]+ww[(i+1)%n],i)) diff-=1 print(sum(ww)) main()