Problem p02616 (python)

Fixed submission:
N, K = map(int, input().split())
X = list(map(int, input().split()))
MOD = 10 ** 9 + 7
 
pos = sorted(v for v in X if v >= 0)
neg = sorted(-v for v in X if v < 0)
if N == K:
    ans = 1
    for x in X:
        ans *= x
        ans %= MOD
    print(ans % MOD)
    exit()
ok = False  # True: ans>=0, False: ans<0
if pos:
    #正の数があるとき
    ok = True
else:
    #全部負-> Kが奇数なら負にならざるをえない。
    ok = (K % 2 == 0)
 
ans = 1
if ok:
    # ans >= 0
    if K % 2 == 1:
        #Kが奇数の場合初めに1つ正の数を掛けておく
        #こうすれば後に非負でも負でも2つずつのペアで
        #考えられる
        ans = ans * pos.pop() % MOD
 
    # 答えは非負になる→二つの数の積は必ず非負になる．
    cand = []
    while len(pos) >= 2:
        x = pos.pop() * pos.pop()
        cand.append(x)
 
    while len(neg) >= 2:
        x = neg.pop() * neg.pop()
        cand.append(x)
 
    cand.sort(reverse=True) # ペアの積が格納されているので必ず非負
    # ペア毎に掛けていく
    for i in range(K // 2):
        ans = ans * cand[i] % MOD
else:
    # ans <= 0
    cand = sorted(X, key=lambda x: abs(x))
    for i in range(K):
        ans = ans * cand[i] % MOD
 
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = list(map(int, input().split()))
MOD = 10**9 + 7
if K == N:
    #そのままが答え
    ans = 1
    for a in A:
        ans *= a
        ans %= MOD
    print(ans % MOD)
    exit()

# pos = [a for a in A if a > 0]
neg = [a for a in A if a < 0]
n_neg = len(neg)
if n_neg == N and K % 2 == 1:
    #負になるしかない
    A.sort(reverse=True)
    ans = 1
    for i in range(K):
        ans *= A[i]
        ans %= MOD
    print(ans % MOD)
    exit()

# 正にできる
A.sort(key=lambda x: abs(x), reverse=True)
P = 1
is_p_neg = 0
for i in range(K):
    P *= A[i]
    if A[i] < 0:
        is_p_neg = not(is_p_neg)
        P %= MOD

if not is_p_neg:
    # P >= 0なら答え
    print(P%MOD)
    exit()

ng_left = N
ng_right = N
not_ng_right = N
po_left = N
for i in range(N):
    if A[i] > 0:
        po_left = i
        break
for i in range(N):
    if A[i] < 0:
        ng_left = i
        break
for i in range(K, N):
    if A[i] >= 0:
        not_ng_right = i
        break
for i in range(K, N):
    if A[i] > 0:
        ng_right = i
        break

A += [None] # right側のポインターが初期のままかどうかを判別
x1 = A[ng_left]
y1 = A[not_ng_right]
x2 = A[po_left]
y2 = A[ng_right]
if y1 is None:
    P //= x2
    P *= y2
    P %= MOD
elif y2 is None:
    P //= x1
    P *= y1
    P %= MOD
else:
    if abs(y1*x2) < abs(y2*x1):
        P //= x2
        P *= y2
        P %= MOD
    else:
        P //= x1
        P *= y1
        P %= MOD
print(P %MOD)

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

is_all_neg = 1
for i in range(N):
    if A[i] > 0:
        is_all_neg = 0
        
if is_all_neg:
    if K%2 == 1:
        A_1 = sorted(A, reverse=True)
        for i in range(K):
            ans *= A_1[i]
        print(ans % p)
    else:
        A_1 = sorted(A)
        for i in range(K):
            ans *= A_1[i]
        print(ans % p)
else:
    A_1 = sorted(A, key=lambda x: abs(x), reverse=True)
    is_pos = 1
    ans = 1
    minus = []
    for i in range(K):
        ans *= A_1[i] % p
        if A_1[i] < 0:
            minus.append(i)
        if ans > 0:
            is_pos = 1
        else:
            is_pos = 0
        if i == K-1:
            if is_pos:
                
                break
            else:
                idx = minus.pop()
                ans /= A_1[idx]
                for j in range(K, N):
                    if A_1[j] > 0:
                        ans *= A_1[j]
                        break
    print(int(ans)%p)

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

is_all_neg = 1
for i in range(N):
    if A[i] > 0:
        is_all_neg = 0
        
if is_all_neg:
    if K%2 == 1:
        A_1 = sorted(A, reverse=True)
        for i in range(K):
            ans *= A_1[i]
        print(ans % p)
    else:
        A_1 = sorted(A)
        for i in range(K):
            ans *= A_1[i]
        print(ans % p)
else:
    A_1 = sorted(A, key=lambda x: abs(x), reverse=True)
    is_pos = 1
    ans = 1
    minus = []
    for i in range(K):
        ans *= A_1[i]
        if A_1[i] < 0:
            minus.append(i)
        if ans > 0:
            is_pos = 1
        else:
            is_pos = 0
        if i == K-1:
            if is_pos:
                
                break
            else:
                
                idx = minus.pop()
                ans /= A_1[idx]
                for j in range(K-1, N):
                    if A_1[j] > 0:
                        ans *= A_1[j]
                        break
    print(int(ans)%p)