Problem:


Score : 500 points

Problem Statement

Given are N integers A_1,\ldots,A_N.

We will choose exactly K of these elements. Find the maximum possible product of the chosen elements.

Then, print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Constraints

1 \leq K \leq N \leq 2\times 10^5

|A_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N K
A_1 \ldots A_N

Output

Print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Sample Input 1

4 2
1 2 -3 -4

Sample Output 1

12

The possible products of the two chosen elements are 2, -3, -4, -6, -8, and 12, so the maximum product is 12.

Sample Input 2

4 3
-1 -2 -3 -4

Sample Output 2

1000000001

The possible products of the three chosen elements are -24, -12, -8, and -6, so the maximum product is -6.

We print this value modulo (10^9+7), that is, 1000000001.

Sample Input 3

2 1
-1 1000000000

Sample Output 3

1000000000

The possible products of the one chosen element are -1 and 1000000000, so the maximum product is 1000000000.

Sample Input 4

10 10
1000000000 100000000 10000000 1000000 100000 10000 1000 100 10 1

Sample Output 4

999983200

Be sure to print the product modulo (10^9+7).



Sample input:
4 2
1 2 -3 -4


Sample output:
12

Submission status: Runtime Error

Solution code:
import sys
import functools
import operator
import numpy as np
input = sys.stdin.readline
#-------------
n,k = map(int,input().split())
a = list(map(int,input().split()))
#-------------
b = np.abs(a)
b = sorted(a,reverse = True)
b_k = b[k-1] 
a_new = [a[_] for _ in range(len(a)) if np.abs(a)[_] >= b_k]
a_new.sort()
D = [[a_new[i+j-k] for j in range(k)] for i in range(len(a_new))]
A = (functools.reduce(operator.mul,D[_]) for _ in range(len(D)))
l_max = max(A)
waru = 10**9 + 7
print(l_max % waru)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
m,p,z=[],[],0
for a in A:
    if a<0:
        m.append(a)
    elif a>0:
        p.append(a)
    else:
        z+=1
m.sort()
p.sort()
mod=10**9+7
from functools import reduce
if (len(p) or K%2==0) and K<len(m)+len(p) or K==len(m)+len(p) and len(m)%2==0:
    r=1
    i,j=0,0
    while i+j<K:
        if i<len(m)-1 and i+j<K-1 and m[i]*m[i+1]>p[-j-1]*p[-j-2]:
            r=r*m[i]*m[i+1]%mod
            i+=2
        else:
            r=r*p[-j-1]%mod
            j+=1
    print(r)
elif z:
    print(0)
else:
    print(reduce(lambda x,y:x*y%mod,(m+p)[-K:]))

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
m,p,z=[],[],0
for a in A:
    if a<0:
        m.append(a)
    elif a>0:
        p.append(a)
    else:
        z+=1
m.sort()
p.sort()
mod=10**9+7
from functools import reduce
if len(p) and (K<len(m)+len(p) or K==len(m)+len(p) and len(m)%2==0):
    r=1
    i,j=0,0
    while i+j<K:
        if i<len(m)-1 and m[i]*m[i+1]>p[-j-1]*p[-j-2]:
            r=r*m[i]*m[i+1]%mod
            i+=2
        else:
            r=r*p[-j-1]%mod
            j+=1
    print(r)
elif z:
    print(0)
else:
    print(reduce(lambda x,y:x*y%mod,(m+p)[-K:]))

Submission status: Runtime Error

Solution code:
import itertools
import heapq
import sys
import functools
import operator
import numpy as np
input = sys.stdin.readline
#-------------
n,k = map(int,input().split())
a = list(map(int,input().split()))
#-------------
b = np.abs(a)
b = sorted(a,reverse = True)
b_k = b[k-1] 
a_new = [a[_] for _ in range(len(a)) if np.abs(a)[_] >= b_k]
a_new.sort()
D = [[a_new[i+j-k] for j in range(k)] for i in range(10)]
A = (functools.reduce(operator.mul,D[_]) for _ in range(len(D)))
l_max = max(A)
waru = 10**9 + 7
print(l_max % waru)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
nums = list(map(int, input().split()))
p = []
m = []

for i in nums:
    if i >= 0:
        p.append(i)
    else:
        m.append(i)

p.sort()
m.sort(reverse = True)

ans = 1
MOD = 1e9 + 7

if n == k:
    for i in nums:
        ans = ans * i % MOD
    print(int(ans))
    exit()

if not p and k % 2 == 1:
    for i in range(k):
        ans = ans * m[i] % MOD
    print(int(ans))
    exit()

ps = []
ms = []
cnt = 0
for i in range (k):
    if not p:
        ms.append(m.pop())
        continue
    if not m:
        ps.append(p.pop())
        cnt += 1
        continue
    
    if p[-1] >= abs(m[-1]):
        ps.append(p.pop())
    else:
        ms.append(m.pop())
        cnt += 1

if cnt % 2 == 1:
    if not ps or not ms or not p or not m:  
        if not ms or not p:
            ps.pop()
            ms.append(m.pop())
        if not ps or not m:
            ms.pop()
            ps.append(p.pop())
    elif m[-1] / ps[-1] >= p[-1] / ms[-1]:
        ps.pop()
        ms.append(m.pop())
    else:
        ms.pop()
        ps.append(p.pop())

for i in ps:
    ans = ans * i % MOD
for i in ms:
    ans = ans * i % MOD

print(int(ans))

Submission status: Runtime Error

Solution code:

n,k=map(int,input().split())
a=list(map(int,input().split()))
mod=10**9+7
m=[]
p=[]

for i in range(n):
    if a[i]<0:
        m.append(a[i])
    else:
        p.append(a[i])
m.sort(reverse=True)
p.sort()
ans=1

if len(m)==n and k%2==1:
    for i in range(k):
        ans*=m[i]
        ans%=mod
elif n==k:
    for i in range(k):
        ans*=a[i]
        ans%=mod
else:
    cnt=0
    for i in range(k):
        if len(m)==0:
            lp=p.pop()
            ans*=lp

        elif len(p)==0:
            lm=m.pop()
            ans*=lm
            cnt+=1

        else:
            if abs(m[-1])<p[-1]:
                lp=p.pop()
                ans*=lp
            else:
                lm=m.pop()
                ans*=lm
                cnt+=1
        ans%=mod 

    if cnt%2!=0:
        if len(p)>0 and len(m)>0:
            ans*=max(pow(lm,mod-2,mod)*p[-1], pow(lp,mod-2,mod)*m[-1])
        elif len(p)<1:
            ans*=pow(lp,mod-2,mod)*m[-1]
        else:
            ans*=pow(lm,mod-2,mod)*p[-1]

print(ans%mod)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
nums = list(map(int, input().split()))
p = []
m = []

for i in nums:
    if i >= 0:
        p.append(i)
    else:
        m.append(i)

p.sort()
m.sort(reverse = True)

ans = 1
MOD = 1e9 + 7

if n == k:
    for i in nums:
        ans = ans * i % MOD
    print(int(ans))
    exit()

if not p and k % 2 == 1:
    for i in range(k):
        ans = ans * m[i] % MOD
    print(int(ans))
    exit()

ps = []
ms = []
cnt = 0
for i in range (k):
    if not p:
        ms.append(m.pop())
        continue
    if not m:
        ps.append(p.pop())
        cnt += 1
        continue
    
    if p[-1] >= abs(m[-1]):
        ps.append(p.pop())
    else:
        ms.append(m.pop())
        cnt += 1

if cnt % 2 == 1:
    if not p or m[-1] / ps[-1] >= p[-1] / ms[-1]:
        ps.pop()
        ms.append(m.pop())
    if not m or m[-1] / ps[-1] < p[-1] / ms[-1]:
        ms.pop()
        ps.append(p.pop())

for i in ps:
    ans = ans * i % MOD
for i in ms:
    ans = ans * i % MOD

print(int(ans))

Submission status: Runtime Error

Solution code:

n,k=map(int,input().split())
a=list(map(int,input().split()))
mod=10**9+7
m=[]
p=[]

for i in range(n):
    if a[i]<0:
        m.append(a[i])
    else:
        p.append(a[i])
m.sort(reverse=True)
p.sort()
ans=1

if len(m)==n and k%2==1:
    for i in range(k):
        ans*=m[i]
        ans%=mod
else:
    cnt=0
    for i in range(k):
        if len(m)==0:
            lp=p.pop()
            ans*=lp

        elif len(p)==0:
            lm=m.pop()
            ans*=lm
            cnt+=1

        else:
            if abs(m[-1])<p[-1]:
                lp=p.pop()
                ans*=lp
            else:
                lm=m.pop()
                ans*=lm
                cnt+=1
        ans%=mod 

    if cnt%2!=0:
        if len(p)>0 and len(m)>0:
            ans*=max(pow(lm,mod-2,mod)*p[-1], pow(lp,mod-2,mod)*m[-1])
        elif len(p)<1:
            ans*=pow(lp,mod-2,mod)*m[-1]
        else:
            ans*=pow(lm,mod-2,mod)*p[-1]

print(ans%mod)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
nums = list(map(int, input().split()))
p = []
m = []

for i in nums:
    if i >= 0:
        p.append(i)
    else:
        m.append(i)

p.sort()
m.sort(reverse = True)

ans = 1
MOD = 1e9 + 7

if n == k:
    for i in nums:
        ans = ans * i % MOD
    print(ans)
    exit()

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

ps = []
ms = []
cnt = 0
for i in range (k):
    if not p:
        ms.append(m.pop())
        continue
    if not m:
        ps.append(p.pop())
        cnt += 1
        continue
    
    if p[-1] >= abs(m[-1]):
        ps.append(p.pop())
    else:
        ms.append(m.pop())
        cnt += 1

if cnt % 2 == 1:
    if not p or m[-1] / ps[-1] >= p[-1] / ms[-1]:
        ps.pop()
        ms.append(m.pop())
    if not m or m[-1] / ps[-1] < p[-1] / ms[-1]:
        ms.pop()
        ps.append(p.pop())

for i in ps:
    ans = ans * i % MOD
for i in ms:
    ans = ans * i % MOD

print(ans)

Submission status: Runtime Error

Solution code:

n,k=map(int,input().split())
a=list(map(int,input().split()))
mod=10**9+7
m=[]
p=[]

for i in range(n):
    if a[i]<0:
        m.append(a[i])
    else:
        p.append(a[i])
m.sort(reverse=True)
p.sort()
ans=1

if len(m)==n and k%2==1:
    for i in range(k):
        ans*=m[i]
        ans%=mod
elif k==1:
    ans=p[-1]
else:
    cnt=0
    for i in range(k-2):
        if len(m)==0:
            lp=p.pop()
            ans*=lp

        elif len(p)==0:
            lm=m.pop()
            ans*=lm
            cnt+=1

        else:
            if abs(m[-1])<p[-1]:
                lp=p.pop()
                ans*=lp
            else:
                lm=m.pop()
                ans*=lm
                cnt+=1
        ans%=mod 

    if cnt%2==0:
        if len(p)>1 and len(m)>1:
            ans*=max(p[-1]*p[-2],m[-1]*m[-2])
        elif len(p)<2:
            ans*=m[-1]*m[-2]
        else:
            ans*=p[-1]*p[-2]

    elif len(m)>0:
        ans*=p[-1]*m[-1]

    else:
        if len(p)>1 and len(m)>1:
            ans*=max(p[-1]*m[-1],pow(lm,mod-2,mod)*p[-1]*p[-2], pow(lp,mod-2,mod)*m[-1]*m[-2])
        elif len(p)<1:
            ans*=pow(lp,mod-2,mod)*m[-1]*m[-2]
        else:
            ans*=pow(lm,mod-2,mod)*p[-1]*p[-2]
print(ans%mod)

Submission status: Runtime Error

Solution code:

n,k=map(int,input().split())
a=list(map(int,input().split()))
mod=10**9+7
m=[]
p=[]

for i in range(n):
    if a[i]<0:
        m.append(a[i])
    else:
        p.append(a[i])
m.sort(reverse=True)
p.sort()
ans=1

if len(m)==n and k%2==1:
    for i in range(k):
        ans*=m[i]
        ans%=mod
elif k==1:
    ans=p[-1]
else:
    cnt=0
    for i in range(k-2):
        if len(m)==0:
            lp=p.pop()
            ans*=lp

        elif len(p)==0:
            lm=m.pop()
            ans*=lm
            cnt+=1

        else:
            if abs(m[-1])<p[-1]:
                lp=p.pop()
                ans*=lp
            else:
                lm=m.pop()
                ans*=lm
                cnt+=1
        ans%=mod 

    if cnt%2==0:
        if len(p)>1 and len(m)>1:
            ans*=max(p[-1]*p[-2],m[-1]*m[-2])
        elif len(p)<2:
            ans*=m[-1]*m[-2]
        else:
            ans*=p[-1]*p[-2]

    elif len(m)>0:
        ans*=p[-1]*m[-1]

    else:
        ans=ans*pow(lm,mod-2,mod)*p[-1]*p[-2]  

print(ans%mod)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a_list = list(map(int, input().split()))

l_abs = sorted(a_list, key=abs, reverse=True)

_min = False
if len(sorted([i for i in a_list if i < 0])) == n and k % 2 == 1:
  _min = True
  # 最悪
cnt = 0
ans = 1

if k == n:
  for i in a_list:
    ans *= i
elif _min is True:
  l_minus = sorted([i for i in a_list if i < 0], reverse=True)
  for i in range(k):
    ans *= l_minus[i]
else:
  ans = 1
  l_used = l_abs[:k]
  l_unused = l_abs[k:]
  for i in range(k):
    ans *= l_abs[i]
  if ans < 0:
    l_minus = sorted([i for i in l_unused if i <= 0])
    l_plus =  sorted([i for i in l_unused if i > 0],reverse=True)
    if len(l_minus) == 0:
      l_used_minus_min = min(l_used)
      if l_used_minus_min < 0:
        ans = ans * l_plus[0] / l_used_minus_min
    elif len(l_plus) == 0:
      l_used_plus = sorted([i for i in l_used if i > 0])
      if len(l_used_plus) != 0:
        ans = ans * l_minus[0] / l_used_plus[0]
    else:
      a = abs(l_minus[0] * sorted([i for i in l_used if i > 0])[0])
      b = abs(l_plus[0] * min(l_used))
      if a > b:
        ans = ans * l_minus[0] / sorted([i for i in l_used if i > 0])[0]
      else:
        ans = ans * l_plus[0] / min(l_used)
      
print(ans % (pow(10,9) + 7))

Submission status: Runtime Error

Solution code:
import collections

N,K = list(map(int, input().split()))
As = list(map(int, input().split()))
As.sort()


#N=Kで選択肢ないとき
if(N == K):
    ans = 1
    for A in As:
        ans *= A
    print(ans % (10**9 + 7))
    exit()

#ansが負となる時
if(max(As) < 0 and K % 2 == 1):
    ans = 1
    for A in As[(N-K):]:
        ans *= A
    
    print(ans %(10**9 + 7))
    exit()

#Nの値が3以下の時
if(N < 4):
    if(K == 1):
        print(max(As) % (10**9+7))
        exit()
    
    elif(N == 3 and K == 2):
        ans = max(As[0] * As[1], As[1] * As[2])
        print(ans % 10 ** 9 + 7)
        exit()

#王道ルート
As_que = collections.deque(As)
#print(As_que)

ans = 1
if(K % 2 == 1):
    ans *= As_que.pop()

left_pop = As_que.popleft() * As_que.popleft()
right_pop = As_que.pop() * As_que.pop()

#rint(left_pop,right_pop)
for _ in range((N // 2 - 1)):
    if(right_pop >= left_pop):
        ans *= right_pop
        right_pop = As_que.pop() * As_que.pop()
    
    else:
        ans *= left_pop
        left_pop = As_que.popleft() * As_que.popleft()

print(ans %(10**9 + 7))

Submission status: Runtime Error

Solution code:
# 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)

Submission status: Runtime Error

Solution code:
import math

n, k = map(int, input().split(' '))
l = map(int, input().split(' '))
pos = []
neg = []


for i in l:
	if i >= 0:
		pos.append(i)
	else:
		neg.append(i)

p = 0
n = 0
pos = sorted(pos)[::-1]
neg = sorted(neg)

answer = []
placeholder = -1

if k % 2 == 1:
	if len(pos) > 0:
		placeholder = pos.pop()
	else:
		placeholder = -2
		neg = neg[::-1]
	k -= 1

pos.append(0)
pos.append(0)
neg.append(0)
neg.append(0)

for i in range(k//2):
	if pos[p+0] * pos[p+1] > neg[n+0] * neg[n+1]:
		answer.append(pos[p+0])
		answer.append(pos[p+1])
		p += 2
	else:
		answer.append(neg[n+0])
		answer.append(neg[n+1])
		n += 2

if placeholder == -2:
	answer.append(neg[n])
elif placeholder != -1:
	if pos[p] > n:
		placeholder = pos[p]
	answer.append(placeholder)

s = 1
for i in answer:
	s *= i
print(s % 1000000007)

Submission status: Runtime Error

Solution code:
import bisect
N,K=map(int,input().split())
A=list(map(int,input().split()))
Plis=[]
Nlis=[]
for i in range(N):
  if A[i] >=0:
    bisect.insort(Plis,A[i])
  else:
    bisect.insort(Nlis,A[i])
Plis.reverse()

pn=len(Plis)
nn=N-pn

Ppro=[1]
Npro=[1]

for i in range(pn):
  Ppro+=[Ppro[i]*Plis[i]]
  
for j in range(nn):
  Npro+=[Npro[j]*Nlis[j]]


if N==K:
  print(Npro[-1]*Ppro[-1]%(10**9+7))
elif pn==0 and K%2==1:
  ans=int(Npro[-1]/Npro[-1-K])%(10**9+7)
  print(ans)
else:
  ans=0
  maxn = min(K,nn)
  maxn = (maxn//2)*2  
  for i in range(maxn//2):
    if Npro[2*i]*Ppro[K-2*i] >ans:
      ans =Npro[2*i]*Ppro[K-2*i]
  ans = ans%(10**9+7)
  print(ans)

Submission status: Runtime Error

Solution code:
#x:配列
mod=10**9+7
def multarray(x):
    ret=1
    for i in x:
        ret*=i
        ret%=mod
    return ret

n,k=map(int,input().split())
a=list(map(int,input().split()))

#n==kの場合はかけるだけ
if n==k:
    print(multarray(a))
    exit()

#0以上の数と負の数に分けて格納
ap,am=[],[]
for i in range(n):
    if a[i]>=0:
        ap.append(a[i])
    else:
        am.append(a[i])
#それぞれ絶対値の順番にソート
ap.sort(reverse=True)
am.sort()

#0以上の数のみか正の数のみの場合は先に場合分け
if len(am)==0:
    print(multarray(ap[:k]))
    exit()
if len(ap)==0:
    if k%2==0:
        print(multarray(am[:k]))
    else:
        print(multarray(am[::-1][:k]))
    exit()

#組にして格納する(0以上か負かを記録しておく)
apm2=[]
for i in range(len(am)//2):
    apm2.append((am[2*i]*am[2*i+1],0))
for i in range(len(ap)//2):
    apm2.append((ap[2*i]*ap[2*i+1],1))
apm2.sort(reverse=True)

#Kが偶数の時
if k%2==0:
    print(multarray(apm2[:k//2]))
    exit()

#Kが奇数の時
#0以上の数で次にどこを見るのかをチェックしておく
p=0
ans=[]
for i in range(k//2):
    p+=(2*apm2[i][1])
    ans.append(apm2[i][0])

ans_cand=[]
#一個増やす場合
if p!=len(ap):
    ans_cand.append(multarray(ans)*ap[p]%mod)
#一個減らして二個増やす場合
if p!=0:
    #減らすindex
    check_i=ans.index(ap[p-1]*ap[p-2])
    #ap[p-1]を減らす操作(0除算を避ける)
    ans[check_i]=ap[p-2]
    ans.append(apm2[k//2][0])
    ans_cand.append(multarray(ans))
print(max(ans_cand))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
MOD = 10 ** 9 + 7
if max(a) < 0 and k % 2:
    a.sort()
    ans = a[-1]
    for i in range(-2, -n, -1):
        ans *= a[i]
        ans %= MOD
elif max(a) < 0 and k % 2 == 0:
    a.sort()
    ans = a[0]
    for i in range(1, k):
        ans *= a[i]
        ans %= MOD
else:
    a.sort(key=lambda x: abs(x), reverse=True)
    ans = a[0]
    minus = True if ans < 0 else False
    for i in range(1, k):
        ans *= a[i]
        ans %= MOD
        if a[i] < 0:
            minus = not minus
    if minus:
        zzz = a[k - 1]
        ans //= zzz
        if zzz >= 0:
            for i in range(k, n):
                if a[i] < 0:
                    ans *= a[i]
                    ans %= MOD
                    break
        else:
            for i in range(k, n):
                if a[i] >= 0:
                    ans *= a[i]
                    ans %= MOD
                    break

print(ans % MOD)

Submission status: Runtime Error

Solution code:
import numpy as np
n, k = map(int, input().split())
A = tuple(map(int, input().split()))
A = np.array(A)
mod = 10**9+7
cz = np.sum(np.where(A == 0, 1, 0))
ap = np.sort(A[np.where(A > 0, True, False)])[::-1]
am = np.sort(A[np.where(A < 0, True, False)])

if n == k:
    ans = 1
    for a in A:
        ans *= a
        ans %= mod
    print(ans)

elif n == len(am) + cz:
    # 全てが負の場合
    if k%2 == 1:
        # kが奇数の場合積は負、絶対値が小さい方から選ぶ
        if cz > 0:
            print(0)
        else:
            am = np.sort(am)[::-1]
            ans = am[:k].prod()
            print(ans%mod)
    else:
        if cz > n-k:
            print(0)
        else:
            # kが偶数の場合積は正、絶対値が大きい方から選ぶ
            am = np.sort(am)
            ans = am[:k].prod()
            print(ans%mod)
else:
    # kが奇数の場合、積は奇数個の正と偶数個の負で構成される
    # kが偶数の場合、積は偶数個の正と偶数個の負で構成される
    # kが奇数の場合は、必ず最大の正の値は積に含まれる
    # kが偶数の場合と同じ形にすることができる
    if k%2 == 1:
        k -= 1
        ans = ap[0]
        ap = ap[1:]
    else:
        ans = 1

    from collections import deque
    ap2 = deque([])
    am2 = deque([])
    for i in range(0, len(ap) - (len(ap)%2==1), 2):
        ap2.append(ap[i]*ap[i+1])
    for i in range(0, len(am) - (len(am)%2==1), 2):
        am2.append(am[i]*am[i+1])

    pidx = 0
    midx = 0
    while k > 0:
        if len(am2) and len(ap2):
            if ap2[0] >= am2[0]:
                ans *= ap2.pop()
            else:
                ans *= am2.pop()
        else:
            if len(am2) == 0:
                ans *= ap2.pop()
            else:
                ans *= am2.pop()
        ans %= mod
        k -= 2
    print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
n, k = map(int, input().split())
A = tuple(map(int, input().split()))
A = np.array(A)
mod = 10**9+7
cz = np.sum(np.where(A == 0, 1, 0))
ap = np.sort(A[np.where(A > 0, True, False)])[::-1]
am = np.sort(A[np.where(A < 0, True, False)])

if n == k:
    ans = 1
    for a in A:
        ans *= a
        ans %= mod
    print(ans)

elif n == len(am) + cz:
    # 全てが負の場合
    if k%2 == 1:
        # kが奇数の場合積は負、絶対値が小さい方から選ぶ
        if cz > 0:
            print(0)
        else:
            am = np.sort(am)[::-1]
            ans = am[:k].prod()
            print(ans%mod)
    else:
        # kが偶数の場合積は正、絶対値が大きい方から選ぶ
        am = np.sort(am)
        ans = am[:k].prod()
        print(ans%mod)
else:
    # kが奇数の場合、積は奇数個の正と偶数個の負で構成される
    # kが偶数の場合、積は偶数個の正と偶数個の負で構成される
    # kが奇数の場合は、必ず最大の正の値は積に含まれる
    # kが偶数の場合と同じ形にすることができる
    if k%2 == 1:
        k -= 1
        ans = ap[0]
        ap = ap[1:]
    else:
        ans = 1

    from collections import deque
    ap2 = deque([])
    am2 = deque([])
    for i in range(0, len(ap) - (len(ap)%2==1), 2):
        ap2.append(ap[i]*ap[i+1])
    for i in range(0, len(am) - (len(am)%2==1), 2):
        am2.append(am[i]*am[i+1])

    pidx = 0
    midx = 0
    while k > 0:
        if len(am2) and len(ap2):
            if ap2[0] >= am2[0]:
                ans *= ap2.pop()
            else:
                ans *= am2.pop()
        else:
            if len(am2) == 0:
                ans *= ap2.pop()
            else:
                ans *= am2.pop()
        ans %= mod
        k -= 2
    print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
n, k = map(int, input().split())
A = tuple(map(int, input().split()))
A = np.array(A)
mod = 10**9+7
cz = np.sum(np.where(A == 0, 1, 0))
ap = np.sort(A[np.where(A > 0, True, False)])[::-1]
am = np.sort(A[np.where(A < 0, True, False)])

if n == k:
    ans = 1
    for a in A:
        ans *= a
        ans %= mod
    print(ans)

elif n == len(am) + cz:
    # 全てが負の場合
    if k%2 == 1:
        # kが奇数の場合積は負、絶対値が小さい方から選ぶ
        if cz > 0:
            print(0)
        else:
            am = np.sort(am)[::-1]
            ans = am[:k].prod()
            print(ans%mod)
    else:
        # kが偶数の場合積は正、絶対値が大きい方から選ぶ
        am = np.sort(am)
        ans = am[:k].prod()
        print(ans%mod)
else:
    # kが奇数の場合、積は奇数個の正と偶数個の負で構成される
    # kが偶数の場合、積は偶数個の正と偶数個の負で構成される
    # kが奇数の場合は、必ず最大の正の値は積に含まれる
    # kが偶数の場合と同じ形にすることができる
    if k%2 == 1:
        k -= 1
        ans = ap[0]
        ap = ap[1:]
    else:
        ans = 1

    pidx = 0
    midx = 0
    while k > 0:

        if len(ap) - pidx > 1 and ap[pidx] * ap[pidx+1] >= am[midx] * am[midx]:
            ans *= (ap[pidx] * ap[pidx+1])
            pidx += 2
        else:
            if len(am) - midx > 1:
                ans *= (am[midx] * am[midx+1])
                midx += 2
            else:
                ans *= (ap[pidx] * ap[pidx+1])
                pidx += 2
        ans %= mod
        k -= 2
    print(ans)

Submission status: Runtime Error

Solution code:
import itertools
import sys
import functools
import operator
import numpy as np
input = sys.stdin.readline
#-------------
n,k = map(int,input().split())
a = list(map(int,input().split()))
#-------------
b = np.abs(a)
b.sort(reverse = True)
b_k = b[k-1] 
a_new = [a[i] for i in range(len(a)) if np.abs(a)[i] >= b_k]
 
comb = itertools.combinations(a_new,k)  
l = [list(_) for _ in comb]  
A = (functools.reduce(operator.mul,l[_]) for _ in range(len(l)))
l_max = max(A)
waru = 10**9 + 7
print(l_max % waru)

Submission status: Runtime Error

Solution code:
import itertools
import heapq
import sys
import functools
import operator
import numpy as np
input = sys.stdin.readline
#-------------
n,k = map(int,input().split())
a = list(map(int,input().split()))
#-------------
b = np.abs(a)
b = sorted(a,reverse = True)
b_k = b[k-1] 
a_new = [a[i] for i in a if np.abs(a)[i] >= b_k]
 
comb = itertools.combinations(a_new,k)  
l = [list(_) for _ in comb]  
A = (functools.reduce(operator.mul,l[_]) for _ in range(len(l)))
l_max = max(A)
waru = 10**9 + 7
print(l_max % waru)

Submission status: Runtime Error

Solution code:
import itertools
import heapq
import sys
import functools
import operator
import numpy as np
input = sys.stdin.readline
#-------------
n,k = map(int,input().split())
a = list(map(int,input().split()))
#-------------
b = np.abs(a)
b.sort(reverse = True)
b_k = b[k-1] 
a_new = [a[i] for i in a if np.abs(a)[i] >= b_k]
 
comb = itertools.combinations(a_new,k)  
l = [list(_) for _ in comb]  
A = (functools.reduce(operator.mul,l[_]) for _ in range(len(l)))
l_max = max(A)
waru = 10**9 + 7
print(l_max % waru)

Submission status: Runtime Error

Solution code:
import numpy as np
n, k = map(int, input().split())
A = tuple(map(int, input().split()))
A = np.array(A)
mod = 10**9+7
cz = np.sum(np.where(A == 0, 1, 0))
ap = np.sort(A[np.where(A > 0, True, False)])[::-1]
am = np.sort(A[np.where(A < 0, True, False)])

if n == k:
    ans = 1
    for a in A:
        ans *= a
        ans %= mod
    print(ans)

elif n == len(am) + cz:
    # 全てが負の場合
    if k%2 == 1:
        # kが奇数の場合積は負、絶対値が小さい方から選ぶ
        if cz > 0:
            print(0)
        else:
            am = np.sort(am)[::-1]
            ans = am[:k].prod()
            print(ans%mod)
    else:
        # kが偶数の場合積は正、絶対値が大きい方から選ぶ
        am = np.sort(am)
        ans = am[:k].prod()
        print(ans%mod)
else:
    # kが奇数の場合、積は奇数個の正と偶数個の負で構成される
    # kが偶数の場合、積は偶数個の正と偶数個の負で構成される
    # kが奇数の場合は、必ず最大の正の値は積に含まれる
    # kが偶数の場合と同じ形にすることができる
    if k%2 == 1:
        k -= 1
        ans = ap[0]
        ap = ap[1:]
    else:
        ans = 1

    k //= 2
    ap2 = []
    for i in range(0, len(ap) - (len(ap)%2 == 1), 2):
        ap2.append(ap[i]*ap[i+1])
    am2 = []
    for j in range(0, len(am) - (len(am)%2 == 1), 2):
        am2.append(am[j]*am[j+1])

    # 正の積
    for i in range(0, min(k, len(ap2))):
        ans *= ap2[i]
        ans %= mod

    # 負の積
    for k in range(0, max(0, k-len(ap2))):
        ans *= am2[k]
        ans %= mod

    mans = ans
    pidx = min(k, len(ap))-1
    midx = max(0, k-len(ap2))
    if len(ap2) and len(am2):
        while pidx >= 0 and midx < len(am2):
            mans *= am2[midx]
            mans //= ap2[pidx]
            mans %= mod
            pidx -= 1
            midx += 1
            ans = max(mans, ans)
    print(ans)

Submission status: Runtime Error

Solution code:
import itertools
import heapq
import sys
import functools
import operator
import numpy as np
input = sys.stdin.readline
#-------------
n,k = map(int,input().split())
a = list(map(int,input().split()))
#-------------
b = np.abs(a)
b = sorted(a,reverse = True)
b_k = b[k-1] 
a_new = [a[i] for i in range(len(a)) if np.abs(a)[i] > b_k]
 
comb = itertools.combinations(a_new,k)  
l = [list(_) for _ in comb]  
A = (functools.reduce(operator.mul,l[_]) for _ in range(len(l)))
l_max = max(A)
waru = 10**9 + 7
print(l_max % waru)

Submission status: Runtime Error

Solution code:
mod=10**9+7
n,k=map(int,input().split())
arr=list(map(int,input().split()))
cnt_plus=0
cnt_zero=0
cnt_minus=0
arr_plus=[]
arr_minus=[]
for i in range(n):
 if arr[i]>0:
   cnt_plus+=1
   arr_plus.append(arr[i])
 elif arr[i]<0:
   cnt_minus+=1
   arr_minus.append(-arr[i])
 else:
   cnt_zero+=1
if cnt_plus+cnt_minus<k: #K個の積の中に0を含む場合、答えは0
 print(0)
 exit()
if k==n: #K=Nの場合
 if cnt_zero!=0: #0がひとつでもあれば答えは0
   print(0)
 else:
   ans=1
   for i in range(cnt_plus):
     ans*=arr_plus[i]
     ans%=mod
   for i in range(cnt_minus):
     ans*=arr_minus[i]
     ans%=mod
   if cnt_minus%2==0: #負の数が偶数個であれば積は正
     print(ans)
   else: #そうでなければ積は負となる
     print((-ans)%mod)
 exit()
if cnt_plus==0 and k%2==1: #正の数がないとき
 if cnt_zero!=0: #0がひとつでもあれば答えは0にできる
   print(0)
 else: #0がないときは負の数を絶対値が小さい順に掛けるのが最善
   arr_minus=sorted(arr_minus)
   tmp=1
   for i in range(k):
     tmp*=arr_minus[i]
     tmp%=mod
   print((-tmp)%mod)
 exit()
else: #正の数と負の数から合計K個取れるとき
 arr_plus=sorted(arr_plus,reverse=True)
 arr_minus=sorted(arr_minus,reverse=True)
 if k%2==0: #Kが偶数であれば何もしない
   ans=1
   pos_plus=0
   pos_minus=0
   cnt=0
 else: #Kが奇数であれば正の数から最大のものを取っておき、Kが偶数の場合に帰着させる
   ans=arr_plus[0]
   pos_plus=1
   pos_minus=0
   cnt=1
 while cnt<k:
   cand1=0
   if pos_plus<=cnt_plus-2: #正の数を2つ取れるなら取る
     cand1=arr_plus[pos_plus]*arr_plus[pos_plus+1]      
   cand2=0
   if pos_minus<=cnt_minus-2 #負の数を2つ取れるなら取る:
     cand2=arr_minus[pos_minus]*arr_minus[pos_minus+1]
   if cand1>=cand2: #正の数を2つ取った積のほうが大きい場合
     pos_plus+=2
     ans*=cand1
     ans%=mod
   else: #負の数を2つ取った積のほうが大きい場合
     pos_minus+=2
     ans*=cand2
     ans%=mod
   cnt+=2
 print(ans)
 exit()

Submission status: Runtime Error

Solution code:
import itertools
import heapq
import sys
import functools
import operator
import numpy as np
input = sys.stdin.readline
#-------------
n,k = map(int,input().split())
a = list(map(int,input().split()))
#-------------
b = np.abs(a)
b = sorted(a,reverse = True)
b_k = b[k] 
a_new = [a[i] for i in range(len(a)) if np.abs(a)[i] > b_k]
 
comb = itertools.combinations(a_new,k)  
l = [list(_) for _ in comb]  
A = (functools.reduce(operator.mul,l[_]) for _ in range(len(l)))
l_max = max(A)
waru = 10**9 + 7
print(l_max % waru)

Submission status: Runtime Error

Solution code:
from collections import deque
import sys

N, K = map(int, input().split())
A = deque(sorted(list(map(int, input().split()))))

mod = 10 ** 9 + 7
num = 0
Ans = 1

while num < K-1:
    non_minus = A[-1] * A[-2]
    minus = A[0] * A[1]
    if non_minus > minus:
        Ans = Ans * A.pop() % mod
        num += 1
    else:
        Ans = Ans * A.popleft() * A.popleft() % mod
        num += 2

if num == K - 1:
    Ans = Ans * A[-1] % mod
    
if A[-1] < 0 and K % 2 == 1:
    Ans = 1
    for i in range(K):
        Ans = Ans * A[i] % mod

print(Ans)

Submission status: Runtime Error

Solution code:
from collections import deque
import sys

N, K = map(int, input().split())
A = deque(sorted(list(map(int, input().split()))))

mod = 10 ** 9 + 7
num = 0
Ans = 1

while num < K-1:
    non_minus = A[-1] * A[-2]
    minus = A[0] * A[1]
    if non_minus > minus:
        Ans = Ans * A.pop() % mod
        num += 1
    else:
        Ans = Ans * A.popleft() * A.popleft() % mod
        num += 2

if num == K - 1:
    Ans = Ans * A[-1] % mod
    
if A[-1] < 0 and K % 2 == 1:
    for i in range(K):
        Ans = Ans * A[i] % mod
    print(Ans)
    sys.exit()

print(Ans)

Submission status: Runtime Error

Solution code:
import itertools
import heapq
import sys
import functools
import operator
import numpy as np
input = sys.stdin.readline
#-------------
n,k = map(int,input().split())
a = list(map(int,input().split()))
#-------------
b = sorted(a,reverse = True)
b = np.abs(b)
b_k = b[k]

a_new = [a[i] for i in range(len(a)) if np.abs(a)[i] > b_k]
 
comb = itertools.combinations(a_new,k)  
l = [list(_) for _ in comb]  
A = (functools.reduce(operator.mul,l[_]) for _ in range(len(l)))
l_max = max(A)
waru = 10**9 + 7
print(l_max % waru)

Submission status: Runtime Error

Solution code:
from collections import deque
import sys

N, K = map(int, input().split())
A = deque(sorted(list(map(int, input().split()))))

mod = 10 ** 9 + 7
num = 0
Ans = 1

if A[-1] < 0 and K % 2 == 1:
    for i in range(K):
        Ans = Ans * A[i] % mod
    print(Ans)
    sys.exit()

while num < K:
    non_minus = A[-1] * A[-2]
    minus = A[0] * A[1]
    if num == K-1 or non_minus > minus:
        Ans = Ans * A.pop() % mod
        num += 1
    else:
        Ans = Ans * A.popleft() * A.popleft() % mod
        num += 2

print(Ans)