import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(min(n, k) + 1):
    have = []
    for i in range(a):
        have.append(v[i])

    o_have = copy.deepcopy(have)

    for b in range(min(n, k) - a + 1):
        for i in range(b):
            have.append(v[n-i-1])

        heapq.heapify(have)
        # print(1, have)
        for c in range(k - a - b):
            if len(have) <= 0:
                break
            item = heapq.heappop(have)
            # print(c, item, have)
            if item >= 0:
                have.append(item)
                break
        ans = max(ans, sum(have))
        # print(2, have, ans, a, b)
        have = copy.deepcopy(o_have)

print(ans) import heapq, copy heapq copy 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 = -10 ** 10 ans -10 ** 10 -10 10 for a in range(min(n, k) + 1):
    have = []
    for i in range(a):
        have.append(v[i])

    o_have = copy.deepcopy(have)

    for b in range(min(n, k) - a + 1):
        for i in range(b):
            have.append(v[n-i-1])

        heapq.heapify(have)
        # print(1, have)
        for c in range(k - a - b):
            if len(have) <= 0:
                break
            item = heapq.heappop(have)
            # print(c, item, have)
            if item >= 0:
                have.append(item)
                break
        ans = max(ans, sum(have))
        # print(2, have, ans, a, b)
        have = copy.deepcopy(o_have) a range(min(n, k) + 1) range min(n, k) + 1 min(n, k) min n k 1 have = [] have [] for i in range(a):
        have.append(v[i])

     i range(a) range a have.append(v[i]) have.append have append v[i] v i o_have = copy.deepcopy(have) o_have copy.deepcopy(have) copy.deepcopy copy deepcopy have for b in range(min(n, k) - a + 1):
        for i in range(b):
            have.append(v[n-i-1])

        heapq.heapify(have)
        # print(1, have)
        for c in range(k - a - b):
            if len(have) <= 0:
                break
            item = heapq.heappop(have)
            # print(c, item, have)
            if item >= 0:
                have.append(item)
                break
        ans = max(ans, sum(have))
        # print(2, have, ans, a, b)
        have = copy.deepcopy(o_have) b range(min(n, k) - a + 1) range min(n, k) - a + 1 min(n, k) - a min(n, k) min n k a 1 for i in range(b):
            have.append(v[n-i-1])

         i range(b) range b have.append(v[n-i-1]) have.append have append v[n-i-1] v n-i-1 n-i n i 1 heapq.heapify(have) heapq.heapify heapq heapify have for c in range(k - a - b):
            if len(have) <= 0:
                break
            item = heapq.heappop(have)
            # print(c, item, have)
            if item >= 0:
                have.append(item)
                break
         c range(k - a - b) range k - a - b k - a k a b if len(have) <= 0:
                break
             len(have) <= 0 len(have) len have 0 break item = heapq.heappop(have) item heapq.heappop(have) heapq.heappop heapq heappop have if item >= 0:
                have.append(item)
                break
         item >= 0 item 0 have.append(item) have.append have append item break ans = max(ans, sum(have)) ans max(ans, sum(have)) max ans sum(have) sum have have = copy.deepcopy(o_have) have copy.deepcopy(o_have) copy.deepcopy copy deepcopy o_have print(ans) print ans have = [] [] have item = heapq.heappop(have) heapq.heappop(have) item n, k = map(int, input().split()) map(int, input().split()) n v = list(map(int, input().split())) list(map(int, input().split())) v ans = -10 ** 10 -10 ** 10 ans ans = max(ans, sum(have)) max(ans, sum(have)) ans have = copy.deepcopy(o_have) copy.deepcopy(o_have) have k = map(int, input().split()) map(int, input().split()) k o_have = copy.deepcopy(have) copy.deepcopy(have) o_have
import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(min(n, k) + 1):
    have = []
    for i in range(a):
        have.append(v[i])

    o_have = copy.deepcopy(have)

    for b in range(min(n, k) - a + 1):
        for i in range(b):
            have.append(v[n-i-1])

        heapq.heapify(have)
        # print(1, have)
        for c in range(k - a - b):
            if len(have) <= 0:
                break
            item = heapq.heappop(have)
            # print(c, item, have)
            if item >= 0:
                have.append(item)
                break
        ans = max(ans, sum(have))
        # print(2, have, ans, a, b)
        have = copy.deepcopy(o_have)

print(ans)
