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()))
mod=10**9+7

if N==K:
    ans=1
    for i in range(N):
        ans=(ans*A[i])%mod
    print(ans%mod)
else:
    neg=[]
    posi=[]
    zero=0
    for i in range(N):
        if A[i]>0:
            posi.append(A[i])
        elif A[i]<0:
            neg.append(-A[i])
        else:
            zero+=1
    if K>N-zero:
        print(0)
    elif len(neg)==N and K%2==1:
        neg.sort()
        ans=1
        for i in range(K):
            ans=(-ans*neg[i])%mod
        print(ans%mod)
    else:
        A.sort(key=lambda x:-abs(x))
        temp=A[:K]
        rest=A[K:]
        check=0
        for i in range(K):
            if temp[i]<0:
                check+=1
        if check%2==0:
            ans=1
            for i in range(K):
                ans=(ans*abs(temp[i]))%mod
            print(ans%mod)
        else:
            if check!=K and check!=0:
                rest.sort()
                temp.sort()
                #neg->posi
                val1=-1
                val2=-1
                for i in range(K-1):
                    if temp[i]<0 and temp[i+1]>0:
                        val1=temp[i]
                        val2=temp[i+1]
                        break
                #(abs(val1),rest[-1])
                #posi->neg
                #(abs(val2),abs(rest[0]))
                if rest[-1]>=0 and rest[0]<0:
                    if abs(rest[-1])*abs(val2)>abs(rest[0])*abs(val1):
                        ans=1
                        for i in range(K):
                            ans=(ans*abs(temp[i]))%mod
                        ans=(ans*abs(rest[-1]))%mod
                        ans=(ans*pow(abs(val1),mod-2,mod))%mod
                        print(ans%mod)
                    else:
                        ans=1
                        for i in range(K):
                            ans=(ans*abs(temp[i]))%mod
                        ans=(ans*abs(rest[0]))%mod
                        ans=(ans*pow(abs(val2),mod-2,mod))%mod
                        print(ans%mod)
                elif rest[-1]<0:
                    ans=1
                    for i in range(K):
                        ans=(ans*abs(temp[i]))%mod
                    ans=(ans*abs(rest[0]))%mod
                    ans=(ans*pow(abs(val2),mod-2,mod))%mod
                    print(ans%mod)
                else:
                    ans=1
                    for i in range(K):
                        ans=(ans*abs(temp[i]))%mod
                    ans=(ans*abs(rest[-1]))%mod
                    ans=(ans*pow(abs(val1),mod-2,mod))%mod
                    print(ans%mod)
            elif check==K:
                val1=abs(temp[-1])
                ans=1
                for i in range(K):
                    ans=(ans*abs(temp[i]))%mod
                ans=(ans*abs(rest[-1]))%mod
                ans=(ans*pow(abs(val1),mod-2,mod))%mod
                print(ans%mod)
            else:
                val2=abs(temp[0])
                ans=1
                for i in range(K):
                    ans=(ans*abs(temp[i]))%mod
                ans=(ans*abs(rest[0]))%mod
                ans=(ans*pow(abs(val2),mod-2,mod))%mod
                print(ans%mod)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
A.sort(reverse=True)
mod=10**9+7
P=1
B=[]
C=False
for i in range(N):
  if A[i]:
    B.append(A[i])
  else:
    C=True
if len(B)<K:
  print(0)
  exit()
B.sort(reverse=True,key=lambda x:abs(x))
Y=0
for i in range(K):
  if B[i]<0:
    Y+=1
  P=P*B[i]%mod
if Y&1==0:
  print(P)
  exit()
if Y==K:
  B.sort()
  if B[-1]<0:
    if C:
      print(0)
      exit()
    P=1
    for i in range(K):
      P=P*B[-(i+1)]%mod
    print(P)
    exit()
  P=P*pow(B[0],mod-2,mod)*B[-1]%mod
  print(P)
  exit()
Y,Z=0,0
for i in range(K,len(B)):
  if B[i]<0:
    Y=B[i]
    break
for i in range(K,len(B)):
  if B[i]>0:
    Z=B[i]
    break
if Y==0:
  for i in range(K-1,-1,-1):
    if B[i]>0:
      P=P*pow(B[i],mod-2,mod)
      break
  print(P*Z%mod)
elif Z==0:
  for i in range(K-1,-1,-1):
    if B[i]<0:
      P=P*pow(B[i],mod-2,mod)
      break
  print(P*Y%mod)
if Y==0 or Z==0:
  exit()
V,W=0,0
for i in range(K-1,-1,-1):
  if B[i]<0:
    V=B[i]
    break
for i in range(K-1,-1,-1):
  if B[i]>0:
    W=B[i]
    break
if Y*V>Z*W:
  print(P*Y*pow(W,mod-2,mod)%mod)
else:
  print(P*Z*pow(V,mod-2,mod)%mod)

Submission status: Wrong Answer

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    _LI = lambda : [-int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()
    DD = ((1,0),(0,1),(-1,0),(0,-1))

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

    ans = 1
    ma = max(A)
    if ma <= 0 and K % 2:
        x = sorted(A,reverse=True)
        for i in range(K):
            ans = (ans * A[i]) % MOD
        print(ans)
        return

    x = sorted(A, key=lambda x: abs(x), reverse=True)
    m = 0
    for i in range(K):
        ans = (ans * x[i]) % MOD
        if x[i] < 0 :m += 1

    if K == N or m % 2 == 0:
        print(ans)
        return
    if x[K] == 0:
        print(0)
        return
    else:
        for i in range(K-1,-1,-1):
            if x[i] * x[K] < 0:
                ans = (ans * pow(x[i],MOD-2,MOD) * x[K]) % MOD
                print(ans)
                return
        print(ans)
        return



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse = True, key = lambda x: abs(x))
mod = 10**9+7
nega = 0
posi = 0
zero = 0
for i in range(n):
    if a[i] > 0:
        posi += 1
    elif a[i] == 0:
        zero += 1
    else:
        nega += 1

if n == k:
    ans = 1
    for i in range(n):
        ans *= abs(a[i])
        ans %= mod
    if nega%2 == 1:
        ans *= -1

elif posi == 0:

    if k % 2 == 0:
        ans = 1
        for i in range(k):
            ans *= abs(a[i])
            ans %= mod

    else:
        if a.count(0) > 0:
            ans = 0
        else:
            a.reverse()
            ans = 1
            for i in range(k):
                ans *= abs(a[i])
                ans %= mod

            ans *= -1


else:
    posi = 0
    nega = 0
    zero = 0
    ans = 1
    for i in range(k):
        ans *= abs(a[i])
        ans %= mod

    for i in range(k):
        if a[i] > 0:
            posi += 1
        elif a[i] < 0:
            nega += 1
        else:
            zero += 1
    if zero > 0:
        ans = 0

    elif nega % 2 != 0:
        minposi = 0
        minnega = 0
        for i in range(k-1, -1, -1):
            if a[i] > 0:
                if minposi == 0:
                    minposi = a[i]
            else:
                if minnega == 0:
                    minnega = a[i]

        maxposi = 0
        maxnega = 0
        for i in range(k, n):
            if a[i] > 0:
                if maxposi == 0:
                    maxposi = a[i]
            else:
                if maxnega == 0:
                    maxnega = a[i]

        if abs(minposi*maxposi) < abs(minnega*minnega):
            ans *= pow(abs(minposi), mod-2, mod)
            ans *= abs(maxnega)
        else:
            ans *= pow(abs(minnega), mod-2, mod)
            ans *= abs(maxposi)

print(ans % mod)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
mod = 10**9 + 7
plus = []
minus = []
for i in map(int, input().split()):
    if i > 0:
        plus.append(i)
    elif i < 0:
        minus.append(i)
plen = len(plus)
mlen = len(minus)
zlen = N - plen - mlen
if K > plen + mlen:
    print(0)
    import sys
    sys.exit()
plus.sort(reverse=True)
minus.sort()
pp = [1]
mp = [1]
for p in plus:
    pp.append((pp[-1]*p)%mod)
for m in minus:
    mp.append((mp[-1]*(-m))%mod)
ppm = [1]
mpm = [1]
for p in plus[::-1]:
    ppm.append((ppm[-1]*p)%mod)
for m in minus[::-1]:
    mpm.append((mpm[-1]*(-m))%mod)
ans = -float("inf")
# kこマイナスから選ぶ
for k in range(K+1):
    if mlen < k:
        continue
    if plen < K-k:
        continue
    # 最大を目指す
    if k % 2 == 0:
        tmp = mp[k] * pp[K-k]
        ans = max(ans, tmp)
    # 最小を目指す
    else:
        tmp = -(mpm[k] * ppm[K-k])
        ans = max(ans, tmp)
if ans < 0 and zlen > 0:
    print(0)
    import sys
    sys.exit()
print(ans%mod)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
mod = 10**9 + 7
b_p = []
b_n = []
ab = []
for i in range(n):
    ab.append(abs(a[i]))
    if a[i] >= 0:
        b_p.append(a[i])
    else:
        b_n.append(a[i])
b_p = sorted(b_p,reverse=True)
b_n = sorted(b_n,reverse=True)
ab = sorted(ab)
ans = 1
i = 0
x = 0
y = 0
while i < k-1:
    i += 2
    if x < len(b_p)-1 and y < len(b_n)-1:
        if b_p[x]*b_p[x+1] > b_n[y]*b_n[y+1]:
            ans *= b_p[x]*b_p[x+1]
            ans %= mod
            x += 2
        else:
            ans *= b_n[y]*b_n[y+1]
            ans %= mod
            y += 2
    
    elif x < len(b_p)-1:
        ans *= b_p[x]*b_p[x+1]
        ans %= mod
        x += 2
    elif y < len(b_n)-1:
        ans *= b_n[y]*b_n[y+1]
        ans %= mod
        y += 2
    else:
        break
if i != k:
    if x < len(b_p):
        ans *= b_p[x]
        ans %= mod
    else:
        if 0 in b_p:
            ans = 0
        else:
            ans = 1
            for i in range(k):
                ans *= ab[i]
                ans %= mod
            ans *= -1
print(ans%mod)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
A = [int(a) for a in input().split()]

mod = 10**9+7

cp = 0
cm = 0
for i in range(N):
    n = 1
    if A[i] < 0:
        n = -1
    elif A[i] == 0:
        n = 0
    A[i] = (abs(A[i]), n)
    if n > 0:
        cp += 1
    elif n < 0:
        cm += 1

f = -1
for i in range(0, K+1, 2):
    if i <= cm and K-i <= cp:
        f = 1
        break


if K > cp+cm:
    ans = 0
elif f == -1:
    if N > cp+cm:
        ans = 0
    else:
        A.sort()
        ans = 1
        for i in range(K):
            ans *= A[i][0]
            ans %= mod
        ans *= -1
        ans %= mod
else:
    A.sort(reverse=True)
    cnt = 0
    ans = 1
    for i in range(K):
        ans *= A[i][0]
        ans %= mod
        if A[i][1] < 0:
            cnt += 1

    if cnt % 2 == 1:
        a1 = 1
        f11 = False
        f12 = False
        for i in range(K, N):
            if A[i][1] < 0:
                a1 *= A[i][0]
                f11 = True
                break
        for i in range(K-1, -1, -1):
            if A[i][1] > 0:
                a1 /= A[i][0]
                f12 = True
                break
        a2 = 1
        f21 = False
        f22 = False
        for i in range(K, N):
            if A[i][1] > 0:
                a2 *= A[i][0]
                f21 = True
                break
        for i in range(K-1, -1, -1):
            if A[i][1] < 0:
                a2 /= A[i][0]
                f22 = True
                break

        if a1 > a2 and f11 and f12 and f21 and f22:
            for i in range(K, N):
                if A[i][1] < 0:
                    ans *= A[i][0]
                    ans %= mod
                    break
            for i in range(K-1, -1, -1):
                if A[i][1] > 0:
                    ans *= pow(A[i][0], mod-2, mod)
                    ans %= mod
                    break
        elif f11 and f12 and f21 and f22:
            for i in range(K, N):
                if A[i][1] > 0:
                    ans *= A[i][0]
                    ans %= mod
                    break
            for i in range(K-1, -1, -1):
                if A[i][1] < 0:
                    ans *= pow(A[i][0], mod-2, mod)
                    ans %= mod
                    break
        elif f11 and f12:
            for i in range(K, N):
                if A[i][1] < 0:
                    ans *= A[i][0]
                    ans %= mod
                    break
            for i in range(K-1, -1, -1):
                if A[i][1] > 0:
                    ans *= pow(A[i][0], mod-2, mod)
                    ans %= mod
                    break
        else:
            for i in range(K, N):
                if A[i][1] > 0:
                    ans *= A[i][0]
                    ans %= mod
                    break
            for i in range(K-1, -1, -1):
                if A[i][1] < 0:
                    ans *= pow(A[i][0], mod-2, mod)
                    ans %= mod
                    break

print(ans)

Submission status: Wrong Answer

Solution code:
# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
MOD = 10**9 + 7
def main(N, K, A):
    negative = sorted([a for a in A if a < 0])
    neg = len(negative)
    zero = [a for a in A if a == 0]
    positive = sorted([a for a in A if a > 0])
    pos = len(positive)

    if N - K < len(zero):
        return 0
    N -= len(zero)
    A = sorted([a for a in A if a != 0], key=lambda x: abs(x))

    ans = Mint(1)
    if ((neg & (~1)) + pos < K) or (neg & 1 and K == N) or (pos == 0 and K & 1):
        # マイナスになっちゃう
        for i in range(K):
            ans *= abs(A[i])
        ans = -ans
    else:
        # プラスにできる
        A.reverse()
        neg_cnt = 0
        for i in range(K - 1):
            ans *= abs(A[i])
            if A[i] < 0: neg_cnt += 1
        for i in range(K - 1, N):
            if neg_cnt & 1:
                if A[i] < 0:
                    ans *= abs(A[i])
                    break
            else:
                if A[i] > 0:
                    ans *= A[i]
                    break

    return ans

class Mint:
    def __init__(self, value=0):
        self.value = value % MOD
        if self.value < 0: self.value += MOD

    @staticmethod
    def get_value(x): return x.value if isinstance(x, Mint) else x

    def inverse(self):
        a, b = self.value, MOD
        u, v = 1, 0
        while b:
            t = a // b
            b, a = a - t * b, b
            v, u = u - t * v, v
        if u < 0: u += MOD
        return u

    def __repr__(self): return str(self.value)
    def __eq__(self, other): return self.value == other.value
    def __neg__(self): return Mint(-self.value)
    def __hash__(self): return hash(self.value)
    def __bool__(self): return self.value != 0

    def __iadd__(self, other):
        self.value = (self.value + Mint.get_value(other)) % MOD
        return self

    def __add__(self, other):
        new_obj = Mint(self.value)
        new_obj += other
        return new_obj
    __radd__ = __add__

    def __isub__(self, other):
        self.value = (self.value - Mint.get_value(other)) % MOD
        if self.value < 0: self.value += MOD
        return self

    def __sub__(self, other):
        new_obj = Mint(self.value)
        new_obj -= other
        return new_obj

    def __rsub__(self, other):
        new_obj = Mint(Mint.get_value(other))
        new_obj -= self
        return new_obj

    def __imul__(self, other):
        self.value = self.value * Mint.get_value(other) % MOD
        return self

    def __mul__(self, other):
        new_obj = Mint(self.value)
        new_obj *= other
        return new_obj
    __rmul__ = __mul__

    def __ifloordiv__(self, other):
        other = other if isinstance(other, Mint) else Mint(other)
        self *= other.inverse()
        return self

    def __floordiv__(self, other):
        new_obj = Mint(self.value)
        new_obj //= other
        return new_obj

    def __rfloordiv__(self, other):
        new_obj = Mint(Mint.get_value(other))
        new_obj //= self
        return new_obj

if __name__ == '__main__':
    input = sys.stdin.readline
    N, K = map(int, input().split())
    *A, = map(int, input().split())
    print(main(N, K, A))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse=True)
m = 1
mod = 1000000007
for i in range(k):
    m = (m%mod) * (a[i]%mod)
print(m%mod)

Submission status: Wrong Answer

Solution code:
import heapq
import sys

MOD = 10**9+7

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

a_plus=[]
a_minus=[]
a_zero = 0

for ai in a:
    if ai>0:
        heapq.heappush(a_plus, -ai)
    elif ai<0:
        heapq.heappush(a_minus, ai)
    else:
        a_zero += 1
        
if a_zero > n-k:
    print(0)
    sys.exit()
elif (len(a_minus) == n and k%2 == 1) or (n==k and len(a_minus)%2 == 1):
    if a_zero>0:
        print(0)
        sys.exit()
    for _ in range(n-k):
        heapq.heappop(a_minus)
    ans = 1
    for _ in range(len(a_minus)):
        ans *= - heapq.heappop(a_minus)
        ans %= MOD
    for _ in range(len(a_plus)):
        ans *= - heapq.heappop(a_plus)
        ans %= MOD
    print(-ans % MOD)
    sys.exit()


ans = 1
last_plus = MOD
last_minus = MOD
sign = 1

for _ in range(k):
    if len(a_minus)==0 or (len(a_plus)>0 and a_plus[0] < a_minus[0]):
        last_plus = -heapq.heappop(a_plus)
        ans *= last_plus
    else:
        last_minus = -heapq.heappop(a_minus)
        ans *= last_minus
        sign *= -1
    
    ans %= MOD
    

if sign>0:
    print(ans)
else:
    heapq.heappush(a_plus,0)
    heapq.heappush(a_minus,0)
    
    if -a_plus[0] / last_minus > -a_minus[0] / last_plus:
        ans = ans * (-a_plus[0]) % MOD * pow(last_minus,MOD-2,MOD) % MOD
    else:
        ans = ans * (-a_minus[0]) % MOD * pow(last_plus,MOD-2,MOD) % MOD
    print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub


import sys
# sys.setrecursionlimit(10**6)
# buff_readline = sys.stdin.buffer.readline
buff_readline = sys.stdin.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(buff_readline())


def read_int_n():
    return list(map(int, buff_readline().split()))


def read_float():
    return float(buff_readline())


def read_float_n():
    return list(map(float, buff_readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()

def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


class Mod:
    def __init__(self, m):
        self.m = m

    def add(self, a, b):
        return (a + b) % self.m

    def sub(self, a, b):
        return (a - b) % self.m

    def mul(self, a, b):
        return ((a % self.m) * (b % self.m)) % self.m

    def div(self, a, b):
        return self.mul(a, pow(b, self.m-2, self.m))

    def pow(self, a, b):
        return pow(a, b, self.m)



@mt
def slv(N, K, A):
    M = 10**9+7
    if N - A.count(0) < K:
        return 0

    def p(N, K, A):
        pa = [a for a in A if a > 0]
        na = [-a for a in A if a < 0]
        # positive
        pa.sort()
        na.sort()
        ans = 1
        if K % 2 == 1:
            if len(pa) == 0:
                return None
            ans *= pa.pop()
            K -= 1
        for _ in range(K//2):
            if len(pa) >= 2 and len(na) >= 2:
                if pa[-1] * pa[-2] > na[-1] * na[-2]:
                    ans *= pa[-1] * pa[-2]
                    pa.pop()
                    pa.pop()
                else:
                    ans *= na[-1] * na[-2]
                    na.pop()
                    na.pop()
            elif len(pa) >= 2:
                ans *= pa[-1] * pa[-2]
                pa.pop()
                pa.pop()
            elif len(na) >= 2:
                ans *= na[-1] * na[-2]
                na.pop()
                na.pop()
            else:
                break
            ans %= M
        else:
            return ans
        return None

    def n(N, K, A):
        pa = [a for a in A if a > 0]
        na = [-a for a in A if a < 0]
        # negative
        pa.sort(reverse=True)
        na.sort(reverse=True)
        ans = 1
        for _ in range(K):
            if pa and na:
                if pa < na:
                    ans *= pa[-1]
                    pa.pop()
                else:
                    ans *= -na[-1]
                    na.pop()
            elif pa:
                ans *= pa[-1]
                pa.pop()
            else:
                ans *= -na[-1]
                na.pop()
            ans %= M
        return ans

    a = p(N, K, A)
    if a:
        return a
    return n(N, K, A)


def main():
    N, K = read_int_n()
    A = read_int_n()
    print(slv(N, K, A))

    import random
    # N = random.choice([9, 10])
    # K = random.randint(1, N)
    # A = [random.randint(-20, 20) for _  in range(N)]

    # N = 9
    # K = 9
    # A = [4, -9, -18, 7, 3, -5, -15, 14, -20]
    # print(N, K)
    # print(A)
    # print(slv(N, K, A))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def answer(A, n, k): 
    A.sort()
    product = 1
    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] 
        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 
            i += 2
        else : 
            product *= right_product 
            j -= 2
    return product 

n,m=map(int,input().split())
ans=list(map(int,input().split()))
x=answer(ans,n,m)
mod=(10**9)+7
x=(x+1000*mod)%mod
print(x)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
mod = 10**9 + 7
plus = []
minus = []
for i in map(int, input().split()):
    if i > 0:
        plus.append(i)
    elif i < 0:
        minus.append(i)
plen = len(plus)
mlen = len(minus)
zlen = N - plen - mlen
if K > plen + mlen:
    print(0)
    import sys
    sys.exit()
plus.sort(reverse=True)
minus.sort()
pp = [1]
mp = [1]
for p in plus:
    pp.append((pp[-1]*p)%mod)
for m in minus:
    mp.append((mp[-1]*(-m))%mod)
ppm = [1]
mpm = [1]
for p in plus[::-1]:
    ppm.append((ppm[-1]*p)%mod)
for m in minus[::-1]:
    mpm.append((mpm[-1]*(-m))%mod)
ans = -float("inf")
# kこマイナスから選ぶ
for k in range(K+1):
    if mlen < k:
        continue
    if plen < K-k:
        continue
    # 最大を目指す
    if k % 2 == 0:
        print(k)
        tmp = mp[k] * pp[K-k]
        ans = max(ans, tmp)
    # 最小を目指す
    else:
        tmp = -(mpm[k] * ppm[K-k])
        ans = max(ans, tmp)
if ans < 0 and zlen > 0:
    print(0)
    import sys
    sys.exit()
print(ans%mod)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
p=1000000007
product = 1
if A[n - 1] == 0 and (k & 1):
      print(0)

if A[n - 1] <= 0 and (k & 1):
        for i in range(n - 1, n - k - 1, -1):
            product *= A[i]

        print(product%p)
else:

    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
                i += 2

            else:
                product *= right_product
                j -= 2

    print(product%p)

Submission status: Wrong Answer

Solution code:
mod = 10**9+7
N,K = map(int,input().split())
A = list(map(int,input().split()))
canPositive = 0
Positive = 0
ans = 1
def inv(arg):
    return pow(arg,mod-2,mod)
for i in range(N):
    if A[i] >= 0:
        Positive += 1
if N - K >= 1 and Positive >= 1:
    canPositive = 1
if Positive == 0 and K % 2 == 0:
    canPositive = 1
anslist = []
if canPositive:
    A.sort(key=lambda x:abs(x),reverse=True)
    n = 0
    for a in A[:K]:
        anslist.append(a)
        if a < 0:
           n += 1
    if n % 2 == 1:
        minpos = 0
        minneg = 0
        maxpos = 0
        maxneg = 0
        for i in anslist[::-1]:
            if i > 0:
                minpos = i
                break
        for i in anslist[::-1]:
            if i < 0:
                minneg = i
                break
        for i in A[K:]:
            if i < 0:
                maxneg = i
                break
        for i in A[K:]:
            if i > 0:
                maxpos = i
                break
        if maxneg == 0 or minpos == 0:
            print(minneg,maxpos)
            ans *= maxpos*inv(minneg) 
        elif maxpos == 0 or minneg == 0:
            ans *= maxneg*inv(minpos) 
        else:
            if abs(minpos*maxpos) > abs(maxneg*minneg):
                ans *= maxpos*inv(minneg) 
            else:
                ans *= maxneg*inv(minpos) 
else:
    A.sort(key=lambda x:abs(x))
    for a in A[:K]:
        anslist.append(a)
for i in anslist:
    ans *= i
    ans %= mod
print(ans)