Problem p02616 (python)

Fixed submission:
MOD = (10 ** 9) + 7
n, k  = map(int, input().split())
a = list(map(int, input().split()))
 
negs = [-x for x in a if x < 0]
non_negs = [x for x in a if x >= 0]
 
if len(non_negs) == 0 and k % 2 == 1:
    negs.sort()
    ans = 1
    for i in range(k):
        ans = ans * -negs[i] % MOD
    print(ans)
    exit()
 
negs_p, non_negs_p = 0, 0
 
negs.sort(reverse = True)
non_negs.sort(reverse = True)
 
for i in range(k):
    if negs_p == len(negs):
        non_negs_p += 1
    elif non_negs_p == len(non_negs):
        negs_p += 1
    elif negs[negs_p] > non_negs[non_negs_p]:
        negs_p += 1
    else:
        non_negs_p += 1
 
if negs_p % 2 == 0 or k == n:
    ans = 1
    for i in range(negs_p):
        ans = ans * -negs[i] % MOD
    for i in range(non_negs_p):
        ans = ans * non_negs[i] % MOD
    print(ans)
    exit()
 
if negs_p == len(negs) or non_negs_p == 0:
    negs_p -= 1
    non_negs_p += 1
    ans = 1
    for i in range(negs_p):
        ans = ans * -negs[i] % MOD
    for i in range(non_negs_p):
        ans = ans * non_negs[i] % MOD
    print(ans)
    exit()
 
if non_negs_p == len(non_negs) or negs_p == 0:
    negs_p += 1
    non_negs_p -= 1
    ans = 1
    for i in range(negs_p):
        ans = ans * -negs[i] % MOD
    for i in range(non_negs_p):
        ans = ans * non_negs[i] % MOD
    print(ans)
    exit()
 
 
a = negs[negs_p - 1]
b = negs[negs_p]
c = non_negs[non_negs_p - 1]
d = non_negs[non_negs_p]
 
if a * b > c * d:
    negs_p += 1
    non_negs_p -= 1
else:
    negs_p -= 1
    non_negs_p += 1
 
ans = 1
for i in range(negs_p):
    ans = ans * -negs[i] % MOD
for i in range(non_negs_p):
    ans = ans * non_negs[i] % MOD
print(ans)

Buggy submission (verdict: Runtime Error):
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, K = map(int, readline().split())
A = list(map(int,readline().split()))
MOD = 10**9 +7
B = []
C = []
BC = []
for a in A:
    if a >= 0:
        B.append(a)
        BC.append(a)
    else:
        C.append(-a)
        BC.append(-a)
ans = 1
if len(B) == 0:
    C = sorted(C)
    for i in range(K):
        ans *= C[i]
        ans %= MOD
    if K % 2 == 1:
        ans *= -1
        ans %= MOD
    print(ans)
    sys.exit()
if N == K:
    for i in range(K):
        ans *= BC[i]
        ans %= MOD
    if len(C) % 2 == 1:
        ans *= -1
        ans %= MOD
    print(ans)
    sys.exit()  
negs_p = 0
non_negs_p = 0
for i in range(K):
    if negs_p == len(C):
        non_negs_p += 1
    elif non_negs_p == len(B):
        negs_p += 1
    elif C[negs_p] > B[non_negs_p]:
        negs_p += 1
    else:
        non_negs_p += 1
B = sorted(B,reverse = True)
C = sorted(C,reverse = True)
BC = sorted(BC,reverse = True)

for i in range(K):
    ans *= BC[i]
    ans %= MOD

if negs_p % 2 == 1:
    if negs_p == len(C):      
        b,c = C[negs_p-1],B[non_negs_p]
        ans = (ans*MOD // b) * c % MOD
    elif non_negs_p == len(B):
        a,d = C[negs_p],B[non_negs_p-1]
        ans = (ans*MOD // d) * a % MOD
    else:
        a,b,c,d = C[negs_p],C[negs_p-1],B[non_negs_p],B[non_negs_p-1]
        if a*b > c*d:
            ans = (ans*MOD // d) * a % MOD
        else:
            ans = (ans*MOD // b) * c % MOD
print(ans)

Buggy submission (verdict: Runtime Error):
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, K = map(int, readline().split())
A = list(map(int,readline().split()))
MOD = 10**9 +7
B = []
C = []
BC = []
for a in A:
    if a >= 0:
        B.append(a)
        BC.append(a)
    else:
        C.append(-a)
        BC.append(-a)
ans = 1
if len(B) == 0:
    C = sorted(C)
    for i in range(K):
        ans *= C[i]
        ans %= MOD
    ans *= -1
    ans %= MOD
    print(ans)
    sys.exit()
if N == K:
    for i in range(K):
        ans *= BC[i]
        ans %= MOD
    if len(C) % 2 == 1:
        ans *= -1
        ans %= MOD
    print(ans)
    sys.exit()  
negs_p = 0
non_negs_p = 0
for i in range(K):
    if negs_p == len(C):
        non_negs_p += 1
    elif non_negs_p == len(B):
        negs_p += 1
    elif C[negs_p] > B[non_negs_p]:
        negs_p += 1
    else:
        non_negs_p += 1
B = sorted(B,reverse = True)
C = sorted(C,reverse = True)
BC = sorted(BC,reverse = True)

for i in range(K):
    ans *= BC[i]
    ans %= MOD

if negs_p % 2 == 1:
    if negs_p == len(C):      
        b,c = C[negs_p-1],B[non_negs_p]
        ans = (ans*MOD // b) * c % MOD
    elif non_negs_p == len(B):
        a,d = C[negs_p],B[non_negs_p-1]
        ans = (ans*MOD // d) * a % MOD
    else:
        a,b,c,d = C[negs_p],C[negs_p-1],B[non_negs_p],B[non_negs_p-1]
        if a*b > c*d:
            ans = (ans*MOD // d) * a % MOD
        else:
            ans = (ans*MOD // b) * c % MOD
print(ans)

Buggy submission (verdict: Runtime Error):
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, K = map(int, readline().split())
A = list(map(int,readline().split()))
MOD = 10**9 +7
B = []
C = []
BC = []
for a in A:
    if a >= 0:
        B.append(a)
        BC.append(a)
    else:
        C.append(-a)
        BC.append(-a)
ans = 1
if len(B) == 0:
    C = sorted(C)
    for i in range(K):
        ans *= C[i]
        ans %= MOD
    ans *= -1
    ans %= MOD
    print(ans)
    sys.exit()
if N == K:
    for i in range(K):
        ans *= BC[i]
        ans %= MOD
    if len(C) % 2 == 1:
        ans *= -1
        ans %= MOD
    print(ans)
    sys.exit()  
negs_p = 0
non_negs_p = 0
for i in range(K):
    if negs_p == len(C):
        non_negs_p += 1
    elif non_negs_p == len(B):
        negs_p += 1
    elif C[negs_p] > B[non_negs_p]:
        negs_p += 1
    else:
        non_negs_p += 1
B = sorted(B,reverse = True)
C = sorted(C,reverse = True)
BC = sorted(BC,reverse = True)

print(BC)
for i in range(K):
    ans *= BC[i]
    ans %= MOD

if negs_p % 2 == 1:
    if negs_p == len(C):      
        b,c = C[negs_p-1],B[non_negs_p]
        ans = (ans*MOD // b) * c % MOD
    elif non_negs_p == len(B):
        a,d = C[negs_p],B[non_negs_p-1]
        ans = (ans*MOD // d) * a % MOD
    else:
        a,b,c,d = C[negs_p],C[negs_p-1],B[non_negs_p],B[non_negs_p-1]
        if a*b > c*d:
            ans = (ans*MOD // d) * a % MOD
        else:
            ans = (ans*MOD // b) * c % MOD
print(ans)

Buggy submission (verdict: Runtime Error):
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, K = map(int, readline().split())
A = list(map(int,readline().split()))
MOD = 10**9 +7
B = []
C = []
BC = []
for a in A:
    if a >= 0:
        B.append(a)
        BC.append(a)
    else:
        C.append(-a)
        BC.append(-a)
ans = 1
if len(B) == 0:
    C = sorted(C)
    for i in range(K):
        ans *= C[i]
        ans %= MOD
    ans *= -1
    ans %= MOD
    print(ans)
    sys.exit()
if N == K:
    for i in range(K):
        ans *= BC[i]
        ans %= MOD
    if len(C) % 2 == 1:
        ans *= -1
        ans %= MOD
    print(ans)
    sys.exit()  
negs_p = 0
non_negs_p = 0
for i in range(K):
    if negs_p == len(C):
        non_negs_p += 1
    elif non_negs_p == len(B):
        negs_p += 1
    elif C[negs_p] > B[non_negs_p]:
        negs_p += 1
    else:
        non_negs_p += 1
B = sorted(B,reverse = True)
C = sorted(C,reverse = True)
BC = sorted(BC,reverse = True)

for i in range(K):
    ans *= BC[i]
    ans %= MOD
    
if negs_p % 2 == 1:
    if negs_p == len(C) and non_negs_p == len(B):
        pass
    elif negs_p == len(C):      
        b,c = C[negs_p-1],B[non_negs_p]
        ans = ans // b * c % MOD
    elif non_negs_p == len(B):
        a,d = C[negs_p],B[non_negs_p-1]
        ans = ans // d * a % MOD
    else:
        a,b,c,d = C[negs_p],C[negs_p-1],B[non_negs_p],B[non_negs_p-1]
        if a*b > c*d:
            ans = ans // d * a % MOD
        else:
            ans = ans // b * c % MOD
print(ans)

Buggy submission (verdict: Runtime Error):
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, K = map(int, readline().split())
A = list(map(int,readline().split()))
MOD = 10**9 +7
B = []
C = []
BC = []
for a in A:
    if a >= 0:
        B.append(a)
        BC.append(a)
    else:
        C.append(-a)
        BC.append(-a)
ans = 1
if K % 2 == 1 and len(B) == 0:
    C = sorted(C)
    for i in range(K):
        ans *= C[i]
        ans %= MOD
    ans *= -1
    ans %= MOD
    print(ans)
    sys.exit()
    
negs_p = 0
non_negs_p = 0
for i in range(K):
    if negs_p == len(C):
        non_negs_p += 1
    elif non_negs_p == len(B):
        negs_p += 1
    elif C[negs_p] > B[non_negs_p]:
        negs_p += 1
    else:
        non_negs_p += 1
B = sorted(B,reverse = True)
C = sorted(C,reverse = True)
BC = sorted(BC,reverse = True)

for i in range(K):
    ans *= BC[i]
    ans %= MOD
    
if negs_p % 2 == 1 and N != K:
    if negs_p == len(C) and non_negs_p == len(B):
        pass
    elif negs_p == len(C):      
        b,c = C[negs_p-1],B[non_negs_p]
        ans = ans // b * c % MOD
    elif non_negs_p == len(B):
        a,d = C[negs_p],B[non_negs_p-1]
        ans = ans // d * a % MOD
    else:
        a,b,c,d = C[negs_p],C[negs_p-1],B[non_negs_p],B[non_negs_p-1]
        if a*b > c*d:
            ans = ans // d * a % MOD
        else:
            ans = ans // b * c % MOD
print(ans)

Buggy submission (verdict: Runtime Error):
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, K = map(int, readline().split())
A = list(map(int,readline().split()))
MOD = 10**9 +7
B = []
C = []
BC = []
for a in A:
    if a >= 0:
        B.append(a)
        BC.append(a)
    else:
        C.append(-a)
        BC.append(-a)
ans = 1
if K % 2 == 1 and len(B) == 0:
    C = sorted(C)
    for i in range(K):
        ans *= C[i]
        ans %= MOD
    ans *= -1
    ans %= MOD
    print(ans)
    sys.exit()
    
negs_p = 0
non_negs_p = 0
for i in range(K):
    if negs_p == len(C):
        non_negs_p += 1
    elif non_negs_p == len(B):
        negs_p += 1
    elif C[negs_p] > B[non_negs_p]:
        negs_p += 1
    else:
        non_negs_p += 1
B = sorted(B,reverse = True)
C = sorted(C,reverse = True)
BC = sorted(BC,reverse = True)

for i in range(K):
    ans *= BC[i]
    ans %= MOD
    
if negs_p % 2 == 1:
    if negs_p == len(C) and non_negs_p == len(B):
        pass
    elif negs_p == len(C):      
        b,c = C[negs_p-1],B[non_negs_p]
        ans = ans // b * c % MOD
    elif non_negs_p == len(B):
        a,d = C[negs_p],B[non_negs_p-1]
        ans = ans // d * a % MOD
    else:
        a,b,c,d = C[negs_p],C[negs_p-1],B[non_negs_p],B[non_negs_p-1]
        if a*b > c*d:
            ans = ans // d * a % MOD
        else:
            ans = ans // b * c % MOD
print(ans)