import sys
import itertools
# import numpy as np
import time
import math
 
sys.setrecursionlimit(10 ** 7)
 
from collections import defaultdict
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

MAX = min(N, K) + 1

ans = 0
for a in range(MAX):
    for b in range(MAX):
        if a + b >= MAX:
            continue
        jewels = []
        for i in range(a):
            jewels.append(V[i])
        for j in range(b):
            jewels.append(V[-(j + 1)])
        jewels = sorted(jewels)
        total = sum(jewels)
        if K - (a + b) <= 0:
            ans = max(total, ans)
            continue
        for i in range(K - a - b):
            if i < len(jewels) and jewels[i] < 0:
                total += abs(jewels[i])
        ans = max(total, ans)
print(ans) import sys sys import itertools itertools import time time import math math sys.setrecursionlimit(10 ** 7) sys.setrecursionlimit sys setrecursionlimit 10 ** 7 10 7 from collections import defaultdict read = sys.stdin.buffer.read read sys.stdin.buffer.read sys.stdin.buffer sys.stdin sys stdin buffer read readline = sys.stdin.buffer.readline readline sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline readlines = sys.stdin.buffer.readlines readlines sys.stdin.buffer.readlines sys.stdin.buffer sys.stdin sys stdin buffer readlines 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 MAX = min(N, K) + 1 MAX min(N, K) + 1 min(N, K) min N K 1 ans = 0 ans 0 for a in range(MAX):
    for b in range(MAX):
        if a + b >= MAX:
            continue
        jewels = []
        for i in range(a):
            jewels.append(V[i])
        for j in range(b):
            jewels.append(V[-(j + 1)])
        jewels = sorted(jewels)
        total = sum(jewels)
        if K - (a + b) <= 0:
            ans = max(total, ans)
            continue
        for i in range(K - a - b):
            if i < len(jewels) and jewels[i] < 0:
                total += abs(jewels[i])
        ans = max(total, ans) a range(MAX) range MAX for b in range(MAX):
        if a + b >= MAX:
            continue
        jewels = []
        for i in range(a):
            jewels.append(V[i])
        for j in range(b):
            jewels.append(V[-(j + 1)])
        jewels = sorted(jewels)
        total = sum(jewels)
        if K - (a + b) <= 0:
            ans = max(total, ans)
            continue
        for i in range(K - a - b):
            if i < len(jewels) and jewels[i] < 0:
                total += abs(jewels[i])
        ans = max(total, ans) b range(MAX) range MAX if a + b >= MAX:
            continue
         a + b >= MAX a + b a b MAX continue jewels = [] jewels [] for i in range(a):
            jewels.append(V[i])
         i range(a) range a jewels.append(V[i]) jewels.append jewels append V[i] V i for j in range(b):
            jewels.append(V[-(j + 1)])
         j range(b) range b jewels.append(V[-(j + 1)]) jewels.append jewels append V[-(j + 1)] V -(j + 1) j + 1 j 1 jewels = sorted(jewels) jewels sorted(jewels) sorted jewels total = sum(jewels) total sum(jewels) sum jewels if K - (a + b) <= 0:
            ans = max(total, ans)
            continue
         K - (a + b) <= 0 K - (a + b) K a + b a b 0 ans = max(total, ans) ans max(total, ans) max total ans continue for i in range(K - a - b):
            if i < len(jewels) and jewels[i] < 0:
                total += abs(jewels[i])
         i range(K - a - b) range K - a - b K - a K a b if i < len(jewels) and jewels[i] < 0:
                total += abs(jewels[i])
         i < len(jewels) and jewels[i] < 0 i < len(jewels) i len(jewels) len jewels jewels[i] < 0 jewels[i] jewels i 0 total += abs(jewels[i]) total abs(jewels[i]) abs jewels[i] jewels i ans = max(total, ans) ans max(total, ans) max total ans print(ans) print ans readline = sys.stdin.buffer.readline sys.stdin.buffer.readline readline jewels = [] [] jewels ans = max(total, ans) max(total, ans) ans V = list(map(int, readline().split())) list(map(int, readline().split())) V MAX = min(N, K) + 1 min(N, K) + 1 MAX total = sum(jewels) sum(jewels) total total += abs(jewels[i]) abs(jewels[i]) total read = sys.stdin.buffer.read sys.stdin.buffer.read read readlines = sys.stdin.buffer.readlines sys.stdin.buffer.readlines readlines K = map(int, readline().split()) map(int, readline().split()) K jewels = sorted(jewels) sorted(jewels) jewels N, K = map(int, readline().split()) map(int, readline().split()) N ans = 0 0 ans ans = max(total, ans) max(total, ans) ans
import sys
import itertools
# import numpy as np
import time
import math
 
sys.setrecursionlimit(10 ** 7)
 
from collections import defaultdict
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

MAX = min(N, K) + 1

ans = 0
for a in range(MAX):
    for b in range(MAX):
        if a + b >= MAX:
            continue
        jewels = []
        for i in range(a):
            jewels.append(V[i])
        for j in range(b):
            jewels.append(V[-(j + 1)])
        jewels = sorted(jewels)
        total = sum(jewels)
        if K - (a + b) <= 0:
            ans = max(total, ans)
            continue
        for i in range(K - a - b):
            if i < len(jewels) and jewels[i] < 0:
                total += abs(jewels[i])
        ans = max(total, ans)
print(ans)