# coding: utf-8
import sys
sys.setrecursionlimit(10 ** 7)

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# 持っている宝石をDに詰めるのは最後
N, K = lr()
V = lr()
answer = 0
V = V[::-1] + V[::-1]
for x in range(K+1):
    # x回Dから取り出す、K-x回戻すことが可能
    x = min(x, N)
    for i in range(N-1, N-1+x+1):
        take = V[i-x+1:i+1]
        take.sort(reverse=True)
        result = sum(take)
        if result > answer:
            answer = result
        for j in range(K-x):
            if take:
                result -= take.pop()
                if result > answer:
                    answer = result

print(answer) import sys sys sys.setrecursionlimit(10 ** 7) sys.setrecursionlimit sys setrecursionlimit 10 ** 7 10 7 sr = lambda: sys.stdin.readline().rstrip() sr lambda: sys.stdin.readline().rstrip() sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip ir = lambda: int(sr()) ir lambda: int(sr()) int(sr()) int sr() sr lr = lambda: list(map(int, sr().split())) lr lambda: list(map(int, sr().split())) list(map(int, sr().split())) list map(int, sr().split()) map int sr().split() ().split () sr split N, K = lr() N lr() lr K V = lr() V lr() lr answer = 0 answer 0 V = V[::-1] + V[::-1] V V[::-1] + V[::-1] V[::-1] V ::-1 -1 V[::-1] V ::-1 -1 for x in range(K+1):
    # x回Dから取り出す、K-x回戻すことが可能
    x = min(x, N)
    for i in range(N-1, N-1+x+1):
        take = V[i-x+1:i+1]
        take.sort(reverse=True)
        result = sum(take)
        if result > answer:
            answer = result
        for j in range(K-x):
            if take:
                result -= take.pop()
                if result > answer:
                    answer = result x range(K+1) range K+1 K 1 x = min(x, N) x min(x, N) min x N for i in range(N-1, N-1+x+1):
        take = V[i-x+1:i+1]
        take.sort(reverse=True)
        result = sum(take)
        if result > answer:
            answer = result
        for j in range(K-x):
            if take:
                result -= take.pop()
                if result > answer:
                    answer = result i range(N-1, N-1+x+1) range N-1 N 1 N-1+x+1 N-1+x N-1 N 1 x 1 take = V[i-x+1:i+1] take V[i-x+1:i+1] V i-x+1:i+1 i-x+1 i-x i x 1 i+1 i 1 take.sort(reverse=True) take.sort take sort reverse=True reverse True result = sum(take) result sum(take) sum take if result > answer:
            answer = result
         result > answer result answer answer = result answer result for j in range(K-x):
            if take:
                result -= take.pop()
                if result > answer:
                    answer = result j range(K-x) range K-x K x if take:
                result -= take.pop()
                if result > answer:
                    answer = result take result -= take.pop() result take.pop() take.pop take pop if result > answer:
                    answer = result result > answer result answer answer = result answer result print(answer) print answer V = V[::-1] + V[::-1] V[::-1] + V[::-1] V result = sum(take) sum(take) result sr = lambda: sys.stdin.readline().rstrip() lambda: sys.stdin.readline().rstrip() sr lr = lambda: list(map(int, sr().split())) lambda: list(map(int, sr().split())) lr x = min(x, N) min(x, N) x answer = 0 0 answer answer = result result answer N, K = lr() lr() N V = lr() lr() V take = V[i-x+1:i+1] V[i-x+1:i+1] take answer = result result answer result -= take.pop() take.pop() result K = lr() lr() K ir = lambda: int(sr()) lambda: int(sr()) ir
# coding: utf-8
import sys
sys.setrecursionlimit(10 ** 7)

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# 持っている宝石をDに詰めるのは最後
N, K = lr()
V = lr()
answer = 0
V = V[::-1] + V[::-1]
for x in range(K+1):
    # x回Dから取り出す、K-x回戻すことが可能
    x = min(x, N)
    for i in range(N-1, N-1+x+1):
        take = V[i-x+1:i+1]
        take.sort(reverse=True)
        result = sum(take)
        if result > answer:
            answer = result
        for j in range(K-x):
            if take:
                result -= take.pop()
                if result > answer:
                    answer = result

print(answer)
