Problem p02616 (python)

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

if k == n:
    for i in range(n):
        ans *= a[i]
        ans %= mod
    print(ans)
    exit()
if max(a) < 0 and k%2 == 1:
    a.sort(reverse=True)
    for i in range(k):
        ans *= a[i]
        ans %= mod
    print(ans)
    exit()

a.sort(key=lambda x: abs(x))
cnt = 0
for i in range(1, k+1):
    ans *= a[-i]
    ans %= mod
    if a[-i] < 0:
        cnt += 1

if cnt%2 == 0:
    print(ans)
else:
    a_plus = None
    a_minus = None
    b_plus = None
    b_minus = None
    for i in range(k, 0, -1):
        if a[-i] >= 0 and a_plus == None:
            a_plus = a[-i]
        if a[-i] < 0 and a_minus == None:
            a_minus = a[-i]
    for i in range(k+1, n+1):
        if a[-i] >= 0 and b_plus == None:
            b_plus = a[-i]
        if a[-i] < 0 and b_minus == None:
            b_minus = a[-i]
    if a_plus == None or b_minus == None:
        ans *= pow(a_minus, mod-2, mod)
        ans %= mod
        ans *= b_plus
        ans %= mod
    elif a_minus == None or b_plus == None:
        ans *= pow(a_plus, mod-2, mod)
        ans %= mod
        ans *= b_minus
        ans %= mod
    else:
        if abs(a_plus*b_plus) > abs(a_minus*b_minus):
            ans *= pow(a_minus, mod-2, mod)
            ans %= mod
            ans *= b_plus
            ans %= mod
        else:
            ans *= pow(a_plus, mod-2, mod)
            ans %= mod
            ans *= b_minus
            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
ans = 1

if k == n:
    for i in range(n):
        ans *= a[i]
        ans %= mod
    print(ans)
    exit()
if max(a) < 0 and k%2 == 1:
    a.sort(reverse=True)
    for i in range(k):
        ans *= a[i]
        ans %= mod
    print(ans)
    exit()

plus, minus = [], []
for i in range(n):
    if a[i] >= 0: plus.append(a[i])
    else: minus.append(a[i])

plus.sort()
minus.sort(reverse=True)
while k > 0:
    if len(plus) >= 2: nump = plus[-1] * plus[-2]
    elif len(plus) == 1: nump = plus[-1]
    else: nump = -10**10
    if len(minus) >= 2: numm = minus[-1] * minus[-2]
    elif len(minus) == 1: numm = minus[-1]
    else: numm == -10**10
    if nump > numm or k == 1:
        ans *= plus.pop()
        ans %= mod
        k -= 1
    else:
        ans *= minus.pop()
        ans *= minus.pop()
        ans %= mod
        k -= 2
print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10**9 + 7
ans = 1

if k == n:
    for i in range(n):
        ans *= a[i]
        ans %= mod
    print(ans)
    exit()
if max(a) < 0:
    a.sort(reverse=True)
    for i in range(k):
        ans *= a[i]
        ans %= mod
    print(ans)
    exit()

plus, minus = [], []
for i in range(n):
    if a[i] >= 0: plus.append(a[i])
    else: minus.append(a[i])

plus.sort()
minus.sort(reverse=True)
while k > 0:
    if len(plus) >= 2: nump = plus[-1] * plus[-2]
    elif len(plus) == 1: nump = plus[-1]
    else: nump = -10**10
    if len(minus) >= 2: numm = minus[-1] * minus[-2]
    elif len(minus) == 1: numm = minus[-1]
    else: numm == -10**10
    if nump > numm or k == 1:
        ans *= plus.pop()
        ans %= mod
        k -= 1
    else:
        ans *= minus.pop()
        ans *= minus.pop()
        ans %= mod
        k -= 2
print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10**9 + 7
ans = 1

if k == n:
    for i in range(n):
        ans *= a[i]
        ans %= mod
    print(ans)
    exit()
if max(a) < 0:
    for i in range(k):
        ans *= a[i]
        ans %= mod
    print(ans)
    exit()

plus, minus = [], []
for i in range(n):
    if a[i] >= 0: plus.append(a[i])
    else: minus.append(a[i])

plus.sort()
minus.sort(reverse=True)
while k > 0:
    if len(plus) >= 2: nump = plus[-1] * plus[-2]
    elif len(plus) == 1: nump = plus[-1]
    else: nump = -10**10
    if len(minus) >= 2: numm = minus[-1] * minus[-2]
    elif len(minus) == 1: numm = minus[-1]
    else: numm == -10**10
    if nump > numm or k == 1:
        ans *= plus.pop()
        ans %= mod
        k -= 1
    else:
        ans *= minus.pop()
        ans *= minus.pop()
        ans %= mod
        k -= 2
print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10**9 + 7
ans = 1

if k == 1:
    print(max(a))
    exit()

lst = []
minus = []
for i in range(n):
    if a[i] >= 0:
        lst.append([a[i], 1])
    else:
        minus.append(a[i])

if k % 2 == 1 and lst != []:
    lst.sort(key=lambda x: x[0])
    ans *= lst.pop()[0]
    k -= 1
elif k % 2 == 1 and lst == []:
    a.sort(reverse=True)
    for i in range(k):
        ans *= a[i]
        ans %= mod
    print(ans)
    exit()

minus.sort(reverse=True)
while minus != []:
    if len(minus) >= 2:
        a = minus.pop()
        b = minus.pop()
        lst.append([a*b, 2])
    else:
        a = minus.pop()
        lst.append([a, 1])

lst.sort(key=lambda x: x[0])
while k > 0:
    lst1 = lst.pop()
    lst2 = lst.pop()
    if [lst1[1], lst2[1]] == [1, 1]:
        ans *= lst1[0]
        ans %= mod
        ans *= lst2[0]
        ans %= mod
    elif [lst1[1], lst2[1]] == [1, 2]:
        ans *= lst2[0]
        ans %= mod
        lst.append(lst1)
    elif [lst1[1], lst2[1]] == [2, 1]:
        ans *= lst1[0]
        ans %= mod
        lst.append(lst2)
    else:
        ans *= lst1[0]
        ans %= mod
        lst.append(lst2)
    k -= 2
print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10**9 + 7
ans = 1

if k == n:
    for i in range(n):
        ans *= a[i]
        ans %= mod
    print(ans)
    exit()
if max(a) < 0 and k%2 == 1:
    a.sort(reverse=True)
    for i in range(k):
        ans *= a[i]
        ans %= mod
    print(ans)
    exit()

a.sort(key=lambda x: abs(x))
cnt = 0
for i in range(1, k+1):
    ans *= a[-i]
    ans %= mod
    if a[-i] < 0:
        cnt += 1

if cnt%2 == 0:
    print(ans)
else:
    a_plus = None
    a_minus = None
    b_plus = None
    b_minus = None
    for i in range(k, 0, -1):
        if a[-i] >= 0 and a_plus == None:
            a_plus = a[-i]
        if a[-i] < 0 and a_minus == None:
            a_minus = a[-i]
    for i in range(k+1, n+1):
        if a[-i] >= 0 and b_plus == None:
            b_plus = a[-i]
        if a[-1] < 0 and b_minus == None:
            b_minus = a[-i]
    if a_plus == None or b_minus == None:
        ans *= pow(a_minus, mod-2, mod)
        ans %= mod
        ans *= b_plus
        ans %= mod
    elif a_minus == None or b_plus == None:
        ans *= pow(a_plus, mod-2, mod)
        ans %= mod
        ans *= b_minus
        ans %= mod
    else:
        if abs(a_plus*b_plus) > abs(a_minus*b_minus):
            ans *= pow(a_minus, mod-2, mod)
            ans %= mod
            ans *= b_plus
            ans %= mod
        else:
            ans *= pow(a_plus, mod-2, mod)
            ans %= mod
            ans *= b_minus
            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
ans = 1

if k == n:
    for i in range(n):
        ans *= a[i]
        ans %= mod
    print(ans)
    exit()
if max(a) < 0 and k%2 == 1:
    a.sort(reverse=True)
    for i in range(k):
        ans *= a[i]
        ans %= mod
    print(ans)
    exit()

a.sort(key=lambda x: abs(x))
cnt = 0
for i in range(1, k+1):
    ans *= abs(a[-i])
    ans %= mod
    if a[-i] < 0:
        cnt += 1

if cnt%2 == 0:
    print(ans)
else:
    a_plus = None
    a_minus = None
    b_plus = None
    b_minus = None
    for i in range(k, 0, -1):
        if a[-i] >= 0 and a_plus == None:
            a_plus = abs(a[-i])
        if a[-i] < 0 and a_minus == None:
            a_minus = abs(a[-i])
    for i in range(k+1, n+1):
        if a[-i] >= 0 and b_plus == None:
            b_plus = abs(a[-i])
        if a[-1] < 0 and b_minus == None:
            b_minus = abs(a[-i])
    if a_plus == None or b_minus == None:
        ans *= pow(a_minus, mod-2, mod)
        ans %= mod
        ans *= b_plus
        ans %= mod
    elif a_minus == None or b_plus == None:
        ans *= pow(a_plus, mod-2, mod)
        ans %= mod
        ans *= b_minus
        ans %= mod
    else:
        if a_plus*b_plus > a_minus*b_minus:
            ans *= pow(a_minus, mod-2, mod)
            ans %= mod
            ans *= b_plus
            ans %= mod
        else:
            ans *= pow(a_plus, mod-2, mod)
            ans %= mod
            ans *= b_minus
            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
ans = 1

if k == 1:
    max(a)
    exit()

lst = []
minus = []
for i in range(n):
    if a[i] >= 0:
        lst.append([a[i], 1])
    else:
        minus.append(a[i])

if k % 2 == 1 and lst != []:
    lst.sort(key=lambda x: x[0])
    ans *= lst.pop()[0]
    k -= 1
elif k % 2 == 1 and lst == []:
    a.sort(reverse=True)
    for i in range(k):
        ans *= a[i]
        ans %= mod
    print(ans)
    exit()

minus.sort(reverse=True)
while minus != []:
    if len(minus) >= 2:
        a = minus.pop()
        b = minus.pop()
        lst.append([a*b, 2])
    else:
        a = minus.pop()
        lst.pop([a, 1])

lst.sort(key=lambda x: x[0])
while k > 0:
    lst1 = lst.pop()
    lst2 = lst.pop()
    if [lst1[1], lst2[1]] == [1, 1]:
        ans *= lst1[0]
        ans %= mod
        ans *= lst2[0]
        ans %= mod
    elif [lst1[1], lst2[1]] == [1, 2]:
        ans *= lst2[0]
        ans %= mod
        lst.append(lst1)
    elif [lst1[1], lst2[1]] == [2, 1]:
        ans *= lst1[0]
        ans %= mod
        lst.append(lst2)
    else:
        ans *= lst1[0]
        ans %= mod
        lst.append(lst2)
    k -= 2
print(ans)