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:
MOD = int(10e8)+7
def prod(arr):
    ans=1
    for i in arr:ans = (ans*i)%MOD
    return ans
n,k = map(int,input().split())
arr = list(map(int,input().split()))
arr.sort(key=abs,reverse = True)
def negs():
    return len([arr[i] for i in range(k) if arr[i]<0])%2==0
#print(arr)
candi = prod(arr[:k])
if negs() or k==n:
    print(candi%MOD)
else:
    nextPos = max(arr[k:])
    smallNeg = -1
    for i in range(k-1,-1,-1):
        if arr[i]<0:
            smallNeg = arr[i]
            break
    candi //= smallNeg
    candi *= nextPos
    if nextPos<0:
        print(prod(arr[-k:])%MOD)
    else:
        print(candi%MOD)

Submission status: Wrong Answer

Solution code:
MOD = int(10e8)+7
print(MOD)
def prod(arr):
    ans=1
    for i in arr:ans = (ans*i)%MOD
    return ans
n,k = map(int,input().split())
arr = list(map(int,input().split()))
arr.sort(key=abs,reverse = True)
def negs():
    return len([arr[i] for i in range(k) if arr[i]<0])%2==0
#print(arr)
candi = prod(arr[:k])
if negs() or k==n:
    print(candi%MOD)
else:
    nextPos = max(arr[k:])
    smallNeg = -1
    for i in range(k-1,-1,-1):
        if arr[i]<0:
            smallNeg = arr[i]
            break
    candi //= smallNeg
    candi *= nextPos
    if nextPos<0:
        print(prod(arr[-k:])%MOD)
    else:
        print(candi%MOD)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
al = list(map(int, input().split()))
al_abs = []
al_p = []
al_m = []
zero_cnt = 0
MOD = 10**9+7

for a in al:
    if a == 0:
        zero_cnt+=1
    elif a > 0: 
        al_abs.append((abs(a),1))
    else: 
        al_abs.append((abs(a),-1))
al_abs.sort(reverse=True)


if n == k:
    ans = 1
    for a in al:
        ans *= a
        ans %= MOD
    print(ans)
    exit()


if len(al_abs) < k:
    print(0)
    exit()



# if len(al_abs) == k:
#     ans = 1
#     m_cnt = 0
#     for a,s in al_abs:
#         if s == -1: m_cnt += 1
#         ans *= a
#         ans %= MOD
#     if m_cnt%2 == 0:
#         print(ans)
#     else:
#         print(0)
#     exit()
    

# if not al_p:
#     if k%2 == 1 and zero_cnt > 0:
#         print(0)
#     else:
#         if k%2 == 1:
#             al_abs.sort()
#         ans = 1
#         for a,s in al_abs[:k]:
#             ans *= (a*(-1))
#             ans %= MOD
#         print(ans)
#     exit()



val = 1
m_cnt = 0
last_m = 0
last_p = 0
for i in range(k):
    v,sig = al_abs[i]
    val *= v
    val %= MOD
    if sig == -1:
        m_cnt += 1
        last_m = v
    else:
        last_p = v

if m_cnt %2 == 0:
    print(val)
    exit()


# minus -> plus 

## search next p
next_p = 0
for i in range(k,len(al_abs)):
    v,sig = al_abs[i]
    if sig == 1:
        next_p = v
        break

## search next m
next_m = 0
for i in range(k,len(al_abs)):
    v,sig = al_abs[i]
    if sig == -1:
        next_m = v
        break

if next_p == 0 and next_m == 0:
    print(0)
    exit()

# last_m -> next_p
if next_p*last_p >= next_m*last_m or last_p == 0:
# if next_p > 0 and next_m == 0:
    ans = 1
    skip_flag = False
    for i in range(k):
        v,sig = al_abs[i]
        if not skip_flag and v == last_m and sig == -1:
            ans *= next_p
            skip_flag = True
        else:
            ans *= v
        # print(ans)
        ans %= MOD
    print(ans)
    exit()


# last_p -> next_m
# if next_p == 0 and next_m > 0:
elif next_p*last_p <= next_m*last_m:
    ans = 1
    skip_flag = False
    for i in range(k):
        v,sig = al_abs[i]
        if not skip_flag and v == last_p and sig == 1:
            ans *= next_m
            skip_flag = True
        else:
            ans *= v
        ans %= MOD
    print(ans)
    exit()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
al = list(map(int, input().split()))
al_abs = []
al_p = []
al_m = []
zero_cnt = 0
MOD = 10**9+7


for a in al:
    if a == 0:
        zero_cnt+=1
    else:
        if a > 0: 
            al_abs.append((abs(a),1))
            al_p.append(a)
        elif a < 0: 
            al_abs.append((abs(a),-1))

al_abs.sort(reverse=True)
if len(al_abs) < k:
    print(0)
    exit()

if len(al_abs) == k:
    ans = 1
    m_cnt = 0
    for a,s in al_abs:
        if s == -1: m_cnt += 1
        ans *= a
        ans %= MOD
    if m_cnt%2 == 0:
        print(ans)
    else:
        print(0)
    exit()
    

if not al_p:
    if k%2 == 1 and zero_cnt > 0:
        print(0)
    else:
        if k%2 == 1:
            al_abs.sort()
        ans = 1
        for a,s in al_abs[:k]:
            ans *= (a*(-1))
            ans %= MOD
        print(ans)
    exit()



val = 1
m_cnt = 0
last_m = 0
last_p = 0
for i in range(k):
    v,sig = al_abs[i]
    val *= v
    val %= MOD
    if sig == -1:
        m_cnt += 1
        last_m = v
    else:
        last_p = v

if m_cnt %2 == 0:
    print(val)
    exit()


# minus -> plus 

## search next p
next_p = 0
for i in range(k,len(al_abs)):
    v,sig = al_abs[i]
    if sig == 1:
        next_p = v
        break

## search next m
next_m = 0
for i in range(k,len(al_abs)):
    v,sig = al_abs[i]
    if sig == -1:
        next_m = v
        break

# print(next_m, next_p)

if next_p == 0 and next_m == 0:
    print(0)
    exit()

# last_m -> next_p
if next_p*last_p >= next_m*last_m or last_p == 0:
# if next_p > 0 and next_m == 0:
    ans = 1
    skip_flag = False
    for i in range(k):
        v,sig = al_abs[i]
        if not skip_flag and v == last_m and sig == -1:
            ans *= next_p
            skip_flag = True
        else:
            ans *= v
        # print(ans)
        ans %= MOD
    print(ans)
    exit()


# last_p -> next_m
# if next_p == 0 and next_m > 0:
elif next_p*last_p <= next_m*last_m:
    ans = 1
    skip_flag = False
    for i in range(k):
        v,sig = al_abs[i]
        if not skip_flag and v == last_p and sig == 1:
            ans *= next_m
            skip_flag = True
        else:
            ans *= v
        ans %= MOD
    print(ans)
    exit()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
al = list(map(int, input().split()))
al_abs = []
al_p = []
al_m = []
zero_cnt = 0
MOD = 10**9+7


for a in al:
    if a == 0:
        zero_cnt+=1
    else:
        if a > 0: 
            al_abs.append((abs(a),1))
            al_p.append(a)
        elif a < 0: 
            al_abs.append((abs(a),-1))

al_abs.sort(reverse=True)
if len(al_abs) < k:
    print(0)
    exit()

if len(al_abs) == k:
    ans = 1
    m_cnt = 0
    for a,s in al_abs:
        if s == -1: m_cnt += 1
        ans *= a
        ans %= MOD
    if m_cnt%2 == 0:
        print(ans)
    else:
        print(0)
    exit()
    

if not al_p:
    if k%2 == 1 and zero_cnt > 0:
        print(0)
    else:
        ans = 1
        for a,s in al_abs[:k]:
            ans *= (a*(-1))
            ans %= MOD
        print(ans)
    exit()



val = 1
m_cnt = 0
last_m = 0
last_p = 0
for i in range(k):
    v,sig = al_abs[i]
    val *= v
    val %= MOD
    if sig == -1:
        m_cnt += 1
        last_m = v
    else:
        last_p = v

if m_cnt %2 == 0:
    print(val)
    exit()


# minus -> plus 

## search next p
next_p = 0
for i in range(k,len(al_abs)):
    v,sig = al_abs[i]
    if sig == 1:
        next_p = v
        break

## search next m
next_m = 0
for i in range(k,len(al_abs)):
    v,sig = al_abs[i]
    if sig == -1:
        next_m = v
        break

# print(next_m, next_p)

if next_p == 0 and next_m == 0:
    print(0)
    exit()

# last_m -> next_p
if next_p*last_p >= next_m*last_m or last_p == 0:
# if next_p > 0 and next_m == 0:
    ans = 1
    skip_flag = False
    for i in range(k):
        v,sig = al_abs[i]
        if not skip_flag and v == last_m and sig == -1:
            ans *= next_p
            skip_flag = True
        else:
            ans *= v
        # print(ans)
        ans %= MOD
    print(ans)
    exit()


# last_p -> next_m
# if next_p == 0 and next_m > 0:
elif next_p*last_p <= next_m*last_m:
    ans = 1
    skip_flag = False
    for i in range(k):
        v,sig = al_abs[i]
        if not skip_flag and v == last_p and sig == 1:
            ans *= next_m
            skip_flag = True
        else:
            ans *= v
        ans %= MOD
    print(ans)
    exit()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
al = list(map(int, input().split()))
al_abs = []
al_p = []
al_m = []
zero_cnt = 0
MOD = 10**9+7


for a in al:
    if a == 0:
        zero_cnt+=1
    else:
        if a > 0: 
            al_abs.append((abs(a),1))
            al_p.append(a)
        elif a < 0: 
            al_abs.append((abs(a),-1))
            al_m.append(abs(a))

al_abs.sort(reverse=True)
if len(al_abs) < k:
    print(0)
    exit()

if len(al_abs) == k:
    ans = 1
    m_cnt = 0
    for a,s in al_abs[:k]:
        if s == -1: m_cnt += 1
        ans *= (a*s)
        ans %= MOD
    if m_cnt%2 == 0:
        print(ans)
    else:
        print(0)
    exit()
    

if not al_p:
    al_abs.sort()
    ans = 1
    for a,s in al_abs[:k]:
        ans *= (a*(-1))
        ans %= MOD
    print(ans)
    exit()



val = 1
m_cnt = 0
last_m = 0
last_p = 0
for i in range(k):
    v,sig = al_abs[i]
    val *= v
    val %= MOD
    if sig == -1:
        m_cnt += 1
        last_m = v
    else:
        last_p = v

if m_cnt %2 == 0:
    print(val)
    exit()


# minus -> plus 

## search next p
next_p = 0
for i in range(k,len(al_abs)):
    v,sig = al_abs[i]
    if sig == 1:
        next_p = v
        break

## search next m
next_m = 0
for i in range(k,len(al_abs)):
    v,sig = al_abs[i]
    if sig == -1:
        next_m = v
        break

# print(next_m, next_p)

if next_p == 0 and next_m == 0:
    print(0)
    exit()

# last_m -> next_p
if next_p*last_p >= next_m*last_m or last_p == 0:
# if next_p > 0 and next_m == 0:
    ans = 1
    skip_flag = False
    for i in range(k):
        v,sig = al_abs[i]
        if not skip_flag and v == last_m and sig == -1:
            ans *= next_p
            skip_flag = True
        else:
            ans *= v
        # print(ans)
        ans %= MOD
    print(ans)
    exit()


# last_p -> next_m
# if next_p == 0 and next_m > 0:
elif next_p*last_p <= next_m*last_m:
    ans = 1
    skip_flag = False
    for i in range(k):
        v,sig = al_abs[i]
        if not skip_flag and v == last_p and sig == 1:
            ans *= next_m
            skip_flag = True
        else:
            ans *= v
        ans %= MOD
    print(ans)
    exit()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
al = list(map(int, input().split()))
al_abs = []
al_p = []
al_m = []
zero_cnt = 0
MOD = 10**9+7


for a in al:
    if a == 0:
        zero_cnt+=1
    else:
        if a > 0: 
            al_abs.append((abs(a),1))
            al_p.append(a)
        else: 
            al_abs.append((abs(a),-1))
            al_m.append(abs(a))

al_abs.sort(reverse=True)
if len(al_abs) < k:
    print(0)
    exit()

if len(al_abs) == k:
    ans = 1
    m_cnt = 0
    for a,s in al_abs[:k]:
        if s == -1: m_cnt += 1
        ans *= (a*s)
        ans %= MOD
    if m_cnt%2 == 0:
        print(ans)
    else:
        print(0)
    exit()
    

if not al_p:
    al_abs.sort()
    ans = 1
    for a,s in al_abs[:k]:
        ans *= (a*(-1))
        ans %= MOD
    print(ans)
    exit()



val = 1
m_cnt = 0
last_m = 0
last_p = 0
for i in range(k):
    v,sig = al_abs[i]
    val *= v
    val %= MOD
    if sig == -1:
        m_cnt += 1
        last_m = v
    else:
        last_p = v

if m_cnt %2 == 0:
    print(val)
    exit()


# minus -> plus 

## search next p
next_p = 0
for i in range(k,len(al_abs)):
    v,sig = al_abs[i]
    if sig == 1:
        next_p = v
        break

## search next m
next_m = 0
for i in range(k,len(al_abs)):
    v,sig = al_abs[i]
    if sig == -1:
        next_m = v
        break

# print(next_m, next_p)

if next_p == 0 and next_m == 0:
    print(0)
    exit()

# last_m -> next_p
if next_p*last_p >= next_m*last_m or last_p == 0:
# if next_p > 0 and next_m == 0:
    ans = 1
    skip_flag = False
    for i in range(k):
        v,sig = al_abs[i]
        if not skip_flag and v == last_m and sig == -1:
            ans *= next_p
            skip_flag = True
        else:
            ans *= v
        # print(ans)
        ans %= MOD
    print(ans)
    exit()


# last_p -> next_m
# if next_p == 0 and next_m > 0:
elif next_p*last_p <= next_m*last_m:
    ans = 1
    skip_flag = False
    for i in range(k):
        v,sig = al_abs[i]
        if not skip_flag and v == last_p and sig == 1:
            ans *= next_m
            skip_flag = True
        else:
            ans *= v
        ans %= MOD
    print(ans)
    exit()

Submission status: Wrong Answer

Solution code:
import sys

mod = 10 ** 9 + 7
N, K = map(int,input().split())
A = list(map(int,input().split()))
if all([a < 0 for a in A]):
    if K % 2 == 0:
        A.sort()
        ans = 1
        for i in range(K):
            ans *= A[i]
            ans %= mod
        print(ans)
        sys.exit()
    else:
        A.sort(reverse = True)
        ans = 1
        for i in range(K):
            ans *= A[i]
            ans %= mod
        print(ans)
        sys.exit()

A.sort()
l = 0
r = N - 1
ans = 1

if K % 2 == 1:
    ans *= A[r]
    ans %= mod

for _ in range(K // 2):
    if A[l] * A[l+1] >= A[r] * A[r-1]:
        ans *= A[l] * A[l+1]
        ans %= mod
        l += 2
    else:
        ans *= A[r] * A[r-1]
        ans %= mod
        r -= 2

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
al = list(map(int, input().split()))
al_abs = []
al_p = []
al_m = []
zero_cnt = 0
MOD = 10**9+7


for a in al:
    if a == 0:
        zero_cnt+=1
    else:
        if a > 0: 
            al_abs.append((abs(a),1))
            al_p.append(a)
        else: 
            al_abs.append((abs(a),-1))
            al_m.append(abs(a))

al_abs.sort(reverse=True)
if len(al_abs) < k:
    print(0)
    exit()

if len(al_abs) == k:
    ans = 1
    for a,s in al_abs[:k]:
        ans *= (a*s)
        ans %= MOD
    print(ans)
    exit()

if not al_p:
    al_abs.sort()
    ans = 1
    for a,s in al_abs[:k]:
        ans *= (a*(-1))
        ans %= MOD
    print(ans)
    exit()



val = 1
m_cnt = 0
last_m = 0
last_p = 0
for i in range(k):
    v,sig = al_abs[i]
    val *= v
    val %= MOD
    if sig == -1:
        m_cnt += 1
        last_m = v
    else:
        last_p = v

if m_cnt %2 == 0:
    print(val)
    exit()


# minus -> plus 

## search next p
next_p = 0
for i in range(k,len(al_abs)):
    v,sig = al_abs[i]
    if sig == 1:
        next_p = v
        break

## search next m
next_m = 0
for i in range(k,len(al_abs)):
    v,sig = al_abs[i]
    if sig == -1:
        next_m = v
        break

# print(next_m, next_p)

if next_p == 0 and next_m == 0:
    print(0)
    exit()

# last_m -> next_p
if next_p*last_p >= next_m*last_m or last_p == 0:
# if next_p > 0 and next_m == 0:
    ans = 1
    skip_flag = False
    for i in range(k):
        v,sig = al_abs[i]
        if not skip_flag and v == last_m and sig == -1:
            ans *= next_p
            skip_flag = True
        else:
            ans *= v
        # print(ans)
        ans %= MOD
    print(ans)
    exit()


# last_p -> next_m
# if next_p == 0 and next_m > 0:
elif next_p*last_p <= next_m*last_m:
    ans = 1
    skip_flag = False
    for i in range(k):
        v,sig = al_abs[i]
        if not skip_flag and v == last_p and sig == 1:
            ans *= next_m
            skip_flag = True
        else:
            ans *= v
        ans %= MOD
    print(ans)
    exit()

Submission status: Wrong Answer

Solution code:
import sys

mod = 10 ** 9 + 7
N, K = map(int,input().split())
A = list(map(int,input().split()))
if all([a < 0 for a in A]):
    if K % 2 == 0:
        A.sort()
        ans = 1
        for i in range(K):
            ans *= A[i]
            ans %= mod
        print(ans)
        sys.exit()
    else:
        A.sort(reverse = True)
        ans = 1
        for i in range(K):
            ans *= A[i]
            ans %= mod
        print(ans)
        sys.exit()

A.sort()
l = 0
r = N - 1
ans = 1

for _ in range(K // 2):
    if A[l] * A[l+1] >= A[r] * A[r-1]:
        ans *= A[l] * A[l+1]
        ans %= mod
        l += 2
    else:
        ans *= A[r] * A[r-1]
        ans %= mod
        r -= 2

if K % 2 == 1:
    ans *= A[r]
    ans %= mod

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
al = list(map(int, input().split()))
al_abs = []
al_p = []
al_m = []
zero_cnt = 0
MOD = 10**9+7


for a in al:
    if a == 0:
        zero_cnt+=1
    else:
        if a > 0: 
            al_abs.append((abs(a),1))
            al_p.append(a)
        else: 
            al_abs.append((abs(a),-1))
            al_m.append(abs(a))

al_abs.sort(reverse=True)
if len(al_abs) < k:
    print(0)
    exit()


if not al_p:
    al_abs.sort()
    ans = 1
    for a,s in al_abs[:k]:
        ans *= (a*(-1))
        ans %= MOD
    print(ans)
    exit()


val = 1
m_cnt = 0
last_m = 0
last_p = 0
for i in range(k):
    v,sig = al_abs[i]
    val *= v
    val %= MOD
    if sig == -1:
        m_cnt += 1
        last_m = v
    else:
        last_p = v

if m_cnt %2 == 0:
    print(val)
    exit()


# minus -> plus 

## search next p
next_p = 0
for i in range(k,len(al_abs)):
    v,sig = al_abs[i]
    if sig == 1:
        next_p = v
        break

## search next m
next_m = 0
for i in range(k,len(al_abs)):
    v,sig = al_abs[i]
    if sig == -1:
        next_m = v
        break

# print(next_m, next_p)

if next_p == 0 and next_m == 0:
    print(0)
    exit()

# last_m -> next_p
if next_p*last_p >= next_m*last_m or last_p == 0:
# if next_p > 0 and next_m == 0:
    ans = 1
    skip_flag = False
    for i in range(k):
        v,sig = al_abs[i]
        if not skip_flag and v == last_m and sig == -1:
            ans *= next_p
            skip_flag = True
        else:
            ans *= v
        # print(ans)
        ans %= MOD
    print(ans)
    exit()


# last_p -> next_m
# if next_p == 0 and next_m > 0:
elif next_p*last_p <= next_m*last_m:
    ans = 1
    skip_flag = False
    for i in range(k):
        v,sig = al_abs[i]
        if not skip_flag and v == last_p and sig == 1:
            ans *= next_m
            skip_flag = True
        else:
            ans *= v
        ans %= MOD
    print(ans)
    exit()

Submission status: Wrong Answer

Solution code:
import sys

mod = 10 ** 9 + 7
N, K = map(int,input().split())
A = list(map(int,input().split()))
if all([a < 0 for a in A]):
    if K % 2 == 0:
        A.sort()
        ans = 1
        for i in range(K):
            ans *= A[i]
            ans %= mod
        print(ans)
        sys.exit()
    else:
        A.sort(reverse = True)
        ans = 1
        for i in range(K):
            ans *= A[i]
            ans %= mod
        print(ans)
        sys.exit()

A.sort()
l = 0
r = N - 1
ans = 1

for _ in range(K // 2):
    if A[l] * A[l+1] > A[r] * A[r-1]:
        ans *= A[l] * A[l+1]
        ans %= mod
        l += 2
    else:
        ans *= A[r] * A[r-1]
        ans %= mod
        r -= 2

if K % 2 == 1:
    ans *= A[r]
    ans %= mod

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
al = list(map(int, input().split()))
al_abs = []
al_p = []
al_m = []
zero_cnt = 0
MOD = 10**9+7


for a in al:
    if a == 0:
        zero_cnt+=1
    else:
        if a > 0: 
            al_abs.append((abs(a),1))
            al_p.append(a)
        else: 
            al_abs.append((abs(a),-1))
            al_m.append(abs(a))

al_abs.sort(reverse=True)
if len(al_abs) < k:
    print(0)
    exit()


if not al_p:
    al_abs.sort()
    ans = 1
    for a,s in al_abs[:k]:
        ans *= (a*(-1))
        ans %= MOD
    print(ans)
    exit()


val = 1
m_cnt = 0
last_m = 0
last_p = 0
for i in range(k):
    v,sig = al_abs[i]
    val *= v
    val %= MOD
    if sig == -1:
        m_cnt += 1
        last_m = v
    else:
        last_p = v

if m_cnt %2 == 0:
    print(val)
    exit()


# minus -> plus 

## search next p
next_p = 0
for i in range(k,len(al_abs)):
    v,sig = al_abs[i]
    if sig == 1:
        next_p = v
        break

## search next m
next_m = 0
for i in range(k,len(al_abs)):
    v,sig = al_abs[i]
    if sig == -1:
        next_m = v
        break

# print(next_m, next_p)

if next_p == 0 and next_m == 0:
    print(0)
    exit()

# last_m -> next_p
if next_p*last_p >= next_m*last_m or last_p == 0:
# if next_p > 0 and next_m == 0:
    ans = 1
    skip_flag = False
    for i in range(k):
        v,sig = al_abs[i]
        if not skip_flag and v == last_m and sig == -1:
            val *= next_p
            skip_flag = True
        else:
            val *= v
        val %= MOD
    print(ans)
    exit()


# last_p -> next_m
# if next_p == 0 and next_m > 0:
elif next_p*last_p <= next_m*last_m:
    ans = 1
    skip_flag = False
    for i in range(k):
        v,sig = al_abs[i]
        if not skip_flag and v == last_p and sig == 1:
            val *= next_m
            skip_flag = True
        else:
            val *= v
        val %= MOD
    print(ans)
    exit()

Submission status: Wrong Answer

Solution code:
import sys

mod = 10 ** 9 + 7
N, K = map(int,input().split())
A = list(map(int,input().split()))
if all([a < 0 for a in A]):
    if K % 2 == 0:
        A.sort()
        ans = 1
        for i in range(K):
            ans *= A[i]
            ans %= mod
        print(ans)
        sys.exit()
    else:
        A.sort(reverse = True)
        ans = 1
        for i in range(K):
            ans *= A[i]
            ans %= mod
        print(ans)
        sys.exit()

A.sort()
l = 0
r = N - 1
ans = 1

for _ in range(K // 2):
    if A[l] * A[l+1] > A[r] * A[r-1]:
        ans *= A[l] * A[l+1]
        ans %= mod
        l += 2
    else:
        ans *= A[r] * A[r-1]
        ans %= mod
        r -= 2

if K % 2 == 1:
    ans *= A[l]
    ans %= mod

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
al = list(map(int, input().split()))
al_abs = []
al_p = []
al_m = []
zero_cnt = 0
MOD = 10**9+7


for a in al:
    if a == 0:
        zero_cnt+=1
    else:
        if a > 0: 
            al_abs.append((abs(a),1))
            al_p.append(a)
        else: 
            al_abs.append((abs(a),-1))
            al_m.append(abs(a))

al_abs.sort(reverse=True)
if len(al_abs) < k:
    print(0)
    exit()


if not al_p:
    al_abs.sort()
    ans = 1
    for a,s in al_abs[:k]:
        ans *= (a*(-1))
        ans %= MOD
    print(ans)
    exit()


val = 1
m_cnt = 0
last_m = 0
last_p = 0
for i in range(k):
    v,sig = al_abs[i]
    val *= v
    val %= MOD
    if sig == -1:
        m_cnt += 1
        last_m = v
    else:
        last_p = v

if m_cnt %2 == 0:
    print(val)
    exit()


# minus -> plus 

## search next p
next_p = 0
for i in range(k,len(al_abs)):
    v,sig = al_abs[i]
    if sig == 1:
        next_p = v
        break

## search next m
next_m = 0
for i in range(k,len(al_abs)):
    v,sig = al_abs[i]
    if sig == -1:
        next_m = v
        break

print(next_m, next_p)

if next_p == 0 and next_m == 0:
    print(0)
    exit()

# last_m -> next_p
if next_p*last_p >= next_m*last_m or last_p == 0:
# if next_p > 0 and next_m == 0:
    ans = 1
    skip_flag = False
    for i in range(k):
        v,sig = al_abs[i]
        if not skip_flag and v == last_m and sig == -1:
            val *= next_p
            skip_flag = True
        else:
            val *= v
        val %= MOD
    print(ans)
    exit()


# last_p -> next_m
# if next_p == 0 and next_m > 0:
elif next_p*last_p <= next_m*last_m:
    ans = 1
    skip_flag = False
    for i in range(k):
        v,sig = al_abs[i]
        if not skip_flag and v == last_p and sig == 1:
            val *= next_m
            skip_flag = True
        else:
            val *= v
        val %= MOD
    print(ans)
    exit()

Submission status: Wrong Answer

Solution code:
import sys

mod = 10 ** 9 + 7
N, K = map(int,input().split())
A = list(map(int,input().split()))
if all([a < 0 for a in A]):
    if K % 2 == 0:
        A.sort(reverse = True)
        ans = 1
        for i in range(K):
            ans *= A[i]
            ans %= mod
        print(ans)
        sys.exit()
    else:
        A.sort()
        ans = 1
        for i in range(K):
            ans *= A[i]
            ans %= mod
        print(ans)
        sys.exit()

A.sort()
l = 0
r = N - 1
ans = 1

for _ in range(K // 2):
    if A[l] * A[l+1] > A[r] * A[r-1]:
        ans *= A[l] * A[l+1]
        ans %= mod
        l += 2
    else:
        ans *= A[r] * A[r-1]
        ans %= mod
        r -= 2

if K % 2 == 1:
    ans *= A[l]

print(ans)

Submission status: Wrong Answer

Solution code:
MOD = 10 ** 9 + 7

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

if alst[0] >= 0:
    ans = 1
    alst = alst[::-1]
    for i in range(k):
        ans *= alst[i]
        ans %= MOD
    print(ans)
    exit()
if alst[-1] <= 0:
    ans = 1
    if k % 2 == 1:
        alst = alst[::-1]
    for i in range(k):
        ans *= alst[i]
        ans %= MOD
    print(ans)
    exit()

plus = [0]
minus = [0]
for a in alst:
    if a > 0:
        plus.append(a)
    elif a < 0:
        minus.append(a)
plus.sort(reverse = True)
minus.sort()

p_pos = 0
m_pos = 0
ans = 1

for _ in range(k):
    pp = plus[p_pos]
    mm = minus[m_pos] * -1
    if pp == mm == 0:
        print(0)
        exit()
    if pp >= mm:
        p_pos += 1
        ans *= pp
    else:
        m_pos += 1
        ans *= mm
    ans %= MOD

if m_pos % 2 == 0:
    print(ans)
    exit()
else:
    mm = minus[m_pos] * minus[m_pos - 1]
    pp = plus[p_pos] * plus[p_pos - 1]

    if pp >= mm:
        ans *= plus[p_pos] * pow(minus[m_pos - 1], MOD - 2, MOD)
        ans %= MOD
    else:
        ans *= minus[m_pos] * pow(plus[p_pos - 1], MOD - 2, MOD)
        ans %= MOD
    print(ans)

Submission status: Wrong Answer

Solution code:
MOD = 10 ** 9 + 7

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

if alst[0] >= 0:
    ans = 1
    alst = alst[::-1]
    for i in range(k):
        ans *= alst[i]
        ans %= MOD
    print(ans)
    exit()
if alst[-1] <= 0:
    ans = 1
    if k % 2 == 1:
        alst = alst[::-1]
    for i in range(k):
        ans *= alst[i]
        ans %= MOD
    print(ans)
    exit()

plus = [0]
minus = [0]
for a in alst:
    if a > 0:
        plus.append(a)
    elif a < 0:
        minus.append(a)
plus.sort(reverse = True)
minus.sort()

p_pos = 0
m_pos = 0
ans = 1
for _ in range(k):
    pp = plus[p_pos]
    mm = minus[m_pos] * -1
    if pp == mm == 0:
        print(0)
        exit()
    if pp >= mm:
        p_pos += 1
        ans *= pp
    else:
        m_pos += 1
        ans *= mm
    ans %= MOD

if m_pos % 2 == 0:
    print(ans)
    exit()
else:
    mm = minus[m_pos] * minus[m_pos - 1]
    pp = plus[p_pos] * plus[p_pos - 1]
    if pp >= mm:
        ans *= plus[p_pos] + pow(minus[m_pos - 1], MOD - 2, MOD)
        ans %= MOD
    else:
        ans *= minus[m_pos] + pow(plus[p_pos - 1], MOD - 2, MOD)
        ans %= MOD
    print(ans)

Submission status: Wrong Answer

Solution code:
#E_Multiplication4
N,K = map(int,input().split())
A = list(map(int,input().split()))
#ソート
arr = []
j = 0
i = 0
seki = 1
arr = sorted(A,reverse = True)
while (i + j) < K:
    if arr[(N - j -1)] > 0 :
        seki = seki * arr[i]
        i += 1
    else:
        if arr[i] < 0 :
            if K % 2 == 0:
                seki = seki *  arr[(N - j -1)]
                j += 1
            else:
                seki = seki * arr[i]
                i += 1
        else:
            if abs(arr[i]) > abs( arr[(N - j -1)]) :
                seki = seki * arr[i]
                i += 1
            else:
                if (K-i-j == 1 and j % 2 == 0 ):
                    seki = seki * arr[i]
                    i += 1
                else:
                    seki = seki * arr[(N - j -1)]
                    j += 1
out = seki % 1000000007
        
print(out)

Submission status: Wrong Answer

Solution code:
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq
mod = 10**9+7
n,k = ma()
A = lma()

Am = [a for a in A if a<0];Am.sort(reverse=True)
zeros = A.count(0)
ans = 1
if k%2==1 and len(Am) +zeros==n:
    if zeros>0:
        ans = 0
    else:
        for i in range(k):
            ans= (ans * Am[i]) %mod
    print(ans%mod)
    exit()

Ap = [abs(a) for a in A if abs(a) != 0]
if len(Ap) < k:
    print(0)
    exit()

Ap.sort(reverse=True)
for i in range(k):
    ans= (ans * Ap[i]) %mod
print(ans%mod)

Submission status: Wrong Answer

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

s = sorted(a,reverse=True)
mod = 10**9 + 7
ans = 1

if k % 2 == 1 and s[0] < 0:
        for i in range(k):
            ans = ans * s[i] % mod
        print(ans)
        exit()

l = 0
r = n - 1
if k % 2 == 1:
  ans = ans * s[r]
  r -= 1

for _ in range(k // 2):
  ml = s[l] * s[l + 1]
  mr = s[r] * s[r - 1]
  if ml > mr:
    ans = ans * ml % mod
    l += 2
  else:
    ans = ans * mr % mod
    r -= 2

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations 
import numpy as np

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

c = list(combinations(a,k))
max = -10**9
for i in c:
  p = np.prod(i)
  if max < p:
    max = p

r = max % 10**9 + 7
print(r)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a1 = list(map(int, input().split()))
a1.sort(reverse=True)
a2 = a1[:]
mul1 = 1
mul2 = 1
while k > 0:
  if k >= 2:
    k -= 2
    if (a1[0]*a1[1]) >= (a1[-1]*a1[-2]):
      mul1 *= a1[0]*a1[1]
      a1.pop(0)
      a1.pop(0)
    else:
      mul1 *= a1[-1]*a1[-2]
      a1.pop(-1)
      a1.pop(-1)
    if (a2[0]*a2[1]) >= (a2[-1]*a2[-2]):
      mul2 *= a2[-1]*a2[-2]
      a2.pop(-1)
      a2.pop(-1)
    else:
      mul2 *= a2[0]*a2[1]
      a2.pop(0)
      a2.pop(0)
  else:
    k -= 1
    if (a1[0]) >= (a1[-1]):
      mul1 *= a1[0]
      a1.pop(0)
    else:
      mul1 *= a1[-1]
      a1.pop(-1)
    if (a2[0]) >= (a2[-1]):
      mul2 *= a2[0]
      a2.pop(0)
    else:
      mul1 *= a2[-1]
      a1.pop(-1)
  mul1 = mul1%(10**9+7)
  mul2 = mul2%(10**9+7)
print(max(mul1, mul2))

Submission status: Wrong Answer

Solution code:
print(1)