import sys
import copy
import heapq
from collections import deque
input = lambda: sys.stdin.readline().rstrip()
INF = 10**9 + 7


def solve():
    N, K = map(int, input().split())
    V = deque(list(map(int, input().split())))

    ans = -INF
    for n in range(1, N + 1):
        if n > K:
            break
        for rn in range(n + 1):
            ln = n - rn
            tv = copy.copy(V)
            pv = 0
            mv = []
            heapq.heapify(mv)
            for _ in range(rn):
                v = tv.pop()
                if v >= 0:
                    pv += v
                else:
                    heapq.heappush(mv, v)
            for _ in range(ln):
                v = tv.popleft()
                if v >= 0:
                    pv += v
                else:
                    heapq.heappush(mv, v)

            mvlen = min(K - n, len(mv))
            for _ in range(0, mvlen):
                heapq.heappop(mv)

            #print(ln, rn, pv, mv)
            ans = max(ans, pv + sum(mv))

    print(max(0, ans))


if __name__ == '__main__':
    solve() import sys sys import copy copy import heapq heapq from collections import deque input = lambda: sys.stdin.readline().rstrip() input lambda: sys.stdin.readline().rstrip() sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip INF = 10**9 + 7 INF 10**9 + 7 10**9 10 9 7 def solve():
    N, K = map(int, input().split())
    V = deque(list(map(int, input().split())))

    ans = -INF
    for n in range(1, N + 1):
        if n > K:
            break
        for rn in range(n + 1):
            ln = n - rn
            tv = copy.copy(V)
            pv = 0
            mv = []
            heapq.heapify(mv)
            for _ in range(rn):
                v = tv.pop()
                if v >= 0:
                    pv += v
                else:
                    heapq.heappush(mv, v)
            for _ in range(ln):
                v = tv.popleft()
                if v >= 0:
                    pv += v
                else:
                    heapq.heappush(mv, v)

            mvlen = min(K - n, len(mv))
            for _ in range(0, mvlen):
                heapq.heappop(mv)

            #print(ln, rn, pv, mv)
            ans = max(ans, pv + sum(mv))

    print(max(0, ans)) solve N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K V = deque(list(map(int, input().split()))) V deque(list(map(int, input().split()))) deque list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split ans = -INF ans -INF INF for n in range(1, N + 1):
        if n > K:
            break
        for rn in range(n + 1):
            ln = n - rn
            tv = copy.copy(V)
            pv = 0
            mv = []
            heapq.heapify(mv)
            for _ in range(rn):
                v = tv.pop()
                if v >= 0:
                    pv += v
                else:
                    heapq.heappush(mv, v)
            for _ in range(ln):
                v = tv.popleft()
                if v >= 0:
                    pv += v
                else:
                    heapq.heappush(mv, v)

            mvlen = min(K - n, len(mv))
            for _ in range(0, mvlen):
                heapq.heappop(mv)

            #print(ln, rn, pv, mv)
            ans = max(ans, pv + sum(mv))

     n range(1, N + 1) range 1 N + 1 N 1 if n > K:
            break
         n > K n K break for rn in range(n + 1):
            ln = n - rn
            tv = copy.copy(V)
            pv = 0
            mv = []
            heapq.heapify(mv)
            for _ in range(rn):
                v = tv.pop()
                if v >= 0:
                    pv += v
                else:
                    heapq.heappush(mv, v)
            for _ in range(ln):
                v = tv.popleft()
                if v >= 0:
                    pv += v
                else:
                    heapq.heappush(mv, v)

            mvlen = min(K - n, len(mv))
            for _ in range(0, mvlen):
                heapq.heappop(mv)

            #print(ln, rn, pv, mv)
            ans = max(ans, pv + sum(mv))

     rn range(n + 1) range n + 1 n 1 ln = n - rn ln n - rn n rn tv = copy.copy(V) tv copy.copy(V) copy.copy copy copy V pv = 0 pv 0 mv = [] mv [] heapq.heapify(mv) heapq.heapify heapq heapify mv for _ in range(rn):
                v = tv.pop()
                if v >= 0:
                    pv += v
                else:
                    heapq.heappush(mv, v)
             _ range(rn) range rn v = tv.pop() v tv.pop() tv.pop tv pop if v >= 0:
                    pv += v
                else:
                    heapq.heappush(mv, v)
             v >= 0 v 0 pv += v pv v heapq.heappush(mv, v) heapq.heappush heapq heappush mv v for _ in range(ln):
                v = tv.popleft()
                if v >= 0:
                    pv += v
                else:
                    heapq.heappush(mv, v)

             _ range(ln) range ln v = tv.popleft() v tv.popleft() tv.popleft tv popleft if v >= 0:
                    pv += v
                else:
                    heapq.heappush(mv, v)

             v >= 0 v 0 pv += v pv v heapq.heappush(mv, v) heapq.heappush heapq heappush mv v mvlen = min(K - n, len(mv)) mvlen min(K - n, len(mv)) min K - n K n len(mv) len mv for _ in range(0, mvlen):
                heapq.heappop(mv)

            #print(ln, rn, pv, mv)
             _ range(0, mvlen) range 0 mvlen heapq.heappop(mv) heapq.heappop heapq heappop mv ans = max(ans, pv + sum(mv)) ans max(ans, pv + sum(mv)) max ans pv + sum(mv) pv sum(mv) sum mv print(max(0, ans)) print max(0, ans) max 0 ans if __name__ == '__main__':
    solve() __name__ == '__main__' __name__ '__main__' solve() solve INF = 10**9 + 7 10**9 + 7 INF def solve():
    N, K = map(int, input().split())
    V = deque(list(map(int, input().split())))

    ans = -INF
    for n in range(1, N + 1):
        if n > K:
            break
        for rn in range(n + 1):
            ln = n - rn
            tv = copy.copy(V)
            pv = 0
            mv = []
            heapq.heapify(mv)
            for _ in range(rn):
                v = tv.pop()
                if v >= 0:
                    pv += v
                else:
                    heapq.heappush(mv, v)
            for _ in range(ln):
                v = tv.popleft()
                if v >= 0:
                    pv += v
                else:
                    heapq.heappush(mv, v)

            mvlen = min(K - n, len(mv))
            for _ in range(0, mvlen):
                heapq.heappop(mv)

            #print(ln, rn, pv, mv)
            ans = max(ans, pv + sum(mv))

    print(max(0, ans)) def solve():
    N, K = map(int, input().split())
    V = deque(list(map(int, input().split())))

    ans = -INF
    for n in range(1, N + 1):
        if n > K:
            break
        for rn in range(n + 1):
            ln = n - rn
            tv = copy.copy(V)
            pv = 0
            mv = []
            heapq.heapify(mv)
            for _ in range(rn):
                v = tv.pop()
                if v >= 0:
                    pv += v
                else:
                    heapq.heappush(mv, v)
            for _ in range(ln):
                v = tv.popleft()
                if v >= 0:
                    pv += v
                else:
                    heapq.heappush(mv, v)

            mvlen = min(K - n, len(mv))
            for _ in range(0, mvlen):
                heapq.heappop(mv)

            #print(ln, rn, pv, mv)
            ans = max(ans, pv + sum(mv))

    print(max(0, ans)) solve input = lambda: sys.stdin.readline().rstrip() lambda: sys.stdin.readline().rstrip() input
import sys
import copy
import heapq
from collections import deque
input = lambda: sys.stdin.readline().rstrip()
INF = 10**9 + 7


def solve():
    N, K = map(int, input().split())
    V = deque(list(map(int, input().split())))

    ans = -INF
    for n in range(1, N + 1):
        if n > K:
            break
        for rn in range(n + 1):
            ln = n - rn
            tv = copy.copy(V)
            pv = 0
            mv = []
            heapq.heapify(mv)
            for _ in range(rn):
                v = tv.pop()
                if v >= 0:
                    pv += v
                else:
                    heapq.heappush(mv, v)
            for _ in range(ln):
                v = tv.popleft()
                if v >= 0:
                    pv += v
                else:
                    heapq.heappush(mv, v)

            mvlen = min(K - n, len(mv))
            for _ in range(0, mvlen):
                heapq.heappop(mv)

            #print(ln, rn, pv, mv)
            ans = max(ans, pv + sum(mv))

    print(max(0, ans))


if __name__ == '__main__':
    solve()
