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 copy
N, K=map(int,input().split())
A=list(map(int,input().split()))
B=sorted([(a,abs(a)) for a in A],key=lambda x:x[1],reverse=True)
R=10**9+7
P=1
minus=0
last_neg=-1
last_pos=-1
for i in range(K):
    if B[i][0]>0:
        if last_pos != -1:
            P = (P * B[last_pos][1]) % R
        last_pos=i
    elif B[i][0]<0:
        if last_neg != -1:
            P = (P * B[last_neg][1]) % R
        minus+=1
        last_neg=i
    else:
        print(0)
        exit()
F=copy.copy(P)
if last_pos!=-1:
    F=(F*B[last_pos][1]) % R
if last_neg!=-1:
    F=(F*B[last_neg][1]) % R
if minus%2==1:
    if K == N:
        print(-F)
        exit()
    first_neg=-1
    first_pos=-1
    count=[0,0]
    for i in range(K,N):
        if count[0]==0 and B[i][0]>0:
            first_pos=i
            count[0]=1
        elif count[1]==0 and B[i][0]<0:
            first_neg=i
            count[1]=1
        elif count==[1,1]:
            break
    if first_pos==-1 and last_pos==-1:
        P = 1
        for i in range(K):
           P = (P * B[N - i - 1][0]) % R
        print(P)
        exit()
    list_product=[]
    if first_neg!=-1:
        list_product.append((P*B[last_neg][1]*B[first_neg][1]) %R)
    if first_pos!=-1 and last_pos!=-1:
        list_product.append((P*B[first_pos][1]*B[last_pos][1]) %R)
    print(max(list_product))
else:
    print(F)

Submission status: Wrong Answer

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

def main():
    n,k = map(int,input().split())
    A = map(int,input().split())

    A = sorted(A, key=lambda x:abs(x), reverse=True)

    ans = 1
    minPos, maxNeg = None, None
    for a in A[:k]:
        ans *= a
        if a >= 0 and (minPos is None or a < minPos):
            minPos = a
        if a < 0 and (maxNeg is None or a > maxNeg):
            maxNeg = a

    if ans >= 0:
        print(ans%(10**9 + 7))
    else:
        maxPos, minNeg = None, None
        for a in A[k+1:]:
            if a >= 0 and (maxPos is None or a > maxPos):
                maxPos = a
            if a < 0 and (minNeg is None or a < minNeg):
                minNeg = a

        if minPos != None and minNeg != None:
            if ans//minPos * minNeg > ans:
                ans = ans//minPos * minNeg

        if maxNeg != None and maxPos != None:
            if ans//maxNeg * maxPos > ans:
                ans = ans//maxNeg * maxPos

        print(ans % (10**9+7))
main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from itertools import permutations

def main():
    n,k=map(int,input().split())
    A = map(int,input().split())

    def prod(iterables):
        res = None
        for element in iterables:
            tmp = 1
            for e in element:
                tmp *= e
            if res is None or tmp > res:
                res = tmp
        return res
    
    pos, neg = [], []
    for a in A:
        if a >= 0:
            pos.append(a)
        else:
            neg.append(a)

    pos_n, neg_n = len(pos), len(neg)
    ans = None

    for i in range(neg_n):
        if k-i > pos_n:
            continue
        tmp = prod(permutations(neg, i)) * prod(permutations(pos, k-i)) % (10**9+7)
        if ans is None or tmp > ans:
            ans = tmp
    print(ans)
main()

Submission status: Wrong Answer

Solution code:
import sys
from itertools import permutations

def main():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))

    ans = -sys.maxsize
    for per in permutations(A, k):
        tmp = 1
        for p in per:
            tmp *= p
        if tmp > ans:
            ans = tmp
    print(ans)
main()

Submission status: Wrong Answer

Solution code:
from collections import deque

N, K = map(int, input().split())
table = list(map(int, input().split()))
jnum = K - sum(1 for i in table if i >= 0)
q_plus = deque(sorted([i for i in table if i >= 0], reverse=True))
q_mins = deque(sorted([-i for i in table if i < 0], reverse=True))
s_plus = []
s_mins = []
total = 1


if jnum < 0 or jnum % 2 == 0:
    for i in range(K):
        if not q_plus:
            s_mins.append(q_mins.popleft())
        elif not q_mins:
            s_plus.append(q_plus.popleft())
        elif q_plus[0] > q_mins[0]:
            s_plus.append(q_plus.popleft())
        else:
            s_mins.append(q_mins.popleft())
    if len(s_mins) % 2 == 1:
        if not q_plus:
            s_plus[-1] = q_mins[0]
        elif not q_mins or not s_plus:
            s_mins[-1] = q_plus[0]
        elif q_mins[0] * s_mins[-1] > q_plus[0] * s_plus[-1]:
            s_plus[-1] = q_mins[0]
        else:
            s_mins[-1] = q_plus[0]
    s_plus.extend(s_mins)
    for j in s_plus:
        total = total * j % (10 ** 9 + 7)

else:
    for i in range(K):
        if not q_plus:
            total = -total * q_mins.pop() % (10 ** 9 + 7)
        elif not q_mins:
            total = total * q_plus.pop() % (10 ** 9 + 7)
        elif q_plus[-1] > q_mins[-1]:
            total = -total * q_mins.pop() % (10 ** 9 + 7)
        else:
            total = total * q_plus.pop() % (10 ** 9 + 7)
    total = total % (10 ** 9 + 7)
print(total)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, K = map(int, input().split())
table = list(map(int, input().split()))
jnum = K - sum(1 for i in table if i >= 0)
q_plus = deque(sorted([i for i in table if i >= 0], reverse=True))
q_mins = deque(sorted([-i for i in table if i < 0], reverse=True))
s_plus = []
s_mins = []
total = 1


if jnum < 0 or jnum % 2 == 0:
    for i in range(K):
        if not q_plus:
            s_mins.append(q_mins.popleft())
        elif not q_mins:
            s_plus.append(q_plus.popleft())
        elif q_plus[0] > q_mins[0]:
            s_plus.append(q_plus.popleft())
        else:
            s_mins.append(q_mins.popleft())
    if len(s_mins) % 2 == 1:
        if not q_plus:
            s_plus[-1] = q_mins[0]
        elif not q_mins or not s_plus:
            s_mins[-1] = q_plus[0]
        elif q_mins[0] * s_mins[-1] > q_plus[0] * s_plus[-1]:
            s_plus[-1] = q_mins[0]
        else:
            s_mins[-1] = q_plus[0]
    s_plus.extend(s_mins)
    for j in s_plus:
        total = total * j % (10 ** 9 + 7)

else:
    for i in range(K):
        if not q_plus:
            total = -total * q_mins.pop()
        elif not q_mins:
            total = -total * q_plus.pop()
        elif q_plus[-1] > q_mins[-1]:
            total = -total * q_mins.pop()
        else:
            total = -total * q_plus.pop()
    total = total % (10 ** 9 + 7)
print(total)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, K = map(int, input().split())
table = list(map(int, input().split()))
jnum = K - sum(1 for i in table if i >= 0)
q_plus = deque(sorted([i for i in table if i >= 0], reverse=True))
q_mins = deque(sorted([-i for i in table if i < 0], reverse=True))
s_plus = []
s_mins = []
total = 1


if jnum < 0 or jnum % 2 == 0:
    for i in range(K):
        if not q_plus:
            s_mins.append(q_mins.popleft())
        elif not q_mins:
            s_plus.append(q_plus.popleft())
        elif q_plus[0] > q_mins[0]:
            s_plus.append(q_plus.popleft())
        else:
            s_mins.append(q_mins.popleft())
    if len(s_mins) % 2 == 1:
        if not q_plus:
            s_plus[-1] = q_mins[0]
        elif not q_mins or not s_plus:
            s_mins[-1] = q_plus[0]
        elif q_mins[0] * s_mins[-1] > q_plus[0] * s_plus[-1]:
            s_plus[-1] = q_mins[0]
        else:
            s_mins[-1] = q_plus[0]
    s_plus.extend(s_mins)
    for j in s_plus:
        total = total * j % (10 ** 9 + 7)

else:
    for i in range(K):
        if not q_plus:
            total = total * q_mins.pop() * -1 % (10 ** 9 + 7)
        elif not q_mins:
            total = total * q_plus.pop() * -1 % (10 ** 9 + 7)
        elif q_plus[-1] > q_mins[-1]:
            total = total * q_mins.pop() * -1 % (10 ** 9 + 7)
        else:
            total = total * q_plus.pop() * -1 % (10 ** 9 + 7)

print(total)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, K = map(int, input().split())
table = list(map(int, input().split()))
jnum = K - sum(1 for i in table if i >= 0)
q_plus = deque(sorted([i for i in table if i >= 0], reverse=True))
q_mins = deque(sorted([-i for i in table if i < 0], reverse=True))
s_plus = []
s_mins = []
total = 1


if jnum < 0 or jnum % 2 == 0:
    for i in range(K):
        if not q_plus:
            s_mins.append(q_mins.pop())
        elif not q_mins:
            s_plus.append(q_plus.pop())
        elif q_plus[0] > q_mins[0]:
            s_plus.append(q_plus.pop())
        else:
            s_mins.append(q_mins.pop())
    if len(s_mins) % 2 == 1:
        if not q_plus:
            s_plus[-1] = q_mins[0]
        elif not q_mins or not s_plus:
            s_mins[-1] = q_plus[0]
        elif q_mins[0] * s_mins[-1] > q_plus[0] * s_plus[-1]:
            s_plus[-1] = q_mins[0]
        else:
            s_mins[-1] = q_plus[0]
    s_plus.extend(s_mins)
    for j in s_plus:
        total = total * j % (10 ** 9 + 7)

else:
    for i in range(K):
        if not q_plus:
            total = total * q_mins.pop() * -1 % (10 ** 9 + 7)
        elif not q_mins:
            total = total * q_plus.pop() * -1 % (10 ** 9 + 7)
        elif q_plus[-1] > q_mins[-1]:
            total = total * q_mins.pop() * -1 % (10 ** 9 + 7)
        else:
            total = total * q_plus.pop() * -1 % (10 ** 9 + 7)

print(total)

Submission status: Wrong Answer

Solution code:
MOD = 10**9 + 7

def modpow(a,n):
    r=1
    while n > 0:
        if n%2==1:
            r = r*a%MOD
        a = a*a % MOD
        n = n//2
    return r

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



As.sort(key=lambda x:abs(x),reverse=True)
As_srt=sorted(As)

if N==K or (As_srt[-1]<0 and K%2==1):
    ans=1
    for i in range(K):
        ans *= As[N-i-1]
        ans %= MOD
    print(ans)
else:
    ans = 1
    sign = 1
    for i in range(K):
        ans *=As[i]
        ans %=MOD
        if As[i]<0:
            sign *= -1
    if sign < 0:
        # ansを正にする
        rm_p=None
        rm_n=None
        for i in range(K):
            if rm_p!=None and rm_n!=None:
                break
            if rm_p==None and As[K-i]>0:
                rm_p=As[K-i]
            if rm_n==None and As[K-i]<0:
                rm_n=As[K-i]
        
        apd_zp=None
        apd_n=None
        for i in range(N-K):
            if apd_zp!=None and apd_n!=None:
                break
            if apd_zp==None and As[K+i]>=0:
                apd_zp=As[K+i]
            if apd_n==None and As[K+i]<0:
                apd_n=As[K+i]
        if rm_n == None or apd_zp == None:
            rm=rm_p
            apd=apd_n
        elif rm_p == None or apd_n == None:
            rm=rm_n
            apd=apd_zp
        else:
            if abs(rm_p * apd_zp) <= abs(rm_n * apd_n):
               rm=rm_p
               apd=apd_n
            else:
                rm=rm_n
                apd=apd_zp
        ans *= apd
        ans %=MOD
        rm_inv = modpow(rm,MOD-2)
        ans *= rm_inv
        ans %= MOD

    print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
As = list(map(int,input().split()))

MOD = 10**9 + 7

As.sort(key=lambda x:abs(x),reverse=True)
As_srt=sorted(As)

if N==K or (As_srt[-1]<0 and K%2==1):
    ans=1
    for i in range(K):
        ans *= As[N-i-1]
        ans %= MOD
    print(ans)
else:
    ans = 1
    sign = 1
    for i in range(K):
        ans *=As[i]
        ans %=MOD
        if As[i]<0:
            sign *= -1
    if sign < 0:
        # ansを正にする
        pass
    print(ans)

Submission status: Wrong Answer

Solution code:

import itertools

n, k = map(int, input().split())
num = input().split()
num_list = list(itertools.combinations(num, k))

seki = []
for xxx in num_list:
    counter = 1
    for x in xxx:
        counter = counter * int(x)
    seki.append(counter)


print(num_list)
print(int(max(seki)) % (10 ** 9 + 7))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
from functools import reduce


def solve(*args: str) -> str:
    n, k = map(int, args[0].split())
    A = sorted(map(int, args[1].split()))
    mod = 10**9+7

    if n == k:
        return str(reduce(lambda a, b: (a*b) % mod, A, 1))

    if k % 2 and A[-1] < 0:
        return str(reduce(lambda a, b: (a*b) % mod, A[n-k:], 1))

    l, r = 0, n-1
    count = 0
    ret = 1
    while count < k-1:
        pos = max(0, A[r]*A[r-1])
        neg = max(0, A[l]*A[l+1])
        if pos <= neg:
            ret *= neg
            l += 2
        else:
            ret *= pos
            r -= 2
        ret %= mod
        count += 2

    if count < k:
        ret *= A[r]
        ret %= mod

    return str(ret)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Submission status: Wrong Answer

Solution code:
import sys
 
n, k = map(int, input().split())
a = [int(x) for x in input().split()]
mod = pow(10, 9)+7
zero = 0
plus = []
minus = []
for i in range(n):
  if a[i] == 0:
    zero += 1
  elif a[i] > 0:
    plus.append(a[i])
  else:
    minus.append(a[i])
 
P, M = len(plus), len(minus)
plus.sort(reverse=True)
minus.sort()
ans = 1
if n == k:
  for i in range(n):
    ans *= a[i]
    ans %= mod
elif P+M < k:
  ans = 0
elif P == 0 and k%2 ==1 :
  if zero >= 1:
    ans = 0
  else:
    #minus.reverse()
    for i in range(k):
      ans *= minus[i]
      ans %= mod
else:
  q = 0
  if k%2:
    ans *= plus[0]
    ans %= mod
    q = 1
  judge = []
  for i in range((len(plus)-q)//2):
    judge.append(plus[2*i+q]*plus[2*i+q+1])
  for i in range(len(minus)//2):
    judge.append(minus[2*i]*minus[2*i+1])
  judge.sort(reverse=True)
  for i in range(k//2):
    ans *= (judge[i]%mod)
    ans %= mod
 
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
 
n, k = map(int, input().split())
a = [int(x) for x in input().split()]
mod = pow(10, 9)+7
zero = 0
plus = []
minus = []
for i in range(n):
  if a[i] == 0:
    zero += 1
  elif a[i] > 0:
    plus.append(a[i])
  else:
    minus.append(a[i])
 
P, M = len(plus), len(minus)
plus.sort(reverse=True)
minus.sort()
ans = 1
if n == k:
  for i in range(n):
    ans *= a[i]
    ans %= mod
elif P+M < k:
  ans = 0
elif P == 0:
  if zero >= 1 and k%2 == 1:
    ans = 0
  else:
    if k%2 == 0:
      minus.reverse()
    for i in range(k):
      ans *= minus[i]
      ans %= mod
else:
  q = 0
  if k%2:
    ans *= plus[0]
    ans %= mod
    q = 1
  judge = []
  for i in range((len(plus)-q)//2):
    judge.append(plus[2*i+q]*plus[2*i+q+1])
  for i in range(len(minus)//2):
    judge.append(minus[2*i]*minus[2*i+1])
  judge.sort(reverse=True)
  for i in range(k//2):
    ans *= (judge[i]%mod)
    ans %= mod
 
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
 
n, k = map(int, input().split())
a = [int(x) for x in input().split()]
mod = pow(10, 9)+7
zero = 0
plus = []
minus = []
for i in range(n):
  if a[i] == 0:
    zero += 1
  elif a[i] > 0:
    plus.append(a[i])
  else:
    minus.append(a[i])
 
P, M = len(plus), len(minus)
plus.sort(reverse=True)
minus.sort()
ans = 1
if n == k:
  for i in range(n):
    ans *= a[i]
    ans %= mod
elif P+M < k:
  ans = 0
elif P == 0:
  if zero and k%2 == 0:
    ans = 0
  else:
    if k%2:
      minus.reverse()
    for i in range(k):
      ans *= minus[i]
      ans %= mod
else:
  q = 0
  if k%2:
    ans *= plus[0]
    ans %= mod
    q = 1
  judge = []
  for i in range((len(plus)-q)//2):
    judge.append(plus[2*i+q]*plus[2*i+q+1])
  for i in range(len(minus)//2):
    judge.append(minus[2*i]*minus[2*i+1])
  judge.sort(reverse=True)
  for i in range(k//2):
    ans *= (judge[i]%mod)
    ans %= mod

print(ans)

Submission status: Wrong Answer

Solution code:
def main(n, k, lst):
    pos, neg = 0, 0
    for i in lst:
        if i >= 0: pos += 1
    neg = n - pos

    lst = sorted(lst)
    count = 0
    product = 1
    if pos < k:
        for i in lst[-k:]:
            product *= i
        return product % (10 ** 9 + 7)
    while count < k:
        if k - count != 1:
            if lst[-1] * lst[-2] >= lst[0] * lst[1]:
                product *= lst[-1] * lst[-2]
                lst = lst[:-2]
            else:
                product *= lst[0] * lst[1]
                lst = lst[2:]
            count += 2
        else:
            product *= lst[-1]
            lst = lst[:-1]

            count += 1
    #print(product)
    return product % (10 ** 9 + 7)
        
n, k = [int(x) for x in input().split()]
lst = list(map(int, input().split()))
print(main(n, k, lst))

Submission status: Wrong Answer

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

MOD = 10 ** 9 + 7

A = [(abs(a), a) for a in A]
A.sort(reverse = True)
#print(A)

answer = 1
ans_neg = []
sign = 1
for (absa, a) in A[:K]:
    if a < 0:
        sign *= -1
        ans_neg.append(a)
    if a == 0:
        print(0)
        exit()
    answer = (answer * a) % MOD

pos, neg = [], []
for (absa, a) in A[K:]:
    if a > 0:
        pos.append(a)

if sign == 1:
    print(answer)
    exit()
else:
    if pos:
        answer = (answer * pos[0] * pow(ans_neg[-1], MOD - 2, MOD)) % MOD
    else:
        A = A[::-1]
        answer = 1
        for (absa, a) in A[:K]:
            answer = (answer * a) % MOD
    print(answer)

Submission status: Wrong Answer

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

MOD = 10 ** 9 + 7

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

A = [(abs(a), a) for a in A]
A.sort(reverse = True)
#print(A)

answer = 1
ans_neg = []
sign = 1
for (absa, a) in A[:K]:
    if a < 0:
        sign *= -1
        ans_neg.append(neg.pop())
    elif a > 0:
        pos.pop()
    else:
        print(0)
        exit()
    answer = (answer * a) % MOD
    
if sign == 1:
    print(answer)
    exit()
else:
    if pos:
        answer = (answer * pos.pop() * pow(ans_neg.pop(), MOD - 2)) % MOD
    else:
        A = A[::-1]
        answer = 1
        for (absa, a) in A[:K]:
            answer = (answer * a) % MOD
    print(answer)

Submission status: Wrong Answer

Solution code:
n,k=[int(i) for i in raw_input().split()]
a=[int(i) for i in raw_input().split()]
a.sort()
b=[]
c=[]
for i in range(n):
    if a[i]<0:
        b.append(a[i])
    else:
        c.append(a[i])
b.sort()
c.sort()
o=[]
i=0;n=len(c);m=len(b);l=0
j=n-1
while l<k:
    if(j>0 and i<m-1 and l<k-1):
        if(c[j]*c[j-1])>(b[i])*(b[i+1]):
           o+=[c[j],c[j-1]]
           j-=2
        else:
           o+=[b[i],b[i+1]]
           i+=2
        l+=2
    elif(i<m-1 and l<k-1):
        o+=[b[i],b[i+1]]
        l+=2
        i+=2
    elif(j>-1):
        o+=[c[j]]
        l+=1
        j-=1
    elif(i<m):
        o+=[b[i]]
        l+=1
        i+=1
ct=0
for i in o:
    if i<0:
        ct+=1
if ct%2:
    o=[]
    l=0;j=0;i=m-1
    while l<k:
        if(i>0 and j<n-1 and l<k-1):
            if(c[j]*c[j+1])<(b[i])*(b[i-1]):
               o+=[c[j],c[j+1]]
               j+=2
            else:
               o+=[b[i],b[i-1]]
               i-=2
            l+=2
    
        elif(j<n):
            o+=[c[j]]
            l+=1
            j+=1
        elif(i>-1):
            o+=[b[i]]
            l+=1
            i-=1 
s=1
for i in o:
    s*=i
    s%=(10**9+7)
print s

Submission status: Wrong Answer

Solution code:
n,k=[int(i) for i in raw_input().split()]
a=[int(i) for i in raw_input().split()]
a.sort()
b=[]
c=[]
for i in range(n):
    if a[i]<0:
        b.append(a[i])
    else:
        c.append(a[i])
b.sort()
c.sort()
o=[]
i=0;n=len(c);m=len(b);l=0
j=n-1
while l<k:
    if(j>0 and i<m-1 and l<k-1):
        if(c[j]*c[j-1])>(b[i])*(b[i+1]):
           o+=[c[j],c[j-1]]
           j-=2
        else:
           o+=[b[i],b[i+1]]
           i+=2
        l+=2
    
    
    elif(i<m):
        o+=[b[i]]
        l+=1
        i+=1
    elif(j>-1):
        o+=[c[j]]
        l+=1
        j-=1
ct=0
for i in o:
    if i<0:
        ct+=1
if ct%2:
    o=[]
    l=0;j=0;i=m-1
    while l<k:
        if(i>0 and j<n-1 and l<k-1):
            if(c[j]*c[j+1])<(b[i])*(b[i-1]):
               o+=[c[j],c[j+1]]
               j+=2
            else:
               o+=[b[i],b[i-1]]
               i-=2
            l+=2
        elif(i>-1):
            o+=[b[i]]
            l+=1
            i-=1 
        elif(j<n):
            o+=[c[j]]
            l+=1
            j+=1
        
s=1
for i in o:
    s*=i
    s%=(10**9+7)
print s

Submission status: Wrong Answer

Solution code:
n,k=[int(i) for i in raw_input().split()]
a=[int(i) for i in raw_input().split()]
a.sort()
b=[]
c=[]
for i in range(n):
    if a[i]<0:
        b.append(a[i])
    else:
        c.append(a[i])
b.sort()
c.sort()
o=[]
i=0;n=len(c);m=len(b);l=0
j=n-1
while l<k:
    if(j>0 and i<m-1 and l<k-1):
        if(c[j]*c[j-1])>(b[i])*(b[i+1]):
           o+=[c[j],c[j-1]]
           j-=2
        else:
           o+=[b[i],b[i+1]]
           i+=2
        l+=2
    
    elif(j>-1):
        o+=[c[j]]
        l+=1
        j-=1
    elif(i<m):
        o+=[b[i]]
        l+=1
        i+=1
ct=0
for i in o:
    if i<0:
        ct+=1
if ct%2:
    o=[]
    l=0;j=0;i=m-1
    while l<k:
        if(i>0 and j<n-1 and l<k-1):
            if(c[j]*c[j+1])<(b[i])*(b[i-1]):
               o+=[c[j],c[j+1]]
               j+=2
            else:
               o+=[b[i],b[i-1]]
               i-=2
            l+=2
    
        elif(j<n):
            o+=[c[j]]
            l+=1
            j+=1
        elif(i>-1):
            o+=[b[i]]
            l+=1
            i-=1 
s=1
for i in o:
    s*=i
    s%=(10**9+7)
print s

Submission status: Wrong Answer

Solution code:
n,k=[int(i) for i in raw_input().split()]
a=[int(i) for i in raw_input().split()]
a.sort()
b=[]
c=[]
for i in range(n):
    if a[i]<0:
        b.append(a[i])
    else:
        c.append(a[i])
b.sort()
c.sort()
o=[]
i=0;n=len(c);m=len(b);l=0
j=n-1
while l<k:
    if(j>0 and i<m-1 and l<k-1):
        if(c[j]*c[j-1])>(b[i])*(b[i+1]):
           o+=[c[j],c[j-1]]
           j-=2
        else:
           o+=[b[i],b[i+1]]
           i+=2
        l+=2
    
    elif(j>-1):
        o+=[c[j]]
        l+=1
        j-=1
    elif(i<m):
        o+=[b[i]]
        l+=1
        i+=1
if n==0:
    o=b[m-k:]
s=1
for i in o:
    s*=i
    s%=(10**9+7)
print s

Submission status: Wrong Answer

Solution code:
n,k=[int(i) for i in raw_input().split()]
a=[int(i) for i in raw_input().split()]
a.sort()
b=[]
c=[]
for i in range(n):
    if a[i]<0:
        b.append(a[i])
    else:
        c.append(a[i])
b.sort()
c.sort()
o=[]
i=0;n=len(c);m=len(b);l=0
j=n-1
while l<k:
    if(j>0 and i<m-1 and l<k-1):
        if(c[j]*c[j-1])>=(b[i])*(b[i+1]):
           o+=[c[j],c[j-1]]
           j-=2
        else:
           o+=[b[i],b[i+1]]
           i+=2
        l+=2
    
    elif(j>-1):
        o+=[c[j]]
        l+=1
        j-=1
    elif(i<m):
        o+=[b[i]]
        l+=1
        i+=1
if n==0:
    o=b[m-k:]
s=1
for i in o:
    s*=i
    s%=(10**9+7)
print s

Submission status: Wrong Answer

Solution code:
n,k=[int(i) for i in raw_input().split()]
a=[int(i) for i in raw_input().split()]
a.sort()
b=[]
c=[]
for i in range(n):
    if a[i]<0:
        b.append(a[i])
    else:
        c.append(a[i])
b.sort()
c.sort()
o=[]
i=0;n=len(c);m=len(b);l=0
j=n-1
while l<k:
    if(j>0 and i<m-1):
        if(c[j]*c[j-1])>=(b[i])*(b[i+1]):
           o+=[c[j],c[j-1]]
           j-=2
        else:
           o+=[b[i],b[i+1]]
           i+=2
        l+=2
    
    elif(j>-1):
        o+=[c[j]]
        l+=1
        j-=1
    elif(i<m):
        o+=[b[i]]
        l+=1
        i+=1
if n==0:
    o=b[m-k:]
s=1
for i in o:
    s*=i
    s%=(10**9+7)
print s

Submission status: Wrong Answer

Solution code:
import numpy as np

def getPrime(n):
    N=np.arange(start=2,stop=n+1)
    index=0
    currentP=2
    while(currentP<n):
        currentP=N[index]
        preN=N[:index+1]
        postN=N[index+1:]
        N=np.concatenate([preN,postN[postN%currentP!=0]])
        index+=1
        if index>=len(N):
            break
    return N

def factorize(n,ps):
    cnts=np.zeros_like(ps)
    for i,p in enumerate(ps):
        while(n%p==0):
            n=n//p
            cnts[i]+=1
        if n==1:
            break
    return cnts


def getMaximam(n):
    ans=2
    if n==1:
        return 2
    while(True):
        if n%ans!=0:
            ans+=1
            continue
        else:
            n=n//ans
            if n==1:
                break
    return ans

def factorizeWithOneArgument(n):
    p=2
    l=[]
    cnt=[]
    if n==1:
        return np.array([1]),np.array([1])
    isDiv=False
    while(True):
        if n%p!=0:
            isDiv=False
            p+=1
            continue
        else:
            if isDiv==False:
                l.append(p)
                cnt.append(0)
                isDiv=True
            cnt[-1]+=1
            n=n//p
            if n==1:
                break
    return np.array(l),np.array(cnt)

def main():
    NK=input().split(" ")
    N,K=int(NK[0]),int(NK[1])
    A=input().split(" ")
    A=np.array([int(i) for i in A],dtype=np.int64)
    absA=np.abs(A)
    argAbsA=np.argsort(absA)
    posA=A[A>=0]
    factors=A[argAbsA][-K:]
    negativeNum=(factors<0).astype(np.uint8).sum()
    isReplace=True
    if negativeNum%2==1:
        temp=A[argAbsA][K-1]
        isReplace=False
        if temp>=0:
            for i in range(K):
                if factors[i]<0:
                    factors[i]=temp
                    isReplace=True
                    break
        else:
            for i in range(K):
                if factors[i]>=0:
                    factors[i]=temp
                    isReplace=True
                    break
        if not isReplace:
            factors=A[argAbsA][:K]
    maxP=2
    factors=np.abs(factors)
    for f in factors:
        buf=getMaximam(f)
        if maxP<buf:
            maxP=buf
    ps=getPrime(maxP).astype(np.uint64)
    cnts=np.zeros_like(ps,np.uint64)
    for f in factors:
        primeFact=factorize(f,ps)
        cnts=cnts+primeFact
        if (cnts>1000000007).astype(np.uint8).sum()>0:
            cnt[cnts>1000000007]-=1000000006
    ps=ps[cnts>0]
    cnts=cnts[cnts>0]
    val1=1
    for i in range(len(ps)):
        val1*=ps[i]**cnts[i]

    if not isReplace:
        val1*=-1

    while(val1>1000000007):
        val1-=1000000007
    while(val1<0):
        val1+=1000000007
    print(int(val1))

def main2():
    NK=input().split(" ")
    N,K=int(NK[0]),int(NK[1])
    A=input().split(" ")
    A=np.array([int(i) for i in A],dtype=np.int64)
    absA=np.abs(A)
    argAbsA=np.argsort(absA)
    posA=A[A>=0]
    factors=A[argAbsA][-K:]
    negativeNum=(factors<0).astype(np.uint8).sum()
    isReplace=True
    if negativeNum%2==1:
        temp=A[argAbsA][K-1]
        isReplace=False
        if temp>=0:
            for i in range(K):
                if factors[i]<0:
                    factors[i]=temp
                    isReplace=True
                    break
        else:
            for i in range(K):
                if factors[i]>=0:
                    factors[i]=temp
                    isReplace=True
                    break
        if not isReplace:
            factors=A[argAbsA][:K]

    factors=np.abs(factors)
    ps=np.array([],dtype=np.uint64).reshape(0,2)
    for f in factors:
        p,c=factorizeWithOneArgument(f)
        for i in range(len(p)):
            if (ps[:,0]==p[i]).sum()==0:
                b=np.array([p[i],c[i]]).reshape(1,2)
                ps=np.concatenate([ps,b],axis=0).astype(np.uint64)
            else:
                ps[ps[:,0]==p[i],1]+=c[i]
        if (ps[:,1]>1000000007).sum()>0:
            ps[ps[:,1]>1000000007,1]-=1000000006

    val1=1
    # print(ps)
    for i in range(len(ps)):
        val1*=ps[i,0]**ps[i,1]

    if not isReplace:
        val1*=-1
    if val1>1000000007:
        val1=val1%1000000007
    while(val1<0):
        val1+=1000000007
    print(int(val1))

if __name__ == "__main__":
    main2()

Submission status: Wrong Answer

Solution code:
import numpy as np
if __name__ == "__main__":
    NK=input().split(" ")
    N,K=int(NK[0]),int(NK[1])
    A=input().split(" ")
    A=np.array([int(i) for i in A],dtype=np.int64)
    absA=np.abs(A)
    argAbsA=np.argsort(absA)
    posA=A[A>=0]
    factors=A[argAbsA][-K:]
    negativeNum=(factors<0).astype(np.uint8).sum()
    if negativeNum%2==1:
        temp=A[argAbsA][K-1]
        isReplace=False
        if temp>=0:
            for i in range(K):
                if factors[i]<0:
                    factors[i]=temp
                    isReplace=True
                    break
        else:
            for i in range(K):
                if factors[i]>=0:
                    factors[i]=temp
                    isReplace=True
                    break
        if not isReplace:
            factors=A[argAbsA][:K]
    val1=1
    for i in factors:
        val1*=i
    while(val1>1000000007):
        val1-=1000000007
    while(val1<0):
        val1+=1000000007
    print(val1)