#!/usr/bin/env python3
import sys
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

sys.setrecursionlimit(10**6)
INF = 10**12
m = 10**9 + 7

def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))

    # 方針: 操作Aの回数・操作Bの回数で全探索
    ans = 0
    R = min(N,K)

    for i in range(R+1):
        for j in range(R+1-i):
            tmp = 0
            q = []# 価値が負の宝石をいれる(C)
            
            for k in range(i):
                tmp += V[k]
                if V[k] < 0:
                    q.append(V[k])
            for k in range(j):
                tmp += V[N-1-k]
                if V[N-1-k] < 0:
                    q.append(V[N-1-k])

            q.sort()
            tmp -= sum(q[:min(len(q),K-i-j)])
            ans = max(ans,tmp)
    print(ans)

if __name__ == "__main__":
    main() import sys sys from collections import deque, Counter from heapq import heappop, heappush from bisect import bisect_right from itertools import accumulate sys.setrecursionlimit(10**6) sys.setrecursionlimit sys setrecursionlimit 10**6 10 6 INF = 10**12 INF 10**12 10 12 m = 10**9 + 7 m 10**9 + 7 10**9 10 9 7 def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))

    # 方針: 操作Aの回数・操作Bの回数で全探索
    ans = 0
    R = min(N,K)

    for i in range(R+1):
        for j in range(R+1-i):
            tmp = 0
            q = []# 価値が負の宝石をいれる(C)
            
            for k in range(i):
                tmp += V[k]
                if V[k] < 0:
                    q.append(V[k])
            for k in range(j):
                tmp += V[N-1-k]
                if V[N-1-k] < 0:
                    q.append(V[N-1-k])

            q.sort()
            tmp -= sum(q[:min(len(q),K-i-j)])
            ans = max(ans,tmp)
    print(ans) main 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 = 0 ans 0 R = min(N,K) R min(N,K) min N K for i in range(R+1):
        for j in range(R+1-i):
            tmp = 0
            q = []# 価値が負の宝石をいれる(C)
            
            for k in range(i):
                tmp += V[k]
                if V[k] < 0:
                    q.append(V[k])
            for k in range(j):
                tmp += V[N-1-k]
                if V[N-1-k] < 0:
                    q.append(V[N-1-k])

            q.sort()
            tmp -= sum(q[:min(len(q),K-i-j)])
            ans = max(ans,tmp)
     i range(R+1) range R+1 R 1 for j in range(R+1-i):
            tmp = 0
            q = []# 価値が負の宝石をいれる(C)
            
            for k in range(i):
                tmp += V[k]
                if V[k] < 0:
                    q.append(V[k])
            for k in range(j):
                tmp += V[N-1-k]
                if V[N-1-k] < 0:
                    q.append(V[N-1-k])

            q.sort()
            tmp -= sum(q[:min(len(q),K-i-j)])
            ans = max(ans,tmp)
     j range(R+1-i) range R+1-i R+1 R 1 i tmp = 0 tmp 0 q = [] q [] for k in range(i):
                tmp += V[k]
                if V[k] < 0:
                    q.append(V[k])
             k range(i) range i tmp += V[k] tmp V[k] V k if V[k] < 0:
                    q.append(V[k])
             V[k] < 0 V[k] V k 0 q.append(V[k]) q.append q append V[k] V k for k in range(j):
                tmp += V[N-1-k]
                if V[N-1-k] < 0:
                    q.append(V[N-1-k])

             k range(j) range j tmp += V[N-1-k] tmp V[N-1-k] V N-1-k N-1 N 1 k if V[N-1-k] < 0:
                    q.append(V[N-1-k])

             V[N-1-k] < 0 V[N-1-k] V N-1-k N-1 N 1 k 0 q.append(V[N-1-k]) q.append q append V[N-1-k] V N-1-k N-1 N 1 k q.sort() q.sort q sort tmp -= sum(q[:min(len(q),K-i-j)]) tmp sum(q[:min(len(q),K-i-j)]) sum q[:min(len(q),K-i-j)] q :min(len(q),K-i-j) min(len(q),K-i-j) min len(q) len q K-i-j K-i K i j ans = max(ans,tmp) ans max(ans,tmp) max ans tmp print(ans) print ans if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main INF = 10**12 10**12 INF m = 10**9 + 7 10**9 + 7 m def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))

    # 方針: 操作Aの回数・操作Bの回数で全探索
    ans = 0
    R = min(N,K)

    for i in range(R+1):
        for j in range(R+1-i):
            tmp = 0
            q = []# 価値が負の宝石をいれる(C)
            
            for k in range(i):
                tmp += V[k]
                if V[k] < 0:
                    q.append(V[k])
            for k in range(j):
                tmp += V[N-1-k]
                if V[N-1-k] < 0:
                    q.append(V[N-1-k])

            q.sort()
            tmp -= sum(q[:min(len(q),K-i-j)])
            ans = max(ans,tmp)
    print(ans) def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))

    # 方針: 操作Aの回数・操作Bの回数で全探索
    ans = 0
    R = min(N,K)

    for i in range(R+1):
        for j in range(R+1-i):
            tmp = 0
            q = []# 価値が負の宝石をいれる(C)
            
            for k in range(i):
                tmp += V[k]
                if V[k] < 0:
                    q.append(V[k])
            for k in range(j):
                tmp += V[N-1-k]
                if V[N-1-k] < 0:
                    q.append(V[N-1-k])

            q.sort()
            tmp -= sum(q[:min(len(q),K-i-j)])
            ans = max(ans,tmp)
    print(ans) main
#!/usr/bin/env python3
import sys
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

sys.setrecursionlimit(10**6)
INF = 10**12
m = 10**9 + 7

def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))

    # 方針: 操作Aの回数・操作Bの回数で全探索
    ans = 0
    R = min(N,K)

    for i in range(R+1):
        for j in range(R+1-i):
            tmp = 0
            q = []# 価値が負の宝石をいれる(C)
            
            for k in range(i):
                tmp += V[k]
                if V[k] < 0:
                    q.append(V[k])
            for k in range(j):
                tmp += V[N-1-k]
                if V[N-1-k] < 0:
                    q.append(V[N-1-k])

            q.sort()
            tmp -= sum(q[:min(len(q),K-i-j)])
            ans = max(ans,tmp)
    print(ans)

if __name__ == "__main__":
    main()
