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)
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)
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)

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)

-from heapq import heappop - +# 入力 +# 個数m決め打ち -for x in range(-(-k // 2)): -    if 2 * x > k: +for m in range(n + 1): +    rest = k +    rest -= m +    if rest < 0: -    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)) +    # 左から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)