import sys
import heapq
def input(): return sys.stdin.readline().rstrip()

def main():
    n, k = map(int, input().split())
    v = list(map(int, input().split()))
    ans = 0
    for i in range(n):
        for j in range(i, n):
            h=[0]
            heapq.heapify(h)
            count = 0
            for p in range(i-1):
                heapq.heappush(h, v[p])
                count += 1
            for m in range(j+1, n):
                heapq.heappush(h, v[m])
                count += 1
            while h[0] < 0 and count < k:
                t = heapq.heappop(h)
                count += 1
            if count <= k:
                ans = max(ans,sum(h))
    counts = n
    heapq.heapify(v)
    while h[0] < 0 and counts < k:
        t = heapq.heappop(v)
        counts += 1
    if counts <= k:
        ans = max(ans,sum(v))
    print(ans)
   
if __name__ == '__main__':
    main() import sys sys import heapq heapq def input(): return sys.stdin.readline().rstrip() input def main():
    n, k = map(int, input().split())
    v = list(map(int, input().split()))
    ans = 0
    for i in range(n):
        for j in range(i, n):
            h=[0]
            heapq.heapify(h)
            count = 0
            for p in range(i-1):
                heapq.heappush(h, v[p])
                count += 1
            for m in range(j+1, n):
                heapq.heappush(h, v[m])
                count += 1
            while h[0] < 0 and count < k:
                t = heapq.heappop(h)
                count += 1
            if count <= k:
                ans = max(ans,sum(h))
    counts = n
    heapq.heapify(v)
    while h[0] < 0 and counts < k:
        t = heapq.heappop(v)
        counts += 1
    if counts <= k:
        ans = max(ans,sum(v))
    print(ans)
    main 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 = 0 ans 0 for i in range(n):
        for j in range(i, n):
            h=[0]
            heapq.heapify(h)
            count = 0
            for p in range(i-1):
                heapq.heappush(h, v[p])
                count += 1
            for m in range(j+1, n):
                heapq.heappush(h, v[m])
                count += 1
            while h[0] < 0 and count < k:
                t = heapq.heappop(h)
                count += 1
            if count <= k:
                ans = max(ans,sum(h))
     i range(n) range n for j in range(i, n):
            h=[0]
            heapq.heapify(h)
            count = 0
            for p in range(i-1):
                heapq.heappush(h, v[p])
                count += 1
            for m in range(j+1, n):
                heapq.heappush(h, v[m])
                count += 1
            while h[0] < 0 and count < k:
                t = heapq.heappop(h)
                count += 1
            if count <= k:
                ans = max(ans,sum(h))
     j range(i, n) range i n h=[0] h [0] 0 heapq.heapify(h) heapq.heapify heapq heapify h count = 0 count 0 for p in range(i-1):
                heapq.heappush(h, v[p])
                count += 1
             p range(i-1) range i-1 i 1 heapq.heappush(h, v[p]) heapq.heappush heapq heappush h v[p] v p count += 1 count 1 for m in range(j+1, n):
                heapq.heappush(h, v[m])
                count += 1
             m range(j+1, n) range j+1 j 1 n heapq.heappush(h, v[m]) heapq.heappush heapq heappush h v[m] v m count += 1 count 1 while h[0] < 0 and count < k:
                t = heapq.heappop(h)
                count += 1
             h[0] < 0 and count < k h[0] < 0 h[0] h 0 0 count < k count k t = heapq.heappop(h) t heapq.heappop(h) heapq.heappop heapq heappop h count += 1 count 1 if count <= k:
                ans = max(ans,sum(h))
     count <= k count k ans = max(ans,sum(h)) ans max(ans,sum(h)) max ans sum(h) sum h counts = n counts n heapq.heapify(v) heapq.heapify heapq heapify v while h[0] < 0 and counts < k:
        t = heapq.heappop(v)
        counts += 1
     h[0] < 0 and counts < k h[0] < 0 h[0] h 0 0 counts < k counts k t = heapq.heappop(v) t heapq.heappop(v) heapq.heappop heapq heappop v counts += 1 counts 1 if counts <= k:
        ans = max(ans,sum(v))
     counts <= k counts k ans = max(ans,sum(v)) ans max(ans,sum(v)) max ans sum(v) sum v print(ans) print ans if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def input(): return sys.stdin.readline().rstrip() def input(): return sys.stdin.readline().rstrip() input def main():
    n, k = map(int, input().split())
    v = list(map(int, input().split()))
    ans = 0
    for i in range(n):
        for j in range(i, n):
            h=[0]
            heapq.heapify(h)
            count = 0
            for p in range(i-1):
                heapq.heappush(h, v[p])
                count += 1
            for m in range(j+1, n):
                heapq.heappush(h, v[m])
                count += 1
            while h[0] < 0 and count < k:
                t = heapq.heappop(h)
                count += 1
            if count <= k:
                ans = max(ans,sum(h))
    counts = n
    heapq.heapify(v)
    while h[0] < 0 and counts < k:
        t = heapq.heappop(v)
        counts += 1
    if counts <= k:
        ans = max(ans,sum(v))
    print(ans)
    def main():
    n, k = map(int, input().split())
    v = list(map(int, input().split()))
    ans = 0
    for i in range(n):
        for j in range(i, n):
            h=[0]
            heapq.heapify(h)
            count = 0
            for p in range(i-1):
                heapq.heappush(h, v[p])
                count += 1
            for m in range(j+1, n):
                heapq.heappush(h, v[m])
                count += 1
            while h[0] < 0 and count < k:
                t = heapq.heappop(h)
                count += 1
            if count <= k:
                ans = max(ans,sum(h))
    counts = n
    heapq.heapify(v)
    while h[0] < 0 and counts < k:
        t = heapq.heappop(v)
        counts += 1
    if counts <= k:
        ans = max(ans,sum(v))
    print(ans)
    main
import sys
import heapq
def input(): return sys.stdin.readline().rstrip()

def main():
    n, k = map(int, input().split())
    v = list(map(int, input().split()))
    ans = 0
    for i in range(n):
        for j in range(i, n):
            h=[0]
            heapq.heapify(h)
            count = 0
            for p in range(i-1):
                heapq.heappush(h, v[p])
                count += 1
            for m in range(j+1, n):
                heapq.heappush(h, v[m])
                count += 1
            while h[0] < 0 and count < k:
                t = heapq.heappop(h)
                count += 1
            if count <= k:
                ans = max(ans,sum(h))
    counts = n
    heapq.heapify(v)
    while h[0] < 0 and counts < k:
        t = heapq.heappop(v)
        counts += 1
    if counts <= k:
        ans = max(ans,sum(v))
    print(ans)
   
if __name__ == '__main__':
    main()