N, K = list(map(int, input().split()))
a = list(map(int, input().split()))

def f(arr):
    s = sum(a[N-K:N])
    if s<0:
        s = 0
    for i in range(N - K):
        if a[i] > 0:
            s += a[i]
    return s

print(max(f(a), f(a[::-1])))