Problem p02616 (python)

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

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

ans = 1
if k > len(plus) + len(minus):
    ans = 0
elif len(minus) == 0:
    plus.sort(reverse=True)
    for i in range(k):
        ans = (ans * plus[i]) % mod
elif len(plus) == 0:
    if k % 2 == 0:
        minus.sort()
        for i in range(k):
            ans = (ans * minus[i]) % mod
    else:
        if n != len(minus):
            ans = 0
        else:
            minus.sort(reverse=True)
            for i in range(k):
                ans = (ans * minus[i]) % mod
elif k == len(plus) + len(minus) and (len(minus) % 2) == 1:
    if n != k:
        ans = 0
    else:
        for x in plus:
            ans = (ans * x) % mod
        for y in minus:
            ans = (ans * y) % mod
else:
    plus.sort(reverse=True)
    minus.sort()
    i, j = 0, 0
    if k % 2 == 1:
        ans = plus[0]
        i = 1
    while i + j < k:
        tmp_p = plus[i] * plus[i + 1] if i <= len(plus) - 2 else 1
        tmp_m = minus[j] * minus[j + 1] if j <= len(minus) - 2 else 1
        if tmp_p >= tmp_m:
            ans = (ans * (tmp_p % mod)) % mod
            i += 2
        else:
            ans = (ans * (tmp_m % mod)) % mod
            j += 2
print(ans)

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

plus = []
minus = []
for i in a:
    if i > 0: plus.append(i)
    if i < 0: minus.append(-i)
plus.sort(reverse=True)
minus.sort(reverse=True)

ans = 1
if k == n:
    for x in a:
        ans = (ans * x) % mod
elif k > len(plus) + len(minus):
    ans = 0
elif n == len(plus):
    for x in plus[:k]:
        ans = (ans * x) % mod
elif n == len(minus):
    if k % 2 == 1:
        ans = -1
        minus.sort()
    for x in minus[:k]:
        ans = (ans * x) % mod
else:
    i, j = 0, 0
    if k % 2 == 1:
        ans = plus[0]
        i += 1
    while i + j < k:
        x_p = plus[i] * plus[i + 1] if i < len(plus) - 1 else 0
        x_m = minus[j] * minus[j + 1] if j < len(minus) - 1 else 0
        if x_p > x_m:
            ans = (ans * x_p) % mod
            i += 2
        else:
            ans = (ans * x_m) % mod
            j += 2
print(ans)

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

plus = []
minus = []
for i in a:
    if i > 0: plus.append(i)
    if i < 0: minus.append(-i)
plus.sort(reverse=True)
minus.sort(reverse=True)

ans = 1
if k == n:
    for x in a:
        ans = (ans * x) % mod
elif k > len(plus) + len(minus):
    ans = 0
elif n == len(plus):
    for x in plus[:k]:
        ans = (ans * x) % mod
elif n == len(minus):
    if k % 2 == 1:
        ans = -1
        minus.sort()
    for x in minus[:k]:
        ans = (ans * x) % mod
else:
    i, j = 0, 0
    if k % 2 == 1:
        ans = plus[0]
        i += 1
    while i + j < k:
        x_p = (plus[i] * plus[i + 1]) % mod if i < len(plus) - 1 else 0
        x_m = (minus[j] * minus[j + 1]) % mod if j < len(minus) - 1 else 0
        if x_p > x_m:
            ans = (ans * x_p) % mod
            i += 2
        else:
            ans = (ans * x_m) % mod
            j += 2
print(ans)

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

plus = []
minus = []
for i in a:
    if i > 0: plus.append(i)
    if i < 0: minus.append(-i)
plus.sort(reverse=True)
minus.sort(reverse=True)

ans = 1
if k == n:
    for x in a:
        ans = (ans * x) % mod
elif k > len(plus) + len(minus):
    ans = 0
elif n == len(plus):
    for x in plus[:k]:
        ans = (ans * x) % mod
elif n == len(minus):
    if k % 2 == 1:
        ans = -1
        minus.sort()
    for x in minus[:k]:
        ans = (ans * x) % mod
else:
    i, j = 0, 0
    while i + j < k:
        if i + j == k - 1:
            ans = (ans * plus[i]) % mod
            i += 1
        else:
            x_p = (plus[i] * plus[i + 1]) % mod if i < len(plus) - 1 else 0
            x_m = (minus[j] * minus[j + 1]) % mod if j < len(minus) - 1 else 0
            if x_p > x_m:
                ans = (ans * x_p) % mod
                i += 2
            else:
                ans = (ans * x_m) % mod
                j += 2
print(ans)

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

plus = []
minus = []
for i in a:
    if i > 0: plus.append(i)
    if i < 0: minus.append(-i)
plus.sort(reverse=True)
minus.sort(reverse=True)

ans = 1
if k == n:
    for x in a[:k]:
        ans = (ans * x) % mod
elif k > len(plus) + len(minus):
    ans = 0
elif n == len(plus):
    for x in plus[:k]:
        ans = (ans * x) % mod
elif n == len(minus):
    if k % 2 == 1:
        ans = -1
        minus.sort()
    for x in minus[:k]:
        ans = (ans * x) % mod
else:
    i, j = 0, 0
    while i + j < k:
        if i + j == k - 1:
            ans = (ans * plus[i]) % mod
            i += 1
        else:
            x_p = (plus[i] * plus[i + 1]) % mod if i < len(plus) - 1 else 1
            x_m = (minus[j] * minus[j + 1]) % mod if j < len(minus) - 1 else 1
            if x_p > x_m:
                ans = (ans * x_p) % mod
                i += 2
            else:
                ans = (ans * x_m) % mod
                j += 2
print(ans)

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

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

ans = 1
if k > len(plus) + len(minus):
    ans = 0
elif len(minus) == 0:
    plus.sort(reverse=True)
    for i in range(k):
        ans = (ans * plus[i]) % mod
elif len(plus) == 0:
    if k % 2 == 0:
        minus.sort()
        for i in range(k):
            ans = (ans * minus[i]) % mod
    else:
        minus.sort(reverse=True)
        for i in range(k):
            ans = (ans * minus[i]) % mod
elif k == len(plus) + len(minus) and (len(minus) % 2) == 1:
    if n != k:
        ans = 0
    else:
        for x in plus:
            ans = (ans * x) % mod
        for y in minus[:k - len(plus)]:
            ans = (ans * y) % mod
else:
    plus.sort(reverse=True)
    minus.sort()
    if k > len(plus) + len(minus):
        ans = 0
    else:
        i, j = 0, 0
        if k % 2 == 1:
            ans = plus[0]
            i = 1
        while i + j < k:
            tmp_p = plus[i] * plus[i + 1] if i <= len(plus) - 2 else 1
            tmp_m = minus[j] * minus[j + 1] if j <= len(plus) - 2 else 1
            if tmp_p >= tmp_m:
                ans = (ans * (tmp_p % mod)) % mod
                i += 2
            else:
                ans = (ans * (tmp_m % mod)) % mod
                j += 2
print(ans)

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

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

ans = 1
if k > len(plus) + len(minus):
    ans = 0
elif len(minus) == 0:
    plus.sort(reverse=True)
    if k > len(plus):
        ans = 0
    else:
        for i in range(k):
            ans = (ans * plus[i]) % mod
elif len(plus) == 0:
    if k > len(minus):
        ans = 0
    elif k % 2 == 0:
        minus.sort()
        for i in range(k):
            ans = (ans * minus[i]) % mod
    else:
        if n != len(minus):
            ans = 0
        else:
            minus.sort(reverse=True)
            for i in range(k):
                ans = (ans * minus[i]) % mod
elif k == len(plus) + len(minus) and (len(minus) % 2) == 1:
    if n != len(plus) + len(minus):
        ans = 0
    else:
        for x in plus:
            ans = (ans * x) % mod
        for y in minus:
            ans = (ans * y) % mod
else:
    plus.sort(reverse=True)
    minus.sort()
    if k > len(plus) + len(minus):
        ans = 0
    else:
        i, j = 0, 0
        if k % 2 == 1:
            ans = plus[0]
            i = 1
        while i + j < k:
            tmp_p = plus[i] * plus[i + 1] if i <= len(plus) - 2 else 1
            tmp_m = minus[j] * minus[j + 1] if j <= len(plus) - 2 else 1
            if tmp_p >= tmp_m:
                ans = (ans * (tmp_p % mod)) % mod
                i += 2
            else:
                ans = (ans * (tmp_m % mod)) % mod
                j += 2
print(ans)

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

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

ans = 1
if k > len(plus) + len(minus):
    ans = 0
elif len(minus) == 0:
    plus.sort(reverse=True)
    if k > len(plus):
        ans = 0
    else:
        for i in range(k):
            ans = (ans * plus[i]) % mod
elif len(plus) == 0:
    if k > len(minus):
        ans = 0
    elif k % 2 == 0:
        minus.sort()
        for i in range(k):
            ans = (ans * minus[i]) % mod
    else:
        if n != len(minus):
            ans = 0
        else:
            minus.sort(reverse=True)
            for i in range(k):
                ans = (ans * minus[i]) % mod
elif k == len(plus) + len(minus) and (len(minus) % 2) == 1:
    if n != len(plus) + len(minus):
        ans = 0
    else:
        for x in plus:
            ans = (ans * x) % mod
        for y in minus:
            ans = (ans * y) % mod
else:
    plus.sort(reverse=True)
    minus.sort()
    if k > len(plus) + len(minus):
        ans = 0
    else:
        i, j = 0, 0
        if k % 2 == 1:
            ans = plus[0]
            i = 1
        while i + j < k:
            tmp_p = plus[i] * plus[i + 1] if i <= len(plus) - 2 else 1
            tmp_m = minus[j] * minus[j + 1] if j <= len(plus) - 2 else 1
            if tmp_p >= tmp_m:
                ans = (ans * (tmp_p % mod)) % mod
                i += 2
            else:
                ans = (ans * (tmp_m % mod)) % mod
                j += 2
print(ans)

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

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

ans = 1
if k > len(plus) + len(minus):
    ans = 0
elif len(minus) == 0:
    plus.sort(reverse=True)
    if k > len(plus):
        ans = 0
    else:
        for i in range(k):
            ans = (ans * plus[i]) % mod
elif len(plus) == 0:
    if k % 2 == 0:
        if k > len(minus):
            ans = 0
        else:
            minus.sort()
            for i in range(k):
                ans = (ans * minus[i]) % mod
    else:
        if n != len(minus) or k > len(minus):
            ans = 0
        else:
            minus.sort(reverse=True)
            for i in range(k):
                ans = (ans * minus[i]) % mod
elif k == len(plus) + len(minus) and (len(minus) % 2) == 1:
    if n != len(plus) + len(minus):
        ans = 0
    else:
        for x in plus:
            ans = (ans * x) % mod
        for y in minus:
            ans = (ans * y) % mod
else:
    plus.sort(reverse=True)
    minus.sort()
    i, j = 0, 0
    if k % 2 == 1:
        ans = plus[0]
        i = 1
    while i + j < k:
        tmp_p = plus[i] * plus[i + 1] if i <= len(plus) - 2 else 1
        tmp_m = minus[j] * minus[j + 1] if j <= len(plus) - 2 else 1
        if tmp_p >= tmp_m:
            ans = (ans * (tmp_p % mod)) % mod
            i += 2
        else:
            ans = (ans * (tmp_m % mod)) % mod
            j += 2
print(ans)

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

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

ans = 1
if k > len(plus) + len(minus):
    ans = 0
elif len(minus) == 0:
    plus.sort(reverse=True)
    for i in range(k):
        ans = (ans * plus[i]) % mod
elif len(plus) == 0:
    if k % 2 == 0:
        minus.sort()
        for i in range(k):
            ans = (ans * minus[i]) % mod
    else:
        minus.sort(reverse=True)
        for i in range(k):
            ans = (ans * minus[i]) % mod
elif k == len(plus) + len(minus) and (len(minus) % 2) == 1:
    if n != len(plus) + len(minus):
        ans = 0
    else:
        for x in plus:
            ans = (ans * x) % mod
        for y in minus:
            ans = (ans * y) % mod
else:
    plus.sort(reverse=True)
    minus.sort()
    i, j = 0, 0
    if k % 2 == 1:
        ans = plus[0]
        i = 1
    while i + j < k:
        tmp_p = plus[i] * plus[i + 1] if i <= len(plus) - 2 else 1
        tmp_m = minus[j] * minus[j + 1] if j <= len(plus) - 2 else 1
        if tmp_p >= tmp_m:
            ans = (ans * (tmp_p % mod)) % mod
            i += 2
        else:
            ans = (ans * (tmp_m % mod)) % mod
            j += 2
print(ans)

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

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

ans = 1
if k > len(plus) + len(minus):
    ans = 0
elif len(minus) == 0:
    plus.sort(reverse=True)
    for i in range(k):
        ans = (ans * plus[i]) % mod
elif len(plus) == 0:
    if k % 2 == 0:
        minus.sort()
        for i in range(k):
            ans = (ans * minus[i]) % mod
    else:
        minus.sort(reverse=True)
        for i in range(k):
            ans = (ans * minus[i]) % mod
elif k - len(plus) > 0 and (k - len(plus)) % 2 == 1:
    plus.sort(reverse=True)
    minus.sort()
    ans = minus[0]
    i, j = 0, 1
    while i + j < k:
        tmp_p = plus[i] * plus[i + 1] if i <= len(plus) - 2 else float('inf')
        tmp_m = minus[j] * minus[j + 1] if i <= len(minus) - 2 else float('inf')
        if tmp_p <= tmp_m:
            ans = (ans * (tmp_p % mod)) % mod
            i += 2
        else:
            ans = (ans * (tmp_m % mod)) % mod
            j += 2
else:
    plus.sort()
    minus.sort(reverse=True)
    i, j = 0, 0
    while i + j < k:
        tmp_p = plus[i] * plus[i + 1] if i <= len(plus) - 2 else 0
        tmp_m = minus[j] * minus[j + 1] if j <= len(plus) - 2 else 0
        if tmp_p >= tmp_m:
            ans = (ans * plus[i]) % mod
            i += 1
        else:
            ans = (ans * (tmp_m % mod)) % mod
            j += 2
print(ans)