Problem p02616 (python)

Fixed submission:
import numpy as np

N, K = map(int, input().split())
A = list(map(int, input().split()))
M = 10**9 + 7

def prod(X):
    ans = 1
    for x in X:
        ans = ans * x % M
    return ans

def solve():
    # Case 1: N == K
    if N == K:
        return prod(A)

    # Case 2: ans is negative
    if all(a < 0 for a in A) and K % 2 == 1:
        return prod(sorted(A)[-K:])

    # Case 3: ans is positive
    posA = np.int64(sorted([a for a in A if a >= 0]))
    negA = np.int64(sorted([a for a in A if a < 0]))

    ans = 1
    if K % 2 == 1: # Keep the largest positive
        ans = ans * posA[-1] % M
        posA = posA[:-1]
    
    # Construct largest value by pairing items
    if len(posA) % 2 == 1: # Discard the one with smallest absolute value
        posA = posA[1:]
    if len(negA) % 2 == 1: # Discard the one with smallest absolute value
        negA = negA[:-1]
    pos_pairs = posA[0::2] * posA[1::2]
    neg_pairs = negA[0::2] * negA[1::2]
    pairs = np.concatenate([pos_pairs, neg_pairs], axis=0).tolist()
    pairs = sorted(pairs, reverse=True)[:K//2]
    ans = ans * prod(pairs) % M

    return ans

print(solve())

Buggy submission (verdict: Runtime Error):
import numpy as np

N, K = map(int, input().split())
A = list(map(int, input().split()))
M = 10**9 + 7

def prod(X):
    ans = 1
    for x in X:
        ans = ans * x % M
    return ans

def solve():
    # Case 1: N == K
    if N == K:
        return prod(A)

    # Case 2: ans is negative
    if all(a < 0 for a in A) and K % 2 == 1:
        return prod(sorted(A)[-K:])

    # Case 3: ans is positive
    posA = np.int64(sorted([a for a in A if a >= 0]))
    negA = np.int64(sorted([a for a in A if a < 0]))

    ans = 1
    if K % 2 == 1: # keep the largest positive
        ans = ans * posA[-1] % M
        posA = posA[:-1]
    
    pos_pairs = posA[0::2] * posA[1::2]
    neg_pairs = negA[0::2] * negA[1::2]
    pairs = np.concatenate([pos_pairs, neg_pairs], axis=0)
    pairs = np.sort(pairs)[-(K//2):].tolist()
    ans = ans * prod(pairs) % M
    return ans

print(solve())

Buggy submission (verdict: Runtime Error):
import numpy as np

N, K = map(int, input().split())
A = list(map(int, input().split()))
M = 10**9 + 7

def prod(X):
    ans = 1
    for x in X:
        ans = ans * x % M
    return ans

def solve():
    # Case 1: N == K
    if N == K:
        return prod(A)

    # Case 2: ans is negative
    if all(a < 0 for a in A) and K % 2 == 1:
        return prod(sorted(A)[-K:])

    # Case 3: ans is positive
    posA = np.int64(sorted([a for a in A if a >= 0]))
    negA = np.int64(sorted([a for a in A if a < 0]))

    ans = 1
    if K % 2 == 1: # keep the largest positive
        ans = ans * posA[-1] % M
        posA = posA[:-1]
    
    pos_pairs = posA[0::2] * posA[1::2]
    neg_pairs = negA[0::2] * negA[1::2]
    pairs = np.concatenate([pos_pairs, neg_pairs], axis=0)
    pairs = np.sort(pairs)[-K//2:].tolist()
    ans = ans * prod(pairs) % M
    return ans

print(solve())

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = list(map(int, input().split()))
M = 10**9 + 7

ans = 1
posA = sorted([a for a in A if a > 0])
negA = sorted([a for a in A if a < 0])
for _ in range(K // 2):
    if len(negA) < 2:
        ans = ans * posA[-1] * posA[-2] % M
        posA.pop()
        posA.pop()
        continue

    if len(posA) < 2:
        ans = ans * negA[-1] * negA[-2] % M
        negA.pop()
        negA.pop()
        continue

    p1 = negA[-1] * negA[-2]
    p2 = posA[-1] * posA[-2]
    if p1 > p2:
        ans = ans * p1 % M
        negA.pop()
        negA.pop()
    else:
        ans = ans * p2 % M
        posA.pop()
        posA.pop()

# print(ans)

if K % 2 == 1:
    if len(negA) == 0:
        ans = ans * posA[-1] % M
    elif len(posA) == 0:
        ans = ans * negA[-1] % M    
    else:
        if ans < 0:
            ans = ans * negA[-1] % M
        else:
            ans = ans * posA[-1] % M


print(ans)