#!/usr/bin/env python3

import heapq

n, k = list(map(int, input().split()))
v = list(map(int, input().split()))

max_try = min(n, k)
ans = 0

for a in range(0, max_try+1):
    for b in range(0, max_try+1-a):
        c = k-(a+b)
        # print(a, b, c, "", end="")
        # print(a, b, c)
        ans_tmp = 0
        garbage = []
        heapq.heapify(garbage)
        for i in range(a):
            num = v[i]
            ans_tmp += num

            if num < 0:
                heapq.heappush(garbage, num)

        for i in range(b):
            num = v[-1-i]
            ans_tmp += num

            if num < 0:
                heapq.heappush(garbage, num)

        # print(garbage)
        for i in range(c):
            # print(garbage)
            if len(garbage) == 0:
                break
            num = -heapq.heappop(garbage)
            # print(num)
            ans_tmp += num
        # print()
        # print(ans_tmp)
        ans = max(ans, ans_tmp)

print(ans) import heapq heapq n, k = list(map(int, input().split())) n list(map(int, input().split())) list 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_try = min(n, k) max_try min(n, k) min n k ans = 0 ans 0 for a in range(0, max_try+1):
    for b in range(0, max_try+1-a):
        c = k-(a+b)
        # print(a, b, c, "", end="")
        # print(a, b, c)
        ans_tmp = 0
        garbage = []
        heapq.heapify(garbage)
        for i in range(a):
            num = v[i]
            ans_tmp += num

            if num < 0:
                heapq.heappush(garbage, num)

        for i in range(b):
            num = v[-1-i]
            ans_tmp += num

            if num < 0:
                heapq.heappush(garbage, num)

        # print(garbage)
        for i in range(c):
            # print(garbage)
            if len(garbage) == 0:
                break
            num = -heapq.heappop(garbage)
            # print(num)
            ans_tmp += num
        # print()
        # print(ans_tmp)
        ans = max(ans, ans_tmp) a range(0, max_try+1) range 0 max_try+1 max_try 1 for b in range(0, max_try+1-a):
        c = k-(a+b)
        # print(a, b, c, "", end="")
        # print(a, b, c)
        ans_tmp = 0
        garbage = []
        heapq.heapify(garbage)
        for i in range(a):
            num = v[i]
            ans_tmp += num

            if num < 0:
                heapq.heappush(garbage, num)

        for i in range(b):
            num = v[-1-i]
            ans_tmp += num

            if num < 0:
                heapq.heappush(garbage, num)

        # print(garbage)
        for i in range(c):
            # print(garbage)
            if len(garbage) == 0:
                break
            num = -heapq.heappop(garbage)
            # print(num)
            ans_tmp += num
        # print()
        # print(ans_tmp)
        ans = max(ans, ans_tmp) b range(0, max_try+1-a) range 0 max_try+1-a max_try+1 max_try 1 a c = k-(a+b) c k-(a+b) k a+b a b ans_tmp = 0 ans_tmp 0 garbage = [] garbage [] heapq.heapify(garbage) heapq.heapify heapq heapify garbage for i in range(a):
            num = v[i]
            ans_tmp += num

            if num < 0:
                heapq.heappush(garbage, num)

         i range(a) range a num = v[i] num v[i] v i ans_tmp += num ans_tmp num if num < 0:
                heapq.heappush(garbage, num)

         num < 0 num 0 heapq.heappush(garbage, num) heapq.heappush heapq heappush garbage num for i in range(b):
            num = v[-1-i]
            ans_tmp += num

            if num < 0:
                heapq.heappush(garbage, num)

        # print(garbage)
         i range(b) range b num = v[-1-i] num v[-1-i] v -1-i -1 i ans_tmp += num ans_tmp num if num < 0:
                heapq.heappush(garbage, num)

        # print(garbage)
         num < 0 num 0 heapq.heappush(garbage, num) heapq.heappush heapq heappush garbage num for i in range(c):
            # print(garbage)
            if len(garbage) == 0:
                break
            num = -heapq.heappop(garbage)
            # print(num)
            ans_tmp += num
        # print()
        # print(ans_tmp)
         i range(c) range c if len(garbage) == 0:
                break
             len(garbage) == 0 len(garbage) len garbage 0 break num = -heapq.heappop(garbage) num -heapq.heappop(garbage) heapq.heappop(garbage) heapq.heappop heapq heappop garbage ans_tmp += num ans_tmp num ans = max(ans, ans_tmp) ans max(ans, ans_tmp) max ans ans_tmp print(ans) print ans ans = 0 0 ans ans_tmp = 0 0 ans_tmp num = v[-1-i] v[-1-i] num v = list(map(int, input().split())) list(map(int, input().split())) v max_try = min(n, k) min(n, k) max_try num = -heapq.heappop(garbage) -heapq.heappop(garbage) num num = v[i] v[i] num ans_tmp += num num ans_tmp k = list(map(int, input().split())) list(map(int, input().split())) k garbage = [] [] garbage ans_tmp += num num ans_tmp ans = max(ans, ans_tmp) max(ans, ans_tmp) ans n, k = list(map(int, input().split())) list(map(int, input().split())) n ans_tmp += num num ans_tmp c = k-(a+b) k-(a+b) c
#!/usr/bin/env python3

import heapq

n, k = list(map(int, input().split()))
v = list(map(int, input().split()))

max_try = min(n, k)
ans = 0

for a in range(0, max_try+1):
    for b in range(0, max_try+1-a):
        c = k-(a+b)
        # print(a, b, c, "", end="")
        # print(a, b, c)
        ans_tmp = 0
        garbage = []
        heapq.heapify(garbage)
        for i in range(a):
            num = v[i]
            ans_tmp += num

            if num < 0:
                heapq.heappush(garbage, num)

        for i in range(b):
            num = v[-1-i]
            ans_tmp += num

            if num < 0:
                heapq.heappush(garbage, num)

        # print(garbage)
        for i in range(c):
            # print(garbage)
            if len(garbage) == 0:
                break
            num = -heapq.heappop(garbage)
            # print(num)
            ans_tmp += num
        # print()
        # print(ans_tmp)
        ans = max(ans, ans_tmp)

print(ans)
