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:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A = sorted(A, key=lambda x: abs(x), reverse=True)
signs = [int(a < 0) for a in A]

if N == K :
  select = A
### Aがすべて負なら大きいほうから選択する
elif sum(signs) == N :
  select = A[-K:]
else :
  ### 基本絶対値の大きいほうから選択する
  ### 負の数が奇数個になるなら、一番絶対値の小さい数を
  ### それと違う符号の数に入れかえる
  if sum(signs[:K]) % 2 == 1 :
    select = A[:K-1]
    s = signs[K-1]
    for i in range(K, N) :
      if signs[i] != s :
        select.append(A[i])
        break
  else :
    select = A[:K]

ans = 1
for i in select :
  ans = (ans * i) % int(10**9+7)
print(ans)

Submission status: Wrong Answer

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

B,C=[],[]
for i in A:
    if i>=0:B.append(i)
    else:C.append(i)

B.sort()
C.sort(key=lambda x:abs(x))

ans,flag=1,1
while k!=0:
    if k>=2:
        if len(B)>=2 and len(C)>=2:
            if B[-1]*B[-2]>C[-1]*C[-2]:
                ans *=B.pop()
                k -=1
            else:
                ans *=C.pop()*C.pop()
                k -=2
        elif len(B)>=2:
            ans *=B.pop()*B.pop()
            k -=2
        elif len(C)>=2:
            ans *=C.pop()*C.pop()
            k -=2
        else:break
    else:
        if len(B)>=1:ans *=B.pop()
        else:flag=0
        k -=1

    ans %=mod

if flag:print(ans)
else:
    ans=1
    A=sorted(A,key=lambda x:abs(x))
    for i in range(K):
        ans *=A[i]
        ans %=mod
    print(ans)

Submission status: Wrong Answer

Solution code:
MOD=10**9+7
N,K=map(int,input().split())
A=list(map(int,input().split()))
A=sorted(A,key=lambda x:abs(x))
Max=1
Min=1
for i in range(K):
    Max*=A[-i-1]
    Min*=A[i]
    Max%=MOD
    Min%=MOD
print(max(Max,Min)%MOD)

Submission status: Wrong Answer

Solution code:
MOD=10**9+7
N,K=map(int,input().split())
A=list(map(int,input().split()))
A=sorted(A,key=lambda x:abs(x))
Max=1
Min=1
for i in range(K):
    Max*=A[-i-1]
    Min*=A[i]
print(max(Max,Min)%MOD)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
mi = []
ze = []
pl = []
q = 10**9 + 7
ans = 1
if n == k:
    for _ in A:
        ans *= _
    print(ans % q)
    quit()

for a in A:
    if a < 0: mi.append(a)
    else: pl.append(a)

mi.sort()
pl.sort(reverse=True)

mi_ = len(mi)
pl_ = len(pl)


if len(mi) == n:
    for i in range(k):
        ans *= mi[n-1-i]
    print(ans % q)
    quit()

while(True):
    if k == 1:
        if pl_ > 0:
            ans *= pl[0]
        else:
            ans *= mi[0]
        print(ans % q)
        quit()
    if k == 0:
        print(ans % q)
        quit()
    else:
        if pl_ < 2:
            ans *= mi[0]*mi[1]
            mi.pop()
            mi.pop()
            mi_ -= 2
        elif mi_ < 2:
            ans *= pl[0]*pl[1]
            pl.pop()
            pl.pop()
            pl_ -= 2
        else:
            if mi[0]*mi[1] >= pl[0]*pl[1]:
                ans *= mi[0]*mi[1]
                mi.pop()
                mi.pop()
                mi_ -= 2
            else:
                ans *= pl[0]*pl[1]
                pl.pop()
                pl.pop()
                pl_ -= 2
        k -= 2

Submission status: Wrong Answer

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

MOD = 10**9+7

def f(n, k, a):
  res = 1
  if a[-1] < 0 and k&1:
    for i in range(1, k+1):
      res *= a[-i]
      res %= MOD
    return res
  
  left, right = 0, n-1
  fl, fr = a[left]*a[left+1], a[right]*a[right-1]
  for i in range(k//2):
    if fl>=fr:
      res *= fl
      res %= MOD
      left += 2
      if left < n-1: fl = a[left]*a[left+1]
    else:
      res *= fr
      res %= MOD
      right -= 2
      if right > 0: fr = a[right]*a[right-1]
  if k&1:
    res *= a[right]
    res %= MOD
  return res

print(f(n, k, a))

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random,resource

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n,k = LI()
    a = sorted(LI())

    r = 1
    i = n - 1
    j = 0
    if k % 2 == 1:
        r = a[i]
        i -= 1

    for _ in range(k//2):
        t = a[i] * a[i-1]
        u = a[j] * a[j+1]
        if (r >= 0 and t >= u) or (r < 0 and t < u):
            r *= t
            r %= mod
            i -= 2
        else:
            r *= u
            r %= mod
            j += 2


    return r


print(main())

Submission status: Wrong Answer

Solution code:
MOD = 10**9 + 7

def prod(a):
    res = 1
    for x in a:
        res = (res * x) % MOD
    return res

def solve(n, k, a):
    if k == 1:
        return max(a)
    elif k == n:
        return prod(a)
    elif (max(a) < 0) and (k % 2 == 1):
        a = sorted(a, reverse=True)
        return prod(a[:k])
    else:
        a = sorted(a, key=lambda x: -abs(x))
        num_negative = len(list(filter(lambda x: x < 0, a[:k])))
        if num_negative % 2 == 0:
            return prod(a[:k])
        else:
            i1, j1 = k-1, k
            for i in range(k):
                if a[i] < 0:
                    i1 = i
            for j in range(k,n):
                if a[j] >= 0:
                    j1 = j
                    break
            i2, j2 = k-1, k
            for i in range(k):
                if a[i] >= 0:
                    i2 = i
            for j in range(k, n):
                if a[j] < 0:
                    j2 = j
                    break
            if abs(a[j1] * a[i2]) > abs(a[i1] * a[j2]):
                return prod(a[:i1] + a[i1+1:k] + [a[j1]])
            else:
                return prod(a[:i2] + a[i2+1:k] + [a[j2]])

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

Submission status: Wrong Answer

Solution code:
MOD = 10**9 + 7

def prod(a):
    res = 1
    for x in a:
        res = (res * x) % MOD
    return res

def solve(n, k, a):
    if k == 1:
        return max(a)
    elif k == n:
        return prod(a)
    elif (max(a) < 0) and (k % 2 == 1):
        a = sorted(a, reverse=True)
        return prod(a[:k])
    else:
        a = sorted(a, key=lambda x: -abs(x))
        num_negative = len(list(filter(lambda x: x < 0, a[:k])))
        if num_negative % 2 == 0:
            return prod(a[:k])
        else:
            i1, j1 = 0, n-1
            for i in range(k):
                if a[i] < 0:
                    i1 = i
            for j in range(k,n):
                if a[j] >= 0:
                    j1 = j
                    break
            i2, j2 = 0, n-1
            for i in range(k):
                if a[i] >= 0:
                    i2 = i
            for j in range(k, n):
                if a[j] < 0:
                    j2 = j
                    break
            if abs(a[j1] * a[i2]) > abs(a[i1] * a[j2]):
                return prod(a[:i1] + a[i1+1:k] + [a[j1]])
            else:
                return prod(a[:i2] + a[i2+1:k] + [a[j2]])

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

Submission status: Wrong Answer

Solution code:
MOD = 10**9 + 7

def prod(a):
    res = 1
    for x in a:
        res = (res * x) % MOD
    return res

def solve(n, k, a):
    if k == 1:
        return max(a)
    elif k == n:
        return prod(a)
    elif max(a) < 0:
        a = sorted(a, reverse=True)
        return prod(a[:k])
    else:
        a = sorted(a, key=lambda x: -abs(x))
        num_negative = len(list(filter(lambda x: x < 0, a[:k])))
        if num_negative % 2 == 0:
            return prod(a[:k])
        else:
            i1, j1 = 0, n-1
            for i in range(k):
                if a[i] < 0:
                    i1 = i
            for j in range(k,n):
                if a[j] >= 0:
                    j1 = j
                    break
            i2, j2 = 0, n-1
            for i in range(k):
                if a[i] >= 0:
                    i2 = i
            for j in range(k, n):
                if a[j] < 0:
                    j2 = j
                    break
            if abs(a[j1] * a[i2]) > abs(a[i1] * a[j2]):
                return prod(a[:i1] + a[i1+1:k] + [a[j1]])
            else:
                return prod(a[:i2] + a[i2+1:k] + [a[j2]])

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

Submission status: Wrong Answer

Solution code:
MOD = 10**9 + 7

def prod(a):
    res = 1
    for x in a:
        res = (res * x) % MOD
    return res

def solve(n, k, a):
    if k == 1:
        return max(a)
    elif k == n:
        return prod(a)
    elif max(a) < 0:
        a = sorted(a, reverse=True)
        return prod(a[:k])
    else:
        a = sorted(a, key=lambda x: -abs(x))
        num_negative = len(list(filter(lambda x: x < 0, a[:k])))
        if num_negative % 2 == 0:
            return prod(a[:k])
        else:
            i1, j1 = None, None
            for i in range(k):
                if a[i] < 0:
                    i1 = i
            for j in range(k,n):
                if a[j] >= 0:
                    j1 = j
                    break
            i2, j2 = None, None
            for i in range(k):
                if a[i] >= 0:
                    i2 = i
            for j in range(k, n):
                if a[j] < 0:
                    j2 = j
                    break
            return prod(a[:k])
            if ((i1 is None) or (j1 is None)) and ((i2 is None) or (j2 is None)):
                return prod(a[:k])
            elif (i1 is None) or (j1 is None):
                return prod(a[:i2] + a[i2+1:k] + [a[j2]])
            elif (i2 is None) or (j2 is None):
                return prod(a[:i1] + a[i1+1:k] + [a[j1]])
            elif abs(a[j1] * a[i2]) > abs(a[i1] * b[j2]):
                return prod(a[:i1] + a[i1+1:k] + [a[j1]])
            else:
                return prod(a[:i2] + a[i2+1:k] + [a[j2]])

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

Submission status: Wrong Answer

Solution code:
MOD = 10**9 + 7

def prod(a):
    res = 1
    for x in a:
        res = (res * x) % MOD
    return res

def solve(n, k, a):
    if k == 1:
        return max(a)
    elif k == n:
        return prod(a)
    elif max(a) < 0:
        a = sorted(a, reverse=True)
        return prod(a[:k])
    else:
        a = sorted(a, key=lambda x: -abs(x))
        num_negative = len(list(filter(lambda x: x < 0, a[:k])))
        if num_negative % 2 == 0:
            return prod(a[:k])
        else:
            i1, j1 = None, None
            for i in range(k):
                if a[i] < 0:
                    i1 = i
            for j in range(k,n):
                if a[j] >= 0:
                    j1 = j
                    break
            i2, j2 = None, None
            for i in range(k):
                if a[i] >= 0:
                    i2 = i
            for j in range(k, n):
                if a[j] < 0:
                    j2 = j
                    break
            return -1
            if (i1 is None) or (j1 is None):
                return prod(a[:i2] + a[i2+1:k] + [a[j2]])
            elif (i2 is None) or (j2 is None):
                return prod(a[:i1] + a[i1+1:k] + [a[j1]])
            elif abs(a[j1] * a[i2]) > abs(a[i1] * b[j2]):
                return prod(a[:i1] + a[i1+1:k] + [a[j1]])
            else:
                return prod(a[:i2] + a[i2+1:k] + [a[j2]])

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

Submission status: Wrong Answer

Solution code:
import operator
from functools import reduce
N, K = map(int, input().split())
A = list(map(int, input().split()))
MOD = 10 ** 9 + 7

positives = [a for a in A if a > 0]
negatives = [a for a in A if a < 0]
positive_cnt = len(positives)
negative_cnt = len(negatives)
zero_cnt = A.count(0)


if 2 * min(K // 2, negative_cnt // 2) + positive_cnt >= K:
    ans = 1
    positives.sort(key=lambda x: abs(x), reverse=True)
    negatives.sort(key=lambda x: abs(x), reverse=True)

    if K % 2 == 1:
        ans *= positives.pop(0)
        K -= 1

    X = []
    for pair in zip(*[iter(positives)] * 2):
        X.append(reduce(operator.mul, pair, 1))
    for pair in zip(*[iter(negatives)] * 2):
        X.append(reduce(operator.mul, pair, 1))
    X.sort(reverse=True)

    ans *= reduce(lambda a, b: a * b % MOD, X[:K // 2], 1)
    print(ans)

elif zero_cnt:
    print(0)

else:
    A.sort(key=lambda x: abs(x))
    print(reduce(lambda a, b: a * b % MOD, A[:K], 1) % MOD)

Submission status: Wrong Answer

Solution code:
import functools

def multiply(x, y):
  mod = pow(10,9) + 7
  ans = x * y
  if ans >= mod or ans < 0:
    ans %= mod
  return ans

n, k = map(int, input().split())
num = list(map(int, input().split()))
sort_num = sorted(num, reverse=True)
abssort_num = sorted(num, key = abs, reverse=True)
minusNum = 0
cnt = 0
answer = 1
mod = pow(10,9) + 7
flg = -1
minusCnt = 0
delFlg = 0
if (sort_num[0] < 0) and (k % 2 == 1):
  combi = abssort_num[:k]
  answer = functools.reduce(multiply, combi)
elif k == n:
  answer = functools.reduce(multiply, num)
else:
  for i in range(n):
    if abssort_num[i] < 0:
      minusCnt += 1
      flg = i
  if minusCnt != 0:
    if (minusCnt % 2) == 1:
      del abssort_num[flg]
      n -= 1
  for i in range(n):
    if cnt == k:
      break
    if cnt == k - 1:
      if minusNum % 2 == 0:
        if abssort_num[i] > 0:
          answer *= abssort_num[i]
          answer %= mod
          break
        else:
          continue
      else:
        if abssort_num[i] < 0:
          answer *= abssort_num[i]
          answer %= mod
          break
        else:
          continue
    else:
      answer *= abssort_num[i]
      answer %= mod
      cnt += 1
      if abssort_num[i] < 0:
        minusNum += 1
print(answer)

Submission status: Wrong Answer

Solution code:
import functools

def multiply(x, y):
  mod = pow(10,9) + 7
  ans = x * y
  if ans >= mod or ans < 0:
    ans %= mod
  return ans

n, k = map(int, input().split())
num = list(map(int, input().split()))
sort_num = sorted(num, reverse=True)
abssort_num = sorted(num, key = abs, reverse=True)
minusNum = 0
cnt = 0
answer = 1
mod = pow(10,9) + 7
flg = -1
minusCnt = 0
delFlg = 0
if (sort_num[0] < 0) and (k % 2 == 1):
  combi = abssort_num[-k:]
  answer = functools.reduce(multiply, combi)
elif k == n:
  answer = functools.reduce(multiply, num)
else:
  for i in range(n):
    if abssort_num[i] < 0:
      minusCnt += 1
      flg = i
  if minusCnt != 0:
    if (minusCnt % 2) == 1:
      del abssort_num[flg]
      n -= 1
  for i in range(n):
    if cnt == k:
      break
    if cnt == k - 1:
      if minusNum % 2 == 0:
        if abssort_num[i] > 0:
          answer *= abssort_num[i]
          answer %= mod
          cnt += 1
        else:
          continue
      else:
        if abssort_num[i] < 0:
          answer *= abssort_num[i]
          answer %= mod
          cnt += 1
        else:
          continue
    else:
      answer *= abssort_num[i]
      answer %= mod
      cnt += 1
      if abssort_num[i] < 0:
        minusNum += 1
print(answer)

Submission status: Wrong Answer

Solution code:
import functools

def multiply(x, y):
  mod = pow(10,9) + 7
  ans = x * y
  if ans >= mod or ans < 0:
    ans %= mod
  return ans

n, k = map(int, input().split())
num = list(map(int, input().split()))
sort_num = sorted(num, reverse=True)
abssort_num = sorted(num, key = abs, reverse=True)
minusNum = 0
cnt = 0
answer = 1
mod = pow(10,9) + 7
flg = -1
minusCnt = 0
if (sort_num[0] < 0) and (k % 2 == 1):
  combi = abssort_num[-k:]
  answer = functools.reduce(multiply, combi)
elif k == n:
  answer = functools.reduce(multiply, num)
else:
  for i in range(n):
    if abssort_num[i] < 0:
      minusCnt += 1
      flg = i
  if minusCnt == 1:
    del abssort_num[flg]
  for i in range(n):
    if cnt == k:
      break
    if cnt == k - 1:
      if minusNum % 2 == 0:
        if abssort_num[i] > 0:
          answer *= abssort_num[i]
          answer %= mod
          cnt += 1
        else:
          continue
      else:
        if abssort_num[i] < 0:
          answer *= abssort_num[i]
          answer %= mod
          cnt += 1
        else:
          continue
    else:
      answer *= abssort_num[i]
      answer %= mod
      cnt += 1
      if abssort_num[i] < 0:
        minusNum += 1
print(answer)

Submission status: Wrong Answer

Solution code:
import functools

def multiply(x, y):
  mod = pow(10,9) + 7
  ans = x * y
  if ans >= mod or ans < 0:
    ans %= mod
  return ans

n, k = map(int, input().split())
num = list(map(int, input().split()))
sort_num = sorted(num, reverse=True)
abssort_num = sorted(num, key = abs, reverse=True)
minusNum = 0
cnt = 0
answer = 1
mod = pow(10,9) + 7
if (sort_num[0] < 0) and (k % 2 == 1):
  combi = abssort_num[-k:]
  answer = functools.reduce(multiply, combi)
elif k == n:
  answer = functools.reduce(multiply, num)
else:
  for i in range(n):
    if cnt == k:
      break
    if cnt == k-1:
      if minusNum %2 == 0:
        if abssort_num[i] > 0:
          answer *= abssort_num[i]
          answer %= mod
          cnt += 1
        else:
          continue
      else:
        if abssort_num[i] < 0:
          answer *= abssort_num[i]
          answer %= mod
          cnt += 1
        else:
          continue
    else:
      answer *= abssort_num[i]
      answer %= mod
      cnt += 1
      if abssort_num[i] < 0:
        minusNum += 1
print(answer)

Submission status: Wrong Answer

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

def main():
    N, K = map(int,input().split())
    A = list(map(int,input().split()))
    MOD = 10**9+7
    if K == N:
        ans = 1
        for num in A:
            ans *= num
        print((ans+MOD)%MOD)
        exit()
    posi = 0
    for num in A:
        if num < 0:
            posi += 1
    if posi == N and K%2 == 1:
        ans = 1
        for i in range(K):
            num = A[i]
            ans *= num
        print((ans+MOD)%MOD)
        exit()
    A.sort(key=lambda x:abs(x), reverse=True)
    use = A[:K]
    unuse = A[K:]
    nega = 0
    leat_nega = 0
    ans = 1
    for num in use:
        ans *= num
        if num < 0:
            nega += 1
            last_nega = num
    if nega%2 == 0:
        print((ans+MOD)%MOD)
    else:
        ans //= last_nega
        for num in unuse:
            if not num < 0:
                ans *= num
                break
        print((ans+MOD)%MOD)
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from sys import stdin, stdout
input, print = stdin.readline, stdout.write

MOD = int(1e9 + 7)

def divide(p, q):
    return (pow(q, MOD - 2, MOD) * (p % MOD)) % MOD

n, k = map(int, input().split())
a = list(map(int, input().split()))
b = []
negative = 0
for i in range(n):
    if a[i] > 0:
        b.append([a[i], 1])
    elif a[i] < 0:
        b.append([-a[i], -1])
        negative += 1
    else:
        b.append([0, 0])
b.sort(key = lambda x: x[0])
ans = 1
if negative == n and k % 2:
    for i in range(k):
        ans = (ans * -b[i][0]) % MOD
else:
    i = n - 1
    negative = 0
    last_negative = 0
    while i >= n - k:
        ans = (ans * b[i][0]) % MOD
        if b[i][1] == -1:
            negative += 1
            last_negative = b[i][0]
        i -= 1
    if negative % 2:
        ans = divide(ans, last_negative)
        while i != -1 or b[i][0] < 0:
            i -= 1
        ans = (ans * b[i][0]) % MOD
print(str(ans))

Submission status: Wrong Answer

Solution code:
import functools

def sign(x):
    if x>0:
        return 1
    elif x==0:
        return 0
    else:
        return -1
    
def multiply(x,y):
    ans = x*y
    if ans>=10**9+7 or ans<0:
        ans %= (10**9+7)
    return ans

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

if (A[-1]<0 and K%2==1):
    combi = A[-K:]
    if K >1:
        answer =  functools.reduce(multiply,combi)

elif K == N:
    answer = functools.reduce(multiply,A)
    
else:
    signs = 1
    i,j = 0,0
    for _ in range(K):
        if abs(A[i]) > abs(A[N-j-1]):
            signs *= sign(A[i])
            i += 1
            
        else:
            signs *= sign(A[N-j-1])
            j += 1
    combi = A[N-j:] + A[:i]
    del A[N-j:]
    del A[:i]
    combi = combi[::-1]     
       
    if signs == -1:
        if A:
            if A[0]>=0:
                del combi[0]
                combi.append(A[-1])
            elif A[0]<0 and A[-1] >0:
                if combi[0]*A[0] > combi[-1]*A[-1]:
                    del combi[-1]
                    combi.insert(0,A[0])
                else:
                    del combi[0]
                    combi.append(A[-1])
            elif A[-1] <= 0:
                del combi[-1]
                combi.insert(0,A[0]) 
                
    answer =  functools.reduce(multiply,combi)
print(answer)

Submission status: Wrong Answer

Solution code:
import functools

def sign(x):
    if x>0:
        return 1
    elif x==0:
        return 0
    else:
        return -1
    
def multiply(x,y):
    ans = x*y
    if ans>=10**9+7 or ans<0:
        ans %= (10**9+7)
    return ans

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

if (A[-1]<0 and K%2==1):
    combi = A[-K:]
    if K >1:
        answer =  functools.reduce(multiply,combi)
        answer %= 10**9+7

elif K == N:
    answer = functools.reduce(multiply,A)
    
else:
    signs = 1
    i,j = 0,0
    for _ in range(K):
        if abs(A[i]) > abs(A[N-j-1]):
            signs *= sign(A[i])
            i += 1
            
        else:
            signs *= sign(A[N-j-1])
            j += 1
    combi = A[N-j:] + A[:i]
    del A[N-j:]
    del A[:i]
    combi = combi[::-1]     
       
    if signs == -1:
        if A:
            if A[0]>=0:
                del combi[0]
                combi.append(A[-1])
            elif A[0]<0 and A[-1] >0:
                if combi[0]*A[0] > combi[-1]*A[-1]:
                    del combi[-1]
                    combi.insert(0,A[0])
                else:
                    del combi[0]
                    combi.append(A[-1])
            elif A[-1] <= 0:
                del combi[-1]
                combi.insert(0,A[0]) 
                
    answer =  functools.reduce(multiply,combi)
print(answer)

Submission status: Wrong Answer

Solution code:
import functools

def sign(x):
    if x>0:
        return 1
    elif x==0:
        return 0
    else:
        return -1
    
def multiply(x,y):
    ans = x*y
    if ans>=10**9+7 or ans<0:
        ans %= (10**9+7)
    return ans

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

if (A[-1]<0 and K%2==1):
    combi = A[-K:]
    if K >1:
        answer =  functools.reduce(multiply,combi)
        answer %= 10**9+7

elif K == N:
    answer = functools.reduce(multiply,A)
    
else:
    signs = 1
    i,j = 0,0
    for _ in range(K):
        if abs(A[i]) > abs(A[N-j-1]):
            signs *= sign(A[i])
            i += 1
            
        else:
            signs *= sign(A[N-j-1])
            j += 1
    combi = A[N-j:] + A[:i]
    del A[N-j:]
    del A[:i]
    combi = combi[::-1]     
       
    if signs == -1:
        if A:
            if A[0]>=0:
                del combi[0]
                combi.append(A[-1])
            elif A[0]<0 and A[-1] >0:
                if combi[0]*A[0] > combi[-1]*A[-1]:
                    del combi[-1]
                    combi.insert(0,A[0])
                else:
                    del combi[0]
                    combi.append(A[-1])
            elif A[-1] <= 0:
                del combi[-1]
                combi.insert(0,A[0]) 
                
    answer =  functools.reduce(multiply,map(abs,combi))
print(answer)

Submission status: Wrong Answer

Solution code:
import functools

def sign(x):
    if x>0:
        return 1
    elif x==0:
        return 0
    else:
        return -1
    
def multiply(x,y):
    ans = x*y
    if ans>=10**9+7 or ans<0:
        ans %= (10**9+7)
    return ans

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

if (A[-1]<0 and K%2==1):
    combi = A[-K:]
    if K >1:
        answer =  functools.reduce(multiply,map(abs,combi[1:]))
        answer *= combi[0]
    else:
        answer = combi[0]
    answer %= 10**9+7

elif K == N:
    answer = functools.reduce(multiply,A)
    
else:
    signs = 1
    i,j = 0,0
    for _ in range(K):
        if abs(A[i]) > abs(A[N-j-1]):
            signs *= sign(A[i])
            i += 1
            
        else:
            signs *= sign(A[N-j-1])
            j += 1
    combi = A[N-j:] + A[:i]
    del A[N-j:]
    del A[:i]
    combi = combi[::-1]     
       
    if signs == -1:
        if A:
            if A[0]>=0:
                del combi[0]
                combi.append(A[-1])
            elif A[0]<0 and A[-1] >0:
                if combi[0]*A[0] > combi[-1]*A[-1]:
                    del combi[-1]
                    combi.insert(0,A[0])
                else:
                    del combi[0]
                    combi.append(A[-1])
            elif A[-1] <= 0:
                del combi[-1]
                combi.insert(0,A[0]) 
                
    answer =  functools.reduce(multiply,map(abs,combi))
print(answer)

Submission status: Wrong Answer

Solution code:
import functools

def sign(x):
    if x>0:
        return 1
    elif x==0:
        return 0
    else:
        return -1
    
def multiply(x,y):
    ans = x*y
    if ans>=10**9+7:
        ans %= (10**9+7)
    return ans

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

if (A[-1]<0 and K%2==1):
    combi = A[-K:]
    if K >1:
        answer =  functools.reduce(multiply,map(abs,combi[1:]))
        answer *= combi[0]
    else:
        answer = combi[0]
    answer %= 10**9+7

elif K == N:
    answer = functools.reduce(multiply,map(abs,A))
    
else:
    signs = 1
    i,j = 0,0
    for _ in range(K):
        if abs(A[i]) > abs(A[N-j-1]):
            signs *= sign(A[i])
            i += 1
            
        else:
            signs *= sign(A[N-j-1])
            j += 1
    combi = A[N-j:] + A[:i]
    del A[N-j:]
    del A[:i]
    combi = combi[::-1]     
       
    if signs == -1:
        if A:
            if A[0]>=0:
                del combi[0]
                combi.append(A[-1])
            elif A[0]<0 and A[-1] >0:
                if combi[0]*A[0] > combi[-1]*A[-1]:
                    del combi[-1]
                    combi.insert(0,A[0])
                else:
                    del combi[0]
                    combi.append(A[-1])
            elif A[-1] <= 0:
                del combi[-1]
                combi.insert(0,A[0]) 
                
    answer =  functools.reduce(multiply,map(abs,combi))
print(answer)

Submission status: Wrong Answer

Solution code:
import functools

def sign(x):
    if x>0:
        return 1
    elif x==0:
        return 0
    else:
        return -1
    
def multiply(x,y):
    ans = x*y
    if ans>=10**9+7:
        ans %= (10**9+7)
    return ans

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

if (A[-1]<0 and K%2==1) or K==N:
    combi = A[-K:]
    if K >1:
        answer =  functools.reduce(multiply,map(abs,combi[1:]))
        answer *= combi[0]
    else:
        answer = combi[0]
    answer %= 10**9+7
else:
    signs = 1
    i,j = 0,0
    for _ in range(K):
        if abs(A[i]) > abs(A[N-j-1]):
            signs *= sign(A[i])
            i += 1
            
        else:
            signs *= sign(A[N-j-1])
            j += 1
    combi = A[N-j:] + A[:i]
    del A[N-j:]
    del A[:i]
    combi = combi[::-1]     
       
    if signs == -1:
        if A:
            if A[0]>=0:
                del combi[0]
                combi.append(A[-1])
            elif A[0]<0 and A[-1] >0:
                if combi[0]*A[0] > combi[-1]*A[-1]:
                    del combi[-1]
                    combi.insert(0,A[0])
                else:
                    del combi[0]
                    combi.append(A[-1])
            elif A[-1] <= 0:
                del combi[-1]
                combi.insert(0,A[0]) 
                
    answer =  functools.reduce(multiply,map(abs,combi))
print(answer)

Submission status: Wrong Answer

Solution code:
import functools

def sign(x):
    if x>0:
        return 1
    elif x==0:
        return 0
    else:
        return -1
    
def multiply(x,y):
    ans = x*y
    if ans>=10**9+7:
        ans %= (10**9+7)
    return ans

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

if A[-1]<0 and K%2==1:
    combi = A[-K:]
    if K >1:
        answer =  functools.reduce(multiply,map(abs,combi[:-1]))
        answer *= combi[-1]
    else:
        answer = combi[0]
    answer %= 10**9+7
else:
    signs = 1
    i,j = 0,0
    for _ in range(K):
        if abs(A[i]) > abs(A[N-j-1]):
            signs *= sign(A[i])
            i += 1
            
        else:
            signs *= sign(A[N-j-1])
            j += 1
    combi = A[N-j:] + A[:i]
    del A[N-j:]
    del A[:i]
    combi = combi[::-1]     
       
    if signs == -1:
        if A:
            if A[0]>=0:
                del combi[0]
                combi.append(A[-1])
            elif A[0]<0 and A[-1] >0:
                if combi[0]*A[0] > combi[-1]*A[-1]:
                    del combi[-1]
                    combi.insert(0,A[0])
                else:
                    del combi[0]
                    combi.append(A[-1])
            elif A[-1] <= 0:
                del combi[-1]
                combi.insert(0,A[0]) 
        

    answer =  functools.reduce(multiply,combi)
print(answer)