import heapq

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

V = list(map(int, input().split()))

ans = 0
for i in range(1, K+1):
    if i > N:
        continue
    for j in range(i+1):
        tmp = 0
        q = V[:j] + V[N-i+j:]
        heapq.heapify(q)
        for _ in range(min(i, K-i)):
            tmp_min = heapq.heappop(q)
            ans = max(ans, sum(q))
            if tmp_min > 0:
                ans = max(ans, sum(q)+tmp_min)
                break
        ans = max(ans, sum(q))

print(ans) import heapq heapq N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K V = list(map(int, input().split())) V list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split ans = 0 ans 0 for i in range(1, K+1):
    if i > N:
        continue
    for j in range(i+1):
        tmp = 0
        q = V[:j] + V[N-i+j:]
        heapq.heapify(q)
        for _ in range(min(i, K-i)):
            tmp_min = heapq.heappop(q)
            ans = max(ans, sum(q))
            if tmp_min > 0:
                ans = max(ans, sum(q)+tmp_min)
                break
        ans = max(ans, sum(q)) i range(1, K+1) range 1 K+1 K 1 if i > N:
        continue
     i > N i N continue for j in range(i+1):
        tmp = 0
        q = V[:j] + V[N-i+j:]
        heapq.heapify(q)
        for _ in range(min(i, K-i)):
            tmp_min = heapq.heappop(q)
            ans = max(ans, sum(q))
            if tmp_min > 0:
                ans = max(ans, sum(q)+tmp_min)
                break
        ans = max(ans, sum(q)) j range(i+1) range i+1 i 1 tmp = 0 tmp 0 q = V[:j] + V[N-i+j:] q V[:j] + V[N-i+j:] V[:j] V :j j V[N-i+j:] V N-i+j: N-i+j N-i N i j heapq.heapify(q) heapq.heapify heapq heapify q for _ in range(min(i, K-i)):
            tmp_min = heapq.heappop(q)
            ans = max(ans, sum(q))
            if tmp_min > 0:
                ans = max(ans, sum(q)+tmp_min)
                break
         _ range(min(i, K-i)) range min(i, K-i) min i K-i K i tmp_min = heapq.heappop(q) tmp_min heapq.heappop(q) heapq.heappop heapq heappop q ans = max(ans, sum(q)) ans max(ans, sum(q)) max ans sum(q) sum q if tmp_min > 0:
                ans = max(ans, sum(q)+tmp_min)
                break
         tmp_min > 0 tmp_min 0 ans = max(ans, sum(q)+tmp_min) ans max(ans, sum(q)+tmp_min) max ans sum(q)+tmp_min sum(q) sum q tmp_min break ans = max(ans, sum(q)) ans max(ans, sum(q)) max ans sum(q) sum q print(ans) print ans q = V[:j] + V[N-i+j:] V[:j] + V[N-i+j:] q ans = max(ans, sum(q)) max(ans, sum(q)) ans ans = max(ans, sum(q)) max(ans, sum(q)) ans V = list(map(int, input().split())) list(map(int, input().split())) V tmp = 0 0 tmp ans = 0 0 ans N, K = map(int, input().split()) map(int, input().split()) N K = map(int, input().split()) map(int, input().split()) K ans = max(ans, sum(q)+tmp_min) max(ans, sum(q)+tmp_min) ans tmp_min = heapq.heappop(q) heapq.heappop(q) tmp_min
import heapq

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

V = list(map(int, input().split()))

ans = 0
for i in range(1, K+1):
    if i > N:
        continue
    for j in range(i+1):
        tmp = 0
        q = V[:j] + V[N-i+j:]
        heapq.heapify(q)
        for _ in range(min(i, K-i)):
            tmp_min = heapq.heappop(q)
            ans = max(ans, sum(q))
            if tmp_min > 0:
                ans = max(ans, sum(q)+tmp_min)
                break
        ans = max(ans, sum(q))

print(ans)