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 itertools
import heapq
n,k = map(int,input().split())
a = list(map(int,input().split()))
if n>=2k:
  a_plus = heapq.nlargest(k, a)
  a_minus = heapq.nsmallest(k, a)
  a_new = a_plus+a_minus
  comb = itertools.combinations(a_new,k)
else:
  comb = itertools.combinations(a,k)
 
l = []
for i in comb:
  l.append(list(i))
  
A = []
one = [1]*len(l)
for i in range(len(l)):
  for j in range(k):
    one[i]*=l[i][j]
    A.append(one[i])
    
l_max = max(one)
waru = 10**9 + 7
 
mod = l_max % waru
print(mod)

Submission status: Runtime Error

Solution code:
import itertools
import heapq
n,k = map(int,input().split())
a = list(map(int,input().split()))
if n>=2k:
  a_plus = heapq.nlargest(k, a)
  a_minus = heapq.nsmallest(k, a)
  comb = a_plus+a_minus
else:
  comb = itertools.combinations(a,k)
 
l = []
for i in comb:
  l.append(list(i))
  
A = []
one = [1]*len(l)
for i in range(len(l)):
  for j in range(k):
    one[i]*=l[i][j]
    A.append(one[i])
    
l_max = max(one)
waru = 10**9 + 7
 
mod = l_max % waru
print(mod)

Submission status: Runtime Error

Solution code:
import itertools
import heapq
n,k = map(int,input().split())
a = list(map(int,input().split()))
if n>=2k+1:
  a_plus = heapq.nlargest(k, a)
  a_minus = heapq.nsmallest(k, a)
  comb = a_plus+a_minus
else:
  comb = itertools.combinations(a,k)
 
l = []
for i in comb:
  l.append(list(i))
  
A = []
one = [1]*len(l)
for i in range(len(l)):
  for j in range(k):
    one[i]*=l[i][j]
    A.append(one[i])
    
l_max = max(one)
waru = 10**9 + 7
 
mod = l_max % waru
print(mod)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections
from decimal import Decimal # 10進数で考慮できる

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))
"""
N, K = LI()
A = LI()

def abssort(A):
    return sorted(A, key = lambda x:abs(x), reverse= True)

tmps = []
A_abs = abssort(A)
cnt = 0
while len(tmps) > K:
# for i in range(K):
    if A_abs[cnt] != 0:
        tmps.append(A_abs[i])
        cnt += 1

cnt = 0
for i in range(K):
    if tmps[i] < 0:
        cnt += 1
if cnt % 2 == 0:
    ans = 1
    for i in range(K):
        ans *= tmps[i]
        ans = ans % mod
    print(ans)
    sys.exit()
else:
    tmp_1 = copy.deepcopy(tmps)
    tmp_2 = copy.deepcopy(tmps)
    tmp_1.reverse()
    tmp_2.reverse()
    tmp_2_ = 0
    tmp_1_ = 0
    # -を一つ消す
    for i in range(K):
        if tmp_1[i] < 0:
            tmp_1.remove(tmp_1[i])
            break
    # + を一つたす
    for i in range(K, N):
        if A_abs[i] > 0:
            tmp_1.append(A_abs[i])
            tmp_1_ = 1
            break
    else:
        # +がなかった時
        tmp_1 = []
        tmp_1_ = -1 
        A_abs.reverse()
        for i in range(K):
            tmp_1.append(A_abs[i])

    # +を消して-を追加
    cnt = 0
    for i in range(K):
        if tmp_2[i] > 0:
            tmp_2.remove(tmp_2[i])
            cnt += 1
            break
    if cnt == 1:
        for i in range(K, N):
            if A_abs[i] < 0:
                tmp_2.append(A_abs[i])
                tmp_2_ = 1
                break
        else:
            tmp_2_ = 0
    else:
        tmp_2_ = -1
# print(tmp_1, tmp_2)



tmp_1_m = 1
tmp_2_m = 1
for i in range(K):
    tmp_1_m *= tmp_1[i]
    tmp_1_m = tmp_1_m % mod

if tmp_2_ == 0:
    pass
else:
    for i in range(K):
        tmp_2_m *= tmp_2[i]
        tmp_2_m = tmp_2_m % mod

# print(tmp_1_, tmp_2_)
if tmp_2_ == 0:
    print(tmp_1_m)
elif tmp_1_ == 1 and tmp_2_ == -1:
    print(tmp_1_m)
elif tmp_1_ == -1 and tmp_2_ == 1:
    print(tmp_2_m)
elif tmp_1_ == 1 and tmp_2_ == 1:
    for i in range(K):
        if tmp_1[i] == tmp_2[i]:
            pass
        elif abs(tmp_1[i]) > abs(tmp_2[i]):
            print(tmp_2_m)
            sys.exit()
        else:
            print(tmp_1_m)
            sys.exit()
else:
    print(tmp_1_m)
    # for i in range(K):
    #     if tmp_1[i] == tmp_2[i]:
    #         pass
    #     elif abs(tmp_1[i]) > abs(tmp_2[i]):
    #         print(tmp_2_m)
    #         sys.exit()
    #     else:
    #         print(tmp_1_m)
    #         sys.exit()
    """

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

# 正負に関係なくsort
A_abs = sorted(A, key = lambda x:abs(x), reverse= True)
A_p = [] # plusを入れる
A_n = [] # -を入れる
for i in range(N):
    if A[i] < 0:
        A_n.append(A[i])
    else:
        A_p.append(A[i])

A_n.sort()
A_p.sort(reverse = True)

ok = True
if len(A_p) > 0:
    # 正の数が存在している
    if N == K:
        # 選択肢がない時
        ok = (len(A_n) % 2 == 0)
        # 負の数が偶数個
    else:
        ok = True
else:
    ok = (K % 2 == 0)

ans = 1
if ok == False:
    A_n.sort(reverse= True)
    for i in range(K):
        ans *= A_n[i]
        ans = ans % mod
else:
    if K % 2 == 1:
        # 奇数個選ぶ
        ans *= A_p[0]
        ans = ans % mod
        A_p = A_p[1:]
    position = 0
    pairs = []
    cnt_p = len(A_p)
    cnt_n = len(A_n)
    while cnt_p - position > 1:
        pairs.append(A_p[position] * A_p[position + 1])
        position += 2
    position = 0
    while cnt_n - position > 1:
        pairs.append(A_n[position] * A_n[position + 1])
        position += 2

    pairs.sort(reverse=True)
    for i in range(K // 2):
        ans *= pairs[i]
        ans = ans % mod

print(ans)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections
from decimal import Decimal # 10進数で考慮できる

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))
"""
N, K = LI()
A = LI()

def abssort(A):
    return sorted(A, key = lambda x:abs(x), reverse= True)

tmps = []
A_abs = abssort(A)
cnt = 0
while len(tmps) > K:
# for i in range(K):
    if A_abs[cnt] != 0:
        tmps.append(A_abs[i])
        cnt += 1

cnt = 0
for i in range(K):
    if tmps[i] < 0:
        cnt += 1
if cnt % 2 == 0:
    ans = 1
    for i in range(K):
        ans *= tmps[i]
        ans = ans % mod
    print(ans)
    sys.exit()
else:
    tmp_1 = copy.deepcopy(tmps)
    tmp_2 = copy.deepcopy(tmps)
    tmp_1.reverse()
    tmp_2.reverse()
    tmp_2_ = 0
    tmp_1_ = 0
    # -を一つ消す
    for i in range(K):
        if tmp_1[i] < 0:
            tmp_1.remove(tmp_1[i])
            break
    # + を一つたす
    for i in range(K, N):
        if A_abs[i] > 0:
            tmp_1.append(A_abs[i])
            tmp_1_ = 1
            break
    else:
        # +がなかった時
        tmp_1 = []
        tmp_1_ = -1 
        A_abs.reverse()
        for i in range(K):
            tmp_1.append(A_abs[i])

    # +を消して-を追加
    cnt = 0
    for i in range(K):
        if tmp_2[i] > 0:
            tmp_2.remove(tmp_2[i])
            cnt += 1
            break
    if cnt == 1:
        for i in range(K, N):
            if A_abs[i] < 0:
                tmp_2.append(A_abs[i])
                tmp_2_ = 1
                break
        else:
            tmp_2_ = 0
    else:
        tmp_2_ = -1
# print(tmp_1, tmp_2)



tmp_1_m = 1
tmp_2_m = 1
for i in range(K):
    tmp_1_m *= tmp_1[i]
    tmp_1_m = tmp_1_m % mod

if tmp_2_ == 0:
    pass
else:
    for i in range(K):
        tmp_2_m *= tmp_2[i]
        tmp_2_m = tmp_2_m % mod

# print(tmp_1_, tmp_2_)
if tmp_2_ == 0:
    print(tmp_1_m)
elif tmp_1_ == 1 and tmp_2_ == -1:
    print(tmp_1_m)
elif tmp_1_ == -1 and tmp_2_ == 1:
    print(tmp_2_m)
elif tmp_1_ == 1 and tmp_2_ == 1:
    for i in range(K):
        if tmp_1[i] == tmp_2[i]:
            pass
        elif abs(tmp_1[i]) > abs(tmp_2[i]):
            print(tmp_2_m)
            sys.exit()
        else:
            print(tmp_1_m)
            sys.exit()
else:
    print(tmp_1_m)
    # for i in range(K):
    #     if tmp_1[i] == tmp_2[i]:
    #         pass
    #     elif abs(tmp_1[i]) > abs(tmp_2[i]):
    #         print(tmp_2_m)
    #         sys.exit()
    #     else:
    #         print(tmp_1_m)
    #         sys.exit()
    """

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

# 正負に関係なくsort
A_abs = sorted(A, key = lambda x:abs(x), reverse= True)
A_p = [] # plusを入れる
A_n = [] # -を入れる
for i in range(N):
    if A[i] < 0:
        A_n.append(A[i])
    else:
        A_p.append(A[i])

A_n.sort()
A_p.sort(reverse = True)

ok = True
if len(A_p) > 0:
    # 正の数が存在している
    if N == K:
        # 選択肢がない時
        ok = (len(A_n) % 2 == 0)
        # 負の数が偶数個
    else:
        ok = True
else:
    ok = (K % 2 == 0)

ans = 1
if ok == False:
    A_n.sort(reverse= True)
    for i in range(K):
        ans *= A_n[i]
        ans = ans % mod
else:
    if K % 2 == 1:
        # 奇数個選ぶ
        ans *= A_p[0]
        A_p = A_p[1:]
    position = 0
    pairs = []
    cnt_p = len(A_p)
    cnt_n = len(A_n)
    while cnt_p - position > 1:
        pairs.append(A_p[position] * A_p[position + 1])
        position += 2
    position = 0
    while cnt_n - position > 1:
        pairs.append(A_n[position] * A_n[position + 1])
        position += 2

    pairs.sort(reverse=True)
    for i in range(K // 2):
        ans *= pairs[i]
        ans = ans % mod

print(ans)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections
from decimal import Decimal # 10進数で考慮できる

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))
"""
N, K = LI()
A = LI()

def abssort(A):
    return sorted(A, key = lambda x:abs(x), reverse= True)

tmps = []
A_abs = abssort(A)
cnt = 0
while len(tmps) > K:
# for i in range(K):
    if A_abs[cnt] != 0:
        tmps.append(A_abs[i])
        cnt += 1

cnt = 0
for i in range(K):
    if tmps[i] < 0:
        cnt += 1
if cnt % 2 == 0:
    ans = 1
    for i in range(K):
        ans *= tmps[i]
        ans = ans % mod
    print(ans)
    sys.exit()
else:
    tmp_1 = copy.deepcopy(tmps)
    tmp_2 = copy.deepcopy(tmps)
    tmp_1.reverse()
    tmp_2.reverse()
    tmp_2_ = 0
    tmp_1_ = 0
    # -を一つ消す
    for i in range(K):
        if tmp_1[i] < 0:
            tmp_1.remove(tmp_1[i])
            break
    # + を一つたす
    for i in range(K, N):
        if A_abs[i] > 0:
            tmp_1.append(A_abs[i])
            tmp_1_ = 1
            break
    else:
        # +がなかった時
        tmp_1 = []
        tmp_1_ = -1 
        A_abs.reverse()
        for i in range(K):
            tmp_1.append(A_abs[i])

    # +を消して-を追加
    cnt = 0
    for i in range(K):
        if tmp_2[i] > 0:
            tmp_2.remove(tmp_2[i])
            cnt += 1
            break
    if cnt == 1:
        for i in range(K, N):
            if A_abs[i] < 0:
                tmp_2.append(A_abs[i])
                tmp_2_ = 1
                break
        else:
            tmp_2_ = 0
    else:
        tmp_2_ = -1
# print(tmp_1, tmp_2)



tmp_1_m = 1
tmp_2_m = 1
for i in range(K):
    tmp_1_m *= tmp_1[i]
    tmp_1_m = tmp_1_m % mod

if tmp_2_ == 0:
    pass
else:
    for i in range(K):
        tmp_2_m *= tmp_2[i]
        tmp_2_m = tmp_2_m % mod

# print(tmp_1_, tmp_2_)
if tmp_2_ == 0:
    print(tmp_1_m)
elif tmp_1_ == 1 and tmp_2_ == -1:
    print(tmp_1_m)
elif tmp_1_ == -1 and tmp_2_ == 1:
    print(tmp_2_m)
elif tmp_1_ == 1 and tmp_2_ == 1:
    for i in range(K):
        if tmp_1[i] == tmp_2[i]:
            pass
        elif abs(tmp_1[i]) > abs(tmp_2[i]):
            print(tmp_2_m)
            sys.exit()
        else:
            print(tmp_1_m)
            sys.exit()
else:
    print(tmp_1_m)
    # for i in range(K):
    #     if tmp_1[i] == tmp_2[i]:
    #         pass
    #     elif abs(tmp_1[i]) > abs(tmp_2[i]):
    #         print(tmp_2_m)
    #         sys.exit()
    #     else:
    #         print(tmp_1_m)
    #         sys.exit()
    """

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

# 正負に関係なくsort
A_abs = sorted(A, key = lambda x:abs(x), reverse= True)
A_p = [] # plusを入れる
A_n = [] # -を入れる
for i in range(N):
    if A[i] < 0:
        A_n.append(A[i])
    else:
        A_p.append(A[i])

A_n.sort()
A_p.sort(reverse = True)

ok = True
if len(A_p) > 0:
    # 正の数が存在している
    if N == K:
        # 選択肢がない時
        ok = (len(A_n) % 2 == 0)
        # 負の数が偶数個
    else:
        ok = True
else:
    ok = (K % 2 == 0)

ans = 1
if ok == False:
    A_n.sort(reverse= True)
    for i in range(K):
        ans *= A_n[i]
        ans = ans % mod
else:
    if K % 2 == 1:
        # 奇数個選ぶ
        ans *= A_p[0]
        A_p = A_p[1:]
    position = 0
    pairs = []
    cnt_p = len(A_p)
    cnt_n = len(A_n)
    while cnt_p - position > 1:
        pairs.append(A_p[position] * A_p[position + 1])
        position += 2
    position = 0
    while cnt_n - position > 1:
        pairs.append(A_n[position] * A_n[position + 1])
        position += 2

    pairs.sort(reverse=True)
    for i in range(K // 2):
        pairs[i] = pairs[i] % mod

    # print(pairs)
    for i in range(K // 2):
        ans *= pairs[i]
        ans = ans % mod

print(ans)

Submission status: Runtime Error

Solution code:
import itertools
n,k = map(int,input().split())
a = list(map(int,input().split()))
 
comb = itertools.combinations(a,3)
 
l = []
for i in comb:
  l.append(list(i))
  
A = []
one = [1]*len(l)
for i in range(len(l)):
  for j in range(3):
    one[i]*=l[i][j]
    A.append(one[i])
    
l_max = max(one)
waru = 10**9 + 7
 
mod = l_max % waru
print(mod)

Submission status: Runtime Error

Solution code:
import itertools
n,k = input().split
a = list(map(int,input().split()))

comb = itertools.combinations(a,3)

l = []
for i in comb:
  l.append(list(i))
  
A = []
one = [1]*len(l)
for i in range(len(l)):
  for j in range(3):
    one[i]*=l[i][j]
    A.append(one[i])
    
l_max = max(one)
waru = 10**9 + 7

mod = l_max % waru
print(mod)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
inlist = list(map(int, input().split()))
inlist.sort(reverse=True)
poslist=list()
neglist=list()
pindex=0
nindex=N-1
for i in range(K):
  if abs(inlist[pindex])>=abs(inlist[nindex]):
    poslist.append(inlist[pindex])
    pindex+=1
  else:
    neglist.append(inlist[nindex])
    nindex-=1
if len(neglist)%2 !=0:
  if pindex <= nindex:
    if poslist[-1]*inlist[pindex] < neglist[-1]*inlist[nindex]:
      poslist.pop()
      neglist.append(inlist[nindex])
    else:
      neglist.pop()
      poslist.append(inlist[pindex])

mult=1
poslist.extend(neglist)
for i in range(K):
  mult = mult * poslist[i]

ans = mult
if abs(mult)>1000000000:
  ans=mult%1000000007
print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
inlist = list(map(int, input().split()))
inlist.sort(reverse=True)
poslist=list()
neglist=list()
pindex=0
nindex=N-1
for i in range(K):
  if abs(inlist[pindex])>=abs(inlist[nindex]):
    poslist.append(inlist[pindex])
    pindex+=1
  else:
    neglist.append(inlist[nindex])
    nindex-=1
if len(neglist)%2 !=0:
  if pindex <= nindex:
    if poslist[-1]*inlist[pindex] < neglist[-1]*inlist[nindex]:
      poslist.pop()
      neglist.append(inlist[nindex])
    else:
      neglist.pop()
      poslist.append(inlist[pindex])

if poslist[-1]==0 or neglist[-1]==0:
  print(0)
else:
  mult=1
  poslist.extend(neglist)
  print(poslist)
  for i in range(K):
    mult = mult * poslist[i]
ans = mult
if abs(mult)>1000000000:
  ans=mult%1000000007
print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
n,k=map(int,input().split())
alist=list(map(int,input().split()))
plalist,mailist=[],[]
p=10**9+7
for i in alist:
  if i>0:
    plalist.append(i)
  else:
    mailist.append(i)
#大きい順
dplalist=sorted(plalist,reverse=True)
#絶対値が大きい順
dmailist=sorted(mailist)
total=1
cnt,i,j=0,0,0
if dplalist==[]:
  for i in range(k):
    i+=1
    val=dmailist[n-i]
    total*=val
    if not i%2==0:
      total=int(total%p)
elif len(dmailist)==1 or len(dmailist)==0:
  for i in range(k):
    i+=1
    val=dplalist[n-i]
    total*=val
    total=int(total%p)
else:
  while cnt<k:
    x=dplalist[i]*dplalist[i+1]
    y=dmailist[2*j]*dmailist[2*j+1]
    if x>y or cnt==k-1:
      total*=dplalist[2*i]
      total=int(total%p)
      cnt+=1
      i+=1
    else:
      total*=y
      total=int(total%p)
      cnt+=2
      j+=1
print(int(total%p))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
n,k=map(int,input().split())
alist=list(map(int,input().split()))
plalist,mailist=[],[]
p=10**9+7
for i in alist:
  if i>0:
    plalist.append(i)
  else:
    mailist.append(i)
#大きい順
dplalist=sorted(plalist,reverse=True)
#絶対値が大きい順
dmailist=sorted(mailist)
total=1
cnt,i,j=0,0,0
if dplalist==[]:
  for i in range(k):
    i+=1
    val=dmailist[n-i]
    total*=val
    if i%2==0:
      total=int(total%p)
elif len(dmailist)==1 or len(dmailist)==0:
  for i in range(k):
    i+=1
    val=dplalist[n-i]
    total*=val
    total=int(total%p)
else:
  while cnt<k:
    x=dplalist[i]*dplalist[i+1]
    y=dmailist[2*j]*dmailist[2*j+1]
    if x>y or cnt==k-1:
      total*=dplalist[2*i]
      total=int(total%p)
      cnt+=1
      i+=1
    else:
      total*=y
      total=int(total%p)
      cnt+=2
      j+=1
print(int(total%p))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
n,k=map(int,input().split())
alist=list(map(int,input().split()))
plalist,mailist=[],[]
p=10**9+7
for i in alist:
  if i>0:
    plalist.append(i)
  else:
    mailist.append(i)
#大きい順
dplalist=sorted(plalist,reverse=True)
#絶対値が大きい順
dmailist=sorted(mailist)
total=1
cnt,i,j=0,0,0
if dplalist==[]:
  for i in range(k):
    i+=1
    val=dmailist[n-i]
    total*=val
elif len(dmailist)==1 or len(dmailist)==0:
  for i in range(k):
    i+=1
    val=dplalist[n-i]
    total*=val
    total=int(total%p)
else:
  while cnt<k:
    x=dplalist[i]*dplalist[i+1]
    y=dmailist[2*j]*dmailist[2*j+1]
    if x>y or cnt==k-1:
      total*=dplalist[2*i]
      total=int(total%p)
      cnt+=1
      i+=1
    else:
      total*=y
      total=int(total%p)
      cnt+=2
      j+=1
print(int(total%p))

Submission status: Runtime Error

Solution code:
from collections import deque
n, k = map(int,input().split())
a = list(map(int,input().split()))
mod = 10 ** 9 + 7

p = []
m = []
for i in a:
    if i >= 0:p.append(i)
    elif i < 0:m.append(i)

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

if len(p) == 0 and k % 2 == 1:
    for i in range(k):
        res = (res * m[-(i + 1)]) %mod
    print(res)

else:
    p_ind = 0
    m_ind = 0
    while k:
        
        if p_ind >= len(p):
            temp1 = 0
        else:
            temp = p[p_ind] * p[p_ind + 1] if p_ind + 1 < len(p) else 0
            temp1 = p[p_ind]
            
        temp2 = m[m_ind] * m[m_ind + 1] if m_ind + 1 < len(m) else 0
        if temp > temp2 or k == 1:
            res = (res * temp1) %mod
            p_ind += 1
            k -= 1
        else:
            res = (res * temp2) %mod
            m_ind += 2
            k -= 2
    print(res)

Submission status: Runtime Error

Solution code:
from collections import deque
n, k = map(int,input().split())
a = list(map(int,input().split()))
mod = 10 ** 9 + 7

p = []
m = []
for i in a:
    if i >= 0:p.append(i)
    elif i < 0:m.append(i)

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

if len(p) == 0 and k % 2 == 1:
    for i in range(k):
        res = (res * m[-(i + 1)]) %mod
    print(res)

else:
    p_ind = 0
    m_ind = 0
    while k:
        
        if p_ind >= len(p):
            temp1 = 0
        else:
            temp = p[p_ind] * p[p_ind + 1] if p_ind + 1 < len(p) else p[p_ind]
            temp1 = p[p_ind]
            
        temp2 = m[m_ind] * m[m_ind + 1] if m_ind + 1 < len(m) else 0
        if temp > temp2 or k == 1:
            res = (res * temp1) %mod
            p_ind += 1
            k -= 1
        else:
            res = (res * temp2) %mod
            m_ind += 2
            k -= 2
    print(res)

Submission status: Runtime Error

Solution code:
from collections import deque
n, k = map(int,input().split())
a = list(map(int,input().split()))
mod = 10 ** 9 + 7

p = []
m = []
for i in a:
    if i >= 0:p.append(i)
    elif i < 0:m.append(i)

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

if len(p) == 0 and k % 2 == 1:
    for i in range(k):
        res = (res * m[-(i + 1)]) %mod
    print(res)

else:
    p_ind = 0
    m_ind = 0
    while k:
        
        if p_ind >= len(p):
            temp = 0
        else:
            temp = p[p_ind] * p[p_ind + 1] if p_ind + 1 < len(p) else p[p_ind]
        temp1 = m[m_ind] * m[m_ind + 1] if m_ind + 1 < len(m) else -1
        if temp > temp1 or k == 1:
            res = (res * p[p_ind]) %mod
            p_ind += 1
            k -= 1
        else:
            res = (res * temp1) %mod
            m_ind += 2
            k -= 2
    print(res)

Submission status: Runtime Error

Solution code:
from collections import deque
n, k = map(int,input().split())
a = list(map(int,input().split()))
mod = 10 ** 9 + 7

p = []
m = []
for i in a:
    if i >= 0:p.append(i)
    elif i < 0:m.append(i)

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

if len(p) == 0 and k % 2 == 1:
    for i in range(k):
        res = (res * m[-(i + 1)]) %mod
    print(res)

else:
    p_ind = 0
    m_ind = 0
    while k:
        
        temp = p[p_ind] * p[p_ind + 1] if p_ind + 1 < len(p) else p[p_ind]
        temp1 = m[m_ind] * m[m_ind + 1] if m_ind + 1 < len(m) else -1
        if temp > temp1 or k == 1:
            res = (res * p[p_ind]) %mod
            p_ind += 1
            k -= 1
        else:
            res = (res * temp1) %mod
            m_ind += 2
            k -= 2
    print(res)

Submission status: Runtime Error

Solution code:
from collections import deque
n, k = map(int,input().split())
a = list(map(int,input().split()))
mod = 10 ** 9 + 7

p = []
m = []
for i in a:
    if i >= 0:p.append(i)
    elif i < 0:m.append(i)

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

if len(p) == 0 and k % 2 == 1:
    for i in range(k):
        res = (res * m[-(i + 1)]) %mod
    print(res)

else:
    p_ind = 0
    m_ind = 0
    while k:
        
        temp = p[p_ind] * p[p_ind + 1] if p_ind + 1 < len(p) else p[p_ind]
        temp1 = m[m_ind] * m[m_ind + 1] if m_ind + 1 < len(m) else m[m_ind]
        if temp > temp1 or k == 1:
            res = (res * p[p_ind]) %mod
            p_ind += 1
            k -= 1
        else:
            res = (res * temp1) %mod
            m_ind += 2
            k -= 2
    print(res)

Submission status: Runtime Error

Solution code:
from collections import deque
n, k = map(int,input().split())
a = list(map(int,input().split()))
mod = 10 ** 9 + 7

p = []
m = []
for i in a:
    if i >= 0:p.append(i)
    elif i < 0:m.append(i)

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

if len(p) == 0 and k % 2 == 1:
    for i in range(k):
        res = (res * m[-(i + 1)]) %mod
    print(res)

else:
    p_ind = 0
    m_ind = 0
    while k:
        if k == 1:
            res = (res * p[p_ind]) %mod
            k -= 1
            break
        temp = p[p_ind] * p[p_ind + 1] if p_ind + 1 < len(p) else 0
        temp1 = m[m_ind] * m[m_ind + 1] if m_ind + 1 < len(m) else -1
        if temp > temp1:
            res = (res * p[p_ind]) %mod
            p_ind += 1
            k -= 1
        else:
            res = (res * temp1) %mod
            m_ind += 2
            k -= 2
    print(res)

Submission status: Runtime Error

Solution code:
from collections import deque
n, k = map(int,input().split())
a = list(map(int,input().split()))
mod = 10 ** 9 + 7

p = []
m = []
for i in a:
    if i >= 0:p.append(i)
    elif i < 0:m.append(i)

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

if len(p) == 0 and k % 2 == 1:
    for i in range(k):
        res = (res * m[-(i + 1)]) %mod
    print(res)

else:
    p_ind = 0
    m_ind = 0
    while k:
        if k == 1:
            res = (res * p[p_ind]) %mod
            k -= 1
            break
        temp = p[p_ind] * p[p_ind + 1] if p_ind + 1 < len(p) else 1
        temp1 = m[m_ind] * m[m_ind + 1] if m_ind + 1 < len(m) else 0
        if temp > temp1:
            res = (res * p[p_ind]) %mod
            p_ind += 1
            k -= 1
        else:
            res = (res * temp1) %mod
            m_ind += 2
            k -= 2
    print(res)

Submission status: Runtime Error

Solution code:
plus = []
minus = []
zero = 0
ans = 1

for i in a:
    if i > 0:
        plus.append(i)
    elif i < 0:
        minus.append(-i)
    else:
        zero += 1

plus.sort()
minus.sort()
p_l = len(plus)
m_l = len(minus)
cnt = 0

if k == 1:
    print(max(a))
elif zero > n - k:
    print(0)
elif zero == n - k:
    for i in plus:
        ans = (i * ans) % num
    for j in minus:
        ans = (ans * j) % num
    if m_l % 2 == 0:
        print(ans)
    else:
        print(-ans % num)
elif len(plus) == 0:
    if k % 2 == 0:
        for j in range(k):
            ans = ans * (minus[-(j + 1)]) % num
        print(ans % num)
    else:
        for j in range(k):
            ans = ans * (minus[j]) % num
        print(-ans % num)
else:
    for i in range(k):
        if p_l > 1 and m_l > 1:
            if plus[-1] >= minus[-1]:
                ans = ans * plus.pop() % num
                p_l -= 1
            else:
                ans = ans * minus.pop() % num
                cnt += 1
                m_l -= 1
        elif m_l == 1:
            if cnt % 2 == 1:
                ans = ans * minus.pop() % num
                m_l -= 1
                cnt += 1
            else:
                ans = ans * plus.pop() % num
                p_l -= 1
        elif p_l == 1:
            if k - i - 1 % 2 == 0:
                ans = ans * plus.pop() % num
                p_l -= 1
            else:
                ans = ans * minus.pop() % num
                cnt += 1
                m_l -= 1
        elif p_l == 0:
            ans = ans * minus.pop() % num
            m_l -= 1
            cnt += 1
        elif m_l == 0:
            ans = ans * plus.pop() % num
            p_l -= 1
    if cnt % 2 == 0:
        print(ans % num)
    else:
        print(-ans % num)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
aa = list(map(np.int64, input().split()))
import numpy as np
aa = np.array(aa)

def mul(dd):
    mod = 10**9+7
    ret = 1
    for d in dd:
        ret = (ret*d)%mod
    return ret

def sol(aa, n, k):
    mod = 10**9+7
    aap = aa[aa>0]
    aam = aa[aa<0]
    if n == k:
        return mul(aa)
    if len(aap) + 2*(len(aam)//2) < k:
        return 0

    # マイナスのみ
    if len(aam) == n:
        aam.sort()
        if k%2==1:
            return mul(aam[:k])
        else:
            return mul(aam[-k:])

    aap = aa[aa>=0]
    aap.sort()
    aap = aap[::-1]
    aam.sort()

    ret=1
    if k%2 >0:
        k = k-1
        ret *= aap[0]
        aap = aap[1:]

    aap2 = [(aap[2*i]*aap[2*i+1])%mod for i in range(len(aap)//2)]
    aam2 = [(aam[2*i]*aam[2*i+1])%mod for i in range(len(aam)//2)]
    aap2.extend(aam2)
    aap2.sort(reverse=True)
    return (ret*mul(aap2[:k//2]))%mod
print(sol(aa, n, k))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
aa = list(map(int, input().split()))
import numpy as np
aa = np.array(aa)
mod = 10**9+7

def mul(dd):
    ret = 1
    for d in dd:
        ret = (ret*d)%mod
    return ret

def sol(aa, n, k):
    aap = aa[aa>0]
    aam = aa[aa<0]
    if n == k:
        return mul(aa)
    if len(aam) == n and k%2==1:
        aam.sort()
        aam = aam[::-1]
        return mul(aam[:k])

    aap.sort()
    aap = aap[::-1]
    aam.sort()

    ret=1
    if k%2 >0:
        k = k-1
        ret *= aap[0]
        aap = aap[1:]

    aap2 = [aap[2*i]*aap[2*i+1] for i in range(len(aap)//2)]
    aam2 = [aam[2*i]*aam[2*i+1] for i in range(len(aam)//2)]
    aap2.extend(aam2)
    aap2.sort(reverse=True)
    return (ret*mul(aap2[:k//2]))%mod

print(sol(aa, n, k))

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
la = list(map(int,input().split()))
la = sorted(la)
la = sorted(la,key=abs,reverse=True)
lg = la[:K]
lgp = []
lgm = []
lr = la[K:]
lrp = []
lrm = []
for a in lg:
    if a >= 0:
        lgp.append(a)
    else:
        lgm.append(a)
for a in lr:
    if a >= 0:
        lrp.append(a)
    else:
        lrm.append(a)
if len(lgm) % 2 == 1:
    if len(lrp) > 0 and len(lrm) > 0:
        if len(lgp) > 0 and len(lgm) > 0:
            if lgp[len(lgp) - 1] * lrp[0] > lgm[len(lgm) - 1] * lrm[0]:
                lg.pop(lg.index(lgm[len(lgm) - 1]))
                lg.append(lrp[0])
            else:
                lg.pop(lg.index(lgp[len(lgp) - 1]))
                lg.append(lrm[0])
        else:
            if len(lgp) > 0:
                lg.pop(lg.index(lgp[len(lgp) - 1]))
                lg.append(lrm[0])
            else:
                lg.pop(lg.index(lgp[len(lgp) - 1]))
                lg.append(lrm[0])
    elif len(lrp) > 0:
        if len(lgm) > 0:
            lg.pop(lg.index(lgm[len(lgm) - 1]))
            lg.append(lrp[0])
        else:
            lg = la[N - K:]
    elif len(lrm) > 0:
        if len(lgp) > 0:
            lg.pop(lg.index(lgp[len(lgp) - 1]))
            lg.append(lrm[0])
        else:
            lg = la[N - K:]
ans = 1
for a in lg:
    ans *= a
    ans %= 10 ** 9 + 7
print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
aa = list(map(int, input().split()))
import numpy as np
aa = np.array(aa)

def mul(dd):
    mod = 10**9+7
    ret = 1
    for d in dd:
        ret = (ret*d)%mod
    return ret

def sol(aa, n, k):
    mod = 10**9+7
    aap = aa[aa>0]
    aam = aa[aa<0]
    if n == k:
        return mul(aa)
    if len(aap) + 2*(len(aam)//2) < k:
        return 0
    
    if len(aam) == n and k%2==1:
        aam.sort()
        aam = aam[::-1]
        return mul(aam[:k])

    aap = aa[aa>=0]
    aap.sort()
    aap = aap[::-1]
    aam.sort()

    ret=1
    if k%2 >0:
        k = k-1
        ret *= aap[0]
        aap = aap[1:]

    aap2 = [aap[2*i]*aap[2*i+1] for i in range(len(aap)//2)]
    aam2 = [aam[2*i]*aam[2*i+1] for i in range(len(aam)//2)]
    ip=0
    im=0
    for i in range(k//2):
        if ip < len(aap2) and aap2[ip] >= aam2[im]:
            ret = (ret * aap2[ip])%mod
            ip += 1
        else:
            ret = (ret * aam2[im])%mod
            im += 1
    return ret%mod

print(sol(aa, n, k))