def main():

    n,k = list(map(int,input().split()))

    v = list(map(int,input().split()))

    m = 0

    for k_ in range(k+1):

        v_c = v

        if k_>n:

            v_ = v_c

            v_c = []

            s = k-n

        else:

            v_ = v_c[0:k_]

            v_c = v_c[k_:]

            s = k-k_

        for i in range(s+1):

            if i>len(v_c):

                v__ =v_+ v_c

                s_ = s-len(v_c)

            else:

                v__ = v_+ v_c[len(v_c)-i:]

                s_ = s-i

            while s_>0 and len(v__)>0 and min(v__)<0:

                v__.pop(v__.index(min(v__)))

                s_ = s_-1

            if sum(v__)>m:

                m = sum(v__)

    print(m)



if __name__ == "__main__":

    main()
def main():

    import copy

    n,k = list(map(int,input().split()))

    v = list(map(int,input().split()))

    ans = 0

    for i in range(min(n+1,k+1)):

        t = v[0:i]

        for j in range(min(n-i+1,k-i+1)):

            t_ = copy.deepcopy(t)

            t_ += v[n-j:n]

            

            r = k-i-j

            t_ = sorted(t_)

            cnt = 0

            for l in range(len(t_)):

                if t_[l]<0 and cnt<r:

                    t_[l] = 0

                    cnt += 1

                else:

                    break

            if ans<sum(t_):

                ans = sum(t_)

    print(ans)



if __name__ == "__main__":

    main()

def main():
    n, k = list(map(int, input().split()))
    v = list(map(int, input().split()))
    m = 0
    for k_ in range(k + 1):
        v_c = v
        if k_ > n:
            v_ = v_c
            v_c = []
            s = k - n
        else:
            v_ = v_c[0:k_]
            v_c = v_c[k_:]
            s = k - k_
        for i in range(s + 1):
            if i > len(v_c):
                v__ = v_ + v_c
                s_ = s - len(v_c)
            else:
                v__ = v_ + v_c[len(v_c) - i :]
                s_ = s - i
            while s_ > 0 and len(v__) > 0 and min(v__) < 0:
                v__.pop(v__.index(min(v__)))
                s_ = s_ - 1
            if sum(v__) > m:
                m = sum(v__)
    print(m)


if __name__ == "__main__":
    main()

def main():
    import copy

    n, k = list(map(int, input().split()))
    v = list(map(int, input().split()))
    ans = 0
    for i in range(min(n + 1, k + 1)):
        t = v[0:i]
        for j in range(min(n - i + 1, k - i + 1)):
            t_ = copy.deepcopy(t)
            t_ += v[n - j : n]
            r = k - i - j
            t_ = sorted(t_)
            cnt = 0
            for l in range(len(t_)):
                if t_[l] < 0 and cnt < r:
                    t_[l] = 0
                    cnt += 1
                else:
                    break
            if ans < sum(t_):
                ans = sum(t_)
    print(ans)


if __name__ == "__main__":
    main()

+    import copy + -    m = 0 -    for k_ in range(k + 1): -        v_c = v -        if k_ > n: -            v_ = v_c -            v_c = [] -            s = k - n -        else: -            v_ = v_c[0:k_] -            v_c = v_c[k_:] -            s = k - k_ -        for i in range(s + 1): -            if i > len(v_c): -                v__ = v_ + v_c -                s_ = s - len(v_c) -            else: -                v__ = v_ + v_c[len(v_c) - i :] -                s_ = s - i -            while s_ > 0 and len(v__) > 0 and min(v__) < 0: -                v__.pop(v__.index(min(v__))) -                s_ = s_ - 1 -            if sum(v__) > m: -                m = sum(v__) -    print(m) +    ans = 0 +    for i in range(min(n + 1, k + 1)): +        t = v[0:i] +        for j in range(min(n - i + 1, k - i + 1)): +            t_ = copy.deepcopy(t) +            t_ += v[n - j : n] +            r = k - i - j +            t_ = sorted(t_) +            cnt = 0 +            for l in range(len(t_)): +                if t_[l] < 0 and cnt < r: +                    t_[l] = 0 +                    cnt += 1 +                else: +                    break +            if ans < sum(t_): +                ans = sum(t_) +    print(ans)