Problem p02616 (python)

Fixed submission:
MOD=10**9+7
N,K=[int(s) for s in input().split()]
ls=[int(s) for s in input().split()]
ans=set()
ls=sorted(ls, key=lambda x: -abs(x))
mul=1
sign=0
zeroflag=0
flag=[0 for _ in range(N)]
for i in range(K):
  flag[i]=1
  if ls[i]<0:
    sign=(sign+1)%2
if sign==1:
  if 0 in ls:
    zeroflag=1
  a=[0,0,0,0]
  b=[0,0,0,0]
  for j in range(K,N):
    if ls[j]>0:
      for k in range(K):
        if ls[K-1-k]<0:
          a[0]=ls[j]
          b[0]=j
          a[1]=ls[K-1-k]
          b[1]=K-1-k
          break
      break
  for j in range(K,N):
    if ls[j]<0:
      for k in range(K):
        if ls[K-1-k]>0:
          a[2]=ls[j]
          b[2]=j
          a[3]=ls[K-1-k]
          b[3]=K-1-k
          break
      break
  if a==[0,0,0,0]:
    flag.reverse()
  elif b[0]==0 and b[1]==0:
    flag[b[2]]=1
    flag[b[3]]=0
  elif b[2]==0 and b[3]==0:
    flag[b[0]]=1
    flag[b[1]]=0
  elif abs(a[0]*a[3])>abs(a[1]*a[2]):
    flag[b[0]]=1
    flag[b[1]]=0
  else:
    flag[b[2]]=1
    flag[b[3]]=0
if sign==1 and a==[0,0,0,0] and zeroflag==1:
  print(0)
else:
  mul=1
  for i in range(N):
    if flag[i]==1:
      mul=mul*ls[i]%MOD
  print(mul)

Buggy submission (verdict: Runtime Error):
MOD=10**9+7
N,K=[int(s) for s in input().split()]
ls=[int(s) for s in input().split()]
pls=[]
mls=[]
for i in range(N):
  if ls[i]>=0:
    pls.append(ls[i])
  else:
    mls.append(ls[i])
pls=sorted(pls)
mls=sorted(mls,reverse=True)

#N=Kのときは全部かけるしかない
if N==K:
  ans=1
  for i in range(N):
    ans=(ans*ls[i])%MOD
  print(ans)
  exit()
  
#全部負の数でKが奇数のとき
if pls==[] and K%2==1:
  ans=1
  for i in range(K):
    ans=(ans*mls[-1-i])%MOD
  print(ans)
  exit()

#その他
p=len(pls)
m=len(mls)
ans=1
k=K
if k%2==1:
  ans=ans*pls[-1]%MOD
  del pls[-1]
  k-=1
while k>0:
  if m==1:
    ans=(ans*pls[-1]*pls[-2])%MOD
    del pls[-1]
    del pls[-1]
    p-=2
  elif p>1 and pls[-1]*pls[-2]>mls[-1]*mls[-2]:
    ans=(ans*pls[-1]*pls[-2])%MOD
    del pls[-1]
    del pls[-1]
    p-=2
  else:
    ans=(ans*mls[-1]*mls[-2])%MOD
    del mls[-1]
    del mls[-1]
    m-=2
  k-=2
print(ans)

Buggy submission (verdict: Runtime Error):
MOD=10**9+7
N,K=[int(s) for s in input().split()]
ls=[int(s) for s in input().split()]
pls=[]
mls=[]
for i in range(N):
  if ls[i]>=0:
    pls.append(ls[i])
  else:
    mls.append(ls[i])
pls=sorted(pls)
mls=sorted(mls,reverse=True)

#N=Kのときは全部かけるしかない
if N==K:
  ans=1
  for i in range(N):
    ans=(ans*ls[i])%MOD
  print(ans)
  exit()
  
#全部負の数でKが奇数のとき
if pls==[] and K%2==1:
  ans=1
  for i in range(K):
    ans=(ans*mls[-1-i])%MOD
  print(ans)
  exit()

#その他
p=len(pls)
m=len(mls)
ans=1
k=K
if k%2==1:
  ans=ans*pls[-1]%MOD
  del pls[-1]
  k-=1
while k>0:
  if m==1:
    ans=(ans*pls[-1]*pls[-2])%MOD
    del pls[-1]
    del pls[-1]
  elif p>1 and pls[-1]*pls[-2]>mls[-1]*mls[-2]:
    ans=(ans*pls[-1]*pls[-2])%MOD
    del pls[-1]
    del pls[-1]
  else:
    ans=(ans*mls[-1]*mls[-2])%MOD
    del mls[-1]
    del mls[-1]
  k-=2
print(ans)

Buggy submission (verdict: Runtime Error):
MOD=10**9+7
N,K=[int(s) for s in input().split()]
ls=[int(s) for s in input().split()]
ans=set()
if 0 in ls:
  ans.add(0)
ls=sorted(ls, key=lambda x: -abs(x))
mul=1
for i in range(K):
  mul*=ls[i]
if mul>0:
  ans.add(mul)
else:
 #残りのプラスのものをマイナスと取り換える
 for j in range(K,N):
    if ls[j]>0:
      for k in range(K):
        if ls[K-1-k]<0:
          ans.add(mul*ls[j]//ls[K-1-k])
          break
      break
 #残りのマイナスのものをプラスと取り換える
 for j in range(K,N):
    if ls[j]<0:
      for k in range(K):
        if ls[K-1-k]>0:
          ans.add(mul*ls[j]//ls[K-1-k])
          break
      break
print(max(ans)%MOD)

Buggy submission (verdict: Runtime Error):
MOD=10**9+7
N,K=[int(s) for s in input().split()]
ls=[int(s) for s in input().split()]
ans=set()
if 0 in ls:
  ans.add(0)
else:
  als=sorted(ls, key=lambda x: -abs(x))
  mul=1
  for i in range(K):
    mul*=als[i]
  if mul>0:
    ans.add(mul)
  else:
    ls2=als[:K]
    ls3=als[K:]
    M2=max(ls2)
    m2=min(ls2)
    M3=max(ls3)
    m3=max(ls3)
    ans=set()
    if M2!=0:
      ans.add(mul*m3//M2)
      ans.add(mul*m3//m2)
    if m2!=0:
      ans.add(mul*M3//M2)
      ans.add(mul*M3//m2)
print(max(ans)%MOD)

Buggy submission (verdict: Runtime Error):
MOD=10**9+7
import bisect
N,K=[int(s) for s in input().split()]
ls=[int(s) for s in input().split()]
ans=set()
if 0 in ls:
  ans.add(0)
else:
  als=sorted(ls, key=lambda x: -abs(x))
  mul=1
  for i in range(K):
    mul*=als[i]
  if mul>0:
    ans.add(mul)
  else:
    ls2=als[:K]
    ls3=als[K:]
    M2=max(ls2)
    m2=min(ls2)
    M3=max(ls3)
    m3=max(ls3)
    ans=set()
    if M2!=0:
      ans.add(mul*m3//M2)
      ans.add(mul*m3//m2)
    if m2!=0:
      ans.add(mul*M3//M2)
      ans.add(mul*M3//m2)
print(max(ans)%MOD)

Buggy submission (verdict: Runtime Error):
MOD=10**9+7
import bisect
N,K=[int(s) for s in input().split()]
ls=[int(s) for s in input().split()]
ans=set()
if 0 in ls:
  ans.add(0)

als=sorted(ls, key=lambda x: -abs(x))
mul=1
for i in range(K):
  mul*=als[i]
if mul>0:
  print(mul%MOD)
else:
  ls2=als[:K]
  ls3=als[K:]
  M2=max(ls2)
  m2=min(ls2)
  M3=max(ls3)
  m3=max(ls3)
  ans=set()
  ans.add(mul*m3//M2)
  ans.add(mul*m3//m2)
  ans.add(mul*M3//M2)
  ans.add(mul*M3//m2)
  print(max(ans)%MOD)

Buggy submission (verdict: Runtime Error):
MOD=10**9+7
import bisect
N,K=[int(s) for s in input().split()]
ls=[int(s) for s in input().split()]
ans=set()
if 0 in ls:
  ans.add(0)

als=sorted(ls, key=lambda x: -abs(x))
mul=1
for i in range(K):
  mul*=als[i]
if mul>0:
  print(mul%MOD)
else:
  ls2=als[:K]
  ls3=als[K:]
  ls2.sort()
  ls3.sort()
  ans=set()
  ans.add(mul*ls3[0]//ls2[-1])
  ans.add(mul*ls3[-1]//ls2[0])
  ans.add(mul*ls3[-1]//ls2[-1])
  ans.add(mul*ls3[0]//ls2[0])
  print(max(ans)%MOD)

Buggy submission (verdict: Runtime Error):
MOD=10**9+7
import bisect
N,K=[int(s) for s in input().split()]
ls=[int(s) for s in input().split()]
ls=sorted(ls)
s=bisect.bisect_left(ls,0)
t=bisect.bisect_right(ls,0)
mls=ls[:s]
pls=ls[t:]
M=s
P=N-t
ans=set()
#プラスの値にすること不可能か？
if (P==0 and K%2==1) or (N==K and M%2==1):
  #ゼロにできるか？
  if N-P-M>0:
    print(0)
  else:
    mls.reverse()
    mul=1
    if P==0:
      for i in range(K):
        mul*=mls[i]
    else:
      for i in range(N):
        mul*=ls[i]
    ans.add(mul)
    print(max(ans)%MOD)
else:
  als=sorted(ls, key=lambda x: -abs(x))
  mul=1
  for i in range(K):
    mul*=als[i]
  if mul>0:
    print(mul%MOD)
  else:
    ls2=als[:K]
    ls3=als[K:]
    ls2.sort()
    ls3.sort()
    ans=set()
    ans.add(mul*ls3[0]//ls2[-1])
    ans.add(mul*ls3[-1]//ls2[0])
    print(max(ans)%MOD)

Buggy submission (verdict: Runtime Error):
MOD=10**9+7
import bisect
N,K=[int(s) for s in input().split()]
ls=[int(s) for s in input().split()]
ls=sorted(ls)
s=bisect.bisect_left(ls,0)
t=bisect.bisect_right(ls,0)
mls=ls[:s]
pls=ls[t:]
M=s
P=N-t
ans=set()
#プラスの値にすること不可能か？
if (P==0 and K%2==1) or (N==K and M%2==1):
  #ゼロにできるか？
  if N-P-M>0:
    print(0)
  else:
    mls.reverse()
    mul=1
    if P==0:
      for i in range(K):
        mul*=mls[i]
    else:
      for i in range(N):
        mul*=ls[i]
    ans.add(mul)
else:
  pls.reverse()
  for a in range(K+1)[::2]:
  #  print(a,pls[:K-a+1],mls[:a])
    if K-a>P:
      continue
    if a>M:
      break
    mul=1
    for i in range(a):
      mul*=mls[i]
    for i in range(K-a):
      mul*=pls[i]
    ans.add(mul)
print(max(ans)%MOD)