Problem p02616 (python)

Fixed submission:
    #n,k,*a=map(int,open(0).read().split())
n, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.sort()
mod=10**9+7
ans=1
i=0
j=-1
kk=k
while kk>1:
    if a[i]*a[i+1]>a[j]*a[j-1]:
        ans=ans*a[i]*a[i+1]%mod
        i+=2
        kk-=2
    else:
        ans=ans*a[j]%mod
        j-=1
        kk-=1
if kk==1:
    ans=ans*a[j]%mod

if a[-1]<0 and k%2==1:
    ans=1
    for i in a[n-k:]:
        ans=ans*i%mod
print(ans)

Buggy submission (verdict: Runtime Error):


MOD = 10**9 + 7
n, k = map(int, input().split())
a = list(map(int, input().split()))
plus,minus=[],[]
for i in a:
    if i<0:
        minus.append(i)
    else:
        plus.append(i)
plus.sort(reverse = True)
minus.sort()
plus.append(-float('inf'))
plus.append(float('inf'))
minus.append(float('inf'))
minus.append(-float('inf'))
ans = 1
if n == k:
    for i in range(n):
        ans *= a[i]
        ans %= MOD
    print(ans)
elif len(plus) == 2 and k % 2 == 1:
    for i in range(3, k + 3):
        ans *= minus[-i]
        ans %= MOD
    print(ans)
elif k == n - 1:
    if len(minus) % 2 == 1:
        for i in range(len(minus) - 3):
            ans *= minus[i]
            ans %= MOD
        for i in range(len(plus) - 2):
            ans *= plus[i]
            ans %= MOD
    else:
        for i in range(len(minus) - 2):
            ans *= minus[i]
            ans %= MOD
        for i in range(len(plus) - 3):
            ans *= plus[i]
            ans %= MOD
    print(ans)
else:
    cplus = 0
    cminus = 0
    p = k // 2
    if k % 2 == 1:
        ans *= plus[0]
        cplus = 1
    else:
        for _ in range(p):
            if plus[cplus] * plus[cplus + 1] > minus[cminus] * minus[cminus]:
                ans *= plus[cplus] * plus[cplus + 1]
                ans %= MOD
                cplus += 2
            else:
                ans *= minus[cminus] * minus[cminus]
                ans %= MOD
                cminus += 2
    print(ans)

Buggy submission (verdict: Runtime Error):


MOD = 10**9 + 7
n, k = map(int, input().split())
a = list(map(int, input().split()))
plus,minus=[],[]
for i in a:
    if i<0:
        minus.append(i)
    else:
        plus.append(i)
plus.sort(reverse = True)
minus.sort()
plus.append(-float('inf'))
plus.append(float('inf'))
minus.append(float('inf'))
minus.append(-float('inf'))
ans = 1
if n == k:
    for i in range(n):
        ans *= a[i]
        ans %= MOD
    print(ans)
elif len(plus) == 2 and k % 2 == 1:
    for i in range(3, k + 3):
        ans *= minus[-i]
        ans %= MOD
    print(ans)
else:
    cplus = 0
    cminus = 0
    p = k // 2
    if k % 2 == 1:
        ans *= plus[0]
        cplus = 1
    else:
        for _ in range(p):
            if plus[cplus] * plus[cplus + 1] > minus[cminus] * minus[cminus]:
                ans *= plus[cplus] * plus[cplus + 1]
                ans %= MOD
                cplus += 2
            else:
                ans *= minus[cminus] * minus[cminus]
                ans %= MOD
                cminus += 2
    print(ans)

Buggy submission (verdict: Runtime Error):


MOD = 10**9 + 7
n, k = map(int, input().split())
a = list(map(int, input().split()))
plus,minus=[],[]
for i in a:
    if i<0:
        minus.append(i)
    else:
        plus.append(i)
plus.sort(reverse = True)
minus.sort()
plus.append(-float('inf'))
plus.append(float('inf'))
minus.append(float('inf'))
minus.append(-float('inf'))
ans = 1
if n == k:
    for i in range(n):
        ans *= a[i]
        ans %= MOD
    print(ans)
elif len(plus) == 2 and k % 2 == 1:
    for i in range(3, k + 3):
        ans *= minus[-i]
        ans %= MOD
    print(ans)
else:
    cplus = 0
    cminus = 0
    p = k // 2
    if k % 2 == 1:
        ans *= plus[0]
        cplus = 1
    for _ in range(p):
        if plus[cplus] * plus[cplus + 1] > minus[cminus] * minus[cminus]:
            ans *= plus[cplus] * plus[cplus + 1]
            ans %= MOD
            cplus += 2
        else:
            ans *= minus[cminus] * minus[cminus]
            ans %= MOD
            cminus += 2
    print(ans)

Buggy submission (verdict: Runtime Error):
import numpy as np

MOD = 10**9 + 7
n, k = map(int, input().split())
a = np.array(list(map(int, input().split())))
ans = 1
plus = a[a >= 0]
plus = np.sort(plus)[::-1]
plus = np.append(plus, [-np.inf, np.inf])
minus = a[a < 0]
minus = np.sort(minus)
minus = np.append(minus, [np.inf, -np.inf])
if len(plus) == 2 and k % 2 == 1:
    for i in range(3, k + 3):
        ans *= minus[-i]
        ans %= MOD
else:
    c = 0
    cplus = 0
    cminus = 0
    while c < k - 1:
        if plus[cplus] * plus[cplus + 1] > minus[cminus] * minus[cminus + 1]:
            ans *= plus[cplus]
            ans %= MOD
            cplus += 1
            c += 1
        else:
            ans *= minus[cminus] * minus[cminus + 1]
            ans %= MOD
            cminus += 2
            c += 2
    if c == k - 1:
        ans *= plus[cplus]
        ans %= MOD
print(int(ans))

Buggy submission (verdict: Runtime Error):
import numpy as np

MOD = 10**9 + 7
n, k = map(int, input().split())
a = np.array(list(map(int, input().split())))
ans = 1
plus = a[a >= 0]
plus = np.sort(plus)[::-1]
plus = np.append(plus, [0, 0])
minus = a[a < 0]
minus = np.sort(minus)
minus = np.append(minus, [0, 0])
if n == k:
    for i in range(len(a)):
        ans *= a[i]
        ans %= MOD
    print(ans)
    exit()
if len(plus) == 2 and k % 2 == 1:
    for i in range(3, k + 3):
        ans *= minus[-i]
        ans %= MOD
else:
    if k % 2 == 1:
        p = k // 2
        ans *= plus[0]
        cplus = 1
        cminus = 0
    else:
        p = k // 2
        cplus = 0
        cminus = 0
    for l in range(p):
        if plus[cplus] * plus[cplus + 1] > minus[cminus] * minus[cminus + 1]:
            ans *= plus[cplus] * plus[cplus + 1]
            ans %= MOD
            cplus += 2
        else:
            ans *= minus[cminus] * minus[cminus + 1]
            ans %= MOD
            cminus += 2
print(int(ans))