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:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

plus_zero = []
minus = []

for i in a:
    if i >= 0:
        plus_zero.append(i)
    if i < 0:
        minus.append(-i)

from collections import deque

plus_zero = deque(sorted(plus_zero, reverse=True))
minus = deque(sorted(minus, reverse=True))
absolute = deque(sorted(a, key=abs, reverse=True))

ans_lst = []
# 境界条件: k == n
if k == n:
    ans_lst.extend(a)
# 負
elif len(plus_zero) == 0 and (k % 2) != 0:
    for _ in range(k):
        ans_lst.append(-minus.pop())
# 正 or 0
else:
    minus_num = 0
    for _ in range(k):
        v = absolute.popleft()
        if v < 0:
            minus_num += 1
        ans_lst.append(v)
    if minus_num % 2 != 0:
        abs_min_plus_zero, abs_min_minus = None, None
        abs_max_plus_zero, abs_max_minus = None, None
        for i in ans_lst:
            if i >= 0:
                if abs_min_plus_zero is None:
                    abs_min_plus_zero = i
                else:
                    abs_min_plus_zero = min(i, abs_min_plus_zero)
            else:
                if abs_min_minus is None:
                    abs_min_minus = i
                else:
                    abs_min_minus = max(abs_min_minus, i)
        for i in absolute:
            if i >= 0:
                if abs_max_plus_zero is None:
                    abs_max_plus_zero = i
                else:
                    abs_max_plus_zero = max(i, abs_max_plus_zero)
            else:
                if abs_max_minus is None:
                    abs_max_minus = i
                else:
                    abs_max_minus = min(abs_max_minus, i)
        case1, case2 = False, False
        if abs_min_plus_zero is not None and abs_max_minus is not None:
            case1 = True
        if abs_min_minus is not None and abs_max_plus_zero is not None:
            case2 = True
        if case1 and case2:
            pass
        #    if abs_max_minus * abs_min_minus <= abs_max_plus_zero * abs_min_plus_zero:
        #        ans_lst.remove(abs_min_minus)
        #        ans_lst.append(abs_max_plus_zero)
        #    else:
        #        ans_lst.remove(abs_min_plus_zero)
        #        ans_lst.append(abs_max_minus)
        elif case1:
            ans_lst.remove(abs_min_plus_zero)
            ans_lst.append(abs_max_minus)
        elif case2:
            ans_lst.remove(abs_min_minus)
            ans_lst.append(abs_max_plus_zero)


# functools.reduceはTLEをくらう場合があるので気をつける
ans = 1
for i in ans_lst:
    ans = (ans * i) % MOD
print(ans)

Submission status: Wrong Answer

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

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

plus_zero = []
minus = []

for i in a:
    if i >= 0:
        plus_zero.append(i)
    if i < 0:
        minus.append(-i)

from collections import deque

plus_zero = deque(sorted(plus_zero, reverse=True))
minus = deque(sorted(minus, reverse=True))
absolute = deque(sorted(a, key=abs, reverse=True))

ans_lst = []
# 境界条件: k == n
if k == n:
    ans_lst.extend(a)
# 負
elif len(plus_zero) == 0 and (k % 2) != 0:
    for _ in range(k):
        ans_lst.append(-minus.pop())
# 正 or 0
else:
    minus_num = 0
    for _ in range(k):
        v = absolute.popleft()
        if v < 0:
            minus_num += 1
        ans_lst.append(v)
    if minus_num % 2 != 0:
        abs_min_plus_zero, abs_min_minus = None, None
        abs_max_plus_zero, abs_max_minus = None, None
        for i in ans_lst:
            if i >= 0:
                if abs_min_plus_zero is None:
                    abs_min_plus_zero = i
                else:
                    abs_min_plus_zero = min(i, abs_min_plus_zero)
            else:
                if abs_min_minus is None:
                    abs_min_minus = i
                else:
                    abs_min_minus = max(abs_min_minus, i)
        for i in absolute:
            if i >= 0:
                if abs_max_plus_zero is None:
                    abs_max_plus_zero = i
                else:
                    abs_max_plus_zero = max(i, abs_max_plus_zero)
            else:
                if abs_max_minus is None:
                    abs_max_minus = i
                else:
                    abs_max_minus = min(abs_max_minus, i)
        case1, case2 = False, False
        if abs_min_plus_zero is not None and abs_max_minus is not None:
            case1 = True
        if abs_min_minus is not None and abs_max_plus_zero is not None:
            case2 = True
        if case1 and case2:
            pass
        #    if abs_max_minus * abs_min_minus <= abs_max_plus_zero * abs_min_plus_zero:
        #        ans_lst.remove(abs_min_minus)
        #        ans_lst.append(abs_max_plus_zero)
        #    else:
        #        ans_lst.remove(abs_min_plus_zero)
        #        ans_lst.append(abs_max_minus)
        elif case1:
            ans_lst.remove(abs_min_plus_zero)
            ans_lst.append(abs_max_minus)
        #elif case2:
        #    ans_lst.remove(abs_min_minus)
        #    ans_lst.append(abs_max_plus_zero)


# functools.reduceはTLEをくらう場合があるので気をつける
ans = 1
for i in ans_lst:
    ans = (ans * i) % MOD
print(ans)

Submission status: Wrong Answer

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

p = 1
n = 0
ans = 1
cnt = 0
while cnt < K-1:
  if A[n]*A[n+1] > A[-p]*A[-p-1]:
    ans = ans*A[n]*A[n+1]%mod
    n += 2
    cnt += 2
    
  else:
    ans = ans*A[-p]%mod
    p += 1
    cnt += 1
    
if cnt == K-1:
  ans = ans*A[-p]%mod
  
if A[-1]<0 and K%2==1:
  ans = 1
  for i in A[N-K:]:
    ans = ans*cnt%mod

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
m=10**9+7
ans=0
if n==k:
  ans=a[0]
  for i in range(1,n):
    ans=(ans*a[i])%m
else:
  a.sort()
  if a[n-1]<0:
    ans=a[n-1]
    for i in range(n-2,n-k-1,-1):
      ans=(ans*a[i])%m
  else:
    if k&1:
      ans=a[n-1]
      plus=n-2
      pluss=a[n-2]*a[n-3]
    else:
      ans=1
      plus=n-1
      pluss=a[n-1]*a[n-2]
    minus=0
    minuss=a[0]*a[1]
    for i in range(k//2):
      if minuss>=pluss:
        ans=(ans*(minuss%m))%m
        minus+=2
        minuss=a[minus]*a[minus+1]
      else:
        ans=(ans*(pluss%m))%m
        plus-=2
        pluss=a[plus]*a[plus-1]
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
*A, = map(int, input().split())
p = 10**9 + 7
A.sort()
ans = 1
if A[n - 1] <= 0 and k % 2 == 1:
    for i in range(k):
        ans *= A[n - 1 - i]
        ans %= p
    print(ans)
    exit()
if k % 2 == 1:
    ans *= A.pop()
    ans %= p
    n -= 1
    k -= 1
A = [A[i] * A[i + 1] for i in range(0, n - 1, 2)]
A.sort(reverse=True)
for i in range(k // 2):
    ans *= A[i]
    ans %= p
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
*A, = map(int, input().split())
p = 10**9 + 7
A.sort()
ans = 1
if A[n - 1] <= 0 and k % 2 == 1:
    for i in range(k):
        ans *= A[n - 1 - i]
        ans %= p
    print(ans)
    exit()
if k % 2 == 1:
    ans *= A.pop()
    ans %= p
    n -= 1
A = [A[i] * A[i + 1] for i in range(0, n - 1, 2)]
A.sort(reverse=True)
for i in range(k // 2):
    ans *= A[i]
    ans %= p
print(ans)

Submission status: Wrong Answer

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

N,K=map(int,input().split())
A=list(map(int,input().split()))
mod=10**9+7

B=[]
C=[]
Z=[]
 
for a in A:
    if a>0:
        B.append(a)
    elif a<0:
        C.append(-a)
    else:
        Z.append(0)
 
B.sort(reverse=True)
C.sort(reverse=True)
 
if K==len(B)+len(C):
    ANS=1
    for b in B:
        ANS=ANS*b%mod
    for c in C:
        ANS=ANS*(-c)%mod
 
    if len(C)%2==1 and Z!=[]:
        print(0)
    else:
        print(ANS)
    sys.exit()
 
if K>len(B)+len(C):
    print(0)
    sys.exit()
 
if B==[]:
    ANS=1
    for i in range(K):
        ANS=ANS*(C[i])%mod
 
    if Z==[] and K%2==0:
        print(ANS)
    elif Z==[] and K%2==1:
        print(-ANS%mod)
    elif K%2==1:
        print(0)
    else:
        print(ANS)
 
    sys.exit()

if K<=len(B):
    S=1
    for i in range(K):
        S=S*B[i]%mod

    ind=0
    while True:
        if ind+1<len(C) and K-2-ind>=0 and C[ind]*C[ind+1]>B[K-1-ind]*B[K-2-ind]:
            S=S*C[ind]*C[ind+1]*pow(B[K-1-ind]*B[K-2-ind],mod-2,mod)%mod
            ind+=2
        else:
            break

    print(S%mod)

else:
    if (K-len(B))%2==0:
        S=1
        for i in range(len(B)):
            S=S*B[i]%mod

        for j in range(K-len(B)):
            S=S*C[j]%mod

        ind=0
        c_start=K-len(B)

        while True:
            if c_start+ind+1<len(C) and len(B)-2-ind>=0 and C[c_start+ind]*C[c_start+ind+1]>B[len(B)-1-ind]*B[len(B)-2-ind]:
                S=S*C[c_start+ind]*C[c_start+ind+1]*pow(B[len(B)-1-ind]*B[len(B)-2-ind],mod-2,mod)%mod
                ind+=2
            else:
                break

    else:
        S=1
        for i in range(len(B)-1):
            S=S*B[i]%mod

        for j in range(K-len(B)+1):
            S=S*C[j]%mod

        ind=0
        c_start=K-len(B)+1

        while True:
            if c_start+ind+1<len(C) and len(B)-3-ind>=0 and C[c_start+ind]*C[c_start+ind+1]>B[len(B)-2-ind]*B[len(B)-3-ind]:
                S=S*C[c_start+ind]*C[c_start+ind+1]*pow(B[len(B)-2-ind]*B[len(B)-3-ind],mod-2,mod)%mod
                ind+=2
            else:
                break

    print(S%mod)

Submission status: Wrong Answer

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

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

plus_zero = []
minus = []

for i in a:
    if i >= 0:
        plus_zero.append(i)
    if i < 0:
        minus.append(-i)

from collections import deque

plus_zero = deque(sorted(plus_zero, reverse=True))
minus = deque(sorted(minus, reverse=True))
absolute = deque(sorted(a, key=abs, reverse=True))

ans_lst = []
# 境界条件: k == n
if k == n:
    ans_lst.extend(a)
# 負
elif len(plus_zero) == 0 and (k % 2) != 0:
    for _ in range(k):
        ans_lst.append(-minus.pop())
# 正 or 0
else:
    minus_num = 0
    for _ in range(k):
        v = absolute.popleft()
        if v < 0:
            minus_num += 1
        ans_lst.append(v)
    if minus_num % 2 != 0:
        abs_min_plus_zero, abs_min_minus = None, None
        abs_max_plus_zero, abs_max_minus = None, None
        for i in ans_lst:
            if i >= 0:
                if abs_min_plus_zero is None:
                    abs_min_plus_zero = i
                else:
                    abs_min_plus_zero = min(i, abs_min_plus_zero)
            else:
                if abs_min_minus is None:
                    abs_min_minus = i
                else:
                    abs_min_minus = max(abs_min_minus, i)
        for i in absolute:
            if i >= 0:
                if abs_max_plus_zero is None:
                    abs_max_plus_zero = i
                else:
                    abs_max_plus_zero = max(i, abs_max_plus_zero)
            else:
                if abs_max_minus is None:
                    abs_max_minus = i
                else:
                    abs_max_minus = min(abs_max_minus, i)
        case1, case2 = False, False
        if abs_min_plus_zero is not None and abs_max_minus is not None:
            case1 = True
        if abs_min_minus is not None and abs_max_plus_zero is not None:
            case2 = True
        #if case1 and case2:
        #    if abs_max_minus * abs_min_minus <= abs_max_plus_zero * abs_min_plus_zero:
        #        ans_lst.remove(abs_min_minus)
        #        ans_lst.add(abs_max_plus_zero)
        #    else:
        #        ans_lst.remove(abs_min_plus_zero)
        #        ans_lst.add(abs_max_minus)
        #elif case1:
        #    ans_lst.remove(abs_min_plus_zero)
        #    ans_lst.add(abs_max_minus)
        #elif case2:
        #    ans_lst.remove(abs_min_minus)
        #    ans_lst.add(abs_max_plus_zero)

# functools.reduceはTLEをくらう場合があるので気をつける
ans = 1
for i in ans_lst:
    ans = (ans * i) % MOD
print(ans)

Submission status: Wrong Answer

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

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

plus_zero = []
minus = []

for i in a:
    if i >= 0:
        plus_zero.append(i)
    if i < 0:
        minus.append(-i)

from collections import deque

plus_zero = deque(sorted(plus_zero, reverse=True))
minus = deque(sorted(minus, reverse=True))
absolute = deque(sorted(a, key=abs, reverse=True))

ans_lst = []
# 境界条件: k == n
if k == n:
    ans_lst.extend(a)
# 負
elif len(plus_zero) == 0 and (k % 2) != 0:
    for _ in range(k):
        ans_lst.append(-minus.pop())
# 正 or 0
else:
    minus_num = 0
    for _ in range(k):
        v = absolute.popleft()
        if v < 0:
            minus_num += 1
        ans_lst.append(v)
    if minus_num % 2 != 0:
        abs_min_plus_zero, abs_min_minus = None, None
        abs_max_plus_zero, abs_max_minus = None, None
        for i in ans_lst:
            if i >= 0:
                if abs_min_plus_zero is None:
                    abs_min_plus_zero = i
                else:
                    abs_min_plus_zero = min(i, abs_min_plus_zero)
            else:
                if abs_min_minus is None:
                    abs_min_minus = i
                else:
                    abs_min_minus = max(abs_min_minus, i)
        for i in absolute:
            if i >= 0:
                if abs_max_plus_zero is None:
                    abs_max_plus_zero = i
                else:
                    abs_max_plus_zero = max(i, abs_max_plus_zero)
            else:
                if abs_max_minus is None:
                    abs_max_minus = i
                else:
                    abs_max_minus = min(abs_max_minus, i)
        #case1, case2 = False, False
        #if abs_min_plus_zero is not None and abs_max_minus is not None:
        #    case1 = True
        #if abs_min_minus is not None and abs_max_plus_zero is not None:
        #    case2 = True
        #if case1 and case2:
        #    if abs_max_minus * abs_min_minus <= abs_max_plus_zero * abs_min_plus_zero:
        #        ans_lst.remove(abs_min_minus)
        #        ans_lst.add(abs_max_plus_zero)
        #    else:
        #        ans_lst.remove(abs_min_plus_zero)
        #        ans_lst.add(abs_max_minus)
        #elif case1:
        #    ans_lst.remove(abs_min_plus_zero)
        #    ans_lst.add(abs_max_minus)
        #elif case2:
        #    ans_lst.remove(abs_min_minus)
        #    ans_lst.add(abs_max_plus_zero)

# functools.reduceはTLEをくらう場合があるので気をつける
ans = 1
for i in ans_lst:
    ans = (ans * i) % MOD
print(ans)

Submission status: Wrong Answer

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

N,K=map(int,input().split())
A=list(map(int,input().split()))
mod=10**9+7

B=[]
C=[]
Z=[]

for a in A:
    if a>0:
        B.append(a)
    elif a<0:
        C.append(-a)
    else:
        Z.append(0)
 
B.sort(reverse=True)
C.sort(reverse=True)
 
if K==len(B)+len(C):
    ANS=1
    for b in B:
        ANS=ANS*b%mod
    for c in C:
        ANS=ANS*(-c)%mod
 
    if len(C)%2==1 and Z!=[]:
        print(0)
    else:
        print(ANS)
    sys.exit()
 
if K>len(B)+len(C):
    print(0)
    sys.exit()
 
if B==[]:
    if Z==[] and K%2==0:
        ANS=1
        for i in range(K):
            ANS=ANS*(C[i])%mod
            
    elif Z==[] and K%2==1:
        C.reverse()
        ANS=1
        for i in range(K):
            ANS=ANS*(C[i])%mod
        print(-ANS%mod)
        
    elif K%2==1:
        print(0)
    else:
        ANS=1
        for i in range(K):
            ANS=ANS*(C[i])%mod
 
    sys.exit()

if K<=len(B):
    S=1
    for i in range(K):
        S=S*B[i]%mod

    ind=0
    while True:
        if ind+1<len(C) and K-2-ind>=0 and C[ind]*C[ind+1]>B[K-1-ind]*B[K-2-ind]:
            S=S*C[ind]*C[ind+1]*pow(B[K-1-ind]*B[K-2-ind],mod-2,mod)%mod
            ind+=2
        else:
            break

    print(S%mod)

else:
    if (K-len(B))%2==0:
        S=1
        for i in range(len(B)):
            S=S*B[i]%mod

        for j in range(K-len(B)):
            S=S*C[j]%mod

        ind=K-len(B)

    else:
        S=1
        for i in range(len(B)-1):
            S=S*B[i]%mod

        for j in range(K-len(B)+1):
            S=S*C[j]%mod

        ind=K-len(B)+1
        

    while True:
        if ind+1<len(C) and K-2-ind>=0 and C[ind]*C[ind+1]>B[K-1-ind]*B[K-2-ind]:
            S=S*C[ind]*C[ind+1]*pow(B[K-1-ind]*B[K-2-ind],mod-2,mod)%mod
            ind+=2
        else:
            break

    print(S%mod)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#
# Author: Tetsuya Ishikawa <tiskw111@gmail.com>
# Date  : July 05, 2020
##################################################### SOURCE START #####################################################

import heapq
import collections

class HeapQueue:

    def __init__(self, xs, mode):
        if   mode == "min": self.xs = [+x for x in xs]
        elif mode == "max": self.xs = [-x for x in xs]
        else              : raise NotImplementedError("HeapQueue.__init__: mode should be 'min' or 'max'")
        heapq.heapify(xs)
        self.mode = mode

    def __len__(self):
        return len(self.xs)

    def pop(self):
        if   self.mode == "min": return heapq.heappop(self.xs)
        elif self.mode == "max": return heapq.heappop(self.xs) * (-1)

    def push(self, x):
        if   self.mode == "min": return heapq.heappush(self.xs,  x)
        elif self.mode == "max": return heapq.heappush(self.xs, -x)

    # @staticmethod
    # def __test01__():
    #     xs = HeapQueue([-3, 2, 9, -4], "min")
    #     x0 = xs.pop()
    #     x1 = xs.pop()
    #     x2 = xs.pop()
    #     x3 = xs.pop()
    #     return (x0 == -4 and x1 == -3 and x2 == 2 and x3 == 9)


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

D = int(1E9 + 7)
r = 1

if N == K:

    for a in As:
        r = (r * a) % D

elif all(a >= 0 for a in As):

    As = sorted(As, reverse=True)
    for a in As[:K]:
        r = (r * a) % D

elif K % 2 == 1 and all(a < 0 for a in As):

    As = sorted(As, reverse=True)
    for a in As[:K]:
        r = (r * a) % D

elif K % 2 == 0 and all(a < 0 for a in As):

    As = sorted(As)
    for a in As[:K]:
        r = (r * a) % D

else:

    As = sorted(As, key = lambda a: abs(a), reverse=True)

    used = As[:K]
    rest = As[K:]
    num_negative_sign = sum([1 if x < 0 else 0 for x in used])

    if num_negative_sign % 2 != 0:

        used_min_pos = None
        used_min_neg = None
        for k in range(K-1, -1, -1):
            if   used_min_pos is None and used[k] >= 0: used_min_pos = k
            elif used_min_neg is None and used[k] <  0: used_min_neg = k
            if used_min_pos and used_min_neg: break

        rest_min_pos = None
        rest_min_neg = None
        for k in range(N-K):
            if   rest_min_pos is None and rest[k] >= 0: rest_min_pos = k
            elif rest_min_neg is None and rest[k] <  0: rest_min_neg = k
            if rest_min_pos and rest_min_neg: break

        # print(used)
        # print(used_min_pos)
        # print(used_min_neg)
        # print(rest)
        # print(rest_min_pos)
        # print(rest_min_neg)

        if used_min_pos is None or rest_min_neg is None:
            used[used_min_neg] = rest[rest_min_pos]
        elif used_min_neg is None or rest_min_pos is None:
            used[used_min_pos] = rest[rest_min_neg]
        else:
            v1 = abs(used[used_min_pos] * rest[rest_min_pos])
            v2 = abs(used[used_min_neg] * rest[rest_min_neg])
            if v1 > v2:
                used[used_min_pos] = rest[rest_min_neg]
            else:
                used[used_min_neg] = rest[rest_min_pos]

    for a in used:
        r = (r * a) % D

print(r)

##################################################### SOURCE FINISH ####################################################
# vim: expandtab tabstop=4 shiftwidth=4 fdm=marker

Submission status: Wrong Answer

Solution code:
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n,k = map(int,input().split())
A = list(map(int,input().split()))
A.sort(reverse = True)
plus = []
minus = []
zero= 0 
for ai in A:
  if ai <0:
    minus.append(ai)
  else:
    plus.append(ai)
pn = len(plus)
mn = len(minus)
minusdouble = []
plus0double = []
plus1double = []
for i in range(pn//2):
  plus0double.append(plus[i*2]*plus[i*2+1])
for i in range(1,(pn-1)//2):
  plus1double.append(plus[i*2-1]+plus[i*2])
for i in range(mn//2):
  minusdouble.append(minus[i*2]*minus[i*2+1])
alldouble0 = plus0double + minusdouble
alldouble1 = plus1double + minusdouble
alldouble0.sort(reverse = True)
alldouble1.sort(reverse = True)
if k == 1:
  print(max(A)%mod)
elif n-pn%2 < k:
  out = 1
  for i in range(k):
    out *= A[i]
    out%= mod
  print(out)
else:
  if pn == 0:
    ans1 = ans2 = 1
    for i in range(k):
      ans1 *= minus[i]
      ans1 %= mod
    for i in range(mn-1,mn-1-k,-1):
      ans2 *= minus[i]
      ans2 %= mod
    print(max(ans1,ans2))
  elif k % 2 == 1:
    out = 1
    out *= plus[0]
    for i in range(k//2):
      out *= alldouble1[i]
      out %= mod
    print(out)
  else:
    out = 1
    for i in range(k//2):
      out *= alldouble0[i]
      out %=mod
    print(out)

# print(alldouble0)

Submission status: Wrong Answer

Solution code:
MOD=10**9 + 7
n,k=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
s=0
e=n-1
ans=1
lp=[1]
ln=[1]
if a[e]<0 or n==k:
    for i in range(k):
        ans= ans * a[e-i] % MOD
    print(ans)
    exit()
isneg = False
while k>0:
    if a[s]+a[e]>=0:
        lp.append(a[e])
        e-=1
        k-=1
    else:
        ln.append(a[s])
        s+=1
        isneg= ~isneg
        k-=1

if isneg:
    if a[e]<=0:
        ln.append(a[s])
        lp[len(lp)-1]=1
    elif a[s]>=0:
        ln[len(ln)-1]=0
        lp.append(a[e])
    else:
        if a[s]+a[e]>=0:
            lp.append(a[e])
            ln[len(ln)-1]=0
        else:
            lp[len(lp)-1]=1
            ln.append(a[s])
for p in lp:
    ans = ans * p %MOD
for n in ln:
    ans = ans * n %MOD

print(ans)

Submission status: Wrong Answer

Solution code:
mod = 10**9+7

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

p = 1
m = 0
ans=1
cnt = 0

while cnt < K-1:
  if A[m]*A[m+1] > A[-p]*A[-p-1]:
    ans = ans*A[m]*A[m+1]%mod
    m +=2
    cnt +=2
    
  else:
    ans = ans*A[-m]%mod
    p +=1
    cnt +=1

if cnt == K-1:
  ans = ans*A[-p]%mod
  
if A[-1] < 0 and K%2 == 1:
  ans = 1
  for i in A[N-K:]:
    ans = ans*i%mod
    
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
mod = 10**9 + 7
nums = []
p_cnt = 0
m_cnt = 0
for i in list(map(int, input().split())):
    if i >= 0:
        nums.append((i, True))
        p_cnt += 1
    else:
        nums.append((-i, False))
nums.sort()
if p_cnt == 0 and K%2!=0:
    ans = 1
    for k in range(K):
        ans *= -nums[k][0]
        ans %= mod
    print(ans)
    import sys
    sys.exit()
pl = []
ml = []
for k in range(K):
    x, b = nums.pop()
    if b:
        pl.append(x)
    else:
        ml.append(x)
if len(ml) % 2 != 0:
    while nums:
        x, b = nums.pop()
        if b:
            if ml:
                ml.pop()
                pl.append(x)
                break
        else:
            if pl:
                pl.pop()
                ml.append(x)
                break
ans = 1
for x in pl:
    ans *= x
    ans %= mod
for x in ml:
    ans *= -x
    ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = [int(x) for x in input().split()]
a.sort()
mod = 10**9 + 7
if k%2==0:
  i = 0
  left = 0
  right = n-1
  ans = 1
  while i<k:
    m1 = a[left]*a[left+1]
    m2 = a[right]*a[right-1]
    #print(m1,m2)
    if m1<m2:
      m2%=mod
      ans*=m2
      #print("r",right)
      right -=2
    else:
      m1 %= mod
      ans *= m1
      #print(left)
      left+=2
    ans %= mod
    i+=2
  print(ans)
else:
  pos = []
  neg = []
  for i in a:
    if i<0:
      neg.append(i)
    else:
      pos.append(i)
  if len(pos)+2*(len(neg)//2)>=k:
    i = 0
    left = 0
    right = n-1
    ans = 1
    while i<k-1:
      m1 = a[left]*a[left+1]
      m2 = a[right]*a[right-1]
      #print(m1,m2)
      if m1<m2:
        m2%=mod
        ans*=m2
        #print("r",right)
        right -=2
      else:
        m1 %= mod
        ans *= m1
        #print(left)
        left+=2
      ans %= mod
      i+=2
    ans *= a[right]
    ans %= mod
    print(ans)
  else:
    for i in range(n):
      a[i] = abs(a[i])
    a.sort()
    ans = 1
    for i in range(k):
      ans *= a[i]
      ans %= mod
    ans = -ans
    ans %= mod
    print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = [int(x) for x in input().split()]
a.sort()
mod = 10**9 + 7
if k%2==0:
  i = 0
  left = 0
  right = n-1
  ans = 1
  while i<k:
    m1 = a[left]*a[left+1]
    m2 = a[right]*a[right-1]
    #print(m1,m2)
    if m1<m2:
      m2%=mod
      ans*=m2
      #print("r",right)
      right -=2
    else:
      m1 %= mod
      ans *= m1
      #print(left)
      left+=2
    ans %= mod
    i+=2
  print(ans)
else:
  pos = []
  neg = []
  for i in a:
    if i<0:
      neg.append(i)
    else:
      pos.append(i)
  if len(pos)+2*(len(neg)//2)>=k:
    i = 0
    left = 0
    right = n-1
    ans = 1
    while i<k-1:
      m1 = a[left]*a[left+1]
      m2 = a[right]*a[right-1]
      #print(m1,m2)
      if m1<m2:
        m2%=mod
        ans*=m2
        #print("r",right)
        right -=2
      else:
        m1 %= mod
        ans *= m1
        #print(left)
        left+=2
      ans %= mod
      i+=2
    ans *= a[right]
    ans %= mod
    print(ans)
  else:
    for i in range(n):
      a[i] = abs(a[i])
    a.sort()
    ans = 1
    for i in range(k):
      ans *= a[i]
      ans %= mod
    print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n, k = LI()
a = LI()
a.sort(key = lambda x:abs(x), reverse = True)
ans = 1
p = 0
m = 0
plus = []
minus = []
f = 0
for i in range(n):
    if a[i] >= 0:
        p += 1
        if i < k:
            plus.append(a[i])
            ans *= a[i]
            ans %= mod
    else:
        m += 1
        if i < k:
            minus.append(a[i])
            f ^= 1
            ans *= -a[i]
            ans %= mod
if f:
    ans = -ans
if ans < 0:
    if k == n:
        print(ans)
    elif p == 0:
        a.sort(key = lambda x:abs(x))
        ans = 1
        for i in range(k):
            ans *= -a[i]
            ans %= mod
        if k % 2:
            ans = -ans
        print(ans % mod)
    else:
        ans1 = ans // minus[-1]
        ans2 = -inf
        f1 = 0
        f2 = 0
        for i in range(k, n):
            if a[i] >= 0:
                ans1 *= a[i]
                f1 = 1
                break
        if plus:
            ans2 = ans // plus[-1]
            ans2 = -ans2
            for i in range(k, n):
                if a[i] < 0:
                    ans2 *= -a[i]
                    f2 = 1
                    break
        if f1 and f2:
            print(max(ans1, ans2) % mod)
        elif f1:
            print(ans1 % mod)
        else:
            print(ans2 % mod)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import operator
from functools import reduce
N,K = map(int,input().split())
lsA = list(map(int,input().split()))
if N==K:
    print(reduce(operator.mul,lsA)%(10**9+7))
else:
    if max(lsA)<=0:
        lsA.sort(reverse=True)
        print(reduce(operator.mul,lsA[:K])%(10**9+7))
    else:
        lsA.sort(key=abs,reverse=True)
        lsB1 = lsA[:K]
        lsB2 = lsA[:K]
        if len([i for i in lsB1 if i > 0]) % 2 == 0:
            print(reduce(operator.mul,lsB1)%(10**9+7))
        elif len([i for i in lsB1 if i > 0]) % 2 == 1:
            for j in range(K):
                if lsB1[K-1-j] < 0:
                    lsB1.remove(lsB1[K-1-j])
                    for k in range(K,N):
                        if lsA[k] > 0:
                            lsB1.append(lsA[k])
                            break
                    break
            a1 = reduce(operator.mul,lsB1)
            for j in range(K):
                if lsB2[K-1-j] > 0:
                    lsB2.remove(lsB2[K-1-j])
                    for k in range(K,N):
                        if lsA[k] < 0:
                            lsB2.append(lsA[k])
                            break
                    break                
            a2 = reduce(operator.mul,lsB2)
            print(max(a1,a2)%(10**9+7))