def main():
    import sys
    input = sys.stdin.readline
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    V.insert(0, 0)
    ans = 0
    for i in range(min(N+1, K+1)):
        for j in range(min(N+1, K+1)):
            if i + j > min(N, K):
                break
            if i == j == 0:
                continue
            minus_l = []
            sum_v = 0
            for ii in range(i+1):
                v = V[ii]
                if v < 0:
                    minus_l.append(v)
                sum_v += v
            for jj in range(j+1):
                v = V[-jj]
                if v < 0:
                    minus_l.append(v)
                sum_v += v
            minus_l.sort(reverse=True)
            for x in range(K-i-j):
                if minus_l:
                    sum_v -= minus_l.pop()
                else:
                    break
            ans = max(ans, sum_v)
    print(ans)
if __name__ == '__main__':
    main() def main():
    import sys
    input = sys.stdin.readline
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    V.insert(0, 0)
    ans = 0
    for i in range(min(N+1, K+1)):
        for j in range(min(N+1, K+1)):
            if i + j > min(N, K):
                break
            if i == j == 0:
                continue
            minus_l = []
            sum_v = 0
            for ii in range(i+1):
                v = V[ii]
                if v < 0:
                    minus_l.append(v)
                sum_v += v
            for jj in range(j+1):
                v = V[-jj]
                if v < 0:
                    minus_l.append(v)
                sum_v += v
            minus_l.sort(reverse=True)
            for x in range(K-i-j):
                if minus_l:
                    sum_v -= minus_l.pop()
                else:
                    break
            ans = max(ans, sum_v)
    print(ans) main import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline 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 V.insert(0, 0) V.insert V insert 0 0 ans = 0 ans 0 for i in range(min(N+1, K+1)):
        for j in range(min(N+1, K+1)):
            if i + j > min(N, K):
                break
            if i == j == 0:
                continue
            minus_l = []
            sum_v = 0
            for ii in range(i+1):
                v = V[ii]
                if v < 0:
                    minus_l.append(v)
                sum_v += v
            for jj in range(j+1):
                v = V[-jj]
                if v < 0:
                    minus_l.append(v)
                sum_v += v
            minus_l.sort(reverse=True)
            for x in range(K-i-j):
                if minus_l:
                    sum_v -= minus_l.pop()
                else:
                    break
            ans = max(ans, sum_v)
     i range(min(N+1, K+1)) range min(N+1, K+1) min N+1 N 1 K+1 K 1 for j in range(min(N+1, K+1)):
            if i + j > min(N, K):
                break
            if i == j == 0:
                continue
            minus_l = []
            sum_v = 0
            for ii in range(i+1):
                v = V[ii]
                if v < 0:
                    minus_l.append(v)
                sum_v += v
            for jj in range(j+1):
                v = V[-jj]
                if v < 0:
                    minus_l.append(v)
                sum_v += v
            minus_l.sort(reverse=True)
            for x in range(K-i-j):
                if minus_l:
                    sum_v -= minus_l.pop()
                else:
                    break
            ans = max(ans, sum_v)
     j range(min(N+1, K+1)) range min(N+1, K+1) min N+1 N 1 K+1 K 1 if i + j > min(N, K):
                break
             i + j > min(N, K) i + j i j min(N, K) min N K break if i == j == 0:
                continue
             i == j == 0 i == j i j 0 continue minus_l = [] minus_l [] sum_v = 0 sum_v 0 for ii in range(i+1):
                v = V[ii]
                if v < 0:
                    minus_l.append(v)
                sum_v += v
             ii range(i+1) range i+1 i 1 v = V[ii] v V[ii] V ii if v < 0:
                    minus_l.append(v)
                 v < 0 v 0 minus_l.append(v) minus_l.append minus_l append v sum_v += v sum_v v for jj in range(j+1):
                v = V[-jj]
                if v < 0:
                    minus_l.append(v)
                sum_v += v
             jj range(j+1) range j+1 j 1 v = V[-jj] v V[-jj] V -jj jj if v < 0:
                    minus_l.append(v)
                 v < 0 v 0 minus_l.append(v) minus_l.append minus_l append v sum_v += v sum_v v minus_l.sort(reverse=True) minus_l.sort minus_l sort reverse=True reverse True for x in range(K-i-j):
                if minus_l:
                    sum_v -= minus_l.pop()
                else:
                    break
             x range(K-i-j) range K-i-j K-i K i j if minus_l:
                    sum_v -= minus_l.pop()
                else:
                    break
             minus_l sum_v -= minus_l.pop() sum_v minus_l.pop() minus_l.pop minus_l pop break ans = max(ans, sum_v) ans max(ans, sum_v) max ans sum_v print(ans) print ans if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    import sys
    input = sys.stdin.readline
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    V.insert(0, 0)
    ans = 0
    for i in range(min(N+1, K+1)):
        for j in range(min(N+1, K+1)):
            if i + j > min(N, K):
                break
            if i == j == 0:
                continue
            minus_l = []
            sum_v = 0
            for ii in range(i+1):
                v = V[ii]
                if v < 0:
                    minus_l.append(v)
                sum_v += v
            for jj in range(j+1):
                v = V[-jj]
                if v < 0:
                    minus_l.append(v)
                sum_v += v
            minus_l.sort(reverse=True)
            for x in range(K-i-j):
                if minus_l:
                    sum_v -= minus_l.pop()
                else:
                    break
            ans = max(ans, sum_v)
    print(ans) def main():
    import sys
    input = sys.stdin.readline
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    V.insert(0, 0)
    ans = 0
    for i in range(min(N+1, K+1)):
        for j in range(min(N+1, K+1)):
            if i + j > min(N, K):
                break
            if i == j == 0:
                continue
            minus_l = []
            sum_v = 0
            for ii in range(i+1):
                v = V[ii]
                if v < 0:
                    minus_l.append(v)
                sum_v += v
            for jj in range(j+1):
                v = V[-jj]
                if v < 0:
                    minus_l.append(v)
                sum_v += v
            minus_l.sort(reverse=True)
            for x in range(K-i-j):
                if minus_l:
                    sum_v -= minus_l.pop()
                else:
                    break
            ans = max(ans, sum_v)
    print(ans) main
def main():
    import sys
    input = sys.stdin.readline
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    V.insert(0, 0)
    ans = 0
    for i in range(min(N+1, K+1)):
        for j in range(min(N+1, K+1)):
            if i + j > min(N, K):
                break
            if i == j == 0:
                continue
            minus_l = []
            sum_v = 0
            for ii in range(i+1):
                v = V[ii]
                if v < 0:
                    minus_l.append(v)
                sum_v += v
            for jj in range(j+1):
                v = V[-jj]
                if v < 0:
                    minus_l.append(v)
                sum_v += v
            minus_l.sort(reverse=True)
            for x in range(K-i-j):
                if minus_l:
                    sum_v -= minus_l.pop()
                else:
                    break
            ans = max(ans, sum_v)
    print(ans)
if __name__ == '__main__':
    main()