Problem p02616 (python)

Fixed submission:
from collections import deque
rand = False
verbose = False

if rand:
    import random
    N, K = 200000, 10000
    A = random.choices(range(-10**9, 10**9), k=N)
else:
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))

MOD = 10**9 + 7

def mul(A):
    ret = 1
    for i in range(len(A)):
        ret = (ret * A[i]) % MOD
#        print (ret)
    return ret

if K==N:
    print(mul(A))
    exit()

POS, NEG = list(), list()
for i in range(len(A)):
    if A[i] < 0:
        NEG.append(A[i])
    else:
        POS.append(A[i])

POS.sort(reverse=True)
NEG.sort()

if len(POS)==0 and K%2==1: # must go negative
    print(mul(NEG[-K:]))
    exit()

POS = deque(POS)
NEG = deque(NEG)

ans = list()
while len(ans)<K:
    if len(NEG) <= 1:
        if verbose: print('no more neg numbers')
        ans.append(POS.popleft()) # no negative numbers to add
    elif len(POS) <= 1:
        if verbose: print('no more pos numbers')
        if K-len(ans)>=2:
            ans.append(NEG.popleft())
            ans.append(NEG.popleft())
        else:
            ans.append(POS.popleft())
    elif POS[0]*POS[1] > NEG[0]*NEG[1] or K-len(ans)==1:
        if verbose: print('pos bigger')
        ans.append(POS.popleft())
    else:
        if verbose: print('neg biger')
        ans.append(NEG.popleft())
        ans.append(NEG.popleft())

print(mul(ans))

Buggy submission (verdict: Runtime Error):
from collections import deque
rand = False
verbose = False

if rand:
    import random
    N, K = 200000, 10000
    A = random.choices(range(-10**9, 10**9), k=N)
else:
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))

MOD = 10**9 + 7

def mul(A):
    ret = 1
    for i in range(len(A)):
        ret = (ret * A[i]) % MOD
#        print (ret)
    return ret

if K==N:
    print(mul(A))
    exit()

POS, NEG = list(), list()
for i in range(len(A)):
    if A[i] < 0:
        NEG.append(A[i])
    else:
        POS.append(A[i])

POS.sort(reverse=True)
NEG.sort()

if len(POS)==0 and K%2==1: # must go negative
    print(mul(NEG[-K:]))
    exit()

POS = deque(POS)
NEG = deque(NEG)

ans = list()
while len(ans)<K:
    if len(NEG) <= 1:
        if verbose: print('no more neg numbers')
        ans.append(POS.popleft()) # no negative numbers to add
    elif len(POS) <= 1:
        if verbose: print('no more pos numbers')
        if K-len(ans)>=2:
            ans.append(NEG.popleft())
        else:
            ans.append(POS.popleft())
    elif POS[0]*POS[1] > NEG[0]*NEG[1] or K-len(ans)==1:
        if verbose: print('pos bigger')
        ans.append(POS.popleft())
    else:
        if verbose: print('neg biger')
        ans.append(NEG.popleft())
        ans.append(NEG.popleft())

print(mul(ans))

Buggy submission (verdict: Runtime Error):

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

MOD = 10**9 + 7

def mul(A):
    ret = 1
    for i in range(len(A)): ret = ret * A[i] % MOD
    return ret

if K==N:
    print(mul(A))
    exit()

POS, NEG = list(), list()
for i in range(len(A)):
    if A[i] < 0:
        NEG.append(A[i])
    else:
        POS.append(A[i])

POS.sort(reverse=True)
NEG.sort()

if len(POS)==0 and K%2==1: # must go negative
    print(mul(NEG[-K:]))
    exit()

ans = list()
while len(ans)<K:
    if len(NEG) <= 1:
        ans.append(POS.pop()) # no negative numbers to add
    elif len(POS) <= 1:
        if K-len(ans)>=2:
            ans.append(NEG[:K-len(ans)])
        else:
            ans.append(POS.pop())
    elif POS[0]*POS[1] > NEG[0]*NEG[1] or K-len(ans)==1:
        ans.append(POS.pop())
    else:
        ans.append(NEG.pop())
        ans.append(NEG.pop())

print(mul(ans))

Buggy submission (verdict: Runtime Error):
x = list(input().split())
a = list((int(i) for i in input().split()))
n, k = int(x[0]), int(x[1])
#import random
import queue
#n = random.choice(range(3, 8))
#k = random.choice(range(1, n))
#a = random.choices(range(-1000000, 1000000, 1), k=n)

MOD = 10**9 + 7

# check negative mode
neg = True
for i in range(n):
    if a[i] > 0:
        neg = False
if not (neg and k%2==1): neg = False
if neg: flag = 1
else: flag = -1

res = queue.PriorityQueue()
for i in range(n): res.put((flag * abs(a[i]), a[i]))
ans = 1
ansq = []

if neg: # negative case
    for i in range (k):
        (acur, cur) =  res.get()
        ans = ans * cur
        ansq.append(ans)

else: # positive case
    sm_pos, sm_neg, lg_pos, lg_neg = MOD, MOD, 0, 0
    for i in range (k):
        (acur, cur) = res.get()
        ans = ans * cur
        ansq.append(cur)
        if cur > 0 and sm_pos > acur: sm_pos = acur
        elif cur < 0 and sm_neg > acur: sm_neg = acur
    while res.qsize() > 0:
        (acur, cur) = res.get()
        if cur > 0 and lg_pos > acur: lg_pos = acur
        elif cur < 0 and lg_neg > acur: lg_neg = acur
        if lg_pos != 0 and lg_neg != 0: break
#    print (sm_pos, sm_neg, lg_pos, lg_neg, ans)
    if ans < 0:
        if lg_neg / sm_pos > lg_pos / sm_neg:
            ans = ans * lg_neg / sm_pos
        else:
            ans = ans * lg_pos / sm_neg

print (int(ans % MOD))

Buggy submission (verdict: Runtime Error):
x = list(input().split())
a = list((int(i) for i in input().split()))
n, k = int(x[0]), int(x[1])
#import random
#n = random.choice(range(10000))
#k = random.choice(range(n))
#a = random.choices(range(-1000000, 1000000, 1), k=n)
#print('n =', n, 'k =', k)

MOD = 10**9 + 7

a_abs = []
for i in range(len(a)): a_abs.append(abs(a[i]))

res = sorted(list(zip(a_abs, a)))
val = []
for i in range(len(res)): val.append(res.pop()[1])

# check negative mode
signA, signB, neg = True, False, False
for i in range(len(val)):
    if val[i] < 0:
        signA = not signA
    else:
        signB = True
if (n==k and not signA) or (not signB and k%2 == 1):
    neg = True # answer will be negative

index_replaced, index_replace = None, None
index_replaced_a, index_replace_a = None, None
if (not neg): # positive
    # take top k
    ans = val[0:k]
    sign = True
    for i in range(len(ans)):
        if ans[i] < 0:
            sign = not sign
            
    # replace if necessary
    if (not sign):
        # find smallest negative
        for i in range(len(ans), 0, -1):
            if ans[i-1] < 0:
                index_replaced = i-1
                break
        # find largest positive
        for i in range(k, len(val)):
            if val[i] > 0:
                index_replace = i
                break
        # find smallest positive
        for i in range(len(ans), 0, -1):
            if ans[i-1] > 0:
                index_replaced_a = i-1
                break
        # find largest negative
        for i in range(k, len(val)):
            if val[i] < 0:
                index_replace_a = i
                break

        if index_replaced==None or index_replace==None:
            ans[index_replaced_a] = val[index_replace_a]
        elif index_replaced_a==None or index_replace_a==None:
            ans[index_replaced] = val[index_replace]
        elif (ans[index_replaced]/val[index_replace] > 
            ans[index_replaced_a]/val[index_replace_a]):
            ans[index_replaced] = val[index_replace]
        else:
            ans[index_replaced_a] = val[index_replace_a]

    # multiplication
    result = 1
    for i in range(len(ans)):
        result = (result * ans[i]) % MOD

    print(result)

else: # negative case
    ans = val[len(val)-k:]
    result = 1
    for i in range(len(ans)):
        result = (result * ans[i]) % MOD

    print(result)

Buggy submission (verdict: Runtime Error):
x = list(input().split())
a = list((int(i) for i in input().split()))
n, k = int(x[0]), int(x[1])
#import random
#n = random.choice(range(10000))
#k = random.choice(range(n))
#a = random.choices(range(-1000000, 1000000, 1), k=n)
#print('n =', n, 'k =', k)

MOD = 10**9 + 7

a_abs = []
for i in range(len(a)): a_abs.append(abs(a[i]))

res = sorted(list(zip(a_abs, a)))
val = []
for i in range(len(res)): val.append(res.pop()[1])

# check negative mode
signA, signB, neg = True, False, False
for i in range(len(val)):
    if val[i] < 0:
        signA = not signA
    else:
        signB = True
if (n==k and not signA) or (not signB):
    neg = True # answer will be negative

index_replaced, index_replace = None, None
index_replaced_a, index_replace_a = None, None
if (not neg): # positive
    # take top k
    ans = val[0:k]
    sign = True
    for i in range(len(ans)):
        if ans[i] < 0:
            sign = not sign
            
    # replace if necessary
    if (not sign):
        # find smallest negative
        for i in range(len(ans), 0, -1):
            if ans[i-1] < 0:
                index_replaced = i-1
                break
        # find largest positive
        for i in range(k, len(val)):
            if val[i] > 0:
                index_replace = i
                break
        # find smallest positive
        for i in range(len(ans), 0, -1):
            if ans[i-1] > 0:
                index_replaced_a = i-1
                break
        # find largest negative
        for i in range(k, len(val)):
            if val[i] < 0:
                index_replace_a = i
                break

        if index_replaced==None or index_replace==None:
            ans[index_replaced_a] = val[index_replace_a]
        elif index_replaced_a==None or index_replace_a==None:
            ans[index_replaced] = val[index_replace]
        elif (ans[index_replaced]/val[index_replace] > 
            ans[index_replaced_a]/val[index_replace_a]):
            ans[index_replaced] = val[index_replace]
        else:
            ans[index_replaced_a] = val[index_replace_a]

    # multiplication
    result = 1
    for i in range(len(ans)):
        result = (result * ans[i]) % MOD

    print(result)

else: # negative case
    ans = val[len(val)-k:]
    result = 1
    for i in range(len(ans)):
        result = (result * ans[i]) % MOD

    print(result)

Buggy submission (verdict: Runtime Error):
x = list(input().split())
a = list((int(i) for i in input().split()))
n, k = int(x[0]), int(x[1])
#import random
#n = random.choice(range(10000))
#k = random.choice(range(n))
#a = random.choices(range(-1000000, 1000000, 1), k=n)
#print('n =', n, 'k =', k)

MOD = 10**9 + 7

a_abs = []
for i in range(len(a)): a_abs.append(abs(a[i]))

res = sorted(list(zip(a_abs, a)))
val = []
for i in range(len(res)): val.append(res.pop()[1])

# check negative mode
signA, signB, neg = True, False, False
for i in range(len(val)):
    if val[i] < 0:
        signA = not signA
    else:
        signB = True
if (n==k and not signA) or (not signB):
    neg = True # answer will be negative

index_replaced, index_replace = None, None
index_replaced_a, index_replace_a = None, None
if (not neg): # positive
    # take top k
    ans = val[0:k]
    sign = True
    for i in range(len(ans)):
        if ans[i] < 0:
            sign = not sign
            
    # replace if necessary
    if (not sign):
        # find smallest negative
        for i in range(len(ans), 0, -1):
            if ans[i-1] < 0:
                index_replaced = i-1
                break
        # find largest positive
        for i in range(k, len(val)):
            if val[i] > 0:
                index_replace = i
                break
        # find smallest positive
        for i in range(len(ans), 0, -1):
            if ans[i-1] > 0:
                index_replaced_a = i-1
                break
        # find largest negative
        for i in range(k, len(val)):
            if val[i] < 0:
                index_replace_a = i
                break

        if index_replaced==None or index_replace==None:
            ans[index_replaced_a] = val[index_replace_a]
        elif index_replaced_a==None or index_replace_a==None:
            ans[index_replaced] = val[index_replace]
        elif (ans[index_replaced]/val[index_replace] > 
            ans[index_replaced_a]/val[index_replace_a]):
            ans[index_replaced] = val[index_replace]
        else:
            ans[index_replaced_a] = val[index_replace_a]

    # multiplication
    result = 1
    for i in range(len(ans)):
        result = (result * ans[i]) % MOD

    print(result)

else: # negative case
    ans = val[len(val)-k:]
    result = 1
    for i in range(len(ans)):
        result = (result * ans[i]) % MOD

    print(result)

Buggy submission (verdict: Runtime Error):
x = list(input().split())
a = list((int(i) for i in input().split()))
n, k = int(x[0]), int(x[1])

MOD = 10**9 + 7

a_abs = []
for i in range(len(a)):
    a_abs.append(abs(a[i]))

res = sorted(list(zip(a_abs, a)))
val = []
for i in range(len(res)): val.append(res.pop()[1])

# check negative mode
signA, signB, neg = True, False, False
for i in range(len(val)):
    if val[i] < 0:
        signA = not signA
    else:
        signB = True
if (n==k and not signA) or (not signB):
    neg = True # answer will be negative

index_replaced, index_replace = None, None
index_replaced_a, index_replace_a = None, None
if (not neg): # positive
    # take top k
    ans = val[0:k]
    sign = True
    for i in range(len(ans)):
        if ans[i] < 0:
            sign = not sign
            
    # replace if necessary
    if (not sign):
        # find smallest negative
        for i in range(len(ans), 0, -1):
            if ans[i-1] < 0:
                index_replaced = i-1
                break
        # find largest positive
        for i in range(k, len(val)):
            if val[i] > 0:
                index_replace = i
                break
        # find smallest positive
        for i in range(len(ans), 0, -1):
            if ans[i-1] > 0:
                index_replaced_a = i-1
                break
        # find largest negative
        for i in range(k, len(val)):
            print(i, val[i])
            if val[i] < 0:
                index_replace_a = i
                break

        if index_replaced==None or index_replace==None:
            ans[index_replaced_a] = val[index_replace_a]
        elif index_replaced_a==None or index_replace_a==None:
            ans[index_replaced] = val[index_replace]
        elif (ans[index_replaced]/val[index_replace] > 
            ans[index_replaced_a]/val[index_replace_a]):
            ans[index_replaced] = val[index_replace]
        else:
            ans[index_replaced_a] = val[index_replace_a]

    # multiplication
    result = 1
    for i in range(len(ans)):
        result = (result * ans[i]) % MOD

    print(result)

else: # negative case
    ans = val[len(val)-k:]
    result = 1
    for i in range(len(ans)):
        result = (result * ans[i]) % MOD

    print(result)

Buggy submission (verdict: Runtime Error):
x = list(input().split())
a = list((int(i) for i in input().split()))
n, k = int(x[0]), int(x[1])

MOD = 10**9 + 7

a_abs = []
for i in range(len(a)):
    a_abs.append(abs(a[i]))

res = sorted(list(zip(a_abs, a)))
val = []
for i in range(len(res)): val.append(res.pop()[1])

# check negative mode
signA, signB, neg = True, False, False
for i in range(len(val)):
    if val[i] < 0:
        signA = not signA
    else:
        signB = True
if (n==k and not signA) or (not signB):
    neg = True # answer will be negative

if (not neg): # positive
    # take top k
    ans = val[0:k]
    sign = True
    for i in range(len(ans)):
        if ans[i] < 0:
            sign = not sign
            
    # replace if necessary
    if (not sign):
        index_replaced, index_replace = 0, 0
        # find smallest negative
        for i in range(k, 0, -1):
            if ans[i] < 0:
                index_replaced = i
                break
        # find largest positive
        for i in range(k, len(val)):
            if val[i] > 0:
                index_replace = i
        ans[index_replaced] = val[index_replace]

    # multiplication
    result = 1
    for i in range(len(ans)):
        result = (result * ans[i]) % MOD

    print(result)

else: # negative case
    ans = val[len(val)-k:]
    result = 1
    for i in range(len(ans)):
        result = (result * ans[i]) % MOD

    print(result)