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

from sys import stdin
input = lambda: stdin.readline().rstrip()


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

tl = []
for A in map(int,input().split()):
    tl.append((abs(A), A > 0))

tl.sort()
l = list(reversed(tl))

f = True
answer = 1
M = 10**9 + 7

last_Posi_idx = -1
last_Nega_idx = -1

answers = []
for i in range(K):
    A, F = l[i]
    answers.append(A)
    f = f == F
    if F:
        last_Posi_idx = i
    else:
        last_Nega_idx = i

# end with positive
if f:
    answer = 1
    for num in answers:
        answer = answer * num % M
    print(answer)
    exit()

alt_p_answer = -1
alt_n_answer = -1

alt_p = 'NOTFOUND'
alt_n = 'NOTFOUND'

# alt p
if last_Posi_idx > -1:
    for i in range(K,len(l)):
        A, F = l[i]
        if not F:
            alt_p = A
    if alt_p != 'NOTFOUND':
        alt_p_answer = 1
        for i in range(answers):
            num = answers[i] if i != last_Posi_idx else alt_p
            alt_p_answer = alt_p_answer * num % M

if last_Nega_idx > -1:
    for i in range(K,len(l)):
        A, F = l[i]
        if F:
            alt_n = A
    if alt_n != 'NOTFOUND':
        alt_n_answer = 1
        for i in range(answers):
            num = answers[i] if i != last_Nega_idx else alt_n
            alt_n_answer = alt_n_answer * num % M

if alt_p == 'NOTFOUND' and alt_n == 'NOTFOUND':
    answer = 1
    for i in range(K):
        A, F = tl[i]
        f = 1 if F else -1
        answer = answer * A * f % M
    print(answer)
else:
    answer = max(alt_n_answer, alt_p_answer)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
def resolve():
    from bisect import bisect_left
    n,k=map(int,input().split())
    a=sorted(list(map(int,input().split())))
    m=bisect_left(a,0)
    MOD=10**9+7
    if m==0:
        ans=1
        for i in a[-k:]:
            ans=(ans*i)%MOD
    elif m==n:
        ans=1
        if k%2==0:
            for i in a[:k]:
                ans=(ans*i)%MOD
        else:
            for i in a[-k:]:
                ans=(ans*i)%MOD
    else:
        cal=1
        i=(m//2)*2
        for j in a[:i]:
            cal=(cal*j)%MOD
        if k-i>0:
            for j in a[-(k-i):]:
                cal=(cal*j)%MOD
        ans=cal
        for i in range((m//2)*2-2,-2,-2):
            if k-i>n-m:
                break
            for j in a[i-2:i]:
                cal=(cal*pow(j,MOD-2,MOD))%MOD
            if k-i>=0:
                if -(k-i)+2==0:
                    for j in a[-(k-i):]:
                    cal=(cal*j)%MOD
                else:
                    for j in a[-(k-i):-(k-i)+2]:
                        cal=(cal*j)%MOD
            ans=max(ans,cal)
    print(ans)

if __name__ == '__main__':
    resolve()

Submission status: Runtime Error

Solution code:
import bisect
from collections import deque
N, K = map(int,input().split())
mod = 10 ** 9 + 7
K2 = K

A = list(map(int, input().split()))
A.sort(reverse = True)
A2 = A[:]
A = deque(A)
A2 = deque(A2)

B = []
for i in A:
    if i < 0:
        B.append(i)

B.sort()
B = deque(B)

i = 0
fail = False
ans = 1
while True:
    if K > 1:
        plus = A[0] * A[1]
        if (len(B)>=2):
            minus = B[0] * B[1]
        else:
            minus = 0
        if A[0]< 0 or A[1] < 0:
            fail = True
            break
        if plus > minus:
            A.popleft()
            A.popleft()
            ans = ans * plus % mod
        else:
            B.popleft()
            B.popleft()
            ans = ans * minus % mod
        K -= 2
    else:
        if A[0] < 0:
            fail = True
            break
        else:
            ans = ans * A[0]
        K -= 1
    if K == 0:
        break

if fail:
    ans = 1
    for i in range(K2):
        ans *= A2.popleft()
    print(ans % mod)
else:
    print(ans)

Submission status: Runtime Error

Solution code:
import sys

def L(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI():return list(map(int,sys.stdin.readline().rstrip().split()))
def LS():return list(sys.stdin.readline().rstrip().split())

def anser(prod):
    print(prod%(10**9+7))
    exit()

def solver(N,K,A):
    #print(A)
    all_minus = False
    minus_count = sum( [1 if i<=0 else 0 for i in A] )
    if minus_count == len(A):
        _sort = sorted(A,reverse=True)
        all_minus = True
    else:
        _sort = sorted(A, key=lambda x: abs(x),reverse=True)

    #print(_sort)

    prod = 1
    last_idx = -1
    stack = []
    for i in range(K):
        #stack.append(_sort[i])
        prod *= _sort[i]
        last_idx = i

    if prod >= 0 or all_minus:
        anser(prod)

    else:
        #print(last_idx)
        #print(stack, prod)
        bef_prod = prod

        for i in range(last_idx, N+1):
            if _sort[i] >= 0:
                continue

            prod = prod // _sort[last_idx] * _sort[i]
            last_idx = i


            #stack.pop()
            #stack.append(_sort[last_idx+1])

            if prod >= 0:
                anser(prod)

        anser(bef_prod)


def main():

    N,K = LI()
    A = LI()

    solver(N,K,A)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
A_list = list(map(int, input().split()))

plus_A_list = [A_list[i] for i in range(N) if A_list[i] > 0]
plus_A_list.sort()
minus_A_list = [A_list[i] for i in range(N) if A_list[i] <= 0]
minus_A_list.sort()

mod_num = 10 ** 9 + 7
len_p = len(plus_A_list)
len_m = len(minus_A_list)
if len(plus_A_list) == 0:
    ans = 1
    for _ in range(1, K + 1):
        ans = (ans * minus_A_list.pop()) % mod_num
    print(ans)
elif K % 2 == 1:
    ans = plus_A_list.pop()
    len_p -= 1
    if len_p >= 2:
        temp_p = plus_A_list.pop() * plus_A_list.pop()
        len_p -= 2
    if len_m >= 2:
        temp_m = minus_A_list.pop() * minus_A_list.pop()
        len_m -= 2
    for i in range(1, K, 2):
        if len_p < 2:
            ans = ans * temp_m % mod_num
            if len_m >= 2:
                temp_m = minus_A_list.pop() * minus_A_list.pop()
                len_m -= 2
        elif len_m < 2:
            ans = ans * temp_p % mod_num
            if len_p >= 2:
                temp_p = plus_A_list.pop() * plus_A_list.pop()
                len_p -= 2
        else:
            if temp_p >= temp_m:
                ans = ans * temp_p % mod_num
                if len_p >= 2:
                    temp_p = plus_A_list.pop() * plus_A_list.pop()
                    len_p -= 2
            else:
                ans = ans * temp_m % mod_num
                if len_m >= 2:
                    temp_m = minus_A_list.pop() * minus_A_list.pop()
                    len_m -= 2
else:
    if len_p >= 2:
        temp_p = plus_A_list.pop() * plus_A_list.pop()
        len_p -= 2
    if len_m >= 2:
        temp_m = minus_A_list.pop() * minus_A_list.pop()
        len_m -= 2
    for i in range(1, K, 2):
        if len_p < 2:
            ans = ans * temp_m % mod_num
            if len_m >= 2:
                temp_m = minus_A_list.pop() * minus_A_list.pop()
                len_m -= 2
        elif len_m < 2:
            ans = ans * temp_p % mod_num
            if len_p >= 2:
                temp_p = plus_A_list.pop() * plus_A_list.pop()
                len_p -= 2
        else:
            if temp_p >= temp_m:
                ans = ans * temp_p % mod_num
                if len_p >= 2:
                    temp_p = plus_A_list.pop() * plus_A_list.pop()
                    len_p -= 2
            else:
                ans = ans * temp_m % mod_num
                if len_m >= 2:
                    temp_m = minus_A_list.pop() * minus_A_list.pop()
                    len_m -= 2
    print(ans)

Submission status: Runtime Error

Solution code:
M = 1000000007
n, k = map(int, input().split())
l = list(map(int, input().split()))
k1 = k
pos = []
neg = []
zero = []
for i in l:
    if i > 0: pos.append(i)
    if i < 0: neg.append(i)
    if i == 0: zero.append(i)
pos.sort(reverse = True)
neg.sort()


if k == 1: 
    print(pos[0] % M)
    exit(0)

check = 0
if k % 2 == 1:
    check = 1
    k -= 3

p1, p2 = 0, 0
n1, n2 = len(pos), len(neg)

ans = 1
ch1, ch2 = 1, 1
while 1:
    if k == 0: break
    
    if p1 + 1 >= n1: ch1 = 0 
    if p2 + 1 >= n2: ch2 = 0
    
    if ch1 and ch2:
        k -= 2
        t1 = pos[p1] * pos[p1 + 1]
        t2 = neg[p2] * neg[p2 + 1]
        if t1 >= t2:
            p1 += 2
            ans = (ans * t1) % M
        else:
            p2 += 2
            ans = (ans * t2) % M
    elif ch1:
        k -= 2
        t1 = pos[p1] * pos[p1 + 1]
        ans = (ans * t1) % M
        p1 += 2
    elif ch2:
        k -= 2
        t2 = neg[p2] * neg[p2 + 1]
        ans = (ans * t2) % M
        p2 += 2
    else: break

if k != 0:
    if p1 + 1 >= n1 and p2 + 1 >= n2:
        ans = (ans * pos[p1]) % M
        ans = (ans * neg[p2]) % M
else:
    ch1, ch2 = 1, 1
    if check:
        if p1 + 2 >= n1: ch1 = 0 
        if p2 + 1 >= n2: ch2 = 0
        
        if ch1 and ch2:
            t1 = pos[p1] * pos[p1 + 1] * pos[p1 + 2]
            t2 = neg[p2] * neg[p2 + 1] * pos[p1]
            if t1 >= t2:
                ans = (ans * t1) % M
            else:
                ans = (ans * t2) % M
        elif ch1:
            t1 = pos[p1] * pos[p1 + 1] * pos[p1 + 2]
            ans = (ans * t1) % M
        elif ch2:
            if p1 < n1:
                t2 = neg[p2] * neg[p2 + 1] * pos[p1]
                ans = (ans * t2) % M
            else:
                if len(zero) > 0: ans = 0
                else:
                    tmp = []
                    for i in range(n):
                        if l[i] >= 0: tmp.append([l[i], 1])
                        else: tmp.append([-l[i], 0])
                    tmp.sort()
                    Ans = 1
                    for i in range(k1):
                        if tmp[i][1] == 1: Ans = (Ans * tmp[i][0]) % M
                        else: Ans = (Ans * tmp[i][0] * -1) % M
                    ans = Ans
        else:
            if len(zero) > 0: ans = 0
            else:
                t2 = neg[p2] * pos[p1] * pos[p1 + 1]
                ans = (ans * t2) % M
print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
N = [0]*n
for i in range(len(a)):
    if a[i] >= 0:
        N[i] = [a[i], 1]
    else:
        N[i] = [a[i], -1]

A = []
B = []
C = []

for i in N:
    if i[0] >= 0:
        B.append(i)
    else:
        C.append(i)
    A.append([abs(i[0]), i[1]])
    
A = sorted(A, reverse=True, key=lambda x: x[0])
B = sorted(B, reverse=True, key=lambda x: x[0])
C = sorted(C, key=lambda x: x[0])

s = 1
for i in range(k):
    s *= A[i][1]
if s == 1:
    for i in range(k):
        if ans*A[i] > 1000000007
        ans = (ans*A[i])//1000000007
    print(ans//1000000007)
else:
    for i in range(k):
        if

Submission status: Runtime Error

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

mod=10**9+7

if k%2==0:
    ans = 1
    for i in range(n):
        a[i]=abs(a[i])
    a.sort(reverse=True)
    for i in range(k):
        ans*=a[i]
        ans%=mod
    print(ans)
else:
    # 正,負,0で分ける
    b=[]
    c=[]
    d=[]
    for i in a:
        if i>0:
            b.append(i)
        elif i<0:
            c.append(i)
        else:
            d.append(i)
    b.sort(reverse=True)
    c.sort()

    if len(b)>0 and k-((len(b)//2)*2+len(b)%2)<=len(c):
        # 累積積
        b1=[1,b[0]]
        c1=[1,c[0]]
        x=b[0]
        y=c[0]
        for i in range(1,len(b)):
            x*=b[i]
            b1.append(x)
        for i in range(1,len(c)):
            y*=abs(c[i])
            c1.append(y)
        ans=0
        for i in range(len(b)):
            if i%2==0 and k-i<=len(c):
                cnt=b1[i+1]*c1[k-i-1]
                ans=max(cnt,ans)
        print(ans%mod)

    elif len(b)+len(c)<k:
        print(0)
    else:
        ans=1
        c.sort(reverse=True)
        for i in range(k):
            ans*=c[i]
        print(ans%mod)

Submission status: Runtime Error

Solution code:
def pf(a,n,p):
    bi = str(format(n,"b"))#2進表現に
    res = 1
    for i in range(len(bi)):
        res = (res*res) %p
        if bi[i] == "1":
            res = (res*a) %p
    return res

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

pos=[]
neg=[]
for a in A:
    if a<0:
        neg.append(abs(a))
    elif a>0:
        pos.append(a)
pos=pos[::-1]
ans=[1]
mod=10**9+7

if K>len(pos) and (K-len(pos))%2==1:
    ABS=[]
    for a in A:
        ABS.append(abs(a))
    ABS.sort()
    for j in range(K):
        ans.append(ans[-1]*ABS[j]%mod)
    ans.append(-1*ans[-1]) 
else:
    p=0
    n=0
    for k in range(K):
        if p<len(pos) and n<len(neg):
            if pos[p]>=neg[n]:
                ans.append(ans[-1]*pos[p]%mod)
                p+=1
            else:
                ans.append(ans[-1]*neg[n]%mod)
                n+=1
        elif p==len(pos) and n<len(neg):
            ans.append(ans[-1]*neg[n]%mod)
            n+=1
        elif n==len(neg) and p<len(pos):
            ans.append(ans[-1]*pos[p]%mod)
            p+=1
        else:
            ans.append(0)
            break
    if n%2==1:
        ans.append(ans[-1]*pos[p]*pf(neg[n-1],mod-2,mod))

print(ans[-1]%mod)

Submission status: Runtime Error

Solution code:
n,k = map(int, raw_input().split(' '))
ais = map(int , raw_input().split(' '))

pos = [ai for ai in ais if ai > 0]
pos.sort(key = lambda x:-x)
neg = [-ai for ai in ais if ai < 0]
neg.sort(key = lambda x:-x)

def g(neg, k):
	i = len(neg) -1
	r = 1
	while(k):
		r *= -neg[i]
		i -=1
		k -=1
		r %= (10 ** 9) + 7
	return r
def gg(pos, k):
	r =  1
	for i in range(k):
		r *= pos[i]
		r %= (10 **9) + 7
	return r
def f(pos,neg, k):

	if len(pos) == 0: return g(neg, k)
	if len(neg) <= 1: return gg(pos, k)

	i,j = 0,0
	p = 1
	mod = (10 ** 9) + 7
	while(k >= 2 and i < len(pos) and j+1 < len(neg)):
		if j + 1 < len(neg):
			if neg[j] * neg[j+1] > pos[i] * (pos[i+1] if i+1 < len(pos) else 1):
				p *= neg[j] * neg[j+1]
				p %= mod
				j += 2
				k -= 2
			else:
				p *= pos[i]
				i +=1
				p %= mod
				k -=1
	while(k and i < len(pos)):
		p *= pos[i]
		p %+ mod
		k-=1
		i+=1
	return p
print f(pos,neg, k) if k > 1 else max(ais)

Submission status: Runtime Error

Solution code:
import numpy as np

def kmaxprod_v2(data, k):
    if len(data) < k:
        return np.nan
    data = np.asanyarray(data)
    dfp = data.astype(object) if data.dtype in (
        int, np.int64, np.int32, np.int16, np.int8) else data
    if len(data) == k:
        return np.prod(dfp)
    neg = data <= 0
    if k % 2 == 1 and np.count_nonzero(neg) == len(data):
        return np.prod(-np.partition(-data, k)[:k].astype(dfp.dtype))
    ap = np.argpartition(-np.absolute(data), k)
    low, high = ap[k:], ap[:k]
    greedy = np.prod(dfp[high])
    if greedy >= 0:
        return greedy
    bpo = np.max(dfp[low])
    if bpo <= 0:
        spfn = 0
    else:
        neg_high = np.where(neg[high])[0]
        wni_ind = np.argmax(data[high[neg_high]])
        wni_ind = neg_high[wni_ind]
        spfn = bpo*np.prod(dfp[high[:wni_ind]])*np.prod(dfp[high[wni_ind+1:]])
    pos_high = np.where(~neg[high])[0]
    if len(pos_high) == 0:
        snfp = 0
    else:
        wpi_ind = np.argmin(data[high[pos_high]])
        wpi_ind = pos_high[wpi_ind]
        bno = np.min(dfp[low])
        snfp = bno*np.prod(dfp[high[:wpi_ind]])*np.prod(dfp[high[wpi_ind+1:]])
    return max(spfn, snfp)

n, k = input().split()
val = list(map(int, input().strip().split()))
print(kmaxprod_v2(val, int(k))%1000000007)

Submission status: Runtime Error

Solution code:
import sys
sys.stdin=open('input_file.txt')

#++++
N=10**9+7
n , k = map(int, input().split())
x = list(map(int,input().split()))
posi=[]
nega=[]
ze=[]
p=0
ne=0
z=0

for i in range(n):
    if x[i]<0:
        nega.append(x[i]*(-1))
        ze.append([x[i]*(-1),-1])
        ne+=1
    elif x[i]>0:
        posi.append(x[i])
        ze.append([x[i],1])
        p+=1
    else:
        z+=1

if n+p<k:
    print('0')
    exit

elif k%2==1 and p==0:
    if z>=1:
        print('0')
        exit()
    else:
        ans=1
        nega.sort()
        for i in range(k):
            ans*=nega[i]
            ans%=N
        print(ans*(-1)%N)
elif k%2==1:
    ze.sort(key = lambda x:x[0],reverse=True)
    ans=1
    fu=1
    for i in range(k):
        ans*=ze[i][0]
        fu*=ze[i][1]
        ans%=N
    if fu==-1:
        st=0
        gl=0
        for j in range(min(k,n-k)):
            if ze[k-j]<0 and st==0:
                st=k-j
            if ze[k+j]>0 and gl==0:
                gl=k+j
        for i in range(k):
            if i!=st:
                ans*=ze[i][0]
                ans%=N
            ans*=ze[gl]
            ans%=N
    print(ans)
else:
    ab=nega+posi
    ab.sort(reverse=True)
    ans=1
    for i in range(k):
        ans*=ab[i]
        ans%=N
    print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import numpy as np

m = 10 ** 9 + 7

A,B = map(int,input().split())
L = list(map(int, input().split()))
S = sorted(L,reverse=True)

v = np.prod(S[:B])

if v &m <= 10**9 + 6
  print(v%m)
else:
  print(100**9+6)

Submission status: Runtime Error

Solution code:
def readinput():
    n,k=int(input())
    a=list(map(int,input().split()))
    return n,k,a

def main(n,k,a):
    MOD=10**9+7
    a.sort(reverse=True)
    ans=1
    for i in range(k):
        ans=ans*a[i]%MOD
    return ans

if __name__=='__main__':
    n,k,a=readinput()
    ans=main(n,k,a)
    print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=sorted(a)[::-1]
x = []
x = 1
if k % 2 == 0:
    for i in b:
        if i < 0:
            c.append(i * -1)
        else:
            c.append(i)
    c = sorted(c)[::-1]
    for i in range(k):
        x *= c[i]
else:
    for i in range(k):
    x *= b[i]
print(x % 1000000007)

Submission status: Runtime Error

Solution code:
n,k=map(int, input().split())
a=list(map(int, input().split()))
a.sort()
ans=1
seki=[]
sei=0
fu=0
flag=0
minus=0
for x in a:
    if x==0:
        a.remove(0)
    if x>0:
        sei+=1
    if x<0:
        fu+=1
if k>len(a):
    flag=1
elif (k>sei) and (fu%2==1):
    if flag==1:
        ans=0
    else:
        ans=1
        for x in range(a):
            ans=(ans*x)%1000000007
else:
    for x in range(k):
        print(max(abs(a[0]),abs(a[-1])))
        ans=(ans*max(abs(a[0]),abs(a[-1])))%1000000007
        if max(abs(a[0]),abs(a[-1]))==-a[0]:
            seki.append(a[0])
            dame=a[0]
            a.remove(a[0])
            
            minus=abs(minus-1)
        else:
            seki.append(a[-1])
            
            a.remove(a[-1])
  
if minus==1:
    ans=ans//dame
    if a[-1]>0:
        ans=(ans*ans[-1])%100000007
    else:
        if flag==1:
            ans=0
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = sorted(list(map(int, input().split())))
p = (N + 1) // 2
B = [0 for _ in range(N)]

for i, a in enumerate(A[:p]):
    B[2 * i] = a

for i, a in enumerate(A[p:]):
    B[2 * i + 1] = a

cnt = 0
B.append(B[0])
B = [B[-2]] + B

for i in range(1, len(B) - 1):
    if B[i - 1] > B[i + 1]:
        cnt += B[i + 1]
    else:
        cnt += B[i - 1]

print(cnt)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())

A = list(map(int,input().split()))
#cumsum = [0]*(N+1)
mod = pow(10,9)+7

A.sort()
counter = 1
if A[N-1]=<0:
    for i in range(K):
        counter *= A[i]
        print(counter %mod)
        exit()
if K%2==0:
    num = K//2
    right = N
    left = -1
    for k in range(num):
        a = A[left+1]*A[left+2]
        b = A[right-1]*A[right-2]
        if a>=b:
            counter *= a
            left +=2
        else:
            counter *= b
            right -=2
    print(counter%mod)
else:
    num = K//2
    right = N
    left = -1
    for k in range(num):
        a = A[left+1]*A[left+2]
        b = A[right-1]*A[right-2]
        if a>=b:
            counter *= a
            left +=2
        else:
            counter *= b
            right -=2
    ok = counter * A[right-1]
    print(ok%mod)

Submission status: Runtime Error

Solution code:
n , k = map(int, input().split())
a = list(map(int, input().split()))
neg = []
pos = []
mod = 10**9+7
for i in range(n):
    if a[i] >= 0:
        pos.append(a[i])
    else:
        neg.append(a[i])
neg.sort()
pos.sort(reverse = True)
min_num = -1
good_num = -1
if k % 2 == 1 and len(pos) == 0:
    ans = 1
    neg.sort(reverse = True)
    for i in range(k):
        ans *= neg[i]
        ans %= mod
    print(ans % mod)
else:
    #最大化するとき
    for i in range(0,k+1,2):
        #i個だけnegから取る。
        if i > len(neg) or k - i > len(pos):
            continue
        m = 0
        if i - 1 >= 0:
            m = abs(neg[i-1])
        if k-i-1 >= 0:
            m = min(m, pos[k-i-1])
        if m > min_num:
            min_num = m
            good_num = i
    ans = 1
    for i in range(good_num):
        ans *= neg[i]
        ans %= mod
    for i in range(k - good_num):
        ans *= pos[i]
        ans %= mod
    print(ans % mod)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
a.sort(key=lambda x:-x if x<0 else x,reverse=True)
p=1
for i in range(k-1):
  p*=a[i]
for i in a[k:]:
  if p<0 and i<0:
    p*=i
    break
  if p>=0 and i>=0:
    p*=i
    break
else:
  p=1
  for i in range(a[:-k]):
    p*=i
print(p%(10**9+7))

Submission status: Runtime Error

Solution code:
def maxProductSubarrayOfSizeK(A, n, k): 
  
    MOD = 10**9+7
    A.sort()   
    product = 1
    
    if A[k-1] < 0 and k%2 ==1:
        temp = A[:A.index(0)]
        temp.sort(reverse=True)
        for i in range(k):
            product *= temp[i]
            product %= MOD
        
  
    if (A[n - 1] == 0 and (k & 1)): 
        return 0
  
    if (A[n - 1] <= 0 and (k & 1)) : 
        for i in range(n - 1, n - k + 1, -1): 
            product *= A[i] 
            product %= MOD
        return product 
  
    i = 0  
    j = n - 1
  
    if (k & 1): 
        product *= A[j] 
        j-= 1
        k-=1
  
    k >>= 1
  
    for itr in range( k) : 
  
        left_product = A[i] * A[i + 1] 
  
        right_product = A[j] * A[j - 1] 
  
        if (left_product > right_product) : 
            product *= left_product 
            product %= MOD
            i += 2
          
        else : 
            product *= right_product 
            product %= MOD
            j -= 2
  
    return product

N,K = map(int,input().split())
A = list(map(int,input().split()))
print((maxProductSubarrayOfSizeK(A, N, K))% MOD)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
m = 10**9+7
arr = list(map(int, input().split()))
arr.sort()

if arr[-1] == 0 and k % 2:
	print(0)
else:
	if arr[-1] < 0 and k % 2:
		p = 1
		while k:
			p = (p * abs(arr[n - 1])) % m
			k -= 1
			n -= 1
		print(p)
	else:
		neg = []
		pos = []
		for i in arr:
			if i < 0:
				neg += [abs(i)]
			else:
				pos += [i]
		
		if arr[-1] > 0 and k % 2:
			arr2 = neg + pos[:-1]
			arr2.sort()
			p = 1
			while k > 1:
				p = (p * abs(arr2[n - 1])) % m
				k -= 1
				n -= 1
			print((p * pos[-1]) % m)
		else:
			arr2 = neg + pos
			arr2.sort()
			p = 1
			while k:
				p = (p * abs(arr2[n - 1])) % m
				k -= 1
				n -= 1
			print(p)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split(" "))
mlist = list(map(int, input().split(" ")))
unsortedlist = sorted(mlist, key=lambda x: abs(x))
rresult = 1
for i in range(k):
  rresult *= unsortedlist[i]%(10**9+7)
sortedlist = unsortedlist[::-1]
plus = []
minus = []
zero = []
for i in sortedlist:
  if i > 0:
    plus.append(i)
  elif i < 0:
    minus.append(i)
  else:
    zero.append(i)

used_list = []    
used_plus = []
used_minus = []
used_zero = []

result = 1
for i in range(k):
  a = sortedlist[0]
  result *= a
  sortedlist.remove(a)
  used_list.append(a)
  if a > 0:
    plus.remove(a)
    used_plus.append(a)
  elif a == 0:
    zero.remove(a)
    used_zero.append(a)
  elif a < 0:
    minus.remove(a)
    used_minus.append(a)
if result >= 0:
  print(result%(10**9+7))
elif used_list[-1] >0:
  b = 0
  c = 0
  if minus:
    b = minus[0]*result/used_list[-1]
  if plus and used_minus:
    c = plus[0]*result/used_minus[-1]
  if b == 0 and c == 0:
    print(rresult%(10**9+7))
  else:
    print(max(b,c)%(10**9+7))
elif used_list[-1] <0:
  d = 0
  e =  0
  if plus:
    d = plus[0]*result/used_list[-1]
  if minus and used_plus:
    e = minus[0]*result/used_list[-1]
  if d == 0 and e == 0:
    print(rresult%(10**9+7))
  else:
    print(max(d,e)%(10**9+7))