Problem p02616 (python)

Fixed submission:
def prod(li, MOD):
    ret = 1
    for a in li:
        ret *= a
        ret %= MOD
    return ret

N, K = map(int, input().split())
MOD = 1_000_000_007
aaa = list(map(int, input().split()))
if N == K:
    print(prod(aaa, MOD))
    exit()
aaa.sort(key=lambda x : abs(x))
if sum(a < 0 for a in aaa[-K:]) % 2 == 0:
    print(prod(aaa[-K:], MOD))
else:
    if all(a <= 0 for a in aaa):
        bbb = aaa[:K]
    else:
        try:
            x1, y1 = min([a for a in aaa[-K:] if a > 0]), min([a for a in aaa[:-K] if a < 0])
        except:
            x1, y1 = 1, 0
        try:
            x2, y2 = max([a for a in aaa[-K:] if a < 0]), max([a for a in aaa[:-K] if a >= 0])
        except:
            x2, y2 = 1, 0
        bbb = aaa[-K:]
        if abs(x2 * y1) > abs(x1 * y2):
            bbb.remove(x1)
            bbb.append(y1)
        else:
            bbb.remove(x2)
            bbb.append(y2)
    print(prod(bbb, MOD))

Buggy submission (verdict: Runtime Error):
def prod(li, MOD):
    ret = 1
    for a in li:
        ret *= a
        ret %= MOD
    return ret

N, K = map(int, input().split())
MOD = 1_000_000_007
aaa = list(map(int, input().split()))
if N == K:
    print(prod(aaa, MOD))
    exit()
aaa.sort(key=lambda x : abs(x))
if sum(a < 0 for a in aaa[-K:]) % 2 == 0:
    print(prod(aaa[-K:], MOD))
else:
    if all(a <= 0 for a in aaa):
        bbb = aaa[:K]
    else:
        try:
            x1, y1 = min([a for a in aaa[-K:] if a > 0]), min([a for a in aaa[:-K] if a < 0])
        except:
            x1, y1 = 10**10, 1
        try:
            x2, y2 = max([a for a in aaa[-K:] if a < 0]), max([a for a in aaa[:-K] if a >= 0])
        except:
            x2, y2 = 10**10, 1
        bbb = aaa[-K:]
        if abs(x2 * y1) > abs(x1 * y2):
            bbb.remove(x1)
            bbb.append(y1)
        else:
            bbb.remove(x2)
            bbb.append(y2)
    print(prod(bbb, MOD))

Buggy submission (verdict: Runtime Error):
def prod(li, MOD):
    ret = 1
    for a in li:
        ret *= a
        ret %= MOD
    return ret

N, K = map(int, input().split())
MOD = 1_000_000_007
aaa = list(map(int, input().split()))
if N == K:
    print(prod(aaa, MOD))
    exit()
aaa.sort(key=lambda x : abs(x))
if sum(a < 0 for a in aaa[-K:]) % 2 == 0:
    print(prod(aaa[-K:], MOD))
else:
    if all(a <= 0 for a in aaa):
        bbb = aaa[:K]
    else:
        try:
            x1, y1 = max([a for a in aaa[-K:] if a > 0]), max([a for a in aaa[:-K] if a < 0])
        except:
            x1, y1 = 1, -(10**9+1)
        try:
            x2, y2 = max([a for a in aaa[-K:] if a < 0]), max([a for a in aaa[:-K] if a >= 0])
        except:
            x2, y2 = -(10**9+1), 1
        bbb = aaa[-K:]
        if abs(x2 * y1) > abs(x1 * y2):
            bbb.remove(x1)
            bbb.append(y1)
        else:
            bbb.remove(x2)
            bbb.append(y2)
    print(prod(bbb, MOD))

Buggy submission (verdict: Runtime Error):
def prod(li, MOD):
    ret = 1
    for a in li:
        ret *= a
        ret %= MOD
    return ret

N, K = map(int, input().split())
MOD = 1_000_000_007
aaa = list(map(int, input().split()))
if N == K:
    print(prod(aaa, MOD))
    exit()
if K == 1:
    print(max(aaa) % MOD)
    exit()
aaa.sort(key=lambda x : abs(x))
if sum(a < 0 for a in aaa[-K:]) % 2 == 0:
    print(prod(aaa[-K:], MOD))
else:
    if all(a <= 0 for a in aaa):
        print(prod(aaa[:K], MOD))
    elif all(a <= 0 for a in aaa[:-K]):
        for a in aaa[-K:][::-1]:
            if a > 0:
                break
        bbb = aaa[-K:].remove(a)
        bbb.append(aaa[-(K + 1)])
        print(prod(bbb, MOD))
    elif all(a > 0 for a in aaa[:-K]):
        for a in aaa[-K:][::-1]:
            if a < 0:
                break
        bbb = aaa[-K:].remove(a)
        bbb.append(aaa[-(K + 1)])
        print(prod(bbb, MOD))
    else:
        bbb = aaa[-(K-1):]
        for a in aaa[:-K][::-1]:
            if a * aaa[-K] <= 0:
                bbb.append(a)
                break
        print(prod(bbb, MOD))

Buggy submission (verdict: Runtime Error):
def prod(li, MOD):
    ret = 1
    for a in li:
        ret *= a
        ret %= MOD
    return ret

N, K = map(int, input().split())
MOD = 1_000_000_007
aaa = list(map(int, input().split()))
if N == K:
    print(prod(aaa, MOD))
    exit()
aaa.sort(key=lambda x : abs(x))
if sum(a < 0 for a in aaa[-K:]) % 2 == 0:
    print(prod(aaa[-K:], MOD))
else:
    if all(a <= 0 for a in aaa):
        print(prod(aaa[:K], MOD))
    elif all(a <= 0 for a in aaa[:-K]):
        for a in aaa[-K:][::-1]:
            if a > 0:
                break
        bbb = aaa[-K:].remove(a)
        bbb.append(aaa[-(K + 1)])
        print(prod(bbb, MOD))
    elif all(a > 0 for a in aaa[:-K]):
        for a in aaa[-K:][::-1]:
            if a < 0:
                break
        bbb = aaa[-K:].remove(a)
        bbb.append(aaa[-(K + 1)])
        print(prod(bbb, MOD))
    else:
        bbb = aaa[-(K-1):]
        for a in aaa[:-K][::-1]:
            if a * aaa[-K] <= 0:
                bbb.append(a)
                break
        print(prod(bbb, MOD))

Buggy submission (verdict: Runtime Error):
def prod(li, MOD):
    ret = 1
    for a in li:
        ret *= a
        ret %= MOD
    return ret

N, K = map(int, input().split())
MOD = 1_000_000_007
aaa = list(map(int, input().split()))
aaa.sort(key=lambda x : abs(x))
if sum(a < 0 for a in aaa[-K:]) % 2 == 0:
    print(prod(aaa[-K:], MOD))
else:
    if all(a <= 0 for a in aaa):
        print(prod(aaa[:K], MOD))
    elif all(a <= 0 for a in aaa[:-K]):
        for a in aaa[-K:]:
            if a > 0:
                break
        bbb = aaa[-K:].remove(a)
        bbb.append(aaa[-(K + 1)])
        print(prod(bbb, MOD))
    elif all(a > 0 for a in aaa[:-K]):
        for a in aaa[-K:]:
            if a < 0:
                break
        bbb = aaa[-K:].remove(a)
        bbb.append(aaa[-(K + 1)])
        print(prod(bbb, MOD))
    else:
        bbb = aaa[-(K-1):]
        for a in aaa[:-K]:
            if a * aaa[-K] <= 0:
                bbb.append(a)
        print(prod(bbb, MOD))

Buggy submission (verdict: Runtime Error):
def prod(li, MOD):
    ret = 1
    for a in li:
        ret *= a
        ret %= MOD
    return ret

N, K = map(int, input().split())
aaa = list(map(int, input().split()))
aaa.sort(key=lambda x : abs(x))
pus = []
miu = []
for i in range(K):
    a = aaa.pop()
    if a >= 0:
        pus.append(a)
    else:
        miu.append(a)
if len(pus) == 0:
    if len(miu) % 2 == 1:
        aaa.extend(miu)
        aaa.sort(key=lambda x : abs(x))
        miu = aaa[:K]
elif len(miu) == 0:
    pass
elif len(miu) % 2 == 1:
    x = min(pus) if abs(min(pus)) > abs(max(miu)) else max(miu)
    if x >= 0 and all(a >= 0 for a in aaa):
        miu.pop()
        pus.append(aaa[-1])
        pus.append(aaa[-2])
    elif x < 0 and all(a <= 0 for a in aaa):
        pus.pop()
        miu.append(aaa[-1])
        miu.append(aaa[-2])
    else:
        if abs(pus[-1]) > abs(miu[-1]):
            miu.pup()
        else:
            pus.pop()
        for y in aaa[::-1]:
            if x * y <= 0:
                if y >= 0:
                    pus.append(y)
                else:
                    miu.append(y)

ans = 1
MOD = 1_000_000_007
ans = prod(pus, MOD) * prod(miu, MOD)
ans %= MOD
print(ans)