import sys
import random
import heapq
input = sys.stdin.readline
def fg():
    return int(input())
def fgh():
    return [int(xx) for xx in input().split()]
def tt():
    sys.stdout.flush()
def sd():
    print('Yes')
def df():
    print('No')
'''for __ in range(fg()):
    n, k = fgh()
    a = fgh()
    a = a[::-1]
    dp = [[-23239239293] * (n + 1) for i in range(k + 1)]
    dp[0][0] = 0
    for i in range(k):
        for j in range(1, n + 1):
            dp[i + 1][j - 1] = max(dp[i + 1][j - 1], dp[i][j] - a[i])
        for j in range(n):
            dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + a[i])
    print(max(dp[k]))
    ans = a[0]
    a.pop(0)
    b = 1
    h = []
    for i in range(k - 1):
        b += 1
        heapq.heappush(h, a[i])
        ans += a[i]
        if b > n:
            b -= 2
            ans -= 2 * h[0]
            heapq.heappop(h)
    print(ans)'''
n, k = fgh()
a = fgh()
h = []
s = 0
ans = 0
for i in range(n):
    ans = max(ans, a[i] + s + i + 1)
    if len(h) < k - 1:
        heapq.heappush(h, a[i])
        s += a[i]
    else:
        if len(h) and h[0] < a[i]:
            s += a[i] - h[0]
            heapq.heappop(h)
            heapq.heappush(h, a[i])
print(ans)