Problem p02616 (python)

Fixed submission:
n, k = map(int, input().split())
a_list = list(map(int, input().split()))
mod = 10**9+7
a_list.sort()
ans = 1
if k % 2 == 1 and a_list[-1] < 0:
    for i in range(k):
        ans *= a_list[n - 1 - i]
        ans %= mod
else:
    l = 0
    r = -1
    mlt1 = a_list[0] * a_list[1]
    mlt2 = a_list[-2] * a_list[-1]
    count = 0
    while True:
        if count == k:
            break
        elif count == k - 1:
            ans *= a_list[r] % mod
            ans %= mod
            break
        if mlt1 >= mlt2:
            ans *= mlt1 % mod
            l += 2
            count += 2
            if l <= n - 2:
                mlt1 = a_list[l + 1] * a_list[l]
        else:
            ans *= a_list[r] % mod
            r -= 1
            count += 1
            if r >= - n + 1:
                mlt2 = a_list[r - 1] * a_list[r]
        ans %= mod
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
M = 10**9 + 7
if N == K:
    result = 1
    for i in range(K):
        result = result * A[i] % M
    print(result)
    exit()
minus = [i for i in A if i < 0]
l_minus = len(minus)
plus = [i for i in A if i > 0]
l_plus = len(plus)
plus.sort(reverse=1)
minus.sort()
if len(plus) == 0 and K % 2 == 1:
    result = 1
    for i in range(K):
        result = result * minus[-i-1] % M
    print(result)
    exit()
if l_plus + l_minus < K:
    print(0)
    exit()

m_x = 0
p_x = 0
max_mm = -1
if (m_x+1) * 2 <= l_minus:
    max_mm = minus[m_x * 2] * minus[m_x * 2 + 1]
max_pp = -1
if (p_x+1) * 2 <= l_plus:
    max_pp = plus[p_x * 2] * plus[p_x * 2 + 1]
counter = 0
array = []
while counter * 2 + 1 < K:
    if max_pp > max_mm:
        array.append(plus[p_x * 2])
        array.append(plus[p_x * 2 + 1])
        p_x += 1
        if (p_x+1) * 2 <= l_plus:
            max_pp = plus[p_x * 2] * plus[p_x * 2 + 1]
        else:
            max_pp = -1

    else:
        array.append(minus[m_x * 2])
        array.append(minus[m_x * 2 + 1])
        m_x += 1
        if (m_x+1) * 2 <= l_minus:
            max_mm = minus[m_x * 2] * minus[m_x * 2 + 1]
        else:
            max_mm = -1
    counter += 1
if K%2 == 1:
    array.append(plus[p_x * 2])
result = 1
for each in array:
    result = result * each % M
print(result)

Buggy submission (verdict: Runtime Error):
N, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
M = 10**9 + 7
if N == K:
    result = 1
    for i in range(K):
        result = result * A[i] % M
    print(result)
    exit()
minus = [i for i in A if i < 0]
l_minus = len(minus)
plus = [i for i in A if i >= 0]
l_plus = len(plus)
plus.sort(reverse=1)
minus.sort()
if len(plus) == 0 and K % 2 == 1:
    result = 1
    for i in range(K):
        result = result * minus[-i-1] % M
    print(result)
    exit()

m_x = 0
p_x = 0
max_mm = -1
if (m_x+1) * 2 <= l_minus:
    max_mm = minus[m_x * 2] * minus[m_x * 2 + 1]
max_pp = -1
if (p_x+1) * 2 <= l_plus:
    max_pp = plus[p_x * 2] * plus[p_x * 2 + 1]
counter = 0
array = []
while counter * 2 + 1 <= K:
    if max_pp > max_mm:
        array.append(plus[p_x * 2])
        array.append(plus[p_x * 2 + 1])
        p_x += 1
        if (p_x+1) * 2 <= l_plus:
            max_pp = plus[p_x * 2] * plus[p_x * 2 + 1]
        else:
            max_pp = -1

    else:
        array.append(minus[m_x * 2])
        array.append(minus[m_x * 2 + 1])
        m_x += 1
        if (m_x+1) * 2 <= l_minus:
            max_mm = minus[m_x * 2] * minus[m_x * 2 + 1]
        else:
            max_mm = -1
    counter += 1
if K%2 == 1:
    array.append(plus[p_x * 2])
result = 1
for each in array:
    result = result * each % M
print(result)

Buggy submission (verdict: Runtime Error):
N, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
M = 10**9 + 7
if N == K:
    result = 1
    for i in range(K):
        result = result * A[i] % M
    print(result)
    exit()
minus = [i for i in A if i <= 0]
l_minus = len(minus)
plus = [i for i in A if i > 0]
l_plus = len(plus)
plus.sort(reverse=1)
minus.sort()
if len(plus) == 0 and K % 2 == 1:
    result = 1
    for i in range(K):
        result = result * minus[-i-1] % M
    print(result)
    exit()

m_x = 0
p_x = 0
max_mm = -1
if (m_x+1) * 2 <= l_minus:
    max_mm = minus[m_x * 2] * minus[m_x * 2 + 1]
max_pp = -1
if (p_x+1) * 2 <= l_plus:
    max_pp = plus[p_x * 2] * plus[p_x * 2 + 1]
counter = 0
array = []
while counter * 2 + 1 <= K:
    if max_pp > max_mm:
        array.append(plus[p_x * 2])
        array.append(plus[p_x * 2 + 1])
        p_x += 1
        if (p_x+1) * 2 <= l_plus:
            max_pp = plus[p_x * 2] * plus[p_x * 2 + 1]
        else:
            max_pp = -1
    elif max_pp == -1 and max_mm == -1:
        break
    else:
        array.append(minus[m_x * 2])
        array.append(minus[m_x * 2 + 1])
        m_x += 1
        if (m_x+1) * 2 <= l_minus:
            max_mm = minus[m_x * 2] * minus[m_x * 2 + 1]
        else:
            max_mm = -1
    counter += 1
if K%2 == 1:
    array.append(plus[p_x * 2])
result = 1
for each in array:
    result = result * each % M
print(result)

Buggy submission (verdict: Runtime Error):
N, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
M = 10**9 + 7
A = sorted(A, key=abs, reverse=1)
# 負の数になる場合
if max(A) < 0:
    result = 1
    for i in range(K):
        result = result * A[-i-1] % M
    print(result)
    exit()

minimum_minus_id = K
minimum_plus_id = K
minus_count = 0

for i in range(K):
    if A[i] < 0:
        minus_count += 1
        minimum_minus_id = i
    else:
        minimum_plus_id = i
result = 1

if minus_count%2 == 1:
    # 一番小さい負の数を正の数にするか、一番小さい正の数を負の数にするか
    second_minus_id = N
    second_plus_id = N

    for i in range(K, N):
        if A[i] >= 0:
            second_plus_id = i
            break
    for i in range(K, N):
        if A[i] < 0:
            second_minus_id = i
            break
    if second_minus_id == N or minimum_plus_id == K:
        A[minimum_minus_id] = A[second_plus_id]
    elif second_plus_id == N:
        A[minimum_plus_id] = A[second_minus_id]
    elif minimum_minus_id == K:
        pass
    else:
        if A[minimum_minus_id] * A[second_minus_id ] > A[minimum_plus_id] * A[second_plus_id]:
            A[minimum_plus_id] = A[second_minus_id]
        else:
            A[minimum_minus_id] = A[second_plus_id]

for i in range(K):
    result = result * A[i] % M
print(result)

Buggy submission (verdict: Runtime Error):
N, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
M = 10**9 + 7
A = sorted(A, key=abs, reverse=1)
# 負の数になる場合
if max(A) < 0:
    result = 1
    for i in range(K):
        result = result * A[-i-1] % M
    print(result)
    exit()

minimum_minus_id = K
minimum_plus_id = K
minus_count = 0

for i in range(K):
    if A[i] < 0:
        minus_count += 1
        minimum_minus_id = i
    else:
        minimum_plus_id = i
result = 1

if minus_count%2 == 1:
    # 一番小さい負の数を正の数にするか、一番小さい正の数を負の数にするか
    second_minus_id = N
    second_plus_id = N

    for i in range(K, N):
        if A[i] >= 0:
            second_plus_id = i
            break
    for i in range(K, N):
        if A[i] < 0:
            second_minus_id = i
            break
    if second_minus_id == N or minimum_plus_id == K:
        A[minimum_minus_id] = A[second_plus_id]
    elif second_plus_id == N:
        A[minimum_plus_id] = A[second_minus_id]

    else:
        if A[minimum_minus_id] * A[second_minus_id ] > A[minimum_plus_id] * A[second_plus_id]:
            A[minimum_plus_id] = A[second_minus_id]
        else:
            A[minimum_minus_id] = A[second_plus_id]

for i in range(K):
    result = result * A[i] % M
print(result)

Buggy submission (verdict: Runtime Error):
N, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
M = 10**9 + 7
A = sorted(A, key=abs, reverse=1)
# 負の数になる場合
if max(A) < 0:
    result = 1
    for i in range(K):
        result = result * A[-i-1] % M
    print(result)
    exit()

minimum_minus_id = K
minimum_plus_id = K
minus_count = 0

for i in range(K):
    if A[i] < 0:
        minus_count += 1
        minimum_minus_id = i
    else:
        minimum_plus_id = i
result = 1

if minus_count%2 ==1:
    # 一番小さい負の数を正の数にするか、一番小さい正の数を負の数にするか
    second_minus_id = N
    second_plus_id = N

    for i in range(K, N):
        if A[i] >= 0:
            second_plus_id = i
            break
    for i in range(K, N):
        if A[i] < 0:
            second_minus_id = i
            break
    if second_minus_id == N:
        A[minimum_minus_id] = A[second_plus_id]
    elif second_plus_id == N:
        A[minimum_plus_id] = A[second_minus_id]
    else:
        if A[minimum_minus_id] * A[second_minus_id ] > A[minimum_plus_id] * A[second_plus_id]:
            A[minimum_plus_id] = A[second_minus_id]
        else:
            A[minimum_minus_id] = A[second_plus_id]

for i in range(K):
    result = result * A[i] % M
print(result)

Buggy submission (verdict: Runtime Error):
N, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
M = 10**9 + 7
A = sorted(A, key=abs, reverse=1)
# 負の数になる場合
if max(A) < 0:
    result = 1
    for i in range(K):
        result = result * A[-i-1] % M
    print(result)
    exit()

minimum_minus_id = K
minimum_plus_id = K
minus_count = 0

for i in range(K):
    if A[i] < 0:
        minus_count += 1
        minimum_minus_id = i
    else:
        minimum_plus_id = i
result = 1

if minus_count%2 ==1:
    # 一番小さい負の数を正の数にするか、一番小さい正の数を負の数にするか
    second_minus_id = N
    second_plus_id = N

    for i in range(K, N):
        if A[i] >= 0:
            second_plus_id = i
            break
    for i in range(K, N):
        if A[i] < 0:
            second_minus_id = i
            break
    if second_minus_id == N:
        A[minimum_minus_id] = A[second_plus_id]
    else:
        if A[minimum_minus_id] * A[second_minus_id ] > A[minimum_plus_id] * A[second_plus_id]:
            A[minimum_plus_id] = A[second_minus_id]
        else:
            A[minimum_minus_id] = A[second_plus_id]

for i in range(K):
    result = result * A[i] % M
print(result)

Buggy submission (verdict: Runtime Error):
N, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
M = 10**9 + 7
A = sorted(A, key=abs, reverse=1)
# 負の数になる場合
if max(A) < 0:
    result = 1
    for i in range(K):
        result = result * A[-i-1] % M
    print(result)
    exit()

minimum_minus_id = K
minimum_plus_id = K
minus_count = 0

for i in range(K):
    if A[i] < 0:
        minus_count += 1
        minimum_minus_id = i
    else:
        minimum_plus_id = i
result = 1

if minus_count%2 ==1:
    # 一番小さい負の数を正の数にするか、一番小さい正の数を負の数にするか
    max_mm = A[minimum_minus_id]
    second_minus_id = N
    max_pp = A[minimum_plus_id]
    second_plus_id = N

    for i in range(K, N):
        if A[i] >= 0:
            second_minus_id = i
            break
    for i in range(K, N):
        if A[i] < 0:
            second_plus_id = i
            break
    if second_minus_id == N:
        A[minimum_minus_id] = A[second_plus_id]
    else:
        if A[minimum_minus_id] * A[second_minus_id ] > A[minimum_plus_id] * A[second_plus_id]:
            A[minimum_plus_id] = A[second_minus_id]
        else:
            A[minimum_minus_id] = A[second_plus_id]

for i in range(K):
    result = result * A[i] % M
print(result)