Problem p02616 (python)

Fixed submission:
mod = 10**9+7

import sys
input = sys.stdin.buffer.readline

def main():

    n, k = map(int, input().split())
    A = list(map(int, input().split()))

    PZ = []
    N = []
    for a in A:
        if a >= 0:
            PZ.append(a)
        else:
            N.append(a)

    flag = True
    if len(PZ) > 0:
        if n == k:
            if len(N)%2 != 0:
                flag = False
    else:
        if k%2 != 0:
            flag = False

    if flag:
        if k%2 == 0:
            PZ.sort(reverse=True)
            N.sort()
            s = len(PZ)
            t = len(N)
            C = []
            for i in range(s//2):
                C.append(PZ[2*i]*PZ[2*i+1])
            for i in range(t//2):
                C.append(N[2*i]*N[2*i+1])
            C.sort(reverse=True)
            ans = 1
            for i in range(k//2):
                ans *= C[i]
                ans %= mod
        else:
            PZ.sort()
            ans = PZ.pop()
            PZ.reverse()
            N.sort()
            s = len(PZ)
            t = len(N)
            C = []
            for i in range(s//2):
                C.append(PZ[2*i]*PZ[2*i+1])
            for i in range(t//2):
                C.append(N[2*i]*N[2*i+1])
            C.sort(reverse=True)
            for i in range(k//2):
                ans *= C[i]
                ans %= mod
    else:
        ABS = [abs(a) for a in A]
        ABS.sort()
        ans = 1
        for i in range(k):
            ans *= ABS[i]
            ans %= mod
        ans *= -1
        ans %= mod
    print(ans)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
mod = 10**9+7

import sys
input = sys.stdin.buffer.readline

def main():

    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    P = []
    N = []

    for a in A:
        if a >= 0:
            P.append(a)
        else:
            N.append(a)
    P.sort(reverse=True)
    N.sort()
    #print(P)
    #print(N)

    if len(P) == 0:
        if k%2 == 0:
            N.sort()
            ans = 1
            for i in range(k):
                ans *= N[i]
                ans %= mod
        else:
            N.sort(reverse=True)
            ans = 1
            for i in range(k):
                ans *= N[i]
                ans %= mod
        print(ans)
        exit()

    if len(P) == 1:
        if k%2 == 0:
            N.sort()
            ans = 1
            for i in range(k):
                ans *= N[i]
                ans %= mod
        else:
            ans = P[0]
            N.sort(reverse=True)
            for i in range(k-1):
                ans *= N[i]
                ans %= mod
        print(ans)
        exit()


    if len(N) == 0:
        ans = 1
        for i in range(k):
            ans *= P[i]
            ans %= mod
        print(ans)
        exit()

    if len(N) == 1:
        if len(P) >= k:
            ans = 1
            for i in range(k):
                ans *= P[i]
                ans %= mod
            print(ans)
            exit()
        else:
            print(N[0])
            exit()

    import heapq
    if k%2 == 0:
        P_ = []
        N_ = []
        if len(P) != 1:
            for i in range(len(P)):
                if i%2 == 1:
                    continue
                if i+1 <= len(P)-1:
                    P_.append(-P[i]*P[i+1])
            for i in range(len(N)):
                if i%2 == 1:
                    continue
                if i+1 <= len(N)-1:
                    N_.append(-N[i]*N[i+1])
            heapq.heapify(P_)
            heapq.heapify(N_)
            q = k//2
            ans = 1
            for i in range(q):
                if P_:
                    x = heapq.heappop(P_)*(-1)
                else:
                    x = -1
                if N_:
                    y = heapq.heappop(N_)*(-1)
                else:
                    y = -1
                if x >= y:
                    ans *= x
                    ans %= mod
                    if y >= 0:
                        heapq.heappush(N_, y*(-1))
                else:
                    ans *= y
                    ans %= mod
                    if x >= 0:
                        heapq.heappush(P_, x*(-1))
        else:
            ans = 1
            for i in range(k):
                ans *= N[i]
                ans %= mod
    else:
        p = P[0]
        P = P[1:]
        P_ = []
        N_ = []
        if len(P) != 1:
            for i in range(len(P)):
                if i%2 == 1:
                    continue
                if i+1 <= len(P)-1:
                    P_.append(-P[i]*P[i+1])
            for i in range(len(N)):
                if i%2 == 1:
                    continue
                if i+1 <= len(N)-1:
                    N_.append(-N[i]*N[i+1])
            heapq.heapify(P_)
            heapq.heapify(N_)
            q = k//2
            ans = p
            for i in range(q):
                if P_:
                    x = heapq.heappop(P_)*(-1)
                else:
                    x = -1
                if N_:
                    y = heapq.heappop(N_)*(-1)
                else:
                    y = -1
                if x >= y:
                    ans *= x
                    ans %= mod
                    if y >= 0:
                        heapq.heappush(N_, y*(-1))
                else:
                    ans *= y
                    ans %= mod
                    if x >= 0:
                        heapq.heappush(P_, x*(-1))
        else:
            ans = p
            N.sort(reverse=True)
            for i in range(k):
                ans *= N[i]
                ans %= mod
    print(ans)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
mod = 10**9+7

import sys
input = sys.stdin.buffer.readline

def main():

    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    P = []
    N = []

    for a in A:
        if a >= 0:
            P.append(a)
        else:
            N.append(a)
    P.sort(reverse=True)
    N.sort()
    #print(P)
    #print(N)

    if len(P) == 0:
        if k%2 == 0:
            N.sort()
            ans = 1
            for i in range(k):
                ans *= N[i]
                ans %= mod
        else:
            N.sort(reverse=True)
            ans = 1
            for i in range(k):
                ans *= N[i]
                ans %= mod
        print(ans)
        exit()

    if len(P) == 1:
        if k%2 == 0:
            N.sort()
            ans = 1
            for i in range(k):
                ans *= N[i]
                ans %= mod
        else:
            ans = P[0]
            N.sort(reverse=True)
            for i in range(k-1):
                ans *= N[i]
                ans %= mod
        print(ans)
        exit()


    if len(N) == 0:
        ans = 1
        for i in range(k):
            ans *= P[i]
            ans %= mod
        print(ans)
        exit()

    if len(N) == 1:
        if len(P) >= k:
            ans = 1
            for i in range(k):
                ans *= P[i]
                ans %= mod
            print(ans)
            exit()
        else:
            print(N[0])
            exit()

    import heapq
    if k%2 == 0:
        P_ = []
        N_ = []
        if len(P) != 1:
            for i in range(len(P)):
                if i%2 == 1:
                    continue
                if i+1 <= len(P)-1:
                    P_.append(-P[i]*P[i+1])
            for i in range(len(N)):
                if i%2 == 1:
                    continue
                if i+1 <= len(N)-1:
                    N_.append(-N[i]*N[i+1])
            heapq.heapify(P_)
            heapq.heapify(N_)
            q = k//2
            ans = 1
            for i in range(q):
                if P_:
                    x = heapq.heappop(P_)*(-1)
                else:
                    x = -1
                if N_:
                    y = heapq.heappop(N_)*(-1)
                else:
                    y = -1
                if x >= y:
                    ans *= x
                    ans %= mod
                    if y >= 0:
                        heapq.heappush(N_, y*(-1))
                else:
                    ans *= y
                    ans %= mod
                    if x >= 0:
                        heapq.heappush(P_, x*(-1))
        else:
            ans = 1
            for i in range(k):
                ans *= N[i]
                ans %= mod
    else:
        p = P[0]
        P = P[1:]
        P_ = []
        N_ = []
        if len(P) != 1:
            for i in range(len(P)):
                if i%2 == 1:
                    continue
                if i+1 <= len(P)-1:
                    P_.append(-P[i]*P[i+1])
            for i in range(len(N)):
                if i%2 == 1:
                    continue
                if i+1 <= len(N)-1:
                    N_.append(-N[i]*N[i+1])
            heapq.heapify(P_)
            heapq.heapify(N_)
            q = k//2
            ans = p
            for i in range(q):
                if P_:
                    x = heapq.heappop(P_)*(-1)
                else:
                    x = -1
                if N_:
                    y = heapq.heappop(N_)*(-1)
                else:
                    y = -1
                if x >= y:
                    ans *= x
                    ans %= mod
                    if y >= 0:
                        heapq.heappush(N_, y*(-1))
                else:
                    ans *= y
                    ans %= mod
                    if x >= 0:
                        heapq.heappush(P_, x*(-1))
        else:
            ans = p
            for i in range(k):
                ans *= N[i]
                ans %= mod
    print(ans)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
mod = 10**9+7

import sys
input = sys.stdin.buffer.readline

def main():

    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    P = []
    N = []

    for a in A:
        if a >= 0:
            P.append(a)
        else:
            N.append(a)
    P.sort(reverse=True)
    N.sort()
    #print(P)
    #print(N)

    if len(P) == 0:
        if k%2 == 0:
            N.sort()
            ans = 1
            for i in range(k):
                ans *= N[i]
                ans %= mod
        else:
            N.sort(reverse=True)
            ans = 1
            for i in range(k):
                ans *= N[i]
                ans %= mod
        print(ans)
        exit()

    if len(P) == 1:
        if k%2 == 0:
            N.sort()
            ans = 1
            for i in range(k):
                ans *= N[i]
                ans %= mod
        else:
            ans = P[0]
            N.sort(reverse=True)
            for i in range(k-1):
                ans *= N[i]
                ans %= mod
        print(ans)
        exit()


    if len(N) == 0:
        ans = 1
        for i in range(k):
            ans *= P[i]
            ans %= mod
        print(ans)
        exit()

    if len(N) == 1:
        if len(P) >= k:
            ans = 1
            for i in range(k):
                ans *= P[i]
                ans %= mod
            print(ans)
            exit()
        else:
            print(N[0])
            exit()

    import heapq
    if k%2 == 0:
        P_ = []
        N_ = []
        if len(P) != 1:
            for i in range(len(P)):
                if i%2 == 1:
                    continue
                P_.append(-P[i]*P[i+1])
            for i in range(len(N)):
                if i%2 == 1:
                    continue
                N_.append(-N[i]*N[i+1])
            heapq.heapify(P_)
            heapq.heapify(N_)
            q = k//2
            ans = 1
            for i in range(q):
                if P_:
                    x = heapq.heappop(P_)*(-1)
                else:
                    x = -1
                if N_:
                    y = heapq.heappop(N_)*(-1)
                else:
                    y = -1
                if x >= y:
                    ans *= x
                    ans %= mod
                    if y >= 0:
                        heapq.heappush(N_, y*(-1))
                else:
                    ans *= y
                    ans %= mod
                    if x >= 0:
                        heapq.heappush(P_, x*(-1))
        else:
            ans = 1
            for i in range(k):
                ans *= N[i]
                ans %= mod
    else:
        p = P[0]
        P = P[1:]
        P_ = []
        N_ = []
        if len(P) != 1:
            for i in range(len(P)):
                if i%2 == 1:
                    continue
                P_.append(-P[i]*P[i+1])
            for i in range(len(N)):
                if i%2 == 1:
                    continue
                N_.append(-N[i]*N[i+1])
            heapq.heapify(P_)
            heapq.heapify(N_)
            q = k//2
            ans = p
            for i in range(q):
                if P_:
                    x = heapq.heappop(P_)*(-1)
                else:
                    x = -1
                if N_:
                    y = heapq.heappop(N_)*(-1)
                else:
                    y = -1
                if x >= y:
                    ans *= x
                    ans %= mod
                    if y >= 0:
                        heapq.heappush(N_, y*(-1))
                else:
                    ans *= y
                    ans %= mod
                    if x >= 0:
                        heapq.heappush(P_, x*(-1))
        else:
            ans = p
            for i in range(k):
                ans *= N[i]
                ans %= mod
    print(ans)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
mod = 10**9+7

import sys
input = sys.stdin.buffer.readline

def main():

    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    P = []
    N = []

    for a in A:
        if a >= 0:
            P.append(a)
        else:
            N.append(a)
    P.sort(reverse=True)
    N.sort()
    #print(P)
    #print(N)

    if len(P) == 0:
        if k%2 == 0:
            N.sort()
            ans = 1
            for i in range(k):
                ans *= N[i]
                ans %= mod
        else:
            N.sort(reverse=True)
            ans = 1
            for i in range(k):
                ans *= N[i]
                ans %= mod
        print(ans)
        exit()

    if len(P) == 1:
        if k%2 == 0:
            N.sort()
            ans = 1
            for i in range(k):
                ans *= N[i]
                ans %= mod
        else:
            ans = P[0]
            N.sort(reverse=True)
            for i in range(k-1):
                ans *= N[i]
                ans %= mod
        print(ans)
        exit()


    if len(N) == 0:
        ans = 1
        for i in range(k):
            ans *= P[i]
            ans %= mod
        print(ans)
        exit()

    if len(N) == 1:
        if len(P) >= k:
            ans = 1
            for i in range(k):
                ans *= P[i]
                ans %= mod
            print(ans)
            exit()
        else:
            print(N[0])
            exit()

    import heapq
    if k%2 == 0:
        P_ = []
        N_ = []
        if len(P) != 1:
            for i in range(len(P)):
                if i%2 == 1:
                    continue
                P_.append(-P[i]*P[i+1])
            for i in range(len(N)):
                if i%2 == 1:
                    continue
                N_.append(-N[i]*N[i+1])
            heapq.heapify(P_)
            heapq.heapify(N_)
            q = k//2
            ans = 1
            for i in range(q):
                if P_:
                    x = heapq.heappop(P_)*(-1)
                else:
                    x = -1
                if N_:
                    y = heapq.heappop(N_)*(-1)
                else:
                    y = -1
                if x >= y:
                    ans *= x
                    if y >= 0:
                        heapq.heappush(N_, y*(-1))
                else:
                    ans *= y
                    if x >= 0:
                        heapq.heappush(P_, x*(-1))
        else:
            ans = 1
            for i in range(k):
                ans *= N[i]
                ans %= mod
    else:
        p = P[0]
        P = P[1:]
        P_ = []
        N_ = []
        if len(P) != 1:
            for i in range(len(P)):
                if i%2 == 1:
                    continue
                P_.append(-P[i]*P[i+1])
            for i in range(len(N)):
                if i%2 == 1:
                    continue
                N_.append(-N[i]*N[i+1])
            heapq.heapify(P_)
            heapq.heapify(N_)
            q = k//2
            ans = p
            for i in range(q):
                if P_:
                    x = heapq.heappop(P_)*(-1)
                else:
                    x = -1
                if N_:
                    y = heapq.heappop(N_)*(-1)
                else:
                    y = -1
                if x >= y:
                    ans *= x
                    if y >= 0:
                        heapq.heappush(N_, y*(-1))
                else:
                    ans *= y
                    if x >= 0:
                        heapq.heappush(P_, x*(-1))
        else:
            ans = p
            for i in range(k):
                ans *= N[i]
                ans %= mod
    print(ans)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
A = list(map(int, input().split()))
mod = 10**9+7
P = []
N = []

for a in A:
    if a >= 0:
        P.append(a)
    else:
        N.append(a)
P.sort(reverse=True)
N.sort()
#print(P)
#print(N)

if len(P) == 0:
    if k%2 == 0:
        N.sort()
        ans = 1
        for i in range(k):
            ans *= N[i]
            ans %= mod
    else:
        N.sort(reverse=True)
        ans = 1
        for i in range(k):
            ans *= N[i]
            ans %= mod
    print(ans)
    exit()

if len(P) == 1:
    if k%2 == 0:
        N.sort()
        ans = 1
        for i in range(k):
            ans *= N[i]
            ans %= mod
    else:
        ans = P[0]
        N.sort(reverse=True)
        for i in range(k-1):
            ans *= N[i]
            ans %= mod
    print(ans)
    exit()


if len(N) == 0:
    ans = 1
    for i in range(k):
        ans *= P[i]
        ans %= mod
    print(ans)
    exit()

if len(N) == 1:
    if len(P) >= k:
        ans = 1
        for i in range(k):
            ans *= P[i]
            ans %= mod
        print(ans)
        exit()
    else:
        print(N[0])
        exit()

import heapq
if k%2 == 0:
    P_ = []
    N_ = []
    if len(P) != 1:
        for i in range(len(P)):
            if i%2 == 1:
                continue
            P_.append(-P[i]*P[i+1])
        for i in range(len(N)):
            if i%2 == 1:
                continue
            N_.append(-N[i]*N[i+1])
        heapq.heapify(P_)
        heapq.heapify(N_)
        q = k//2
        ans = 1
        for i in range(q):
            x = heapq.heappop(P_)*(-1)
            y = heapq.heappop(N_)*(-1)
            if x >= y:
                ans *= x
                heapq.heappush(N_, y*(-1))
            else:
                ans *= y
                heapq.heappush(P_, x*(-1))
    else:
        ans = 1
        for i in range(k):
            ans *= N[i]
            ans %= mod
else:
    p = P[0]
    P = P[1:]
    P_ = []
    N_ = []
    if len(P) != 1:
        for i in range(len(P)):
            if i%2 == 1:
                continue
            P_.append(-P[i]*P[i+1])
        for i in range(len(N)):
            if i%2 == 1:
                continue
            N_.append(-N[i]*N[i+1])
        heapq.heapify(P_)
        heapq.heapify(N_)
        q = k//2
        ans = p
        for i in range(q):
            x = heapq.heappop(P_)*(-1)
            y = heapq.heappop(N_)*(-1)
            if x >= y:
                ans *= x
                ans %= mod
                heapq.heappush(N_, y*(-1))
            else:
                ans *= y
                ans %= mod
                heapq.heappush(P_, x*(-1))
    else:
        ans = p
        for i in range(k):
            ans *= N[i]
            ans %= mod
print(ans)