Problem p02616 (python)

Fixed submission:
n,k=map(int,input().split())
A=sorted(list(map(int,input().split())))


mod=10**9+7

if A[-1]<0 and k%2==1:
    now=1
    for i in range(k):
        now*=A[-i-1]%mod
        now%=mod
    print(now) ;exit()

l=0 ; r=n-1
ans=1
if k%2==1:
    ans=A[-1]
    r-=1

for i in range(k//2):
    mi=A[l]*A[l+1]
    pl=A[r]*A[r-1]
    if mi>pl:
        ans*= mi%mod
        ans%=mod
        l+=2
    else:
        ans*= pl%mod
        ans%=mod
        r-=2
print(ans%mod)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
lst = sorted(list(map(int, input().split())))
mod = 10 ** 9 + 7

if lst[-1] < 0 or k % 2 == 1:
    ans1 = 1
    for i in range(k):
        ans1 *= lst[- i - 1] % mod
        ans %= mod
    print(ans1)
    exit()
	
ans2 = 1
l = 0
r = n - 1
if k % 2 == 1:
    ans2 = lst[-1]
    r -= 1
	
for i in range(k // 2):
    left = lst[l] * lst[l + 1]
    right = lst[r - 1] * lst[r]
    if left > right:
        ans2 *= left % mod
        ans2 %= mod
        l += 2
    else:
        ans2 *= right % mod
        ans2 %= mod
        r -= 2
		
print(ans2 % mod)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
lst = sorted(list(map(int, input().split())))
mod = 10 ** 9 + 7

if lst[-1] < 0 or k % 2 == 1:
	ans1 = 1
	for i in range(k):
		ans1 *= lst[- i - 1] % mod
		ans %= mod
	print(ans1)
	exit()
	
ans2 = 1
l = 0
r = n - 1
if k % 2 == 1:
	ans2 = lst[-1]
	r -= 1
	
for i in range(k // 2):
	left = lst[l] * lst[l + 1]
	right = lst[r - 1] * lst[r]
	if left > right:
		ans2 *= left % mod
		ans2 %= mod
		l += 2
	else:
		ans2 *= right % mod
		ans2 %= mod
		r -= 2
		
print(ans2 % mod)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
lst = list(map(int, input().split()))
lst_abs = sorted(lst, key = abs, reverse = True)
#print("lst_abs", lst_abs)
lst_abs_rev = sorted(lst, key = abs, reverse = False)
#print("lst_abs_rev", lst_abs_rev)
lst_posi = [i for i in lst_abs if i >= 0]
#print("lst_posi", lst_posi)
lst_nega = [i for i in lst_abs if i < 0]
#print("lst_nega", lst_nega)
S = []
ans = 1

if lst_posi == [] and k % 2 == 1:
#	print("only nega and k is odd")
	for i in range(k):
		ans *= lst_abs_rev[i]

else:
	while len(S) != k:
		if len(S) == k - 1:
			S.append(lst_posi.pop(0))
#			print(S)
			
		elif len(lst_posi) < 2:
			if (k - len(S)) % 2 == 1:
				S.append(lst_posi.pop(0))
#				print(S)
				
			else:
				for i in range(k - len(S)):
					S.append(lst_nega.pop(0))
#					print(S)
						
		elif len(lst_nega) < 2:
			if (k - len(S)) % 2 == 1:
				S.append(lst_nega.pop(0))
#				print(S)
			else:
				for i in range(k - len(S)):
					S.append(lst_posi.pop(0))
#					print(S)
		elif lst_posi[0] * lst_posi[1] >= lst_nega[0] * lst_nega[1]:
			S.append(lst_posi.pop(0))
#			print(S)
		
		else:
			S.append(lst_nega.pop(0))
			S.append(lst_nega.pop(0))
#			print(S)

	for i in range(k):
		ans *= S[i]
			
print(ans % (10 ** 9 + 7))

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
lst = list(map(int, input().split()))
sortlst = sorted(lst, key = abs, reverse = True)
#print(sortlst)

signlst = ["posi" if sortlst[i] >= 0 else "nega" for i in range(n)] 
#print(signlst)

if "nega" not in signlst:
#	print("posi only")
	ans = 1
	for j in range(k):
		ans *= sortlst[j] 

elif "posi" not in signlst:
#	print("only nega")
	ans = 1
	if k % 2 == 0:
#		print("nega even")
		for l in range(k):
			ans *= sortlst[l]
		
	else:
#		print("nega odd")
		resortlst = sorted(lst, key = abs, reverse = False)
#		print(resortlst)
		ans = 1
		for m in range(k):
			ans *= resortlst[m]

else:
#	print("mix")
	ans = 0
	seki = 1
	for o in range(k):
		seki *= sortlst[o]

	if seki >= 0:
		ans = seki
		
	else:
		if "posi" not in signlst[k : n] and "nega" not in signlst[k : n]:
			ans = seki
			
		else:
			sortlst2 = sorted(sortlst[:k], reverse = True)
			minposi_index = max([i for i in range(k) if sortlst2[i] >= 0])
			kouho1 = seki / sortlst2[minposi_index + 1] * max(sortlst[k : n])
			kouho2 = seki / sortlst2[minposi_index] * min(sortlst[k : n])
			if "nega" not in signlst[k : n]:
				ans = kouho1
				
			elif "posi" not in signlst[k : n]:
				ans = kouho2
				
			else:
				ans = max(kouho1, kouho2)

#print(ans)
print(ans % (10 ** 9 + 7))