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):
    for b in range(min(n, k) - a + 1):
        have = []
        for i in range(a):
            have.append(v[i])
        for i in range(b):
            have.append(v[-i-1])

        tmp = sum(have)
        # heapq.heapify(have)
        have.sort()
        ans = max(ans, tmp)

        for c in range(k - a - b):
            if c >= len(have):
                break
            item = have[c]
            if item >= 0:
                break

            tmp -= item
            ans = max(ans, tmp)

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):
    for b in range(min(n, k) - a + 1):
        have = []
        for i in range(a):
            have.append(v[i])
        for i in range(b):
            have.append(v[-i-1])

        tmp = sum(have)
        # heapq.heapify(have)
        have.sort()
        ans = max(ans, tmp)

        for c in range(k - a - b):
            if c >= len(have):
                break
            item = have[c]
            if item >= 0:
                break

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

        tmp = sum(have)
        # heapq.heapify(have)
        have.sort()
        ans = max(ans, tmp)

        for c in range(k - a - b):
            if c >= len(have):
                break
            item = have[c]
            if item >= 0:
                break

            tmp -= item
            ans = max(ans, tmp) 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 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 for i in range(b):
            have.append(v[-i-1])

         i range(b) range b have.append(v[-i-1]) have.append have append v[-i-1] v -i-1 -i i 1 tmp = sum(have) tmp sum(have) sum have have.sort() have.sort have sort ans = max(ans, tmp) ans max(ans, tmp) max ans tmp for c in range(k - a - b):
            if c >= len(have):
                break
            item = have[c]
            if item >= 0:
                break

            tmp -= item
            ans = max(ans, tmp) c range(k - a - b) range k - a - b k - a k a b if c >= len(have):
                break
             c >= len(have) c len(have) len have break item = have[c] item have[c] have c if item >= 0:
                break

             item >= 0 item 0 break tmp -= item tmp item ans = max(ans, tmp) ans max(ans, tmp) max ans tmp print(ans) print ans tmp = sum(have) sum(have) tmp ans = max(ans, tmp) max(ans, tmp) ans tmp -= item item tmp ans = -10 ** 10 -10 ** 10 ans k = map(int, input().split()) map(int, input().split()) k have = [] [] have v = list(map(int, input().split())) list(map(int, input().split())) v n, k = map(int, input().split()) map(int, input().split()) n item = have[c] have[c] item ans = max(ans, tmp) max(ans, tmp) ans
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):
    for b in range(min(n, k) - a + 1):
        have = []
        for i in range(a):
            have.append(v[i])
        for i in range(b):
            have.append(v[-i-1])

        tmp = sum(have)
        # heapq.heapify(have)
        have.sort()
        ans = max(ans, tmp)

        for c in range(k - a - b):
            if c >= len(have):
                break
            item = have[c]
            if item >= 0:
                break

            tmp -= item
            ans = max(ans, tmp)

print(ans)
