Problem p02616 (python)

Fixed submission:
from collections import deque
n , k = map(int,input().split())
a = list(map(int,input().split()))
mod = 10**9 + 7
ans = 1
plus = []
minus = []

if n == k:
    for i in range(n):
        ans *= a[i]
        ans %= mod
    print(ans)
    exit()
    
for i in range(n):
    if a[i] >= 0:
        plus.append(a[i])
    elif a[i] < 0:
        minus.append(a[i])

if not plus and k % 2 == 1:
    minus.sort(reverse=True)
    for i in range(k):
        ans *= minus[i]
        ans %= mod
    print(ans)
    exit()

plus.sort(reverse=True)
minus.sort()
plus = deque(plus)
minus = deque(minus)

cou = []
while True:
    if len(cou) == k:
        break
    elif len(cou) == k-1:
        cou.append(plus.popleft())
        break
    else:
        if len(plus)>=2 and len(minus)>=2:
            p1 = plus.popleft()
            p2 = plus.popleft()
            m1 = minus.popleft()
            m2 = minus.popleft()
            if p1*p2 > m1*m2:
                cou.append(p1)
                plus.appendleft(p2)
                minus.appendleft(m2)
                minus.appendleft(m1)
            else:
                cou.append(m1)
                cou.append(m2)
                plus.appendleft(p2)
                plus.appendleft(p1)
        elif len(plus) < 2:
            m1 = minus.popleft()
            m2 = minus.popleft()
            cou.append(m1)
            cou.append(m2)
        elif len(minus) < 2:
            p1 = plus.popleft()
            cou.append(p1)

        
for i in cou:
    ans *= i
    ans %= mod
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import deque
n , k = map(int,input().split())
a = list(map(int,input().split()))
mod = 10**9 + 7
a.sort(reverse=True)
ans = 1
plus = []
minus = []

if n == k:
    for i in range(n):
        ans *= a[i]
        ans %= mod
    print(ans)
    exit()
    
for i in range(n):
    if a[i] >= 0:
        plus.append(a[i])
    elif a[i] < 0:
        minus.append(a[i])
if not plus and k % 2 == 1:
    minus.sort(reverse=True)
    for i in minus:
        ans *= minus
        ans %= mod
    print(ans)
    exit()
plus.sort(reverse=True)
minus.sort()
plus = deque(plus)
minus = deque(minus)
cou = []
while True:
    if len(cou) == k:
        break
    elif len(cou) == k-1:
        cou.append(plus.popleft())
        break
    else:
        if len(plus)>=2 and len(minus)>=2:
            p1 = plus.popleft()
            p2 = plus.popleft()
            m1 = minus.popleft()
            m2 = minus.popleft()
            if p1*p2 > m1*m2:
                cou.append(p1)
                cou.append(p2)
                minus.appendleft(m2)
                minus.appendleft(m1)
            else:
                cou.append(m1)
                cou.append(m2)
                plus.appendleft(p2)
                plus.appendleft(p1)
        elif len(plus) < 2:
            m1 = minus.popleft()
            m2 = minus.popleft()
            cou.append(m1)
            cou.append(m2)
        elif len(minus) < 2:
            p1 = plus.popleft()
            p2 = plus.popleft()
            cou.append(p1)
            cou.append(p2)
        
for i in cou:
    ans *= cou
    ans %= mod
print(ans)

Buggy submission (verdict: Runtime Error):
n , k = map(int,input().split())
a = list(map(int,input().split()))
mod = 10**9 + 7
a.sort(reverse=True)
ans = 1
plus = []
minus = []
zero = 0
for i in range(n):
    if a[i] > 0:
        plus.append(a[i])
    elif a[i] < 0:
        minus.append(a[i])
    elif a[i] == 0:
        zero += 1
if len(plus) + len(minus) < k:
    print(0)
elif len(minus) == 0:
    for i in range(k):
        ans *= plus[i]
        ans %= mod
    print(ans)
elif len(plus) == 0:
    if k % 2 == 0:
        for i in range(k):
            ans *= minus[n-1-i]
            ans %= mod
        print(ans)
    elif k % 2 == 1:
        if zero >= 1:
            print(0)
        elif zero == 0:
            for i in range(k):
                ans *= minus[i]
                ans %= mod
            print(ans)
elif len(plus) + len(minus) == k:
    if len(minus) % 2 == 1 and zero >= 1:
        print(0)
    else:
        for i in plus:
            ans *= i
            ans %= mod
        for i in minus:
            ans *= i
            ans %= mod
        print(ans)
elif len(plus) + len(minus) == k+1:
    if len(minus) % 2 == 1:
        for i in plus:
            ans *= i
            ans %= mod
        minus.sort()
        for i in range(len(minus)-1):
            ans *= minus[i]
            ans %= mod
        print(ans)
    elif len(minus) % 2 == 0:
        for i in range(len(plus)-1):
            ans *= plus[i]
            ans %= mod
        for i in minus:
            ans *= i
            ans %= mod
        print(ans)
elif len(plus) + len(minus) == k+2 and len(plus) % 2 == 1 and len(minus) % 2 == 1:
    for i in range(len(plus)-1):
        ans *= plus[i]
        ans %= mod
    minus.sort()
    for i in range(len(minus)-1):
        ans *= minus[i]
        ans %= mod
    print(ans)
else:
    if k % 2 == 1:
        ans = plus[0]
        del plus[0]
        k -= 1
    re = []
    for i in range(k//2):
        if 2*i + 1 >= len(plus):
            break
        re.append(plus[2*i]*plus[2*i+1])
    minus.sort()
    for i in range(k//2):
        if 2*i + 1 >= len(plus):
            break
        re.append(minus[2*i]*minus[2*i+1])
    re.sort(reverse=True)
    for i in range(k//2):
        ans *= re[i]
        ans %= mod
    print(ans)

Buggy submission (verdict: Runtime Error):
n , k = map(int,input().split())
a = list(map(int,input().split()))
mod = 10**9 + 7
a.sort(reverse=True)
ans = 1
plus = []
minus = []
zero = 0
for i in range(n):
    if a[i] > 0:
        plus.append(a[i])
    elif a[i] < 0:
        minus.append(a[i])
    elif a[i] == 0:
        zero += 1
if len(plus) + len(minus) < k:
    print(0)
elif len(minus) == 0:
    for i in range(k):
        ans *= plus[i]
        ans %= mod
    print(ans)
elif len(plus) == 0:
    if k % 2 == 0:
        for i in range(k):
            ans *= minus[n-1-i]
            ans %= mod
        print(ans)
    elif k % 2 == 1:
        if zero >= 1:
            print(0)
        elif zero == 0:
            for i in range(k):
                ans *= minus[i]
                ans %= mod
            print(ans)
elif len(plus) + len(minus) == k:
    if len(minus) % 2 == 1 and zero >= 1:
        print(0)
    else:
        for i in plus:
            ans *= i
            ans %= mod
        for i in minus:
            ans *= i
            ans %= mod
        print(ans)
else:
    if k % 2 == 1:
        ans = plus[0]
        del plus[0]
        k -= 1
    re = []
    for i in range(k//2):
        if 2*i + 1 >= len(plus):
            break
        re.append(plus[2*i]*plus[2*i+1])
    minus.sort()
    for i in range(k//2):
        if 2*i + 1 >= len(plus):
            break
        re.append(minus[2*i]*minus[2*i+1])
    re.sort(reverse=True)
    for i in range(k//2):
        ans *= re[i]
        ans %= mod
    print(ans)

Buggy submission (verdict: Runtime Error):
n , k = map(int,input().split())
a = list(map(int,input().split()))
mod = 10**9 + 7
a.sort(reverse=True)
ans = 1
plus = []
minus = []
zero = 0
for i in range(n):
    if a[i] > 0:
        plus.append(a[i])
    elif a[i] < 0:
        minus.append(a[i])
    elif a[i] == 0:
        zero += 1
if len(plus) + len(minus) < k:
    print(0)
elif len(minus) == 0:
    for i in range(k):
        ans *= plus[i]
        ans %= mod
    print(ans)
elif len(plus) == 0:
    if k % 2 == 0:
        for i in range(k):
            ans *= minus[n-1-i]
            ans %= mod
        print(ans)
    elif k % 2 == 1:
        if zero >= 1:
            print(0)
        elif zero == 0:
            for i in range(k):
                ans *= minus[i]
                ans %= mod
            print(ans)
else:
    if k % 2 == 1:
        ans = plus[0]
        del plus[0]
        k -= 1
    re = []
    for i in range(k//2):
        if 2*i + 1 >= len(plus):
            break
        re.append(plus[2*i]*plus[2*i+1])
    minus.sort()
    for i in range(k//2):
        if 2*i + 1 >= len(plus):
            break
        re.append(minus[2*i]*minus[2*i+1])
    re.sort(reverse=True)
    for i in range(k//2):
        ans *= re[i]
        ans %= mod
    print(ans)

Buggy submission (verdict: Runtime Error):
n , k = map(int,input().split())
a = list(map(int,input().split()))
mod = 10**9 + 7
a.sort(reverse=True)
ans = 1
plus = []
minus = []
zero = 0
for i in range(n):
    if a[i] > 0:
        plus.append(a[i])
    elif a[i] < 0:
        minus.append(a[i])
    elif a[i] == 0:
        zero += 1
if len(plus) + len(minus) < k:
    print(0)
elif len(minus) == 0:
    for i in range(k):
        ans *= plus[i]
        ans %= mod
    print(ans)
elif len(plus) == 0:
    if k % 2 == 0:
        for i in range(k):
            ans *= minus[n-1-i]
            ans %= mod
        print(ans)
    elif k % 2 == 1:
        if zero >= 1:
            print(0)
        elif zero == 0:
            for i in range(k):
                ans *= minus[i]
                ans %= mod
            print(ans)
else:
    rplus = [1]
    for i in range(len(plus)):
        rplus.append(rplus[-1] * plus[i])
    rminus = [1]
    for i in reversed(range(len(minus))):
        rminus.append(rminus[-1] * minus[i])
    for i in range(0,k+1,2):
        cou = 1
        if len(rminus) < i or len(rplus) < k - i:
            continue
        cou = rminus[i] * rplus[k-i]
        if cou > ans:
            ans = cou
    print(ans%mod)

Buggy submission (verdict: Runtime Error):
n , k = map(int,input().split())
a = list(map(int,input().split()))
mod = 10**9 + 7
a.sort(reverse=True)
ans = 1
plus = []
minus = []
zero = 0
for i in range(n):
    if a[i] > 0:
        plus.append(a[i])
    elif a[i] < 0:
        minus.append(a[i])
    elif a[i] == 0:
        zero += 1
if len(plus) + len(minus) < k:
    print(0)
elif len(minus) == 0:
    for i in range(k):
        ans *= plus[i]
        ans %= mod
    print(ans)
elif len(plus) == 0:
    if k % 2 == 0:
        for i in range(k):
            ans *= minus[n-1-i]
            ans %= mod
        print(ans)
    elif k % 2 == 1:
        if zero >= 1:
            print(0)
        elif zero == 0:
            for i in range(k):
                ans *= minus[i]
                ans %= mod
            print(ans)
else:
    rplus = [1]
    for i in range(len(plus)):
        rplus.append(rplus[-1] * plus[i])
    rminus = [1]
    for i in reversed(range(len(minus))):
        rminus.append(rminus[-1] * minus[i])
    for i in range(0,k+1,2):
        cou = 1
        if len(rminus) < i or len(rplus) < k - i:
            continue
        cou = rminus[i] * rplus[k-i]
        if cou > ans:
            ans = cou
    print(ans%mod)