import sys input = lambda :sys.stdin.readline()[:-1] ni = lambda :int(input()) na = lambda :list(map(int,input().split())) yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES") no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO") ####################################################################### from heapq import * ans = 0 n,k = na() k-=1 a = na() b = [-float("inf")] for i in range(1,n,2): b.append(max(b[-1],a[i])) s = 0 hq = [] if n % 2: hq.append(a[-1]) s += a[-1] while len(hq) > k: x = heappop(hq) s-=x if len(hq) == k: ans = max(ans, s + b[-1]) for i in range(n//2-1,-1,-1): heappush(hq, a[i*2]) s += a[i*2] heappush(hq, a[i*2+1]) s += a[i*2 + 1] while len(hq) > k: x = heappop(hq) s-=x if len(hq) == k: ans = max(ans, s + b[i]) print(ans)