from heapq import heappop, heapify


N, K, *V = map(int, open(0).read().split())

# i: 操作回数
# j: 取り出す個数
# k: 左から取り出す範囲 [0, k)
ans = 0
for i in range(K + 1):
    for j in range(min(N + 1, i + 1)):
        for k in range(j + 1):
            right = N - j + k
            tmp = V[:k] + V[right:]

            put_out_cnt = i - j
            heapify(tmp)
            while tmp and put_out_cnt > 0:
                heappop(tmp)
                put_out_cnt -= 1
            ans = max(ans, sum(tmp))
print(ans) from heapq import heappop, heapify N, K, *V = map(int, open(0).read().split()) N map(int, open(0).read().split()) map int open(0).read().split() (0).read().split (0).read() (0).read (0) open 0 read split K *V V ans = 0 ans 0 for i in range(K + 1):
    for j in range(min(N + 1, i + 1)):
        for k in range(j + 1):
            right = N - j + k
            tmp = V[:k] + V[right:]

            put_out_cnt = i - j
            heapify(tmp)
            while tmp and put_out_cnt > 0:
                heappop(tmp)
                put_out_cnt -= 1
            ans = max(ans, sum(tmp)) i range(K + 1) range K + 1 K 1 for j in range(min(N + 1, i + 1)):
        for k in range(j + 1):
            right = N - j + k
            tmp = V[:k] + V[right:]

            put_out_cnt = i - j
            heapify(tmp)
            while tmp and put_out_cnt > 0:
                heappop(tmp)
                put_out_cnt -= 1
            ans = max(ans, sum(tmp)) j range(min(N + 1, i + 1)) range min(N + 1, i + 1) min N + 1 N 1 i + 1 i 1 for k in range(j + 1):
            right = N - j + k
            tmp = V[:k] + V[right:]

            put_out_cnt = i - j
            heapify(tmp)
            while tmp and put_out_cnt > 0:
                heappop(tmp)
                put_out_cnt -= 1
            ans = max(ans, sum(tmp)) k range(j + 1) range j + 1 j 1 right = N - j + k right N - j + k N - j N j k tmp = V[:k] + V[right:] tmp V[:k] + V[right:] V[:k] V :k k V[right:] V right: right put_out_cnt = i - j put_out_cnt i - j i j heapify(tmp) heapify tmp while tmp and put_out_cnt > 0:
                heappop(tmp)
                put_out_cnt -= 1
             tmp and put_out_cnt > 0 tmp put_out_cnt > 0 put_out_cnt 0 heappop(tmp) heappop tmp put_out_cnt -= 1 put_out_cnt 1 ans = max(ans, sum(tmp)) ans max(ans, sum(tmp)) max ans sum(tmp) sum tmp print(ans) print ans ans = max(ans, sum(tmp)) max(ans, sum(tmp)) ans K, *V = map(int, open(0).read().split()) map(int, open(0).read().split()) K put_out_cnt = i - j i - j put_out_cnt right = N - j + k N - j + k right N, K, *V = map(int, open(0).read().split()) map(int, open(0).read().split()) N tmp = V[:k] + V[right:] V[:k] + V[right:] tmp put_out_cnt -= 1 1 put_out_cnt ans = 0 0 ans
from heapq import heappop, heapify


N, K, *V = map(int, open(0).read().split())

# i: 操作回数
# j: 取り出す個数
# k: 左から取り出す範囲 [0, k)
ans = 0
for i in range(K + 1):
    for j in range(min(N + 1, i + 1)):
        for k in range(j + 1):
            right = N - j + k
            tmp = V[:k] + V[right:]

            put_out_cnt = i - j
            heapify(tmp)
            while tmp and put_out_cnt > 0:
                heappop(tmp)
                put_out_cnt -= 1
            ans = max(ans, sum(tmp))
print(ans)
