def main():
    import copy
    n,k = map(int,input().split())
    v = list(map(int,input().split()))
    ans = 0
    for i in range(min(n+1,k+1)):
        t = v[0:i]
        for j in range(min(n-i+1,k-i+1)):
            t_ = copy.deepcopy(t)
            t_ += v[n-j:n]
            
            r = k-i-j
            t_ = sorted(t_)
            cnt = 0
            for l in range(len(t_)):
                if t_[l]<0 and cnt<r:
                    t_[l] = 0
                    cnt += 1
                else:
                    break
            if ans<sum(t_):
                ans = sum(t_)
    print(ans)

if __name__ == "__main__":
    main() def main():
    import copy
    n,k = map(int,input().split())
    v = list(map(int,input().split()))
    ans = 0
    for i in range(min(n+1,k+1)):
        t = v[0:i]
        for j in range(min(n-i+1,k-i+1)):
            t_ = copy.deepcopy(t)
            t_ += v[n-j:n]
            
            r = k-i-j
            t_ = sorted(t_)
            cnt = 0
            for l in range(len(t_)):
                if t_[l]<0 and cnt<r:
                    t_[l] = 0
                    cnt += 1
                else:
                    break
            if ans<sum(t_):
                ans = sum(t_)
    print(ans) main import copy copy 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(min(n+1,k+1)):
        t = v[0:i]
        for j in range(min(n-i+1,k-i+1)):
            t_ = copy.deepcopy(t)
            t_ += v[n-j:n]
            
            r = k-i-j
            t_ = sorted(t_)
            cnt = 0
            for l in range(len(t_)):
                if t_[l]<0 and cnt<r:
                    t_[l] = 0
                    cnt += 1
                else:
                    break
            if ans<sum(t_):
                ans = sum(t_)
     i range(min(n+1,k+1)) range min(n+1,k+1) min n+1 n 1 k+1 k 1 t = v[0:i] t v[0:i] v 0:i 0 i for j in range(min(n-i+1,k-i+1)):
            t_ = copy.deepcopy(t)
            t_ += v[n-j:n]
            
            r = k-i-j
            t_ = sorted(t_)
            cnt = 0
            for l in range(len(t_)):
                if t_[l]<0 and cnt<r:
                    t_[l] = 0
                    cnt += 1
                else:
                    break
            if ans<sum(t_):
                ans = sum(t_)
     j range(min(n-i+1,k-i+1)) range min(n-i+1,k-i+1) min n-i+1 n-i n i 1 k-i+1 k-i k i 1 t_ = copy.deepcopy(t) t_ copy.deepcopy(t) copy.deepcopy copy deepcopy t t_ += v[n-j:n] t_ v[n-j:n] v n-j:n n-j n j n r = k-i-j r k-i-j k-i k i j t_ = sorted(t_) t_ sorted(t_) sorted t_ cnt = 0 cnt 0 for l in range(len(t_)):
                if t_[l]<0 and cnt<r:
                    t_[l] = 0
                    cnt += 1
                else:
                    break
             l range(len(t_)) range len(t_) len t_ if t_[l]<0 and cnt<r:
                    t_[l] = 0
                    cnt += 1
                else:
                    break
             t_[l]<0 and cnt<r t_[l]<0 t_[l] t_ l 0 cnt<r cnt r t_[l] = 0 t_[l] t_ l 0 cnt += 1 cnt 1 break if ans<sum(t_):
                ans = sum(t_)
     ans<sum(t_) ans sum(t_) sum t_ ans = sum(t_) ans sum(t_) sum t_ print(ans) print ans if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def main():
    import copy
    n,k = map(int,input().split())
    v = list(map(int,input().split()))
    ans = 0
    for i in range(min(n+1,k+1)):
        t = v[0:i]
        for j in range(min(n-i+1,k-i+1)):
            t_ = copy.deepcopy(t)
            t_ += v[n-j:n]
            
            r = k-i-j
            t_ = sorted(t_)
            cnt = 0
            for l in range(len(t_)):
                if t_[l]<0 and cnt<r:
                    t_[l] = 0
                    cnt += 1
                else:
                    break
            if ans<sum(t_):
                ans = sum(t_)
    print(ans) def main():
    import copy
    n,k = map(int,input().split())
    v = list(map(int,input().split()))
    ans = 0
    for i in range(min(n+1,k+1)):
        t = v[0:i]
        for j in range(min(n-i+1,k-i+1)):
            t_ = copy.deepcopy(t)
            t_ += v[n-j:n]
            
            r = k-i-j
            t_ = sorted(t_)
            cnt = 0
            for l in range(len(t_)):
                if t_[l]<0 and cnt<r:
                    t_[l] = 0
                    cnt += 1
                else:
                    break
            if ans<sum(t_):
                ans = sum(t_)
    print(ans) main
def main():
    import copy
    n,k = map(int,input().split())
    v = list(map(int,input().split()))
    ans = 0
    for i in range(min(n+1,k+1)):
        t = v[0:i]
        for j in range(min(n-i+1,k-i+1)):
            t_ = copy.deepcopy(t)
            t_ += v[n-j:n]
            
            r = k-i-j
            t_ = sorted(t_)
            cnt = 0
            for l in range(len(t_)):
                if t_[l]<0 and cnt<r:
                    t_[l] = 0
                    cnt += 1
                else:
                    break
            if ans<sum(t_):
                ans = sum(t_)
    print(ans)

if __name__ == "__main__":
    main()
