Problem p03032 (python)

Fixed submission:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, log2
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, K = MAP()
V = LIST()
R = min(N, K)

max_ = 0
for A in range(R+1):
	for B in range(R+1):
		if A+B > R:
			continue
		if B == 0:
			lis = V[:A]
		else:
			lis = V[:A] +  V[-B:]
		# print(A, B)
		# print(lis)
		lis.sort()
		for i in range(K-(A+B)):
			if lis and lis[0] < 0:
				lis.pop(0)
		tmp = sum(lis)
		max_ = max(max_, tmp)
print(max_)

Buggy submission (verdict: Runtime Error):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, log2
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, K = MAP()
V = LIST()
max_ = 0
for j in range(K%2, K+1, 2):
	if j == 0:
		continue
	if j > N:  # N こまでしかとれない
		break
	# print("---j={}---".format(j))
	for i in range(j+1):  # jは残す数
		if (j+K)//2 >= N:
			tmp_lis = V[:]
		else:
			l = -i
			r = (j+K)//2 - i
			if l == 0 or r == -1:
				tmp_lis = V[l:r]
			else:
				tmp_lis = V[0:r] + V[l:]
		tmp_lis.sort(reverse=True)
		# print(tmp_lis)
		tmp = sum(tmp_lis[:j])
		max_ = max(max_, tmp)
		if (j == N or j == N-1) and K > N:  # とれる個数が多いとき
			for i in range(K-N):
				# print(tmp_lis)
				tmp_lis.pop()
				tmp = sum(tmp_lis)
				max_ = max(max_, tmp)
print(max_)

Buggy submission (verdict: Runtime Error):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, log2
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, K = MAP()
V = LIST()
max_ = 0
for j in range(K%2, K+1, 2):
	if j == 0:
		continue
	if j > N:  # N こまでしかとれない
		break
	# print("---j={}---".format(j))
	for i in range(j+1):  # jは残す数
		if (j+K)//2 >= N:
			tmp_lis = V[:]
		else:
			l = -i
			r = (j+K)//2 - i
			if l == 0 or r == -1:
				tmp_lis = V[l:r]
			else:
				tmp_lis = V[0:r] + V[l:]
		tmp_lis.sort(reverse=True)
		# print(tmp_lis)
		tmp = sum(tmp_lis[:j])
		max_ = max(max_, tmp)
		if (j == N or N-1) and K > N:  # とれる個数が多いとき
			for i in range(K-N):
				# print(tmp_lis)
				tmp_lis.pop()
				tmp = sum(tmp_lis)
				max_ = max(max_, tmp)
print(max_)