Problem p02616 (python)

Fixed submission:
import sys
input = sys.stdin.readline
mod  = int(1e9+7)

n,k = map(int,input().split())
L = list(map(int,input().split()))
ans = 1 
if k==n:
    for i in L:
        ans*=i
        ans%=mod
    print(ans%mod)
    sys.exit()

pos = []
neg = []
for i in L:
    if i>=0:
        pos.append(i)
    else:
        neg.append(i)

pos.sort(reverse=True)
neg.sort()

if len(pos) == 0 and k%2!=0:
    for i in range(1,k+1):
        ans *= neg[-i]
        ans %= mod
    print(ans%mod)
    sys.exit()

if k%2!=0:
    ans*=pos.pop(0)
    k-=1

l=[]
for i in range(1,len(pos),2):
    l.append(pos[i]*pos[i-1])
    
for i in range(1,len(neg),2):
    l.append(neg[i]*neg[i-1])

l.sort(reverse = True)
for i in range(k//2):
    ans *= l[i]
    ans %= mod
print(ans%mod)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
mod = int(1e9+7)
n,k = map(int,input().split())
L=list(map(int,input().split()))

ans = 1
if k==n:
    for i in L
        ans*=i
        ans%=mod
    print(ans%mod)
    sys.exit()
pos = []
neg = []
for i in L:
    if i>=0:
        pos.append(i)
    else:
        neg.append(i)
pos.sort(reverse=True)
if neg!=[]:
    neg.sort()
if len(pos) == 0 and k%2:
    for i in range(1,k+1):
        ans*=neg[-i]
        ans%=mod
    print(ans%mod)
    sys.exit()
if k%2:
    ans*=pos.pop(0)
    k-=1
l=[]
for i in range(1,len(pos),2):
    l.append(pos[i]*pos[i-1])

for i in range(1,len(neg),2):
    l.append(neg[i]*neg[i-1])
l.sort(reverse = True)

for i in range(k//2):
    ans*=l[i]
    ans%=mod

print(ans%mod)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
mod = int(1e9+7)
n,k = map(int,input().split())
L=list(map(int,input().split()))

ans = 1
"""
if k==n:
    for i in L
        ans*=i
        ans%=mod
    print(ans%mod)
    sys.exit()
"""
pos = []
neg = []
for i in L:
    if i>=0:
        pos.append(i)
    else:
        neg.append(i)
pos.sort(reverse=True)
if neg!=[]:
    neg.sort()
if len(pos) == 0 and k%2:
    for i in range(1,k+1):
        ans*=neg[-i]
        ans%=mod
    print(ans%mod)
    sys.exit()
if k%2:
    ans*=pos.pop(0)
    k-=1
l=[]
for i in range(1,len(pos),2):
    l.append(pos[i]*pos[i-1])

for i in range(1,len(neg),2):
    l.append(neg[i]*neg[i-1])
l.sort(reverse = True)

for i in range(k//2):
    ans*=l[i]
    ans%=mod

print(ans%mod)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
mod = int(1e9+7)
n,k = map(int,input().split())
L=list(map(int,input().split()))

ans = 1
"""
if k==n:
    for i in L
        ans*=i
        ans%=mod
    print(ans%mod)
    sys.exit()
"""
pos = []
neg = []
for i in L:
    if i>=0:
        pos.append(i)
    else:
        neg.append(i)
pos.sort(reverse=True)
neg.sort()
if len(pos) == 0 and k%2:
    for i in range(1,k+1):
        ans*=neg[-i]
        ans%=mod
    print(ans%mod)
    sys.exit()
if k%2:
    ans*=pos.pop(0)
    k-=1
l=[]
for i in range(1,len(pos),2):
    l.append(pos[i]*pos[i-1])

for i in range(1,len(neg),2):
    l.append(neg[i]*neg[i-1])
l.sort(reverse = True)

for i in range(k//2):
    ans*=l[i]
    ans%=mod

print(ans%mod)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
from itertools import accumulate
import operator as op
mod = int(1e9+7)
n,k = map(int,input().split())
L=list(map(int,input().split()))
L.sort()
zero = L.count(0)
if zero>0:
    L.remove(0)

l1 = L[:k]
l2 = L[n-zero-k:]
pr1 = 1
pr2 = 1
count1=0
count2=0 
val1 = []
val2 = []

for i in range(k):
    pr1 = abs(pr1* l1[i])%mod
    if l1[i]<0:
        count1+=1
        pr1 = ((-1)**count1)*pr1
    pr2 = abs(pr2* l2[i])%mod
    if l2[i]<0:
        count2+=1
        pr2 = ((-1)**count2)*pr2

val = max(pr1,pr2)
print(val%mod)