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

    ans = -float('inf')
    for left in range(min(N,K)+1):
        for right in range(min(N,K)-left+1):
            took = V[:left] + V[-1:-(right+1):-1]
            took.sort()
            sm = sum(took)
            for put in range(K-(left+right)):
                if len(took) == put:
                    break
                if took[put] >= 0:
                    break
                sm += abs(took[put])
            
            ans = max(ans,sm)
    
    print(ans)
if __name__ == '__main__':
    solve() def solve():
    N,K = map(int,input().split())
    V = list(map(int,input().split()))

    ans = -float('inf')
    for left in range(min(N,K)+1):
        for right in range(min(N,K)-left+1):
            took = V[:left] + V[-1:-(right+1):-1]
            took.sort()
            sm = sum(took)
            for put in range(K-(left+right)):
                if len(took) == put:
                    break
                if took[put] >= 0:
                    break
                sm += abs(took[put])
            
            ans = max(ans,sm)
    
    print(ans) solve 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 ans = -float('inf') ans -float('inf') float('inf') float 'inf' for left in range(min(N,K)+1):
        for right in range(min(N,K)-left+1):
            took = V[:left] + V[-1:-(right+1):-1]
            took.sort()
            sm = sum(took)
            for put in range(K-(left+right)):
                if len(took) == put:
                    break
                if took[put] >= 0:
                    break
                sm += abs(took[put])
            
            ans = max(ans,sm)
    
     left range(min(N,K)+1) range min(N,K)+1 min(N,K) min N K 1 for right in range(min(N,K)-left+1):
            took = V[:left] + V[-1:-(right+1):-1]
            took.sort()
            sm = sum(took)
            for put in range(K-(left+right)):
                if len(took) == put:
                    break
                if took[put] >= 0:
                    break
                sm += abs(took[put])
            
            ans = max(ans,sm)
    
     right range(min(N,K)-left+1) range min(N,K)-left+1 min(N,K)-left min(N,K) min N K left 1 took = V[:left] + V[-1:-(right+1):-1] took V[:left] + V[-1:-(right+1):-1] V[:left] V :left left V[-1:-(right+1):-1] V -1:-(right+1):-1 -1 -(right+1) right+1 right 1 -1 took.sort() took.sort took sort sm = sum(took) sm sum(took) sum took for put in range(K-(left+right)):
                if len(took) == put:
                    break
                if took[put] >= 0:
                    break
                sm += abs(took[put])
            
             put range(K-(left+right)) range K-(left+right) K left+right left right if len(took) == put:
                    break
                 len(took) == put len(took) len took put break if took[put] >= 0:
                    break
                 took[put] >= 0 took[put] took put 0 break sm += abs(took[put]) sm abs(took[put]) abs took[put] took put ans = max(ans,sm) ans max(ans,sm) max ans sm print(ans) print ans if __name__ == '__main__':
    solve() __name__ == '__main__' __name__ '__main__' solve() solve def solve():
    N,K = map(int,input().split())
    V = list(map(int,input().split()))

    ans = -float('inf')
    for left in range(min(N,K)+1):
        for right in range(min(N,K)-left+1):
            took = V[:left] + V[-1:-(right+1):-1]
            took.sort()
            sm = sum(took)
            for put in range(K-(left+right)):
                if len(took) == put:
                    break
                if took[put] >= 0:
                    break
                sm += abs(took[put])
            
            ans = max(ans,sm)
    
    print(ans) def solve():
    N,K = map(int,input().split())
    V = list(map(int,input().split()))

    ans = -float('inf')
    for left in range(min(N,K)+1):
        for right in range(min(N,K)-left+1):
            took = V[:left] + V[-1:-(right+1):-1]
            took.sort()
            sm = sum(took)
            for put in range(K-(left+right)):
                if len(took) == put:
                    break
                if took[put] >= 0:
                    break
                sm += abs(took[put])
            
            ans = max(ans,sm)
    
    print(ans) solve
def solve():
    N,K = map(int,input().split())
    V = list(map(int,input().split()))

    ans = -float('inf')
    for left in range(min(N,K)+1):
        for right in range(min(N,K)-left+1):
            took = V[:left] + V[-1:-(right+1):-1]
            took.sort()
            sm = sum(took)
            for put in range(K-(left+right)):
                if len(took) == put:
                    break
                if took[put] >= 0:
                    break
                sm += abs(took[put])
            
            ans = max(ans,sm)
    
    print(ans)
if __name__ == '__main__':
    solve()