Problem p02616 (python)

Fixed submission:
import sys

# after これ通る？コピペs


def solve(n, k, aaa):
    MOD = 10 ** 9 + 7

    if n == k:
        ans = 1
        for a in aaa:
            ans = ans * a % MOD
        return ans

    pos = []  # Strictly, Non-neg
    neg = []
    for a in aaa:
        if a >= 0:
            pos.append(a)
        elif a < 0:
            neg.append(a)

    if len(pos) == 0:
        if k % 2 == 1:
            neg.sort(reverse=True)
        else:
            neg.sort()
        ans = 1
        for a in neg[:k]:
            ans = ans * a % MOD
        return ans

    pos.sort()
    neg.sort(reverse=True)
    ans = 1
    r = k

    if r % 2 == 1:
        ans = ans * pos.pop() % MOD
        r -= 1

    while r > 0:
        if len(pos) <= 1:
            ans = ans * neg.pop() * neg.pop() % MOD
        elif len(neg) <= 1:
            ans = ans * pos.pop() * pos.pop() % MOD
        else:
            pm = pos[-1] * pos[-2]
            nm = neg[-1] * neg[-2]
            if pm > nm:
                ans = ans * pos.pop() * pos.pop() % MOD
            else:
                ans = ans * neg.pop() * neg.pop() % MOD
        r -= 2

    return ans


n, k, *aaa = map(int, sys.stdin.buffer.read().split())
print(solve(n, k, aaa))

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!

n,k,*a=map(int,open(0).read().split())

MOD=10**9+7

a.sort()
# print(a)

if a[-1]<0: #all minus
    if k%2==1:
        ans=1
        for i in range(k):
            ans=ans*a.pop()
            ans=ans%MOD
        print(ans)
        exit()
    a=a[::-1]
    for i in range(k):
        ans=ans*a.pop()
        ans=ans%MOD
    print(ans)
    exit()

if n==k:
  ans=1
  for i in range(k):
    ans=ans*a[i]%MOD
  print(ans)
  exit()    
    
l=0
r=n-1
ans=1
cnt=0
while True:
    if cnt==k:
        break
    if a[l]*a[l+1]>a[r]:
        ans=ans*a[l]%MOD
        ans=ans*a[l+1]%MOD
        l+=2
        cnt+=2
    else:
        ans=ans*a[r]%MOD
        r-=1
        cnt+=1
print(ans)

Buggy submission (verdict: Runtime Error):
n,k,*a=map(int,open(0).read().split())
a.sort(reverse=True,key=lambda x:abs(x))
MOD=10**9+7
ans=1
if n==k:
  for i in a:
    ans*=i
    ans%=MOD
  print(ans)
  exit()
if k%2==1 and sum([i>=0 for i in a])==0:
  ans=1
  for i in range(k):
    ans*=a[-1-i]
    ans%=MOD
  print(ans)
  exit()

sp=[]
sn=[]
negidx=False
posidx=False
flg=1
for i in range(k):
  if a[i]>=0:
    posidx=i
  else:
    negidx=i
    flg*=-1
  sp.append(a[i])
  sn.append(a[i])
  
if flg==1:
  ans=1
  for si in sp:
    ans*=si
    ans%=MOD
  print(ans)
  exit()
else:
  for i in range(k,n):
    if a[i]>0:
      sn[negidx]=a[i]
      ans1=1
      for si in sn:
        ans1*=si
        ans1%=MOD
      break
  for i in range(k,n):
    if a[i]<0:
      sp[posidx]=a[i]
      ans2=1
      for si in sp:
        ans2*=si
        ans2%=MOD        
      break
  print(max(ans1,ans2))

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

if n==k:
  ans=1
  for i in range(k):ans=ans*a[i]%MOD
  print(ans)
  exit()

p=[]
m=[]
for q in a:
  if q>=0:p+=q,
  else:m+=q,

if p==[] and k%2==1:
  m.sort(reverse=True)
  ans=1
  for i in range(k):ans=ans*m[i]%MOD
  print(ans)
  exit()

a.sort(key=lambda x:abs(x),reverse=True)

s=[]
flg=1
for i in range(k):
  s.append(a[i])
  if a[i]<0:
    flg*=-1
if flg==-1:
  tmp=s.pop()
  if -tmp in s:
    s.append(a[k])
    break
  for i in range(k,n):
    if a[i]*tmp<=0:
      s.append(a[i])
      break
ans=1
for i in s:
  ans=ans*i%MOD
print(ans)

Buggy submission (verdict: Runtime Error):
# import time
# t1=time.time()
def LI():return list(map(int,input().split()))
def I(): return int(input())
from collections import deque, Counter, defaultdict
# from heapq import heappop, heappush
# from pprint import pprint
# import numpy as np
# import itertools

MOD=10**9+7

n,k=LI()
a=LI()
if k==1:
    a.sort()
    print(a[-1]%MOD)
    exit()
if n==k:
    ans=1
    for i in range(k):
        ans=ans*a[i]%MOD
    print(ans)
    exit()
    
# minus=[]
# plus=[]
# for q in a:
#     if q>=0:
#         plus.append(q)
#     else:
#         minus.append(q)
# if len(plus)==n:
#     plus.sort(reverse=True)
#     ans=1
#     for i in range(k):
#         ans=ans*plus[i]%MOD
#     print(ans)
#     exit()
    
# if len(minus)==n:
#     if k%2==0:
#         minus.sort()
#         ans=1
#         for i in range(k):
#             ans=ans*minus[i]%MOD
#         print(ans)
#         exit()
#     else:
#         minus.sort(reverse=True)
#         ans=1
#         for i in range(k):
#             ans=ans*minus[i]%MOD
#         print(ans)
#         exit()
      
# if k%2==0:
#     if len(minus)==1:
#         plus.sort(reverse=True)
#         ans=1
#         for i in range(k):
#             ans=ans*plus[i]%MOD
#         print(ans)
#         exit()
#     if len(minus)>=2:
#         minus.sort()
#         minus=deque(minus)
#         while True:
#             if len(minus)<=1:
#                 break
#             if k==1:
#                 break
#             a=minus.popleft()
#             b=minus.popleft()
#             plus.append(a*b)
#             k-=1
            
#     plus.sort(reverse=True)
#     ans=1
#     for i in range(k):
#         ans=ans*plus[i]%MOD
#     print(ans)
#     exit()

# if k%2==1:
#     if len(minus)==1:
#         if k<=len(plus):
#             ans=1
#             plus.sort(reverse=True)
#             for i in range(k):
#                 ans=ans*plus[i]%MOD
#             print(ans)
#             exit()
#         else:
#             ans=1
#             ans=ans*minus[0]
#             plus.sort()
#             for i in range(k-1):
#                 ans=ans*plus[i]%MOD
#             print(ans)
#             exit()
#     else:
#         minus.sort()
#         minus=deque(minus)
#         while True:
#             if len(minus)<=1:
#                 break
#             if k==1:
#                 break
#             a=minus.popleft()
#             b=minus.popleft()
#             plus.append(a*b)
#             k-=1
            
#     plus.sort(reverse=True)
#     ans=1
#     for i in range(k):
#         ans=ans*plus[i]%MOD
#     print(ans)
#     exit()
    
    
for q in a:
    b.append(q%MOD)
b.sort(reverse=True)

ans=1
for i in range(k):
    ans=ans*b[i]%MOD
print(ans)

Buggy submission (verdict: Runtime Error):
# import time
# t1=time.time()
def LI():return list(map(int,input().split()))
def I(): return int(input())
from collections import deque, Counter, defaultdict
# from heapq import heappop, heappush
# from pprint import pprint
# import numpy as np
# import itertools

MOD=10**9+7

n,k=LI()
a=LI()
if k==1:
    a.sort()
    print(a[-1]%MOD)
    exit()
if n==k:
    ans=1
    for i in range(k):
        ans=ans*a[i]%MOD
    print(ans)
    exit()
    
# minus=[]
# plus=[]
# for q in a:
#     if q>=0:
#         plus.append(q)
#     else:
#         minus.append(q)
# if len(plus)==n:
#     plus.sort(reverse=True)
#     ans=1
#     for i in range(k):
#         ans=ans*plus[i]%MOD
#     print(ans)
#     exit()
    
# if len(minus)==n:
#     if k%2==0:
#         minus.sort()
#         ans=1
#         for i in range(k):
#             ans=ans*minus[i]%MOD
#         print(ans)
#         exit()
#     else:
#         minus.sort(reverse=True)
#         ans=1
#         for i in range(k):
#             ans=ans*minus[i]%MOD
#         print(ans)
#         exit()
      
# if k%2==0:
#     if len(minus)==1:
#         plus.sort(reverse=True)
#         ans=1
#         for i in range(k):
#             ans=ans*plus[i]%MOD
#         print(ans)
#         exit()
#     if len(minus)>=2:
#         minus.sort()
#         minus=deque(minus)
#         while True:
#             if len(minus)<=1:
#                 break
#             if k==1:
#                 break
#             a=minus.popleft()
#             b=minus.popleft()
#             plus.append(a*b)
#             k-=1
            
#     plus.sort(reverse=True)
#     ans=1
#     for i in range(k):
#         ans=ans*plus[i]%MOD
#     print(ans)
#     exit()

# if k%2==1:
#     if len(minus)==1:
#         if k<=len(plus):
#             ans=1
#             plus.sort(reverse=True)
#             for i in range(k):
#                 ans=ans*plus[i]%MOD
#             print(ans)
#             exit()
#         else:
#             ans=1
#             ans=ans*minus[0]
#             plus.sort()
#             for i in range(k-1):
#                 ans=ans*plus[i]%MOD
#             print(ans)
#             exit()
#     else:
#         minus.sort()
#         minus=deque(minus)
#         while True:
#             if len(minus)<=1:
#                 break
#             if k==1:
#                 break
#             a=minus.popleft()
#             b=minus.popleft()
#             plus.append(a*b)
#             k-=1
            
#     plus.sort(reverse=True)
#     ans=1
#     for i in range(k):
#         ans=ans*plus[i]%MOD
#     print(ans)
#     exit()
    
    
for q in a:
    b.append(q%MOD)
b.sort(reverse=True)
print(b)
ans=1
for i in range(k):
    ans=ans*b[i]%MOD
print(ans)