import sys
import bisect
N, K = map(int, sys.stdin.readline().split())
V = list(map(int, sys.stdin.readline().split()))

# 累積和を計算しとく
sum_list = [V[0]]
for i in range(1, N):
    add = V[i] + sum_list[-1]
    sum_list.append(add)

ans = 0

max_take = min(N, K)

for A in range(max_take + 1):
    for B in range(max_take - A + 1):
        if A > 0 and B > 0:
            list = V[:A] + V[N - B:]
            total_sum = sum(list) #sum_list[A - 1] + (sum_list[N - 1] - sum_list[N - 1 - B])
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        elif A == 0 and B > 0:
            list = V[N - B:]
            total_sum = sum(list) #sum_list[N - 1] - sum_list[N - 1 - B]
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        elif A > 0 and B == 0:
            list = V[:A]
            total_sum = sum(list) #sum_list[A - 1]
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        else:
            continue
        
        #print("A = " + str(A) + " B = " + str(B))
        #print(total_sum)
        ans=max(ans,total_sum)
            
print(ans) import sys sys import bisect bisect N, K = map(int, sys.stdin.readline().split()) N map(int, sys.stdin.readline().split()) map int sys.stdin.readline().split() sys.stdin.readline().split sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline split K V = list(map(int, sys.stdin.readline().split())) V list(map(int, sys.stdin.readline().split())) list map(int, sys.stdin.readline().split()) map int sys.stdin.readline().split() sys.stdin.readline().split sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline split sum_list = [V[0]] sum_list [V[0]] V[0] V 0 for i in range(1, N):
    add = V[i] + sum_list[-1]
    sum_list.append(add) i range(1, N) range 1 N add = V[i] + sum_list[-1] add V[i] + sum_list[-1] V[i] V i sum_list[-1] sum_list -1 sum_list.append(add) sum_list.append sum_list append add ans = 0 ans 0 max_take = min(N, K) max_take min(N, K) min N K for A in range(max_take + 1):
    for B in range(max_take - A + 1):
        if A > 0 and B > 0:
            list = V[:A] + V[N - B:]
            total_sum = sum(list) #sum_list[A - 1] + (sum_list[N - 1] - sum_list[N - 1 - B])
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        elif A == 0 and B > 0:
            list = V[N - B:]
            total_sum = sum(list) #sum_list[N - 1] - sum_list[N - 1 - B]
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        elif A > 0 and B == 0:
            list = V[:A]
            total_sum = sum(list) #sum_list[A - 1]
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        else:
            continue
        
        #print("A = " + str(A) + " B = " + str(B))
        #print(total_sum)
        ans=max(ans,total_sum)
             A range(max_take + 1) range max_take + 1 max_take 1 for B in range(max_take - A + 1):
        if A > 0 and B > 0:
            list = V[:A] + V[N - B:]
            total_sum = sum(list) #sum_list[A - 1] + (sum_list[N - 1] - sum_list[N - 1 - B])
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        elif A == 0 and B > 0:
            list = V[N - B:]
            total_sum = sum(list) #sum_list[N - 1] - sum_list[N - 1 - B]
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        elif A > 0 and B == 0:
            list = V[:A]
            total_sum = sum(list) #sum_list[A - 1]
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        else:
            continue
        
        #print("A = " + str(A) + " B = " + str(B))
        #print(total_sum)
        ans=max(ans,total_sum)
             B range(max_take - A + 1) range max_take - A + 1 max_take - A max_take A 1 if A > 0 and B > 0:
            list = V[:A] + V[N - B:]
            total_sum = sum(list) #sum_list[A - 1] + (sum_list[N - 1] - sum_list[N - 1 - B])
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        elif A == 0 and B > 0:
            list = V[N - B:]
            total_sum = sum(list) #sum_list[N - 1] - sum_list[N - 1 - B]
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        elif A > 0 and B == 0:
            list = V[:A]
            total_sum = sum(list) #sum_list[A - 1]
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        else:
            continue
        
        #print("A = " + str(A) + " B = " + str(B))
        #print(total_sum)
         A > 0 and B > 0 A > 0 A 0 B > 0 B 0 list = V[:A] + V[N - B:] list V[:A] + V[N - B:] V[:A] V :A A V[N - B:] V N - B: N - B N B total_sum = sum(list) total_sum sum(list) sum list list.sort() list.sort list sort n = bisect.bisect_left(list, 0) n bisect.bisect_left(list, 0) bisect.bisect_left bisect bisect_left list 0 if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
         n <= K - (A + B) n K - (A + B) K A + B A B total_sum -= sum(list[:n]) total_sum sum(list[:n]) sum list[:n] list :n n total_sum -= sum(list[:K - (A + B)]) total_sum sum(list[:K - (A + B)]) sum list[:K - (A + B)] list :K - (A + B) K - (A + B) K A + B A B elif A == 0 and B > 0:
            list = V[N - B:]
            total_sum = sum(list) #sum_list[N - 1] - sum_list[N - 1 - B]
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
         A == 0 and B > 0 A == 0 A 0 B > 0 B 0 list = V[N - B:] list V[N - B:] V N - B: N - B N B total_sum = sum(list) total_sum sum(list) sum list list.sort() list.sort list sort n = bisect.bisect_left(list, 0) n bisect.bisect_left(list, 0) bisect.bisect_left bisect bisect_left list 0 if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
         n <= K - (A + B) n K - (A + B) K A + B A B total_sum -= sum(list[:n]) total_sum sum(list[:n]) sum list[:n] list :n n total_sum -= sum(list[:K - (A + B)]) total_sum sum(list[:K - (A + B)]) sum list[:K - (A + B)] list :K - (A + B) K - (A + B) K A + B A B elif A > 0 and B == 0:
            list = V[:A]
            total_sum = sum(list) #sum_list[A - 1]
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
         A > 0 and B == 0 A > 0 A 0 B == 0 B 0 list = V[:A] list V[:A] V :A A total_sum = sum(list) total_sum sum(list) sum list list.sort() list.sort list sort n = bisect.bisect_left(list, 0) n bisect.bisect_left(list, 0) bisect.bisect_left bisect bisect_left list 0 if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
         n <= K - (A + B) n K - (A + B) K A + B A B total_sum -= sum(list[:n]) total_sum sum(list[:n]) sum list[:n] list :n n total_sum -= sum(list[:K - (A + B)]) total_sum sum(list[:K - (A + B)]) sum list[:K - (A + B)] list :K - (A + B) K - (A + B) K A + B A B continue ans=max(ans,total_sum) ans max(ans,total_sum) max ans total_sum print(ans) print ans n = bisect.bisect_left(list, 0) bisect.bisect_left(list, 0) n total_sum = sum(list) sum(list) total_sum N, K = map(int, sys.stdin.readline().split()) map(int, sys.stdin.readline().split()) N V = list(map(int, sys.stdin.readline().split())) list(map(int, sys.stdin.readline().split())) V add = V[i] + sum_list[-1] V[i] + sum_list[-1] add sum_list = [V[0]] [V[0]] sum_list list = V[:A] + V[N - B:] V[:A] + V[N - B:] list list = V[:A] V[:A] list ans=max(ans,total_sum) max(ans,total_sum) ans list = V[N - B:] V[N - B:] list total_sum -= sum(list[:K - (A + B)]) sum(list[:K - (A + B)]) total_sum n = bisect.bisect_left(list, 0) bisect.bisect_left(list, 0) n ans = 0 0 ans total_sum -= sum(list[:K - (A + B)]) sum(list[:K - (A + B)]) total_sum total_sum -= sum(list[:n]) sum(list[:n]) total_sum total_sum -= sum(list[:K - (A + B)]) sum(list[:K - (A + B)]) total_sum total_sum -= sum(list[:n]) sum(list[:n]) total_sum total_sum = sum(list) sum(list) total_sum total_sum -= sum(list[:n]) sum(list[:n]) total_sum total_sum = sum(list) sum(list) total_sum n = bisect.bisect_left(list, 0) bisect.bisect_left(list, 0) n max_take = min(N, K) min(N, K) max_take K = map(int, sys.stdin.readline().split()) map(int, sys.stdin.readline().split()) K
import sys
import bisect
N, K = map(int, sys.stdin.readline().split())
V = list(map(int, sys.stdin.readline().split()))

# 累積和を計算しとく
sum_list = [V[0]]
for i in range(1, N):
    add = V[i] + sum_list[-1]
    sum_list.append(add)

ans = 0

max_take = min(N, K)

for A in range(max_take + 1):
    for B in range(max_take - A + 1):
        if A > 0 and B > 0:
            list = V[:A] + V[N - B:]
            total_sum = sum(list) #sum_list[A - 1] + (sum_list[N - 1] - sum_list[N - 1 - B])
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        elif A == 0 and B > 0:
            list = V[N - B:]
            total_sum = sum(list) #sum_list[N - 1] - sum_list[N - 1 - B]
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        elif A > 0 and B == 0:
            list = V[:A]
            total_sum = sum(list) #sum_list[A - 1]
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        else:
            continue
        
        #print("A = " + str(A) + " B = " + str(B))
        #print(total_sum)
        ans=max(ans,total_sum)
            
print(ans)
