Problem p02616 (python)

Fixed submission:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    M, P = [], []
    for a in A:
        M.append(a) if a < 0 else P.append(a)

    m, p = len(M), len(P)
    ok = False
    if p > 0:
        ok = m % 2 == 0 if n == k else True
    else:
        ok = k % 2 == 0

    res = 1
    if not ok:
        A.sort(key=lambda x: abs(x))
        for i in range(k):
            res = (res * A[i]) % mod
    else:
        P.sort()
        M.sort(reverse=True)
        if k % 2:
            a = P.pop()
            res = (res * a) % mod
        M_2 = []
        while len(P) >= 2:
            a = P.pop()
            a *= P.pop()
            M_2.append(a)
        while len(M) >= 2:
            a = M.pop()
            a *= M.pop()
            M_2.append(a)
        M_2.sort(reverse=True)
        for i in range(k // 2):
            res = (res * M_2[i]) % mod
    print(res)


if __name__ == '__main__':
    resolve()

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    P, M = [], []
    for a in A:
        P.append(a) if a >= 0 else M.append(a)

    res = 1
    if len(P) == n:
        A.sort(reverse=True)
        for i in range(k):
            res = (res * A[i]) % mod
    elif len(M) == n:
        if k % 2:
            A.sort(reverse=True)
        else:
            A.sort()
        res = 1
        for i in range(k):
            res = (res * A[i]) % mod
    else:
        P.sort(reverse=True)
        M.sort()
        B = []
        if k % 2:
            k -= 1
            b = P.pop(0)
            res = (res * b) % mod
        for i in range(0, len(P), 2):
            if i + 1 < len(P):
                B.append(P[i] * P[i + 1])
        for j in range(0, len(M), 2):
            if j + 1 < len(M):
                B.append(M[j] * M[j + 1])
        B.sort(reverse=True)
        for i in range(k // 2):
            res = (res * B[i]) % mod

    print(res)

if __name__ == '__main__':
    resolve()

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    minus = []
    plus = []
    for a in A:
        if a < 0:
            minus.append(a)
        else:
            plus.append(a)

    if len(minus) == 0:
        A.sort(reverse=True)
        res = 1
        for i in range(k):
            res = (res * A[i]) % mod
        print(res)
    elif len(plus) == 0:
        res = 1
        if k % 2 != 0:
            A.sort(reverse=True)
            for i in range(k):
                res = (res * A[i]) % mod
        else:
            A.sort()
            for i in range(k):
                res = (res * A[i]) % mod
        print(res)
    else:
        minus.sort()
        plus.sort(reverse=True)
        if len(plus) > 1 and len(minus) > 1:
            dp = [0 for _ in range(k + 1)]
            dp[0] = 1
            dp[1] = plus[0] % mod
            dp[2] = max(minus[0] * minus[1] % mod, plus[0] * plus[1] % mod)
            for i in range(3, k + 1):
                if i % 2 != 0:
                    dp[i] = dp[i - 1] * plus[i] % mod
                else:
                    dp[i] = max(dp[i - 1] * plus[i] % mod, dp[i - 2] * minus[i - 1] * minus[i] % mod)
            print(dp[-1])
        else:
            if len(plus) == 1:
                dp = [0 for _ in range(k + 1)]
                dp[0] = 1
                for i in range(1, k + 1):
                    if i % 2 != 0:
                        dp[i] = dp[i - 1] * plus[0] % mod
                    else:
                        dp[i] = max(dp[i - 2] * minus[i - 1] * minus[i] % mod)
                print(dp[-1])
            else:
                res = 1
                for i in range(k):
                    res = (res * plus[i]) % mod
                print(res)


if __name__ == '__main__':
    resolve()