Problem p02616 (python)

Fixed submission:
"""
E.
"""
from functools import reduce
from collections import deque


def prod(x, y):
    return x * y % MOD

    
N, K = map(int, input().split())
a_list = list(map(int, input().split()))
a_list.sort(reverse=True)
# abs_a_list = sorted(a_list, key=abs, reverse=True)
MOD = 10**9 + 7

if K%2 == 1:
    flg = True
    for a in a_list:
        if a >= 0:
            flg = False
            break
    if flg:
        print(reduce(prod, a_list[:K]))
        exit()

S = []
a_list = deque(a_list)
i = 0
while i < K:
    if len(a_list)>=2:
        if a_list[0]*a_list[1] > a_list[-1]*a_list[-2]:
            S.append(a_list.popleft())
            i += 1
        elif a_list[0]*a_list[1] <= a_list[-1]*a_list[-2] and i <= K-2:
            S.append(a_list.pop())
            S.append(a_list.pop())
            i += 2
        else:
            S.append(a_list.popleft())
            i += 1
    else:
        S.append(a_list.popleft())
        i += 1
        
ans = reduce(prod, S)
print(ans)

Buggy submission (verdict: Runtime Error):
"""
E.
"""
from functools import reduce
from collections import deque


def prod(x, y):
    return x * y % MOD

    
N, K = map(int, input().split())
a_list = list(map(int, input().split()))
a_list.sort(reverse=True)
# abs_a_list = sorted(a_list, key=abs, reverse=True)
MOD = 10**9 + 7

if K%2 == 1:
    flg = True
    for a in a_list:
        if a >= 0:
            flg = False
            break
    if flg:
        print(reduce(prod, a_list[:K]))
        exit()

S = []
i = 0
a_list = deque(a_list)
while i < K:
    if a_list[0]*a_list[1] >= a_list[-1]*a_list[-2]:
        S.append(a_list.popleft())
        i += 1
    elif a_list[0]*a_list[1] < a_list[-1]*a_list[-2] and i <= K-2:
        S.append(a_list.pop())
        S.append(a_list.pop())
        i += 2
    else:
        S.append(a_list.popleft())
        i += 1

ans = reduce(prod, S)
print(ans)

Buggy submission (verdict: Runtime Error):
"""
E.
"""
from functools import reduce
from collections import deque


def prod(x, y):
    return x * y % MOD

    
N, K = map(int, input().split())
a_list = list(map(int, input().split()))
a_list.sort(reverse=True)
# abs_a_list = sorted(a_list, key=abs, reverse=True)
MOD = 10**9 + 7

if K%2 == 1:
    flg = True
    for a in a_list:
        if a >= 0:
            flg = False
            break
    if flg:
        print(reduce(prod, a_list[:K]))
        exit()

S = []
i = 0
a_list = deque(a_list)
while i < K:
    if a_list[0]*a_list[1] > a_list[-1]*a_list[-2]:
        S.append(a_list.popleft())
        i += 1
    elif a_list[0]*a_list[1] <= a_list[-1]*a_list[-2] and i <= K-2:
        S.append(a_list.pop())
        S.append(a_list.pop())
        i += 2
    else:
        S.append(a_list.popleft())
        i += 1

ans = reduce(prod, S)
print(ans)

Buggy submission (verdict: Runtime Error):
"""
E.
"""
from functools import reduce
from collections import deque


def prod(x, y):
    return x * y % MOD

    
N, K = map(int, input().split())
a_list = list(map(int, input().split()))
a_list.sort(reverse=True)
abs_a_list = sorted(a_list, key=abs, reverse=True)
MOD = 10**9 + 7

if K%2 == 1:
    flg = True
    for a in a_list:
        if a >= 0:
            flg = False
            break
    if flg:
        print(reduce(prod, a_list[:K])%MOD)
        exit()

# ans2
S = []
a_list = deque(a_list)
for _ in range(K//2):
    if a_list[0]*a_list[1] >= a_list[-1]*a_list[-2]:
        S.append(a_list.pop(0))
        S.append(a_list.pop(0))
    else:
        S.append(a_list.pop(-1))
        S.append(a_list.pop(-1))
if K%2 == 1:
    S.append(a_list.pop(0))

ans = reduce(prod, S)
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import deque

N, K = map(int, input().split())
a_list = list(map(int, input().split()))
a_list = deque(sorted(a_list))
MOD = 10**9 + 7
 
 
def choose1():
    l = 1
    if a_list[-1]*a_list[-2] >= a_list[0]*a_list[1]:
        l *= a_list.popleft()
        l *= a_list.popleft()
    else:
        l *= a_list.pop()
        l *= a_list.pop()
    return l % MOD

def choose2():
    l = 1
    if a_list[-1]*a_list[-2] >= a_list[0]*a_list[1]:
        l *= a_list.pop()
        l *= a_list.pop()
    else:
        l *= a_list.popleft()
        l *= a_list.popleft()
    return l % MOD
 
 
ans = 1
if K == 1:
    print(a_list[-1]%MOD)
    exit()
else:
    if K % 2 == 0:
        for _ in range(K//2):
            ans *= choose2()
    else:
        ans *= a_list.pop(-1)
        if ans < 0:
            for _ in range((K-1)//2):
                ans *= choose1()
        else:
            for _ in range((K-1)//2):
                ans *= choose2()
 
print(ans%MOD)