import heapq
from collections import deque
import copy

def main():
    n, k = map(int, input().split())
    v = deque([int(x) for x in input().split()])

    left_plus = 0
    left_minus_count = 0
    left_minus_list = []
    
    ans = 0
    sub = 0
    minus_list = []
    minus_count = 0
    heapq.heapify(minus_list)
    for i in range(n-1, max([-1, n-k-1]), -1):
        if v[i] >= 0:
            sub += v[i]
        else:
            heapq.heappush(minus_list, -v[i])
            minus_count += 1
        while n-i+minus_count > k and minus_list:
            sub -= heapq.heappop(minus_list)
            minus_count -= 1
        if n-i+minus_count > k:
            break
        if ans < sub:
            ans = sub
    
    for i in range(min([n, k])):
        if v[i] >= 0:
            left_plus += v[i]
        else:
            left_minus_list.append(-v[i])
            left_minus_count += 1
        
        sub = left_plus
        minus_count = left_minus_count
        minus_list = copy.deepcopy(left_minus_list)
        heapq.heapify(minus_list)
        while i+minus_count+1 > k and minus_list:
            minus_count -= 1
            sub -= heapq.heappop(minus_list)
        if i+minus_count+1 <= k and ans < sub:
            ans = sub
        sub = left_plus
        minus_count = left_minus_count
        minus_list = copy.deepcopy(left_minus_list)
        heapq.heapify(minus_list)
        for j in range(n-1, max([i, n-k+i+1]), -1):
            if v[j] >= 0:
                sub += v[j]
            else:
                minus_count += 1
                heapq.heappush(minus_list, -v[j])
            while minus_count+i+n-j+1 > k and minus_list:
                minus_count -= 1
                sub -= heapq.heappop(minus_list)
            
            if i+(n-j)+minus_count+1 > k:
                break
            if ans < sub:
                ans = sub
        
        
    print(ans)

if __name__ == "__main__":
    main() import heapq heapq from collections import deque import copy copy def main():
    n, k = map(int, input().split())
    v = deque([int(x) for x in input().split()])

    left_plus = 0
    left_minus_count = 0
    left_minus_list = []
    
    ans = 0
    sub = 0
    minus_list = []
    minus_count = 0
    heapq.heapify(minus_list)
    for i in range(n-1, max([-1, n-k-1]), -1):
        if v[i] >= 0:
            sub += v[i]
        else:
            heapq.heappush(minus_list, -v[i])
            minus_count += 1
        while n-i+minus_count > k and minus_list:
            sub -= heapq.heappop(minus_list)
            minus_count -= 1
        if n-i+minus_count > k:
            break
        if ans < sub:
            ans = sub
    
    for i in range(min([n, k])):
        if v[i] >= 0:
            left_plus += v[i]
        else:
            left_minus_list.append(-v[i])
            left_minus_count += 1
        
        sub = left_plus
        minus_count = left_minus_count
        minus_list = copy.deepcopy(left_minus_list)
        heapq.heapify(minus_list)
        while i+minus_count+1 > k and minus_list:
            minus_count -= 1
            sub -= heapq.heappop(minus_list)
        if i+minus_count+1 <= k and ans < sub:
            ans = sub
        sub = left_plus
        minus_count = left_minus_count
        minus_list = copy.deepcopy(left_minus_list)
        heapq.heapify(minus_list)
        for j in range(n-1, max([i, n-k+i+1]), -1):
            if v[j] >= 0:
                sub += v[j]
            else:
                minus_count += 1
                heapq.heappush(minus_list, -v[j])
            while minus_count+i+n-j+1 > k and minus_list:
                minus_count -= 1
                sub -= heapq.heappop(minus_list)
            
            if i+(n-j)+minus_count+1 > k:
                break
            if ans < sub:
                ans = sub
        
        
    print(ans) main n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x v = deque([int(x) for x in input().split()]) v deque([int(x) for x in input().split()]) deque [int(x) for x in input().split()] left_plus = 0 left_plus 0 left_minus_count = 0 left_minus_count 0 left_minus_list = [] left_minus_list [] ans = 0 ans 0 sub = 0 sub 0 minus_list = [] minus_list [] minus_count = 0 minus_count 0 heapq.heapify(minus_list) heapq.heapify heapq heapify minus_list for i in range(n-1, max([-1, n-k-1]), -1):
        if v[i] >= 0:
            sub += v[i]
        else:
            heapq.heappush(minus_list, -v[i])
            minus_count += 1
        while n-i+minus_count > k and minus_list:
            sub -= heapq.heappop(minus_list)
            minus_count -= 1
        if n-i+minus_count > k:
            break
        if ans < sub:
            ans = sub
    
     i range(n-1, max([-1, n-k-1]), -1) range n-1 n 1 max([-1, n-k-1]) max [-1, n-k-1] -1 n-k-1 n-k n k 1 -1 if v[i] >= 0:
            sub += v[i]
        else:
            heapq.heappush(minus_list, -v[i])
            minus_count += 1
         v[i] >= 0 v[i] v i 0 sub += v[i] sub v[i] v i heapq.heappush(minus_list, -v[i]) heapq.heappush heapq heappush minus_list -v[i] v[i] v i minus_count += 1 minus_count 1 while n-i+minus_count > k and minus_list:
            sub -= heapq.heappop(minus_list)
            minus_count -= 1
         n-i+minus_count > k and minus_list n-i+minus_count > k n-i+minus_count n-i n i minus_count k minus_list sub -= heapq.heappop(minus_list) sub heapq.heappop(minus_list) heapq.heappop heapq heappop minus_list minus_count -= 1 minus_count 1 if n-i+minus_count > k:
            break
         n-i+minus_count > k n-i+minus_count n-i n i minus_count k break if ans < sub:
            ans = sub
    
     ans < sub ans sub ans = sub ans sub for i in range(min([n, k])):
        if v[i] >= 0:
            left_plus += v[i]
        else:
            left_minus_list.append(-v[i])
            left_minus_count += 1
        
        sub = left_plus
        minus_count = left_minus_count
        minus_list = copy.deepcopy(left_minus_list)
        heapq.heapify(minus_list)
        while i+minus_count+1 > k and minus_list:
            minus_count -= 1
            sub -= heapq.heappop(minus_list)
        if i+minus_count+1 <= k and ans < sub:
            ans = sub
        sub = left_plus
        minus_count = left_minus_count
        minus_list = copy.deepcopy(left_minus_list)
        heapq.heapify(minus_list)
        for j in range(n-1, max([i, n-k+i+1]), -1):
            if v[j] >= 0:
                sub += v[j]
            else:
                minus_count += 1
                heapq.heappush(minus_list, -v[j])
            while minus_count+i+n-j+1 > k and minus_list:
                minus_count -= 1
                sub -= heapq.heappop(minus_list)
            
            if i+(n-j)+minus_count+1 > k:
                break
            if ans < sub:
                ans = sub
        
        
     i range(min([n, k])) range min([n, k]) min [n, k] n k if v[i] >= 0:
            left_plus += v[i]
        else:
            left_minus_list.append(-v[i])
            left_minus_count += 1
        
         v[i] >= 0 v[i] v i 0 left_plus += v[i] left_plus v[i] v i left_minus_list.append(-v[i]) left_minus_list.append left_minus_list append -v[i] v[i] v i left_minus_count += 1 left_minus_count 1 sub = left_plus sub left_plus minus_count = left_minus_count minus_count left_minus_count minus_list = copy.deepcopy(left_minus_list) minus_list copy.deepcopy(left_minus_list) copy.deepcopy copy deepcopy left_minus_list heapq.heapify(minus_list) heapq.heapify heapq heapify minus_list while i+minus_count+1 > k and minus_list:
            minus_count -= 1
            sub -= heapq.heappop(minus_list)
         i+minus_count+1 > k and minus_list i+minus_count+1 > k i+minus_count+1 i+minus_count i minus_count 1 k minus_list minus_count -= 1 minus_count 1 sub -= heapq.heappop(minus_list) sub heapq.heappop(minus_list) heapq.heappop heapq heappop minus_list if i+minus_count+1 <= k and ans < sub:
            ans = sub
         i+minus_count+1 <= k and ans < sub i+minus_count+1 <= k i+minus_count+1 i+minus_count i minus_count 1 k ans < sub ans sub ans = sub ans sub sub = left_plus sub left_plus minus_count = left_minus_count minus_count left_minus_count minus_list = copy.deepcopy(left_minus_list) minus_list copy.deepcopy(left_minus_list) copy.deepcopy copy deepcopy left_minus_list heapq.heapify(minus_list) heapq.heapify heapq heapify minus_list for j in range(n-1, max([i, n-k+i+1]), -1):
            if v[j] >= 0:
                sub += v[j]
            else:
                minus_count += 1
                heapq.heappush(minus_list, -v[j])
            while minus_count+i+n-j+1 > k and minus_list:
                minus_count -= 1
                sub -= heapq.heappop(minus_list)
            
            if i+(n-j)+minus_count+1 > k:
                break
            if ans < sub:
                ans = sub
        
        
     j range(n-1, max([i, n-k+i+1]), -1) range n-1 n 1 max([i, n-k+i+1]) max [i, n-k+i+1] i n-k+i+1 n-k+i n-k n k i 1 -1 if v[j] >= 0:
                sub += v[j]
            else:
                minus_count += 1
                heapq.heappush(minus_list, -v[j])
             v[j] >= 0 v[j] v j 0 sub += v[j] sub v[j] v j minus_count += 1 minus_count 1 heapq.heappush(minus_list, -v[j]) heapq.heappush heapq heappush minus_list -v[j] v[j] v j while minus_count+i+n-j+1 > k and minus_list:
                minus_count -= 1
                sub -= heapq.heappop(minus_list)
            
             minus_count+i+n-j+1 > k and minus_list minus_count+i+n-j+1 > k minus_count+i+n-j+1 minus_count+i+n-j minus_count+i+n minus_count+i minus_count i n j 1 k minus_list minus_count -= 1 minus_count 1 sub -= heapq.heappop(minus_list) sub heapq.heappop(minus_list) heapq.heappop heapq heappop minus_list if i+(n-j)+minus_count+1 > k:
                break
             i+(n-j)+minus_count+1 > k i+(n-j)+minus_count+1 i+(n-j)+minus_count i+(n-j) i n-j n j minus_count 1 k break if ans < sub:
                ans = sub
        
        
     ans < sub ans sub ans = sub ans sub print(ans) print ans if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def main():
    n, k = map(int, input().split())
    v = deque([int(x) for x in input().split()])

    left_plus = 0
    left_minus_count = 0
    left_minus_list = []
    
    ans = 0
    sub = 0
    minus_list = []
    minus_count = 0
    heapq.heapify(minus_list)
    for i in range(n-1, max([-1, n-k-1]), -1):
        if v[i] >= 0:
            sub += v[i]
        else:
            heapq.heappush(minus_list, -v[i])
            minus_count += 1
        while n-i+minus_count > k and minus_list:
            sub -= heapq.heappop(minus_list)
            minus_count -= 1
        if n-i+minus_count > k:
            break
        if ans < sub:
            ans = sub
    
    for i in range(min([n, k])):
        if v[i] >= 0:
            left_plus += v[i]
        else:
            left_minus_list.append(-v[i])
            left_minus_count += 1
        
        sub = left_plus
        minus_count = left_minus_count
        minus_list = copy.deepcopy(left_minus_list)
        heapq.heapify(minus_list)
        while i+minus_count+1 > k and minus_list:
            minus_count -= 1
            sub -= heapq.heappop(minus_list)
        if i+minus_count+1 <= k and ans < sub:
            ans = sub
        sub = left_plus
        minus_count = left_minus_count
        minus_list = copy.deepcopy(left_minus_list)
        heapq.heapify(minus_list)
        for j in range(n-1, max([i, n-k+i+1]), -1):
            if v[j] >= 0:
                sub += v[j]
            else:
                minus_count += 1
                heapq.heappush(minus_list, -v[j])
            while minus_count+i+n-j+1 > k and minus_list:
                minus_count -= 1
                sub -= heapq.heappop(minus_list)
            
            if i+(n-j)+minus_count+1 > k:
                break
            if ans < sub:
                ans = sub
        
        
    print(ans) def main():
    n, k = map(int, input().split())
    v = deque([int(x) for x in input().split()])

    left_plus = 0
    left_minus_count = 0
    left_minus_list = []
    
    ans = 0
    sub = 0
    minus_list = []
    minus_count = 0
    heapq.heapify(minus_list)
    for i in range(n-1, max([-1, n-k-1]), -1):
        if v[i] >= 0:
            sub += v[i]
        else:
            heapq.heappush(minus_list, -v[i])
            minus_count += 1
        while n-i+minus_count > k and minus_list:
            sub -= heapq.heappop(minus_list)
            minus_count -= 1
        if n-i+minus_count > k:
            break
        if ans < sub:
            ans = sub
    
    for i in range(min([n, k])):
        if v[i] >= 0:
            left_plus += v[i]
        else:
            left_minus_list.append(-v[i])
            left_minus_count += 1
        
        sub = left_plus
        minus_count = left_minus_count
        minus_list = copy.deepcopy(left_minus_list)
        heapq.heapify(minus_list)
        while i+minus_count+1 > k and minus_list:
            minus_count -= 1
            sub -= heapq.heappop(minus_list)
        if i+minus_count+1 <= k and ans < sub:
            ans = sub
        sub = left_plus
        minus_count = left_minus_count
        minus_list = copy.deepcopy(left_minus_list)
        heapq.heapify(minus_list)
        for j in range(n-1, max([i, n-k+i+1]), -1):
            if v[j] >= 0:
                sub += v[j]
            else:
                minus_count += 1
                heapq.heappush(minus_list, -v[j])
            while minus_count+i+n-j+1 > k and minus_list:
                minus_count -= 1
                sub -= heapq.heappop(minus_list)
            
            if i+(n-j)+minus_count+1 > k:
                break
            if ans < sub:
                ans = sub
        
        
    print(ans) main
import heapq
from collections import deque
import copy

def main():
    n, k = map(int, input().split())
    v = deque([int(x) for x in input().split()])

    left_plus = 0
    left_minus_count = 0
    left_minus_list = []
    
    ans = 0
    sub = 0
    minus_list = []
    minus_count = 0
    heapq.heapify(minus_list)
    for i in range(n-1, max([-1, n-k-1]), -1):
        if v[i] >= 0:
            sub += v[i]
        else:
            heapq.heappush(minus_list, -v[i])
            minus_count += 1
        while n-i+minus_count > k and minus_list:
            sub -= heapq.heappop(minus_list)
            minus_count -= 1
        if n-i+minus_count > k:
            break
        if ans < sub:
            ans = sub
    
    for i in range(min([n, k])):
        if v[i] >= 0:
            left_plus += v[i]
        else:
            left_minus_list.append(-v[i])
            left_minus_count += 1
        
        sub = left_plus
        minus_count = left_minus_count
        minus_list = copy.deepcopy(left_minus_list)
        heapq.heapify(minus_list)
        while i+minus_count+1 > k and minus_list:
            minus_count -= 1
            sub -= heapq.heappop(minus_list)
        if i+minus_count+1 <= k and ans < sub:
            ans = sub
        sub = left_plus
        minus_count = left_minus_count
        minus_list = copy.deepcopy(left_minus_list)
        heapq.heapify(minus_list)
        for j in range(n-1, max([i, n-k+i+1]), -1):
            if v[j] >= 0:
                sub += v[j]
            else:
                minus_count += 1
                heapq.heappush(minus_list, -v[j])
            while minus_count+i+n-j+1 > k and minus_list:
                minus_count -= 1
                sub -= heapq.heappop(minus_list)
            
            if i+(n-j)+minus_count+1 > k:
                break
            if ans < sub:
                ans = sub
        
        
    print(ans)

if __name__ == "__main__":
    main()