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 functools

def sign(x):
    if x>0:
        return 1
    elif x==0:
        return 0
    else:
        return -1
    
def multiply(x,y):
    ans = x*y
    if ans>=10**9+7:
        ans %= 10**9+7
    return ans

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

if A[-1]<0 and K%2==1:
    combi = A[-K:]
    answer =  functools.reduce(multiply,map(abs,combi[:-1]))
    answer *= combi[-1]
    answer %= 10**9+7
else:
    signs = 1
    i,j = 0,1
    for _ in range(K):
        if abs(A[i]) > abs(A[N-j]):
            i += 1
            
        else:
            j += 1
    combi = A[N-j+1:] + A[:i]
    del A[-j+1:]
    del A[:i]
    combi = combi[::-1]     
       
    if signs == -1:
        if combi[0] <0:
            del combi[-1]
            combi.append(A[-1])
        else:
            if A[0]>0:
                del combi[-1]
                combi.append(A[-1])
            elif A[0]<0 and A[-1] >0:
                if combi[-1]*A[0] > combi[0]*A[-1]:
                    del combi[0]
                    combi.insert(0,A[0])
                else:
                    del combi[-1]
                    combi.append(A[-1])
            else:
                del combi[0]
                combi.insert(0,A[0])

    answer =  functools.reduce(multiply,map(abs,combi))
print(answer)

Submission status: Wrong Answer

Solution code:
import functools

def sign(x):
    if x>0:
        return 1
    elif x==0:
        return 0
    else:
        return -1
    
def multiply(x,y):
    ans = x*y
    if ans>=10**9+7:
        ans %= 10**9+7
    return ans

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

if A[-1]<0 and K%2==1:
    combi = A[-K:]
    answer =  functools.reduce(multiply,map(abs,combi[:-1]))
    answer *= combi[-1]
    answer %= 10**9+7
else:
    signs = 1
    i,j = 0,1
    for _ in range(K):
        if abs(A[i]) > abs(A[N-j]):
            i += 1
            
        else:
            j += 1
    combi = A[N-j+1:] + A[:i]
    del A[-j+1]
    del A[:i]
    combi = combi[::-1]     
       
    if signs == -1:
        if combi[0] <0:
            del combi[-1]
            combi.append(A[-1])
        else:
            if A[0]>0:
                del combi[-1]
                combi.append(A[-1])
            elif A[0]<0 and A[-1] >0:
                if combi[-1]*A[0] > combi[0]*A[-1]:
                    del combi[0]
                    combi.insert(0,A[0])
                else:
                    del combi[-1]
                    combi.append(A[-1])
            else:
                del combi[0]
                combi.insert(0,A[0])

    answer =  functools.reduce(multiply,map(abs,combi))
print(answer)

Submission status: Wrong Answer

Solution code:
import functools

def sign(x):
    if x>0:
        return 1
    elif x==0:
        return 0
    else:
        return -1
    
def multiply(x,y):
    ans = x*y
    if ans>=10**9+7:
        ans %= 10**9+7
    return ans

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

if A[-1]<0 and K%2==1:
    combi = A[-K:]
    answer =  functools.reduce(multiply,map(abs,combi[:-1]))
    answer *= combi[-1]
    answer %= 10**9+7
else:
    signs = 1
    i,j = 0,1
    for _ in range(K):
        if abs(A[i]) > abs(A[N-j]):
            i += 1
            
        else:
            j += 1
    combi = A[N-j+1:] + A[:i]
    del A[-j+1]
    del A[:i]
            
       
    if signs == -1:
        if combi[0] <0:
            del combi[-1]
            combi.append(A[-1])
        else:
            if A[0]>0:
                del combi[-1]
                combi.append(A[-1])
            elif A[0]<0 and A[-1] >0:
                if combi[-1]*A[0] > combi[0]*A[-1]:
                    del combi[0]
                    combi.insert(0,A[0])
                else:
                    del combi[-1]
                    combi.append(A[-1])
            else:
                del combi[0]
                combi.insert(0,A[0])

    answer =  functools.reduce(multiply,map(abs,combi))
print(answer)

Submission status: Wrong Answer

Solution code:
import functools

def sign(x):
    if x>0:
        return 1
    elif x==0:
        return 0
    else:
        return -1
    
def multiply(x,y):
    ans = x*y
    if ans>=10**9+7:
        ans %= 10**9+7
    return ans

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

if A[-1]<0 and K%2==1:
    combi = A[-K:]
    answer =  functools.reduce(multiply,map(abs,combi[:-1]))
    answer *= combi[-1]
    answer %= 10**9+7
else:
    combi = []
    signs = 1
    for i in range(K):
        if abs(A[0]) > abs(A[-1]):
            signs *= sign(A[0])
            combi.insert(0,A.pop(0))
            
        else:
            signs *= sign(A[-1])
            combi.append(A.pop(-1))

    if signs == -1:
        if combi[-1] <0:
            del combi[0]
            combi.append(A[-1])
        else:
            if A[0]>0:
                del combi[0]
                combi.append(A[-1])
            elif A[0]<0 and A[-1] >0:
                if combi[0]*A[0] > combi[-1]*A[-1]:
                    del combi[-1]
                    combi.insert(0,A[0])
                else:
                    del combi[0]
                    combi.append(A[-1])
            else:
                del combi[-1]
                combi.insert(0,A[0])

    answer =  functools.reduce(multiply,map(abs,combi))
print(answer)

Submission status: Wrong Answer

Solution code:
import functools

def sign(x):
    if x>0:
        return 1
    elif x==0:
        return 0
    else:
        return -1

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

if A[-1]<0 and K%2==1:
    combi = A[-K:]
    answer =  functools.reduce(lambda x,y: x*y%(10**9+7),map(abs,combi[:-1]))
    answer *= combi[-1]
    answer %= 10**9+7
else:
    combi = []

    for i in range(K):
        if abs(A[0]) > abs(A[-1]):
            combi.insert(0,A.pop(0))
        else:
            combi.append(A.pop(-1))

    signs = functools.reduce(lambda x,y: x*y,map(sign,combi))

    if signs == -1:
        if combi[-1] <0:
            del combi[0]
            combi.append(A[-1])
        else:
            if A[0]>0:
                del combi[0]
                combi.append(A[-1])
            elif A[0]<0 and A[-1] >0:
                if combi[0]*A[0] > combi[-1]*A[-1]:
                    del combi[-1]
                    combi.insert(0,A[0])
                else:
                    del combi[0]
                    combi.append(A[-1])
            else:
                del combi[-1]
                combi.insert(0,A[0])

    answer =  functools.reduce(lambda x,y: x*y%(10**9+7),map(abs,combi))
print(answer)

Submission status: Wrong Answer

Solution code:
import functools

def sign(x):
    if x>0:
        return 1
    elif x==0:
        return 0
    else:
        return -1

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

if A[-1]<0 and K%2==1:
    combi = A[-K:]
    answer =  functools.reduce(lambda x,y: x*y%(10**9+7),combi)
else:
    combi = []

    for i in range(K):
        if abs(A[0]) > abs(A[-1]):
            combi.insert(0,A.pop(0))
        else:
            combi.append(A.pop(-1))

    signs = functools.reduce(lambda x,y: x*y,map(sign,combi))

    if signs == -1:
        if combi[-1] <0:
            del combi[0]
            combi.append(A[-1])
        else:
            if A[0]>0:
                del combi[0]
                combi.append(A[-1])
            elif A[0]<0 and A[-1] >0:
                if combi[0]*A[0] > combi[-1]*A[-1]:
                    del combi[-1]
                    combi.insert(0,A[0])
                else:
                    del combi[0]
                    combi.append(A[-1])
            else:
                del combi[-1]
                combi.insert(0,A[0])

    answer =  functools.reduce(lambda x,y: x*y%(10**9+7),map(abs,combi))
print(answer)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
MOD=10**9+7
A=list(map(int,input().split()))
A.sort(reverse=True,key=lambda x:abs(x))

if max(A)<=0:
  """
  if A[K-1]==0:
    print(0)
    exit()
  """
  ans=1
  A.sort(reverse=True)
  for i in range(0,K):
    ans*=A[i]
    ans%=MOD
  ans+=MOD
  print(ans%MOD)
  exit()

ans=1
for i in range(0,K):
  ans*=A[i]
  ans%=MOD
if ans>=0 or K==N:
  ans+=MOD
  print(ans%MOD)
  exit()
elif A[K]==0:
  print(0)
  exit()

mainA=A[:K]
restA=A[K:]

#数えている正の中で絶対値最小
minP,ind_minP=-1,-1
if max(mainA)>0:
  minP=min(map(lambda x:x if x>0 else float("inf"),mainA))
  ind_minP=index(mainA,minP)
#数えている負の中で絶対値最小
minM,ind_minM=1,-1
if min(mainA)<0:
  minM=-min(map(lambda x:abs(x) if x<0 else float("inf"),mainA))
  ind_minM=index(mainA,minM)

#数えていない正の中で絶対値最大
maxrestP=max(max(restA),-1)
#数えていない負の中で絶対値最大
maxrestM=min(min(restA),1)
if not(maxrestM<=-1 and minP>=1):#残りが全て負か0
  mainA.pop[ind_minM]
  mainA.append(maxrestP)
elif not(maxrestP>=1 and minM<= -1):
  mainA.pop[ind_minP]
  mainA.append(maxrestM)
else:
  if maxrestP*minP>maxrestM*minM:
    mainA.pop[ind_minM]
    mainA.append(maxrestP)
  else:
    mainA.pop[ind_minP]
    mainA.append(maxrestM)
  
ans=1
for i in range(0,K):
  ans*=mainA[i]
  ans%=MOD

ans+=MOD
print(ans%MOD)
exit()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
MOD=10**9+7
A=list(map(int,input().split()))
A.sort(reverse=True,key=lambda x:abs(x))

if max(A)<=0:
  """
  if A[K-1]==0:
    print(0)
    exit()
  """
  ans=1
  A.sort(reverse=True)
  for i in range(0,K):
    ans*=A[i]
    ans%=MOD
  ans+=MOD
  print(ans%MOD)
  exit()

ans=1
for i in range(0,K):
  ans*=A[i]
  ans%=MOD
if ans>=0 or K==N:
  ans+=MOD
  print(ans%MOD)
  exit()
elif A[K]==0:
  print(0)
  exit()

mainA=A[:K]
restA=A[K:]

#数えている正の中で絶対値最小
minP,ind_minP=-1,-1
if max(mainA)>0:
  minP=min(map(lambda x:x if x>=0 else float("inf"),mainA))
  ind_minP=index(mainA,minP)
#数えている負の中で絶対値最小
minM,ind_minM=1,-1
if min(mainA)<0:
  minM=-min(map(lambda x:abs(x) if x<=0 else float("inf"),mainA))
  ind_minM=index(mainA,-minM)

#数えていない正の中で絶対値最大
maxrestP=max(max(restA),-1)
#数えていない負の中で絶対値最大
maxrestM=min(min(restA),1)
if not(maxrestM<=-1 and minP>=1):#残りが全て負か0
  mainA.pop[ind_minM]
  mainA.append(maxrestP)
elif not(maxrestP>=1 and minM<= -1):
  mainA.pop[ind_minP]
  mainA.append(maxrestM)
else:
  if maxrestP*minP>maxrestM*minM:
    mainA.pop[ind_minM]
    mainA.append(maxrestP)
  else:
    mainA.pop[ind_minP]
    mainA.append(maxrestM)
  
ans=1
for i in range(0,K):
  ans*=mainA[i]
  ans%=MOD

ans+=MOD
print(ans%MOD)
exit()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
MOD=10**9+7
A=list(map(int,input().split()))
A.sort(reverse=True,key=lambda x:abs(x))

if max(A)<=0:
  """
  if A[K-1]==0:
    print(0)
    exit()
  """
  ans=1
  A.sort(reverse=True)
  for i in range(0,K):
    ans*=A[i]
    ans%=MOD
  ans+=MOD
  print(ans%MOD)
  exit()

ans=1
for i in range(0,K):
  ans*=A[i]
  ans%=MOD
if ans>=0 or K==N or A[K]==0:
  ans+=MOD
  print(ans%MOD)
  exit()

mainA=A[:K]
restA=A[K:]

#数えている正の中で絶対値最小
minP,ind_minP=-1,-1
if max(mainA)>0:
  minP=min(map(lambda x:x if x>=0 else float("inf"),mainA))
  ind_minP=index(mainA,minP)
#数えている負の中で絶対値最小
minM,ind_minM=1,-1
if min(mainA)<0:
  minM=-min(map(lambda x:abs(x) if x<=0 else float("inf"),mainA))
  ind_minM=index(mainA,-minM)

#数えていない正の中で絶対値最大
maxrestP=max(max(restA),-1)
#数えていない負の中で絶対値最大
maxrestM=min(min(restA),1)
if not(maxrestM<=-1 and minP>=1):#残りが全て負か0
  mainA.pop[ind_minM]
  mainA.append(maxrestP)
elif not(maxrestP>=1 and minM<= -1):
  mainA.pop[ind_minP]
  mainA.append(maxrestM)
else:
  if maxrestP*minP>maxrestM*minM:
    mainA.pop[ind_minM]
    mainA.append(maxrestP)
  else:
    mainA.pop[ind_minP]
    mainA.append(maxrestM)
  
ans=1
for i in range(0,K):
  ans*=mainA[i]
  ans%=MOD

ans+=MOD
print(ans%MOD)
exit()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
MOD=10**9+7
A=list(map(int,input().split()))
A.sort(reverse=True,key=lambda x:abs(x))

if max(A)<=0:
  """
  if A[K-1]==0:
    print(0)
    exit()
  """
  ans=1
  A.sort(reverse=True)
  for i in range(0,K):
    ans*=A[i]
    ans%=MOD
  ans+=MOD
  print(ans%MOD)
  exit()

ans=1
for i in range(0,K):
  ans*=A[i]
  ans%=MOD
if ans>=0 or K==N or A[K]==0:
  ans+=MOD
  print(ans%MOD)
  exit()

mainA=A[:K]
restA=A[K:]

#数えている正の中で絶対値最小
minP,ind_minP=-1,-1
if max(mainA)>0:
  minP=min(map(lambda x:x if x>=0 else float("inf"),mainA))
  ind_minP=index(mainA,minP)
#数えている負の中で絶対値最小
minM,ind_minM=-1,-1
if min(mainA)<0:
  minM=-min(map(lambda x:abs(x) if x<=0 else float("inf"),mainA))
  ind_minM=index(mainA,-minM)

#数えていない正の中で絶対値最大
maxrestP=max(max(restA),-1)
#数えていない負の中で絶対値最大
maxrestM=min(min(restA),1)
if not(maxrestM<=-1 and minP>=1):#残りが全て負か0
  mainA.pop[ind_minM]
  mainA.append(maxrestP)
elif not(maxrestP>=1 and minM<= -1):
  mainA.pop[ind_minP]
  mainA.append(maxrestM)
else:
  if maxrestP*minP>maxrestM*minM:
    mainA.pop[ind_minM]
    mainA.append(maxrestP)
  else:
    mainA.pop[ind_minP]
    mainA.append(maxrestM)
  
ans=1
for i in range(0,K):
  ans*=mainA[i]
  ans%=MOD

ans+=MOD
print(ans%MOD)
exit()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
MOD=10**9+7
A=list(map(int,input().split()))
A.sort(reverse=True,key=lambda x:abs(x))

if max(A)<=0:
  """
  if A[K-1]==0:
    print(0)
    exit()
  """
  ans=1
  A.sort(reverse=True)
  for i in range(0,K):
    ans*=A[i]
    ans%=MOD
  ans+=MOD
  print(ans%MOD)
  exit()

ans=1
for i in range(0,K):
  ans*=A[i]
  ans%=MOD
if ans>=0 or K==N or A[K]==0:
  ans+=MOD
  print(ans%MOD)
  exit()

mainA=A[:K]
restA=A[K:]

#数えている正の中で絶対値最小
minP,ind_minP=-1,-1
if max(mainA)>0:
  minP=min(map(lambda x:x if x>=0 else float("inf"),mainA))
  ind_minP=index(mainA,minP)
#数えている負の中で絶対値最小
minM,ind_minM=-1,-1
if min(mainA)<0:
  minM=-min(map(lambda x:abs(x) if x<=0 else float("inf"),mainA))
  ind_minM=index(mainA,-minM)

#数えていない正の中で絶対値最大
maxrestP=max(max(restA),-1)
#数えていない負の中で絶対値最大
maxrestM=min(min(restA),1)
if not(maxrestM<=-1 and minP>1):#残りが全て負か0
  mainA.pop[ind_minM]
  mainA.append(maxrestP)
elif not(maxrestP>=1 and minM<= -1):
  mainA.pop[ind_minP]
  mainA.append(maxrestM)
else:
  if maxrestP*minP>maxrestM*minM:
    mainA.pop[ind_minM]
    mainA.append(maxrestP)
  else:
    mainA.pop[ind_minP]
    mainA.append(maxrestM)
  
ans=1
for i in range(0,K):
  ans*=mainA[i]
  ans%=MOD

ans+=MOD
print(ans%MOD)
exit()

Submission status: Wrong Answer

Solution code:

MOD = 10**9 + 7
n, k = map(int, input().split())
a = list(map(int, input().split()))
plus,minus=[],[]
for i in a:
    if i<0:
        minus.append(i)
    else:
        plus.append(i)
plus.sort(reverse = True)
minus.sort()
plus.append(-10**15)
plus.append(10**15)
minus.append(10**15)
minus.append(-10**15)
ans = 1
if n == k:
    for i in range(n):
        ans *= a[i]
        ans %= MOD
    print(ans)
elif len(plus) == 2 and k % 2 == 1:
    for i in range(3, k + 3):
        ans *= minus[-i]
        ans %= MOD
    print(ans)
elif k == n - 1:
    if len(minus) % 2 == 1:
        for i in range(len(minus) - 3):
            ans *= minus[i]
            ans %= MOD
        for i in range(len(plus) - 2):
            ans *= plus[i]
            ans %= MOD
    else:
        for i in range(len(minus) - 2):
            ans *= minus[i]
            ans %= MOD
        for i in range(len(plus) - 3):
            ans *= plus[i]
            ans %= MOD
    print(ans)
else:
    cplus = 0
    cminus = 0
    p = k // 2
    if k % 2 == 1:
        ans *= plus[0]
        cplus = 1
    else:
        for _ in range(p):
            if plus[cplus] * plus[cplus + 1] > minus[cminus] * minus[cminus + 1]:
                ans *= plus[cplus] * plus[cplus + 1]
                ans %= MOD
                cplus += 2
            else:
                ans *= minus[cminus] * minus[cminus + 1]
                ans %= MOD
                cminus += 2
    print(ans)

Submission status: Wrong Answer

Solution code:


MOD = 10**9 + 7
n, k = map(int, input().split())
a = list(map(int, input().split()))
plus,minus=[],[]
for i in a:
    if i<0:
        minus.append(i)
    else:
        plus.append(i)
plus.sort(reverse = True)
minus.sort()
plus.append(-float('inf'))
plus.append(float('inf'))
minus.append(float('inf'))
minus.append(-float('inf'))
ans = 1
if n == k:
    for i in range(n):
        ans *= a[i]
        ans %= MOD
    print(ans)
elif len(plus) == 2 and k % 2 == 1:
    for i in range(3, k + 3):
        ans *= minus[-i]
        ans %= MOD
    print(ans)
elif k == n - 1:
    if len(minus) % 2 == 1:
        for i in range(len(minus) - 3):
            ans *= minus[i]
            ans %= MOD
        for i in range(len(plus) - 2):
            ans *= plus[i]
            ans %= MOD
    else:
        for i in range(len(minus) - 2):
            ans *= minus[i]
            ans %= MOD
        for i in range(len(plus) - 3):
            ans *= plus[i]
            ans %= MOD
    print(ans)
else:
    cplus = 0
    cminus = 0
    p = k // 2
    if k % 2 == 1:
        ans *= plus[0]
        cplus = 1
    else:
        for _ in range(p):
            if plus[cplus] * plus[cplus + 1] > minus[cminus] * minus[cminus + 1]:
                ans *= plus[cplus] * plus[cplus + 1]
                ans %= MOD
                cplus += 2
            else:
                ans *= minus[cminus] * minus[cminus + 1]
                ans %= MOD
                cminus += 2
    print(ans)

Submission status: Wrong Answer

Solution code:

MOD = 10**9 + 7
n, k = map(int, input().split())
a = list(map(int, input().split()))
plus,minus=[],[]
for i in a:
    if i<0:
        minus.append(i)
    else:
        plus.append(i)
plus.sort(reverse = True)
minus.sort()
plus.append(-10**10)
plus.append(10**10)
minus.append(10**10)
minus.append(-10**10)
ans = 1
if n == k:
    for i in range(n):
        ans *= a[i]
        ans %= MOD
    print(ans)
elif len(plus) == 2 and k % 2 == 1:
    for i in range(3, k + 3):
        ans *= minus[-i]
        ans %= MOD
    print(ans)
else:
    cplus = 0
    cminus = 0
    p = k // 2
    if k % 2 == 1:
        ans *= plus[0]
        cplus = 1
    else:
        for _ in range(p):
            if plus[cplus] * plus[cplus + 1] > minus[cminus] * minus[cminus + 1]:
                ans *= plus[cplus] * plus[cplus + 1]
                ans %= MOD
                cplus += 2
            else:
                ans *= minus[cminus] * minus[cminus + 1]
                ans %= MOD
                cminus += 2
    print(ans)

Submission status: Wrong Answer

Solution code:

MOD = 10**9 + 7
n, k = map(int, input().split())
a = list(map(int, input().split()))
plus,minus=[],[]
for i in a:
    if i<0:
        minus.append(i)
    else:
        plus.append(i)
plus.sort(reverse = True)
minus.sort()
plus.append(-float('inf'))
plus.append(float('inf'))
minus.append(float('inf'))
minus.append(-float('inf'))
ans = 1
if n == k:
    for i in range(n):
        ans *= a[i]
        ans %= MOD
    print(ans)
elif len(plus) == 2 and k % 2 == 1:
    for i in range(3, k + 3):
        ans *= minus[-i]
        ans %= MOD
    print(ans)
else:
    cplus = 0
    cminus = 0
    p = k // 2
    if k % 2 == 1:
        ans *= plus[0]
        cplus = 1
    else:
        for _ in range(p):
            if plus[cplus] * plus[cplus + 1] > minus[cminus] * minus[cminus + 1]:
                ans *= plus[cplus] * plus[cplus + 1]
                ans %= MOD
                cplus += 2
            else:
                ans *= minus[cminus] * minus[cminus + 1]
                ans %= MOD
                cminus += 2
    print(ans)
print(0 * float('inf'))

Submission status: Wrong Answer

Solution code:

MOD = 10**9 + 7
n, k = map(int, input().split())
a = list(map(int, input().split()))
plus,minus=[],[]
for i in a:
    if i<0:
        minus.append(i)
    else:
        plus.append(i)
plus.sort(reverse = True)
minus.sort()
plus.append(-float('inf'))
plus.append(float('inf'))
minus.append(float('inf'))
minus.append(-float('inf'))
ans = 1
if n == k:
    for i in range(n):
        ans *= a[i]
        ans %= MOD
    print(ans)
elif len(plus) == 2 and k % 2 == 1:
    for i in range(3, k + 3):
        ans *= minus[-i]
        ans %= MOD
    print(ans)
else:
    cplus = 0
    cminus = 0
    p = k // 2
    if k % 2 == 1:
        ans *= plus[0]
        cplus = 1
    else:
        for _ in range(p):
            if plus[cplus] * plus[cplus + 1] > minus[cminus] * minus[cminus + 1]:
                ans *= plus[cplus] * plus[cplus + 1]
                ans %= MOD
                cplus += 2
            else:
                ans *= minus[cminus] * minus[cminus + 1]
                ans %= MOD
                cminus += 2
    print(ans)

Submission status: Wrong Answer

Solution code:
"""
E.
"""
from functools import reduce
from collections import deque


def prod(x, y):
    return x * y % MOD

    
N, K = map(int, input().split())
a_list = list(map(int, input().split()))
a_list.sort(reverse=True)
# abs_a_list = sorted(a_list, key=abs, reverse=True)
MOD = 10**9 + 7

if K%2 == 1:
    flg = True
    for a in a_list:
        if a >= 0:
            flg = False
            break
    if flg:
        print(reduce(prod, a_list[:K]))
        exit()

S = []
a_list = deque(a_list)
for _ in range(K//2):
    if a_list[0]*a_list[1] > a_list[-1]*a_list[-2]:
        S.append(a_list.popleft())
        S.append(a_list.popleft())
    else:
        S.append(a_list.pop())
        S.append(a_list.pop())
if 0 in S:
    print(0)
    exit()
        
if K%2 == 1:
    S.append(a_list.popleft())
ans = reduce(prod, S)
print(ans)

Submission status: Wrong Answer

Solution code:
"""
E.
"""
from functools import reduce
from collections import deque


def prod(x, y):
    return x * y % MOD

    
N, K = map(int, input().split())
a_list = list(map(int, input().split()))
a_list.sort(reverse=True)
# abs_a_list = sorted(a_list, key=abs, reverse=True)
MOD = 10**9 + 7

if K%2 == 1:
    flg = True
    for a in a_list:
        if a >= 0:
            flg = False
            break
    if flg:
        print(reduce(prod, a_list[:K]))
        exit()

S = []
a_list = deque(a_list)
for _ in range(K//2):
    if a_list[0]*a_list[1] > a_list[-1]*a_list[-2]:
        S.append(a_list.popleft())
        S.append(a_list.popleft())
    else:
        S.append(a_list.pop())
        S.append(a_list.pop())
if K%2 == 1:
    S.append(a_list.popleft())
ans = reduce(prod, S)
print(ans)

Submission status: Wrong Answer

Solution code:
"""
E.
"""
from functools import reduce
from collections import deque


def prod(x, y):
    return x * y % MOD

    
N, K = map(int, input().split())
a_list = list(map(int, input().split()))
a_list.sort(reverse=True)
# abs_a_list = sorted(a_list, key=abs, reverse=True)
MOD = 10**9 + 7

if K%2 == 1:
    flg = True
    for a in a_list:
        if a >= 0:
            flg = False
            break
    if flg:
        print(reduce(prod, a_list[:K]))
        exit()

S = []
a_list = deque(a_list)
for _ in range(K//2):
    if a_list[0]*a_list[1] >= a_list[-1]*a_list[-2]:
        S.append(a_list.popleft())
        S.append(a_list.popleft())
    else:
        S.append(a_list.pop())
        S.append(a_list.pop())
if K%2 == 1:
    S.append(a_list.popleft())
ans = reduce(prod, S)
print(ans)

Submission status: Wrong Answer

Solution code:
"""
E.
"""
from functools import reduce
from collections import deque


def prod(x, y):
    return x * y % MOD

    
N, K = map(int, input().split())
a_list = list(map(int, input().split()))
a_list.sort(reverse=True)
abs_a_list = sorted(a_list, key=abs, reverse=True)
MOD = 10**9 + 7

if K%2 == 1:
    flg = True
    for a in a_list:
        if a >= 0:
            flg = False
            break
    if flg:
        print(reduce(prod, a_list[:K])%MOD)
        exit()

# ans2
S = []
a_list = deque(a_list)
for _ in range(K//2):
    if a_list[0]*a_list[1] >= a_list[-1]*a_list[-2]:
        S.append(a_list.popleft())
        S.append(a_list.popleft())
    else:
        S.append(a_list.pop())
        S.append(a_list.pop())
if K%2 == 1:
    S.append(a_list.popleft())

ans = reduce(prod, S)
print(ans)

Submission status: Wrong Answer

Solution code:
"""
E.
"""
from functools import reduce


def prod(x, y):
    return x * y % MOD

    
N, K = map(int, input().split())
a_list = list(map(int, input().split()))
a_list.sort(reverse=True)
abs_a_list = sorted(a_list, key=abs, reverse=True)
MOD = 10**9 + 7

if K%2 == 1:
    flg = True
    for a in a_list:
        if a >= 0:
            flg = False
            break
    if flg:
        print(reduce(prod, a_list[:K])%MOD)
        exit()

# ans1
ans = reduce(prod, abs_a_list[:K])
if ans >= 0:
    print(ans%MOD)
else:
    next_neg = None
    next_pos = None
    
    for i in abs_a_list[K:]:
        if i >= 0:
            next_pos = i
    for i in abs_a_list[K:]:
        if i < 0:
            next_neg = i
    
    for i in abs_a_list[K-1:-1:-1]:
        if i < 0 and next_pos!=None:
            ans = max(ans, ans // i * next_pos)
            break
    for i in abs_a_list[K-1:-1:-1]:
        if i >= 0 and next_neg!=None:
            ans = max(ans, ans // i * next_neg)
            break
    print(ans%MOD)

Submission status: Wrong Answer

Solution code:
from functools import reduce


def prod(x, y):
    return x * y % MOD

    
N, K = map(int, input().split())
a_list = list(map(int, input().split()))
a_list.sort(reverse=True)
abs_a_list = sorted(a_list, key=abs, reverse=True)
MOD = 10**9 + 7

if K%2 == 1:
    flg = True
    for a in a_list:
        if a >= 0:
            flg = False
            break
    if flg:
        print(reduce(prod, a_list[:K]))
        exit()

# ans1
ans = reduce(prod, abs_a_list[:K])
if ans >= 0:
    print(ans%MOD)
else:
    next_neg = None
    next_pos = None
    
    for i in abs_a_list[K:]:
        if i >= 0:
            next_pos = i
    for i in abs_a_list[K:]:
        if i < 0:
            next_neg = i
    
    for i in abs_a_list[K:-1:-1]:
        if i < 0 and next_pos!=None:
            ans = max(ans, ans // i * next_pos)
            break
    for i in abs_a_list[K:-1:-1]:
        if i >= 0 and next_neg!=None:
            ans = max(ans, ans // i * next_neg)
            break
    print(ans%MOD)

Submission status: Wrong Answer

Solution code:
from functools import reduce


def prod(x, y):
    return x * y

    
N, K = map(int, input().split())
a_list = list(map(int, input().split()))
a_list.sort(reverse=True)
abs_a_list = sorted(a_list, key=abs, reverse=True)
MOD = 10**9 + 7

if K%2 == 1:
    flg = True
    for a in a_list:
        if a >= 0:
            flg = False
            break
    if flg:
        print(reduce(prod, a_list[:K])%MOD)
        exit()

# ans1
ans = reduce(prod, abs_a_list[:K])
if ans >= 0:
    print(ans%MOD)
else:
    next_neg = None
    next_pos = None
    
    for i in abs_a_list[K:]:
        if i >= 0:
            next_pos = i
    for i in abs_a_list[K:]:
        if i < 0:
            next_neg = i
    
    for i in abs_a_list[K:-1:-1]:
        if i < 0 and next_pos!=None:
            ans = max(ans, ans // i * next_pos)
            break
    for i in abs_a_list[K:-1:-1]:
        if i >= 0 and next_neg!=None:
            ans = max(ans, ans // i * next_neg)
            break
    print(ans%MOD)

Submission status: Wrong Answer

Solution code:
import numpy as np
import copy

MOD = 10**9 + 7
n, k = map(int, input().split())
a = np.array(list(map(int, input().split())))
plus,minus=[],[]
for i in a:
    if i<0: minus+=[i]
    else: plus+=[i]
plus.sort(reverse = True)
minus.sort()
plus.append(-float('inf'))
plus.append(float('inf'))
minus.append(float('inf'))
minus.append(-float('inf'))
ans = 1
cplus = 0
cminus = 0
kk = copy.copy(k)
if len(plus) == 2 and k % 2 == 1:
    for l in range(3, k + 3):
        ans *= minus[-l]
        ans %= MOD
    print(ans)
    exit()
while kk > 1:
    if plus[cplus] * plus[cplus + 1] > minus[cminus] * minus[cminus + 1]:
        ans = ans * plus[cplus] % MOD
        cplus += 1
        kk -= 1
    else:
        ans = ans * minus[cminus] * minus[cminus + 1] % MOD
        cminus += 2
        kk -= 2
if kk == 1:
    ans = ans * plus[cplus] % MOD
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import copy

MOD = 10**9 + 7
n, k = map(int, input().split())
a = np.array(list(map(int, input().split())))
ans = 1
plus = a[a >= 0]
plus = np.sort(plus)[::-1]
plus = np.append(plus, [-np.inf, np.inf])
minus = a[a < 0]
minus = np.sort(minus)
minus = np.append(minus, [np.inf, -np.inf])
cplus = 0
cminus = 0
kk = copy.copy(k)
if len(plus) == 2 and k % 2 == 1:
    for l in range(3, k + 3):
        ans *= minus[-l]
        ans %= MOD
    print(ans)
    exit()
while kk > 1:
    if plus[cplus] * plus[cplus + 1] > minus[cminus] * minus[cminus + 1]:
        ans = ans * plus[cplus] % MOD
        cplus += 1
        kk -= 1
    else:
        ans = ans * minus[cminus] * minus[cminus + 1] % MOD
        cminus += 2
        kk -= 2
if kk == 1:
    ans = ans * plus[cplus] % MOD
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

MOD = 10**9 + 7
n, k = map(int, input().split())
a = np.array(list(map(int, input().split())))
ans = 1
plus = a[a >= 0]
plus = np.sort(plus)[::-1]
plus = np.append(plus, [-np.inf, np.inf])
minus = a[a < 0]
minus = np.sort(minus)
minus = np.append(minus, [np.inf, -np.inf])
if n == k:
    for i in range(len(a)):
        ans *= a[i]
        ans %= MOD
elif len(plus) == 2 and k % 2 == 1:
    for i in range(3, k + 3):
        ans *= minus[-i]
        ans %= MOD
else:
    c = 0
    cplus = 0
    cminus = 0
    while c < k - 1:
        if plus[cplus] * plus[cplus + 1] > minus[cminus] * minus[cminus + 1]:
            ans *= plus[cplus]
            ans %= MOD
            cplus += 1
            c += 1
        else:
            ans *= minus[cminus] * minus[cminus + 1]
            ans %= MOD
            cminus += 2
            c += 2
    if c == k - 1:
        ans *= plus[cplus]
        ans %= MOD
print(int(ans))