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: Runtime Error

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

if all(i<=0 for i in a) or n==k:
    a.sort(reverse=True)
    res = 1
    mod = 10**9 + 7
    for i in range(k):
        res = (res*a[i])%mod
    print(res)
else:
    mainas = []
    plus = []
    for i in range(n):
        if a[i] < 0: mainas.append(a[i])
        else: plus.append(a[i])
        
    mainas.sort()
    mainas.extend([0,0,0,0])
    plus.extend([0,0,0,0])
    plus.sort(reverse=True)

    m, p = 0,0
    r = []
    while k > 1:
        if mainas[p]*mainas[p+1] < plus[m]*plus[m+1]:
            r.extend(plus[m:m+2])
            m += 2
        else:
            r.extend(mainas[p:p+2])
            p += 2
        k -= 2

    if k == 1:
        r.append(plus[m])

    res = 1
    mod = 10**9 + 7
    for i in r:
        res = (res*i)%mod
    print(res)

Submission status: Runtime Error

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

if all(i<=0 for i in a) or n==k:
    a.sort(reverse=True)
    res = 1
    mod = 10**9 + 7
    for i in range(k):
        res = (res*a[i])%mod
    print(res)
else:
    mainas = []
    plus = []
    for i in range(n):
        if a[i] < 0: mainas.append(a[i])
        else: plus.append(a[i])
        
    mainas.sort()
    mainas.extend([0,0])
    plus.extend([0,0])
    plus.sort(reverse=True)

    m, p = 0,0
    r = []
    while k > 1:
        if mainas[p]*mainas[p+1] < plus[m]*plus[m+1]:
            r.extend(plus[m:m+2])
            m += 2
        else:
            r.extend(mainas[p:p+2])
            p += 2
        k -= 2

    if k == 1:
        r.append(plus[m])

    res = 1
    mod = 10**9 + 7
    for i in r:
        res = (res*i)%mod
    print(res)

Submission status: Runtime Error

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

if all(i<=0 for i in a) or n==k:
    a.sort(reverse=True)
    res = 1
    mod = 10**9 + 7
    for i in range(k):
        res = (res*a[i])%mod
    print(res)
else:
    mainas = []
    plus = []
    for i in range(n):
        if a[i] < 0: mainas.append(a[i])
        else: plus.append(a[i])
        
    mainas.sort()
    mainas.extend([0,0])
    plus.sort(reverse=True)
    plus.extend([0,0])

    m, p = 0,0
    r = []
    while k > 1:
        if mainas[p]*mainas[p+1] < plus[m]*plus[m+1]:
            r.extend(plus[m:m+2])
            m += 2
        else:
            r.extend(mainas[p:p+2])
            p += 2
        k -= 2

    if k == 1:
        r.append(plus[m])

    res = 1
    mod = 10**9 + 7
    for i in r:
        res = (res*i)%mod
    print(res)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
import bisect
import copy
import heapq
import itertools
import math
import random
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


N, K = read_values()
def main():
    A = read_list()
    # A = [random.randint(-10 ** 9, 10 ** 9) for _ in range(N)]

    A.sort(reverse=True)
    if N == K or (A[0] < 0 and len(A) % 2 == 1):
        res = 1
        for a in A[:K]:
            res *= a
            res %= mod
        print(res)
        return

    A.sort(key=lambda a: abs(a), reverse=True)

    P1 = list(filter(lambda a: a >= 0, A[:K]))
    P2 = list(filter(lambda a: a >= 0, A[K:]))
    N1 = list(filter(lambda a: a < 0, A[:K]))
    N2 = list(filter(lambda a: a < 0, A[K:]))

    if len(N1) % 2 == 1:
        # if N1[-1] * N2[0] : P1[-1] * P2[0]
        if len(P1) == 0 or len(N2) == 0:
            N1[-1] = P2[0]
        elif len(P2) == 0:
            P1[-1] = N2[0]
        else:
            if P1[-1] * P2[0] >= N1[-1] * N2[0]:
                N1[-1] = P2[0]
            else:
                P1[-1] = N2[0]
    
    res = 1
    for p in P1:
        res *= p
        res %= mod

    for n in N1:
        res *= n
        res %= mod
    
    print(res % mod)
        

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
import bisect
import copy
import heapq
import itertools
import math
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


def main():
    N, K = read_values()
    A = read_list()
    A.sort(reverse=True)
    if A[0] < 0:
        res = 1
        for a in A[:K]:
            res *= a
            res %= mod
        print(res)
        return

    A.sort(key=lambda a: abs(a), reverse=True)

    P1 = list(filter(lambda a: a >= 0, A[:K]))
    P2 = list(filter(lambda a: a >= 0, A[K:]))
    N1 = list(filter(lambda a: a < 0, A[:K]))
    N2 = list(filter(lambda a: a < 0, A[K:]))

    if len(N1) % 2 == 1:
        # if N1[-1] * N2[0] : P1[-1] * P2[0]
        if len(P1) == 0 or len(N2) == 0:
            N1[-1] = P2[0]
        elif len(P2) == 0:
            P1[-1] = N2[0]
        else:
            if P1[-1] * P2[0] >= N1[-1] * N2[0]:
                N1[-1] = P2[0]
            else:
                P1[-1] = N2[0]
    
    res = 1
    for p in P1:
        res *= p
        res %= mod

    for n in N1:
        res *= n
        res %= mod
    
    print(res % mod)
        

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
import bisect
import copy
import heapq
import itertools
import math
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


def main():
    N, K = read_values()
    A = read_list()
    A.sort(reverse=True)
    if A[0] < 0:
        res = 1
        for a in A[:K]:
            res *= a
            res %= mod
        print(res)
        return

    A.sort(key=lambda a: abs(a), reverse=True)
    res = 1
    c = 0
    p = -1
    n = 1
    for a in A[:K]:
        if a < 0:
            c += 1
            n = a
        else:
            p = a
        res *= a
        res %= mod
    
    if c % 2 == 1:
        p1, n1 = None, None
        for a in A[K:]:
            if a >= 0 and p1 is None:
                p1 = a
            if a < 0 and p2 is None:
                n1 = a
        if p1 is None:
            res *= n1
            res *= pow(p, mod - 2, mod)
            res %= mod
        elif n1 is None or p < 0:
            res *= p1
            res *= pow(n, mod - 2, mod)
            res %= mod
        else:
            res *= pow(p, mod - 2, mod)
            res *= pow(n, mod - 2, mod)
            res %= mod
            t = min(p * n1, n * p1)
            res *= t
            res %= mod
    print(res)
            

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int mod = 1000000007;

int main() {
    ll N, K;
    cin >> N >> K;
    vector<ll> A(N);
    rep(i, N) cin >> A[i];

    ll ans = 1;
    if (N == K)
        rep(i, K) ans = (ans * A[i]) % mod;
    else {
        vector<ll> plus, minus;
        rep(i, N) {
            if (A[i] < 0)
                minus.push_back(A[i]);
            else
                plus.push_back(A[i]);
        }

        if (plus.size() == 0 && K % 2 == 1) {
            sort(minus.begin(), minus.end(), greater<>());
            rep(i, K) ans = (ans * minus[i]) % mod;
        } else {
            vector<pair<ll, pair<ll, ll>>> product;
            vector<ll> rest;
            sort(plus.begin(), plus.end(), greater<>());
            if (K % 2 == 1) {
                ans = (ans * plus[0]) % mod;
                plus.erase(plus.begin());
                --K;
            }
            if (!plus.empty()) {
                for (int i = 0; i < plus.size(); i += 2) {
                    if (i == plus.size() - 1) {
                        rest.push_back(plus[i]);
                        break;
                    }
                    product.push_back(
                        make_pair(plus[i] * plus[i + 1],
                                  make_pair(-plus[i], -plus[i + 1])));
                }
            }
            if (!minus.empty()) {
                sort(minus.begin(), minus.end());
                for (int i = 0; i < minus.size(); i += 2) {
                    if (i == minus.size() - 1) {
                        rest.push_back(minus[i]);
                        break;
                    }
                    product.push_back(
                        make_pair(minus[i] * minus[i + 1],
                                  make_pair(-minus[i], -minus[i + 1])));
                }
            }

            sort(product.begin(), product.end(), greater<>());
            for (int i = 0; i * 2 < K; ++i) {
                if (i * 2 == K - 1) {
                    for (int j = i; j < product.size(); ++j) {
                        rest.push_back(-product[j].second.first);
                        rest.push_back(-product[j].second.second);
                    }
                    sort(rest.begin(), rest.end(), greater<>());
                    ans = (ans * rest[0]) % mod;
                    break;
                }
                ans = (ans * (product[i].first % mod)) % mod;
            }
        }
    }

    if (ans < 0) ans += mod;
    cout << ans << endl;
}

Submission status: Runtime Error

Solution code:

"""

https://atcoder.jp/contests/abc173/tasks/abc173_e

クソだけどやろう

正負・0を分割

0がある場合は、最大を0に更新
0がN-K個より多い場合は即終了

残りの場合は、0を取らなくても構成できる場合のみ


"""

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

pl = []
mi = []
znum = 0

if K == 1:
    print (max(A))
    sys.exit()

for i in A:
    if i > 0:
        pl.append(i)
    elif i == 0:
        znum += 1
    else:
        mi.append(i)

ans = float("-inf")
if znum > 0:
    ans = 0

#0にならざるを得ない場合を処理
if znum > N-K:
    print (ans)
    sys.exit()

#答えが負になる場合も処理する
#大きいほうからかけていけばいい
#N個の場合 or 正がない場合は負になりえない
mod = 10**9+7
pl.sort()
mi.sort()

if K == N:
    ans = 1
    for i in range(N):
        ans *= A[i]
        ans %= mod
    print (ans)
    sys.exit()

if K % 2 == 1 and len(pl) == 0:
    if znum > 0:
        ans = 0
    else:
        ans = 1
    mi.reverse()
    for i in range(K):
        ans *= mi[i]
        ans %= mod
    print (ans)
    sys.exit()

#まず、2個づつセットにすることを考える
#正負でセットにしていって、あまり同士も合成する

if K % 2 == 1:
    K -= 1
    ans = pl[-1]
    del pl[-1]
else:
    ans = 1

pl2 = []
pl.reverse()
pl.append(0)
pl2.append(0)
for i in range(0,len(pl),2):
    pl2.append(pl[i] * pl[i+1])
for j in range(0,len(mi),2):
    pl2.append(mi[i] * mi[i+1])

pl2.sort()
pl2.reverse()
for i in range(K//2):
    ans *= pl2[i]
    ans %= mod
print (ans)

Submission status: Runtime Error

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

from  collections import defaultdict
from collections import Counter
import bisect
from functools import reduce

def main():
    mod = 10 ** 9 + 7
    N, K = MI()
    A = LI()
    A_abs = []
    num_zer = 0
    num_plu = 0
    num_min = 0
    for a in A:
        if a == 0:
            A_abs.append([0, '0'])
            num_zer += 1
        elif a < 0:
            A_abs.append([-a, '-'])
            num_min += 1
        else:
            A_abs.append([a, '+'])
            num_plu += 1
    A_abs.sort(key=lambda tup: tup[0], reverse=True)

    if num_plu == 0 and K % 2 != 0:
        B = A_abs[::-1]
        ans = 1
        for i in range(K):
            ans *= B[i][0] % mod
        print(ans % mod)
    else:
        ans = 1
        cnt_all = 0
        cnt_min = 0
        now_plu = 1
        now_min = 1
        for a in A_abs:
            if cnt_all < K:
                ans *= a[0] % mod
                cnt_all += 1
                if a[1] == '-':
                    now_min = a[0]
                    cnt_min += 1
                elif a[1] == '+':
                    now_plu = a[0]
                continue
            elif cnt_all == K:
                if cnt_min % 2 == 0:
                    print(ans)
                    exit()
                else:
                    cnt_all = K + 1
                continue
            elif cnt_all == K + 1:
                if a[1] == '+':
                    next_plu = a[0]
                    cnt_all = K + 2
                elif a[1] == '-':
                    next_min = a[0]
                    cnt_all = K + 2
                else:
                    print(0)
                    exit()
            else:
                break
        if cnt_all == K:
            print(ans % mod)
            exit()
        if now_plu * next_plu < now_min * next_min:
            ans = (ans * pow(now_plu, mod - 2, mod) * next_min) % mod
        else:
            ans = (ans * pow(now_min, mod - 2, mod) * next_plu) % mod


        print(ans)



if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

P=[0,0]
M=[0,0]
for i in range(n):
  if A[i]>=0:
    P.append(A[i])
  else:
    M.append(-A[i])
if P==[0,0]:
  ans=1
  if k%2:
    for i in range(2,k+2):
      ans*=M[i]
    ans=-ans
    ans%=mod
  else:
    for i in range(n-1,n-k-1,-1):
      ans*=M[i]
    ans%=mod
  print(ans)
else:
  P.sort()
  M.sort()
  S=[]
  cnt=0
  ans=1
  while cnt<k:
    x=P[-1]*P[-2]
    y=M[-1]*M[-2]
    if x>y or cnt==k-1:
      ans*=P.pop()
      cnt+=1
    else:
      ans*=M.pop()
      ans*=M.pop()
      cnt+=2
  print(ans%mod)

Submission status: Runtime Error

Solution code:
import numpy as np
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7
#if N==K:
#  ans0 = 1
#  for a in A[0:N]:
#    ans0 *= a
#    ans0 %= MOD
#  print(ans0)
#  exit()

A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]
 
ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

#while ansA>0:
#  ansA-=MOD

B1=A[0:K]
C1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]
 
if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)
if not Bp:Bp.insert(0,B1[-1])
if not Bm:Bm.insert(0,B1[0])
  
B1.remove(Bp[-1])
B1.append(min(B2[0],0))

ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1.remove(Bm[-1])
C1.append(max(B2[-1],0))
 
ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD

BB1=[np.sign(i) for i in B1]
ansBB1 = BB1[0:K].count(-1)%2
BB2=[np.sign(i) for i in C1]
ansBB2 = BB2[0:K].count(-1)%2

if ansBB1==1:
  print(ansC)
  exit()
if ansBB2==1:
  print(ansB)
  exit()

if abs(Bp[-1]*B2[-1])-abs(Bm[-1]*B2[0])>=0:
  print(ansC)
else: print(ansB)

Submission status: Runtime Error

Solution code:
import numpy as np
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7
if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD

B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)
if not Bp:Bp.insert(0,B1[-1])
if not Bm:Bm.insert(0,B1[0])
  
#print(Bp,Bm,B2)

B1.remove(Bp[-1])
B1.append(min(B2[0],0))
#print(B1)
ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(max(B2[-1],0))

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD

if abs(Bp[-1]*B2[-1])-abs(Bm[0]*B2[0])=>0:
  print(ansC)
#elif abs(Bp[-1]*B2[-1])-abs(Bm[0]*B2[0])>0:
#  print(ansC)
else: print(ansB)

#print(max(ansB,ansC))

Submission status: Runtime Error

Solution code:
import numpy as np
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7
if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD

B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)
if not Bp:Bp.insert(0,B1[-1])
if not Bm:Bm.insert(0,B1[0])
  
#print(Bp,Bm,B2)

B1.remove(Bp[-1])
B1.append(min(B2[0],0))
#print(B1)
ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(max(B2[-1],0))

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD

if abs(Bp[-1]*B2[-1])-abs(Bm[0]*B2[0])==a0:
  print(ansC)
elif abs(Bp[-1]*B2[-1])-abs(Bm[0]*B2[0])>0:
  print(ansB)
else: print(ansB)

#print(max(ansB,ansC))

Submission status: Runtime Error

Solution code:
import numpy as np
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7
if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD

B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)
if not Bp:Bp.insert(0,B1[-1])
if not Bm:Bm.insert(0,B1[0])
  
#print(Bp,Bm,B2)

B1.remove(Bp[-1])
B1.append(min(B2[0],0))
#print(B1)
ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(max(B2[-1],0))

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD

if abs(Bp[-1]*B2[-1])-abs(Bm[0]*B2[0])=0:
  print(ansC)
elif abs(Bp[-1]*B2[-1])-abs(Bm[0]*B2[0])>0:
  print(ansB)
else: print(ansB)

#print(max(ansB,ansC))

Submission status: Runtime Error

Solution code:
import numpy as np
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7
if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD

B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)
if not Bp:Bp.insert(0,B1[-1])
if not Bm:Bm.insert(0,B1[0])
  
#print(Bp,Bm,B2)

B1.remove(Bp[-1])
B1.append(min(B2[0],0))
#print(B1)
ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(max(B2[-1],0))

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD

if abs(Bp[-1]*B2[-1])-abs(Bm[0]*B2[0])=0:
  print(ansC)
else: print(ansB)

#print(max(ansB,ansC))

Submission status: Runtime Error

Solution code:
import numpy as np
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD

B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)
if not Bp:Bp.insert(0,B1[-1])
if not Bm:Bm.insert(0,B1[0])
  
#print(Bp,Bm,B2)

B1.remove(Bp[-1])
B1.append(min(B2[0],0))
#print(B1)
ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(max(B2[-1],0))

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD

if Bp[-1]*B2[-1]-Bm[0]*B2[0]:
  print(ansB)
elese print(ansC)

#print(max(ansB,ansC))

Submission status: Runtime Error

Solution code:
import numpy as np
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD

B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)
#if not Bp:Bp.insert(0,B1[0])
#if not Bm:Bm.insert(0,B1[-1])
  
#print(Bp,Bm,B2)

B1.remove(Bp[-1])
B1.append(B2[0])
#print(B1)
ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(B2[-1])

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD

print(max(ansB,ansC)%MOD)

Submission status: Runtime Error

Solution code:
import numpy as np
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD
B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)
#print(Bp,Bm,B2)

B1.remove(Bp[0])
B1.append(B2[0])
#print(B1)
ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(B2[-1])

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD



print(max(ansB,ansC)%MOD)

Submission status: Runtime Error

Solution code:
import numpy as np
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD
B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]
if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)


B1.remove(Bp[-1])
B1.append(B2[0])

ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(B2[-1])

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD



print(max(ansB,ansC)%MOD)

Submission status: Runtime Error

Solution code:
import numpy as np
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD
B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]
if B2[0]>0:B2.insert(0,0)
if B2[-1]<0:B2.insert(-1,0)

print(ansA)
print(Bm,Bp)
B1.remove(Bp[-1])
B1.append(B2[0])

ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(B2[-1])

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD


print(B1,C1)
print(ansB,ansC)
print(max(ansB,ansC)%MOD)

Submission status: Runtime Error

Solution code:
import numpy as np
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD
B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]
if not Bp:Bp=[1]
if not Bm:Bm=[1]
  
B1.remove(Bp[-1])
B1.append(B2[0])

ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(B2[-1])

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD
  
print(max(ansB,ansC)%MOD)

Submission status: Runtime Error

Solution code:
import numpy as np
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD
B1=A[0:K]
B2=sorted(A[K:N])
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

B1.remove(Bp[-1])
B1.append(B2[0])

ansB = 1
for a in B1[0:K]:
  ansB *= a
  ansB %= MOD

C1=A[0:K]
C1.remove(Bm[-1])
C1.append(B2[-1])

ansC = 1
for a in C1[0:K]:
  ansC *= a
  ansC %= MOD
  
print(max(ansB,ansC))

Submission status: Runtime Error

Solution code:
import numpy as np
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

#while ansA>0:
#  ansA-=MOD
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

B=sorted(A[K:N])
if not Bm:ansB1=ansA//Bp[-1]*B[-1]
else:ansB1=ansA//Bp[-1]*B[-1]
if not Bp:ansB2=ansA//Bm[-1]*B[-1]
else:ansB2=ansA//Bm[-1]*B[-1]          
#print(ansA,B,Bp,Bm)

#print(ansB1,ansB2)%MOD
print(max(ansB1,ansB2)%MOD)

Submission status: Runtime Error

Solution code:
import numpy as np
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

B=sorted(A[K:N])
if not Bm:ansB1=ansA//Bp[-1]*B[-1]
else:ansB1=ansA//Bp[-1]*B[-1]
if not Bp:ansB2=ansA//Bm[-1]*B[-1]
else:ansB2=ansA//Bm[-1]*B[-1]          
#print(ansA,B,Bp,Bm)

#print(ansB1,ansB2)%MOD
print(max(ansB1,ansB2)%MOD)

Submission status: Runtime Error

Solution code:
import numpy as np
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2
if ansA==0 or ansAA==0:
  print(ansA)
  exit()

while ansA>0:
  ansA-=MOD
Bp=[i for i in A[0:K] if i>0]
Bm=[i for i in A[0:K] if i<0]

B=sorted(A[K:N])
ansB1=int(ansA//Bp[-1]*B[0])
ansB2=int(ansA//Bm[-1]*B[-1])          
#print(ansA,B,Bp,Bm)

#print(ansB1,ansB2)%MOD
print(max(ansB1,ansB2)%MOD)

Submission status: Runtime Error

Solution code:
import numpy as np
import collections
N,K=map(int, input().split())
A =list(map(int,input().split()))
MOD = 10 ** 9 + 7

if N==K:
  ans0 = 1
  for a in A[0:N]:
    ans0 *= a
    ans0 %= MOD
  print(ans0)
  exit()
A=sorted(A)  
if (K%2==1 and A[-1]<0):
  ans1 = 1
  for a in A[N-K:N]:
    ans1 *= a
    ans1 %= MOD
  print(ans1)
  exit()

A=sorted(A, key=lambda x:abs(x), reverse=True)
AA=[np.sign(i) for i in A]

ansA = 1
for a in A[0:K]:
  ansA *= a
  ansA %= MOD
ansAA = AA[0:K].count(-1)%2

if ansAA==0:
  print(ansA)
  exit()

B=sorted(A[K-1:N])

if AA[K-1]>0:
  ansB=ansAA//A[K-1]*B[0]
else:ansB=ansAA//A[K-1]*B[-1]

print(ansB)