#!/usr/bin/env python
from collections import deque, defaultdict
from itertools import combinations, combinations_with_replacement
import bisect
from math import factorial, sqrt, log, ceil, floor

def main():
    #N, M = map(int, input().split())
    N, K = map(int, input().split())
    Vs = list(map(int, input().split()))

    max_value = None
    for l in range(min([K+1, N+1])):
        for r in range(min([K+1-l, N-l+1])):
            for b in range(K - l - r + 1):
                values = list(Vs[:l])
                if r > 0:
                    values += list(Vs[-r:])
                values = sorted(values)
                for i in range(b):
                    if values and values[0] < 0:
                        values.pop(0)
                    else:
                        break
                value = sum(values)
                if max_value is None or max_value < value:
                    max_value = value

    print(max_value)


if __name__ == '__main__':
    main() from collections import deque, defaultdict from itertools import combinations, combinations_with_replacement import bisect bisect from math import factorial, sqrt, log, ceil, floor def main():
    #N, M = map(int, input().split())
    N, K = map(int, input().split())
    Vs = list(map(int, input().split()))

    max_value = None
    for l in range(min([K+1, N+1])):
        for r in range(min([K+1-l, N-l+1])):
            for b in range(K - l - r + 1):
                values = list(Vs[:l])
                if r > 0:
                    values += list(Vs[-r:])
                values = sorted(values)
                for i in range(b):
                    if values and values[0] < 0:
                        values.pop(0)
                    else:
                        break
                value = sum(values)
                if max_value is None or max_value < value:
                    max_value = value

    print(max_value) main N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K Vs = list(map(int, input().split())) Vs list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split max_value = None max_value None for l in range(min([K+1, N+1])):
        for r in range(min([K+1-l, N-l+1])):
            for b in range(K - l - r + 1):
                values = list(Vs[:l])
                if r > 0:
                    values += list(Vs[-r:])
                values = sorted(values)
                for i in range(b):
                    if values and values[0] < 0:
                        values.pop(0)
                    else:
                        break
                value = sum(values)
                if max_value is None or max_value < value:
                    max_value = value

     l range(min([K+1, N+1])) range min([K+1, N+1]) min [K+1, N+1] K+1 K 1 N+1 N 1 for r in range(min([K+1-l, N-l+1])):
            for b in range(K - l - r + 1):
                values = list(Vs[:l])
                if r > 0:
                    values += list(Vs[-r:])
                values = sorted(values)
                for i in range(b):
                    if values and values[0] < 0:
                        values.pop(0)
                    else:
                        break
                value = sum(values)
                if max_value is None or max_value < value:
                    max_value = value

     r range(min([K+1-l, N-l+1])) range min([K+1-l, N-l+1]) min [K+1-l, N-l+1] K+1-l K+1 K 1 l N-l+1 N-l N l 1 for b in range(K - l - r + 1):
                values = list(Vs[:l])
                if r > 0:
                    values += list(Vs[-r:])
                values = sorted(values)
                for i in range(b):
                    if values and values[0] < 0:
                        values.pop(0)
                    else:
                        break
                value = sum(values)
                if max_value is None or max_value < value:
                    max_value = value

     b range(K - l - r + 1) range K - l - r + 1 K - l - r K - l K l r 1 values = list(Vs[:l]) values list(Vs[:l]) list Vs[:l] Vs :l l if r > 0:
                    values += list(Vs[-r:])
                 r > 0 r 0 values += list(Vs[-r:]) values list(Vs[-r:]) list Vs[-r:] Vs -r: -r r values = sorted(values) values sorted(values) sorted values for i in range(b):
                    if values and values[0] < 0:
                        values.pop(0)
                    else:
                        break
                 i range(b) range b if values and values[0] < 0:
                        values.pop(0)
                    else:
                        break
                 values and values[0] < 0 values values[0] < 0 values[0] values 0 0 values.pop(0) values.pop values pop 0 break value = sum(values) value sum(values) sum values if max_value is None or max_value < value:
                    max_value = value

     max_value is None or max_value < value max_value is None max_value None max_value < value max_value value max_value = value max_value value print(max_value) print max_value if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    #N, M = map(int, input().split())
    N, K = map(int, input().split())
    Vs = list(map(int, input().split()))

    max_value = None
    for l in range(min([K+1, N+1])):
        for r in range(min([K+1-l, N-l+1])):
            for b in range(K - l - r + 1):
                values = list(Vs[:l])
                if r > 0:
                    values += list(Vs[-r:])
                values = sorted(values)
                for i in range(b):
                    if values and values[0] < 0:
                        values.pop(0)
                    else:
                        break
                value = sum(values)
                if max_value is None or max_value < value:
                    max_value = value

    print(max_value) def main():
    #N, M = map(int, input().split())
    N, K = map(int, input().split())
    Vs = list(map(int, input().split()))

    max_value = None
    for l in range(min([K+1, N+1])):
        for r in range(min([K+1-l, N-l+1])):
            for b in range(K - l - r + 1):
                values = list(Vs[:l])
                if r > 0:
                    values += list(Vs[-r:])
                values = sorted(values)
                for i in range(b):
                    if values and values[0] < 0:
                        values.pop(0)
                    else:
                        break
                value = sum(values)
                if max_value is None or max_value < value:
                    max_value = value

    print(max_value) main
#!/usr/bin/env python
from collections import deque, defaultdict
from itertools import combinations, combinations_with_replacement
import bisect
from math import factorial, sqrt, log, ceil, floor

def main():
    #N, M = map(int, input().split())
    N, K = map(int, input().split())
    Vs = list(map(int, input().split()))

    max_value = None
    for l in range(min([K+1, N+1])):
        for r in range(min([K+1-l, N-l+1])):
            for b in range(K - l - r + 1):
                values = list(Vs[:l])
                if r > 0:
                    values += list(Vs[-r:])
                values = sorted(values)
                for i in range(b):
                    if values and values[0] < 0:
                        values.pop(0)
                    else:
                        break
                value = sum(values)
                if max_value is None or max_value < value:
                    max_value = value

    print(max_value)


if __name__ == '__main__':
    main()
