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

def solve(N,K,V):
    ans = 0
    V = V*2
    for l in range(N+1):
        for r in range(N-1,2*N):
            n = r-l+1
            if n == 0 or n>K or n>N:
                continue
            p = K-n
            W = V[l:r+1]
            W.sort()
            total = sum(W)
            firstp = W[:p]
            for w in firstp:
                if w<0:
                    total -= w
                else:
                    break
            ans = max(ans,total)
    return ans
print(solve(N,K,V)) 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 def solve(N,K,V):
    ans = 0
    V = V*2
    for l in range(N+1):
        for r in range(N-1,2*N):
            n = r-l+1
            if n == 0 or n>K or n>N:
                continue
            p = K-n
            W = V[l:r+1]
            W.sort()
            total = sum(W)
            firstp = W[:p]
            for w in firstp:
                if w<0:
                    total -= w
                else:
                    break
            ans = max(ans,total)
    return ans solve ans = 0 ans 0 V = V*2 V V*2 V 2 for l in range(N+1):
        for r in range(N-1,2*N):
            n = r-l+1
            if n == 0 or n>K or n>N:
                continue
            p = K-n
            W = V[l:r+1]
            W.sort()
            total = sum(W)
            firstp = W[:p]
            for w in firstp:
                if w<0:
                    total -= w
                else:
                    break
            ans = max(ans,total)
     l range(N+1) range N+1 N 1 for r in range(N-1,2*N):
            n = r-l+1
            if n == 0 or n>K or n>N:
                continue
            p = K-n
            W = V[l:r+1]
            W.sort()
            total = sum(W)
            firstp = W[:p]
            for w in firstp:
                if w<0:
                    total -= w
                else:
                    break
            ans = max(ans,total)
     r range(N-1,2*N) range N-1 N 1 2*N 2 N n = r-l+1 n r-l+1 r-l r l 1 if n == 0 or n>K or n>N:
                continue
             n == 0 or n>K or n>N n == 0 or n>K n == 0 n 0 n>K n K n>N n N continue p = K-n p K-n K n W = V[l:r+1] W V[l:r+1] V l:r+1 l r+1 r 1 W.sort() W.sort W sort total = sum(W) total sum(W) sum W firstp = W[:p] firstp W[:p] W :p p for w in firstp:
                if w<0:
                    total -= w
                else:
                    break
             w firstp if w<0:
                    total -= w
                else:
                    break
             w<0 w 0 total -= w total w break ans = max(ans,total) ans max(ans,total) max ans total return ans ans N N K K V V print(solve(N,K,V)) print solve(N,K,V) solve N K V def solve(N,K,V):
    ans = 0
    V = V*2
    for l in range(N+1):
        for r in range(N-1,2*N):
            n = r-l+1
            if n == 0 or n>K or n>N:
                continue
            p = K-n
            W = V[l:r+1]
            W.sort()
            total = sum(W)
            firstp = W[:p]
            for w in firstp:
                if w<0:
                    total -= w
                else:
                    break
            ans = max(ans,total)
    return ans def solve(N,K,V):
    ans = 0
    V = V*2
    for l in range(N+1):
        for r in range(N-1,2*N):
            n = r-l+1
            if n == 0 or n>K or n>N:
                continue
            p = K-n
            W = V[l:r+1]
            W.sort()
            total = sum(W)
            firstp = W[:p]
            for w in firstp:
                if w<0:
                    total -= w
                else:
                    break
            ans = max(ans,total)
    return ans solve N, K = map(int, input().split()) map(int, input().split()) N V = list(map(int, input().split())) list(map(int, input().split())) V K = map(int, input().split()) map(int, input().split()) K
N, K = map(int, input().split())
V = list(map(int, input().split()))

def solve(N,K,V):
    ans = 0
    V = V*2
    for l in range(N+1):
        for r in range(N-1,2*N):
            n = r-l+1
            if n == 0 or n>K or n>N:
                continue
            p = K-n
            W = V[l:r+1]
            W.sort()
            total = sum(W)
            firstp = W[:p]
            for w in firstp:
                if w<0:
                    total -= w
                else:
                    break
            ans = max(ans,total)
    return ans
print(solve(N,K,V))