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 numpy as np
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7
if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<=0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD

B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)
if not Bp:Bp.insert(0,B1[-1])
if not Bm:Bm.insert(0,B1[0])
  
#print(Bp,Bm,B2)

B1.remove(Bp[-1])
B1.append(min(B2[0],0))
#print(B1)
ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD
print(B2)
C1=A[0:K]
C1.remove(Bm[-1])
C1.append(max(B2[-1],0))

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD

if abs(Bp[-1]*B2[-1])-abs(Bm[0]*B2[0])>=0:
  print(ansC)
#elif abs(Bp[-1]*B2[-1])-abs(Bm[0]*B2[0])>0:
#  print(ansC)
else: print(ansB)

#print(max(ansB,ansC))

Submission status: Wrong Answer

Solution code:
import numpy as np
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7
if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD

B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)
if not Bp:Bp.insert(0,B1[-1])
if not Bm:Bm.insert(0,B1[0])
  
#print(Bp,Bm,B2)

B1.remove(Bp[-1])
B1.append(min(B2[0],0))
#print(B1)
ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(max(B2[-1],0))

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD

if abs(Bp[-1]*B2[-1])-abs(Bm[0]*B2[0])>=0:
  print(ansC)
#elif abs(Bp[-1]*B2[-1])-abs(Bm[0]*B2[0])>0:
#  print(ansC)
else: print(ansB)

#print(max(ansB,ansC))

Submission status: Wrong Answer

Solution code:
import numpy as np
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7
if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD

B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)
if not Bp:Bp.insert(0,B1[-1])
if not Bm:Bm.insert(0,B1[0])
  
#print(Bp,Bm,B2)

B1.remove(Bp[-1])
B1.append(min(B2[0],0))
#print(B1)
ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(max(B2[-1],0))

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD

if abs(Bp[-1]*B2[-1])-abs(Bm[0]*B2[0])==0:
  print(ansC)
elif abs(Bp[-1]*B2[-1])-abs(Bm[0]*B2[0])>0:
  print(ansC)
else: print(ansB)

#print(max(ansB,ansC))

Submission status: Wrong Answer

Solution code:
import numpy as np
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7
if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD

B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)
if not Bp:Bp.insert(0,B1[-1])
if not Bm:Bm.insert(0,B1[0])
  
#print(Bp,Bm,B2)

B1.remove(Bp[-1])
B1.append(min(B2[0],0))
#print(B1)
ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(max(B2[-1],0))

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD

if abs(Bp[-1]*B2[-1])-abs(Bm[0]*B2[0])==0:
  print(ansC)
elif abs(Bp[-1]*B2[-1])-abs(Bm[0]*B2[0])>0:
  print(ansB)
else: print(ansB)

#print(max(ansB,ansC))

Submission status: Wrong Answer

Solution code:
import numpy as np
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7
if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD

B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)
if not Bp:Bp.insert(0,B1[-1])
if not Bm:Bm.insert(0,B1[0])
  
#print(Bp,Bm,B2)

B1.remove(Bp[-1])
B1.append(min(B2[0],0))
#print(B1)
ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(max(B2[-1],0))

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD

if abs(Bp[-1]*B2[-1])-abs(Bm[0]*B2[0])==0:
  print(ansC)
else: print(ansB)

#print(max(ansB,ansC))

Submission status: Wrong Answer

Solution code:
import numpy as np
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7
if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD

B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)
if not Bp:Bp.insert(0,B1[-1])
if not Bm:Bm.insert(0,B1[0])
  
#print(Bp,Bm,B2)

B1.remove(Bp[-1])
B1.append(min(B2[0],0))
#print(B1)
ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(max(B2[-1],0))

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD

if abs(Bp[-1]*B2[-1])-abs(Bm[0]*B2[0]):
  print(ansC)
else: print(ansB)

#print(max(ansB,ansC))

Submission status: Wrong Answer

Solution code:
import numpy as np
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7
if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD

B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)
if not Bp:Bp.insert(0,B1[-1])
if not Bm:Bm.insert(0,B1[0])
  
#print(Bp,Bm,B2)

B1.remove(Bp[-1])
B1.append(min(B2[0],0))
#print(B1)
ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(max(B2[-1],0))

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD

if abs(Bp[-1]*B2[-1])-abs(Bm[0]*B2[0])>=0:
  print(ansC)
else: print(ansB)

#print(max(ansB,ansC))

Submission status: Wrong Answer

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

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD

B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)
if not Bp:Bp.insert(0,B1[-1])
if not Bm:Bm.insert(0,B1[0])
  
#print(Bp,Bm,B2)

B1.remove(Bp[-1])
B1.append(min(B2[0],0))
#print(B1)
ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(max(B2[-1],0))

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD

if abs(Bp[-1]*B2[-1])-abs(Bm[0]*B2[0])>0:
  print(ansC)
else: print(ansB)

#print(max(ansB,ansC))

Submission status: Wrong Answer

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

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD

B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)
if not Bp:Bp.insert(0,B1[-1])
if not Bm:Bm.insert(0,B1[0])
  
#print(Bp,Bm,B2)

B1.remove(Bp[-1])
B1.append(min(B2[0],0))
#print(B1)
ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(max(B2[-1],0))

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD

if Bp[-1]*B2[-1]-Bm[0]*B2[0]<0:
  print(ansC)
else: print(ansB)

#print(max(ansB,ansC))

Submission status: Wrong Answer

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

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD

B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)
if not Bp:Bp.insert(0,B1[-1])
if not Bm:Bm.insert(0,B1[0])
  
#print(Bp,Bm,B2)

B1.remove(Bp[-1])
B1.append(min(B2[0],0))
#print(B1)
ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(max(B2[-1],0))

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD

if Bp[-1]*B2[-1]-Bm[0]*B2[0]:
  print(ansC)
else: print(ansB)

#print(max(ansB,ansC))

Submission status: Wrong Answer

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

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD

B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)
if not Bp:Bp.insert(0,B1[-1])
if not Bm:Bm.insert(0,B1[0])
  
#print(Bp,Bm,B2)

B1.remove(Bp[-1])
B1.append(min(B2[0],0))
#print(B1)
ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(max(B2[-1],0))

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD


#print(ansB,ansC)

print(max(ansB,ansC))

Submission status: Wrong Answer

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

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD

B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)
if not Bp:Bp.insert(0,B1[-1])
if not Bm:Bm.insert(0,B1[0])
  
#print(Bp,Bm,B2)

B1.remove(Bp[-1])
B1.append(min(B2[0],0))
#print(B1)
ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(max(B2[-1],0))

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD

print(max(ansB,ansC)%MOD)

Submission status: Wrong Answer

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

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD

B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)
if not Bp:Bp.insert(0,B1[-1])
if not Bm:Bm.insert(0,B1[0])
  
#print(Bp,Bm,B2)

B1.remove(Bp[-1])
B1.append(B2[0])
#print(B1)
ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(max(B2[-1],0))

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD

print(max(ansB,ansC)%MOD)

Submission status: Wrong Answer

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

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD

B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)
if not Bp:Bp.insert(0,B1[-1])
if not Bm:Bm.insert(0,B1[0])
  
#print(Bp,Bm,B2)

B1.remove(Bp[-1])
B1.append(min(B2[0],0))
#print(B1)
ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(B2[-1])

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD

print(max(ansB,ansC)%MOD)

Submission status: Wrong Answer

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

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD

B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)
if not Bp:Bp.insert(0,B1[-1])
if not Bm:Bm.insert(0,B1[0])
  
#print(Bp,Bm,B2)

B1.remove(Bp[-1])
B1.append(B2[0])
#print(B1)
ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(B2[-1])

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD

print(max(ansB,ansC)%MOD)

Submission status: Wrong Answer

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

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD

B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)
if not Bp:Bp.insert(0,B1[0])
if not Bm:Bm.insert(0,B1[-1])
  
#print(Bp,Bm,B2)

B1.remove(Bp[-1])
B1.append(B2[0])
#print(B1)
ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(B2[-1])

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD

print(max(ansB,ansC)%MOD)

Submission status: Wrong Answer

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

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()
  
ansA = 1
for a in A[0:K-1]:
  ansA *= a
  ansA %= MOD  

B=sorted(A[K-1:N])
if AA[K-1]>0:
  ansB=ansA*B[0]%MOD
else:ansB=ansA*B[-1]%MOD

print(ansB)

Submission status: Wrong Answer

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

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA>=0 or ansAA==0:
  print(ansA)
  exit()
  
ansA = 1
for a in A[0:K-1]:
  ansA *= a
  ansA %= MOD  

B=sorted(A[K-1:N])
if AA[K-1]>0:
  ansB=ansA*B[0]%MOD
else:ansB=ansA*B[-1]%MOD

print(ansB)

Submission status: Wrong Answer

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

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA>=0:
  print(ansA)
  exit()
  
ansA = 1
for a in A[0:K-1]:
  ansA *= a
  ansA %= MOD  

B=sorted(A[K-1:N])
if AA[K-1]>0:
  ansB=ansA*B[0]%MOD
else:ansB=ansA*B[-1]%MOD

print(ansB)

Submission status: Wrong Answer

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

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0:
  print(ansA)
elif ansAA==0:
  print(ansA)
  exit()
  
ansA = 1
for a in A[0:K-1]:
  ansA *= a
  ansA %= MOD  

B=sorted(A[K-1:N])
if AA[K-1]>0:
  ansB=ansA*B[0]%MOD
else:ansB=ansA*B[-1]%MOD

print(ansB)

Submission status: Wrong Answer

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

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()
  
ansA = 1
for a in A[0:K-1]:
  ansA *= a
  ansA %= MOD  

B=sorted(A[K-1:N])
if AA[K-1]>0:
  ansB=ansA*B[0]%MOD
else:ansB=ansA*B[-1]%MOD

print(ansB)

Submission status: Wrong Answer

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

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

B=sorted(A[K-1:N])

if AA[K-1]>0:
  ansB=ansAA//A[K-1]*B[0]
else:ansB=ansAA//A[K-1]*B[-1]

print(ansB)

Submission status: Wrong Answer

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

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = 1
for a in A[0:K]:
  ansAA *= a
if ansAA>=0:
  print(ansA)
  exit()

B=sorted(A[K-1:N])

if AA[K-1]>0:
  ansB=ansAA//A[K-1]*B[0]
else:ansB=ansAA//A[K-1]*B[-1]

print(ansB)

Submission status: Wrong Answer

Solution code:
import numpy as np
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7
pm=len([i for i in A if i < 0])
pp=len([i for i in A if i > 0])
p0=N-pm-pp

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = 1
for a in A[0:K]:
  ansAA *= a
if ansAA>=0:
  print(ansA)
  exit()

B=sorted(A[K-1:N])
print(B)
Bm=sorted([i for i in A[0:K-1] if i < 0])
Bp=sorted([i for i in A[0:K-1] if i > 0])

if AA[K-1]>0:
  ansB=ansAA//A[K-1]*B[0]
else:ansB=ansAA//A[K-1]*B[-1]

print(ansB)

Submission status: Wrong Answer

Solution code:
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7
A=sorted(A)
if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
  
if K%2==1 and A[-1]<0:
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD

A=sorted(A, reverse=True)
ansB = 1
for a in A[0:K]:
  ansB *= a
  ansB %= MOD

A=sorted(A, key=lambda x:abs(x), reverse=True)
ansC = 1
for a in A[0:K]:
  ansC *= a
  ansC %= MOD

ansD=1
for a in A[0:K-1]:
  ansD *= a
  ansD %= MOD
B=sorted(A[K-1:N])

if ansD>0:ansD *= B[-1]
elif ansD<0:ansD *=B[0]


if 0 in B :ansE=0
else: ansE=ansD
#print(ansA, ansB, ansC)
print(max(ansA, ansB, ansC, ansD, ansE)%MOD)

Submission status: Wrong Answer

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

if K%2==1 and A[-1]<0:
  ans0 = 1
  for a in A[N-K:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD

A=sorted(A, reverse=True)
ansB = 1
for a in A[0:K]:
  ansB *= a
  ansB %= MOD

A=sorted(A, key=lambda x:abs(x), reverse=True)
ansC = 1
for a in A[0:K]:
  ansC *= a
  ansC %= MOD

ansD=1
for a in A[0:K-1]:
  ansD *= a
  ansD %= MOD
B=sorted(A[K-1:N])

if ansD>0:ansD *= B[-1]
elif ansD<0:ansD *=B[0]

#print(ansA, ansB, ansC)
print(max(ansA, ansB, ansC, ansD)%MOD)

Submission status: Wrong Answer

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

if K%2==1 and A[-1]<0:
  ans0 = 1
  for a in A[N-K:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()

ansA = 1
for a in A[0:K]:
  ansA *= a
  if ansA>0:ansA %= MOD

A=sorted(A, reverse=True)
ansB = 1
for a in A[0:K]:
  ansB *= a
  if ansB>0:ansB %= MOD

A=sorted(A, key=lambda x:abs(x), reverse=True)
ansC = 1
for a in A[0:K]:
  ansC *= a
  if ansC>0:ansC %= MOD

ansD=1
for a in A[0:K-1]:
  ansD *= a
B=sorted(A[K-1:N])

if ansD>0:ansD *= B[-1]
elif ansD<0:ansD *=B[0]

#print(ansA, ansB, ansC)
print(max(ansA, ansB, ansC, ansD)%MOD)

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
from functools import reduce
from operator import add, mul
 
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7
A=sorted(A)
if K%2==1 and A[-1]<0:
  ans0 = 1
  for a in A[N-K:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
 
ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
 
A=sorted(A, reverse=True)
ansB = 1
for a in A[0:K]:
  ansB *= a
  ansB %= MOD
 
A=sorted(A, key=lambda x:abs(x), reverse=True)
ansC = 1
for a in A[0:K]:
  ansC *= a
  ansC %= MOD
 
#print(ansA, ansB, ansC)
print(max(ansA, ansB, ansC)%MOD)
exit()
#print(factorial(0,30))
      
#A=sorted(A)
 
#Amin=factorial(0,K)
#if Amin<0 and 0 in A[K:N]:Amin=0
 
A=sorted(A, reverse=True)
 
Amax=factorial(0,K)
 
#A=sorted(A, key=lambda x: abs(x), reverse=True)
 
#B=A[0:K-1]
#C=A[K-1:N]
 
#Bmax=factorial(0,K-1)*C[0]
#print(factorial(0,K-1),C[0])
#print(Bmax)
 
#C=sorted(C)
#print(C)
#Bmin=factorial(0,K-1)*C[0]
#print(Bmin)
#print(Amax,Amin,Bmax,Bmin)
#print(max(Amax,Amin,Bmax,Bmin)%(10**9+7))
print(Amax)
import numpy as np
import sys
from functools import reduce
from operator import add, mul
 
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7
A=sorted(A)
if K%2==1 and A[-1]<0:
  ans0 = 1
  for a in A[N-K:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()

ansA = 1
for a in A[0:K]:
  ansA *= a
  if ansA>0:ansA %= MOD

A=sorted(A, reverse=True)
ansB = 1
for a in A[0:K]:
  ansB *= a
  if ansB>0:nsB %= MOD

A=sorted(A, key=lambda x:abs(x), reverse=True)
ansC = 1
for a in A[0:K]:
  ansC *= a
  if ansC>0:ansC %= MOD

#print(ansA, ansB, ansC)
print(max(ansA, ansB, ansC)%MOD)
exit()

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
from functools import reduce
from operator import add, mul
 
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7
A=sorted(A)
if K%2==1 and A[-1]<0:
  ans0 = 1
  for a in A[N-K:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD

A=sorted(A, reverse=True)
ansB = 1
for a in A[0:K]:
  ansB *= a
  ansB %= MOD

A=sorted(A, key=lambda x:abs(x), reverse=True)
ansC = 1
for a in A[0:K]:
  ansC *= a
  ansC %= MOD

#print(ansA, ansB, ansC)
print(max(ansA, ansB, ansC)%MOD)
exit()
#print(factorial(0,30))
      
#A=sorted(A)
 
#Amin=factorial(0,K)
#if Amin<0 and 0 in A[K:N]:Amin=0

A=sorted(A, reverse=True)
 
Amax=factorial(0,K)
 
#A=sorted(A, key=lambda x: abs(x), reverse=True)
 
#B=A[0:K-1]
#C=A[K-1:N]
 
#Bmax=factorial(0,K-1)*C[0]
#print(factorial(0,K-1),C[0])
#print(Bmax)
 
#C=sorted(C)
#print(C)
#Bmin=factorial(0,K-1)*C[0]
#print(Bmin)
#print(Amax,Amin,Bmax,Bmin)
#print(max(Amax,Amin,Bmax,Bmin)%(10**9+7))
print(Amax)

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
from functools import reduce
from operator import add, mul
 
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7
A=sorted(A)
print(A)
if K%2==1 and A[-1]<0:
  ans0 = 1
  for a in A[N-K:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()

print(A)
ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD

A=sorted(A, reverse=True)
ansB = 1
for a in A[0:K]:
  ansB *= a
  ansB %= MOD

A=sorted(A, key=lambda x:abs(x), reverse=True)
ansC = 1
for a in A[0:K]:
  ansC *= a
  ansC %= MOD

#print(ansA, ansB, ansC)
print(max(ansA, ansB, ansC)%MOD)
exit()
#print(factorial(0,30))
      
#A=sorted(A)
 
#Amin=factorial(0,K)
#if Amin<0 and 0 in A[K:N]:Amin=0

A=sorted(A, reverse=True)
 
Amax=factorial(0,K)
 
#A=sorted(A, key=lambda x: abs(x), reverse=True)
 
#B=A[0:K-1]
#C=A[K-1:N]
 
#Bmax=factorial(0,K-1)*C[0]
#print(factorial(0,K-1),C[0])
#print(Bmax)
 
#C=sorted(C)
#print(C)
#Bmin=factorial(0,K-1)*C[0]
#print(Bmin)
#print(Amax,Amin,Bmax,Bmin)
#print(max(Amax,Amin,Bmax,Bmin)%(10**9+7))
print(Amax)

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
from functools import reduce
from operator import add, mul
 
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7

A=sorted(A)
#print(A)
ansA = 1
for a in A[0:K]:
  ansA *= a
  if ansA>0:ansA %= MOD
A=sorted(A, reverse=True)
ansB = 1
for a in A[0:K]:
  ansB *= a
  if ansB>0:ansB %= MOD

A=sorted(A, key=lambda x:abs(x), reverse=True)
ansC = 1
for a in A[0:K]:
  ansC *= a
  if ansC>0:ansC %= MOD

#print(ansA, ansB, ansC)
print(max(ansA, ansB, ansC)%MOD)
exit()