import sys

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

ans = 0

for i in range(N+1):
    if K < i:
        continue
    score = sum(V[0:i])
    # print("i", i, "l score", score)
    ans = max(ans, score)
    for j in range(N+1):
        if N < i + j or K < i + j:
            continue
        tmp = score
        tmp += sum(V[N-j:])
        ans = max(ans, tmp)
        # print("i", i, "j", j, "lr score", tmp)

        # 捨てる最大個数
        k = K - i - j
        if k < 0:
            continue
        negs = []
        for n in range(0, i):
            if V[n] < 0:
                negs.append(V[n])
        for n in range(N-j, N):
            if V[n] < 0:
                negs.append(V[n])
        negs.sort()
        # print(negs)
        tmp -= sum(negs[:k])
        ans = max(ans, tmp)
        # print("i", i, "j", j, "k", k, "lrd score", tmp)

print(ans) import sys sys 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 ans = 0 ans 0 for i in range(N+1):
    if K < i:
        continue
    score = sum(V[0:i])
    # print("i", i, "l score", score)
    ans = max(ans, score)
    for j in range(N+1):
        if N < i + j or K < i + j:
            continue
        tmp = score
        tmp += sum(V[N-j:])
        ans = max(ans, tmp)
        # print("i", i, "j", j, "lr score", tmp)

        # 捨てる最大個数
        k = K - i - j
        if k < 0:
            continue
        negs = []
        for n in range(0, i):
            if V[n] < 0:
                negs.append(V[n])
        for n in range(N-j, N):
            if V[n] < 0:
                negs.append(V[n])
        negs.sort()
        # print(negs)
        tmp -= sum(negs[:k])
        ans = max(ans, tmp)
        # print("i", i, "j", j, "k", k, "lrd score", tmp) i range(N+1) range N+1 N 1 if K < i:
        continue
     K < i K i continue score = sum(V[0:i]) score sum(V[0:i]) sum V[0:i] V 0:i 0 i ans = max(ans, score) ans max(ans, score) max ans score for j in range(N+1):
        if N < i + j or K < i + j:
            continue
        tmp = score
        tmp += sum(V[N-j:])
        ans = max(ans, tmp)
        # print("i", i, "j", j, "lr score", tmp)

        # 捨てる最大個数
        k = K - i - j
        if k < 0:
            continue
        negs = []
        for n in range(0, i):
            if V[n] < 0:
                negs.append(V[n])
        for n in range(N-j, N):
            if V[n] < 0:
                negs.append(V[n])
        negs.sort()
        # print(negs)
        tmp -= sum(negs[:k])
        ans = max(ans, tmp)
        # print("i", i, "j", j, "k", k, "lrd score", tmp) j range(N+1) range N+1 N 1 if N < i + j or K < i + j:
            continue
         N < i + j or K < i + j N < i + j N i + j i j K < i + j K i + j i j continue tmp = score tmp score tmp += sum(V[N-j:]) tmp sum(V[N-j:]) sum V[N-j:] V N-j: N-j N j ans = max(ans, tmp) ans max(ans, tmp) max ans tmp k = K - i - j k K - i - j K - i K i j if k < 0:
            continue
         k < 0 k 0 continue negs = [] negs [] for n in range(0, i):
            if V[n] < 0:
                negs.append(V[n])
         n range(0, i) range 0 i if V[n] < 0:
                negs.append(V[n])
         V[n] < 0 V[n] V n 0 negs.append(V[n]) negs.append negs append V[n] V n for n in range(N-j, N):
            if V[n] < 0:
                negs.append(V[n])
         n range(N-j, N) range N-j N j N if V[n] < 0:
                negs.append(V[n])
         V[n] < 0 V[n] V n 0 negs.append(V[n]) negs.append negs append V[n] V n negs.sort() negs.sort negs sort tmp -= sum(negs[:k]) tmp sum(negs[:k]) sum negs[:k] negs :k k ans = max(ans, tmp) ans max(ans, tmp) max ans tmp print(ans) print ans negs = [] [] negs tmp += sum(V[N-j:]) sum(V[N-j:]) tmp V = list(map(int, sys.stdin.readline().split())) list(map(int, sys.stdin.readline().split())) V tmp -= sum(negs[:k]) sum(negs[:k]) tmp ans = max(ans, tmp) max(ans, tmp) ans ans = max(ans, score) max(ans, score) ans N, K = map(int, sys.stdin.readline().split()) map(int, sys.stdin.readline().split()) N tmp = score score tmp k = K - i - j K - i - j k K = map(int, sys.stdin.readline().split()) map(int, sys.stdin.readline().split()) K score = sum(V[0:i]) sum(V[0:i]) score ans = max(ans, tmp) max(ans, tmp) ans ans = 0 0 ans
import sys

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

ans = 0

for i in range(N+1):
    if K < i:
        continue
    score = sum(V[0:i])
    # print("i", i, "l score", score)
    ans = max(ans, score)
    for j in range(N+1):
        if N < i + j or K < i + j:
            continue
        tmp = score
        tmp += sum(V[N-j:])
        ans = max(ans, tmp)
        # print("i", i, "j", j, "lr score", tmp)

        # 捨てる最大個数
        k = K - i - j
        if k < 0:
            continue
        negs = []
        for n in range(0, i):
            if V[n] < 0:
                negs.append(V[n])
        for n in range(N-j, N):
            if V[n] < 0:
                negs.append(V[n])
        negs.sort()
        # print(negs)
        tmp -= sum(negs[:k])
        ans = max(ans, tmp)
        # print("i", i, "j", j, "k", k, "lrd score", tmp)

print(ans)