def readinput():
    n,k=map(int,input().split())
    v=list(map(int,input().split()))
    return n,k,v

def main(n,k,v):
    ruisekiL=[0]*(n+1)
    ruisekiR=[0]*(n+1)
    for i in range(1,n+1):
        ruisekiL[i]=ruisekiL[i-1]+v[i-1]
        ruisekiR[i]=ruisekiR[i-1]+v[n-i]
    vv=[]
    for i in range(n):
        vv.append((i+1,v[i]))
    vv.sort(key=lambda x:x[1])
    #print(vv)

    maxown=-10**7
    for kk in range(k+1):
        if n<kk:
            own=ruisekiL[n]
            m=0
            while m<n and vv[m][1]<0: 
                own-=vv[i][1]
                m+=1
            #print(own)
            maxown=max(maxown,own)
        else:
            for l in range(kk//2+1):
                for i in range(kk-l+1):
                    j=kk-i-l
                    jj=n-j+1
                    own=ruisekiL[i]+ruisekiR[j]
                    #print(i,j,own)
                    m=0
                    mcnt=0
                    while(mcnt<l):
                        if i<vv[m][0] and vv[m][0]<jj:
                            m+=1
                            continue
                        #print(i,jj,vv[m])
                        own-=vv[m][1]
                        mcnt+=1
                        m+=1
                    #print(own)
                    maxown=max(own,maxown)
    return maxown

def main2(n,k,v):
    ruisekiL=[0]*(n+1)
    ruisekiR=[0]*(n+1)
    for i in range(1,n+1):
        ruisekiL[i]=ruisekiL[i-1]+v[i-1]
        ruisekiR[i]=ruisekiR[i-1]+v[n-i]
    vv=[]
    for i in range(n):
        vv.append((i+1,v[i]))
    vv.sort(key=lambda x:x[1])
    #print(vv)

    maxown=-10**7
    for i in range(n+1):
        for j in range(n+1):
            if i+j>k or i+j>n:
                break
            jj=n-j+1
            own=ruisekiL[i]+ruisekiR[j]
            #print(i,j,own)
            l=min(i+j,k-i-j)
            m=0
            mcnt=0
            while(mcnt<l):
                if i<vv[m][0] and vv[m][0]<jj:
                    m+=1
                    continue
                if vv[m][1]>=0:
                    break
                own-=vv[m][1]
                mcnt+=1
                m+=1
            #print(own)
            maxown=max(own,maxown)
    return maxown

if __name__=='__main__':
    n,k,v=readinput()
    ans=main2(n,k,v)
    print(ans) def readinput():
    n,k=map(int,input().split())
    v=list(map(int,input().split()))
    return n,k,v readinput 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 return n,k,v return n,k,v n k v def main(n,k,v):
    ruisekiL=[0]*(n+1)
    ruisekiR=[0]*(n+1)
    for i in range(1,n+1):
        ruisekiL[i]=ruisekiL[i-1]+v[i-1]
        ruisekiR[i]=ruisekiR[i-1]+v[n-i]
    vv=[]
    for i in range(n):
        vv.append((i+1,v[i]))
    vv.sort(key=lambda x:x[1])
    #print(vv)

    maxown=-10**7
    for kk in range(k+1):
        if n<kk:
            own=ruisekiL[n]
            m=0
            while m<n and vv[m][1]<0: 
                own-=vv[i][1]
                m+=1
            #print(own)
            maxown=max(maxown,own)
        else:
            for l in range(kk//2+1):
                for i in range(kk-l+1):
                    j=kk-i-l
                    jj=n-j+1
                    own=ruisekiL[i]+ruisekiR[j]
                    #print(i,j,own)
                    m=0
                    mcnt=0
                    while(mcnt<l):
                        if i<vv[m][0] and vv[m][0]<jj:
                            m+=1
                            continue
                        #print(i,jj,vv[m])
                        own-=vv[m][1]
                        mcnt+=1
                        m+=1
                    #print(own)
                    maxown=max(own,maxown)
    return maxown main ruisekiL=[0]*(n+1) ruisekiL [0]*(n+1) [0] 0 n+1 n 1 ruisekiR=[0]*(n+1) ruisekiR [0]*(n+1) [0] 0 n+1 n 1 for i in range(1,n+1):
        ruisekiL[i]=ruisekiL[i-1]+v[i-1]
        ruisekiR[i]=ruisekiR[i-1]+v[n-i]
     i range(1,n+1) range 1 n+1 n 1 ruisekiL[i]=ruisekiL[i-1]+v[i-1] ruisekiL[i] ruisekiL i ruisekiL[i-1]+v[i-1] ruisekiL[i-1] ruisekiL i-1 i 1 v[i-1] v i-1 i 1 ruisekiR[i]=ruisekiR[i-1]+v[n-i] ruisekiR[i] ruisekiR i ruisekiR[i-1]+v[n-i] ruisekiR[i-1] ruisekiR i-1 i 1 v[n-i] v n-i n i vv=[] vv [] for i in range(n):
        vv.append((i+1,v[i]))
     i range(n) range n vv.append((i+1,v[i])) vv.append vv append (i+1,v[i]) i+1 i 1 v[i] v i vv.sort(key=lambda x:x[1]) vv.sort vv sort key=lambda x:x[1] key lambda x:x[1] x[1] x 1 x maxown=-10**7 maxown -10**7 -10 7 for kk in range(k+1):
        if n<kk:
            own=ruisekiL[n]
            m=0
            while m<n and vv[m][1]<0: 
                own-=vv[i][1]
                m+=1
            #print(own)
            maxown=max(maxown,own)
        else:
            for l in range(kk//2+1):
                for i in range(kk-l+1):
                    j=kk-i-l
                    jj=n-j+1
                    own=ruisekiL[i]+ruisekiR[j]
                    #print(i,j,own)
                    m=0
                    mcnt=0
                    while(mcnt<l):
                        if i<vv[m][0] and vv[m][0]<jj:
                            m+=1
                            continue
                        #print(i,jj,vv[m])
                        own-=vv[m][1]
                        mcnt+=1
                        m+=1
                    #print(own)
                    maxown=max(own,maxown)
     kk range(k+1) range k+1 k 1 if n<kk:
            own=ruisekiL[n]
            m=0
            while m<n and vv[m][1]<0: 
                own-=vv[i][1]
                m+=1
            #print(own)
            maxown=max(maxown,own)
        else:
            for l in range(kk//2+1):
                for i in range(kk-l+1):
                    j=kk-i-l
                    jj=n-j+1
                    own=ruisekiL[i]+ruisekiR[j]
                    #print(i,j,own)
                    m=0
                    mcnt=0
                    while(mcnt<l):
                        if i<vv[m][0] and vv[m][0]<jj:
                            m+=1
                            continue
                        #print(i,jj,vv[m])
                        own-=vv[m][1]
                        mcnt+=1
                        m+=1
                    #print(own)
                    maxown=max(own,maxown)
     n<kk n kk own=ruisekiL[n] own ruisekiL[n] ruisekiL n m=0 m 0 while m<n and vv[m][1]<0: 
                own-=vv[i][1]
                m+=1
            #print(own)
             m<n and vv[m][1]<0 m<n m n vv[m][1]<0 vv[m][1] [m] vv m 1 0 own-=vv[i][1] own vv[i][1] [i] vv i 1 m+=1 m 1 maxown=max(maxown,own) maxown max(maxown,own) max maxown own for l in range(kk//2+1):
                for i in range(kk-l+1):
                    j=kk-i-l
                    jj=n-j+1
                    own=ruisekiL[i]+ruisekiR[j]
                    #print(i,j,own)
                    m=0
                    mcnt=0
                    while(mcnt<l):
                        if i<vv[m][0] and vv[m][0]<jj:
                            m+=1
                            continue
                        #print(i,jj,vv[m])
                        own-=vv[m][1]
                        mcnt+=1
                        m+=1
                    #print(own)
                    maxown=max(own,maxown)
     l range(kk//2+1) range kk//2+1 kk//2 kk 2 1 for i in range(kk-l+1):
                    j=kk-i-l
                    jj=n-j+1
                    own=ruisekiL[i]+ruisekiR[j]
                    #print(i,j,own)
                    m=0
                    mcnt=0
                    while(mcnt<l):
                        if i<vv[m][0] and vv[m][0]<jj:
                            m+=1
                            continue
                        #print(i,jj,vv[m])
                        own-=vv[m][1]
                        mcnt+=1
                        m+=1
                    #print(own)
                    maxown=max(own,maxown)
     i range(kk-l+1) range kk-l+1 kk-l kk l 1 j=kk-i-l j kk-i-l kk-i kk i l jj=n-j+1 jj n-j+1 n-j n j 1 own=ruisekiL[i]+ruisekiR[j] own ruisekiL[i]+ruisekiR[j] ruisekiL[i] ruisekiL i ruisekiR[j] ruisekiR j m=0 m 0 mcnt=0 mcnt 0 while(mcnt<l):
                        if i<vv[m][0] and vv[m][0]<jj:
                            m+=1
                            continue
                        #print(i,jj,vv[m])
                        own-=vv[m][1]
                        mcnt+=1
                        m+=1
                    #print(own)
                     mcnt<l mcnt l if i<vv[m][0] and vv[m][0]<jj:
                            m+=1
                            continue
                        #print(i,jj,vv[m])
                         i<vv[m][0] and vv[m][0]<jj i<vv[m][0] i vv[m][0] [m] vv m 0 vv[m][0]<jj vv[m][0] [m] vv m 0 jj m+=1 m 1 continue own-=vv[m][1] own vv[m][1] [m] vv m 1 mcnt+=1 mcnt 1 m+=1 m 1 maxown=max(own,maxown) maxown max(own,maxown) max own maxown return maxown maxown n n k k v v def main2(n,k,v):
    ruisekiL=[0]*(n+1)
    ruisekiR=[0]*(n+1)
    for i in range(1,n+1):
        ruisekiL[i]=ruisekiL[i-1]+v[i-1]
        ruisekiR[i]=ruisekiR[i-1]+v[n-i]
    vv=[]
    for i in range(n):
        vv.append((i+1,v[i]))
    vv.sort(key=lambda x:x[1])
    #print(vv)

    maxown=-10**7
    for i in range(n+1):
        for j in range(n+1):
            if i+j>k or i+j>n:
                break
            jj=n-j+1
            own=ruisekiL[i]+ruisekiR[j]
            #print(i,j,own)
            l=min(i+j,k-i-j)
            m=0
            mcnt=0
            while(mcnt<l):
                if i<vv[m][0] and vv[m][0]<jj:
                    m+=1
                    continue
                if vv[m][1]>=0:
                    break
                own-=vv[m][1]
                mcnt+=1
                m+=1
            #print(own)
            maxown=max(own,maxown)
    return maxown main2 ruisekiL=[0]*(n+1) ruisekiL [0]*(n+1) [0] 0 n+1 n 1 ruisekiR=[0]*(n+1) ruisekiR [0]*(n+1) [0] 0 n+1 n 1 for i in range(1,n+1):
        ruisekiL[i]=ruisekiL[i-1]+v[i-1]
        ruisekiR[i]=ruisekiR[i-1]+v[n-i]
     i range(1,n+1) range 1 n+1 n 1 ruisekiL[i]=ruisekiL[i-1]+v[i-1] ruisekiL[i] ruisekiL i ruisekiL[i-1]+v[i-1] ruisekiL[i-1] ruisekiL i-1 i 1 v[i-1] v i-1 i 1 ruisekiR[i]=ruisekiR[i-1]+v[n-i] ruisekiR[i] ruisekiR i ruisekiR[i-1]+v[n-i] ruisekiR[i-1] ruisekiR i-1 i 1 v[n-i] v n-i n i vv=[] vv [] for i in range(n):
        vv.append((i+1,v[i]))
     i range(n) range n vv.append((i+1,v[i])) vv.append vv append (i+1,v[i]) i+1 i 1 v[i] v i vv.sort(key=lambda x:x[1]) vv.sort vv sort key=lambda x:x[1] key lambda x:x[1] x[1] x 1 x maxown=-10**7 maxown -10**7 -10 7 for i in range(n+1):
        for j in range(n+1):
            if i+j>k or i+j>n:
                break
            jj=n-j+1
            own=ruisekiL[i]+ruisekiR[j]
            #print(i,j,own)
            l=min(i+j,k-i-j)
            m=0
            mcnt=0
            while(mcnt<l):
                if i<vv[m][0] and vv[m][0]<jj:
                    m+=1
                    continue
                if vv[m][1]>=0:
                    break
                own-=vv[m][1]
                mcnt+=1
                m+=1
            #print(own)
            maxown=max(own,maxown)
     i range(n+1) range n+1 n 1 for j in range(n+1):
            if i+j>k or i+j>n:
                break
            jj=n-j+1
            own=ruisekiL[i]+ruisekiR[j]
            #print(i,j,own)
            l=min(i+j,k-i-j)
            m=0
            mcnt=0
            while(mcnt<l):
                if i<vv[m][0] and vv[m][0]<jj:
                    m+=1
                    continue
                if vv[m][1]>=0:
                    break
                own-=vv[m][1]
                mcnt+=1
                m+=1
            #print(own)
            maxown=max(own,maxown)
     j range(n+1) range n+1 n 1 if i+j>k or i+j>n:
                break
             i+j>k or i+j>n i+j>k i+j i j k i+j>n i+j i j n break jj=n-j+1 jj n-j+1 n-j n j 1 own=ruisekiL[i]+ruisekiR[j] own ruisekiL[i]+ruisekiR[j] ruisekiL[i] ruisekiL i ruisekiR[j] ruisekiR j l=min(i+j,k-i-j) l min(i+j,k-i-j) min i+j i j k-i-j k-i k i j m=0 m 0 mcnt=0 mcnt 0 while(mcnt<l):
                if i<vv[m][0] and vv[m][0]<jj:
                    m+=1
                    continue
                if vv[m][1]>=0:
                    break
                own-=vv[m][1]
                mcnt+=1
                m+=1
            #print(own)
             mcnt<l mcnt l if i<vv[m][0] and vv[m][0]<jj:
                    m+=1
                    continue
                 i<vv[m][0] and vv[m][0]<jj i<vv[m][0] i vv[m][0] [m] vv m 0 vv[m][0]<jj vv[m][0] [m] vv m 0 jj m+=1 m 1 continue if vv[m][1]>=0:
                    break
                 vv[m][1]>=0 vv[m][1] [m] vv m 1 0 break own-=vv[m][1] own vv[m][1] [m] vv m 1 mcnt+=1 mcnt 1 m+=1 m 1 maxown=max(own,maxown) maxown max(own,maxown) max own maxown return maxown maxown n n k k v v if __name__=='__main__':
    n,k,v=readinput()
    ans=main2(n,k,v)
    print(ans) __name__=='__main__' __name__ '__main__' n,k,v=readinput() n readinput() readinput k v ans=main2(n,k,v) ans main2(n,k,v) main2 n k v print(ans) print ans n,k,v=readinput() readinput() n def readinput():
    n,k=map(int,input().split())
    v=list(map(int,input().split()))
    return n,k,v def readinput():
    n,k=map(int,input().split())
    v=list(map(int,input().split()))
    return n,k,v readinput def main(n,k,v):
    ruisekiL=[0]*(n+1)
    ruisekiR=[0]*(n+1)
    for i in range(1,n+1):
        ruisekiL[i]=ruisekiL[i-1]+v[i-1]
        ruisekiR[i]=ruisekiR[i-1]+v[n-i]
    vv=[]
    for i in range(n):
        vv.append((i+1,v[i]))
    vv.sort(key=lambda x:x[1])
    #print(vv)

    maxown=-10**7
    for kk in range(k+1):
        if n<kk:
            own=ruisekiL[n]
            m=0
            while m<n and vv[m][1]<0: 
                own-=vv[i][1]
                m+=1
            #print(own)
            maxown=max(maxown,own)
        else:
            for l in range(kk//2+1):
                for i in range(kk-l+1):
                    j=kk-i-l
                    jj=n-j+1
                    own=ruisekiL[i]+ruisekiR[j]
                    #print(i,j,own)
                    m=0
                    mcnt=0
                    while(mcnt<l):
                        if i<vv[m][0] and vv[m][0]<jj:
                            m+=1
                            continue
                        #print(i,jj,vv[m])
                        own-=vv[m][1]
                        mcnt+=1
                        m+=1
                    #print(own)
                    maxown=max(own,maxown)
    return maxown def main(n,k,v):
    ruisekiL=[0]*(n+1)
    ruisekiR=[0]*(n+1)
    for i in range(1,n+1):
        ruisekiL[i]=ruisekiL[i-1]+v[i-1]
        ruisekiR[i]=ruisekiR[i-1]+v[n-i]
    vv=[]
    for i in range(n):
        vv.append((i+1,v[i]))
    vv.sort(key=lambda x:x[1])
    #print(vv)

    maxown=-10**7
    for kk in range(k+1):
        if n<kk:
            own=ruisekiL[n]
            m=0
            while m<n and vv[m][1]<0: 
                own-=vv[i][1]
                m+=1
            #print(own)
            maxown=max(maxown,own)
        else:
            for l in range(kk//2+1):
                for i in range(kk-l+1):
                    j=kk-i-l
                    jj=n-j+1
                    own=ruisekiL[i]+ruisekiR[j]
                    #print(i,j,own)
                    m=0
                    mcnt=0
                    while(mcnt<l):
                        if i<vv[m][0] and vv[m][0]<jj:
                            m+=1
                            continue
                        #print(i,jj,vv[m])
                        own-=vv[m][1]
                        mcnt+=1
                        m+=1
                    #print(own)
                    maxown=max(own,maxown)
    return maxown main v=readinput() readinput() v def main2(n,k,v):
    ruisekiL=[0]*(n+1)
    ruisekiR=[0]*(n+1)
    for i in range(1,n+1):
        ruisekiL[i]=ruisekiL[i-1]+v[i-1]
        ruisekiR[i]=ruisekiR[i-1]+v[n-i]
    vv=[]
    for i in range(n):
        vv.append((i+1,v[i]))
    vv.sort(key=lambda x:x[1])
    #print(vv)

    maxown=-10**7
    for i in range(n+1):
        for j in range(n+1):
            if i+j>k or i+j>n:
                break
            jj=n-j+1
            own=ruisekiL[i]+ruisekiR[j]
            #print(i,j,own)
            l=min(i+j,k-i-j)
            m=0
            mcnt=0
            while(mcnt<l):
                if i<vv[m][0] and vv[m][0]<jj:
                    m+=1
                    continue
                if vv[m][1]>=0:
                    break
                own-=vv[m][1]
                mcnt+=1
                m+=1
            #print(own)
            maxown=max(own,maxown)
    return maxown def main2(n,k,v):
    ruisekiL=[0]*(n+1)
    ruisekiR=[0]*(n+1)
    for i in range(1,n+1):
        ruisekiL[i]=ruisekiL[i-1]+v[i-1]
        ruisekiR[i]=ruisekiR[i-1]+v[n-i]
    vv=[]
    for i in range(n):
        vv.append((i+1,v[i]))
    vv.sort(key=lambda x:x[1])
    #print(vv)

    maxown=-10**7
    for i in range(n+1):
        for j in range(n+1):
            if i+j>k or i+j>n:
                break
            jj=n-j+1
            own=ruisekiL[i]+ruisekiR[j]
            #print(i,j,own)
            l=min(i+j,k-i-j)
            m=0
            mcnt=0
            while(mcnt<l):
                if i<vv[m][0] and vv[m][0]<jj:
                    m+=1
                    continue
                if vv[m][1]>=0:
                    break
                own-=vv[m][1]
                mcnt+=1
                m+=1
            #print(own)
            maxown=max(own,maxown)
    return maxown main2 k,v=readinput() readinput() k ans=main2(n,k,v) main2(n,k,v) ans
def readinput():
    n,k=map(int,input().split())
    v=list(map(int,input().split()))
    return n,k,v

def main(n,k,v):
    ruisekiL=[0]*(n+1)
    ruisekiR=[0]*(n+1)
    for i in range(1,n+1):
        ruisekiL[i]=ruisekiL[i-1]+v[i-1]
        ruisekiR[i]=ruisekiR[i-1]+v[n-i]
    vv=[]
    for i in range(n):
        vv.append((i+1,v[i]))
    vv.sort(key=lambda x:x[1])
    #print(vv)

    maxown=-10**7
    for kk in range(k+1):
        if n<kk:
            own=ruisekiL[n]
            m=0
            while m<n and vv[m][1]<0: 
                own-=vv[i][1]
                m+=1
            #print(own)
            maxown=max(maxown,own)
        else:
            for l in range(kk//2+1):
                for i in range(kk-l+1):
                    j=kk-i-l
                    jj=n-j+1
                    own=ruisekiL[i]+ruisekiR[j]
                    #print(i,j,own)
                    m=0
                    mcnt=0
                    while(mcnt<l):
                        if i<vv[m][0] and vv[m][0]<jj:
                            m+=1
                            continue
                        #print(i,jj,vv[m])
                        own-=vv[m][1]
                        mcnt+=1
                        m+=1
                    #print(own)
                    maxown=max(own,maxown)
    return maxown

def main2(n,k,v):
    ruisekiL=[0]*(n+1)
    ruisekiR=[0]*(n+1)
    for i in range(1,n+1):
        ruisekiL[i]=ruisekiL[i-1]+v[i-1]
        ruisekiR[i]=ruisekiR[i-1]+v[n-i]
    vv=[]
    for i in range(n):
        vv.append((i+1,v[i]))
    vv.sort(key=lambda x:x[1])
    #print(vv)

    maxown=-10**7
    for i in range(n+1):
        for j in range(n+1):
            if i+j>k or i+j>n:
                break
            jj=n-j+1
            own=ruisekiL[i]+ruisekiR[j]
            #print(i,j,own)
            l=min(i+j,k-i-j)
            m=0
            mcnt=0
            while(mcnt<l):
                if i<vv[m][0] and vv[m][0]<jj:
                    m+=1
                    continue
                if vv[m][1]>=0:
                    break
                own-=vv[m][1]
                mcnt+=1
                m+=1
            #print(own)
            maxown=max(own,maxown)
    return maxown

if __name__=='__main__':
    n,k,v=readinput()
    ans=main2(n,k,v)
    print(ans)
