Problem p02616 (python)

Fixed submission:
N,K=map(int,input().split())
A=list(map(int,input().split()))
MOD=10**9+7
p=[]
n=[]
for i in range(N):
    if A[i]<=0:
        n.append(A[i])
    else:
        p.append(A[i])
p.sort(reverse=True)
n.sort()
numplus=len(p)
if numplus==0:
    ans=1
    if K%2==1:
        for i in range(1,K+1):
            ans=(ans*n[i*(-1)])%MOD
    else:
        for i in range(K):
            ans=(ans*n[i])%MOD
elif N==K:
    ans=1
    for i in range(N):
        ans=(ans*A[i])%MOD
else:
    if numplus>=K:
        x=K
        if 0 in p:
            zero=p.index(0)
            len(p)-zero-1
    else:
        if (K-numplus)%2==1:
            x=numplus-1
        else:
            x=numplus
    ans=1
    for i in range(x):
        ans=(ans*p[i])%MOD
    for i in range(K-x):
        ans=(ans*n[i])%MOD
    temp=ans
    while x>1:
        if len(n)<(K-x+2):
            break
        if n[K-x]*n[K-x+1]<p[x-1]*p[x-2]:
            break
        temp=(temp*n[K-x]*n[K-x+1])%MOD
        temp=(temp*pow(p[x-1]*p[x-2],MOD-2,MOD))%MOD
        ans=temp
        x-=2
print(ans)

Buggy submission (verdict: Runtime Error):
N,K=map(int,input().split())
A=list(map(int,input().split()))
MOD=10**9+7
ans=1
p=[]
n=[]
for i in range(N):
    if A[i]<0:
        n.append(A[i])
    else:
        p.append(A[i])
p.sort(reverse=True)
n.sort()
numplus=len(p)
if N==K:
    for i in range(N):
        ans=(ans*A[i])%MOD
elif numplus==0 and K%2==1:
    for i in range(1,K+1):
        ans=(ans*n[i*(-1)])%MOD
else:
    minus=0
    A.sort(reverse=True,key=lambda x:abs(x))
    for i in range(K):
        ans=(ans*A[i])%MOD
        if A[i]<0:
            minus+=1
    if minus%2==1 and ans!=0:
        pwaru=float('inf')
        nwaru=float('inf')
        for i in range(K-1,-1,-1):
            if A[i]>0:
                pwaru=A[i]
            elif A[i]<0:
                nwaru=B[i]
            if pwaru!=float('inf') and nwaru != float('inf'):
                break
        if pwaru==float('inf'):
            pkakeru=float('inf')
            for i in range(K,N):
                if A[i]>=0:
                    pkakeru=A[i]
                    break
            ans=(ans*pkakeru*pow(nwaru,MOD-2,MOD))%MOD
        else:
            pkakeru=0
            for i in range(K,N):
                if A[i]>=0:
                    pkakeru=A[i]
                    break
            nkakeru=0
            for i in range(K,N):
                if A[i]<0:
                    nkakeru=A[i]
                    break
            if nwaru*nkakeru>pwaru*pkakeru:
                ans=(ans*nkakeru*pow(pwaru,MOD-2,MOD))%MOD
            else:
                ans=(ans*pkakeru[i]*pow(nwaru,MOD-2,MOD))%MOD
print(ans)

Buggy submission (verdict: Runtime Error):
N,K=map(int,input().split())
A=list(map(int,input().split()))
MOD=10**9+7
ans=1
p=[]
n=[]
for i in range(N):
    if A[i]<0:
        n.append(A[i])
    else:
        p.append(A[i])
p.sort(reverse=True)
n.sort()
numplus=len(p)
if N==K:
    for i in range(N):
        ans=(ans*A[i])%MOD
elif numplus==0 and K%2==1:
    for i in range(1,K+1):
        ans=(ans*n[i*(-1)])%MOD
else:
    minus=0
    A.sort(reverse=True,key=lambda x:abs(x))
    for i in range(K):
        ans=(ans*A[i])%MOD
        if A[i]<0:
            minus+=1
    if minus%2==1:
        B=A[:K]
        for i in range(K-1,-1,-1):
            if B[i]<0:
                waru=B[i]
                break
        for i in range(K,N):
            if A[i]>=0:
                kakeru=A[i]
                break
        ans=(ans*A[i]*pow(B[i],MOD-2,MOD))%MOD
print(ans)

Buggy submission (verdict: Runtime Error):
N,K=map(int,input().split())
A=list(map(int,input().split()))
MOD=10**9+7
p=[]
n=[]
for i in range(N):
    if A[i]<0:
        n.append(A[i])
    else:
        p.append(A[i])
p.sort(reverse=True)
n.sort()
numplus=len(p)
if numplus==0:
    ans=1
    if K%2==1:
        for i in range(1,K+1):
            ans=(ans*n[i*(-1)])%MOD
    else:
        for i in range(K):
            ans=(ans*n[i])%MOD
elif N==K:
    ans=1
    for i in range(N):
        ans=(ans*A[i])%MOD
else:
    if numplus>=K:
        x=numplus
    else:
        if (K-numplus)%2==1:
            x=numplus-1
        else:
            x=numplus
    ans=1
    for i in range(x):
        ans=(ans*p[i])%MOD
    for i in range(K-x):
        ans=(ans*n[i])%MOD
    temp=ans
    bai=1
    while x>1:
        if len(n)<(K-x+2):
            break
        if n[K-x]*n[K-x+1]<p[x-1]*p[x-2]:
            break
        temp=(temp*n[K-x]*n[K-x+1])%MOD
        temp=(temp*pow(p[x-1]*p[x-2],MOD-2,MOD))%MOD
        ans=temp
        x-=2
print(ans)

Buggy submission (verdict: Runtime Error):
N,K=map(int,input().split())
A=list(map(int,input().split()))
MOD=10**9+7
p=[]
n=[]
for i in range(N):
    if A[i]<0:
        n.append(A[i])
    else:
        p.append(A[i])
p.sort(reverse=True)
n.sort()
numplus=len(p)
if numplus==0:
    ans=1
    for i in range(1,K+1):
        ans=(ans*n[i*(-1)])%MOD
elif N==K:
    ans=1
    for i in range(N):
        ans=(ans*A[i])%MOD
else:
    if numplus>=K:
        x=numplus
    else:
        if (K-numplus)%2==1:
            x=numplus-1
        else:
            x=numplus
    ans=1
    for i in range(x):
        ans=(ans*p[i])%MOD
    for i in range(K-x):
        ans=(ans*n[i])%MOD
    temp=ans
    bai=1
    while x>1:
        if len(n)<(K-x+2):
            break
        bai*=(n[K-x]*n[K-x+1])/(p[x-1]*p[x-2])
        temp=(temp*n[K-x]*n[K-x+1])%MOD
        temp=(temp*pow(p[x-1]*p[x-2],MOD-2,MOD))%MOD
        if bai>1:
            ans=temp
            bai=1
        x-=2
print(ans)

Buggy submission (verdict: Runtime Error):
N,K=map(int,input().split())
A=list(map(int,input().split()))
MOD=10**9+7
p=[]
n=[]
for i in range(N):
    if A[i]<0:
        n.append(A[i])
    else:
        p.append(A[i])
p.sort(reverse=True)
n.sort()
numplus=len(p)
if numplus==0:
    ans=1
    for i in range(1,K+1):
        ans=(ans*n[i*(-1)])%MOD
else:
    if numplus>=K:
        x=K
    else:
        if (K-numplus)%2==1:
            x=numplus-1
        else:
            x=numplus
    ans=1
    for i in range(x):
        ans=(ans*p[i])%MOD
    for i in range(K-x):
        ans=(ans*n[i])%MOD
    temp=ans
    bai=1
    while x>1:
        if len(n)<(K-x+1):
            break
        bai*=(n[K-x]*n[K-x+1])/(p[x-1]*p[x-2])
        temp=(temp*n[K-x]*n[K-x+1])%MOD
        temp=(temp*pow(p[x-1]*p[x-2],MOD-2,MOD))%MOD
        if bai>1:
            ans=temp
            bai=1
        x-=2
print(ans)