import random

N,K=map(int,input().split())
V=list(map(int,input().split()))

if K==0:
    print(0)
    exit()

if 0:
    V=[random.randint(-10**7,10**7) for i in range(N)]

ans=-float("inf")

for i in range(N+1):
    for j in range(N-i+1):
        if i+j>K:
            continue
        tmp=[V[k] for k in range(i)]
        for k in range(j):
            tmp.append(V[-1-k])
        tmp.sort()
        rest=K-i-j
        minus=0
        base=sum(tmp)
        for k in range(min(rest,len(tmp))):
            if tmp[k]<0:
                minus+=tmp[k]
            else:
                break
        test=base-minus
        ans=max(ans,test)

print(ans) import random random 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 if K==0:
    print(0)
    exit() K==0 K 0 print(0) print 0 exit() exit if 0:
    V=[random.randint(-10**7,10**7) for i in range(N)] 0 random.randint(-10**7,10**7) for i in range(N) for i in range(N) i range(N) range N for i in range(N) random.randint(-10**7,10**7) random.randint random randint -10**7 -10 7 10**7 10 7 V=[random.randint(-10**7,10**7) for i in range(N)] V [random.randint(-10**7,10**7) for i in range(N)] ans=-float("inf") ans -float("inf") float("inf") float "inf" for i in range(N+1):
    for j in range(N-i+1):
        if i+j>K:
            continue
        tmp=[V[k] for k in range(i)]
        for k in range(j):
            tmp.append(V[-1-k])
        tmp.sort()
        rest=K-i-j
        minus=0
        base=sum(tmp)
        for k in range(min(rest,len(tmp))):
            if tmp[k]<0:
                minus+=tmp[k]
            else:
                break
        test=base-minus
        ans=max(ans,test) i range(N+1) range N+1 N 1 for j in range(N-i+1):
        if i+j>K:
            continue
        tmp=[V[k] for k in range(i)]
        for k in range(j):
            tmp.append(V[-1-k])
        tmp.sort()
        rest=K-i-j
        minus=0
        base=sum(tmp)
        for k in range(min(rest,len(tmp))):
            if tmp[k]<0:
                minus+=tmp[k]
            else:
                break
        test=base-minus
        ans=max(ans,test) j range(N-i+1) range N-i+1 N-i N i 1 if i+j>K:
            continue
         i+j>K i+j i j K continue V[k] for k in range(i) for k in range(i) k range(i) range i for k in range(i) V[k] V k tmp=[V[k] for k in range(i)] tmp [V[k] for k in range(i)] for k in range(j):
            tmp.append(V[-1-k])
         k range(j) range j tmp.append(V[-1-k]) tmp.append tmp append V[-1-k] V -1-k -1 k tmp.sort() tmp.sort tmp sort rest=K-i-j rest K-i-j K-i K i j minus=0 minus 0 base=sum(tmp) base sum(tmp) sum tmp for k in range(min(rest,len(tmp))):
            if tmp[k]<0:
                minus+=tmp[k]
            else:
                break
         k range(min(rest,len(tmp))) range min(rest,len(tmp)) min rest len(tmp) len tmp if tmp[k]<0:
                minus+=tmp[k]
            else:
                break
         tmp[k]<0 tmp[k] tmp k 0 minus+=tmp[k] minus tmp[k] tmp k break test=base-minus test base-minus base minus ans=max(ans,test) ans max(ans,test) max ans test print(ans) print ans V=[random.randint(-10**7,10**7) for i in range(N)] [random.randint(-10**7,10**7) for i in range(N)] V N,K=map(int,input().split()) map(int,input().split()) N ans=max(ans,test) max(ans,test) ans base=sum(tmp) sum(tmp) base minus+=tmp[k] tmp[k] minus K=map(int,input().split()) map(int,input().split()) K tmp=[V[k] for k in range(i)] [V[k] for k in range(i)] tmp ans=-float("inf") -float("inf") ans rest=K-i-j K-i-j rest minus=0 0 minus V=list(map(int,input().split())) list(map(int,input().split())) V test=base-minus base-minus test
import random

N,K=map(int,input().split())
V=list(map(int,input().split()))

if K==0:
    print(0)
    exit()

if 0:
    V=[random.randint(-10**7,10**7) for i in range(N)]

ans=-float("inf")

for i in range(N+1):
    for j in range(N-i+1):
        if i+j>K:
            continue
        tmp=[V[k] for k in range(i)]
        for k in range(j):
            tmp.append(V[-1-k])
        tmp.sort()
        rest=K-i-j
        minus=0
        base=sum(tmp)
        for k in range(min(rest,len(tmp))):
            if tmp[k]<0:
                minus+=tmp[k]
            else:
                break
        test=base-minus
        ans=max(ans,test)

print(ans)
