from sys import stdin
import heapq
def main():
    #入力
    readline=stdin.readline
    n,k=map(int,readline().split())
    v=list(map(int,readline().split()))
    v_rev=list(reversed(v))

    m=min(n,k)
    ans=0
    for l in range(m+1):
        for r in range(m+1):
            if l+r>m:
                break

            minus=[]
            s=0
            for i in range(l):
                x=v[i]
                if x<0:
                    minus.append(x)
                s+=x

            for i in range(r):
                x=v_rev[i]
                if x<0:
                    minus.append(x)
                s+=x

            if l+r<k:
                minus.sort()
                for i in range(k-l-r):
                    if i>=len(minus):
                        break
                    s-=minus[i]

            ans=max(ans,s)

    print(ans)

if __name__=="__main__":
    main() from sys import stdin import heapq heapq def main():
    #入力
    readline=stdin.readline
    n,k=map(int,readline().split())
    v=list(map(int,readline().split()))
    v_rev=list(reversed(v))

    m=min(n,k)
    ans=0
    for l in range(m+1):
        for r in range(m+1):
            if l+r>m:
                break

            minus=[]
            s=0
            for i in range(l):
                x=v[i]
                if x<0:
                    minus.append(x)
                s+=x

            for i in range(r):
                x=v_rev[i]
                if x<0:
                    minus.append(x)
                s+=x

            if l+r<k:
                minus.sort()
                for i in range(k-l-r):
                    if i>=len(minus):
                        break
                    s-=minus[i]

            ans=max(ans,s)

    print(ans) main readline=stdin.readline readline stdin.readline stdin readline n,k=map(int,readline().split()) n map(int,readline().split()) map int readline().split() ().split () readline split k v=list(map(int,readline().split())) v list(map(int,readline().split())) list map(int,readline().split()) map int readline().split() ().split () readline split v_rev=list(reversed(v)) v_rev list(reversed(v)) list reversed(v) reversed v m=min(n,k) m min(n,k) min n k ans=0 ans 0 for l in range(m+1):
        for r in range(m+1):
            if l+r>m:
                break

            minus=[]
            s=0
            for i in range(l):
                x=v[i]
                if x<0:
                    minus.append(x)
                s+=x

            for i in range(r):
                x=v_rev[i]
                if x<0:
                    minus.append(x)
                s+=x

            if l+r<k:
                minus.sort()
                for i in range(k-l-r):
                    if i>=len(minus):
                        break
                    s-=minus[i]

            ans=max(ans,s)

     l range(m+1) range m+1 m 1 for r in range(m+1):
            if l+r>m:
                break

            minus=[]
            s=0
            for i in range(l):
                x=v[i]
                if x<0:
                    minus.append(x)
                s+=x

            for i in range(r):
                x=v_rev[i]
                if x<0:
                    minus.append(x)
                s+=x

            if l+r<k:
                minus.sort()
                for i in range(k-l-r):
                    if i>=len(minus):
                        break
                    s-=minus[i]

            ans=max(ans,s)

     r range(m+1) range m+1 m 1 if l+r>m:
                break

             l+r>m l+r l r m break minus=[] minus [] s=0 s 0 for i in range(l):
                x=v[i]
                if x<0:
                    minus.append(x)
                s+=x

             i range(l) range l x=v[i] x v[i] v i if x<0:
                    minus.append(x)
                 x<0 x 0 minus.append(x) minus.append minus append x s+=x s x for i in range(r):
                x=v_rev[i]
                if x<0:
                    minus.append(x)
                s+=x

             i range(r) range r x=v_rev[i] x v_rev[i] v_rev i if x<0:
                    minus.append(x)
                 x<0 x 0 minus.append(x) minus.append minus append x s+=x s x if l+r<k:
                minus.sort()
                for i in range(k-l-r):
                    if i>=len(minus):
                        break
                    s-=minus[i]

             l+r<k l+r l r k minus.sort() minus.sort minus sort for i in range(k-l-r):
                    if i>=len(minus):
                        break
                    s-=minus[i]

             i range(k-l-r) range k-l-r k-l k l r if i>=len(minus):
                        break
                     i>=len(minus) i len(minus) len minus break s-=minus[i] s minus[i] minus i ans=max(ans,s) ans max(ans,s) max ans s print(ans) print ans if __name__=="__main__":
    main() __name__=="__main__" __name__ "__main__" main() main def main():
    #入力
    readline=stdin.readline
    n,k=map(int,readline().split())
    v=list(map(int,readline().split()))
    v_rev=list(reversed(v))

    m=min(n,k)
    ans=0
    for l in range(m+1):
        for r in range(m+1):
            if l+r>m:
                break

            minus=[]
            s=0
            for i in range(l):
                x=v[i]
                if x<0:
                    minus.append(x)
                s+=x

            for i in range(r):
                x=v_rev[i]
                if x<0:
                    minus.append(x)
                s+=x

            if l+r<k:
                minus.sort()
                for i in range(k-l-r):
                    if i>=len(minus):
                        break
                    s-=minus[i]

            ans=max(ans,s)

    print(ans) def main():
    #入力
    readline=stdin.readline
    n,k=map(int,readline().split())
    v=list(map(int,readline().split()))
    v_rev=list(reversed(v))

    m=min(n,k)
    ans=0
    for l in range(m+1):
        for r in range(m+1):
            if l+r>m:
                break

            minus=[]
            s=0
            for i in range(l):
                x=v[i]
                if x<0:
                    minus.append(x)
                s+=x

            for i in range(r):
                x=v_rev[i]
                if x<0:
                    minus.append(x)
                s+=x

            if l+r<k:
                minus.sort()
                for i in range(k-l-r):
                    if i>=len(minus):
                        break
                    s-=minus[i]

            ans=max(ans,s)

    print(ans) main
from sys import stdin
import heapq
def main():
    #入力
    readline=stdin.readline
    n,k=map(int,readline().split())
    v=list(map(int,readline().split()))
    v_rev=list(reversed(v))

    m=min(n,k)
    ans=0
    for l in range(m+1):
        for r in range(m+1):
            if l+r>m:
                break

            minus=[]
            s=0
            for i in range(l):
                x=v[i]
                if x<0:
                    minus.append(x)
                s+=x

            for i in range(r):
                x=v_rev[i]
                if x<0:
                    minus.append(x)
                s+=x

            if l+r<k:
                minus.sort()
                for i in range(k-l-r):
                    if i>=len(minus):
                        break
                    s-=minus[i]

            ans=max(ans,s)

    print(ans)

if __name__=="__main__":
    main()