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:
def sgn(x):
    if x>0:
        return 1
    elif x<0:
        return -1
    else:
        return 0
N,K=map(int,input().split())
A=[int(i) for i in input().split()]
mod=10**9+7
if K==N:
    ans=1
    for i in range(N):
        ans=ans*A[i]
        ans%=mod
    print(ans)
    exit()
if max(A)<0 and K%2==1:
    ans=1
    A.sort()
    for i in range(K):
        ans=ans*A[-i-1]
        ans%=mod
    print(ans)
    exit()
A.sort(key=lambda x:abs(x),reverse=True)
P=1
Pfugou=1
for i in range(K):
    P=P*A[i]
    P%=mod
    Pfugou=Pfugou*sgn(A[i])
if Pfugou!=-1:
    print(P)
    exit()
#xiを除いてyiを加える
#abs(x1/y1)が小さいほうが良い
#abs(yi)は大きいほうがいい
#abs(xi)は小さいほうがいい
x1=10**15
x2=10**15
y1=0
y2=0
for i in range(K):
    if A[i]<0:
        x2=abs(A[i])
    elif A[i]>0:
        x1=abs(A[i])
for i in range(N-1,K-1,-1):
    if A[i]<0:
        y1=abs(A[i])
    elif A[i]>0:
        y2=abs(A[i])
if abs(x2*y1)<=abs(x1*y2):
    #y2/x2のほうが大きい
    print((-P*pow(x2,mod-2,mod)*y2)%mod)
else:
    print((-P*pow(x1,mod-2,mod)*y1)%mod)

Submission status: Wrong Answer

Solution code:
def sgn(x):
    if x>0:
        return 1
    elif x<0:
        return -1
    else:
        return 0
N,K=map(int,input().split())
A=[int(i) for i in input().split()]
mod=10**9+7
if K==N:
    ans=1
    for i in range(N):
        ans=ans*A[i]
        ans%=mod
    print(ans)
    exit()
if max(A)<0 and K%2==1:
    ans=1
    A.sort()
    for i in range(K):
        ans=ans*A[-i-1]
        ans%=mod
    print(ans)
    exit()
A.sort(key=lambda x:abs(x),reverse=True)
P=1
Pfugou=1
for i in range(K):
    P=P*A[i]
    P%=mod
    Pfugou=Pfugou*sgn(A[i])
if Pfugou!=-1:
    print(P)
    exit()
#xiを除いてyiを加える
#abs(x1/y1)が小さいほうが良い
#abs(yi)は大きいほうがいい
#abs(xi)は小さいほうがいい
x1=10**15
x2=10**15
y1=0
y2=0
for i in range(K):
    if A[i]<0:
        x2=abs(A[i])
    elif A[i]>0:
        x1=abs(A[i])
for i in range(N-1,K-1,-1):
    if A[i]<0:
        y1=abs(A[i])
    elif A[i]>0:
        y2=abs(A[i])
if abs(x2*y1)<=abs(x1*y2):
    #y2/x2のほうが大きい
    print((P*pow(x2,mod-2,mod)*y2)%mod)
else:
    print((P*pow(x1,mod-2,mod)*y1)%mod)

Submission status: Wrong Answer

Solution code:
def sgn(x):
    if x>0:
        return 1
    elif x<0:
        return -1
    else:
        return 0
N,K=map(int,input().split())
A=[int(i) for i in input().split()]
mod=10**9+7
if K==N:
    ans=1
    for i in range(N):
        ans=ans*A[i]
        ans%=mod
    print(ans)
    exit()
if max(A)<0 and K%2==1:
    ans=1
    A.sort()
    for i in range(K):
        ans=ans*A[-i-1]
        ans%=mod
    print(ans)
    exit()
A.sort(key=lambda x:abs(x),reverse=True)
P=1
Pfugou=1
for i in range(K):
    P=P*A[i]
    P%=mod
    Pfugou=Pfugou*sgn(A[i])
if Pfugou!=-1:
    print(P)
    exit()
#xiを除いてyiを加える
#abs(x1/y1)が小さいほうが良い
#abs(yi)は大きいほうがいい
#abs(xi)は小さいほうがいい
x1=10**15
x2=10**15
y1=0
y2=0
for i in range(K):
    if A[i]<0:
        x2=A[i]
    elif A[i]>0:
        x1=A[i]
for i in range(N-1,K-1,-1):
    if A[i]<0:
        y1=A[i]
    elif A[i]>0:
        y2=A[i]
if abs(x2*y1)<=abs(x1*y2):
    #y2/x2のほうが大きい
    print((P*pow(x2,mod-2,mod)*y2)%mod)
else:
    print((P*pow(x1,mod-2,mod)*y1)%mod)

Submission status: Wrong Answer

Solution code:
MOD = 10**9 + 7
N, K = map(int, input().split())
A = sorted([int(i) for i in input().split()])

B = []
plus, zero, minus = 0, 0, 0
for i in range(N):
    if A[i] > 0:
        B.append((A[i], 1))
        plus += 1
    elif A[i] == 0:
        B.append((0, 0))
        zero += 1
    else:
        B.append((-A[i], -1))
        minus += 1


if plus >= K-(min(K, minus)//2*2):
    B = sorted(B, key=lambda x: x[0], reverse=True)

    c = 1
    for i in range(K):
        c *= B[i][1]

    if c == 1:
        ans = 1
        for i in range(K):
            ans *= B[i][0]
            ans %= MOD

    else:
        ap, an = 1, 1
        skip = []
        for i in range(K-1, -1, -1):
            if B[i][1] == 1:
                ap *= B[i][0]
                skip.append(i)
                break
        for i in range(K-1, -1, -1):
            if B[i][1] == -1:
                an *= B[i][0]
                skip.append(i)
                break
        for i in range(K, N):
            if B[i][1] == 1:
                ap *= B[i][0]
                break
        for i in range(K, N):
            if B[i][1] == -1:
                an *= B[i][0]
                break
        a = max(ap, an)

        ans = 1
        for i in range(K):
            if i not in skip:
                ans *= B[i][0]
                ans %= MOD
        ans *= a
        ans %= MOD

elif zero >= K-(min(K, minus)//2*2):
    print(0)
    exit()

else:
    B = sorted(B, key=lambda x: x[0], reverse=False)
    ans = 1
    for i in range(K):
        ans *= B[i][0]*B[i][1]
        ans %= MOD

print(ans)

Submission status: Wrong Answer

Solution code:
MOD = 10**9 + 7
N, K = map(int, input().split())
A = sorted([int(i) for i in input().split()])

B = []
plus, zero, minus = 0, 0, 0
for i in range(N):
    if A[i] >= 0:
        B.append((A[i], 1))
        plus += 1
    elif A[i] == 0:
        B.append((0, 0))
        zero += 1
    else:
        B.append((-A[i], -1))
        minus += 1


if plus >= K-(min(K, minus)//2*2):
    B = sorted(B, key=lambda x: x[0], reverse=True)

    c = 1
    for i in range(K):
        c *= B[i][1]

    if c == 1:
        ans = 1
        for i in range(K):
            ans *= B[i][0]
            ans %= MOD

    else:
        ap, an = 1, 1
        skip = []
        for i in range(K-1, -1, -1):
            if B[i][1] == 1:
                ap *= B[i][0]
                skip.append(i)
                break
        for i in range(K-1, -1, -1):
            if B[i][1] == -1:
                an *= B[i][0]
                skip.append(i)
                break
        for i in range(K, N):
            if B[i][1] == 1:
                ap *= B[i][0]
                break
        for i in range(K, N):
            if B[i][1] == -1:
                an *= B[i][0]
                break
        a = max(ap, an)

        ans = 1
        for i in range(K):
            if i not in skip:
                ans *= B[i][0]
                ans %= MOD
        ans *= a
        ans %= MOD

elif zero >= K-(min(K, minus)//2*2):
    print(0)
    exit()

else:
    B = sorted(B, key=lambda x: x[0], reverse=False)
    ans = 1
    for i in range(K):
        ans *= B[i][0]*B[i][1]
        ans %= MOD

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
MOD = 10**9 + 7
N, K = map(int, input().split())
A = sorted([int(i) for i in input().split()])

B = []
for i in range(N):
    if A[i] >= 0:
        B.append((A[i], 1))
    else:
        B.append((-A[i], -1))

ne = bisect.bisect_left(A, 0)


if N-ne >= K-(min(K, ne)//2*2):
    B = sorted(B, key=lambda x: x[0], reverse=True)

    c = 1
    for i in range(K):
        c *= B[i][1]

    if c == 1:
        ans = 1
        for i in range(K):
            ans *= B[i][0]
            ans %= MOD

    else:
        ap, an = 1, 1
        skip = []
        for i in range(K-1, -1, -1):
            if B[i][1] == 1:
                ap *= B[i][0]
                skip.append(i)
                break
        for i in range(K-1, -1, -1):
            if B[i][1] == -1:
                an *= B[i][0]
                skip.append(i)
                break
        for i in range(K, N):
            if B[i][1] == 1:
                ap *= B[i][0]
                break
        for i in range(K, N):
            if B[i][1] == -1:
                an *= B[i][0]
                break
        a = max(ap, an)

        ans = 1
        for i in range(K):
            if i not in skip:
                ans *= B[i][0]
                ans %= MOD
        ans *= a
        ans %= MOD

else:
    B = sorted(B, key=lambda x: x[0], reverse=False)
    ans = 1
    for i in range(K):
        ans *= B[i][0]*B[i][1]
        ans %= MOD

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
MOD = 10**9 + 7
N, K = map(int, input().split())
A = sorted([int(i) for i in input().split()])

B = []
for i in range(N):
    if A[i] >= 0:
        B.append((A[i], 1))
    else:
        B.append((-A[i], -1))

ne = bisect.bisect_left(A, 0)


if N-ne >= K-(min(K, ne)//2*2):
    B = sorted(B, key=lambda x: x[0], reverse=True)

    c = 1
    for i in range(K):
        c *= B[i][1]

    if c == 1:
        ans = 1
        for i in range(K):
            ans *= B[i][0]
            ans %= MOD

    else:
        ans = 0

else:
    B = sorted(B, key=lambda x: x[0], reverse=False)
    ans = 1
    for i in range(K):
        ans *= B[i][0]*B[i][1]
        ans %= MOD

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

n, k = map(int,input().split())
a = list(map(int,input().split()))

a = np.array(a)

a_sort_raw = sorted(a)
a_arg_raw = np.argsort(a)

a_abs = list(map(abs,a))
a_abs = np.array(a_abs)

a_sort = sorted(a_abs,reverse=True)

a_arg = np.argsort(-a_abs)

a_arg_k = a_arg[0:k]
a_arg_k_ = a_arg[k:]

a_pow = 1

if all([1 if i < 0 else 0 for i in a]) and (k % 2 != 0):
    for i in range(n-1,k,-1):
        a_pow *= a[a_arg_raw[i]]

else:
    for i in range(k):
        a_pow *= a[a_arg[i]]
    
    if a_pow < 0:
        a_pow_p = 1
        for i in reversed(a_arg_k):
            if a[i] < 0:
                for j in a_arg_k_:
                    if a[j] > 0:
                        a_pow_p = a_pow/a[i]*a[j]
                        break
                break
                
        a_pow_m = 1
        for i in reversed(a_arg_k):
            if a[i] > 0:
                for j in a_arg_k_:
                    if a[j] < 0:
                        a_pow_m = a_pow/a[i]*a[j]
                        break
                break
                
        if a_pow_p > a_pow_m:
            a_pow = a_pow_p
        else:
            a_pow = a_pow_m

output = a_pow % (10**9 + 7)
print(int(output))

Submission status: Wrong Answer

Solution code:
import numpy as np

n, k = map(int,input().split())
a = list(map(int,input().split()))

a = np.array(a)

a_sort_raw = sorted(a)
a_arg_raw = np.argsort(a)

a_abs = list(map(abs,a))
a_abs = np.array(a_abs)

a_sort = sorted(a_abs,reverse=True)

a_arg = np.argsort(-a_abs)

a_arg_k = a_arg[0:k]
a_arg_k_ = a_arg[k:]

a_pow = 1

if all([1 if i < 0 else 0 for i in a]) and (k % 2 != 0):
    for i in range(n-1,k,-1):
        a_pow *= a[a_arg_raw[i]]

else:
    for i in range(k):
        a_pow *= a[a_arg[i]]
    
    if a_pow < 0:
        a_pow_p = 1
        for i in reversed(a_arg_k):
            if a[i] < 0:
                for j in a_arg_k_:
                    if a[j] > 0:
                        a_pow_p = a_pow/a[i]*a[j]
                        break
                break
                
        a_pow_m = 1
        for i in reversed(a_arg_k):
            if a[i] > 0:
                for j in a_arg_k_:
                    if a[j] < 0:
                        a_pow_m = a_pow/a[i]*a[j]
                        break
                break
       
        if a_pow_p > a_pow_m:
            a_pow = a_pow_p

        else:
            a_pow = a_pow_m


output = a_pow % (10**9 + 7)
print(output)

Submission status: Wrong Answer

Solution code:
import numpy as np

n, k = map(int,input().split())
a = list(map(int,input().split()))

a = np.array(a)

a_sort_raw = sorted(a)
a_arg_raw = np.argsort(a)

a_abs = list(map(abs,a))
a_abs = np.array(a_abs)

a_sort = sorted(a_abs,reverse=True)

a_arg = np.argsort(-a_abs)

a_arg_k = a_arg[0:k]
a_arg_k_ = a_arg[k:]

a_pow = 1

if all([1 if i < 0 else 0 for i in a]) and (k % 2 != 0):
    for i in range(n-1,k,-1):
        a_pow *= a[a_arg_raw[i]]

else:
    for i in range(k):
        a_pow *= a[a_arg[i]]
    
    if a_pow < 0:
        a_pow_p = 1
        for i in reversed(a_arg_k):
            if a[i] < 0:
                for j in a_arg_k_:
                    if a[j] > 0:
                        a_pow_p = a_pow/a[i]*a[j]
                        break
                break
                
        a_pow_m = 1
        for i in reversed(a_arg_k):
            if a[i] > 0:
                for j in a_arg_k_:
                    if a[j] < 0:
                        a_pow_m = a_pow/a[i]*a[j]
                        break
                break
        if a_pow_p > a_pow_m:
            a_pow = a_pow_p
        else:
            a_pow = a_pow_m

output = a_pow % (10**9 + 7)
print(output)

Submission status: Wrong Answer

Solution code:
import numpy as np

n, k = map(int,input().split())
a = list(map(int,input().split()))

a = np.array(a)

a_abs = list(map(abs,a))
a_abs = np.array(a_abs)

a_sort = sorted(a_abs,reverse=True)

a_arg = np.argsort(-a_abs)

a_arg_k = a_arg[0:k]
a_arg_k_ = a_arg[k:]

a_pow = 1

if all([1 if i < 0 else 0 for i in a]) and (k % 2 != 0):
    for i in range(n-1,k,-1):
        a_pow *= a[a_arg[i]]

else:
    for i in range(k):
        a_pow *= a[a_arg[i]]
    
    if a_pow < 0:
        a_pow_p = 1
        for i in reversed(a_arg_k):
            if a[i] < 0:
                for j in a_arg_k_:
                    if a[j] > 0:
                        a_pow_p = a_pow/a[i]*a[j]
                        break
                break
                
        a_pow_m = 1
        for i in reversed(a_arg_k):
            if a[i] > 0:
                for j in a_arg_k_:
                    if a[j] < 0:
                        a_pow_m = a_pow/a[i]*a[j]
                        break
                break
        if a_pow_p > a_pow_m:
            a_pow = a_pow_p
        else:
            a_pow = a_pow_m

output = a_pow % (10**9 + 7)
print(output)

Submission status: Wrong Answer

Solution code:
import numpy as np

n, k = map(int,input().split())
a = list(map(int,input().split()))

a = np.array(a)

a_abs = list(map(abs,a))
a_abs = np.array(a_abs)

a_sort = sorted(a_abs,reverse=True)

a_arg = np.argsort(-a_abs)

a_arg_k = a_arg[0:k]
a_arg_k_ = a_arg[k:]

a_pow = 1

if all([1 if i < 0 else 0 for i in a]) and (k % 2 != 0):
    for i in range(n-1,k-1,-1):
        a_pow *= a[a_arg[i]]

else:
    for i in range(k):
        a_pow *= a[a_arg[i]]
    
    if a_pow < 0:
        a_pow_p = 1
        for i in reversed(a_arg_k):
            if a[i] < 0:
                for j in a_arg_k_:
                    if a[j] > 0:
                        a_pow_p = a_pow/a[i]*a[j]
                        break
                break
                
        a_pow_m = 1
        for i in reversed(a_arg_k):
            if a[i] > 0:
                for j in a_arg_k_:
                    if a[j] < 0:
                        a_pow_m = a_pow/a[i]*a[j]
                        break
                break
        if a_pow_p > a_pow_m:
            a_pow = a_pow_p
        else:
            a_pow = a_pow_m

output = a_pow % (10**9 + 7)
print(output)

Submission status: Wrong Answer

Solution code:
import numpy as np

# n, k = map(int,input().split())
# a = list(map(int,input().split()))

n, k = 5, 2
# a = [-8,-1,-2,-4,3,-5,7]
a = [-8,-1,-2,-4,-5]

a = np.array(a)

a_abs = list(map(abs,a))
a_abs = np.array(a_abs)

a_sort = sorted(a_abs,reverse=True)

a_arg = np.argsort(-a_abs)

a_arg_k = a_arg[0:k]
a_arg_k_ = a_arg[k:]

a_pow = 1

if all([1 if i < 0 else 0 for i in a]) and (k % 2 != 0):
    for i in range(n,k-1,-1):
        a_pow *= a[a_arg[i]]

else:
    for i in range(k):
        a_pow *= a[a_arg[i]]
    
    if a_pow < 0:
        a_pow_p = 1
        for i in reversed(a_arg_k):
            if a[i] < 0:
                for j in a_arg_k_:
                    if a[j] > 0:
                        a_pow_p = a_pow/a[i]*a[j]
                        break
                break
                
        a_pow_m = 1
        for i in reversed(a_arg_k):
            if a[i] > 0:
                for j in a_arg_k_:
                    if a[j] < 0:
                        a_pow_m = a_pow/a[i]*a[j]
                        break
                break
        if a_pow_p > a_pow_m:
            a_pow = a_pow_p
        else:
            a_pow = a_pow_m

output = a_pow % (10**9 + 7)
print(output)

Submission status: Wrong Answer

Solution code:
import numpy as np

n, k = map(int,input().split())
a = list(map(int,input().split()))

a = np.array(a)

a_abs = list(map(abs,a))
a_abs = np.array(a_abs)

a_sort = sorted(a_abs,reverse=True)

a_arg = np.argsort(-a_abs)

a_arg_k = a_arg[0:k]
a_arg_k_ = a_arg[k:]

a_pow = 1

if all([1 if i < 0 else 0 for i in a]) and (k % 2 != 0):
    for i in range(n,k-1,-1):
        a_pow *= a[a_arg[i-1]]

else:
    for i in range(k):
        a_pow *= a[a_arg[i]]
    
    if a_pow < 0:
        a_pow_p = 1
        for i in reversed(a_arg_k):
            if a[i] < 0:
                for j in a_arg_k_:
                    if a[j] > 0:
                        a_pow_p = a_pow/a[i]*a[j]
                        break
                break
                
        a_pow_m = 1
        for i in reversed(a_arg_k):
            if a[i] > 0:
                for j in a_arg_k_:
                    if a[j] < 0:
                        a_pow_m = a_pow/a[i]*a[j]
                        break
                break
        if a_pow_p > a_pow_m:
            a_pow = a_pow_p
        else:
            a_pow = a_pow_m

output = a_pow % (10**9 + 7)
print(output)

Submission status: Wrong Answer

Solution code:
import numpy as np

n, k = map(int,input().split())
a = list(map(int,input().split()))

a = np.array(a)

a_sort_raw = sorted(a)
a_arg_raw = np.argsort(a)

a_abs = list(map(abs,a))
a_abs = np.array(a_abs)

a_sort = sorted(a_abs,reverse=True)

a_arg = np.argsort(-a_abs)
a_arg_r = np.argsort(a_abs)

a_pow = 1

if all([1 if i < 0 else 0 for i in a]):
    for i in range(k):
        a_pow *= a[a_arg_r[i]]

else:
    for i in range(k):
        a_pow *= a[a_arg[i]]

    a_arg_k = a_arg[0:k]
    a_arg_k_ = a_arg[k:]
    
    if a_pow < 0:
        a_pow_p = 1
        for i in reversed(a_arg_k):
            if a[i] < 0:
                for j in a_arg_k_:
                    if a[j] > 0:
                        a_pow_p = a_pow/a[i]*a[j]
                        break
                break
                
        a_pow_m = 1
        for i in reversed(a_arg_k):
            if a[i] > 0:
                for j in a_arg_k_:
                    if a[j] < 0:
                        a_pow_m = a_pow/a[i]*a[j]
                        break
                break
        if a_pow_p > a_pow_m:
            a_pow = a_pow_p
        else:
            a_pow = a_pow_m

output = a_pow % (10**9 + 7)
print(output)

Submission status: Wrong Answer

Solution code:
import numpy as np

n, k = map(int,input().split())
a = list(map(int,input().split()))

a = np.array(a)

a_sort_raw = sorted(a)
a_arg_raw = np.argsort(a)

a_abs = list(map(abs,a))
a_abs = np.array(a_abs)

a_sort = sorted(a_abs,reverse=True)

a_arg = np.argsort(-a_abs)
a_arg_r = np.argsort(a_abs)

a_pow = 1

if all([1 if i < 0 else 0 for i in a]):
    for i in range(k):
        a_pow *= a[a_arg_r[i]]

else:
    for i in range(k):
        a_pow *= a[a_arg[i]]

    a_arg_k = a_arg[0:k]
    a_arg_k_ = a_arg[k:]
    
    if a_pow < 0:
        a_pow_p = 1
        for i in reversed(a_arg_k):
            if a[i] < 0:
                for j in a_arg_k_:
                    if a[j] > 0:
                        a_pow_p = a_pow/a[i]*a[j]
                        break
                break
                
        a_pow_m = 1
        for i in reversed(a_arg_k):
            if a[i] > 0:
                for j in a_arg_k_:
                    if a[j] < 0:
                        a_pow_m = a_pow/a[i]*a[j]
                        break
                break
        if a_pow_p > a_pow_m:
            a_pow = a_pow_p
            print(a_pow_p)
        else:
            a_pow = a_pow_m
            print(a_pow_m)

output = a_pow % (10**9 + 7)
print(output)

Submission status: Wrong Answer

Solution code:
DE = 10**9 + 7
N, K = list(map(lambda x: int(x), input().split(' ')))
A = list(map(lambda x: int(x), input().split(' ')))

def main():
  A_posi = []
  A_nega = []
  for a in A:
    if a > 0:
      A_posi.append(a)
    elif a < 0:
      A_nega.append(-a)

  len_posi = len(A_posi)
  len_nega = len(A_nega)
  
  if len_posi + len_nega < K:
    return 0

  if (len_nega % 2 == 1 and K == len_posi + len_nega) or (K % 2 == 1 and len_posi == 0):
    if len_posi + len_nega == N:
      A_posi.extend(A_nega)
      A_posi.sort()
      answer = 1
      for i in range(K):
        answer *= (- A_posi[i]) % DE
        answer %= DE
      return answer
    else:
      return 0
  A_posi.sort(reverse=True)
  A_nega.sort(reverse=True)
  posi = 0
  nega = 0
  answer = 1

  if K % 2 == 1:
    answer = A_posi[0] % DE
    posi = 1

  while posi + nega + 2 <= K:
    p = A_posi[posi] * A_posi[posi + 1] if posi + 1 < len_posi else 0
    n = A_nega[nega] * A_nega[nega + 1] if nega + 1 < len_nega else 0
    if p > n:
      answer *= p % DE
      answer %= DE
      posi += 2
    else:
      answer *= n % DE
      answer %= DE
      nega += 2

  return answer

print(main())

Submission status: Wrong Answer

Solution code:
DE = int(10e9 + 7)
N, K = list(map(lambda x: int(x), input().split(' ')))
A = list(map(lambda x: int(x), input().split(' ')))

def main():
  A_posi = []
  A_nega = []
  for a in A:
    if a > 0:
      A_posi.append(a)
    elif a < 0:
      A_nega.append(-a)

  len_posi = len(A_posi)
  len_nega = len(A_nega)
  
  if len_posi + len_nega < K:
    return 0

  if (len_nega % 2 == 1 and K == len_posi + len_nega) or (K % 2 == 1 and len_posi == 0):
    if len_posi + len_nega == N:
      A_posi.extend(A_nega)
      A_posi.sort()
      answer = 1
      for i in range(K):
        answer *= (- A_posi[i]) % DE
        answer %= DE
      return answer
    else:
      return 0
  A_posi.sort(reverse=True)
  A_nega.sort(reverse=True)
  posi = 0
  nega = 0
  answer = 1

  if K % 2 == 1:
    answer = A_posi[0] % DE
    posi = 1

  while posi + nega + 2 <= K:
    p = A_posi[posi] * A_posi[posi + 1] if posi + 1 < len_posi else 0
    n = A_nega[nega] * A_nega[nega + 1] if nega + 1 < len_nega else 0
    if p > n:
      answer *= p % DE
      answer %= DE
      posi += 2
    else:
      answer *= n % DE
      answer %= DE
      nega += 2

  return answer



print(main())

Submission status: Wrong Answer

Solution code:
DE = int(10e9 + 7)
N, K = list(map(lambda x: int(x), input().split(' ')))
A = list(map(lambda x: int(x), input().split(' ')))

def main():
  A_posi = []
  A_nega = []
  for a in A:
    if a > 0:
      A_posi.append(a)
    elif a < 0:
      A_nega.append(-a)

  len_posi = len(A_posi)
  len_nega = len(A_nega)
  
  if len_posi + len_nega < K:
    return 0

  if (len_nega % 2 == 1 and K == len_posi + len_nega) or (K % 2 == 1 and len_posi == 0):
    if len_posi + len_nega == N:
      answer = 0
      for a in A_posi:
        answer *= a % DE
        answer %= DE
      for a in A_nega:
        answer *= a % DE
        answer %= DE
      return answer
    else:
      return 0
  A_posi.sort(reverse=True)
  A_nega.sort(reverse=True)
  posi = 0
  nega = 0
  answer = 1

  if K % 2 == 1:
    answer = A_posi[0] % DE
    posi = 1

  while posi + nega + 2 <= K:
    p = A_posi[posi] * A_posi[posi + 1] if posi + 1 < len_posi else 0
    n = A_nega[nega] * A_nega[nega + 1] if nega + 1 < len_nega else 0
    if p > n:
      answer *= p % DE
      answer %= DE
      posi += 2
    else:
      answer *= n % DE
      answer %= DE
      nega += 2

  return answer



print(main())

Submission status: Wrong Answer

Solution code:
DE = int(10e9 + 7)
N, K = list(map(lambda x: int(x), input().split(' ')))
A = list(map(lambda x: int(x), input().split(' ')))

def main():
  A_posi = []
  A_nega = []
  for a in A:
    if a > 0:
      A_posi.append(a)
    elif a < 0:
      A_nega.append(-a)

  len_posi = len(A_posi)
  len_nega = len(A_nega)
  
  if len_posi + len_nega < K:
    return 0

  if (len_nega % 2 == 1 and K == len_posi + len_nega) or (K % 2 == 1 and len_posi == 0):
    if len_posi + len_nega == N:
      answer = 0
      for a in A_posi:
        answer *= a % DE
        answer %= DE
      for a in A_nega:
        answer *= a % DE
        answer %= DE
      return answer
    else:
      return 0
  A_posi.sort(reverse=True)
  A_nega.sort()
  posi = 0
  nega = 0
  answer = 1

  if K % 2 == 1:
    answer = A_posi[0] % DE
    posi = 1

  while posi + nega + 2 <= K:
    p = A_posi[posi] * A_posi[posi + 1] if posi + 1 < len_posi else 0
    n = A_nega[nega] * A_nega[nega + 1] if nega + 1 < len_nega else 0
    if p > n:
      answer *= p % DE
      answer %= DE
      posi += 2
    else:
      answer *= n % DE
      answer %= DE
      nega += 2

  return answer



print(main())

Submission status: Wrong Answer

Solution code:
"""
9 5
-4 -3 -2 -1 0 1 2 3
"""

N,K = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
Am = []
Ap = []
for a in A:
    if a < 0:
        Am.append(a)
    else:
        Ap.append(a)

mod = 10**9 + 7

ans = 1

if N == K:
    for a in A:
        ans *= a
        ans %= mod

elif A[-1] < 0:
    for i in range(K):
        ans *= Am[-1-i]
        ans %= mod

else:
    if K%2 == 1:
        ans *= Ap[-1]
        ans %= mod
        K -= 1
        del Ap[-1]
    Amd = []     
    Apd = []
    for i in range(len(Ap)//2):
        Apd.append(Ap[-1-(2*i)]*Ap[-1-(2*i+1)])
    for i in range(len(Am)//2):
        Amd.append(Am[2*i]*Am[2*i+1])
    Apd.append(-1)
    Amd.append(-1)
    
    ip = 0
    im = 0
    while K > 0:
        if Apd[ip] >= Amd[im]:
            ans *= Apd[ip]
            ip += 1
        else:
            ans *= Amd[im]
            im += 1
        ans %= mod
        K -= 2


ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
Am = []
Ap = []
for a in A:
    if a < 0:
        Am.append(a)
    else:
        Ap.append(a)

mod = 10**9 + 7

ans = 1

if N == K:
    for a in A:
        ans *= a
        ans %= mod

elif A[-1] < 0:
    for i in range(K):
        ans *= Am[-1-i]
        ans %= mod

else:
    if K%2 == 1:
        ans *= Ap[-1]
        ans %= mod
        K -= 1
        del Ap[-1]
    Amd = []     
    Apd = []
    for i in range(len(Ap)//2):
        Apd.append(Ap[-1-(2*i)]*Ap[-1-(2*i+1)])
    for i in range(len(Am)//2):
        Amd.append(Am[2*i]*Am[2*i+1])
    Apd.append(-1)
    Amd.append(-1)
    
    while K > 0:
        ip = 0
        im = 0
        if Apd[ip] >= Amd[im]:
            ans *= Apd[ip]
            ip += 1
        else:
            ans *= Amd[im]
            im += 1
        ans %= mod
        K -= 2


ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
Am = []
Ap = []
for a in A:
    if a < 0:
        Am.append(a)
    else:
        Ap.append(a)

mod = 10**9 + 7

ans = 1

if N == K:
    for a in A:
        ans *= a
        ans %= mod

elif A[-1] < 0:
    for i in range(K):
        ans *= Am[-1-i]
        ans %= mod

else:
    if K%2 == 1:
        ans *= Ap[-1]
        ans %= mod
        K -= 1
        del Ap[-1]
    Amd = []     
    Apd = []
    for i in range(len(Ap)//2):
        Apd.append(Ap[-1-(2*i)]*Ap[-1-(2*i+1)])
    for i in range(len(Am)//2):
        Amd.append(Am[2*i]*Am[2*i+1])
    Apd.append(-1)
    Amd.append(-1)
    
    while K > 0:
        ip = 0
        im = 0
        if Apd[ip] >= Amd[im]:
            ans *= Apd[ip]
            ip += 1
        else:
            ans *= Amd[im]
            im +- 1
        ans %= mod
        K -= 2
ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
Am = []
Ap = []
for a in A:
    if a < 0:
        Am.append(a)
    else:
        Ap.append(a)

mod = 10**9 + 7

ans = 1

if N == K:
    for a in A:
        ans *= a
        ans %= mod

elif A[-1] < 0:
    for i in range(K):
        ans *= Am[-1-i]
        ans %= mod

else:
    if K%2 == 1:
        ans *= Ap[-1]
        ans %= mod
        K -= 1
        del Ap[-1]
    Amd = []     
    Apd = []
    for i in range(len(Ap)//2):
        Apd.append(Ap[-1-(2*i)]*Ap[-1-(2*i+1)])
    for i in range(len(Am)//2):
        Amd.append(Am[2*i]*Am[2*i+1])
    Apd.append(-1)
    Amd.append(-1)
    
    while K > 0:
        ip = 0
        im = 0
        if Apd[ip] >= Amd[im]:
            ans *= Apd[ip]
            ip += 1
        else:
            ans *= Amd[im]
            im +- 1
        ans %= mod
        K -= 2
print(ans)