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 = 10**9 + 7
def main():
  N, K = map(int , input().split())
  A = list(map(int, input().split()))
  
  if max(A) < 0 and K % 2 == 1:
    A.sort(reverse = True)
    ans = 1
    for i in range(K):
      ans = ans*A[i]%MOD
    print(ans)
  elif K == N:
    ans = 1
    for i in A:
      ans = ans*i%MOD
    print(ans)
  else:
    A_p = []
    A_m = []
    for i in A:
      if i >= 0:A_p.append(i)
      else:A_m.append(i)
    A_p.sort(reverse =  True)
    A_m.sort()
    num = 0
    ans = 1
    posi_p = 0
    posi_m = 0
    sign = 0
    while (num < K):
      if posi_m == len(A_m) and posi_p < len(A_p):
        ans = ans*A_p[posi_p]%MOD
        posi_p += 1
        num += 1
      elif posi_p == len(A_p) and posi_m < len(A_m):
        ans = ans*A_m[posi_m]%MOD
        sign += 1
        posi_m += 1
        num += 1        
      elif  A_p[posi_p] >= abs(A_m[posi_m]):
        ans = ans*A_p[posi_p]%MOD
        posi_p += 1
        num += 1        
      else:
        ans = ans*A_m[posi_m]%MOD
        sign += 1
        posi_m += 1
        num += 1
      if num == K and sign % 2 == 1:
        if posi_m == len(A_m):
          ans = ans*pow(A_m[posi_m-1], MOD-2, MOD) % MOD
          ans = ans*A_p[posi_p] % MOD
        elif posi_p == len(A_p):
          ans = ans*pow(A_p[posi_p-1],  MOD-2, MOD)
          ans =  ans*A_m[posi_m] % MOD
        elif A_p[posi_p]*A_p[posi_p-1] >= A_m[posi_m]*A[posi_m-1]:
          ans = ans*pow(A_m[posi_m-1], MOD-2, MOD)%MOD
          ans = ans*A_p[posi_p] % MOD
        else:
          ans = ans*pow(A_p[posi_p-1], MOD-2, MOD)%MOD
          ans = ans*A_m[posi_m]%MOD
    print(ans%MOD)
          
    
if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
import numpy as np

# A = np.array(sorted(A, reverse=True, key=lambda x: abs(x)))
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    mod = int(1e9+7)
    p = []
    m = []
    for a in A:
        if a >= 0:
            p.append(a)
        else:
            m.append(a)
    p.sort(reverse=True)
    m.sort(reverse=False)
    # print(p, m)
    ans = 1
    if sum(np.sign(A)) == -N and K%2 == 1:
        A = sorted(A, reverse=True)
        for i in range(K):
            ans = (ans*A[i])%mod
        print(ans)
        return

    m_pair = []
    p_pair = []
    if K%2 == 1:
        ans *= p.pop(0)%mod
    for i in range(0, len(m), 2):
        if i+1 < len(m):
            m_pair.append(m[i]*m[i+1])
        else:
            m_pair.append(m[i])
    for i in range(0, len(p), 2):
        if i+1 < len(p):
            p_pair.append(p[i]*p[i+1])
        else:
            p_pair.append(p[i])
    pairs = m_pair[:]
    pairs.extend(p_pair)
    pairs.sort(reverse=True)
    for i in range(K//2):
        ans = (ans*pairs[i])%mod
    # print(m_pair, p_pair, pairs)
    # last_p = 0
    # last_m = 0
    # eliminate_m = 0
    # eliminate_p = 0
    # add_m = 0
    # add_p = 0
    # ans = 1
    # for i in range(N):
    #     if i < K:
    #         # ans = (ans * A[i])%mod
    #         if A[i] > 0:
    #             ans = (ans*last_p)%mod
    #             last_p = A[i]
    #         else:
    #             ans = (ans*last_m)%mod
    #             last_m = A[i]
    #     else:
    #         if A[i] > 0 and eliminate_m == 0:
    #             eliminate_m = last_p * A[i]
    #             add_p = A[i]
    #         elif A[i] < 0 and eliminate_p == 0:
    #             eliminate_p = last_m * A[i]
    #             add_m = A[i]
    #     if i == K-1:
    #         if ans < 0 or K == N:
    #             ans = (ans*last_p)%mod
    #             ans = (ans*last_m)%mod
    #             break
    # if eliminate_p > 0 or eliminate_m > 0:
    #     if eliminate_m > eliminate_p:
    #         ans = (ans*eliminate_m%mod)%mod
    #         # ans = (ans*pow(last_m, mod-2))%mod
    #         # ans = (ans*add_p)%mod
    #     else:
    #         # ans = (ans*pow(last_p, mod-2))%mod
    #         # ans = (ans*add_m)%mod
    #         ans = (ans*eliminate_p%mod)%mod

    print(ans)

main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
mod = 10 ** 9 + 7
from collections import deque

a = list(map(int, input().split()))
a.sort()
minus, zero, plus = 0, 0, 0
for i in range(n):
    if a[i] < 0:
        minus += 1
    elif a[i] == 0:
        zero += 1
    else:
        plus += 1

que = deque(a)

plus_flg = False
tmp = plus - k
if tmp >= 0:
    plus_flg = True
elif (-tmp <= minus and -tmp%2 == 0) or (-tmp < minus and plus >= 1):
    plus_flg = True

# plus可能
if plus_flg:
    ans = 1
    cnt = 0

    while cnt < k:
        if k - cnt >= 2:
            cnt += 2
            if que[0] * que[1] >= que[-1] * que[-2]:
                ans *= que.popleft()
                ans *= que.popleft()
                ans %= mod
            else:
                ans *= que.pop()
                ans *= que.pop()
                ans %= mod
        else:
            ans *= que.pop()
            ans %= mod
            cnt += 1

    print(ans)



# zero
elif zero > 0:
    print(0)

# minus
else:
    a = [abs(a[i]) for i in range(n)]
    a.sort()
    ans = 1
    for i in range(k):
        ans *= a[i]
        ans %= mod
    ans = -ans % mod
    print(ans)

Submission status: Wrong Answer

Solution code:
#正の場合、0の場合、負の場合で分ける
#負の数のみの場合は調整しようがない
#偶数の時は絶対値の大きい順
#奇数の時は絶対値の小さい順
#正の数のみの場合は大きい順
#大きい順

#他は調整できるので、正で大きくする方向で考える(正→0以上)
#負の数は偶数個ないと正にならないので二つ一組で先にかけて考える
#正の数も同様に先にかけて、その中で大きいものを選べば良い

#負の数が奇数個、正の数が奇数個の時(負の数が余る、正の数が余る)
#Kが偶数の時はいい感じに選んでく(N=kの時は仕方ないので全部)
#Kが奇数の時は正の中で最大のものを選ぶ
#負の数が偶数個、正の数が奇数個の時(正の数が余る)
#Kが偶数の時はいい感じに選んでく
#奇数の時は残りの正の中から選ぶ
#負の数が奇数個、正の数が偶数個の時(負の数が余る)
#N=Kの時は全部選ぶ、それ以外の場合は余った負の数はいらない
#Kが奇数の時は残りの正の中で最大のものを選べば良い
#負の数が偶数個、正の数が偶数個の時
#Kが奇数の時は残りの正の中で最大のものを選べば良い

#N=Kの時は別で分けて考えるとすると、負の数及び正の数によらず
#Kが偶数の時は大きいものからいい感じに選んでく
#Kが奇数の時は最後の残り一個は正の残りの中で最大のものを選ぶ
#と簡単に言い換えることができる

#x:配列
def multarray(x):
    mod=10**9+7
    ret=1
    for i in x:
        ret*=i
        ret%=mod
    return ret

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

if n==k:
    print(multarray(a))
    exit()

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 len(am)==0:
    print(multarray(ap[:k]))
    exit()
if len(ap)==0:
    if k%2==0:
        print(multarray(am[:k]))
    else:
        print(multarray(am[::-1][:k]))
    exit()

apm2=[]
for i in range(len(am)//2):
    apm2.append((am[2*i]*am[2*i+1],0))
for i in range(len(ap)//2):
    apm2.append((ap[2*i]*ap[2*i+1],1))
apm2.sort(reverse=True)

#+の次にどこを見るのかをチェックしておく
#まだかけない
p=0
ans=[]
for i in range(k//2):
    p+=(2*apm2[i][1])
    ans.append(apm2[i][0])

#ここで不正アクセスの可能性を避ける
#プラスが余ってない場合(一個減らして次のマイナス使う)
if k%2==1:
    if p==len(ap):
        #一個減らす
        for i in range(k//2):
            if ans[i]%ap[p-1]==0:
                ans[i]//=ap[p-1]
                break
        ans.append(apm2[k//2][0])
    else:
        ans.append(ap[p])
print(multarray(ans))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7

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

ans = 1
i, j = 0, 0
if n == k:
    if len(plus) % 2 == 1:
        ans *= plus[0]
        i += 1
    if len(minus) % 2 == 1:
        ans *= minus[0]
        j += 1
if n != k and k % 2 == 1:
    if len(plus) != 0:
        ans = plus[0]
        i += 1
    else:
        minus.sort()
        ans = -minus[0]
        j += 1
while i + j != k:
    x_p = plus[i] * plus[i + 1] if i < len(plus) - 1 else 0
    x_m = minus[j] * minus[j + 1] if j < len(minus) - 1 else 0
    if x_p >= x_m and i != len(plus):
        ans = (ans * x_p) % mod
        i += 2
    else:
        ans = (ans * x_m) % mod
        j += 2
print(ans)

Submission status: Wrong Answer

Solution code:
#正の場合、0の場合、負の場合で分ける
#負の数のみの場合は調整しようがない
#偶数の時は絶対値の大きい順
#奇数の時は絶対値の小さい順
#正の数のみの場合は大きい順
#大きい順

#他は調整できるので、正で大きくする方向で考える(正→0以上)
#負の数は偶数個ないと正にならないので二つ一組で先にかけて考える
#正の数も同様に先にかけて、その中で大きいものを選べば良い

#負の数が奇数個、正の数が奇数個の時(負の数が余る、正の数が余る)
#Kが偶数の時はいい感じに選んでく(N=kの時は仕方ないので全部)
#Kが奇数の時は正の中で最大のものを選ぶ
#負の数が偶数個、正の数が奇数個の時(正の数が余る)
#Kが偶数の時はいい感じに選んでく
#奇数の時は残りの正の中から選ぶ
#負の数が奇数個、正の数が偶数個の時(負の数が余る)
#N=Kの時は全部選ぶ、それ以外の場合は余った負の数はいらない
#Kが奇数の時は残りの正の中で最大のものを選べば良い
#負の数が偶数個、正の数が偶数個の時
#Kが奇数の時は残りの正の中で最大のものを選べば良い

#N=Kの時は別で分けて考えるとすると、負の数及び正の数によらず
#Kが偶数の時は大きいものからいい感じに選んでく
#Kが奇数の時は最後の残り一個は正の残りの中で最大のものを選ぶ
#と簡単に言い換えることができる

#x:配列
def multarray(x):
    mod=10**9+7
    ret=1
    for i in x:
        ret*=i
        ret%=mod
    return ret

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

if n==k:
    print(multarray(a))
    exit()

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 len(am)==0:
    print(multarray(ap[:k]))
    exit()
if len(ap)==0:
    if k%2==0:
        print(multarray(am[:k]))
    else:
        print(multarray(am[::-1][:k]))
    exit()

apm2=[]
for i in range(len(am)//2):
    apm2.append(am[2*i]*am[2*i+1])
if len(ap)%2==1 and k%2==1:
    ans=ap[0]
    for i in range(len(ap)//2):
        apm2.append(ap[2*i+1]*ap[2*i+2])
else:
    ans=1
    for i in range(len(ap)//2):
        apm2.append(ap[2*i]*ap[2*i+1])
apm2.sort(reverse=True)

#ここから下では組のみを考えれば良い
ans*=multarray(apm2[:(k//2)])
ans%=(10**9+7)
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7

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

ans = 1
i, j = 0, 0
if n == k:
    if len(plus) % 2 == 1:
        ans *= plus[0]
        i += 1
    if len(minus) % 2 == 1:
        ans *= minus[0]
        j += 1
if n != k and k % 2 == 1:
    if len(plus) != 0:
        ans = plus[0]
        i += 1
    else:
        ans = -minus[0]
        j += 1
while i + j != k:
    x_p = plus[i] * plus[i + 1] if i < len(plus) - 1 else 0
    x_m = minus[j] * minus[j + 1] if j < len(minus) - 1 else 0
    if x_p >= x_m and i != len(plus):
        ans = (ans * x_p) % mod
        i += 2
    else:
        ans = (ans * x_m) % mod
        j += 2
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
mod = 10 ** 9 + 7
from collections import deque

a = list(map(int, input().split()))
a.sort()
minus, zero, plus = 0, 0, 0
for i in range(n):
    if a[i] < 0:
        minus += 1
    elif a[i] == 0:
        zero += 1
    else:
        plus += 1

que = deque(a)

plus_flg = False
tmp = plus - k
if tmp >= 0:
    plus_flg = True
elif -tmp <= minus and (-tmp%2 == 0 or tmp > 0):
    plus_flg = True

# plus可能
if plus_flg:
    ans = 1
    cnt = 0

    while cnt < k:
        if k - cnt >= 2:
            cnt += 2
            if que[0] * que[1] >= que[-1] * que[-2]:
                ans *= que.popleft()
                ans *= que.popleft()
                ans %= mod
            else:
                ans *= que.pop()
                ans *= que.pop()
                ans %= mod
        else:
            ans *= que.pop()
            ans %= mod
            cnt += 1

    print(ans)



# zero
elif zero > 0:
    print(0)

# minus
else:
    a = [abs(a[i]) for i in range(n)]
    a.sort()
    ans = 1
    for i in range(k):
        ans *= a[i]
        ans %= mod
    ans = -ans % mod
    print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
mod = 10 ** 9 + 7

from collections import deque

a = list(map(int, input().split()))
a.sort()
minus, zero, plus = 0, 0, 0
for i in range(n):
    if a[i] < 0:
        minus += 1
    elif a[i] == 0:
        zero += 1
    else:
        plus += 1

que = deque(a)

plus_flg = False
tmp = plus - k
if tmp >= 0:
    plus_flg = True
elif -tmp <= minus and -tmp%2 == 0:
    plus_flg = True

# plus可能
if plus_flg:
    ans = 1
    cnt = 0

    while cnt < k:
        if k - cnt >= 2:
            cnt += 2
            if que[0] * que[1] >= que[-1] * que[-2]:
                ans *= que.popleft()
                ans *= que.popleft()
                ans %= mod
            else:
                ans *= que.pop()
                ans *= que.pop()
                ans %= mod
        else:
            ans *= que.pop()
            ans %= mod
            cnt += 1

    print(ans)



# zero
elif zero > 0:
    print(0)

# minus
else:
    a = [abs(a[i]) for i in range(n)]
    a.sort()
    ans = 1
    for i in range(k):
        ans *= a[i]
        ans %= mod
    ans = -ans % mod
    print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7

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

ans = 1
i, j = 0, 0
if n == k:
    if len(plus) % 2 == 1:
        ans *= plus[0]
        i += 1
    if len(minus) % 2 == 1:
        ans *= minus[0]
        j += 1
if n != k and k % 2 == 1:
    if len(plus) != 0:
        ans = plus[0]
        i += 1
    else:
        ans = -minus[0]
        j += 1
while i + j != k:
    x_p = plus[i] * plus[i + 1] if i < len(plus) - 1 else 0
    x_m = minus[j] * minus[j + 1] if j < len(minus) - 1 else 0
    if x_p >= x_m:
        ans = (ans * x_p) % mod
        i += 2
    else:
        ans = (ans * x_m) % mod
        j += 2
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7

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

ans = 1
i, j = 0, 0
if n == k:
    if len(plus) % 2 == 1:
        ans *= plus[0]
        i += 1
    if len(minus) % 2 == 1:
        ans *= minus[0]
        j += 1
if k % 2 == 1:
    if len(plus) != 0:
        ans = plus[0]
        i += 1
    else:
        ans = -minus[0]
        j += 1
while i + j != k:
    x_p = plus[i] * plus[i + 1] if i < len(plus) - 1 else 0
    x_m = minus[j] * minus[j + 1] if j < len(minus) - 1 else 0
    if x_p >= x_m:
        ans = (ans * x_p) % mod
        i += 2
    else:
        ans = (ans * x_m) % mod
        j += 2
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
mod = 10 ** 9 + 7

from collections import deque

a = list(map(int, input().split()))
a.sort()
minus, zero, plus = 0, 0, 0
for i in range(n):
    if a[i] < 0:
        minus += 1
    elif a[i] == 0:
        zero += 1
    else:
        plus += 1

que = deque(a)

plus_flg = False
tmp = plus - k
if tmp >= 0:
    plus_flg = True
elif tmp*-2 <= minus:
    plus_flg = True

# plus可能
if plus_flg:
    ans = 1
    cnt = 0

    while cnt < k:
        if k - cnt >= 2:
            cnt += 2
            if que[0] * que[1] >= que[-1] * que[-2]:
                ans *= que.popleft()
                ans *= que.popleft()
                ans %= mod
            else:
                ans *= que.pop()
                ans *= que.pop()
                ans %= mod
        else:
            ans *= que.pop()
            ans %= mod
            cnt += 1

    print(ans)



# zero
elif zero > 0:
    print(0)

# minus
else:
    a = [abs(a[i]) for i in range(n)]
    a.sort()
    ans = 1
    for i in range(k):
        ans *= a[i]
        ans %= mod
    ans = -ans % mod
    print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
mod = 10 ** 9 + 7

from collections import deque

a = list(map(int, input().split()))
a.sort()
minus, zero, plus = 0, 0, 0
for i in range(n):
    if a[i] < 0:
        minus += 1
    elif a[i] == 0:
        zero += 1
    else:
        plus += 1

que = deque(a)

plus_flg = False
tmp = plus - k
if tmp >= 0:
    plus_flg = True
elif tmp*-2 >= minus  :
    plus_flg = True

# plus可能
if plus_flg:
    ans = 1
    cnt = 0

    while cnt < k:
        if k - cnt >= 2:
            cnt += 2
            if que[0] * que[1] >= que[-1] * que[-2]:
                ans *= que.popleft()
                ans *= que.popleft()
                ans %= mod
            else:
                ans *= que.pop()
                ans *= que.pop()
                ans %= mod
        else:
            ans *= que.pop()
            ans %= mod
            cnt += 1

    print(ans)



# zero
elif zero > 0:
    print(0)

# minus
else:
    a = [abs(a[i]) for i in range(n)]
    a.sort()
    ans = 1
    for i in range(k):
        ans *= a[i]
        ans %= mod
    ans = -ans % mod
    print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
mod = 10 ** 9 + 7

from collections import deque
a = list(map(int, input().split()))
a.sort()
minus, zero, plus = 0, 0, 0
for i in range(n):
    if a[i] < 0:
        minus += 1
    elif a[i] == 0:
        zero += 1
    else:
        plus += 1

que = deque(a)

plus_flg = False
if plus >= k:
    plus_flg = True
for i in range(plus+1):
    if (k - i) % 2 == 0 and (k - i) // 2 <= minus // 2:
        plus_flg = True
        break
#plus可能
if plus_flg:
    ans = 1
    cnt = 0 
    
    while cnt < k:
        if k - cnt >= 2:
            cnt += 2
            if que[0] * que[1] >= que[-1] * que[-2]:
                ans *= que.popleft()
                ans *= que.popleft()
                ans %= mod
            else:
                ans *= que.pop()
                ans *= que.pop()
                ans %= mod
        else:
            ans *= que.pop()
            ans %= mod
            cnt += 1

    print(ans)



#zero
elif zero > 0:
    print(0)

#minus
else:
    a = [abs(a[i]) for i in range(n)]
    a.sort()
    ans = 1
    for i in range(k):
        ans *= a[i]
        ans %= mod
    ans = -ans % mod
    print(ans)

Submission status: Wrong Answer

Solution code:
#以下は上位者のコピー
n, k = map(int, input().split())
mod = 10 ** 9 + 7

from collections import deque

a = list(map(int, input().split()))
a.sort()
minus, zero, plus = 0, 0, 0
for i in range(n):
    if a[i] < 0:
        minus += 1
    elif a[i] == 0:
        zero += 1
    else:
        plus += 1

que = deque(a)

plus_flg = False
tmp = plus - k
if tmp >= 0:
    plus_flg = True
elif tmp*-2 > minus  :
    plus_flg = True

# plus可能
if plus_flg:
    ans = 1
    cnt = 0

    while cnt < k:
        if k - cnt >= 2:
            cnt += 2
            if que[0] * que[1] >= que[-1] * que[-2]:
                ans *= que.popleft()
                ans *= que.popleft()
                ans %= mod
            else:
                ans *= que.pop()
                ans *= que.pop()
                ans %= mod
        else:
            ans *= que.pop()
            ans %= mod
            cnt += 1

    print(ans)



# zero
elif zero > 0:
    print(0)

# minus
else:
    a = [abs(a[i]) for i in range(n)]
    a.sort()
    ans = 1
    for i in range(k):
        ans *= a[i]
        ans %= mod
    ans = -ans % mod
    print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7

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

ans = 1
i, j = 0, 0
if k % 2 == 1:
    if (n != k and len(plus) != 0) or (n == k and len(plus) % 2 == 1):
        ans = plus[0]
        i += 1
    else:
        if n != k:
            minus.sort()
        ans = -minus[0]
        j += 1
while i + j != k:
    x_p = plus[i] * plus[i + 1] if i < len(plus) - 1 else 0
    x_m = minus[j] * minus[j + 1] if j < len(minus) - 1 else 0
    if x_p >= x_m:
        ans = (ans * x_p) % mod
        i += 2
    else:
        ans = (ans * x_m) % mod
        j += 2
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7

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

ans = 1
i, j = 0, 0
if k % 2 == 1:
    if (n != k and len(plus) != 0) or (n == k and len(plus) % 2 == 1):
        ans = plus[0]
        i += 1
    else:
        minus.sort()
        ans = -minus[0]
        j += 1
while i + j != k:
    x_p = plus[i] * plus[i + 1] if i < len(plus) - 1 else 0
    x_m = minus[j] * minus[j + 1] if j < len(minus) - 1 else 0
    if x_p >= x_m:
        ans = (ans * x_p) % mod
        i += 2
    else:
        ans = (ans * x_m) % mod
        j += 2
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
N ,K = map(int, input().split())
A = list(map(int, input().split()))
A = sorted(A)
mod = 10 ** 9 + 7
# ans = 0
if A.count(0) > (N - K):
  print(0)
  quit()
  
# ans = minus
plus = bisect.bisect_right(A, 0)
minus = bisect.bisect_left(A, 0) 
if N == K:
  ans = 1
  for i in range(N):
    ans *= A[i]
    ans %= mod
  print(ans % mod)
  quit()
  
if (minus == N) and (K % 2 == 1):
  ans = 1
  for i in range(1, K + 1):
    ans *= A[-i]
    ans %= mod
  print(ans % mod)
  quit()
  
ans = 1

count = 0
i = 0
j = -1
while count < K:
  if K - count >= 2:
    fo = A[i] * A[i + 1]
    re = A[j] * A[j - 1]
    #print(fo, re)
    if fo >= re:
      ans *= (fo % mod) 
      ans %= mod
      i += 2
    elif (A[j - 1] <= 0) or (A[j - 2] <= 0):
      if (K % 2 == 0):
        ans *= (re % mod)
        ans %= mod
        j -= 2
      else:  
        ans *= (fo % mod) 
        ans %= mod
        i += 2      
    else:
      ans *= (re % mod)
      ans %= mod
      j -= 2
    count += 2  
  else:
    count += 1
    ans *= A[j]
      
  #print(ans)
print(ans % mod)

Submission status: Wrong Answer

Solution code:
import bisect
N ,K = map(int, input().split())
A = list(map(int, input().split()))
A = sorted(A)
mod = 10 ** 9 + 7
# ans = 0
if A.count(0) > (N - K):
  print(0)
  quit()
  
# ans = minus
plus = bisect.bisect_right(A, 0)
minus = bisect.bisect_left(A, 0) 
if N == K:
  ans = 1
  for i in range(N):
    ans *= A[i]
    ans %= mod
  print(ans % mod)
  quit()
  
if (minus == N) and (K % 2 == 1):
  ans = 1
  for i in range(1, K + 1):
    ans *= A[-i]
    ans %= mod
  print(ans % mod)
  quit()
  
ans = 1

count = 0
i = 0
j = -1
while count < K:
  if K - count >= 2:
    fo = A[i] * A[i + 1]
    re = A[j] * A[j - 1]
    #print(fo, re)
    if fo >= re:
      ans *= (fo % mod) 
      ans %= mod
      i += 2
    elif (A[j - 1] <= 0) or (A[j - 2] < 0):
      if (K % 2 == 0):
        ans *= (re % mod)
        ans %= mod
        j -= 2
      else:  
        ans *= (fo % mod) 
        ans %= mod
        i += 2      
    else:
      ans *= (re % mod)
      ans %= mod
      j -= 2
    count += 2  
  else:
    count += 1
    ans *= A[j]
      
  #print(ans)
print(ans % mod)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7

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

ans = 1
i, j = 0, 0
if k % 2 == 1:
    if len(plus) != 0 or (n == k and len(plus) % 2 == 1):
        ans = plus[0]
        i += 1
    else:
        minus.sort()
        ans = -minus[0]
        j += 1
while i + j != k:
    x_p = plus[i] * plus[i + 1] if i < len(plus) - 1 else 0
    x_m = minus[j] * minus[j + 1] if j < len(minus) - 1 else 0
    if x_p >= x_m:
        ans = (ans * x_p) % mod
        i += 2
    else:
        ans = (ans * x_m) % mod
        j += 2
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7

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

ans = 1
i, j = 0, 0
if k % 2 == 1:
    if len(plus) != 0 or n == k and len(plus) % 2 == 1:
        ans = plus[0]
        i += 1
    else:
        minus.sort()
        ans = -minus[0]
        j += 1
while i + j != k:
    x_p = plus[i] * plus[i + 1] if i < len(plus) - 1 else 0
    x_m = minus[j] * minus[j + 1] if j < len(minus) - 1 else 0
    if x_p >= x_m:
        ans = (ans * x_p) % mod
        i += 2
    else:
        ans = (ans * x_m) % mod
        j += 2
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
N ,K = map(int, input().split())
A = list(map(int, input().split()))
A = sorted(A)
mod = 10 ** 9 + 7
# ans = 0
if A.count(0) > (N - K):
  print(0)
  quit()
  
# ans = minus
plus = bisect.bisect_right(A, 0)
minus = bisect.bisect_left(A, 0) 
if N == K:
  ans = 1
  for i in range(N):
    ans *= A[i]
    ans %= mod
  print(ans % mod)
  quit()
  
if (minus == N) and (K % 2 == 1):
  ans = 1
  for i in range(1, K + 1):
    ans *= A[-i]
    ans %= mod
  print(ans % mod)
  quit()
  
ans = 1

count = 0
i = 0
j = -1
while count < K:
  if K - count >= 2:
    fo = A[i] * A[i + 1]
    re = A[j] * A[j - 1]
    #print(fo, re)
    if fo >= re:
      ans *= (fo % mod) 
      ans %= mod
      i += 2
    elif (A[j - 1] < 0) or (A[j - 2] < 0):
      if (K % 2 == 0):
        ans *= (re % mod)
        ans %= mod
        j -= 2
      else:  
        ans *= (fo % mod) 
        ans %= mod
        i += 2      
    else:
      ans *= (re % mod)
      ans %= mod
      j -= 2
    count += 2  
  else:
    count += 1
    ans *= A[j]
      
  #print(ans)
print(ans % mod)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7

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

ans = 1
i, j = 0, 0
if k % 2 == 1:
    if len(plus) != 0:
        ans = plus[0]
        i += 1
    else:
        minus.sort()
        ans = -minus[0]
        j += 1
while i + j != k:
    x_p = plus[i] * plus[i + 1] if i < len(plus) - 1 else 0
    x_m = minus[j] * minus[j + 1] if j < len(minus) - 1 else 0
    if x_p >= x_m:
        ans = (ans * x_p) % mod
        i += 2
    else:
        ans = (ans * x_m) % mod
        j += 2
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
N ,K = map(int, input().split())
A = list(map(int, input().split()))
A = sorted(A)
mod = 10 ** 9 + 7
# ans = 0
if A.count(0) > (N - K):
  print(0)
  quit()
  
# ans = minus
plus = bisect.bisect_right(A, 0)
minus = bisect.bisect_left(A, 0) 
if N == K:
  ans = 1
  for i in range(N):
    ans *= A[i]
    ans %= mod
  print(ans % mod)
  quit()
  
if (minus == N) and (K % 2 == 1):
  ans = 1
  for i in range(1, K + 1):
    ans *= A[-i]
    ans %= mod
  print(ans % mod)
  quit()
  
ans = 1

count = 0
i = 0
j = -1
while count < K:
  if K - count >= 2:
    fo = A[i] * A[i + 1]
    re = A[j] * A[j - 1]
    #print(fo, re)
    if fo > re:
      ans *= (fo % mod) 
      ans %= mod
      i += 2
    elif (A[j - 1] < 1):
      #if (K % 2 == 0):
      #  ans *= (re % mod)
      #  ans %= mod
      #  j -= 2
      #else:  
        ans *= (fo % mod) 
        ans %= mod
        i += 2      
    else:
      ans *= (re % mod)
      ans %= mod
      j -= 2
    count += 2  
  else:
    count += 1
    ans *= A[j]
      
  #print(ans)
print(ans % mod)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7

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

ans = 1
i, j = 0, 0
if k % 2 == 1:
    if len(plus) != 0:
        ans = plus[0]
        i += 1
    else:
        minus.sort()
        ans = -minus[0]
        j += 1
while i + j != k:
    x_p = plus[i] * plus[i + 1] if i < len(plus) - 1 else 0
    x_m = minus[j] * minus[j + 1] if j < len(minus) - 1 else 0
    if x_p > x_m:
        ans = (ans * x_p) % mod
        i += 2
    else:
        ans = (ans * x_m) % mod
        j += 2
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7

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

ans = 1
if k == n:
    for x in a:
        ans = (ans * x) % mod
else:
    i, j = 0, 0
    if k % 2 == 1:
        if len(plus) != 0:
            ans = plus[0]
            i += 1
        else:
            minus.sort(reverse=True)
            ans = -minus[0]
            j += 1
    while i + j != k:
        x_p = plus[i] * plus[i + 1] if i < len(plus) - 1 else 0
        x_m = minus[j] * minus[j + 1] if j < len(minus) - 1 else 0
        if x_p > x_m:
            ans = (ans * x_p) % mod
            i += 2
        else:
            ans = (ans * x_m) % mod
            j += 2
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
N ,K = map(int, input().split())
A = list(map(int, input().split()))
A = sorted(A)
mod = 10 ** 9 + 7
# ans = 0
if A.count(0) > (N - K):
  print(0)
  quit()
  
# ans = minus
plus = bisect.bisect_right(A, 0)
minus = bisect.bisect_left(A, 0) 
if N == K:
  ans = 1
  for i in range(N):
    ans *= A[i]
    ans %= mod
  print(ans % mod)
  quit()
  
if (minus == N) and (K % 2 == 1):
  ans = 1
  for i in range(1, K + 1):
    ans *= A[-i]
    ans %= mod
  print(ans % mod)
  quit()
  
ans = 1

count = 0
i = 0
j = -1
while count < K:
  if K - count >= 2:
    fo = A[i] * A[i + 1]
    re = A[j] * A[j - 1]
    #print(fo, re)
    if fo > re:
      ans *= (fo % mod) 
      ans %= mod
      i += 2
    elif (A[j - 1] < 1) or (A[j - 2] < 1):
      if (K % 2 == 0):
        ans *= (re % mod)
        ans %= mod
        j -= 2
      else:  
        ans *= (fo % mod) 
        ans %= mod
        i += 2      
    else:
      ans *= (re % mod)
      ans %= mod
      j -= 2
    count += 2  
  else:
    count += 1
    ans *= A[j]
      
  #print(ans)
print(ans % mod)