import heapq

N, K = map(int, input().split())
V = list(map(int, input().split()))
max_cnt = min(N, K)
ans = 0

for a in range(0, max_cnt + 1):
    for b in range(0, max_cnt + 1):
        if a + b > max_cnt:
            break
        if a == b == 0:
            continue
        hoseki = V[:a] + V[::-1][:b]
        heapq.heapify(hoseki)
        tmp = K - (a + b)
        while tmp and len(hoseki):
            my_min = heapq.heappop(hoseki)
            if my_min >= 0:
                heapq.heappush(hoseki, my_min)
                break
            tmp -= 1
        ans = max(ans, sum(hoseki))

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 max_cnt = min(N, K) max_cnt min(N, K) min N K ans = 0 ans 0 for a in range(0, max_cnt + 1):
    for b in range(0, max_cnt + 1):
        if a + b > max_cnt:
            break
        if a == b == 0:
            continue
        hoseki = V[:a] + V[::-1][:b]
        heapq.heapify(hoseki)
        tmp = K - (a + b)
        while tmp and len(hoseki):
            my_min = heapq.heappop(hoseki)
            if my_min >= 0:
                heapq.heappush(hoseki, my_min)
                break
            tmp -= 1
        ans = max(ans, sum(hoseki)) a range(0, max_cnt + 1) range 0 max_cnt + 1 max_cnt 1 for b in range(0, max_cnt + 1):
        if a + b > max_cnt:
            break
        if a == b == 0:
            continue
        hoseki = V[:a] + V[::-1][:b]
        heapq.heapify(hoseki)
        tmp = K - (a + b)
        while tmp and len(hoseki):
            my_min = heapq.heappop(hoseki)
            if my_min >= 0:
                heapq.heappush(hoseki, my_min)
                break
            tmp -= 1
        ans = max(ans, sum(hoseki)) b range(0, max_cnt + 1) range 0 max_cnt + 1 max_cnt 1 if a + b > max_cnt:
            break
         a + b > max_cnt a + b a b max_cnt break if a == b == 0:
            continue
         a == b == 0 a == b a b 0 continue hoseki = V[:a] + V[::-1][:b] hoseki V[:a] + V[::-1][:b] V[:a] V :a a V[::-1][:b] [::-1] V ::-1 -1 :b b heapq.heapify(hoseki) heapq.heapify heapq heapify hoseki tmp = K - (a + b) tmp K - (a + b) K a + b a b while tmp and len(hoseki):
            my_min = heapq.heappop(hoseki)
            if my_min >= 0:
                heapq.heappush(hoseki, my_min)
                break
            tmp -= 1
         tmp and len(hoseki) tmp len(hoseki) len hoseki my_min = heapq.heappop(hoseki) my_min heapq.heappop(hoseki) heapq.heappop heapq heappop hoseki if my_min >= 0:
                heapq.heappush(hoseki, my_min)
                break
             my_min >= 0 my_min 0 heapq.heappush(hoseki, my_min) heapq.heappush heapq heappush hoseki my_min break tmp -= 1 tmp 1 ans = max(ans, sum(hoseki)) ans max(ans, sum(hoseki)) max ans sum(hoseki) sum hoseki print(ans) print ans V = list(map(int, input().split())) list(map(int, input().split())) V hoseki = V[:a] + V[::-1][:b] V[:a] + V[::-1][:b] hoseki ans = 0 0 ans ans = max(ans, sum(hoseki)) max(ans, sum(hoseki)) ans tmp -= 1 1 tmp my_min = heapq.heappop(hoseki) heapq.heappop(hoseki) my_min K = map(int, input().split()) map(int, input().split()) K tmp = K - (a + b) K - (a + b) tmp N, K = map(int, input().split()) map(int, input().split()) N max_cnt = min(N, K) min(N, K) max_cnt
import heapq

N, K = map(int, input().split())
V = list(map(int, input().split()))
max_cnt = min(N, K)
ans = 0

for a in range(0, max_cnt + 1):
    for b in range(0, max_cnt + 1):
        if a + b > max_cnt:
            break
        if a == b == 0:
            continue
        hoseki = V[:a] + V[::-1][:b]
        heapq.heapify(hoseki)
        tmp = K - (a + b)
        while tmp and len(hoseki):
            my_min = heapq.heappop(hoseki)
            if my_min >= 0:
                heapq.heappush(hoseki, my_min)
                break
            tmp -= 1
        ans = max(ans, sum(hoseki))

print(ans)