Problem p02616 (python)

Fixed submission:
n,k = map(int,input().split())
A = list(map(int,input().split()))
mod = 10**9+7
plus = []
minus=[]
for i in A:
    if i>=0:
        plus.append(i)
    else:
        minus.append(i)
ok = False
if len(plus)>0:
    if n==k:
        ok = (len(minus)%2==0)
    else:
        ok = True
else:
    ok = (k%2==0)
ans = 1
if not ok:
    A.sort(key=abs)
    for i in range(k):
        ans *= A[i]
        ans %= mod
else:
    plus.sort()
    minus.sort(reverse=True)
    if k%2 == 1:
        ans *= plus.pop()
    P = []
    while len(plus) >= 2:
        P.append((plus.pop())*(plus.pop()))
    while len(minus) >= 2:
        P.append((minus.pop())*(minus.pop()))
    P.sort(reverse=True)
    for i in range(k//2):
        ans *= P[i]
        ans %= mod
print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
nums = list(map(int, input().split()))
p = []
m = []

for i in nums:
    if i >= 0:
        p.append(i)
    else:
        m.append(i)

p.sort()
m.sort(reverse = True)

ans = 1
MOD = 1e9 + 7

if n == k:
    for i in nums:
        ans = ans * i % MOD
    print(int(ans))
    exit()

if not p and k % 2 == 1:
    for i in range(k):
        ans = ans * m[i] % MOD
    print(int(ans))
    exit()

ps = []
ms = []
cnt = 0
for i in range (k):
    if not p:
        ms.append(m.pop())
        continue
    if not m:
        ps.append(p.pop())
        cnt += 1
        continue
    
    if p[-1] >= abs(m[-1]):
        ps.append(p.pop())
    else:
        ms.append(m.pop())
        cnt += 1

if cnt % 2 == 1:
    if not ps or not ms or not p or not m:  
        if not ms or not p:
            ps.pop()
            ms.append(m.pop())
        if not ps or not m:
            ms.pop()
            ps.append(p.pop())
    elif m[-1] / ps[-1] >= p[-1] / ms[-1]:
        ps.pop()
        ms.append(m.pop())
    else:
        ms.pop()
        ps.append(p.pop())

for i in ps:
    ans = ans * i % MOD
for i in ms:
    ans = ans * i % MOD

print(int(ans))

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
nums = list(map(int, input().split()))
p = []
m = []

for i in nums:
    if i >= 0:
        p.append(i)
    else:
        m.append(i)

p.sort()
m.sort(reverse = True)

ans = 1
MOD = 1e9 + 7

if n == k:
    for i in nums:
        ans = ans * i % MOD
    print(int(ans))
    exit()

if not p and k % 2 == 1:
    for i in range(k):
        ans = ans * m[i] % MOD
    print(int(ans))
    exit()

ps = []
ms = []
cnt = 0
for i in range (k):
    if not p:
        ms.append(m.pop())
        continue
    if not m:
        ps.append(p.pop())
        cnt += 1
        continue
    
    if p[-1] >= abs(m[-1]):
        ps.append(p.pop())
    else:
        ms.append(m.pop())
        cnt += 1

if cnt % 2 == 1:
    if not p or m[-1] / ps[-1] >= p[-1] / ms[-1]:
        ps.pop()
        ms.append(m.pop())
    if not m or m[-1] / ps[-1] < p[-1] / ms[-1]:
        ms.pop()
        ps.append(p.pop())

for i in ps:
    ans = ans * i % MOD
for i in ms:
    ans = ans * i % MOD

print(int(ans))

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
nums = list(map(int, input().split()))
p = []
m = []

for i in nums:
    if i >= 0:
        p.append(i)
    else:
        m.append(i)

p.sort()
m.sort(reverse = True)

ans = 1
MOD = 1e9 + 7

if n == k:
    for i in nums:
        ans = ans * i % MOD
    print(ans)
    exit()

if not p and k % 2 == 1:
    for i in range(k):
        ans = ans * m[i] % MOD
    print(ans)
    exit()

ps = []
ms = []
cnt = 0
for i in range (k):
    if not p:
        ms.append(m.pop())
        continue
    if not m:
        ps.append(p.pop())
        cnt += 1
        continue
    
    if p[-1] >= abs(m[-1]):
        ps.append(p.pop())
    else:
        ms.append(m.pop())
        cnt += 1

if cnt % 2 == 1:
    if not p or m[-1] / ps[-1] >= p[-1] / ms[-1]:
        ps.pop()
        ms.append(m.pop())
    if not m or m[-1] / ps[-1] < p[-1] / ms[-1]:
        ms.pop()
        ps.append(p.pop())

for i in ps:
    ans = ans * i % MOD
for i in ms:
    ans = ans * i % MOD

print(ans)