import heapq
from collections import deque


def max_value(V, N, K, A, B):
    V = deque(V.copy())
    value = 0
    neg_que = list()
    for _ in range(A):
        if len(V) == 0:
            break
        v = V.popleft()
        value += v
        if v < 0:
            heapq.heappush(neg_que, v)
    for _ in range(B):
        if len(V) == 0:
            break
        v = V.pop()
        value += v
        if v < 0:
            heapq.heappush(neg_que, v)
    max_throw_num = max([K - min([N, A + B]), 0])
    for _ in range(max_throw_num):
        if len(neg_que) == 0:
            break
        v = heapq.heappop(neg_que)
        value -= v
    return value


def main():
    N, K = list(map(int, input().split(' ')))
    V = list(map(int, input().split(' ')))
    max_v = 0
    for a in range(K + 1):
        for b in range(K + 1):
            if a + b > K:
                continue
            v = max_value(V, N, K, a, b)
            max_v = max([v, max_v])
    print(max_v)


if __name__ == '__main__':
    main() import heapq heapq from collections import deque def max_value(V, N, K, A, B):
    V = deque(V.copy())
    value = 0
    neg_que = list()
    for _ in range(A):
        if len(V) == 0:
            break
        v = V.popleft()
        value += v
        if v < 0:
            heapq.heappush(neg_que, v)
    for _ in range(B):
        if len(V) == 0:
            break
        v = V.pop()
        value += v
        if v < 0:
            heapq.heappush(neg_que, v)
    max_throw_num = max([K - min([N, A + B]), 0])
    for _ in range(max_throw_num):
        if len(neg_que) == 0:
            break
        v = heapq.heappop(neg_que)
        value -= v
    return value max_value V = deque(V.copy()) V deque(V.copy()) deque V.copy() V.copy V copy value = 0 value 0 neg_que = list() neg_que list() list for _ in range(A):
        if len(V) == 0:
            break
        v = V.popleft()
        value += v
        if v < 0:
            heapq.heappush(neg_que, v)
     _ range(A) range A if len(V) == 0:
            break
         len(V) == 0 len(V) len V 0 break v = V.popleft() v V.popleft() V.popleft V popleft value += v value v if v < 0:
            heapq.heappush(neg_que, v)
     v < 0 v 0 heapq.heappush(neg_que, v) heapq.heappush heapq heappush neg_que v for _ in range(B):
        if len(V) == 0:
            break
        v = V.pop()
        value += v
        if v < 0:
            heapq.heappush(neg_que, v)
     _ range(B) range B if len(V) == 0:
            break
         len(V) == 0 len(V) len V 0 break v = V.pop() v V.pop() V.pop V pop value += v value v if v < 0:
            heapq.heappush(neg_que, v)
     v < 0 v 0 heapq.heappush(neg_que, v) heapq.heappush heapq heappush neg_que v max_throw_num = max([K - min([N, A + B]), 0]) max_throw_num max([K - min([N, A + B]), 0]) max [K - min([N, A + B]), 0] K - min([N, A + B]) K min([N, A + B]) min [N, A + B] N A + B A B 0 for _ in range(max_throw_num):
        if len(neg_que) == 0:
            break
        v = heapq.heappop(neg_que)
        value -= v
     _ range(max_throw_num) range max_throw_num if len(neg_que) == 0:
            break
         len(neg_que) == 0 len(neg_que) len neg_que 0 break v = heapq.heappop(neg_que) v heapq.heappop(neg_que) heapq.heappop heapq heappop neg_que value -= v value v return value value V V N N K K A A B B def main():
    N, K = list(map(int, input().split(' ')))
    V = list(map(int, input().split(' ')))
    max_v = 0
    for a in range(K + 1):
        for b in range(K + 1):
            if a + b > K:
                continue
            v = max_value(V, N, K, a, b)
            max_v = max([v, max_v])
    print(max_v) main N, K = list(map(int, input().split(' '))) N list(map(int, input().split(' '))) list 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 ' ' max_v = 0 max_v 0 for a in range(K + 1):
        for b in range(K + 1):
            if a + b > K:
                continue
            v = max_value(V, N, K, a, b)
            max_v = max([v, max_v])
     a range(K + 1) range K + 1 K 1 for b in range(K + 1):
            if a + b > K:
                continue
            v = max_value(V, N, K, a, b)
            max_v = max([v, max_v])
     b range(K + 1) range K + 1 K 1 if a + b > K:
                continue
             a + b > K a + b a b K continue v = max_value(V, N, K, a, b) v max_value(V, N, K, a, b) max_value V N K a b max_v = max([v, max_v]) max_v max([v, max_v]) max [v, max_v] v max_v print(max_v) print max_v if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    N, K = list(map(int, input().split(' ')))
    V = list(map(int, input().split(' ')))
    max_v = 0
    for a in range(K + 1):
        for b in range(K + 1):
            if a + b > K:
                continue
            v = max_value(V, N, K, a, b)
            max_v = max([v, max_v])
    print(max_v) def main():
    N, K = list(map(int, input().split(' ')))
    V = list(map(int, input().split(' ')))
    max_v = 0
    for a in range(K + 1):
        for b in range(K + 1):
            if a + b > K:
                continue
            v = max_value(V, N, K, a, b)
            max_v = max([v, max_v])
    print(max_v) main def max_value(V, N, K, A, B):
    V = deque(V.copy())
    value = 0
    neg_que = list()
    for _ in range(A):
        if len(V) == 0:
            break
        v = V.popleft()
        value += v
        if v < 0:
            heapq.heappush(neg_que, v)
    for _ in range(B):
        if len(V) == 0:
            break
        v = V.pop()
        value += v
        if v < 0:
            heapq.heappush(neg_que, v)
    max_throw_num = max([K - min([N, A + B]), 0])
    for _ in range(max_throw_num):
        if len(neg_que) == 0:
            break
        v = heapq.heappop(neg_que)
        value -= v
    return value def max_value(V, N, K, A, B):
    V = deque(V.copy())
    value = 0
    neg_que = list()
    for _ in range(A):
        if len(V) == 0:
            break
        v = V.popleft()
        value += v
        if v < 0:
            heapq.heappush(neg_que, v)
    for _ in range(B):
        if len(V) == 0:
            break
        v = V.pop()
        value += v
        if v < 0:
            heapq.heappush(neg_que, v)
    max_throw_num = max([K - min([N, A + B]), 0])
    for _ in range(max_throw_num):
        if len(neg_que) == 0:
            break
        v = heapq.heappop(neg_que)
        value -= v
    return value max_value
import heapq
from collections import deque


def max_value(V, N, K, A, B):
    V = deque(V.copy())
    value = 0
    neg_que = list()
    for _ in range(A):
        if len(V) == 0:
            break
        v = V.popleft()
        value += v
        if v < 0:
            heapq.heappush(neg_que, v)
    for _ in range(B):
        if len(V) == 0:
            break
        v = V.pop()
        value += v
        if v < 0:
            heapq.heappush(neg_que, v)
    max_throw_num = max([K - min([N, A + B]), 0])
    for _ in range(max_throw_num):
        if len(neg_que) == 0:
            break
        v = heapq.heappop(neg_que)
        value -= v
    return value


def main():
    N, K = list(map(int, input().split(' ')))
    V = list(map(int, input().split(' ')))
    max_v = 0
    for a in range(K + 1):
        for b in range(K + 1):
            if a + b > K:
                continue
            v = max_value(V, N, K, a, b)
            max_v = max([v, max_v])
    print(max_v)


if __name__ == '__main__':
    main()
