Problem p02616 (python)

Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7

mia = sorted([i for i in a if i < 0], reverse=True)
pla = sorted([i for i in a if i >= 0])

cnt = 1
if len(pla) == 0 and k % 2 == 1:
    for i in mia[:k]:
        cnt = cnt * i % mod
    print(cnt)
    exit()

while k > 0:
    if k == 1 or len(mia) <= 1:
        if len(pla) == 0:
            cnt *= mia.pop()
        elif len(pla) > 0:
            cnt *= pla.pop()
        k -= 1
    elif len(pla) <= 1:
        cnt *= mia.pop() * mia.pop()
        k -= 2
    elif pla[-1] * pla[-2] > mia[-1] * mia[-2]:
        cnt *= pla.pop()
        k -= 1
    elif pla[-1] * pla[-2] <= mia[-1] * mia[-2]:
        cnt *= mia.pop() * mia.pop()
        k -= 2
    cnt %= mod
else:
    print(cnt)

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():
    N, K = in_nn()
    A = in_nl()

    #import random
    #N, K = 100, 99
    #A = [random.randint(-10**9, 10**9) for _ in range(N)]

    mod = 10 ** 9 + 7

    Am = []
    Ap = []

    for i in range(N):
        if A[i] < 0:
            Am.append(A[i])
        elif A[i] >= 0:
            Ap.append(A[i])

    Ap = list(sorted(Ap, reverse=True))
    Am = list(sorted(Am))

    Nm = len(Am)
    Np = len(Ap)

    if Nm + Np == K:
        ans = 1
        for i in range(K):
            ans *= A[i]
            ans %= mod
    elif Np == 0:
        if K % 2 == 0:
            ans = 1
            for i in range(K):
                ans *= Am[i]
                ans %= mod
        else:
            ans = 1
            for i in range(K):
                ans *= Am[Nm - 1 - i]
                ans %= mod
    else:
        t = A[N + 1]
        count = 0
        nums = []

        Am = deque(Am)
        Ap = deque(Ap)

        while True:
            if count == K:
                break
            elif count == K - 1:
                nums.append(Ap.popleft())
                count += 1
            else:
                if len(Ap) >= 2 and len(Am) >= 2:
                    tp = Ap[0] * A[1]
                    tm = Am[0] * Am[1]
                    if tp > tm:
                        nums.append(Ap.popleft())
                        nums.append(Ap.popleft())
                    else:
                        nums.append(Am.popleft())
                        nums.append(Am.popleft())
                elif len(Ap) >= 2:
                    nums.append(Ap.popleft())
                    nums.append(Ap.popleft())
                elif len(Am) >= 2:
                    nums.append(Am.popleft())
                    nums.append(Am.popleft())
                count += 2

        ans = 1
        for n in nums:
            ans *= n
            ans %= mod
    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
from decimal import Decimal


read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():
    N, K = in_nn()
    A = in_nl()

    #import random
    #N, K = 100, 99
    #A = [random.randint(-10**9, 10**9) for _ in range(N)]
    mod = 10 ** 9 + 7

    Am = []
    Ap = []

    for i in range(N):
        if A[i] < 0:
            Am.append(A[i])
        elif A[i] >= 0:
            Ap.append(A[i])

    Ap = list(sorted(Ap, reverse=True))
    Am = list(sorted(Am))

    Nm = len(Am)
    Np = len(Ap)

    if Nm == 0:
        ans = 1
        for i in range(K):
            ans *= Ap[i]
            ans %= mod
    elif Nm == 1:
        if Nm + Np == K:
            ans = Am[0]
            for i in range(K - 1):
                ans *= Ap[i]
                ans %= mod
        else:
            ans = 1
            for i in range(K):
                ans *= Ap[i]
                ans %= mod
    elif Np == 0:
        if K % 2 == 0:
            ans = 1
            for i in range(K):
                ans *= Am[i]
                ans %= mod
        else:
            ans = 1
            for i in range(K):
                ans *= Am[Nm - 1 - i]
                ans %= mod
    else:
        if K % 2 == 0:
            if Np % 2 == 0:
                pi = Np
                mi = K - pi
            else:
                pi = Np - 1
                mi = K - pi
        else:
            if Np % 2 == 0:
                pi = Np - 1
                mi = K - pi
            else:
                pi = Np
                mi = K - pi
        rate = Decimal(1)
        max_rate = Decimal(1)
        max_i = (pi, mi)
        while True:
            pi -= 2
            mi += 2
            if pi >= 0 and mi <= Nm:
                tm = Decimal(Am[mi - 2] * Am[mi - 1])
                tp = Decimal(Ap[pi - 1] * Ap[pi])
                if tp == 0:
                    break
                rate *= tm / tp
                if max_rate < rate:
                    max_rate = rate
                    max_i = (pi, mi)
            else:
                break
        pi, mi = max_i
        ans = 1
        for i in range(pi):
            ans *= Ap[i]
            ans %= mod
        for i in range(mi):
            ans *= Am[i]
            ans %= mod

    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
from decimal import Decimal

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():
    N, K = in_nn()
    A = in_nl()
    mod = 10 ** 9 + 7

    Am = []
    Ap = []

    for i in range(N):
        if A[i] < 0:
            Am.append(A[i])
        elif A[i] >= 0:
            Ap.append(A[i])

    Ap = list(sorted(Ap, reverse=True))
    Am = list(sorted(Am))

    Nm = len(Am)
    Np = len(Ap)

    if Nm == 0:
        ans = 1
        for i in range(K):
            ans *= Ap[i]
            ans %= mod
    elif Nm == 1:
        if Nm + Np == K:
            ans = Am[0]
            for i in range(K - 1):
                ans *= Ap[i]
                ans %= mod
        else:
            ans = 1
            for i in range(K):
                ans *= Ap[i]
                ans %= mod
    elif Np == 0:
        if K % 2 == 0:
            ans = 1
            for i in range(K):
                ans *= Am[i]
                ans %= mod
        else:
            ans = 1
            for i in range(K):
                ans *= Am[Nm - 1 - i]
                ans %= mod
    else:
        if K % 2 == 0:
            if Np % 2 == 0:
                pi = Np
                mi = 0
            else:
                pi = Np - 1
                mi = 2
        else:
            if Np % 2 == 0:
                pi = Np - 1
                mi = 2
            else:
                pi = Np
                mi = 0
        rate = Decimal(1)
        max_rate = Decimal(1)
        max_i = (pi, mi)
        while True:
            pi -= 2
            mi += 2
            if pi >= 0 and mi <= Nm:
                tm = Decimal(Am[mi - 2] * Am[mi - 1])
                tp = Decimal(Ap[pi - 1] * Ap[pi])
                rate *= tm / tp
                if max_rate < rate:
                    max_rate = rate
                    max_i = (pi, mi)
            else:
                break
        pi, mi = max_i
        ans = 1
        for i in range(pi):
            ans *= Ap[i]
            ans %= mod
        for i in range(mi):
            ans *= Am[i]
            ans %= mod

    print(ans)


if __name__ == '__main__':
    main()