import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(k + 1):
    for b in range(k - a + 1):
        if a + b > min(k, n):
            break
        # print(a, b)
        have = v[:a] + v[n-b:]
        # print(have)

        tmp = sum(have)
        ans = max(ans, tmp)

        have.sort()

        c = k - a - b

        for item in have:
            if c <= 0:
                break
            c -= 1
            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(k + 1):
    for b in range(k - a + 1):
        if a + b > min(k, n):
            break
        # print(a, b)
        have = v[:a] + v[n-b:]
        # print(have)

        tmp = sum(have)
        ans = max(ans, tmp)

        have.sort()

        c = k - a - b

        for item in have:
            if c <= 0:
                break
            c -= 1
            if item >= 0:
                break

            tmp -= item
            ans = max(ans, tmp) a range(k + 1) range k + 1 k 1 for b in range(k - a + 1):
        if a + b > min(k, n):
            break
        # print(a, b)
        have = v[:a] + v[n-b:]
        # print(have)

        tmp = sum(have)
        ans = max(ans, tmp)

        have.sort()

        c = k - a - b

        for item in have:
            if c <= 0:
                break
            c -= 1
            if item >= 0:
                break

            tmp -= item
            ans = max(ans, tmp) b range(k - a + 1) range k - a + 1 k - a k a 1 if a + b > min(k, n):
            break
        # print(a, b)
         a + b > min(k, n) a + b a b min(k, n) min k n break have = v[:a] + v[n-b:] have v[:a] + v[n-b:] v[:a] v :a a v[n-b:] v n-b: n-b n b tmp = sum(have) tmp sum(have) sum have ans = max(ans, tmp) ans max(ans, tmp) max ans tmp have.sort() have.sort have sort c = k - a - b c k - a - b k - a k a b for item in have:
            if c <= 0:
                break
            c -= 1
            if item >= 0:
                break

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

        tmp = sum(have)
        ans = max(ans, tmp)

        have.sort()

        c = k - a - b

        for item in have:
            if c <= 0:
                break
            c -= 1
            if item >= 0:
                break

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

print(ans)