Problem p02616 (python)

Fixed submission:
def solve(n,k,a):
    mod = 10**9 + 7
    ans = 1
    l = 0
    r = n-1
    a.sort()
    
    if k%2 == 1:
        ans *= a[r]
        r-=1
    
    if ans < 0:
        ans = 1
        for i in range(k):
            ans*= a[n-i-1]
            ans%= mod
        return ans
    
    for i in range(k//2):
        tmp_min=a[l]*a[l+1]
        tmp_max=a[r]*a[r-1]
        if tmp_max> tmp_min:
            ans*=tmp_max
            ans%=mod
            r-=2
        else:
            ans*= tmp_min
            ans%=mod
            l+=2
    return ans


n, k = map(int, input().split())
A = list(map(int,input().split()))
ans = solve(n,k,A)
print(ans)

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

def solve(n,k,a):
    mod = 10**9 + 7
    ans = 1
    if k == n:
        for i in a:
            ans = ans * i % mod
        return ans
    else:
        pos = [i for i in a if i>0]
        neg = [i for i in a if i<0]

        if len(neg) + len(pos) < 0:
            return 0            
        elif len(pos)== 0 and k%2== 1:
            neg.sort(reverse=True)
            for i in neg[:k]:
                ans = ans * i % mod
            return ans
        else:
            pos.sort()
            neg.sort(reverse=True)
            ctr = k
            pos = deque(pos)
            neg = deque(neg)

            while ctr >= 2:
                if len(pos) >=2 and len(neg) >2:
                    tmp_p = pos[-1] * pos[-2] %mod
                    tmp_n = neg[-1] * neg[-2] %mod
                    if tmp_p >=tmp_n:
                        ans = ans * tmp_p %mod
                        pos.pop()
                        pos.pop()
                    else:
                        ans = ans * tmp_n %mod
                        neg.pop()
                        neg.pop()
                    ctr-=2
                elif len(neg) >= 2:
                    ans = ans * neg.pop() * neg.pop() %mod 
                    ctr-=2
                else:
                    ans = ans * pos.pop() %mod
                    ctr -=1
                
            if ctr == 1:
                ans = ans * pos.pop() % mod

            return ans
                    
n, k = map(int, input().split())
A = list(map(int,input().split()))
ans = solve(n,k,A)
print(ans)

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

def solve(n,k,a):
    pos=[]
    neg=[]
    mod = 10**9 + 7
    ans = 1
    if k == n:
        for i in a:
            ans = ans * i % mod
        return ans
    else:
        for i in a:
            if i >0:
                pos.append(i)
            elif i<0:
                neg.append(i)

        if len(neg) + len(pos) < 0:
            return 0            
        elif len(pos)== 0:
            if k%2== 1:
                neg.sort(reverse=True)
                for i in neg[:k]:
                    ans = ans * i % mod
                return ans
            else:
                neg.sort()
                for i in neg[:k]:
                    ans = ans * i % mod
                return ans
        elif len(neg)==0:
            pos.sort(reverse=True)
            for i in pos[:k]:
                ans = ans * i % mod
            return ans
        else:
            pos.sort()
            neg.sort(reverse=True)
            ctr = 0
            if k%2 == 1:
                ans = ans * pos.pop() % mod
                ctr +=1
            pos = deque(pos)
            neg = deque(neg)

            while ctr < k:
                if len(pos) <= 1:
                    ans = ans * neg.pop() * neg.pop() %mod 
                elif len(neg) <= 1:
                    ans = ans * pos.pop() * pos.pop() %mod
                else:
                    tmp_p = pos[-1] * pos[-2] %mod
                    tmp_n = neg[-1] * neg[-2] %mod
                    if tmp_p >=tmp_n:
                        ans = ans * tmp_p %mod
                        pos.pop()
                        pos.pop()
                    else:
                        ans = ans * tmp_n %mod
                        neg.pop()
                        neg.pop()
                ctr+=2
            return ans
                    
n, k = map(int, input().split())
A = list(map(int,input().split()))
ans = solve(n,k,A)
print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
A = list(map(int,input().split()))
mod = 10**9 + 7

ans = 1
if k == n:
    for i in A:
        ans*=i
elif sum([A[i]<0 for i in range(n)]) == n and k%2 ==1:
    A = sorted(A, key=abs)
    for i in range(k):
        ans *= A[i]
else:
    nega = []
    posi = []
    for i in A:
        if i>0:
            posi.append(i)
        else:
            nega.append(i)
    posi.sort(reverse=True)
    nega.sort()
    ctr = 0
    pi=0
    ni=0
    if k%2 == 1:
        ans*=posi[pi]
        pi+=1
        ctr+=1
        
    if ctr <k:
        tmp_p = posi[pi]*posi[pi+1]
        tmp_n = nega[ni]*nega[ni+1]
    while ctr < k:
        if tmp_p >= tmp_n:
            ans *= tmp_p
            pi += 2
            ctr+=2
            if ctr<k:
                tmp_p = posi[pi]*posi[pi+1]
        else:
            ans *= tmp_n
            ni += 2
            ctr+=2
            if ctr<k:
                tmp_n = nega[ni]*nega[ni+1]
    

ans %=mod 
print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
A = list(map(int,input().split()))
mod = 10**9 + 7

ans = 1
if k == n:
    for i in A:
        ans*=i
elif sum([A[i]<0 for i in range(n)]) == n and k%2 ==1:
    A = sorted(A, key=abs)
    for i in range(k):
        ans *= A[i]
else:
    A = sorted(A, key=abs, reverse=True)
    ctr = 0
    i = 0
    negactr = 0
    posictr = 0
    nega = 1
    posi = 1
    if k%2 ==1:
        while posictr < 1:
            if A[i] > 0:
                posictr+=1
                ans *= A[i]
                ctr += 1
            else:
                negactr +=1
                nega*=A[i]

            if negactr ==2:
                negactr=0
                ctr+=2
                ans*=nega
                nega=1
            i+=1
        posictr=0

    while ctr < k:
        if A[i] > 0:
            posictr+=1
            posi *= A[i]
        else:
            negactr +=1
            nega*=A[i]

        if posictr ==2:
            posictr=0
            ctr+=2
            ans*=posi
            posi=1
        elif negactr ==2:
            negactr=0
            ctr+=2
            ans*=nega
            nega=1
        i+=1

ans %=mod 
print(ans)