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 datetime
import string
import re
import math

def isort(list, _beg, _end):
    if _beg < _end:
        beg = _beg
        end = _end
        pivot = abs(list[int((beg + end)/2)])
        while(True):
            while abs(list[beg]) < pivot: beg += 1
            while abs(list[end]) > pivot: end -= 1
            if beg <= end:
                tmp = list[beg]
                list[beg] = list[end]
                list[end] = tmp
                beg += 1
                end -= 1
            else:
                break
        isort(list, _beg, end)
        isort(list, beg, _end)

def searchByBool(list):
    for i in range(len(list)):
        if check(list[i]): return i
    return -1

def check(num):
    if num >= 0: return True
    else: return False

sp = list(map(int, input().split()))
N = sp[0]
K = sp[1]
alist = list(map(int, input().split()))
klist = []
isK = True

isort(alist, 0, len(alist) - 1)
alist.reverse()

numMinus = 0
for i in range(K):
    if check(alist[i]) == False: numMinus += 1

# select biggest K numbers in terms of absolute value

# for all chosen numbers
# if the number of negative number is even
if numMinus % 2 == 0:
    klist = alist[0:K]
    isK = False

# if odd
# -> choose the smallest number, and search opposite number
if isK:
    for i in range(K,N):
        if check(alist[K-1]) != check(alist[i]):
            alist[K-1]=alist[i]
            klist = alist[0:K]
            isK = False
            break

# if isK:
#     for i in range(K):
#         if check(alist[K-1-i]) != check(alist[K-1]):
#             alist[K-1-i] = alist[K]
#             klist = alist[0:K]
#             isK = False
#             break

if isK:
    alist.reverse()
    klist = alist[0:K]

multi = klist[0]
for i in range(1, K):
    multi *= klist[i]
    multi = multi % 1000000007

print(multi)

Submission status: Wrong Answer

Solution code:
import sys
N, K = list(map(int, input().split()))
arr = list(map(int, input().split()))
arr.sort(reverse = True)
maxprod = 1
for i in range(K):
    maxprod *= arr[i]
negProd = -(sys.maxsize)
posProd = -(sys.maxsize)
count = 0
if arr[-K] < 0:
    negProd = 1
    for i in range(N):
        if arr[i] < 0:
            negProd *= arr[i]
            count += 1
        if count == K:
            break
count = 0
if arr[0] > 0:
    posProd = 1
    for i in range(N):
        if arr[i] > 0:
            posProd *= arr[i]
            count += 1
        if count == K:
            break
ans = (1000000007 + max(maxprod, posProd, negProd)) % 1000000007
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = list(map(int,input().split())); arr = list(map(int,input().split())); arr.sort(reverse=True)
absarr = [[abs(i),1 if i>0 else -1 if i<0 else 0] for i in arr]; absarr.sort(reverse=True, key=lambda x: x[0])
mod = 10**9+7

zeroflag = True if absarr[-1][0] == 0 else False
from collections import Counter
posnegarr = Counter([i[1] for i in absarr[:k]])
pos = posnegarr[1]; neg = posnegarr[-1]

if absarr[k-1] == 0:
    ans = 0
elif neg % 2 == 0 or n == k:
    ans = 1
    for i in absarr[:k]:
        ans *= i[0]; ans %= mod
    if n == k and neg % 2 == 1:
        ans = -ans % mod
else:
    ans = 1
    for i in absarr[:k]:
        ans *= i[0]; ans %= mod
    if pos > 0:
        if absarr[k][0] == 0:
            ans = 0
        nextpos = nextneg = 0
        tmp = k
        while tmp < n and (nextpos == 0 or nextneg == 0):
            num = absarr[tmp]
            if num[0] == 0:
                break
            elif num[1] > 0 and nextpos == 0:
                nextpos = num[0]
            elif num[1] < 0 and nextneg == 0:
                nextneg = num[0]
            tmp += 1
        backpos = backneg = 0
        tmp = k-1
        while backpos == 0 or backneg == 0:
            num = absarr[tmp]
            if num[1] > 0 and backpos == 0:
                backpos = num[0]
            elif num[1] < 0 and backneg == 0:
                backneg = num[0]
            tmp -= 1
        if ans == 0:
            pass
        elif nextpos*backpos > nextneg*backneg:
            ans *= nextpos*pow(backneg,-1,mod); ans %= mod
        else:
            ans *= nextneg*pow(backpos,-1,mod); ans %= mod
    else:
        nextpos = 0
        tmp = k
        while tmp < n and nextpos == 0:
            num = absarr[tmp]
            if num[1] > 0:
                nextpos = num[0]
            tmp += 1
        backneg = absarr[k-1][0]
        if nextpos != 0:
            ans *= nextpos * pow(backneg,-1,mod)
        elif zeroflag:
            ans = 0
        else:
            ans = 1
            for i in arr[:k]:
                ans *= i; ans %= mod
        
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
N,K=map(int,input().split())
A=list(map(int,input().split()))
MOD=10**9+7

ans=1

if K==N:
    for i in range(N):
        ans*=A[i]
        if ans>0:
            ans=ans%MOD
elif max(A)<0 and K%2==1:
    A.sort(reverse=True)
    for i in range(K):
        ans*=A[i]
        if ans>0:
            ans=ans%MOD
else:
    Ap=[]
    Am=[]
    
    for i in range(N):
        if A[i]>0:
            Ap.append(A[i])
        else:
            Am.append(A[i])
        

    Ap.sort(reverse=True)
    Am.sort()
    
    if K%2==1 and Ap:
        ans*=Ap.pop(0)
    
    
    P=[]
    
    for i in range(len(Ap)//2):
        P.append(Ap[i*2]*Ap[i*2+1])
    
    for i in range(len(Am)//2):
        P.append(Am[i*2]*Am[i*2+1])
    
    P.sort(reverse=True)
    
    for i in range(K//2):
        ans*=P[i]
        ans=ans%MOD

print(ans%MOD)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
N,K=map(int,input().split())
A=list(map(int,input().split()))
MOD=10**9+7

ans=1

if K==N:
    for i in range(N):
        ans*=A[i]
        if ans>0:
            ans=ans%MOD
elif max(A)<0 and K%2==1:
    A.sort(reverse=True)
    for i in range(K):
        ans*=A[i]
        if ans>0:
            ans=ans%MOD
else:
    Ap=[]
    Am=[]
    
    for i in range(N):
        if A[i]>0:
            Ap.append(A[i])
        else:
            Am.append(A[i])
        

    Ap.sort(reverse=True)
    Am.sort()
    
    if K%2==1 and Ap:
        ans*=Ap.pop(0)
    
    if len(Ap)%2==1:
        Ap.append(1)
    
    P=[]
    
    for i in range(len(Ap)//2):
        P.append(Ap[i*2]*Ap[i*2+1])
    
    for i in range(len(Am)//2):
        P.append(Am[i*2]*Am[i*2+1])
    
    P.sort(reverse=True)
    
    for i in range(K//2):
        ans*=P[i]
        ans=ans%MOD

print(ans%MOD)

Submission status: Wrong Answer

Solution code:
import sys
s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2sss = lambda n: [list(sys.stdin.readline().rstrip()) for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))
from collections import deque  # 双方向キュー
from collections import defaultdict  # 初期化済み辞書
from heapq import heapify, heappush, heappop, heappushpop  # プライオリティキュー
from bisect import bisect_left, bisect_right  # 二分探索
sys.setrecursionlimit(int(1e+6))
MOD = int(1e+9) + 7
#import numpy as np  # 1.8.2
#import scipy  # 0.13.3

def main():
    N, K = i2nn()
    A = i2nn()
    countMinus = 0
    for a in A:
        if a < 0:
            countMinus += 1
    # 結果がマイナスになる場合
    b1 = N == countMinus and K % 2 == 1
    b2 = N == K and countMinus % 2 == 1
    if b1 == True or b2 == True:
        n = 1
        A.sort()
        A.reverse()
        for i in range(K):
            n = (n * A[i]) % MOD
        print(n)
        return
    # 結果がマイナスにならない場合
    AA = [(n, n if n >= 0 else -n) for n in A]
    AA.sort(key=lambda v: v[1])
    AA.reverse()
    skip = None
    n = 1
    for i in range(K):
        a, aa = AA[i]
        if a < 0:
            if skip is not None:
                n = (n * skip) % MOD
                n = (n * aa) % MOD
                skip = None
            else:
                skip = aa
        else:
            n = (n * aa) % MOD
    if skip is not None:
        for i in range(K, N):
            a, aa = AA[i]
            if a < 0:
                n = (n * skip) % MOD
                n = (n * aa) % MOD
                skip = None
                break            
    print(n)

main()

Submission status: Wrong Answer

Solution code:
MOD=10**9+7

def moddiv(a,m):
    return pow(a,m-2,m)

def solve():
    res=-10**30
    minus,plus,zero=[],[],[]
    for i in range(N):
        if A[i]<0:
            minus.append(A[i])
        elif A[i]>0:
            plus.append(A[i])
        else:
            zero.append(A[i])
    cump=1
    plus.sort()
    minus.sort()
    for i in range(len(plus)):
        cump=cump*plus[i]%MOD
    j=0
    i=0
    cumm=1
    while(j<=min(len(minus),K)):
        if j>0:
            cumm=cumm*minus[j-1]*minus[j-2]%MOD
        if K-j>len(plus):
            j+=2
            continue
        while(K-j<len(plus)-i):
            cump=cump*moddiv(plus[i],MOD)%MOD
            i+=1
        res=max(res,cumm*cump%MOD)
        j+=2
    if res!=-10**30:
        return res
    if len(zero)>0:
        return 0
    li=minus+plus
    li.sort(key=lambda x:abs(x))
    res=1
    for i in range(K):
        res=res*li[i]%MOD
    return res
N,K=map(int,input().split())
A=list(map(int,input().split()))

if min(A)>=0:
    res=1
    A.sort(reverse=True)
    for i in range(K):
        res=res*A[i]%MOD
elif max(A)<=0:
    res=1
    if not K%2:
        A.sort()
        for i in range(K):
            res=res*A[i]%MOD
    else:
        A.sort(reverse=True)
        for i in range(K):
            res=res*A[i]%MOD
else:
    res=solve()
print(res)

Submission status: Wrong Answer

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

AListAbs = sorted(AList, reverse=True,key=abs)

ans = 1
for i in range(K):
  ans *= AListAbs[i]

if ans > 0:
  ans %= (10**9 + 7)
  print(ans)
else:
  changeNum = -1
  removeNum = 1
  for j in range(K,N,1):
    if AListAbs[j] > 0:
      changeNum = AListAbs[j]
      break
  if changeNum < 0:
    ans = 1
    for i in range(N-1,N-K-1,-1):
      ans *= AListAbs[i]
    print(ans % (10**9 + 7))
    exit()
  for m in range(K-1,0,-1):
    if AListAbs[m] < 0:
      removeNum = AListAbs[m]
      break
  ans = (ans * changeNum // removeNum) % (10**9 + 7)
  print(ans)

Submission status: Wrong Answer

Solution code:
def solve(ls, k, debug=0):
    modulo = 10 ** 9 + 7
    n = len(ls)
    n_neg = 0
    for i in range(n):
        n_neg += ls[i] < 0
    n_pos = n - n_neg

    # Sort by abs
    ls_abs = [abs(x) for x in ls]
    ls_abs_argsort = sorted(list(range(n)), key=lambda i: -ls_abs[i])

    # Check if positive result is impossible
    l = min(k, n_neg)  # consider if we pick as many negatives as possible
    positive = (l % 2 == 0) or ((k <= n_neg) and n_pos > 0) or ((k > n_neg) and n > k)

    # If positive is impossible, pick "abs" small ones
    if not positive:
        p = 1
        for i in range(k):
            x = ls[ls_abs_argsort[-1 - i]]
            p *= x
            p %= modulo
        return p

    #
    # If positive is possible, ...
    #
    # PROP.
    #   At most one swap is needed for making product non-negative.
    #   PROOF. TODO
    #

    # Check sign when we pick "abs" large ones
    s = 1
    for i in range(k):
        x = ls[ls_abs_argsort[i]]
        s *= -1 if x < 0 else 1

    # If non-negative, we take as it is
    if s > 0:
        p = 1
        for i in range(k):
            x = ls[ls_abs_argsort[i]]
            p *= x
            p %= modulo
        return p

    # If negative, ...
    #   1. replace small negative with large positive, or
    #   2. replace small positive with large negative

    # Check if "1" is possible
    swap_pos = None
    for i in range(k, n):
        if ls[ls_abs_argsort[i]] >= 0:
            swap_pos = i
            break

    swap_neg = None
    for i in range(k)[::-1]:
        if ls[ls_abs_argsort[i]] < 0:
            swap_neg = i
            break

    # Go with "1"
    if swap_pos is not None and swap_neg is not None:
        p = 1
        p *= ls[ls_abs_argsort[swap_pos]]
        p %= modulo
        for i in range(k):
            if i == swap_neg:
                continue
            x = ls[ls_abs_argsort[i]]
            p *= x
            p %= modulo
        return p

    # Go with "2"
    swap_pos = None
    for i in range(k)[::-1]:
        if ls[ls_abs_argsort[i]] >= 0:
            swap_pos = i
            break

    swap_neg = None
    for i in range(k, n):
        if ls[ls_abs_argsort[i]] < 0:
            swap_neg = i
            break

    p = 1
    p *= ls[ls_abs_argsort[swap_neg]]
    p %= modulo
    for i in range(k):
        if i == swap_pos:
            continue
        x = ls[ls_abs_argsort[i]]
        p *= x
        p %= modulo
    return p


def main(istr, ostr):
    n, k = list(map(int, istr.readline().strip().split()))
    ls = list(map(int, istr.readline().strip().split()))
    result = solve(ls, k)
    print(result, file=ostr)


if __name__ == "__main__":
    import sys

    main(sys.stdin, sys.stdout)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from functools import reduce

mod = 10**9 + 7
n, k, *a = map(int, open(0).read().split())
a.sort(key=lambda x: abs(x))
ans = reduce(lambda a, b: (a * b) % mod, a[-k:])

c = a[-k:]
j = sum(i < 0 for i in c) % 2
if j:
    c = a[-k:]
    neg = [i for i in c if i < 0]
    pos = [i for i in c if i > 0]
    b = sorted(a[:n - k])
    if b == []:
        print(ans)
        exit()
    if neg == []:
        if pos[0] * b[0] < 0:
            ans = ans * pow(pos[0], mod - 2, mod) * b[0] % mod
        else:
            ans = reduce(lambda a, b: (a * b) % mod, a[:k])
    elif pos == []:
        if neg[0] * b[-1] < 0:
            ans = ans * pow(neg[0], mod - 2, mod) * b[-1] % mod
        else:
            ans = reduce(lambda a, b: (a * b) % mod, a[:k])
    elif pos[0] * b[-1] > neg[0] * b[0] and pos[0] * b[0] < 0:
        ans = ans * pow(pos[0], mod - 2, mod) * b[0] % mod
    elif 0 > b[-1] * neg[0]:
        ans = ans * pow(neg[0], mod - 2, mod) * b[-1] % mod
    else:
        ans = reduce(lambda a, b: (a * b) % mod, a[:k])
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from functools import reduce

mod = 10**9 + 7
n, k, *a = map(int, open(0).read().split())
a.sort(key=lambda x: abs(x))
ans = reduce(lambda a, b: (a * b) % mod, a[-k:])

c = a[-k:]
j = sum(i < 0 for i in c) % 2
if j:
    c = a[-k:]
    neg = [i for i in c if i < 0]
    pos = [i for i in c if i > 0]
    b = sorted(a[:n - k])
    if b == []:
        print(ans)
        exit()
    ne = -1 if pos == [] else b[-1] * pos[0]
    p = -1 if neg == [] else b[0] * neg[0]

    if ne > p and pos[0] * b[0] < 0:
        ans = ans * pow(pos[0], mod - 2, mod) * b[0] % mod
    elif neg[0] * b[-1] < 0:
        ans = ans * pow(neg[0], mod - 2, mod) * b[-1] % mod
    else:
        ans = reduce(lambda a, b: (a * b) % mod, a[:k])
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from functools import reduce

mod = 10**9 + 7
n, k, *a = map(int, open(0).read().split())
a.sort(key=lambda x: abs(x))
ans = reduce(lambda a, b: (a * b) % mod, a[-k:])

c = a[-k:]
j = sum(i < 0 for i in c) % 2
if j:
    c = a[-k:]
    neg = [i for i in c if i < 0]
    pos = [i for i in c if i > 0]
    b = sorted(a[:n - k])
    if b == []:
        print(ans)
        exit()
    ne = 0 if neg == [] else b[-1] / neg[0]
    p = 0 if pos == [] else b[0] / pos[0]

    if ne >= p and 0 > p:
        ans = ans * pow(pos[0], mod - 2, mod) * b[0] % mod
    elif 0 > ne:
        ans = ans * pow(neg[0], mod - 2, mod) * b[-1] % mod
    else:
        ans = reduce(lambda a, b: (a * b) % mod, a[:k])
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from functools import reduce

mod = 10**9 + 7
n, k, *a = map(int, open(0).read().split())
a.sort(key=lambda x: abs(x))
ans = reduce(lambda a, b: (a * b) % mod, a[-k:])

c = a[-k:]
j = sum(i < 0 for i in c) % 2
if j:
    c = a[-k:]
    neg = [i for i in c if i < 0]
    pos = [i for i in c if i > 0]
    b = sorted(a[:n - k])
    if b == []:
        print(ans)
        exit()
    ne = 0 if neg == [] else b[-1] / neg[0]
    p = 0 if pos == [] else b[0] / pos[0]

    if ne > p and 0 > p:
        ans = ans * pow(pos[0], mod - 2, mod) * b[0] % mod
    elif 0 > ne:
        ans = ans * pow(neg[0], mod - 2, mod) * b[-1] % mod
    else:
        ans = reduce(lambda a, b: (a * b) % mod, a[:k])
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from functools import reduce

mod = 10**9 + 7
n, k, *a = map(int, open(0).read().split())
a.sort(key=lambda x: abs(x))
ans = reduce(lambda a, b: (a * b) % mod, a[-k:])

c = a[-k:]
j = sum(i < 0 for i in c) % 2
if j:
    c = a[-k:]
    neg = [i for i in c if i < 0]
    pos = [i for i in c if i > 0]
    b = sorted(a[:n - k])
    if b == []:
        print(ans)
        exit()
    ne = 0 if neg == [] else b[-1] / neg[0]
    p = 0 if pos == [] else b[0] / pos[0]

    if ne > p and 0 > p:
        ans = ans * pow(pos[0], mod - 2, mod) * b[0] % mod
    elif 0 > n:
        ans = ans * pow(neg[0], mod - 2, mod) * b[-1] % mod
    else:
        ans = reduce(lambda a, b: (a * b) % mod, a[:k])
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
MOD = 10 ** 9 + 6
A.sort(reverse=True)
from collections import deque

d = deque(A)
cnt = 0
ans = 1
for i in range(K, 0, -2):
    if i >= 2:
        head0 = A[0]
        head1 = A[1]
        tail0 = A[N - 1 - cnt]
        tail1 = A[N - 2 - cnt]
        a, b = 0, 0
        if head0 * head1 > tail0 * tail1:
            a = d.popleft()
            b = d.popleft()
        else:
            a = d.pop()
            b = d.pop()
        cnt += 2
        ans *= (a * b) % MOD
    else:
        head0 = A[0]
        tail0 = A[N - 1 - cnt]
        a_head0 = abs(head0)
        a_tail0 = abs(tail0)
        if head0 == a_head0:
            if tail0 == a_tail0:
                ans *= max(a_head0, a_tail0) % MOD
            else:
                ans *= head0 % MOD
        else:
            if tail0 == a_tail0:
                ans *= tail0 % MOD
            else:
                ans *= min(a_head0, a_tail0) % MOD
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
A.sort(key=lambda x: abs(x), reverse=True)
ans = 1
h = 1
count = 0
for i in range(N):
    if A[i] < 0:
        if h == 1:
            h = A[i]
        else:
            if count < K - 1:
                ans *= h * A[i]
                h = 1
                count += 2     
    elif A[i] > 0:
        ans *= A[i]
        count += 1
    if count == K:
        break
    if i == N-1 and h != 1:
        ans = 1
        for i in range(K):
            ans *= A[N-i-1]
print(ans%(10**9+7))

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 %R)
        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()
    if first_pos==-1 and first_neg==-1:
        print(0)
        exit()
    list_product=[]
    if first_neg!=-1 and last_pos!=-1:
        list_product.append((P*B[last_neg][1]*B[first_neg][1]) %R)
    if first_pos!=-1:
        P1=(P*B[first_pos][1]) %R
        if last_pos!=-1:
            P1=(P1*B[last_pos][1]) %R
        list_product.append(P1)
    print(max(list_product))
else:
    print(F)

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()
    if first_pos==-1 and first_neg==-1:
        print(0)
        exit()
    list_product=[]
    if first_neg!=-1 and last_pos!=-1:
        list_product.append((P*B[last_neg][1]*B[first_neg][1]) %R)
    if first_pos!=-1:
        P1=(P*B[first_pos][1]) %R
        if last_pos!=-1:
            P1=(P1*B[last_pos][1]) %R
        list_product.append(P1)
    print(max(list_product))
else:
    print(F)

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)
print([b[0] for b in B])
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()
    if first_pos==-1 and first_neg==-1:
        print(0)
        exit()
    list_product=[]
    if first_neg!=-1 and last_pos!=-1:
        list_product.append((P*B[last_neg][1]*B[first_neg][1]) %R)
    if first_pos!=-1:
        P1=(P*B[first_pos][1]) %R
        if last_pos!=-1:
            P1=(P1*B[last_pos][1]) %R
        list_product.append(P1)
    print(list_product)
    print(max(list_product))
else:
    print(F)

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()
    if first_pos==-1 and first_neg==-1:
        print(0)
        exit()
    list_product=[]
    if first_neg!=-1:
        list_product.append((P*B[last_neg][1]*B[first_neg][1]) %R)
    if first_pos!=-1:
        P1=(P*B[first_pos][1]) %R
        if last_pos!=-1:
            P1=(P1*B[last_pos][1]) %R
        list_product.append(P1)
    print(max(list_product))
else:
    print(F)

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)
print([b[0] for b in B])
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()
    if first_pos==-1 and first_neg==-1:
        print(0)
        exit()
    list_product=[]
    if first_neg!=-1:
        print(first_neg)
        list_product.append((P*B[last_neg][1]*B[first_neg][1]) %R)
    if first_pos!=-1:
        print(first_pos)
        P1=(P*B[first_pos][1]) %R
        if last_pos!=-1:
            P1=(P1*B[last_pos][1]) %R
        list_product.append(P1)
    print(list_product)
    print(max(list_product))
else:
    print(F)

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()
    if first_pos==-1 and first_neg==-1:
        print(0)
        exit()
    list_product=[]
    if first_neg!=-1:
        list_product.append((P*B[last_neg][1]*B[first_neg][1]) %R)
    if first_pos!=-1:
        P1=(P*B[first_pos][1]) %R
        if last_pos!=-1:
            P1=(P1*B[last_pos][1]) %R
        list_product.append(P1)
    print(max(list_product))
else:
    print(F)

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:
        P1=(P*B[first_pos][1]) %R
        if last_pos!=-1:
            P1=(P1*B[last_pos][1]) %R
        list_product.append(P1)
    print(max(list_product))
else:
    print(F)