Problem p02616 (python)

Fixed submission:
import numpy as np
n, k = map(int, input().split())
aa = list(map(np.int64, input().split()))
aa = np.array(aa)

def mul(dd):
    mod = 10**9+7
    ret = 1
    for d in dd:
        ret = (ret*d)%mod
    return ret

def sol(aa, n, k):
    mod = 10**9+7
    aap = aa[aa>0]
    aam = aa[aa<0]
    if n == k:
        return mul(aa)
    if len(aap) + 2*(len(aam)//2) < k:
        return 0

    # マイナスのみ
    if len(aam) == n:
        aam.sort()
        if k%2==1:
            return mul(aam[-k:])
        else:
            return mul(aam[:k])

    aap = aa[aa>=0]
    aap.sort()
    aap = aap[::-1]
    aam.sort()

    ret=1
    if k%2 >0:
        k = k-1
        ret *= aap[0]
        aap = aap[1:]

    aap2 = [aap[2*i]*aap[2*i+1] for i in range(len(aap)//2)]
    aam2 = [aam[2*i]*aam[2*i+1] for i in range(len(aam)//2)]
    aap2.extend(aam2)
    aap2.sort(reverse=True)
    aap2 = [i%mod for i in aap2]
    return (ret*mul(aap2[:k//2]))%mod
print(sol(aa, n, k))

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
aa = list(map(np.int64, input().split()))
import numpy as np
aa = np.array(aa)

def mul(dd):
    mod = 10**9+7
    ret = 1
    for d in dd:
        ret = (ret*d)%mod
    return ret

def sol(aa, n, k):
    mod = 10**9+7
    aap = aa[aa>0]
    aam = aa[aa<0]
    if n == k:
        return mul(aa)
    if len(aap) + 2*(len(aam)//2) < k:
        return 0

    # マイナスのみ
    if len(aam) == n:
        aam.sort()
        if k%2==1:
            return mul(aam[:k])
        else:
            return mul(aam[-k:])

    aap = aa[aa>=0]
    aap.sort()
    aap = aap[::-1]
    aam.sort()

    ret=1
    if k%2 >0:
        k = k-1
        ret *= aap[0]
        aap = aap[1:]

    aap2 = [(aap[2*i]*aap[2*i+1])%mod for i in range(len(aap)//2)]
    aam2 = [(aam[2*i]*aam[2*i+1])%mod for i in range(len(aam)//2)]
    aap2.extend(aam2)
    aap2.sort(reverse=True)
    return (ret*mul(aap2[:k//2]))%mod
print(sol(aa, n, k))

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
aa = list(map(int, input().split()))
import numpy as np
aa = np.array(aa)
mod = 10**9+7

def mul(dd):
    ret = 1
    for d in dd:
        ret = (ret*d)%mod
    return ret

def sol(aa, n, k):
    aap = aa[aa>0]
    aam = aa[aa<0]
    if n == k:
        return mul(aa)
    if len(aam) == n and k%2==1:
        aam.sort()
        aam = aam[::-1]
        return mul(aam[:k])

    aap.sort()
    aap = aap[::-1]
    aam.sort()

    ret=1
    if k%2 >0:
        k = k-1
        ret *= aap[0]
        aap = aap[1:]

    aap2 = [aap[2*i]*aap[2*i+1] for i in range(len(aap)//2)]
    aam2 = [aam[2*i]*aam[2*i+1] for i in range(len(aam)//2)]
    aap2.extend(aam2)
    aap2.sort(reverse=True)
    return (ret*mul(aap2[:k//2]))%mod

print(sol(aa, n, k))

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
aa = list(map(int, input().split()))
import numpy as np
aa = np.array(aa)

def mul(dd):
    mod = 10**9+7
    ret = 1
    for d in dd:
        ret = (ret*d)%mod
    return ret

def sol(aa, n, k):
    mod = 10**9+7
    aap = aa[aa>0]
    aam = aa[aa<0]
    if n == k:
        return mul(aa)
    if len(aap) + 2*(len(aam)//2) < k:
        return 0
    
    if len(aam) == n and k%2==1:
        aam.sort()
        aam = aam[::-1]
        return mul(aam[:k])

    aap = aa[aa>=0]
    aap.sort()
    aap = aap[::-1]
    aam.sort()

    ret=1
    if k%2 >0:
        k = k-1
        ret *= aap[0]
        aap = aap[1:]

    aap2 = [aap[2*i]*aap[2*i+1] for i in range(len(aap)//2)]
    aam2 = [aam[2*i]*aam[2*i+1] for i in range(len(aam)//2)]
    ip=0
    im=0
    for i in range(k//2):
        if ip < len(aap2) and aap2[ip] >= aam2[im]:
            ret = (ret * aap2[ip])%mod
            ip += 1
        else:
            ret = (ret * aam2[im])%mod
            im += 1
    return ret%mod

print(sol(aa, n, k))

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
aa = list(map(int, input().split()))
import numpy as np
aa = np.array(aa)

def mul(dd):
    mod = 10**9+7
    ret = 1
    for d in dd:
        ret = (ret*d)%mod
    return ret

def sol(aa, n, k):
    mod = 10**9+7
    aap = aa[aa>0]
    aam = aa[aa<0]
    if n == k:
        return mul(aa)
    if len(aap) + 2*(len(aam)//2) < k:
        return 0
    if len(aam) == n and k%2==1:
        aam.sort()
        aam = aam[::-1]
        return mul(aam[:k])

    aap.sort()
    aap = aap[::-1]
    aam.sort()

    ret=1
    if k%2 >0:
        k = k-1
        ret *= aap[0]
        aap = aap[1:]

    aap2 = [aap[2*i]*aap[2*i+1] for i in range(len(aap)//2)]
    aam2 = [aam[2*i]*aam[2*i+1] for i in range(len(aam)//2)]
    ip=0
    im=0
    for i in range(k//2):
        if ip < len(aap2) and aap2[ip] >= aam2[im]:
            ret = (ret * aap2[ip])%mod
            ip += 1
        else:
            ret = (ret * aam2[im])%mod
            im += 1
    return ret%mod

print(sol(aa, n, k))

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
aa = list(map(int, input().split()))
import numpy as np
aa = np.array(aa)
mod = 10**9+7

def mul(dd):
    ret = 1
    for d in dd:
        ret = (ret*d)%mod
    return ret

def sol(aa, n, k):
    aap = aa[aa>0]
    aam = aa[aa<0]
    if n == k:
        return mul(aa)
    if len(aam) == n and k%2==1:
        aam.sort()
        aam = aam[::-1]
        return mul(aam[:k])

    aap.sort()
    aap = aap[::-1]
    aam.sort()

    ret=1
    if k%2 >0:
        k = k-1
        ret *= aap[0]
        aap = aap[1:]

    aap2 = [aap[2*i]*aap[2*i+1] for i in range(len(aap)//2)]
    aam2 = [aam[2*i]*aam[2*i+1] for i in range(len(aam)//2)]
    ip=0
    im=0
    for i in range(k//2):
        if ip < len(aap2) and aap2[ip] >= aam2[im]:
            ret = (ret * aap2[ip])%mod
            ip += 1
        else:
            ret = (ret * aam2[im])%mod
            im += 1
    return ret%mod

print(sol(aa, n, k))