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: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
m,p=[],[]
pr=10**9+7
for i in A:
    if i>=0:
        p.append(i)
    else:
        m.append(i)
lm=len(m)
m.sort()
p.sort(reverse=1)
lp=len(p)
if N==K:
    a=1
    for i in A:
        a=a*i%pr
    print(a%pr)
    exit()
mm,pp=[],[]
for i in range(1,lm,2):
    mm.append((m[i-1]*m[i]%pr,0,i))
for i in range(1,lp,2):
    pp.append((p[i-1]*p[i]%pr,1,i))
l=mm+pp
l.sort(reverse=1)
def gya(n):
    return pow(n,pr-2,pr)
mt,pt=-1,-1
a=1
if len(l)*2>=K:
    for i in l[:K//2]:
        a=a*i[0]%pr
        if i[1]:
            pt=i[2]
        else:
            mt=i[2]
    if K%2:
        if pt<lp-1:
            a=a*p[pt+1]%pr
        elif pt==-1:
            a=1
            m=m[::-1]
            for j in m[:K]:
                a=a*j%pr
        else:
            a=a*gya(p[pt])*m[mt+1]*m[mt+2]%pr
else:
    for i in l:
        a=a*i[0]%pr
        if i[1]:
            pt=i[2]
        else:
            mt=i[2]
    a=a*p[pt+1]%pr
print(a%pr)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
MOD=10**9+7

def futuuni(List,mi=False):
  res=1
  for i in range(K):
    dat=List[i]
    if mi:
      dat = -dat
    if dat<0:
      dat+=MOD
    res*=(dat%MOD)
    res%=MOD
  print(res)
  exit() 

plus=[]
minas=[]
zero=0
A=list(map(int,input().split()))
for a in A:
  if a>0:
    plus.append(a)
  elif a<0:
    minas.append(a)
  else:
    zero+=1
plus.sort(reverse=True)
minas.sort()
lp=len(plus)
lm=len(minas)
if lp+lm<K:
  print(0)
#膨大な例外処理
if N==K:
  futuuni(A)
if lp+lm<K:
  print(0)
  exit()
if lp==0:
  if K%2==0:
    futuuni(minas)
  else:
    if zero>0:
      print(0)
      exit()
    minas.sort(reverse=True)
    futuuni(minas)
if lm==0:
  futuuni(plus)
#例外処理ここまで

if K%2==0:
  pp=[plus[i]*plus[i-1] for i in range(1,lp,2)]
else:
  pp=[plus[i]*plus[i-1] for i in range(2,lp,2)]
mm=[minas[i]*minas[i-1] for i in range(1,lm,2)]
allpp=len(pp)
allpp_m=K//2-allpp
if K//2<=len(pp):
  allpp=K//2
  allpp_m=0


  
ind=allpp

for i in range(allpp):
  if pp[allpp-1-i]>mm[allpp_m+i]:
    ind=i
    break
  if allpp_m+i+1==len(mm):
    ind=i
    break  
ans=1
if K%2==1:
  ans*=plus[0]

for i in range(allpp_m+ind):
  ans*=((mm[i]+MOD)%MOD)
  ans%=MOD
for i in range(allpp-ind):
  ans*=(pp[i]%MOD)
  ans%=MOD
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    if K == 1:
        return max(A) % (10 ** 9 + 7)
    
    dp = [[(1, 1) for j in range(N + 1)] for i in range(K + 1)]
    for i in range(N):
        dp[0][i] = (A[i], A[i])
        dp[1][i] = (A[i], A[i])


    for i in range(1, len(A)):
        mink = min(i, K)
        for k in range(1, mink):
            max_tmp_val, min_tmp_val = dp[k - 1][i - 1]
            max_tmp, min_tmp = dp[k][i - 1]
            max_val = max(max_tmp, max_tmp_val * A[i], min_tmp_val * A[i])
            min_val= min(min_tmp, max_tmp_val * A[i], min_tmp_val * A[i])
            dp[k][i] = (max_val, min_val)
    
    ans = dp[K][0][0]
    for i in range(len(dp[K])):
        maxval, minval = dp[K-1][i]
        ans = max(ans, maxval, minval)
    return ans % (10 ** 9 + 7)

if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
# from operator import itemgetter
sysread = sys.stdin.readline
read = sys.stdin.read
sys.setrecursionlimit(10 ** 7)
from heapq import heappop, heappush
#from collections import OrderedDict, defaultdict
#import math
#from itertools import product, accumulate, combinations, product
#import bisect# lower_bound etc
#import numpy as np
#from copy import deepcopy
#from collections import deque
#import numba

def run():
    N, K = list(map(int, input().split()))
    A = list(map(int, sysread().split()))
    mod = 10 ** 9 + 7
    INF = mod

    plus = [a for a in A if a >= 0]
    minus = [-a for a in A if a < 0]

    plus = sorted(plus, reverse=True)
    minus = sorted(minus, reverse=True)

    P, M  = [1], [1]
    for p in plus:
        P.append((P[-1] * p) % mod)
    for i, m in enumerate(minus):
        M.append(((-1) ** (i+1) * (M[-1] * m) % mod))
    #print(P, M)


    max_ans = -1
    for i in range(0, K+1, 2):
        if i >= len(M) or K - i >= len(P):continue
        ans = M[i] * P[K-i] % mod
        if max_ans < ans:
            max_ans = ans
        #else:
        #    break

    if max_ans == -1:
        P, M = [1], [1]
        for p in plus[::-1]:
            P.append((P[-1] * p) % mod)
        for m in minus[::-1]:
            M.append(((-1) ** (i+1)) * (M[-1] * m) % mod)
        #print(P, M)

        for i in range(1, K + 1, 2):
            #print(i)
            if i >= len(M) or K - i >= len(P): continue
            ans = (-M[i] * P[K - i]) % mod
            #print(ans)
            if max_ans < ans:
                max_ans = ans

    print(max_ans)






if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
input=sys.stdin.readline

def fact(arr):
  ans = 1
  for s in arr:
    ans *= s
    ans = ans % (10e9+6)
  return ans

def main():
  N, K = map(int, input().split(' '))
  A = list(map(int, input().split(' ')))
  Aplus = []
  Aminus = []
  for a in A:
    if(a >= 0):
      Aplus.append(a)
    else:
      Aminus.append(a)
  Aplus= sorted(Aplus, reverse=True)
  Aminus= sorted(Aminus)
  Aabs = sorted(A, key=lambda x:abs(x), reverse=True)
  plusnum=0
  minusnum=0
  for i in range(K):
    if(Aabs[i] >= 0):
      plusnum+=1
    else:
      minusnum += 1
  if(minusnum % 2 == 1):
    if(minusnum > 0 and plusnum > 0):
      if minusnum < len(Aminus):
        if (plusnum < len(Aplus)):
          if(abs(Aplus[plusnum] * Aminus[minusnum - 2]) > abs(Aplus[plusnum - 2] * Aminus[minusnum])):
            plusnum+=1
            minusnum-=1
          else:
            plusnum-=1
            minusnum+=1
        else:
          plusnum-=1
          minusnum+=1
      else:
        plusnum+=1
        minusnum-=1
  if(plusnum == 0):
    print(int(fact(Aminus[::-1][:minusnum])))
  else:
    print(int(fact(Aplus[:plusnum]+Aminus[:minusnum])))

if __name__=='__main__':
  main()

Submission status: Wrong Answer

Solution code:
import numpy as np

N, K = map(int, input().split())
A = list(map(int, input().split()))
mod = int(1e9+7)

A = np.array(sorted(A, reverse=True, key=lambda x: abs(x)))
if sum(np.sign(A)) == -N and K%2 == 1:
    A = sorted(A, reverse=True)
last_p = 0
last_m = 0
ans = 1
for i in range(N):
    ans = (ans * A[i])%mod
    if A[i] > 0:
        last_p = i
    else:
        last_m = i
    if i == K-1:
        if ans > 0:
            break
        # else:

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
MOD = 10 ** 9 + 7

if N - A.count(0) < K:
    print(0)
    exit(0)

plus = []
minus = []
for a in A:
    if a > 0:
        plus.append(a)
    elif a < 0:
        minus.append(a)
    else:
        plus.append(a)
        minus.append(a)


ans = 1
if K % 2 == 1 and len(plus) == 0:
    minus.sort(reverse=True)
    for i in range(K):
        ans *= minus[i]
        ans %= MOD
    print(ans)
    exit(0)

plus.sort(reverse=True)
minus.sort()
minus.append(0)
minus.append(0)

i = 0
j = 0

if K % 2 == 1 and len(plus) > 0:
    ans = plus[0]
    K -= 1
    plus = plus[1:]

for _ in range(K//2):
    if i+1 >= len(plus):
        ans *= (minus[j] * minus[j+1])
        ans %= MOD
        j += 2
        continue
    elif j+1 >= len(minus):
        ans *= (plus[i] * plus[i+1])
        ans %= MOD
        i += 2
        continue

    a = plus[i] * plus[i+1]
    b = minus[j] * minus[j+1]
    if a > b:
        ans *= a
        i += 2
    else:
        ans *= b
        j += 2
    ans %= MOD

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input('').split(' '))
a=list(map(int,input('').split(' ')))
o=1
for i in range(k):
    o=o*a[i]
print(o)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split(' '))
A = sorted(list(map(int, input().split(' '))))
mod = 10 ** 9 + 7
p = 0
for i in range(N):
    if A[i] >= 0:
        break
p = i
if (p == N - 1 and K % 2 != 0):
    ans = 1
    for i in range(K):
        ans *= A[-1 - i]
        ans %= mod
    print(ans%mod)
    exit()
minus = A[:p]
plus = sorted(A[p:],reverse=True)
print(minus)
print(plus)
len_m = len(minus)
len_p = len(plus)
m = 0
p = 0
ans = 1
while K > 0:
    if K >= 2 and m <= len_m - 2 and p <= len_p - 2:
        if minus[m] * minus[m+1] >= plus[p] * plus[p + 1]:
            ans *= minus[m] * minus[m + 1]
            ans %= mod
            m += 2
        else:
            ans *= plus[p] * plus[p + 1]
            ans %= mod
            p += 2
        K -= 2
    elif K >= 2 and m <= len_m - 2:
        ans *= minus[m] * minus[m + 1]
        ans %= mod
        m += 2
        K -= 2
    elif K >= 2 and p <= len_p - 2:
        ans *= plus[p] * plus[p + 1]
        ans %= mod
        p += 2
        K -= 2
    elif p <= len_p - 1:
        ans *= plus[p]
        ans %= mod
        p += 1
        K -= 1
    else:
        ans *= minus[m]
        ans %= mod
        m += 1
        K -= 1
    print(ans)
print(ans%mod)

Submission status: Wrong Answer

Solution code:
mod = 10**9+7
N,K = map(int,input().split())
A = list(map(int,input().split()))
A.sort(key=lambda x:-abs(x))
p = []
n = []
for i in range(K):
    a = A[i]
    if a>0:
        p.append(a)
    elif a<0:
        n.append(a)

if len(n)%2==0:
    ans = 1
    for a in n:
        ans *= a
        ans %= mod
    for a in p:
        ans *= a
        ans %= mod
else:
    ap = 0
    an = 0
    for i in range(K,N):
        if ap==0 and A[i]>0:
            ap = A[i]
        if an==0 and A[i]<0:
            an = -A[i]
    if len(p)>0:
        ap_ = p[-1]
        an_ = -n[-1]
        if ap==0 and an==0:
            ans = 0
        else:
            if ap*ap_<an*an_:
                n.pop()
                p.append(ap)
            else:
                p.pop()
                n.append(-an)
            ans = 1
            for a in n:
                ans *= a
                ans %= mod
            for a in p:
                ans *= a
                ans %= mod
    else:
        ans = 1
        for a in n:
            ans *= a
            ans %= mod
if len(p)+len(n)<K:
    ans = 0
if len(p)==0 and len(n)==K and K%2==1:
    An = [A[i] for i in range(N) if A[i]<0]
    An.sort()
    An = An[::-1]
    ans = 1
    for i in range(K):
        ans *= An[i]
        ans %= mod
    if A[-1]==0:
        ans = 0
if N==K:
    ans = 1
    for i in range(K):
        ans *= A[i]
        ans %= mod
print(ans%mod)

Submission status: Wrong Answer

Solution code:
from collections import deque
def main():
    N, K = map(int, input().split())
    A = deque(sorted(list(map(int, input().split()))))
    max = 0
    for i in range(0, K, 2):
        j = K-i
        left = 1
        right = 1
        for i2 in range(i):
            left = left*A[i]
        for j2 in range(j):
            right = right*A[-1*j]
        if max < right*left:
            max = right*left
    print(max)
            
            
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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

a.sort()

from bisect import bisect_left, bisect_right
zerostart = bisect_left(a, 0)
posstart = bisect_right(a, 0)

aneg = a[:zerostart]
cntzero = posstart - zerostart
apos = a[posstart:]

cntneg = len(aneg)
cntpos = len(apos)

# 答えが0になる時
if k > n-cntzero:
    print(0)
    exit()

cntneg = len(aneg)
cntpos = len(apos)

mix = [(-neg,-1) for neg in aneg] + [(pos,1) for pos in apos]
mix.sort(key=lambda x:x[0],reverse=True)


# 答えが正になる時
if k <= cntpos or (k%2==0 and  k <= (cntneg//2)*2 + cntpos) or (k%2==1 and cntpos%2==1 and k <= (cntneg//2)*2 + cntpos-1):
    pos_pair_mix=[]
    apos.sort(reverse=True)
    for i in range(cntpos//2):
        pos_pair_mix.append((apos[2*i]*apos[2*i+1],+1,i))

    for i in range(cntneg//2):
        pos_pair_mix.append((aneg[2*i]*aneg[2*i+1],-1,i))

    pos_pair_mix.sort(key=lambda x:x[0],reverse=True)

    ans = 1
    last_apos=0

    for i in range(k//2):
        ans*=pos_pair_mix[i][0]%p
        ans%=p
        if pos_pair_mix[i][1]==1:
            last_apos = pos_pair_mix[i][2]+1
    if k%2==0:
        print(ans%p)
        exit()
    else:
        print((ans*apos[last_apos])%p)
        exit()

# 答えが負になる時
mix = [(neg,-neg,-1) for neg in aneg] + [(pos,pos,1) for pos in apos]
mix.sort(key=lambda x:x[1])

ans = 1
for i in range(k-cntzero):
    ans*=mix[i][0]
    ans%=p

if ans <0 and cntzero>0:
    ans = 0

print(ans%p)

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    if N == K:
        ans = 1
        for a in A:
            ans *= a
            ans %= mod
        return ans

    neg = []
    pos = []
    for a in A:
        if a >= 0:
            pos.append(a)
        elif a < 0:
            neg.append(a)
    pos.sort()
    neg.sort(reverse=True)

    if len(neg) == N:
        res = 1
        for x in neg[:K]:
            res *= x
            res %= mod
        return res

    ans = 1

    if K & 1:
        ans *= pos.pop()
        K -= 1
    # この時点でKは偶数
    while K > 1 and len(pos) > 1 and len(neg) > 1:
        K -= 2
        x = 1
        if pos[-1] * pos[-2] > neg[-1] * neg[-2]:
            ans *= pos.pop()
            ans *= pos.pop()
            ans %= mod
        else:
            ans *= neg.pop()
            ans *= neg.pop()
            ans %= mod
    while K > 1 and len(pos) > 1:
        K -= 2
        ans *= pos.pop()
        ans %= mod
        ans *= pos.pop()
        ans %= mod
    while K > 1 and len(neg) > 1:
        K -= 2
        ans *= neg.pop()
        ans %= mod
        ans *= neg.pop()
        ans %= mod
    while K and pos:
        ans *= pos.pop()
        ans %= mod
        K -= 1
    while K and neg:
        ans *= neg.pop()
        ans %= mod
        K -= 1
    return ans % mod


if __name__ == "__main__":
    print(main())

Submission status: Wrong Answer

Solution code:
def solve():
    n, k = map(int, input().split())
    array = [int(s) for s in input().split()]

    MOD = 10 ** 9 + 7
    array.sort(reverse=True)
    array_r = array[::-1]
    res = 1

    if n == k:
        for m in array:
            res = res * m % MOD
        return res

    if array[0] <=0:
        for i in range(k):
            res = res * array[i] % MOD
        return res

    if array[k-1] >= 0:
        pos = array[:k]
        i = 0
        while i < k-1 and array_r[i] * array_r[i+1] > pos[-1] * pos[-2]:
            res = res * array_r[i] * array_r[i+1] % MOD
            pos.pop()
            pos.pop()
            i += 2
        for m in pos:
            res = res * m % MOD
        return res

    i = 0
    pos = []
    while array[i] >= 0:
        pos.append(array[i])
    if (k - len(pos)) % 2:
        pos.pop()
    for i in range(k - len(pos)):
        res = res * array_r[i] % MOD
    array_r = array_r[k-len(pos):]
    i = 0
    while pos and array_r[i] * array_r[i+1] > pos[-1] * pos[-2]:
        res = res * array_r[i] * array_r[i + 1] % MOD
        pos.pop()
        pos.pop()
        i += 2
    for m in pos:
        res = res * m % MOD
    return res

print(solve())

Submission status: Wrong Answer

Solution code:
def hugo(k):
  h=0
  for i in k:
    if i>=0:
      h+=1
  if h==0:
    return True
  else:
    return False
n,k=map(int,input().split())
mod=10**9+7
a=list(map(int,input().split()))
s=sorted(a,key=abs)
s.reverse()
e=sorted(a)
e.reverse()
a=list(map(abs,a))
a.sort()
a.reverse()
x=1
o=0
m=1
if n==k:
  for t in e:
    x*=t
    x%=mod
  print(x)
else:
  if k%2==1 and hugo(s):
    s.reverse()
    if k>1:
      for i in s[0:k]:
        m*=i
        o+=1
        if o%2==0:
          x=(x*m)%mod
          m=1
      x*=m
      x%=mod
      print(x) 
    else:
      print(s[0]%mod)
  else:
    if k>1:
      for i in s[0:k-1]:
        if i>=0:
          x=(x*i)%mod
        else:
          m*=i
          o+=1
          if o%2==0:
            x=(x*m)%mod
            m=1
      if m<0:
        y=0
        z=1
        l=0
        for i in s[k-1:]:
          if i<0:
            y=i
            break
        for i in s[k-1:]:
          if i>=0:
            z*=i
            l+=1
            if l==2:
              break
        m*=y
        if z>=m:
          x=(x*z)%mod
        else:
          x=(x*m)%mod
      else:
        for i in s[k-1:]:
          if i>=0:
            x*=i
            break
      print(x%mod)
    else:
      for i in s:
        if i>=0:
          print(i)
          break

Submission status: Wrong Answer

Solution code:
def main():

    N, K = map(int, input().split())
    A = sorted([int(i) for i in input().split()])
    MOD = 10 ** 9 + 7
    ans = 0

    start = 0
    end = N - 1
    k, m = divmod(K, 2)
    for i in range(k):
        start_sum = A[start] * A[start + 1]
        end_sum = A[end] * A[end - 1]
        # print(start_sum, end_sum, k)
        if start_sum >= end_sum:
            ans += start_sum % MOD
            ans %= MOD
            start += 2
        else:
            ans += end_sum % MOD
            ans %= MOD
            end -= 2

    if m:
        ans += A[start]

    if ans < 0:
        ans += MOD
    else:
        ans %= MOD

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
MOD = 10**9 + 7
INF = 10**10
N ,K = map(int, input().split())
A = list(map(int, input().split()))

A.sort()

def get_pos_num():
    lb = -1
    ub = N

    while ub - lb > 1:
        mid = (ub + lb) // 2
        if A[mid] > 0:
            ub = mid
        else:
            lb = mid

    return ub

def get_neg_num():
    lb = -1
    ub = N

    while ub - lb > 1:
        mid = (ub + lb) // 2
        if A[mid] >= 0:
            ub = mid
        else:
            lb = mid

    return lb

pos_num = get_pos_num()
neg_num = get_neg_num()
# A[0], A[1], ... , A[neg_num] <- negative number
# A[neg_num + 1], ..., A[pos_num - 1] <- 0
# A[pos_num], ..., A[N-1] <- positive number

neg_ind = 0
pos_ind = N-1
ans = 1

if (N-1) - neg_ind > K or K % 2 == 0:
    while K > 0:
        if K >= 2:
            if pos_ind - 1< pos_num:
                pos_tmp = -1*INF
            else:
                pos_tmp = A[pos_ind]*A[pos_ind-1]

            if neg_ind + 1 > neg_num:
                neg_tmp = -1 * INF
            else:
                neg_tmp = A[neg_ind]*A[neg_ind+1]

            if pos_tmp == -1*INF and neg_tmp == -1*INF:
                ans = 0
                K = 0
                
            elif pos_tmp < neg_tmp:
                neg_ind += 2
                ans = (ans * neg_tmp + MOD) % MOD

                # ans *= neg_tmp
                K -= 2
            else:
                pos_ind -= 2
                ans = (ans * pos_tmp + MOD) % MOD

                # ans *= pos_tmp
                K -= 2
        
        else:
            ans = (ans * A[pos_ind] + MOD) % MOD
            # ans *= A[pos_ind]
            K -= 1
else:
    for i in range(K):
        ans = (ans * A[N-1-i] + MOD) % MOD
        # ans *= A[N-1-i]

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
a=sorted(a)
# print(a)
mai=0
for i in range(n):
    if(a[i]<0):
        mai+=1
    else:
        break
plus=n-mai
mod=pow(10,9)+7
if(plus==0): #負のみ
    if(k%2==0): #負から偶数個選ぶ
        ans=1
        for i in range(k): 
            ans*=a[i]
            ans%=mod
        print(ans)
    else:
        ans=1
        for i in range(k): 
            ans*=a[-(i+1)]
            ans%=mod
        print(ans)
else:
    if((k-plus//2*2-mai//2*2)==1 and plus%2==1): 
        ans=1
        for i in range(plus):
            ans*=a[-(i+1)]
            ans%=mod
        for i in range(k-plus):
            ans*=a[i]
            ans%=mod
        print(ans)
    elif((k-plus//2*2-mai//2*2)==1 and plus%2==0 and mai%2==1):
        ans=1
        for i in range(plus):
            ans*=a[-(i+1)]
            ans%=mod
        for i in range(k-plus):
            ans*=a[-plus-(i+1)]
            ans%=mod
        print(ans)
    elif((k-plus//2*2-mai//2*2)==2):
        ans=1
        for i in range(k):
            ans*=a[-(i+1)]
            ans%=mod
        print(ans)
    else:
        mp=0
        pp=-1
        # for i in range(k):

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

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

mod = 10 ** 9 + 7

A_abs = sorted(A, key=lambda x: abs(x))

if K >= 3:
    ans = 1
    for a in A_abs[:K-2]:
        ans *= a
        ans %= mod

    # ++ or --
    if ans > 0:
        A_rest_abs = A_abs[K-2:]
        A_rest = sorted(A_abs)
        ans *= max(A_rest[-2] * A_rest[-1], A_rest[0] * A_rest[1])
        ans %= mod
    # +-
    else:
        A_rest_abs = A_abs[K-2:]
        A_rest = sorted(A_abs)
        ans *= A_rest[-1] * A_rest[0]
        ans %= mod

else:
    A_rest = sorted(A_abs)
    ans = max(A_rest[-2] * A_rest[-1], A_rest[0] * A_rest[1])


print(ans)

Submission status: Wrong Answer

Solution code:
N = list(map(int, input().split()))
A = list(map(int, input().split()))
K = N[1]

A_rezero = [i for i in A if i != 0]
if len(A_rezero) < K:
    print(0)
else:
    A_sorted = sorted(A_rezero)
    inint = A_sorted[0]
    for i in range(1,K):
        inint=inint*A_sorted[i]

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

Submission status: Wrong Answer

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

if n == k:
    ans = 1
    for x in a:
        ans *= x
    print(ans)
    exit()

p = []
m = []
zero = 0

for x in a:
    if x == 0:
        zero += 1
    if x > 0:
        p.append(x)
    else:
        m.append(x)

if len(p) + len(m) < k:
    print(0)
    exit()


if len(p) % 2 == 0 and len(m) % 2 == 1: 
    p.sort()
    m.sort(reverse=True)
    
    sp = []
    sm = []
    for i in range(1, len(p), 2):
        sp.append(p[i] * p[i-1])
    
    for i in range(1, len(m), 2):
        sm.append(m[i] * m[i-1])
    
    
    ans = 1
    while k > 1:
        k -= 2
        if sp == []:
            ans *= sm[0]
            sm.pop(0)
            m.pop(-1)
            m.pop(-1)
        elif sm == []:
            ans *= sp[0]
            sp.pop(0)
            p.pop(-1)
            p.pop(-1)
        elif sp[0] > sm[0]:
            ans *= sp[0]
            sp.pop(0)
            p.pop(-1)
            p.pop(-1)
        else:
            ans *= sm[0]
            sm.pop(0)
            m.pop(-1)
            m.pop(-1)
    
    ans *= m[-1]

else: 
    p.sort(reverse=True)
    m.sort()
    
    sp = []
    sm = []
    for i in range(1, len(p), 2):
        sp.append(p[i] * p[i-1])
    
    for i in range(1, len(m), 2):
        sm.append(m[i] * m[i-1])
    
    
    ans = 1
    while k > 1:
        k -= 2
        if sp == []:
            ans *= sm[0]
            sm.pop(0)
            m.pop(-1)
            m.pop(-1)
        elif sm == []:
            ans *= sp[0]
            sp.pop(0)
            p.pop(-1)
            p.pop(-1)
        elif sp[0] > sm[0]:
            ans *= sp[0]
            sp.pop(0)
            p.pop(-1)
            p.pop(-1)
        else:
            ans *= sm[0]
            sm.pop(0)
            m.pop(-1)
            m.pop(-1)
    
    if k == 1:
        if p != []:
            ans *= p[0]
        elif zero > 0:
            ans = 0
        else:
            ans *= m[0]
    
    print(ans % (10**9 + 7))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


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

B = A[:K]
ans = 1
m = 1000000007
for i in range(len(B)):
    ans *= B[i]%m
print(ans%m)

Submission status: Wrong Answer

Solution code:
from heapq import heapify, heappop, heappush
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import statistics # Pythonのみ
# import string
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    MOD=10**9+7
    n,k=map(int,input().split())
    A=list(map(int,input().split()))
    Q=[]
    q=[]
    flag=True
    minus=0
    plus=0
    for i in range(n):
        if A[i]<0:
            minus+=1
        elif A[i]>0:
            plus+=1

    if plus>=k:
        pass
    else:
        num=k-plus
        if num%2==0 and minus//2>=num:
            pass
        else:
            flag=False

    if flag:
        for i in A:
            if i>0:
                heappush(Q,-i)
            else:
                heappush(q,i)
        

        while len(q)>=1:
            pv=heappop(q)
            heappush(Q,pv)
        ans=1
        for i in range(k):
            pv=-heappop(Q)
            ans=(ans*pv)%MOD

    else:
        for i in A:
            if i>0:
                heappush(Q,i)
            else:
                heappush(q,-i)
        

        while len(q)>=1:
            pv=-heappop(q)
            heappush(Q,-pv)
        ans=1
        for i in range(k):
            pv=heappop(Q)
            ans=(ans*pv)%MOD
        ans=-ans
        


    
    print(ans%MOD)
    
resolve()

Submission status: Wrong Answer

Solution code:
import sys, math,os
from io import BytesIO, IOBase
#data = BytesIO(os.read(0,os.fstat(0).st_size)).readline
# from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter
# from itertools import permutations,combinations
from decimal import Decimal
def data(): return sys.stdin.readline().strip()
def mdata(): return list(map(int, data().split()))
def outl(var): sys.stdout.write(' '.join(map(str, var)) + '\n')
def out(var): sys.stdout.write(str(var) + '\n')
#sys.setrecursionlimit(100000 + 1)
INF = 10**9
mod = 10**9 + 7


n,k=mdata()
a=mdata()
n1,p=-1,-1
a1=[(abs(a[i]),a[i]) for i in range(n)]
a1.sort(reverse=True)
ans=1
c=1
for i in range(k):
    if a1[i][1]>0:
        p=i
    elif a1[i][1]<0:
        n1=i
        c*=-1
    else:
        c=0
    ans=(ans*a1[i][1])%mod
if c>=0:
    out(ans%mod)
else:
    b1=0
    b2=0
    for i in range(k,n):
        if a1[i][1]>=0 and b1==0:
            u1,v1=a1[n1][1],a1[i][1]
            b1=1
            c=1
        if a1[i][1]<0 and p!=-1 and b2==0:
            u2,v2=a1[p][1],a1[i][1]
            b2=1
            c=1
    if c<0:
        ans=1
        for i in range(k):
            ans = (ans*a1[n-i-1][1])%mod
    else:
        if b1==1 and b2==1:
            if Decimal(v1/u1)>Decimal(v2/u2):
                ans=ans*pow(u1,mod-2,mod)*v1
            else:
                ans = ans * pow(u2, mod - 2, mod) * v2
        elif b1==1:
            ans = ans * pow(u1, mod - 2, mod) * v1
        else:
            ans = ans * pow(u2, mod - 2, mod) * v2
    out(ans%mod)

Submission status: Wrong Answer

Solution code:
#!python3

import numpy as np

LI = lambda: list(map(int, input().split()))

# input
N, K = LI()
A = np.array(LI())

MOD = 10 ** 9 + 7


def main():
    if np.count_nonzero(A == 0) > N - K:
        print(0)
        return
    m = A[A < 0] * -1
    p = A[A > 0]
    m = np.sort(m)[::-1]
    p = np.sort(p)[::-1]
    mn = len(m)
    pn = len(p)
    i, j = 0, 0
    for _ in range(K):
        if i >= mn:
            j += 1
            continue
        if j >= pn:
            i += 1
            continue
        if m[i] < p[j]:
            j += 1
        else:
            i += 1
    if i % 2 == 1:
        if i < mn - 1 and j > 0:
            if j < pn - 1:
                x = m[i] * m[i + 1]
                y = p[j] * p[j + 1]
                if x < y:
                    i -= 1
                    j += 1
                else:
                    i += 1
                    j -= 1
            else:
                i += 1
                j -= 1
        elif j < pn - 1:
            i -= 1
            j += 1
    if i % 2 == 0:
        ans = 1
        for x in m[:i]:
            ans = ans * x % MOD
        for x in p[:j]:
            ans = ans * x % MOD
    else:
        if 0 in A:
            ans = 0
        else:
            ans = -1
            for x in m[mn - i:]:
                ans = ans * x % MOD
            for x in p[pn - j:]:
                ans = ans * x % MOD
    print(ans)


if __name__ == "__main__":
    main()