from heapq import heappop,heappush


def main():
    N,K=map(int,input().split())
    Juwels=list(map(int,input().split()))


    res=0

    for l in range(min(N,K)+1):
        for r in range(min(N,K)-l+1):
            hands=[]
            for juwel in Juwels[:l]:
                heappush(hands,juwel)
            for juwel in Juwels[N-r:]:
                heappush(hands,juwel)

            step=l+r
            while len(hands)>0 and step<K:
                value=heappop(hands)
                if value>=0:
                    heappush(hands,value)
                    break
                step+=1

            res=max(res,sum(hands))

    print(res)

if __name__=="__main__":
    main()

 from heapq import heappop,heappush def main():
    N,K=map(int,input().split())
    Juwels=list(map(int,input().split()))


    res=0

    for l in range(min(N,K)+1):
        for r in range(min(N,K)-l+1):
            hands=[]
            for juwel in Juwels[:l]:
                heappush(hands,juwel)
            for juwel in Juwels[N-r:]:
                heappush(hands,juwel)

            step=l+r
            while len(hands)>0 and step<K:
                value=heappop(hands)
                if value>=0:
                    heappush(hands,value)
                    break
                step+=1

            res=max(res,sum(hands))

    print(res) main N,K=map(int,input().split()) N map(int,input().split()) map int input().split() ().split () input split K Juwels=list(map(int,input().split())) Juwels list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split res=0 res 0 for l in range(min(N,K)+1):
        for r in range(min(N,K)-l+1):
            hands=[]
            for juwel in Juwels[:l]:
                heappush(hands,juwel)
            for juwel in Juwels[N-r:]:
                heappush(hands,juwel)

            step=l+r
            while len(hands)>0 and step<K:
                value=heappop(hands)
                if value>=0:
                    heappush(hands,value)
                    break
                step+=1

            res=max(res,sum(hands))

     l range(min(N,K)+1) range min(N,K)+1 min(N,K) min N K 1 for r in range(min(N,K)-l+1):
            hands=[]
            for juwel in Juwels[:l]:
                heappush(hands,juwel)
            for juwel in Juwels[N-r:]:
                heappush(hands,juwel)

            step=l+r
            while len(hands)>0 and step<K:
                value=heappop(hands)
                if value>=0:
                    heappush(hands,value)
                    break
                step+=1

            res=max(res,sum(hands))

     r range(min(N,K)-l+1) range min(N,K)-l+1 min(N,K)-l min(N,K) min N K l 1 hands=[] hands [] for juwel in Juwels[:l]:
                heappush(hands,juwel)
             juwel Juwels[:l] Juwels :l l heappush(hands,juwel) heappush hands juwel for juwel in Juwels[N-r:]:
                heappush(hands,juwel)

             juwel Juwels[N-r:] Juwels N-r: N-r N r heappush(hands,juwel) heappush hands juwel step=l+r step l+r l r while len(hands)>0 and step<K:
                value=heappop(hands)
                if value>=0:
                    heappush(hands,value)
                    break
                step+=1

             len(hands)>0 and step<K len(hands)>0 len(hands) len hands 0 step<K step K value=heappop(hands) value heappop(hands) heappop hands if value>=0:
                    heappush(hands,value)
                    break
                 value>=0 value 0 heappush(hands,value) heappush hands value break step+=1 step 1 res=max(res,sum(hands)) res max(res,sum(hands)) max res sum(hands) sum hands print(res) print res if __name__=="__main__":
    main() __name__=="__main__" __name__ "__main__" main() main def main():
    N,K=map(int,input().split())
    Juwels=list(map(int,input().split()))


    res=0

    for l in range(min(N,K)+1):
        for r in range(min(N,K)-l+1):
            hands=[]
            for juwel in Juwels[:l]:
                heappush(hands,juwel)
            for juwel in Juwels[N-r:]:
                heappush(hands,juwel)

            step=l+r
            while len(hands)>0 and step<K:
                value=heappop(hands)
                if value>=0:
                    heappush(hands,value)
                    break
                step+=1

            res=max(res,sum(hands))

    print(res) def main():
    N,K=map(int,input().split())
    Juwels=list(map(int,input().split()))


    res=0

    for l in range(min(N,K)+1):
        for r in range(min(N,K)-l+1):
            hands=[]
            for juwel in Juwels[:l]:
                heappush(hands,juwel)
            for juwel in Juwels[N-r:]:
                heappush(hands,juwel)

            step=l+r
            while len(hands)>0 and step<K:
                value=heappop(hands)
                if value>=0:
                    heappush(hands,value)
                    break
                step+=1

            res=max(res,sum(hands))

    print(res) main
from heapq import heappop,heappush


def main():
    N,K=map(int,input().split())
    Juwels=list(map(int,input().split()))


    res=0

    for l in range(min(N,K)+1):
        for r in range(min(N,K)-l+1):
            hands=[]
            for juwel in Juwels[:l]:
                heappush(hands,juwel)
            for juwel in Juwels[N-r:]:
                heappush(hands,juwel)

            step=l+r
            while len(hands)>0 and step<K:
                value=heappop(hands)
                if value>=0:
                    heappush(hands,value)
                    break
                step+=1

            res=max(res,sum(hands))

    print(res)

if __name__=="__main__":
    main()


