import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from heapq import heapify, heappop, heappush 
def main():
    N, K = map(int, readline().split())
    V = [int(i) for i in readline().split()]
    A = V[::-1]
    ans = 0
    for l in range(K+1):
        for r in range(K+1-l):
            if l+r>N:
                continue
            rest = K-(l+r)
            hp = V[:l] + A[:r]
            heapify(hp)
            for _ in range(rest):
                if len(hp)==0:
                    break
                v = heappop(hp)
                if v > 0:
                    heappush(hp, v)
            ans = max(ans, sum(hp))
    print(ans)
if __name__ == '__main__':
    main() import sys sys read = sys.stdin.buffer.read read sys.stdin.buffer.read sys.stdin.buffer sys.stdin sys stdin buffer read readline = sys.stdin.buffer.readline readline sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline readlines = sys.stdin.buffer.readlines readlines sys.stdin.buffer.readlines sys.stdin.buffer sys.stdin sys stdin buffer readlines from heapq import heapify, heappop, heappush def main():
    N, K = map(int, readline().split())
    V = [int(i) for i in readline().split()]
    A = V[::-1]
    ans = 0
    for l in range(K+1):
        for r in range(K+1-l):
            if l+r>N:
                continue
            rest = K-(l+r)
            hp = V[:l] + A[:r]
            heapify(hp)
            for _ in range(rest):
                if len(hp)==0:
                    break
                v = heappop(hp)
                if v > 0:
                    heappush(hp, v)
            ans = max(ans, sum(hp))
    print(ans) main N, K = map(int, readline().split()) N map(int, readline().split()) map int readline().split() ().split () readline split K int(i) for i in readline().split() for i in readline().split() i readline().split() ().split () readline split for i in readline().split() int(i) int i V = [int(i) for i in readline().split()] V [int(i) for i in readline().split()] A = V[::-1] A V[::-1] V ::-1 -1 ans = 0 ans 0 for l in range(K+1):
        for r in range(K+1-l):
            if l+r>N:
                continue
            rest = K-(l+r)
            hp = V[:l] + A[:r]
            heapify(hp)
            for _ in range(rest):
                if len(hp)==0:
                    break
                v = heappop(hp)
                if v > 0:
                    heappush(hp, v)
            ans = max(ans, sum(hp))
     l range(K+1) range K+1 K 1 for r in range(K+1-l):
            if l+r>N:
                continue
            rest = K-(l+r)
            hp = V[:l] + A[:r]
            heapify(hp)
            for _ in range(rest):
                if len(hp)==0:
                    break
                v = heappop(hp)
                if v > 0:
                    heappush(hp, v)
            ans = max(ans, sum(hp))
     r range(K+1-l) range K+1-l K+1 K 1 l if l+r>N:
                continue
             l+r>N l+r l r N continue rest = K-(l+r) rest K-(l+r) K l+r l r hp = V[:l] + A[:r] hp V[:l] + A[:r] V[:l] V :l l A[:r] A :r r heapify(hp) heapify hp for _ in range(rest):
                if len(hp)==0:
                    break
                v = heappop(hp)
                if v > 0:
                    heappush(hp, v)
             _ range(rest) range rest if len(hp)==0:
                    break
                 len(hp)==0 len(hp) len hp 0 break v = heappop(hp) v heappop(hp) heappop hp if v > 0:
                    heappush(hp, v)
             v > 0 v 0 heappush(hp, v) heappush hp v ans = max(ans, sum(hp)) ans max(ans, sum(hp)) max ans sum(hp) sum hp print(ans) print ans if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    N, K = map(int, readline().split())
    V = [int(i) for i in readline().split()]
    A = V[::-1]
    ans = 0
    for l in range(K+1):
        for r in range(K+1-l):
            if l+r>N:
                continue
            rest = K-(l+r)
            hp = V[:l] + A[:r]
            heapify(hp)
            for _ in range(rest):
                if len(hp)==0:
                    break
                v = heappop(hp)
                if v > 0:
                    heappush(hp, v)
            ans = max(ans, sum(hp))
    print(ans) def main():
    N, K = map(int, readline().split())
    V = [int(i) for i in readline().split()]
    A = V[::-1]
    ans = 0
    for l in range(K+1):
        for r in range(K+1-l):
            if l+r>N:
                continue
            rest = K-(l+r)
            hp = V[:l] + A[:r]
            heapify(hp)
            for _ in range(rest):
                if len(hp)==0:
                    break
                v = heappop(hp)
                if v > 0:
                    heappush(hp, v)
            ans = max(ans, sum(hp))
    print(ans) main readlines = sys.stdin.buffer.readlines sys.stdin.buffer.readlines readlines readline = sys.stdin.buffer.readline sys.stdin.buffer.readline readline read = sys.stdin.buffer.read sys.stdin.buffer.read read
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from heapq import heapify, heappop, heappush 
def main():
    N, K = map(int, readline().split())
    V = [int(i) for i in readline().split()]
    A = V[::-1]
    ans = 0
    for l in range(K+1):
        for r in range(K+1-l):
            if l+r>N:
                continue
            rest = K-(l+r)
            hp = V[:l] + A[:r]
            heapify(hp)
            for _ in range(rest):
                if len(hp)==0:
                    break
                v = heappop(hp)
                if v > 0:
                    heappush(hp, v)
            ans = max(ans, sum(hp))
    print(ans)
if __name__ == '__main__':
    main()
