Problem p02616 (python)

Fixed submission:
def resolve():
    from bisect import bisect_left
    n,k=map(int,input().split())
    a=sorted(list(map(int,input().split())))
    m=bisect_left(a,0)
    MOD=10**9+7
    ans=1
    if m==0 or (m==n and k%2==1):
        for i in a[-k:]:
            ans=(ans*i)%MOD
    elif m==n and k%2==0:
        for i in a[:k]:
            ans=(ans*i)%MOD
    else:
        ans=1
        l,r=0,-1
        lm=a[0]*a[1]
        rm=a[-1]*a[-2]
        for i in range(k//2):
            if lm>=rm:
                ans*=lm%MOD
                l+=2
                if l<=n-2:
                    lm=a[l]*a[l+1]
            else:
                ans*=rm%MOD
                r-=2
                if 1<=n+r:
                    rm=a[r]*a[r-1]
            ans%=MOD
        if k%2==1:
            ans*=a[r]
            ans%=MOD
    print(ans)

if __name__ == '__main__':
    resolve()

Buggy submission (verdict: Runtime Error):
def resolve():
    from bisect import bisect_left
    n,k=map(int,input().split())
    a=sorted(list(map(int,input().split())))
    m=bisect_left(a,0)
    MOD=10**9+7
    if m==0:
        ans=1
        for i in a[-k:]:
            ans=(ans*i)%MOD
    elif m==n:
        ans=1
        if k%2==0:
            for i in a[:k]:
                ans=(ans*i)%MOD
        else:
            for i in a[-k:]:
                ans=(ans*i)%MOD
    else:
        cal=1
        i=(m//2)*2
        for j in a[:i]:
            cal=(cal*j)%MOD
        if k-i>0:
            for j in a[-(k-i):]:
                cal=(cal*j)%MOD
        ans=cal
        for i in range((m//2)*2-2,-2,-2):
            if k-i>n-m:
                break
            for j in a[i-2:i]:
                cal=(cal*pow(j,MOD-2,MOD))%MOD
            if k-i>=0:
                if -(k-i)+2==0:
                    for j in a[-(k-i):]:
                    cal=(cal*j)%MOD
                else:
                    for j in a[-(k-i):-(k-i)+2]:
                        cal=(cal*j)%MOD
            ans=max(ans,cal)
    print(ans)

if __name__ == '__main__':
    resolve()

Buggy submission (verdict: Runtime Error):
from numba import jit
@jit(cache = True)
def resolve():
    from bisect import bisect_left
    n,k=map(int,input().split())
    a=sorted(list(map(int,input().split())))
    m=bisect_left(a,0)
    MOD=10**9+7
    if m==0 or m==n:
        ans=1
        for i in a[-k:]:
            ans=(ans*i)%MOD
    else:
        for i in range((m//2)*2,-2,-2):
            ans=0
            cal=1
            if k-i>n-m:
                break
            for j in a[:i]:
                cal=(cal*j)%MOD
            if k-i>0:
                for j in a[-(k-i):]:
                    cal=(cal*j)%MOD
            ans=max(ans,cal)
    print(ans)

if __name__ == '__main__':
    resolve()

Buggy submission (verdict: Runtime Error):
from numba import jit
@jit
def resolve():
    from bisect import bisect_left
    n,k=map(int,input().split())
    a=sorted(list(map(int,input().split())))
    m=bisect_left(a,0)
    MOD=10**9+7
    if m==0 or m==n:
        ans=1
        for i in a[-k:]:
            ans=(ans*i)%MOD
    else:
        for i in range((m//2)*2,-2,-2):
            ans=0
            cal=1
            if k-i>n-m:
                break
            for j in a[:i]:
                cal=(cal*j)%MOD
            if k-i>0:
                for j in a[-(k-i):]:
                    cal=(cal*j)%MOD
            ans=max(ans,cal)
    print(ans)

if __name__ == '__main__':
    resolve()