Problem p02616 (python)

Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10**9 + 7
res = 1
a.sort(reverse=True)
if a[0]<0 and k%2==1:
    for i in range(k):
        res = (res*a[i])%mod
else:
    right = n-1
    left = 0
    while k > 1:
        if a[right]*a[right-1] < a[left]*a[left+1]:
            res = (res*a[left])%mod
            left += 1
            k -= 1
        else:
            res = (res*a[right]*a[right-1])%mod
            right -= 2
            k -= 2
    if k == 1:
        res = (res*a[left])%mod

print(res)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10**9 + 7
res = 1
a.sort(reverse=True)

if all(i <= 0 for i in a):
    for i in range(k):
        res = (res*a[i])%mod
else:
    right = n-1
    left = 0
    while k > 1:
        if a[right]*a[right-1] < a[left]*a[left+1]:
            res = (res*a[left]*a[left+1])%mod
            left += 2
        else:
            res = (res*a[right]*a[right-1])%mod
            right += 2
        k -= 2
    if k == 1:
        res = (res*max(a[left:right+1]))%mod
print(res)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
a = list(map(int, input().split()))

if all(i<=0 for i in a) or n==k:
    a.sort(reverse=True)
    res = 1
    mod = 10**9 + 7
    for i in range(k):
        res = (res*a[i])%mod
    print(res)
else:
    mainas = []
    plus = []
    for i in range(n):
        if a[i] < 0: mainas.append(a[i])
        else: plus.append(a[i])
        
    mainas.sort()
    mainas.extend([0,0,0,0])
    plus.extend([0,0,0,0])
    plus.sort(reverse=True)

    m, p = 0,0
    r = []
    while k > 1:
        if mainas[p]*mainas[p+1] < plus[m]*plus[m+1]:
            r.extend(plus[m:m+2])
            m += 2
        else:
            r.extend(mainas[p:p+2])
            p += 2
        k -= 2

    if k == 1:
        r.append(plus[m])

    res = 1
    mod = 10**9 + 7
    for i in r:
        res = (res*i)%mod
    print(res)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
a = list(map(int, input().split()))

if all(i<=0 for i in a) or n==k:
    a.sort(reverse=True)
    res = 1
    mod = 10**9 + 7
    for i in range(k):
        res = (res*a[i])%mod
    print(res)
else:
    mainas = []
    plus = []
    for i in range(n):
        if a[i] < 0: mainas.append(a[i])
        else: plus.append(a[i])
        
    mainas.sort()
    mainas.extend([0,0])
    plus.extend([0,0])
    plus.sort(reverse=True)

    m, p = 0,0
    r = []
    while k > 1:
        if mainas[p]*mainas[p+1] < plus[m]*plus[m+1]:
            r.extend(plus[m:m+2])
            m += 2
        else:
            r.extend(mainas[p:p+2])
            p += 2
        k -= 2

    if k == 1:
        r.append(plus[m])

    res = 1
    mod = 10**9 + 7
    for i in r:
        res = (res*i)%mod
    print(res)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
a = list(map(int, input().split()))

if all(i<=0 for i in a) or n==k:
    a.sort(reverse=True)
    res = 1
    mod = 10**9 + 7
    for i in range(k):
        res = (res*a[i])%mod
    print(res)
else:
    mainas = []
    plus = []
    for i in range(n):
        if a[i] < 0: mainas.append(a[i])
        else: plus.append(a[i])
        
    mainas.sort()
    mainas.extend([0,0])
    plus.sort(reverse=True)
    plus.extend([0,0])

    m, p = 0,0
    r = []
    while k > 1:
        if mainas[p]*mainas[p+1] < plus[m]*plus[m+1]:
            r.extend(plus[m:m+2])
            m += 2
        else:
            r.extend(mainas[p:p+2])
            p += 2
        k -= 2

    if k == 1:
        r.append(plus[m])

    res = 1
    mod = 10**9 + 7
    for i in r:
        res = (res*i)%mod
    print(res)