CodeEff/ECCO huggingface dataset:
# Original solution
N,K = list(map(int, input().split()))
V = [int(v) for v in input().split()]


def f(L, i, n):
    if i == 0 or len(L) == 0:
        return 0
    if L[n] >= 0:
        t = L[n]
        if n >= 0:
            n += 1
        else:
            n -= 1
        return t+f(L,i-1,n)
    else:
        t = L[n]
        if n >= 0:
            n += 1
        else:
            n -= 1
        if i >= 2:
            return max(f(L, i-2, n), t+f(L, i-1, n))
        else:
            return t+f(L, i-1, n)
    

t = min(K+1, N)
L1 = [0]*(t)
L2 = [0]*(t)
cnt1 = 0
cnt2 = 0
for j in range(t):
    if V[j] <= 0:
        cnt1 += 1
    if cnt1 <= j:
        L1[j] = f(V, j, 0)
    if V[t-j-1] <= 0:
        cnt2 += 1
    if cnt2 <= j:
        L2[t-j-1] = f(V, t-j-1, -1)
    
V_sort = sorted(V)
s = sum(V)
ans = 0
for i in range(1, K+1):
    if i >= N:
        c = s
        j = 0
        while j < N:
            if V_sort[j] < 0:
                c -= V_sort[j]
            else:
                j = N
            j += 1
        ans = max(ans, c)
    else:
        c = 0
        for j in range(i+1):
            c = max(c, L1[j]+L2[i-j])
        ans = max(ans, c)
        
print(ans)

# Optimized solution
N,K = list(map(int, input().split()))
V = [int(v) for v in input().split()]

ans = 0
for i in range(K+1):
    if i < N:
        for j in range(i+1):
            temp1 = V[:j]
            temp2 = V[N-(i-j):]
            temp = temp1 + temp2
            temp.sort()
            s = sum(temp)
            k = 0
            while k < len(temp) and k < K-i:
                if temp[k] < 0:
                    s -= temp[k]
                k += 1
            ans = max(ans, s)
    else:
        temp = sorted(V)
        s = sum(temp)
        j = 0
        while j < min(N, i-N):
            if temp[j] < 0:
                s -= temp[j]
            j += 1
        ans = max(ans, s)
        
print(ans)
# Original solution
N, K = list(map(int, input().split()))
V = list(map(int, input().split()))

def opA(D, v):
  ret = True
  if len(v) != 0:
    D.append(v.pop(0))
  else:
    ret = False
  return D, v, ret

def opB(D, v):
  ret = True
  if len(v) != 0:
    D.append(v.pop(-1))
  else:
    ret = False
  return D, v, ret

def opC(D, v):
  ret = True
  if len(D) != 0:
    v.insert(0, min(D))
    D.remove(min(D))
  else:
    ret = False
  return D, v, ret

def opD(D, v):
  ret = True
  if len(D) != 0:
    v.append(min(D))
    D.remove(min(D))
  else:
    ret = False
  return D, v, ret

def dp(n, D, v, op):
  if n == K:
    return sum(D)
  
  if op == 0:
    D, v, ret = opA(D, v)
  elif op == 1:
    D, v, ret = opB(D, v)
  elif op == 2:
    D, v, ret = opC(D, v)
  else:
    D, v, ret = opD(D, v)
  
  if ret == False:
    return sum(D)

  return max(dp(n + 1, D.copy(), v.copy(), 0), dp(n + 1, D.copy(), v.copy(), 1), dp(n + 1, D.copy(), v.copy(), 2), dp(n + 1, D.copy(), v.copy(), 3))

print((max(dp(0, [], V.copy(), 0), dp(0, [], V.copy(), 1), dp(0, [], V.copy(), 2), dp(0, [], V.copy(), 3))))

# Optimized solution
N, K = list(map(int, input().split()))
V = list(map(int, input().split()))

R = min(N, K)
l = []
ans = 0
for a in range(R + 1):
  for b in range(R + 1 - a):
    l = V[:a] + V[N - b:]
    l.sort()
    for i in range(K - a - b + 1):
      ans = max(ans, sum(l[i:]))
print((max(ans, 0)))
# Original solution
N,K = list(map(int,input().split()))
V = list(map(int,input().split()))
ans = []
for i in range(1,K+1):
    for j in range(2**i):
        gem = 0
        minus = []
        flag = True
        A = 0
        B = -1
        for k in range(i):
            if (j >> k) & 1 == 1:
                if A <= N-1:
                    if V[A] >= 0:
                        gem += V[A]
                    else:
                        minus.append(V[A])
                    A += 1
                else:
                    flag = False
                    break
            else:
                if B >= -N:
                    if V[B] >= 0:
                        gem += V[B]
                    else:
                        minus.append(V[B])
                    B -= 1
                else:
                    flag = False
                    break
        if flag:
            minus.sort(reverse = True)
            for l in range(K-i):
                if minus:
                    minus.pop()
            ans.append(gem + sum(minus))
print((max(ans)))
        

# Optimized solution
N,K = list(map(int,input().split()))
V = list(map(int,input().split()))
ans = []
for i in range(1,min(K,N)+1):
    for a in range(i+1):
        b = i-a
        AB = V[:a]
        if b > 0:
            AB += V[-b:]
        AB.sort(reverse = True)
        for j in range(K-i):
            if AB and AB[-1] < 0:
                AB.pop()
        ans.append(sum(AB))
print((max(ans)))
# Original solution
from heapq import heapify, heappop

N, K = list(map(int, input().split()))
vl = list(map(int, input().split()))
vr = list(reversed(vl))
sum_L = [0]
for vv in vl:
    sum_L.append(sum_L[-1] + vv)

sum_R = [0]
for vv in vr:
    sum_R.append(sum_R[-1] + vv)

ans = 0
for pick in range(min(K, N) + 1):
    for to_remove in range(K + 1):
        if pick + to_remove > K: break
        for left in range(pick + 1):
            right = pick - left

            t = sum_L[left] + sum_R[right]

            q = vl[:left] + vr[:right]
            heapify(q)

            cnt = 0
            while cnt < to_remove and q:
                t -= heappop(q)
                cnt += 1
            ans = max(ans, t)
print(ans)


# Optimized solution
N, K = list(map(int, input().split(" ")))
v = list(map(int, input().split(" ")))

pick_max = min(N, K)
ans = 0
for left in range(pick_max + 1):
    for right in range(pick_max - left + 1):
        t = v[:left] + v[N - right:]
        m = sorted([x for x in t if x < 0])[:K - left - right]
        ans = max(ans, sum(t) - sum(m))
print(ans)
# Original solution
inf = float('inf')

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

volume = max(N, K)
take_max = min(N, K)

dpl = [[-inf] * (volume + 1) for _ in range(N + 1)]
dpr = [[-inf] * (volume + 1) for _ in range(N + 1)]
dpl[0][0] = dpr[0][0] = 0

for i, vv in enumerate(v, 1):
    for take in range(i + 1):
        dpl[i][take] = dpl[i - 1][take]
        if take > 0:
            dpl[i][take] = max(dpl[i][take], dpl[i - 1][take - 1] + vv)

for i, vv in enumerate(reversed(v), 1):
    for take in range(i + 1):
        dpr[i][take] = max(dpr[i][take], dpr[i - 1][take])
        if take > 0:
            dpr[i][take] = max(dpr[i][take], dpr[i - 1][take - 1] + vv)

ans = 0
for take_frm_left in range(take_max + 1):
    for take_frm_right in range(take_max + 1):
        if take_frm_left + take_frm_right > take_max: break
        for use_frm_left in range(take_frm_left + 1):
            remove_frm_left = take_frm_left - use_frm_left
            vl = dpl[take_frm_left][use_frm_left]
            for use_frm_right in range(take_frm_right + 1):
                remove_frm_right = take_frm_right - use_frm_right
                if (take_frm_left + take_frm_right + remove_frm_left + remove_frm_right) > K: continue
                vr = dpr[take_frm_right][use_frm_right]
                # print(vl + vr, take_frm_left, use_frm_left, take_frm_right, use_frm_right)
                ans = max(ans, vl + vr)
print(ans)


# Optimized solution
N, K = list(map(int, input().split()))
v = list(map(int, input().split()))

max_pick = min(K, N)
ans = 0

# 左右から取る量を決め打ち

for left in range(max_pick + 1):
    for right in range(max_pick - left + 1):
        t = v[:left] + v[N - right:]
        m = sorted([x for x in t if x < 0])[:K - left - right]
        ans = max(ans, sum(t) - sum(m))
print(ans)
# Original solution
n, k = list(map(int, input().split()))
v = list(map(int, input().split()))

ans = 0
m = min(n, k)
for i in range(m + 1):
    for j in range(m - i + 1):
        t = v[:i] + v[n - j:]
        t.sort()
        d = tuple([x for x in t if x < 0])[:k - i - j]
        ans = max(ans, sum(t) - sum(d))
print(ans)


# Optimized solution
def main():
    N, K = list(map(int, input().split()))
    *V, = list(map(int, input().split()))

    m = min(N, K)

    ans = 0
    for left in range(m + 1):
        _r = m - left
        for right in range(_r + 1):
            rest = K - left - right
            take = V[:left] + V[N - right:]
            take.sort()
            s = 0
            for x in take:
                if rest and x < 0:
                    rest -= 1
                    continue
                s += x
            ans = max(ans, s)
    print(ans)


if __name__ == '__main__':
    main()

# 操作回数はK回以内
# x=min(K,N)個取り出す
# K-x個戻せる
# Original solution
N, K = list(map(int, input().split()))
V = list(map(int, input().split()))

answer = 0
limit = min(N, K)
for a in range(limit+1): #左から取り出す
    for b in range(limit-a+1): #右から取り出す
        for c in range(K-(a+b)+1):
            use = V[:a] + V[N-b:]
            use.sort()
            s = sum(use[c:])
            answer = max(answer, s)

print(answer)

# Optimized solution
import sys
sys.setrecursionlimit(10 ** 9)

N, K = list(map(int, input().split()))
V = list(map(int, input().split()))
V2 = V * 2
answer = 0
for i in range(1, min(N, K)+1): # iは宝石を取り出す個数
    for j in range(0, min(i, K-i)+1): # jは何個の宝石を手離すか
        for k in range(max(0, N-i), N+1): # V2の中で何番目から取り出すか
            use = V2[k:k+i]
            use.sort()
            s = sum(use[j:])
            if s > answer:
                answer = s

print(answer)
# Original solution
# coding: utf-8
import sys
sys.setrecursionlimit(10 ** 7)

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# 持っている宝石をDに詰めるのは最後
N, K = lr()
V = lr()
answer = 0
V = V[::-1] + V[::-1]
for x in range(K+1):
    # x回Dから取り出す、K-x回戻すことが可能
    x = min(x, N)
    for i in range(N-1, N-1+x+1):
        take = V[i-x+1:i+1]
        take.sort(reverse=True)
        result = sum(take)
        if result > answer:
            answer = result
        for j in range(K-x):
            if take:
                result -= take.pop()
                if result > answer:
                    answer = result

print(answer)


# Optimized solution
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, K = lr()
V = lr()
scores = [0]
limit = min(N, K)
for i in range(limit+1): # iは左から取り出す個数
    for j in range(limit-i+1): # jは右から取り出す個数
        stone = V[:i] + V[N-j:]
        stone.sort()
        result = sum(stone)
        scores.append(result)
        for k in range(min(len(stone), K-(i+j))): # kは戻す個数
            if stone[k] > 0:
                break
            result -= stone[k] 
            scores.append(result)

answer = max(scores)
print(answer)
# 07
# Original solution
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, K = lr()
V = lr()
scores = [0]
limit = min(N, K)
for i in range(limit+1): # iは左から取り出す個数
    for j in range(limit-i+1): # jは右から取り出す個数
        stone = V[:i] + V[N-j:]
        stone.sort()
        result = sum(stone)
        scores.append(result)
        for k in range(min(len(stone), K-(i+j))): # kは戻す個数
            if stone[k] > 0:
                break
            result -= stone[k] 
            scores.append(result)

answer = max(scores)
print(answer)
# 07

# Optimized solution
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, K = lr()
V = lr()
answer = 0
limit = min(N, K)
for i in range(limit+1): # iは左から取り出す個数
    for j in range(limit-i+1): # jは右から取り出す個数
        stone = V[:i] + V[N-j:]
        stone.sort()
        result = sum(stone)
        if result > answer:
            answer = result
        for k in range(min(len(stone), K-(i+j))): # kは戻す個数
            if stone[k] > 0:
                break
            result -= stone[k] 
            if result > answer:
                answer = result

print(answer)
# 07
# Original solution
# https://atcoder.jp/contests/abc128/abc128_d
import itertools as it

N, K = list(map(int, input().split()))
vs = [int(s) for s in input().split()]

acc = [0] + list(it.accumulate(vs))
res = 1000000000
for left in range(min(N, K) + 1):
    for right in range(min(N, K) + 1 - left):
        tmp = acc[N - right] - acc[left]
        rem = K - left - right
        rest = sorted(vs[:left] + vs[N - right :])[:rem]

        for x in rest:
            if x < 0:
                tmp += x
        if tmp < res:
            res = tmp

print((acc[-1] - res))


# Optimized solution
import itertools as it

n, k = list(map(int, input().split()))
vs = [int(s) for s in input().split()]
vs_acc = [0] + list(it.accumulate(vs))
vs_total = vs_acc[-1]

ans = 0
for left in range(min(n, k) + 1):
    for right in range(min(n, k) - left + 1):
        cnt = k - (left + right)
        remaining = vs_acc[n - right] - vs_acc[left]
        subsum = vs_total - remaining

        rest = sorted(vs[:left] + vs[n - right :])[:cnt]
        for x in rest:
            if x < 0:
                subsum -= x
            else:
                break
        ans = max(ans, subsum)

print(ans)
# Original solution
N, K = list(map(int, input().split()))
V = list(map(int, input().split()))

rs = []
def check(k, v, current):
	global rs
	rs += [sum(current)]
	if 0 == k:
		return
	else:
		if current:
			mc = min(current)
			if mc < 0:
				i = current.index(mc)
				check(k - 1, v + [mc], current[:i] + current[i + 1:])
				check(k - 1, [mc] + v, current[:i] + current[i + 1:])
		if v:
			check(k - 1, v[1:], current + [v[0]])
			check(k - 1, v[:-1], current + [v[-1]])
		return

check(K, V, [])

print((max(rs)))


# Optimized solution
N, K = list(map(int, input().split()))
V = list(map(int, input().split()))

vmax = 0
for l in range(0, K + 1):
	for r in range(0, K - l + 1):
		if N < l + r:
			break
		tv = V[:l] + V[N - r:]
		#print(tv)
		tv.sort()
		for n in range(min(len(tv), K - (l + r))):
			if tv[n] < 0:
				tv[n] = 0
		vmax = max(vmax, sum(tv))

print(vmax)
# Original solution
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return list(map(int, stdin.readline().split()))
def li_(): return [int(x) - 1 for x in stdin.readline().split()]
def lf(): return list(map(float, stdin.readline().split()))
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from heapq import heappop

n, k = li()
v = list(li())

ans = 0

for x in range(-(-k//2)):
    if 2*x > k:
        continue

    get = k - x
    if get >= n:
        cnt = 0
        heap = sorted(v)

        while heap[0] < 0 and cnt < x:
            _ = heappop(heap)
            cnt += 1

        ans = max(ans, sum(heap))

    else:
        for i in range(get+1):
            cnt = 0
            heap = sorted(v[:i] + v[n-(get-i):])
            while heap[0] < 0 and cnt < x:
                _ = heappop(heap)
                cnt += 1

            ans = max(ans, sum(heap))

print(ans)

# Optimized solution
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return list(map(int, stdin.readline().split()))
def li_(): return [int(x) - 1 for x in stdin.readline().split()]
def lf(): return list(map(float, stdin.readline().split()))
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

# 入力
n, k = li()
v = list(li())

# 個数m決め打ち
ans = 0
for m in range(n+1):
    rest = k
    rest -= m

    if rest < 0:
        continue

    # 左からi個、右からm-i個
    for i in range(m+1):
        a = v[:i] + v[n-m+i:]
        a.sort()
        suma = sum(a)

        # kを超えないようにマイナスを詰める
        for j in range(rest):
            if j >= len(a):
                break

            if a[j] < 0:
                suma += abs(a[j])
            else:
                break

        ans = max(ans, suma)

print(ans)
# Original solution
N, K = list(map(int, input().split()))
List = list(map(int, input().split()))

ans = [0]
for i in range(min(N, K)+1):
    for j in range(min(N, K)-i+1):
        if j==0:
            temp = List[:i]
        else:
            temp = List[:i]+List[-j:]
        for k in range(min(i+j, K-(i+j))+1):
            ans.append(sum(sorted(temp)[k:]))
    

print((max(ans)))

# Optimized solution
N, K = list(map(int, input().split()))
List = list(map(int, input().split()))

ans = 0
for i in range(min(N, K)+1):
    for j in range(min(N, K)-i+1):
        if j==0:
            temp = List[:i]
        else:
            temp = List[:i]+List[-j:]
        for k in range(min(i+j, K-(i+j))+1):
            ret = sum(sorted(temp)[k:])
            if ret>ans:
                ans = ret
                
print(ans)
# Original solution
N,K = list(map(int,input().split()))
V = list(map(int,input().split()))
vmax = -10**9
for n1 in range(K+1):
    A = V[:n1]
    ind = min(n1,N)
    m = N-ind
    for n2 in range(min(K-n1,m)+1):
        B = V[N-n2:]
        C = A+B
        C = sorted(C,reverse=True)
        k = K-n1-n2
        for i in range(k):
            if len(C)==0 or C[-1]>=0:break
            C.pop()
        vmax = max(vmax,sum(C))
print(vmax)           

# Optimized solution
N,K = list(map(int,input().split()))
V = list(map(int,input().split()))
vmax = 0
for i in range(K+1):
    for j in range(K-i+1):
        cnt = sum(V[:j])
        A = V[:j]
        n = N-(K-i-j)
        if n>=j:
            cnt += sum(V[n:])
            A += V[n:]
        else:
            cnt += sum(V[j:])
            A += V[j:]
        A = sorted(A)
        B = []
        for k in range(i):
            if k<len(A) and A[k]<0:
                B.append(k)
        for k in B:
            cnt -= A[k]
        vmax = max(vmax,cnt)
print(vmax)
# Original solution
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,k = inpl()
a = inpl()
res = 0
for ope in range(k+1):
    for pick in range(ope+1):
        if pick > n: continue
        for back in range(ope - pick + 1):
            for lp in range(pick+1):
                rp = pick - lp
                # print(ope,back,lp,rp)
                q = []
                for i in range(lp):
                    q.append(a[i])
                for i in range(rp):
                    q.append(a[-i-1])
                q.sort()
                q = deque(q)
                for i in range(back):
                    if not q: break
                    q.popleft()
                res = max(res, sum(q))
print(res)

# Optimized solution
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,k = inpl()
a = inpl()
res = 0
for ope in range(k+1):
    for pick in range(ope+1):
        if pick > n: continue
        back = ope - pick
        for lp in range(pick+1):
            rp = pick - lp
            # print(ope,back,lp,rp)
            q = []
            for i in range(lp):
                q.append(a[i])
            for i in range(rp):
                q.append(a[-i-1])
            q.sort()
            ans = sum(q)
            res = max(res,ans)
            for i in range(back):
                if i >= pick: break
                ans -= q[i]
                res = max(res,ans)
print(res)
# Original solution
n, k = list(map(int, input().split()))
v = list(map(int, input().split()))
# 再帰関数でいけそうだぞい
def dfs(que, cost, value):
    if cost <= 0:
        return value
    elif len(que) > 1:
        if que[0] < 0:
            tmp1 = max(dfs(que[1:], cost-1, value+que[0]), dfs(que[1:], cost-2, value))
        else:
            tmp1 = dfs(que[1:], cost-1, value+que[0])
        if que[-1] < 0:
            tmp2 = max(dfs(que[:-1], cost-1, value+que[-1]), dfs(que[:-1], cost-2, value))
        else:
            tmp2 = dfs(que[:-1], cost-1, value+que[-1])
    else:
        tmp1 = value + que[0]
        tmp2 = value + que[-1]
    return max(tmp1, tmp2)

ans = dfs(v, k, 0)
print(ans)


# Optimized solution
n, k = list(map(int, input().split()))
v = list(map(int, input().split()))

ans = 0
for a in range(min(n, k) + 1):
    for b in range(min(n - a, k - a) + 1):
        v_use = v[:a] + v[-b:] if b >= 1 else v[:a]
        t = k - a - b
        s = 0
        for j in sorted(v_use):
            if j < 0 and t > 0:
                s += -j
                t -= 1
        ans = max(ans, sum(v_use) + s)

print(ans)
# Original solution

import copy

class MyClass():

    maxSum = 0

    def do(self, baseList, _temochi, leftIndex, rightIndex, nokoriSousaCount):
        if nokoriSousaCount < 0:
            return

        sum = 0
        minNum = 0

        for each in _temochi:
            sum += each
            if each < 0 and each < minNum:
                minNum = each

        if self.maxSum < sum:
            self.maxSum = sum

        if leftIndex <= rightIndex:
            temochi = copy.copy(_temochi)
            temochi.append(baseList[leftIndex])
            self.do(baseList, temochi, leftIndex + 1, rightIndex, nokoriSousaCount - 1)

            temochi = copy.copy(_temochi)
            temochi.append(baseList[rightIndex])
            self.do(baseList, temochi, leftIndex, rightIndex - 1, nokoriSousaCount - 1)
            
        self.removeMin(_temochi, minNum, leftIndex, rightIndex, nokoriSousaCount - 1)

    def removeMin(self, _temochi, minNum, leftIndex, rightIndex, nokoriSousaCount):
        if minNum < 0 and len(_temochi) > 0 and nokoriSousaCount > 0:
            #minNum = min(_temochi)
            #if minNum < 0:
                #temochi = copy.copy(_temochi)
            _temochi.remove(minNum)
            self.do(baseList, _temochi, leftIndex, rightIndex, nokoriSousaCount)


count, opeCount = list(map(int, input().split()))
baseList = list(map(int, input().split()))

doClass = MyClass()
doClass.do(baseList, [], 0, count - 1, opeCount)

print((doClass.maxSum))




# Optimized solution
class MyClass():
    maxNumber = 0

    def roop(self, baseList, opeCount):
        listCount = len(baseList)
        roopMax = listCount if listCount < opeCount else opeCount
        for leftCount in range(0, roopMax):
            self.getMax(baseList[0: leftCount + 1], opeCount - leftCount - 1)

            for rightCount in range(1, roopMax - leftCount + 1):
                leftList = baseList[0: leftCount]
                rightList = baseList[listCount - rightCount : listCount]
                handList = leftList + rightList

                self.getMax(handList, opeCount - leftCount - rightCount)

                

    def getMax(self, handList, remainOpeCount):
        if len(handList) < 1:
            return

        maxVal = 0
        removeCount = 0

        if remainOpeCount > 0:
            handList.sort()
            for each in handList:
                if each < 0:
                    removeCount += 1
                    remainOpeCount -= 1
                    if remainOpeCount < 1:
                        break
        
        maxVal = sum(handList[removeCount:])
        self.maxNumber = max([maxVal, self.maxNumber])


count, opeCount = list(map(int, input().split()))
baseList = list(map(int, input().split()))

my = MyClass()
my.roop(baseList, opeCount)
print((my.maxNumber))