def resolve():
    N, K = list(map(int, input().split()))
    V = list(map(int, input().split()))
    start = (K+1)//2 if K%2==1 else K//2
    maxvalue = 0
    for npop in range(start, K+1):
        if npop >= N:
            npush = K - N
            negs = sorted([v for v in V if v < 0])
            posis = [v for v in V if v >= 0]
            maxvalue = max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush])))
            continue

        for nleft in range(npop+1):
            nright = npop-nleft
            picked = V[:nleft] + V[N-nright:]
            negs = sorted([v for v in picked if v < 0])
            posis = [v for v in picked if v >= 0]
            npush = min(K-npop, len(negs))
            maxvalue = max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush])))
    print(maxvalue)
            


if '__main__' == __name__:
    resolve() def resolve():
    N, K = list(map(int, input().split()))
    V = list(map(int, input().split()))
    start = (K+1)//2 if K%2==1 else K//2
    maxvalue = 0
    for npop in range(start, K+1):
        if npop >= N:
            npush = K - N
            negs = sorted([v for v in V if v < 0])
            posis = [v for v in V if v >= 0]
            maxvalue = max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush])))
            continue

        for nleft in range(npop+1):
            nright = npop-nleft
            picked = V[:nleft] + V[N-nright:]
            negs = sorted([v for v in picked if v < 0])
            posis = [v for v in picked if v >= 0]
            npush = min(K-npop, len(negs))
            maxvalue = max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush])))
    print(maxvalue)
             resolve 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 start = (K+1)//2 if K%2==1 else K//2 start (K+1)//2 if K%2==1 else K//2 K%2==1 K%2 K 2 1 (K+1)//2 K+1 K 1 2 K//2 K 2 maxvalue = 0 maxvalue 0 for npop in range(start, K+1):
        if npop >= N:
            npush = K - N
            negs = sorted([v for v in V if v < 0])
            posis = [v for v in V if v >= 0]
            maxvalue = max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush])))
            continue

        for nleft in range(npop+1):
            nright = npop-nleft
            picked = V[:nleft] + V[N-nright:]
            negs = sorted([v for v in picked if v < 0])
            posis = [v for v in picked if v >= 0]
            npush = min(K-npop, len(negs))
            maxvalue = max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush])))
     npop range(start, K+1) range start K+1 K 1 if npop >= N:
            npush = K - N
            negs = sorted([v for v in V if v < 0])
            posis = [v for v in V if v >= 0]
            maxvalue = max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush])))
            continue

         npop >= N npop N npush = K - N npush K - N K N v for v in V if v < 0 for v in V if v < 0 v V v < 0 v 0 if v < 0 v negs = sorted([v for v in V if v < 0]) negs sorted([v for v in V if v < 0]) sorted [v for v in V if v < 0] v for v in V if v >= 0 for v in V if v >= 0 v V v >= 0 v 0 if v >= 0 v posis = [v for v in V if v >= 0] posis [v for v in V if v >= 0] maxvalue = max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush]))) maxvalue max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush]))) max maxvalue sum(posis)+(sum(negs)-sum(negs[:npush])) sum(posis) sum posis sum(negs)-sum(negs[:npush]) sum(negs) sum negs sum(negs[:npush]) sum negs[:npush] negs :npush npush continue for nleft in range(npop+1):
            nright = npop-nleft
            picked = V[:nleft] + V[N-nright:]
            negs = sorted([v for v in picked if v < 0])
            posis = [v for v in picked if v >= 0]
            npush = min(K-npop, len(negs))
            maxvalue = max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush])))
     nleft range(npop+1) range npop+1 npop 1 nright = npop-nleft nright npop-nleft npop nleft picked = V[:nleft] + V[N-nright:] picked V[:nleft] + V[N-nright:] V[:nleft] V :nleft nleft V[N-nright:] V N-nright: N-nright N nright v for v in picked if v < 0 for v in picked if v < 0 v picked v < 0 v 0 if v < 0 v negs = sorted([v for v in picked if v < 0]) negs sorted([v for v in picked if v < 0]) sorted [v for v in picked if v < 0] v for v in picked if v >= 0 for v in picked if v >= 0 v picked v >= 0 v 0 if v >= 0 v posis = [v for v in picked if v >= 0] posis [v for v in picked if v >= 0] npush = min(K-npop, len(negs)) npush min(K-npop, len(negs)) min K-npop K npop len(negs) len negs maxvalue = max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush]))) maxvalue max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush]))) max maxvalue sum(posis)+(sum(negs)-sum(negs[:npush])) sum(posis) sum posis sum(negs)-sum(negs[:npush]) sum(negs) sum negs sum(negs[:npush]) sum negs[:npush] negs :npush npush print(maxvalue) print maxvalue if '__main__' == __name__:
    resolve() '__main__' == __name__ '__main__' __name__ resolve() resolve def resolve():
    N, K = list(map(int, input().split()))
    V = list(map(int, input().split()))
    start = (K+1)//2 if K%2==1 else K//2
    maxvalue = 0
    for npop in range(start, K+1):
        if npop >= N:
            npush = K - N
            negs = sorted([v for v in V if v < 0])
            posis = [v for v in V if v >= 0]
            maxvalue = max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush])))
            continue

        for nleft in range(npop+1):
            nright = npop-nleft
            picked = V[:nleft] + V[N-nright:]
            negs = sorted([v for v in picked if v < 0])
            posis = [v for v in picked if v >= 0]
            npush = min(K-npop, len(negs))
            maxvalue = max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush])))
    print(maxvalue)
             def resolve():
    N, K = list(map(int, input().split()))
    V = list(map(int, input().split()))
    start = (K+1)//2 if K%2==1 else K//2
    maxvalue = 0
    for npop in range(start, K+1):
        if npop >= N:
            npush = K - N
            negs = sorted([v for v in V if v < 0])
            posis = [v for v in V if v >= 0]
            maxvalue = max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush])))
            continue

        for nleft in range(npop+1):
            nright = npop-nleft
            picked = V[:nleft] + V[N-nright:]
            negs = sorted([v for v in picked if v < 0])
            posis = [v for v in picked if v >= 0]
            npush = min(K-npop, len(negs))
            maxvalue = max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush])))
    print(maxvalue)
             resolve
def resolve():
    N, K = list(map(int, input().split()))
    V = list(map(int, input().split()))
    start = (K+1)//2 if K%2==1 else K//2
    maxvalue = 0
    for npop in range(start, K+1):
        if npop >= N:
            npush = K - N
            negs = sorted([v for v in V if v < 0])
            posis = [v for v in V if v >= 0]
            maxvalue = max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush])))
            continue

        for nleft in range(npop+1):
            nright = npop-nleft
            picked = V[:nleft] + V[N-nright:]
            negs = sorted([v for v in picked if v < 0])
            posis = [v for v in picked if v >= 0]
            npush = min(K-npop, len(negs))
            maxvalue = max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush])))
    print(maxvalue)
            


if '__main__' == __name__:
    resolve()