Buggy python submission (verdict: Time Limit Exceeded):
import sys, heapq, copy
from collections import deque
input = sys.stdin.readline
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''

N, K = map(int, input().split())
V_init = deque(map(int, input().split()))
max_score = 0
hands = [0] * K
heapq.heapify(hands)

for i_rget in range(K + 1):
	for i_lget in range(max(K - i_rget, 0) + 1):
		for i_del in range(max(K - i_rget - i_lget, 0) + 1):
			score = 0
			hands = [0] * K
			V = copy.copy(V_init)
			for i in range(i_rget + 1):
				if i > 0 and V:
					tmp = V.pop()					
					heapq.heappush(hands, tmp)
					score += tmp
				
			for i in range(i_lget + 1):
				if i > 0 and V:
					tmp = V.popleft()
					heapq.heappush(hands, tmp)
					score += tmp
			
			for i in range(i_del + 1):
				if i > 0:
					tmp = heapq.heappop(hands)
					score -= tmp
			
			if score > max_score:
				max_score = score

print(max_score)


Fixed submission:
import sys, heapq, copy
from collections import deque
input = sys.stdin.readline
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''

N, K = map(int, input().split())
V_init = deque(map(int, input().split()))
max_score = 0
hands = [0] * K
heapq.heapify(hands)

for i_rget in range(K + 1):
	for i_lget in range(max(K - i_rget, 0) + 1):
		tmp = 0
		score = 0
		hands = [0] * K
		V = copy.copy(V_init)
		for i in range(i_rget + 1):
			if i > 0 and V:
				tmp = V.pop()					
				heapq.heappush(hands, tmp)
				score += tmp
			
		for i in range(i_lget + 1):
			if i > 0 and V:
				tmp = V.popleft()
				heapq.heappush(hands, tmp)
				score += tmp
		
		for i in range(max(K - i_rget - i_lget, 0) + 1):
			if i > 0:
				tmp = heapq.heappop(hands)
				if tmp < 0:
					score -= tmp
				else:
					break
		
		if score > max_score:
			max_score = score

print(max_score)

Buggy python submission (verdict: Time Limit Exceeded):
import sys, heapq, copy
from collections import deque
input = sys.stdin.readline
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''

N, K = map(int, input().split())
V_init = deque(map(int, input().split()))
max_score = 0
hands = [0] * K
heapq.heapify(hands)

for i_rget in range(K + 1):
	for i_lget in range(max(K - i_rget, 0) + 1):
		for i_del in range(max(K - i_rget - i_lget, 0) + 1):
			score = 0
			hands = [0] * K
			V = copy.copy(V_init)
			for i in range(i_rget + 1):
				if i > 0 and V:
					tmp = V.pop()					
					heapq.heappush(hands, tmp)
					score += tmp
				
			for i in range(i_lget + 1):
				if i > 0 and V:
					tmp = V.popleft()
					heapq.heappush(hands, tmp)
					score += tmp
			
			for i in range(i_del + 1):
				if i > 0:
					tmp = heapq.heappop(hands)
					score -= tmp
			
			if score > max_score:
				max_score = score

print(max_score)


Fixed submission:
import sys, heapq, copy
from collections import deque
input = sys.stdin.readline
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''

N, K = map(int, input().split())
V_init = deque(map(int, input().split()))
max_score = 0
hands = [0] * K
heapq.heapify(hands)

for i_rget in range(K + 1):
	for i_lget in range(max(K - i_rget, 0) + 1):
		tmp = 0
		score = 0
		hands = [0] * K
		V = copy.copy(V_init)
		for i in range(i_rget + 1):
			if i > 0 and V:
				tmp = V.pop()					
				heapq.heappush(hands, tmp)
				score += tmp
			
		for i in range(i_lget + 1):
			if i > 0 and V:
				tmp = V.popleft()
				heapq.heappush(hands, tmp)
				score += tmp
		
		for i in range(max(K - i_rget - i_lget, 0) + 1):
			if i > 0:
				tmp = heapq.heappop(hands)
				if tmp < 0:
					score -= tmp
				else:
					break
		
		if score > max_score:
			max_score = score

print(max_score)

Buggy python submission (verdict: Runtime Error):
import sys, heapq
from collections import deque
input = sys.stdin.readline
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''

N, K = map(int, input().split())
V_init = deque(map(int, input().split()))
max_score = 0
hands = [0] * K
heapq.heapify(hands)

for i_rget in range(K + 1):
	for i_lget in range(max(K - i_rget, 0) + 1):
		for i_del in range(max(K - i_rget - i_lget, 0) + 1):
			score = 0
			hands = [0] * K
			V = deque.copy(V_init)
			for i in range(i_rget + 1):
				if i > 0 and V:
					tmp = V.pop()					
					heapq.heappush(hands, tmp)
					score += tmp
				
			for i in range(i_lget + 1):
				if i > 0 and V:
					tmp = V.popleft()
					heapq.heappush(hands, tmp)
					score += tmp
			
			for i in range(i_del + 1):
				if i > 0:
					tmp = heapq.heappop(hands)
					score -= tmp
			
			if score > max_score:
				max_score = score

print(max_score)


Fixed submission:
import sys, heapq, copy
from collections import deque
input = sys.stdin.readline
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''

N, K = map(int, input().split())
V_init = deque(map(int, input().split()))
max_score = 0
hands = [0] * K
heapq.heapify(hands)

for i_rget in range(K + 1):
	for i_lget in range(max(K - i_rget, 0) + 1):
		tmp = 0
		score = 0
		hands = [0] * K
		V = copy.copy(V_init)
		for i in range(i_rget + 1):
			if i > 0 and V:
				tmp = V.pop()					
				heapq.heappush(hands, tmp)
				score += tmp
			
		for i in range(i_lget + 1):
			if i > 0 and V:
				tmp = V.popleft()
				heapq.heappush(hands, tmp)
				score += tmp
		
		for i in range(max(K - i_rget - i_lget, 0) + 1):
			if i > 0:
				tmp = heapq.heappop(hands)
				if tmp < 0:
					score -= tmp
				else:
					break
		
		if score > max_score:
			max_score = score

print(max_score)

Buggy python submission (verdict: Runtime Error):
import sys, heapq
from collections import deque
input = sys.stdin.readline
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''

N, K = map(int, input().split())
V_init = deque(map(int, input().split()))
max_score = 0
hands = [0] * K
heapq.heapify(hands)

for i_rget in range(K + 1):
	for i_lget in range(max(K - i_rget, 0) + 1):
		for i_del in range(max(K - i_rget - i_lget, 0) + 1):
			score = 0
			hands = [0] * K
			V = deque.copy(V_init)
			for i in range(i_rget + 1):
				if i > 0 and V:
					tmp = V.pop()					
					heapq.heappush(hands, tmp)
					score += tmp
				
			for i in range(i_lget + 1):
				if i > 0 and V:
					tmp = V.popleft()
					heapq.heappush(hands, tmp)
					score += tmp
			
			for i in range(i_del + 1):
				if i > 0:
					tmp = heapq.heappop(hands)
					score -= tmp
			
			if score > max_score:
				max_score = score

print(max_score)

Fixed submission:
import sys, heapq, copy
from collections import deque
input = sys.stdin.readline
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''

N, K = map(int, input().split())
V_init = deque(map(int, input().split()))
max_score = 0
hands = [0] * K
heapq.heapify(hands)

for i_rget in range(K + 1):
	for i_lget in range(max(K - i_rget, 0) + 1):
		tmp = 0
		score = 0
		hands = [0] * K
		V = copy.copy(V_init)
		for i in range(i_rget + 1):
			if i > 0 and V:
				tmp = V.pop()					
				heapq.heappush(hands, tmp)
				score += tmp
			
		for i in range(i_lget + 1):
			if i > 0 and V:
				tmp = V.popleft()
				heapq.heappush(hands, tmp)
				score += tmp
		
		for i in range(max(K - i_rget - i_lget, 0) + 1):
			if i > 0:
				tmp = heapq.heappop(hands)
				if tmp < 0:
					score -= tmp
				else:
					break
		
		if score > max_score:
			max_score = score

print(max_score)