Problem p02616 (python)

Fixed submission:
mod=10**9+7
num,k=map(int,input().rstrip().split())
mai=list(map(int, input().rstrip().split()))
main=sorted(mai,key=abs,reverse=True)
result=1
def findplus_plus(x):
    for i in range(x,len(main)):
        if main[i]>=0:
            return main[i]
    return ''
def findplus_minus(x):
    for i in range(x,len(main)):
        if main[i]<0:
            return main[i]
    return ''
def findminus_plus(x):
    for i in range(x-1,-1,-1):
        if main[i]>=0:
            return main[i]
    return ''
def findminus_minus(x):
    for i in range(x-1,-1,-1):
        if main[i]<0:
            return main[i]
    return ''

if num==k:
    for i in main:
        result*=i
        result%=mod
elif sorted(main)[len(sorted(main))-1]<0:
    if (k%2)==0:
        for i in range(k):
            result*=main[i]
            result%=mod
    else:
        for i in range(len(main)-1,len(main)-1-k,-1):
            result*=main[i]
            result%=mod

else:
    count=0
    for i in range(k):
        if main[i]<0:
            count+=1
        result*=main[i]
        result%=mod
    if (count%2)==1:
        minplus=findminus_plus(k)
        minminus=findminus_minus(k)
        maxplus=findplus_plus(k)
        maxminus=findplus_minus(k)
        if type(maxplus)==type(''):
            plusplus=-1
            minusminus=minminus*maxminus
        else:
            if type(maxminus)==type('') or type(minplus)==type(''):
                minusminus=-1
                plusplus=0
            else:
                minusminus=minminus*maxminus
                plusplus=minplus*maxplus
        if plusplus>minusminus:
            main.remove(minminus)
            result=1
            for i in range(k-1):
                result*=main[i]
                result%=mod
            result*=maxplus
        elif plusplus<=minusminus:
            main.remove(minplus)
            result=1
            for i in range(k-1):
                result*=main[i]
                result%=mod
            result*=maxminus
            
print(result%mod)

Buggy submission (verdict: Runtime Error):
mod=10**9+7
nums=input().rstrip().split()
num=int(nums[0])
k=int(nums[1])
mai=list(map(int, input().rstrip().split()))
main=sorted(mai,key=abs,reverse=True)
result=1
def findplus_plus(x):
    for i in range(x,len(main)):
        if main[i]>=0:
            return main[i]
    return ''
def findplus_minus(x):
    for i in range(x,len(main)):
        if main[i]<0:
            return main[i]
    return ''
def findminus_plus(x):
    for i in range(x-1,-1,-1):
        if main[i]>=0:
            return main[i]
    return ''
def findminus_minus(x):
    for i in range(x-1,-1,-1):
        if main[i]<0:
            return main[i]
    return ''

if num==k:
    for i in main:
        result*=i
        result%=mod
elif sorted(main)[len(sorted(main))-1]<0:
    if (k%2)==0:
        for i in range(k):
            result*=main[i]
            result%=mod
    else:
        for i in range(len(main)-1,len(main)-1-k,-1):
            result*=main[i]
            result%=mod

else:
    count=0
    for i in range(k):
        if main[i]<0:
            count+=1
        result*=main[i]
        result%=mod
    if (count%2)==1:
        minplus=findminus_plus(k)
        minminus=findminus_minus(k)
        maxplus=findplus_plus(k)
        maxminus=findplus_minus(k)
        if type(maxplus)==type(''):
            plusplus=-1
            minusminus=minminus*maxminus
        else:
            if type(maxminus)==type('') or type(minplus)==type(''):
                minusminus=-1
                plusplus=0
            else:
                minusminus=minminus*maxminus
                plusplus=minplus*maxplus
        if plusplus>minusminus:
            main.remove(minmius)
            result=1
            for i in range(k-1):
                result*=main[i]
                result%=mod
            result*=maxplus
        elif plusplus<=minusminus:
            main.remove(minplus)
            result=1
            for i in range(k-1):
                result*=main[i]
                result%=mod
            result*=maxminus
            
print(result%mod)

Buggy submission (verdict: Runtime Error):
nums=input().rstrip().split()
num=int(nums[0])
k=int(nums[1])
mai=list(map(int, input().rstrip().split()))
main=sorted(mai,key=abs,reverse=True)
result=1
def findplus_plus(x):
    for i in range(x,len(main)):
        if main[i]>=0:
            return main[i]
    return ''
def findplus_minus(x):
    for i in range(x,len(main)):
        if main[i]<0:
            return main[i]
    return ''
def findminus_plus(x):
    for i in range(x-1,-1,-1):
        if main[i]>=0:
            return main[i]
    return ''
def findminus_minus(x):
    for i in range(x-1,-1,-1):
        if main[i]<0:
            return main[i]
    return ''
    
if num==k:
    for i in main:
        if i>0:
            result*=i%(10**9+7)
        else:
            result*=i
elif sorted(main)[len(sorted(main))-1]<0:
    if (k%2)==0:
        for i in range(k):
            result*=main[i]
            result%=10**9+7
    else:
        for i in range(len(main)-1,len(main)-1-k,-1):
            result*=main[i]
            result%=10**9+7

else:
    for i in range(k):
        result*=main[i]
    if result<0:
        minplus=findminus_plus(k)
        minminus=findminus_minus(k)
        maxplus=findplus_plus(k)
        maxminus=findplus_minus(k)
        if type(maxplus)==type(''):
            plusplus=-1
            minusminus=minminus*maxminus
        else:
            if type(maxminus)==type('') or type(minplus)==type(''):
                minusminus=-1
                plusplus=0
            else:
                minusminus=minminus*maxminus
                plusplus=minplus*maxplus
        if plusplus>minusminus:
            main.remove(minmius)
            result=1
            for i in range(k-1):
                result*=main[i]
                result%=10**9+7
            result*=maxplus
        elif plusplus<=minusminus:
            main.remove(minplus)
            result=1
            for i in range(k-1):
                result*=main[i]
                result%=10**9+7
            result*=maxminus
            
print(result%(10**9+7))

Buggy submission (verdict: Runtime Error):
nums=input().rstrip().split()
num=int(nums[0])
k=int(nums[1])
mai=list(map(int, input().rstrip().split()))
main=sorted(mai,key=abs,reverse=True)
result=1
def findplus_plus(x):
    for i in range(x,len(main)):
        if main[i]>=0:
            return main[i]
    return ''
def findplus_minus(x):
    for i in range(x,len(main)):
        if main[i]<0:
            return main[i]
    return ''
def findminus_plus(x):
    for i in range(x-1,-1,-1):
        if main[i]>=0:
            return main[i]
    return ''
def findminus_minus(x):
    for i in range(x-1,-1,-1):
        if main[i]<0:
            return main[i]
    return ''
    
if num==k:
    for i in main:
        if i>0:
            result*=i%(10**9+7)
        else:
            result*=i
elif sorted(main)[len(sorted(main))-1]<0:
    if (k%2)==0:
        for i in range(k):
            result*=main[i]
    else:
        for i in range(len(main)-1,len(main)-1-k,-1):
            result*=main[i]

else:
    for i in range(k):
        result*=main[i]
    if result<0:
        minplus=findminus_plus(k)
        minminus=findminus_minus(k)
        maxplus=findplus_plus(k)
        maxminus=findplus_minus(k)
        if type(maxplus)==type(''):
            plusplus=-1
            minusminus=minminus*maxminus
        else:
            if type(maxminus)==type('') or type(minplus)==type(''):
                minusminus=-1
                plusplus=0
            else:
                minusminus=minminus*maxminus
                plusplus=minplus*maxplus
        if plusplus>minusminus:
            result=int(result*maxplus/minminus)
        elif plusplus<=minusminus:
            result=int(result*maxminus/minplus)
            
print(result%(10**9+7))

Buggy submission (verdict: Runtime Error):
nums=input().rstrip().split()
num=int(nums[0])
k=int(nums[1])
mai=list(map(int, input().rstrip().split()))
main=sorted(mai,key=abs,reverse=True)
result=1
def findplus_plus(x):
    for i in range(x,len(main)):
        if main[i]>=0:
            return main[i]
    return ''
def findplus_minus(x):
    for i in range(x,len(main)):
        if main[i]<0:
            return main[i]
    return ''
def findminus_plus(x):
    for i in range(x-1,-1,-1):
        if main[i]>=0:
            return main[i]
    return ''
def findminus_minus(x):
    for i in range(x-1,-1,-1):
        if main[i]<0:
            return main[i]
    return ''
    
if num==k:
    for i in main:
        result*=i
elif sorted(main)[len(sorted(main))-1]<0:
    if (k%2)==0:
        for i in range(k):
            result*=main[i]
    else:
        for i in range(len(main)-1,len(main)-1-k,-1):
            result*=main[i]

else:
    for i in range(k):
        result*=main[i]
    if result<0:
        if type(findplus_plus(k))==type(''):
            plusplus=-1
            minusminus=findminus_minus(k)*findplus_minus(k)
        else:
            if type(findplus_minus(k))==type('') or type(findminus_plus(k))==type(''):
                minusminus=-1
                plusplus=0
            else:
                minusminus=findminus_minus(k)*findplus_minus(k)
                plusplus=findplus_plus(k)*findminus_plus(k)
        if plusplus>minusminus:
            result=int(result*findplus_plus(k)/findminus_minus(k))
        elif plusplus<=minusminus:
            result=int(result*findplus_minus(k)/findminus_plus(k))
            
print(result%(10**9+7))

Buggy submission (verdict: Runtime Error):
nums=input().rstrip().split()
num=int(nums[0])
k=int(nums[1])
mains=input().rstrip().split()
mai=[]
for i in range(len(mains)):
    mai.append(int(mains[i]))
main=sorted(mai,key=abs,reverse=True)
result=1
def findplus_plus(x):
    for i in range(x,len(main)):
        if main[i]>=0:
            return main[i]
    return ''
def findplus_minus(x):
    for i in range(x,len(main)):
        if main[i]<0:
            return main[i]
    return ''
def findminus_plus(x):
    for i in range(x-1,-1,-1):
        if main[i]>=0:
            return main[i]
    return ''
def findminus_minus(x):
    for i in range(x-1,-1,-1):
        if main[i]<0:
            return main[i]
    return ''
    
if num==k:
    for i in main:
        result*=i
elif sorted(main)[len(sorted(main))-1]<0:
    if (k%2)==0:
        for i in range(k):
            result*=main[i]
    else:
        for i in range(len(main)-1,len(main)-1-k,-1):
            result*=main[i]

else:
    for i in range(k):
        result*=main[i]
    if result<0:
        if type(findplus_plus(k))==type(''):
            plusplus=-1
            minusminus=findminus_minus(k)*findplus_minus(k)
        else:
            if type(findplus_minus(k))==type('') or type(findminus_plus(k))==type(''):
                minusminus=-1
                plusplus=0
            else:
                minusminus=findminus_minus(k)*findplus_minus(k)
                plusplus=findplus_plus(k)*findminus_plus(k)
        if plusplus>minusminus:
            result=int(result*findplus_plus(k)/findminus_minus(k))
        elif plusplus<=minusminus:
            result=int(result*findplus_minus(k)/findminus_plus(k))
            
print(result%(10**9+7))

Buggy submission (verdict: Runtime Error):
nums=input().rstrip().split()
num=int(nums[0])
k=int(nums[1])
mains=input().rstrip().split()
mai=[]
for i in range(len(mains)):
    mai.append(int(mains[i]))
main=sorted(mai,key=abs,reverse=True)
result=1
def findplus_plus(x):
    for i in range(x,len(main)):
        if main[i]>=0:
            return main[i]
    return -1
def findplus_minus(x):
    for i in range(x,len(main)):
        if main[i]<0:
            return main[i]
    return 0
def findminus_plus(x):
    for i in range(x-1,-1,-1):
        if main[i]>=0:
            return main[i]
    return 0
def findminus_minus(x):
    for i in range(x-1,-1,-1):
        if main[i]<0:
            return main[i]
    return 0
    
if num==k:
    for i in main:
        result*=i
elif sorted(main)[len(sorted(main))-1]<0:
    if (k%2)==0:
        for i in range(k):
            result*=main[i]
    else:
        for i in range(len(main)-1,len(main)-1-k,-1):
            result*=main[i]

else:
    for i in range(k):
        result*=main[i]
    if result<0:
        if findplus_plus(k)==-1:
            plusplus=-1
            minusminus=findminus_minus(k)*findplus_minus(k)
        else:
            plusplus=findplus_plus(k)*findminus_plus(k)
            if findplus_minus(k)==0 or findminus_plus(k)==0:
                minusminus=-1
            else:
                minusminus=findminus_minus(k)*findplus_minus(k)
        if plusplus>minusminus:
            result=int(result*findplus_plus(k)/findminus_minus(k))
        elif plusplus<=minusminus:
            result=int(result*findplus_minus(k)/findminus_plus(k))
            
print(result%(10**9+7))

Buggy submission (verdict: Runtime Error):
nums=input().rstrip().split()
num=int(nums[0])
k=int(nums[1])
mains=input().rstrip().split()
mai=[]
for i in range(len(mains)):
    mai.append(int(mains[i]))
main=sorted(mai,key=abs,reverse=True)
result=1
def findplus_plus(x):
    for i in range(x,len(main)-1):
        if main[i]>=0:
            return main[i]
    return -1
def findplus_minus(x):
    for i in range(x,len(main)-1):
        if main[i]<0:
            return main[i]
    return 0
def findminus_plus(x):
    for i in range(x-1,-1,-1):
        if main[i]>=0:
            return main[i]
    return -1
def findminus_minus(x):
    for i in range(x-1,-1,-1):
        if main[i]<0:
            return main[i]
    return 0
    
if num==k:
    for i in main:
        result*=i
elif sorted(main)[len(sorted(main))-1]<0:
    if (k%2)==0:
        for i in range(k):
            result*=main[i]
    else:
        for i in range(len(main)-1,len(main)-1-k,-1):
            result*=main[i]
else:
    for i in range(k):
        result*=main[i]
    if result<0:
        if findplus_plus(k)==-1:
            plusplus=0
            minusminus=findminus_minus(k)*findplus_minus(k)
        else:
            plusplus=findplus_plus(k)*findminus_plus(k)
            if findplus_minus(k)==0 or findminus_plus(k)==-1:
                minusminus=0
            else:
                minusminus=findminus_plus(k)*findminus_minus(k)
        if plusplus>minusminus:
            result=int(result*findplus_plus(k)/findminus_minus(k))
        elif plusplus<=minusminus:
            result=int(result*findplus_minus(k)/findminus_plus(k))
print(result%(10**9+7))

Buggy submission (verdict: Runtime Error):
nums=input().rstrip().split()
num=int(nums[0])
k=int(nums[1])
mains=input().rstrip().split()
mai=[]
for i in range(len(mains)):
    mai.append(int(mains[i]))
main=sorted(mai,key=abs,reverse=True)
result=1
def findplus_plus(x):
    for i in range(x,len(main)-1):
        if main[i]>=0:
            return main[i]
    return -1
def findplus_minus(x):
    for i in range(x,len(main)-1):
        if main[i]<0:
            return main[i]
    return 0
def findminus_plus(x):
    for i in range(x-1,-1,-1):
        if main[i]>=0:
            return main[i]
    return -1
def findminus_minus(x):
    for i in range(x-1,-1,-1):
        if main[i]<0:
            return main[i]
    return 0
    
if num==k:
    for i in main:
        result*=i
elif sorted(main)[len(sorted(main))-1]<0:
    if (k%2)==0:
        for i in range(k):
            result*=main[i]
    else:
        for i in range(len(main)-1,len(main)-1-k,-1):
            result*=main[i]
else:
    for i in range(k):
        result*=main[i]
    if result<0:
        if findplus_plus(k)==-1:
            result1=0
            result2=int(result*findplus_minus(k)/findminus_plus(k))
        else:
            result1=int(result*findplus_plus(k)/findminus_minus(k))
            if findplus_minus(k)==0 or findminus_plus(k)==-1:
                result2=0
            else:
                result2=int(result*findplus_minus(k)/findminus_plus(k))
        result=max[result1,result2]
print(result%(10**9+7))