Problem p02616 (python)

Fixed submission:
N,K = map(int,input().split())
A = list(map(int,input().split()))
mod = 10**9+7
A.sort()
ans = 1
if A[-1]==0 and K&1: print(0);exit()
if A[-1]<0 and K&1:
    for i in range(N-1,N-1-K,-1):
        ans =(ans*A[i])%mod
    print(ans)
    exit()
l,r = 0,N-1
if K&1:
    ans *= A[-1]
    r-=1
    K-=1
p = K//2
for i in range(p):
    lp = A[l]*A[l+1]
    rp = A[r]*A[r-1]
    if lp>rp:
        ans = (ans*lp)%mod
        l += 2
    else:
        ans = (ans*rp)%mod
        r-=2
print(ans%mod)

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
A = list(map(int,input().split()))
mod = 10**9+7
P = []
M = []
zero = []
for a in A:
    if a>0:
        P.append(a)
    elif a<0:
        M.append(a)
    else:
        zero.append(a)
if N==K and zero: print(0);exit()
if len(P)==0:
    if K%2==0:
        ans = 1
        if len(M)>=K:
            M.sort(reverse=True)
            for i in range(K):
                ans *=M[i]
                ans %=mod
            print(ans);exit()
        else: print(0);exit()
    else:
        ans = 1
        if len(M)>=K:
            M.sort()
            for i in range(K):
                ans *=M[i]
                ans %=mod
            print(ans);exit()
        else: print(0);exit()
P.sort(reverse=True)
M.sort(reverse=True)
if K==1:
    A.sort()
    print(A[-1]);exit()
if len(P)>=K:
    ans = 1
    P = P[:K]
    for i in range(K):
        ans *=P[i]
        ans %=mod
    while P:
        while len(M)>=2:
            a1 = M.pop()
            a2 = M.pop()
            b1 = P.pop()
            b2 = P.pop()
            if a1*a2 > b1*b2:
                ans = ans/b1
                ans = ans/b2
                ans *=a1*a2
                ans %=mod
            else:
                break
        break
else:
    ans = 1
    
    K -=len(P)
    if K>len(M):print(0);exit()
    else:
        if K%2!=0:
            if zero:print(0);exit()
            else:
                for i in range(len(P)-1):
                    ans *=P[i]
                    ans %=mod
                if K+1<=len(M):
                    while P:
                        while len(M)>=2:
                            a1 = M.pop()
                            a2 = M.pop()
                            b1 = P.pop()
                            b2 = P.pop()
                            if a1*a2 > b1*b2:
                                ans = ans/b1
                                ans = ans/b2
                                ans *=a1*a2
                                ans %=mod
                            else:
                                break
                        break
                else:
                    print(0);exit()
        else:
            for i in range(len(P)):
                ans *=P[i]
                ans %=mod
            if K<=len(M):
                while P:
                    while len(M)>=2:
                        a1 = M.pop()
                        a2 = M.pop()
                        b1 = P.pop()
                        b2 = P.pop()
                        if a1*a2 > b1*b2:
                            ans = ans/b1
                            ans = ans/b2
                            ans *=a1*a2
                            ans %=mod
                        else:
                            break
                    break
            else:
                print(0);exit()
print(int(ans%mod))

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
A = list(map(int,input().split()))
mod = 10**9+7
P = []
M = []
zero = []
for a in A:
    if a>0:
        P.append(a)
    elif a<0:
        M.append(a)
    else:
        zero.append(a)
if N==K and zero: print(0);exit()
if len(P)==0:
    if K%2==0:
        ans = 1
        if len(M)>=K:
            M.sort(reverse=True)
            for i in range(K):
                ans *=M[i]
                ans %=mod
            print(ans);exit()
        else: print(0);exit()
    else:
        ans = 1
        if len(M)>=K:
            M.sort()
            for i in range(K):
                ans *=M[i]
                ans %=mod
            print(ans);exit()
        else: print(0);exit()
P.sort(reverse=True)
M.sort(reverse=True)
if K==1:
    A.sort()
    print(A[-1]);exit()
if len(P)>=K:
    ans = 1
    P = P[:K]
    for i in range(K):
        ans *=P[i]
        ans %=mod
    while P:
        while len(M)>=2:
            a1 = M.pop()
            a2 = M.pop()
            b1 = P.pop()
            b2 = P.pop()
            if a1*a2 > b1*b2:
                ans = ans/b1
                ans = ans/b2
                ans *=a1*a2
                ans %=mod
            else:
                break
        break
else:
    ans = 1
    
    K -=len(P)
    if K>len(M):print(0);exit()
    else:
        if K%2!=0:
            if zero:print(0);exit()
            else:
                for i in range(len(P)-1):
                    ans *=P[i]
                    ans %=mod
                if K+1<=len(M):
                    while P:
                        while len(M)>=2:
                            a1 = M.pop()
                            a2 = M.pop()
                            b1 = P.pop()
                            b2 = P.pop()
                            if a1*a2 > b1*b2:
                                ans = ans/b1
                                ans = ans/b2
                                ans *=a1*a2
                                ans %=mod
                            else:
                                break
                        break
                else:
                    print(0);exit()
        else:
            for i in range(len(P)):
                    ans *=P[i]
                    ans %=mod
                if K<=len(M):
                    while P:
                        while len(M)>=2:
                            a1 = M.pop()
                            a2 = M.pop()
                            b1 = P.pop()
                            b2 = P.pop()
                            if a1*a2 > b1*b2:
                                ans = ans/b1
                                ans = ans/b2
                                ans *=a1*a2
                                ans %=mod
                            else:
                                break
                        break
                else:
                    print(0);exit()
print(int(ans%mod))

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
A = list(map(int,input().split()))
mod = 10**9+7
P = []
M = []
zero = []
for a in A:
    if a>0:
        P.append(a)
    elif a<0:
        M.append(a)
    else:
        zero.append(a)
if N==K and zero: print(0);exit()
if len(P)==0:
    if K%2==0:
        ans = 1
        if len(M)>=K:
            M.sort(reverse=True)
            for i in range(K):
                ans *=M[i]
                ans %=mod
            print(ans);exit()
        else: print(0);exit()
    else:
        ans = 1
        if len(M)>=K:
            M.sort()
            for i in range(K):
                ans *=M[i]
                ans %=mod
            print(ans);exit()
        else: print(0);exit()
P.sort(reverse=True)
M.sort(reverse=True)
if K==1:
    A.sort()
    print(A[-1]);exit()
if len(P)>=K:
    ans = 1
    P = P[:K]
    for i in range(K):
        ans *=P[i]
        ans %=mod
    while P:
        while len(M)>=2:
            a1 = M.pop()
            a2 = M.pop()
            b1 = P.pop()
            b2 = P.pop()
            if a1*a2 > b1*b2:
                ans = ans/b1
                ans = ans/b2
                ans *=a1*a2
                ans %=mod
            else:
                break
        break
else:
    ans = 1
    
    K -=len(P)
    if K>len(M):print(0);exit()
    else:
        if K%2!=0:
            if zero:print(0);exit()
            else:
                for i in range(len(P)-1):
                    ans *=P[i]
                    ans %=mod
                if K+1<=len(M):
                    while P:
                        while len(M)>=2:
                            a1 = M.pop()
                            a2 = M.pop()
                            b1 = P.pop()
                            b2 = P.pop()
                            if a1*a2 > b1*b2:
                                ans = ans/b1
                                ans = ans/b2
                                ans *=a1*a2
                                ans %=mod
                            else:
                                break
                        break
                else:
                    print(0);exit()
        else:
            for i in range(len(P)):
                    ans *=P[i]
                    ans %=mod
                if K<=len(M):
                    while P:
                        while len(M)>=2:
                            a1 = M.pop()
                            a2 = M.pop()
                            b1 = P.pop()
                            b2 = P.pop()
                            if a1*a2 > b1*b2:
                                ans = ans/b1
                                ans = ans/b2
                                ans *=a1*a2
                                ans %=mod
                            else:
                                break
                        break
                else:
                    print(0);exit()
print(int(ans%mod))