Problem p02616 (python)

Fixed submission:
n,k=map(int,input().split())
lst=list(map(int,input().split()))
mod=10**9+7

plst=sorted([x for x in lst if x>=0],reverse=True)
nlst=sorted([abs(x) for x in lst if x<0],reverse=True)
out=[None for _ in range(k)]
lenn=len(nlst)
lenp=len(plst)



if lenp==0 and k%2==1:
    sm=1
    for i in range(lenn-1,lenn-k-1,-1):
        sm*=-nlst[i]
        sm%=mod
    print(sm)
    exit()

if n==k:
    sm=1
    for x in lst:
        sm*=x
        sm%=mod
    print(sm)
    exit()



ptoken=0
ntoken=0
token=0
pm=0



while token<k:
    if ptoken==lenp:
        out[token]=nlst[ntoken]
        pm=(pm+1)%2
        ntoken+=1
    elif ntoken==lenn:
        out[token]=plst[ptoken]
        ptoken+=1
    elif plst[ptoken]>nlst[ntoken]:
        out[token]=plst[ptoken]
        ptoken+=1
    else :
        out[token]=nlst[ntoken]
        pm=(pm+1)%2
        ntoken+=1
    token+=1

if pm==1:
    if ptoken==lenp:          
        out.remove(plst[ptoken-1])
        out.append(nlst[ntoken])
    elif ntoken==lenn:
        out.remove(nlst[ntoken-1])
        out.append(plst[ptoken])
    elif ptoken==0 : 
        out.remove(nlst[ntoken-1])
        out.append(plst[ptoken])
    else : 
        if nlst[ntoken-1]*nlst[ntoken]>=plst[ptoken-1]*plst[ptoken]:
            out.remove(plst[ptoken-1])
            out.append(nlst[ntoken])
        else :
            out.remove(nlst[ntoken-1])
            out.append(plst[ptoken])               

sm=1
for x in out:
    sm*=x
    sm%=mod

print(sm)

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

plst=sorted([x for x in lst if x>=0],reverse=True)
nlst=sorted([abs(x) for x in lst if x<0],reverse=True)
out=[None for _ in range(k)]
lenn=len(nlst)
lenp=len(plst)



if lenp==0 and k%2==1:
    sm=1
    for i in range(lenn-1,lenn-k-1,-1):
        sm*=-nlst[i]
        sm%=mod
    print(sm)
    exit()


ptoken=0
ntoken=0
token=0
pm=0



while token<k:
    if ptoken==lenp:
        out[token]=nlst[ntoken]
        pm=(pm+1)%2
        ntoken+=1
    elif ntoken==lenn:
        out[token]=plst[ptoken]
        ptoken+=1
    elif plst[ptoken]>nlst[ntoken]:
        out[token]=plst[ptoken]
        ptoken+=1
    else :
        out[token]=nlst[ntoken]
        pm=(pm+1)%2
        ntoken+=1
    token+=1

if pm==1:
    if n!=k:
        if ptoken==lenp:          
            out.remove(plst[ptoken-1])
            out.append(nlst[ntoken])
        elif ntoken==lenn:
            out.remove(nlst[ntoken-1])
            out.append(plst[ptoken])
        else : 
            if nlst[ntoken-1]*nlst[ntoken]>=plst[ptoken-1]*plst[ptoken]:
                out.remove(plst[ptoken-1])
                out.append(nlst[ntoken])
            else :
                out.remove(nlst[ntoken-1])
                out.append(plst[ptoken])               

sm=1
for x in out:
    sm*=x
    sm%=mod

print(sm)

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

plst=sorted([x for x in lst if x>=0],reverse=True)
nlst=sorted([abs(x) for x in lst if x<0],reverse=True)
lenn=len(nlst)
lenp=len(plst)



if lenp==0 and k%2==1:
    sm=1
    for i in range(lenn-1,lenn-k-1,-1):
        sm*=-nlst[i]
        sm%=mod
    print(sm)
    exit()


ptoken=0
ntoken=0
token=0


sm=1
while token<k:
    if ptoken==lenp:
        sm*=-nlst[ntoken]
        ntoken+=1
    elif ntoken==lenn:
        sm*=plst[ptoken]
        ptoken+=1
    elif plst[ptoken]>nlst[ntoken]:
        sm*=plst[ptoken]
        ptoken+=1
    else :
        sm*=-nlst[ntoken]
        ntoken+=1
    token+=1

if sm<0:
    if ptoken<lenp:
        sm//=nlst[ntoken-1]
        sm*=plst[ntoken]
    else : 
        if n!=k :
            sm//=plst[ntoken-1]
            sm*=nlst[ntoken]
sm%=mod
print(sm)

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

plst=sorted([x for x in lst if x>=0],reverse=True)
nlst=sorted([abs(x) for x in lst if x<0],reverse=True)
lenn=len(nlst)
lenp=len(plst)



if lenp==0 and k%2==1:
    sm=1
    for i in range(lenn-1,lenn-k-1,-1):
        sm*=-nlst[i]
        sm%=res
    print(sm)
    exit()

if lenp==0 and k%2==0:
    sm=1
    for i in range(k):
        sm*=nlst[i]
        sm%=res
    print(sm)
    exit()

nlstw=[nlst[2*i]*nlst[2*i+1] for i in range(lenn//2)]
lennw=len(nlstw)

if lenn==0 :
    sm=1
    for i in range(k):
        sm*=plst[i]
        sm%=res
    print(sm)
    exit()


ptoken=0
ntoken=0
nwtoken=0
token=0


sm=1
while token<k:

    if nwtoken==lennw or ptoken+1>=lenp : break

    if token==(k-1) or plst[ptoken]>nlst[ntoken]:
        sm*=plst[ptoken]
        sm%=res
        ptoken+=1
        token+=1
    else :
        if nlstw[nwtoken]>plst[ptoken]*plst[ptoken+1]:
            sm*=nlstw[nwtoken]
            sm%=res
            ntoken+=2
            nwtoken+=1
        else :
            sm*=plst[ptoken]*plst[ptoken+1]
            sm%=res
            ptoken+=2
        token+=2
else : print(sm);exit()

if ptoken+1>=lenp:
    if (k-token)%2==1:
        sm*=plst[ptoken]
        sm%=res
        token+=1
        while k>token:
            sm*=nlstw[nwtoken]
            sm%=res
            token+=2
            nwtoken+=1
    else :
        while k>token:
            if nwtoken==lennw:break
            sm*=nlstw[nwtoken]
            sm%=res
            token+=2
            nwtoken+=1
        if k!=token:
            sm*=plst[ptoken]
            sm%=res
    print(sm)
    exit()

if nwtoken==lennw:

    while k>token:
        if ptoken==lenp: 
            sm*=nlst[-1]
            token+=1
            continue
        sm*=plst[ptoken]
        sm%=res
        ptoken+=1
        token+=1
    print(sm)
    exit()

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

plst=sorted([x for x in lst if x>=0],reverse=True)
nlst=sorted([abs(x) for x in lst if x<0],reverse=True)
lenn=len(nlst)
lenp=len(plst)



if lenp==0 and k%2==1:
    sm=1
    for i in range(lenn-1,lenn-k-1,-1):
        sm*=-nlst[i]
        sm%=res
    print(sm)
    exit()

if lenp==0 and k%2==0:
    sm=1
    for i in range(k):
        sm*=nlst[i]
        sm%=res
    print(sm)
    exit()

nlstw=[nlst[2*i]*nlst[2*i+1] for i in range(lenn//2)]
lennw=len(nlstw)

if lenn==0 :
    sm=1
    for i in range(k):
        sm*=plst[i]
        sm%=res
    print(sm)
    exit()


ptoken=0
ntoken=0
nwtoken=0
token=0


sm=1
while token<k:

    if nwtoken==lennw or ptoken+1>=lenp : break

    if token==(k-1) or plst[ptoken]>nlst[ntoken]:
        sm*=plst[ptoken]
        sm%=res
        ptoken+=1
        token+=1
    else :
        if nlstw[nwtoken]>plst[ptoken]*plst[ptoken+1]:
            sm*=nlstw[nwtoken]
            sm%=res
            ntoken+=2
            nwtoken+=1
        else :
            sm*=plst[ptoken]*plst[ptoken+1]
            sm%=res
            ptoken+=2
        token+=2
else : print(sm);exit()

if ptoken+1>=lenp:
    if (k-token)%2==1:
        sm*=plst[ptoken]
        sm%=res
        token+=1
        while k>token:
            sm*=nlstw[nwtoken]
            sm%=res
            token+=2
            nwtoken+=1
    else :
        while k>token:
            if nwtoken==lennw:break
            sm*=nlstw[nwtoken]
            sm%=res
            token+=2
            nwtoken+=1
        if k!=token:
            sm*=plst[ptoken]
            sm%=res
    print(sm)
    exit()

if nwtoken==lennw:
    while k>token:
        sm*=plst[ptoken]
        sm%=res
        ptoken+=1
        token+=1
    print(sm)
    exit()

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

plst=sorted([x for x in lst if x>=0],reverse=True)
nlst=sorted([abs(x) for x in lst if x<0],reverse=True)
lenn=len(nlst)
lenp=len(plst)



if lenp==0 and k%2==1:
    sm=1
    for i in range(lenn-1,lenn-k-1,-1):
        sm*=-nlst[i]
        sm%=res
    print(sm)
    exit()

if lenp==0 and k%2==0:
    sm=1
    for i in range(k):
        sm*=nlst[i]
        sm%=res
    print(sm)
    exit()

nlstw=[nlst[2*i]*nlst[2*i+1] for i in range(lenn//2)]
lennw=len(nlstw)

if lenn==0 :
    sm=1
    for i in range(k):
        sm*=nlst[i]
        sm%=res
    print(sm)
    exit()


ptoken=0
ntoken=0
nwtoken=0
token=0


sm=1
while token<k:

    if nwtoken==lennw or ptoken+1>=lenp : break

    if token==(k-1) or plst[ptoken]>nlst[ntoken]:
        sm*=plst[ptoken]
        sm%=res
        ptoken+=1
        token+=1
    else :
        if nlstw[nwtoken]>plst[ptoken]*plst[ptoken+1]:
            sm*=nlstw[nwtoken]
            sm%=res
            ntoken+=2
            nwtoken+=1
        else :
            sm*=plst[ptoken]*plst[ptoken+1]
            sm%=res
            ptoken+=2
        token+=2
else : print(sm);exit()

if ptoken+1>=lenp:
    if (k-token)%2==1:
        sm*=plst[ptoken]
        sm%=res
        token+=1
        while k>token:
            sm*=nlstw[nwtoken]
            sm%=res
            token+=2
            nwtoken+=1
    else :
        while k>token:
            if nwtoken==lennw:break
            sm*=nlstw[nwtoken]
            sm%=res
            token+=2
            nwtoken+=1
        if k!=token:
            sm*=plst[ptoken]
            sm%=res
    print(sm)
    exit()

if nwtoken+1==lennw:
    while k>token:
        sm*=plst[ptoken]
        sm%=res
        ptoken+=1
        token+=1
    print(sm)
    exit()