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

    m = min(N, K)

    ans = 0
    for left in range(m + 1):
        _r = m - left
        for right in range(_r + 1):
            rest = K - left - right
            take = V[:left] + V[N - right:]
            take.sort()
            s = 0
            for x in take:
                if rest and x < 0:
                    rest -= 1
                    continue
                s += x
            ans = max(ans, s)
    print(ans)


if __name__ == '__main__':
    main()

# 操作回数はK回以内
# x=min(K,N)個取り出す
# K-x個戻せる def main():
    N, K = map(int, input().split())
    *V, = map(int, input().split())

    m = min(N, K)

    ans = 0
    for left in range(m + 1):
        _r = m - left
        for right in range(_r + 1):
            rest = K - left - right
            take = V[:left] + V[N - right:]
            take.sort()
            s = 0
            for x in take:
                if rest and x < 0:
                    rest -= 1
                    continue
                s += x
            ans = max(ans, s)
    print(ans) main N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K *V, = map(int, input().split()) *V V map(int, input().split()) map int input().split() ().split () input split m = min(N, K) m min(N, K) min N K ans = 0 ans 0 for left in range(m + 1):
        _r = m - left
        for right in range(_r + 1):
            rest = K - left - right
            take = V[:left] + V[N - right:]
            take.sort()
            s = 0
            for x in take:
                if rest and x < 0:
                    rest -= 1
                    continue
                s += x
            ans = max(ans, s)
     left range(m + 1) range m + 1 m 1 _r = m - left _r m - left m left for right in range(_r + 1):
            rest = K - left - right
            take = V[:left] + V[N - right:]
            take.sort()
            s = 0
            for x in take:
                if rest and x < 0:
                    rest -= 1
                    continue
                s += x
            ans = max(ans, s)
     right range(_r + 1) range _r + 1 _r 1 rest = K - left - right rest K - left - right K - left K left right take = V[:left] + V[N - right:] take V[:left] + V[N - right:] V[:left] V :left left V[N - right:] V N - right: N - right N right take.sort() take.sort take sort s = 0 s 0 for x in take:
                if rest and x < 0:
                    rest -= 1
                    continue
                s += x
             x take if rest and x < 0:
                    rest -= 1
                    continue
                 rest and x < 0 rest x < 0 x 0 rest -= 1 rest 1 continue s += x s x ans = max(ans, s) ans max(ans, s) max ans s print(ans) print ans if __name__ == '__main__':
    main()

# 操作回数はK回以内
# x=min(K,N)個取り出す
# K-x個戻せる __name__ == '__main__' __name__ '__main__' main() main def main():
    N, K = map(int, input().split())
    *V, = map(int, input().split())

    m = min(N, K)

    ans = 0
    for left in range(m + 1):
        _r = m - left
        for right in range(_r + 1):
            rest = K - left - right
            take = V[:left] + V[N - right:]
            take.sort()
            s = 0
            for x in take:
                if rest and x < 0:
                    rest -= 1
                    continue
                s += x
            ans = max(ans, s)
    print(ans) def main():
    N, K = map(int, input().split())
    *V, = map(int, input().split())

    m = min(N, K)

    ans = 0
    for left in range(m + 1):
        _r = m - left
        for right in range(_r + 1):
            rest = K - left - right
            take = V[:left] + V[N - right:]
            take.sort()
            s = 0
            for x in take:
                if rest and x < 0:
                    rest -= 1
                    continue
                s += x
            ans = max(ans, s)
    print(ans) main
def main():
    N, K = map(int, input().split())
    *V, = map(int, input().split())

    m = min(N, K)

    ans = 0
    for left in range(m + 1):
        _r = m - left
        for right in range(_r + 1):
            rest = K - left - right
            take = V[:left] + V[N - right:]
            take.sort()
            s = 0
            for x in take:
                if rest and x < 0:
                    rest -= 1
                    continue
                s += x
            ans = max(ans, s)
    print(ans)


if __name__ == '__main__':
    main()

# 操作回数はK回以内
# x=min(K,N)個取り出す
# K-x個戻せる
