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) .

Solution (Python):
import sys
input = sys.stdin.readline
mod = 10**9+7

n, k = map(int, input().split())
A = list(map(int, input().split()))
B = []
C = []
s = 0
t = 0
for a in A:
  if a < 0:
    B.append(-a)
    s += 1
  elif a > 0:
    C.append(a)
    t += 1
if s + t < k:
  print(0)
  exit()
if s + t == k:
  if k == n:
    ans = 1
    for a in A:
      ans *= a
      ans %= mod
    print(ans)
  else:
    if s%2:
      print(0)
    else:
      ans = 1
      for b in B:
        ans *= b
        ans %= mod
      for c in C:
        ans *= c
        ans %= mod
      print(ans)
  exit()
B.sort(reverse=True)
C.sort(reverse=True)
ans = 1
nb = 0
nc = 0
if t == 0:
  if k%2:
    if s == n:
      for b in B[-k:]:
        ans *= b
        ans %= mod
      ans *= -1
      ans %= mod
      print(ans)
    else:
      print(0)
  else:
    for b in B[:k]:
      ans *= b
      ans %= mod
    ans %= mod
    print(ans)
  exit()
for _ in range(k):
  if nb >= s:
    ans *= C[nc]
    ans %= mod
    nc += 1
  elif nc >= t:
    ans *= B[nb]
    ans %= mod
    nb += 1
  else:
    if B[nb] > C[nc]:
      ans *= B[nb]
      ans %= mod
      nb += 1
    else:
      ans *= C[nc]
      ans %= mod
      nc += 1
if nb%2:
  if nb == s:
    ans *= pow(B[-1], mod-2, mod)
    ans %= mod
    ans *= C[nc]
    ans %= mod
    print(ans)
  elif nc == t:
    ans *= B[nb]
    ans %= mod
    ans *= pow(C[-1], mod-2, mod)
    ans %= mod
    print(ans)
  else:
    if nc == 0:
      ans *= pow(B[nb-1], mod-2, mod)
      ans %= mod
      ans *= C[0]
      ans %= mod
      print(ans)
    else:
      if C[nc-1]*C[nc] >= B[nb-1]*B[nb]:
        ans *= pow(B[nb-1], mod-2, mod)
        ans %= mod
        ans *= C[nc]
        ans %= mod
        print(ans)
      else:
        ans *= pow(C[nc-1], mod-2, mod)
        ans %= mod
        ans *= B[nb]
        ans %= mod
        print(ans)
else:
  print(ans)

Solution (Python):
N,K=list(map(int,input().split()))
a=list(map(int,input().split()))
a.sort(key=lambda x:abs(x),reverse=True)

mod=10**9+7

p=[]
m=[]
zero=False
cntm=0
cntp=0

for aa in a:
    if aa>=0:
        p.append(aa)
        if cntp+cntm<K:
            cntp+=1
    elif aa<0:
        m.append(-aa)
        if cntp+cntm<K:
            cntm+=1
    
    if aa==0:
        zero=True

ans=1

if cntm%2==1:
    if cntm+1<=len(m) and cntm+1<=K and cntp+1<=len(p) and cntp+1<=K:
        if p[cntp]*p[cntp-1]>m[cntm]*m[cntm-1]:
            cntp+=1
            cntm-=1
        else:
            cntm+=1
            cntp-=1
    elif cntm+1<=len(m) and cntm+1<=K:
        cntm+=1
        cntp-=1
    elif cntp+1<=len(p) and cntp+1<=K:
        cntp+=1
        cntm-=1
    else:
        if zero:
            print(0)
            exit(0)
        
        for i in range(K):
            ans*=a[N-i-1]
            ans=(ans+mod*mod)%mod
        
        print(ans)
        exit(0)

for i in range(cntm):
    ans*=m[i]
    ans%=mod
for i in range(cntp):
    ans*=p[i]
    ans%=mod

print(ans)

Solution (Python):

import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

n, k = map(int, input().split())
a = list( map(int, input().split()))
p = 10**9+7

a.sort()

from bisect import bisect_left, bisect_right
zerostart = bisect_left(a, 0)
posstart = bisect_right(a, 0)

aneg = a[:zerostart]
cntzero = posstart - zerostart
apos = a[posstart:]

cntneg = len(aneg)
cntpos = len(apos)


if k > n-cntzero:
    print(0)
    exit()


if k <= cntpos or (k%2==0 and  k <= (cntneg//2)*2 + cntpos) or (k%2==1 and cntpos%2==1 and k <= (cntneg//2)*2 + cntpos-1):
    pos_pair_mix=[]

    apos.sort(reverse=True)
    for i in range(cntpos//2):
        pos_pair_mix.append((apos[2*i]*apos[2*i+1],+1,2*i))

    for i in range(cntneg//2):
        pos_pair_mix.append((aneg[2*i]*aneg[2*i+1],-1,2*i))

    pos_pair_mix.sort(key=lambda x:x[0],reverse=True)

    ans = 1
    i_last_apos=0

    for i in range(k//2):
        ans*=pos_pair_mix[i][0]%p
        ans%=p
        if pos_pair_mix[i][1]==1:
            i_last_apos = pos_pair_mix[i][2]+2

    if k%2==0:
        print(ans%p)
        exit()
    else:
        print((ans*apos[i_last_apos])%p)
        exit()


a.sort(key=lambda x:abs(x) )

ans = 1
for i in range(k):
    ans*=a[i]
    ans%=p

print(ans%p)

Solution (Python):
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 min(2 * (negative_cnt // 2), 2 * (K // 2)) + positive_cnt >= K:
    
    positives = sorted(positives, reverse=True)
    negatives = sorted(negatives)

    if K % 2 == 1:
        ans = positives[0]
        positives = positives[1:]
        positive_cnt = len(positives)
    else:
        ans = 1

    positives_twin = [positives[2 * i] * positives[2 * i + 1] for i in range(positive_cnt // 2)] + [0]
    negatives_twin = [negatives[2 * i] * negatives[2 * i + 1] for i in range(negative_cnt // 2)] + [0]
    positives_twin.sort()
    negatives_twin.sort()

    for i in range(K // 2):
        if positives_twin[-1] <= negatives_twin[-1]:
            ans *= negatives_twin[-1]
            ans %= MOD
            negatives_twin.pop()
        else:
            ans *= positives_twin[-1]
            ans %= MOD
            positives_twin.pop()

    print(ans % MOD)

elif zero_cnt > 0:
    
    print(0)
else:
    
    ans = 1
    A_abs = sorted([abs(a) for a in A])
    for a in A_abs[:K]:
        ans *= a
        ans %= MOD
    ans = -ans % MOD
    print(ans)

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

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

a_plus = []
a_minus = []
cnt_0 = 0

for ai in a:
    if(ai==0):
        cnt_0 += 1
    elif(ai > 0):
        a_plus.append(ai)
    else:
        a_minus.append(ai)

a_plus.sort(reverse = True)
a_minus.sort()

cnt_p = len(a_plus)
cnt_m = len(a_minus)

if(cnt_p + cnt_m < k):
    print(0)
    exit()

if(cnt_p + cnt_m == k)&(cnt_0>0)&(cnt_m%2==1):
    print(0)
    exit()


if(cnt_m == n)&(k%2==1):
    ans = 1
    for i in range(k):
        ans *= a_minus[cnt_m - 1 - i]
        ans %= mod
    print(ans)
    exit()
elif(cnt_p == 0)&(k%2==1):
    print(0)
    exit()

p_ind = 0
m_ind = 0

ans = 1
if(k%2==1):
    ans = a_plus[0]
    p_ind = 1

for i in range(k//2):
    tmp_p = 0
    tmp_m = 0
    if(p_ind + 2 <= cnt_p ):
        tmp_p = a_plus[p_ind] * a_plus[p_ind+1]

    if(m_ind + 2 <= cnt_m ):
        tmp_m = a_minus[m_ind] * a_minus[m_ind+1]

    if(tmp_p > tmp_m):
        ans *= tmp_p
        p_ind += 2
    else:
        ans *= tmp_m
        m_ind += 2
    ans %= mod


ans %= mod
print(ans)

Solution (Python):
N,K=list(map(int,input().split()))
a=list(map(int,input().split()))
a.sort(key=lambda x:abs(x),reverse=True)

mod=10**9+7

p=[]
m=[]
zero=False
cntm=0
cntp=0

for aa in a:
    if aa>=0:
        p.append(aa)
        if cntp+cntm<K:
            cntp+=1
    elif aa<0:
        m.append(-aa)
        if cntp+cntm<K:
            cntm+=1
    
    if aa==0:
        zero=True

ans=1

if cntm%2==1:
    if cntm+1<=len(m) and cntm+1<=K and cntp+1<=len(p) and cntp+1<=K:
        if p[cntp]*p[cntp-1]>m[cntm]*m[cntm-1]:
            cntp+=1
            cntm-=1
        else:
            cntm+=1
            cntp-=1
    elif cntm+1<=len(m) and cntm+1<=K:
        cntm+=1
        cntp-=1
    elif cntp+1<=len(p) and cntp+1<=K:
        cntp+=1
        cntm-=1
    else:
        if zero:
            print(0)
            exit(0)
        
        for i in range(K):
            ans*=a[N-i-1]
            ans=(ans+mod*mod)%mod
        
        print(ans)
        exit(0)

for i in range(cntm):
    ans*=m[i]
    ans%=mod
for i in range(cntp):
    ans*=p[i]
    ans%=mod

print(ans)

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))
mod = 10**9+7
A.sort()
ans = 1
if A[-1]==0 and K&1: print(0);exit()
if A[-1]<0 and K&1:
    for i in range(N-1,N-1-K,-1):
        ans =(ans*A[i])%mod
    print(ans)
    exit()
l,r = 0,N-1
if K&1:
    ans *= A[-1]
    r-=1
    K-=1
p = K//2
for i in range(p):
    lp = A[l]*A[l+1]
    rp = A[r]*A[r-1]
    if lp>rp:
        ans = (ans*lp)%mod
        l += 2
    else:
        ans = (ans*rp)%mod
        r-=2
print(ans%mod)

Solution (Python):



def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = sorted(A, key=lambda x: abs(x), reverse=True)
    Bp = [x for x in B if x > 0]
    Bm = [x for x in B if x < 0]
    Bpm = [x for x in B if x != 0]
    Bmm = []
    for i in range(0, len(Bm)-1, 2):
        Bmm.append(Bm[i]*Bm[i+1])

    q = 10**9 + 7

    n_plus, n_zero, n_minus = 0, 0, 0
    for a in A:
        if a > 0:
            n_plus += 1
        elif a < 0:
            n_minus += 1
        else:
            n_zero += 1

    
    

    
    rest = K
    ans = 1

    if K <= n_plus + n_minus and not ((n_plus == 0 or K == n_plus + n_minus) and (K-n_plus) % 2 == 1):
        
        i, j = 0, 0
        while rest > 0:
            if rest >= 2 and j < len(Bmm):
                if i+1 < len(Bp):
                    if Bmm[j] >= Bp[i] * Bp[i+1]:
                        ans = (ans * (Bmm[j] % q)) % q
                        j += 1
                        rest -= 2
                    else:
                        ans = (ans * Bp[i]) % q
                        i += 1
                        rest -= 1
                elif i < len(Bp):
                    if Bmm[j] >= Bp[i]:
                        ans = (ans * (Bmm[j] % q)) % q
                        j += 1
                        rest -= 2
                    else:
                        ans = (ans * Bp[i]) % q
                        i += 1
                        rest -= 1
                else:
                    ans = (ans * (Bmm[j] % q)) % q
                    j += 1
                    rest -= 2
            else:
                ans = (ans * Bp[i]) % q
                i += 1
                rest -= 1
        return ans
    elif n_zero > 0:
        return 0
    else:
        
        for i in range(K):
            ans = (ans * Bpm[len(Bpm)-1-i]) % q
        return ans

        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        


print(main())

Solution (Python):
import sys
INF = float('inf')
MOD = 10**9 + 7
N, K = map(int, input().split())
A = list(map(int, input().split()))
ans = 1
is_pos = 0
for ai in A:
    if ai > 0:
        is_pos += 1
if is_pos == 0 and K % 2 == 1:
    sortA = sorted(A, key=abs)    
    for ai in sortA[:K]:
        ans = (ans * ai) % MOD
    print(ans)
    sys.exit()
        
sortA = sorted(A, reverse=True, key=abs)
pos_list = [0]
neg_list = [0]
for ai in sortA[:K]:
    if ai < 0:
        neg_list += [ai]
    elif ai > 0:
        pos_list += [ai]
    else:
        print(0)
        sys.exit()


first_pos = INF
first_neg = INF
for i in range(K, N):
    if sortA[i] > 0 and first_pos == INF:
        first_pos = sortA[i]
    elif sortA[i] < 0 and first_neg == INF:
        first_neg = sortA[i]
len_pos = len(pos_list)
len_neg = len(neg_list)
if len_pos == 1:
    if len_neg % 2 == 1:
        for i in range(1, len_neg):
            ans = (ans * neg_list[i]) % MOD
    else:
        ans = ans * first_pos
        for i in range(1, len_neg-1):
            ans = (ans * neg_list[i]) % MOD
    print(ans)
    sys.exit()
if len_neg % 2 == 1 or K==N:
    for i in range(1, len_pos):
        ans = (ans * pos_list[i]) % MOD
    for i in range(1, len_neg):
        ans = (ans * neg_list[i]) % MOD
    print(ans)
    sys.exit()

if first_neg == INF and first_pos == INF:
    print(0)
    sys.exit()
if first_neg == INF:
    ans = (ans * first_pos) % MOD
    for i in range(1, len_pos):
        ans = (ans * pos_list[i]) % MOD
    for i in range(1, len_neg-1):
        ans = (ans * neg_list[i]) % MOD
    print(ans)
    sys.exit()
if first_pos == INF:
    ans = (ans * first_neg) % MOD
    for i in range(1, len_pos-1):
        ans = (ans * pos_list[i]) % MOD
    for i in range(1, len_neg):
        ans = (ans * neg_list[i]) % MOD
    print(ans)
    sys.exit()
pos2 = abs(pos_list[-1] * first_pos)
neg2 = abs(neg_list[-1] * first_neg)
if pos2 > neg2:
    ans = (ans * first_pos) % MOD
    for i in range(1, len_pos):
        ans = (ans * pos_list[i]) % MOD
    for i in range(1, len_neg-1):
        ans = (ans * neg_list[i]) % MOD
else:
    ans = (ans * first_neg) % MOD
    for i in range(1, len_pos-1):
        ans = (ans * pos_list[i]) % MOD
    for i in range(1, len_neg):
        ans = (ans * neg_list[i]) % MOD
print(ans)

Solution (Python):
def main(A, n, k):
    MOD = int(1e9) + 7
    A.sort()
    ans = 1
    if A[n - 1] == 0 and (k & 1):
        ans = 0
    elif A[n - 1] <= 0 and (k & 1):
        for i in range(n - 1, n - k - 1, -1):
            ans = (ans*A[i]) % MOD
    else:
        i = 0
        j = n - 1
        if k & 1:
            ans = (ans*A[j]) % MOD
            j -= 1
            k -= 1
        k >>= 1
        for itr in range(k):
            left_product = A[i] * A[i + 1]
            right_product = A[j] * A[j - 1]
            if left_product > right_product:
                ans = (ans*left_product) % MOD
                i += 2
            else:
                ans = (ans*right_product) % MOD
                j -= 2

    print(ans % MOD)


if __name__ == "__main__":
    N, K = map(int, input().split(' '))
    A = list(map(int, input().split(' ')))
    main(A, N, K)

Solution (Python):
from heapq import heapify,heappush,heappop
N, K = map(int,input().split())
A = list(map(int, input().split()))
MOD =1_000_000_007

B = [a for a in A if a > 0]
C = [a for a in A if a<= 0]
B.sort()
C.sort(reverse = True)
ans = 1
if N == K:
  for a in A:
    ans *= a
    ans %= MOD
elif len(B) == 0 and K % 2 == 1:
  for i in range(K):
    ans *= C[i]
    ans %= MOD
else:
  if K % 2 == 1:
    ans *= B[-1]
    B.pop()
    K -= 1
  i = 0 
  j = 0
  k = 0
  while k < K:
    
    flag = 0 
    if len(B) >= 2 and len(C) >= 2 and B[-1]*B[-2] < C[-1]*C[-2]:
      flag = 1
    elif len(B) <= 1 and len(C) >= 2:
      flag = 1
    elif len(B) == 1 and len(C) == 1:
      flag = 2
      
    if flag == 0:  
      ans *= B[-1]*B[-2]
      ans %= MOD
      B.pop()
      B.pop()
    
    elif flag == 1:
      ans *= C[-1]*C[-2]
      ans %= MOD
      C.pop()
      C.pop()
      
    elif flag == 2:
      ans *= C[-1]*B[-1]
      ans %= MOD
      B.pop()
      C.pop()
    
    k += 2

print(ans)

Solution (Python):
import sys, bisect, math, itertools, heapq, collections
from operator import itemgetter

from functools import lru_cache

sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9 + 7
eps = 10**-7


def inp():
    '''
    一つの整数
    '''
    return int(input())


def inpl():
    '''
    一行に複数の整数
    '''
    return list(map(int, input().split()))


def str_inp():
    '''
    文字列をリストとして読み込む
    '''
    return list(input()[:-1])


n, k = inpl()
a = inpl()
data = [[abs(i), i < 0] for i in a]
data.sort(reverse=True)
last_minus = 0
last_plus = 0
ans = 1
minus_amount = 0
for i in range(k):
    elem, sign = data[i][0], data[i][1]
    if elem == 0:
        print(0)
        exit()
    ans = ans * elem % mod
    minus_amount += sign
    if sign:
        last_minus = elem
    else:
        last_plus = elem
if minus_amount % 2 == 0:
    print(ans % mod)
    exit()
if n==k:
    print(-ans % mod)
    exit()
all_minus = 0
for t in data:
    if t[1]:
        all_minus += 1
if all_minus == n:
    ans = 1
    data.sort()
    for i in range(k):
        ans = ans * data[i][0] % mod
    ans = ans * ((-1) % mod)
    print(ans % mod)
    exit()
tmp_plus = 0
tmp_minus = 0
for i in range(k, n):
    elem, sign = data[i][0], data[i][1]
    if last_plus and sign and not tmp_minus:
        tmp_minus = elem
    elif last_minus and not sign and not tmp_plus:
        tmp_plus = elem
if tmp_plus == 0 and tmp_minus == 0:
    ans = 0
elif last_plus == 0:
    ans = ans * pow(last_minus, mod - 2, mod) * tmp_plus % mod
elif last_minus == 0:
    ans = ans * pow(last_plus, mod - 2, mod) * tmp_minus % mod
elif tmp_plus * last_plus > tmp_minus * last_minus:
    ans = ans * pow(last_minus, mod - 2, mod) * tmp_plus % mod
else:
    ans = ans * pow(last_plus, mod - 2, mod) * tmp_minus % mod
print(ans)

Solution (Python):
import sys

def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
MOD=10**9+7
from collections import deque

def main():
    n, k = MII()
    aa = MII()
    pos = [a for a in aa if a >= 0]
    neg = [a for a in aa if a <= 0]
    len_pos = len(pos)
    len_neg = len(neg)
    if n == k:
        res = 1
        for a in aa:
            res *= a
            res %= MOD
        print(res)
    elif len_pos == 0:
        if k%2==1:
            neg.sort(reverse=True)
        else:
            neg.sort()
        res = 1
        for i in range(k):
            res *= neg[i]
            res %= MOD
        print(res)
    else:
        pos.sort(reverse=True)
        neg.sort()
        ans = 1
        if k%2 == 1:
            ans *= pos.pop(0)
            k -= 1

        nums = [e1 * e2 for e1, e2 in zip(neg[::2], neg[1::2])]
        nums += [e1 * e2 for e1, e2 in zip(pos[::2], pos[1::2])]

        nums.sort(reverse=True)
        for e in nums[:k//2]:
            ans *= e
            ans %= MOD
        print(ans)



if __name__ == '__main__':
    main()

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

mod = 10**9+7
n,k = na()
a = na()
a.sort()

ans = 1

if k == 1:
    print(a[-1])
    exit()

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

s = 0
e = -1

while k > 0:
    if k == 1:
        if a[s] >= a[e]:
            ans = ans*a[s]%mod
        else:
            ans = ans*a[e]%mod
        break
    p = a[s]*a[s+1]
    q = a[e]*a[e-1]
    if p >= q:
        ans = p%mod*ans%mod
        s+=2
    else:
        ans = q%mod*ans%mod
        e-=2
    k-=2

print(ans%mod)

Solution (Python):
from queue import PriorityQueue
import sys
from collections import defaultdict
readline = sys.stdin.buffer.readline



def geta(fn=lambda s: s.decode()):
    return map(fn, readline().split())


def gete(fn=lambda s: s.decode()):
    return fn(readline().rstrip())


mod = 10**9+7


def prod(l):
    ret = 1
    for e in l:
        e %= mod
        ret = (ret*e) % mod

    return ret


def main():
    n, k = geta(int)
    a = list(geta(int))

    p, n, z = [], [], 0
    for ai in a:
        if ai > 0:
            p.append(ai)
        elif ai < 0:
            n.append(-ai)
        else:
            z += 1

    ans = 1
    p.sort()
    n.sort()


    if len(p) + len(n) < k:
        print(0)
        exit()
    elif len(p) + len(n) == k:
        if len(n) & 1 == 0:
            print(prod(p) * prod(n) % mod)
            exit()
        else:
            if z > 0:
                print(0)
                exit()
            else:
                print(-prod(p)*prod(n) % mod)
                exit()

    if k & 1 == 1:
        if len(p) == 0:
            if z > 0:
                print(0)
                exit()
            else:
                print(-prod(n[0:k]) % mod)
                exit()
        else:
            k -= 1
            ans = p.pop() % mod

    q = []

    n = n[::-1]
    for i in range(len(n)//2):
        q.append(n[2*i]*n[2*i+1])

    p = p[::-1]
    for i in range(len(p)//2):
        q.append(p[2*i]*p[2*i+1])

    q.sort(reverse=True)

    ans = ans * (prod(q[:k//2])) % mod

    print(ans % mod)


if __name__ == "__main__":
    main()

Solution (Python):


import numpy as np

LI = lambda: list(map(int, input().split()))


N, K = LI()
A = np.array(LI())

MOD = 10 ** 9 + 7


def main():
    if np.count_nonzero(A == 0) > N - K:
        print(0)
        return
    m = A[A < 0] * -1
    p = A[A > 0]
    m = np.sort(m)[::-1]
    p = np.sort(p)[::-1]
    mn = len(m)
    pn = len(p)
    i, j = 0, 0
    for _ in range(K):
        if i >= mn:
            j += 1
            continue
        if j >= pn:
            i += 1
            continue
        if m[i] < p[j]:
            j += 1
        else:
            i += 1
    if i % 2 == 1:
        if i < mn and j > 0:
            if j < pn:
                x = m[i - 1] * m[i]
                y = p[j - 1] * p[j]
                if x < y:
                    i -= 1
                    j += 1
                else:
                    i += 1
                    j -= 1
            else:
                i += 1
                j -= 1
        elif j < pn - 1:
            i -= 1
            j += 1
    if i % 2 == 0:
        ans = 1
        for x in m[:i]:
            ans = ans * x % MOD
        for x in p[:j]:
            ans = ans * x % MOD
    else:
        if 0 in A:
            ans = 0
        else:
            ans = -1
            for x in m[mn - i:]:
                ans = ans * x % MOD
            for x in p[pn - j:]:
                ans = ans * x % MOD
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
MOD = 10**9+7

n, k = [int(_) for _ in input().split()]

a = [int(_) for _ in input().split()]

m = list(filter(lambda x: x < 0, a))
p = list(filter(lambda x: x > 0, a))

m.sort()
p.sort(reverse=True)


if len(p)+len(m) < k:
    print(0)
    exit()
if len(p)+len(m) == k:
    ans = 1
    for p_ in p:
        ans = (ans*p_) % MOD
    for q_ in m:
        ans = (ans*q_) % MOD
    print(ans)
    exit()

if len(p) == 0 and k % 2 == 1:
    ans = 1
    for i in range(k):
        ans = (ans*m[-i-1]) % MOD
    if 0 in a:
        ans *= 0
    print(ans)
    exit()

i, j = 0, 0
if k % 2 == 1:
    d = p.pop(-1)
m.append(0)
m.append(0)
p.append(0)
p.append(0)

ans = 1
for c in range(k//2):
    
    if m[i]*m[i+1] >= p[j]*p[j+1]:
        ans = (ans*m[i]*m[i+1]) % MOD
        i += 2
    else:
        ans = (ans*p[j]*p[j+1]) % MOD
        j += 2

if k % 2 == 1:
    ans = (ans*max(p[j], d)) % MOD


if ans > MOD:
    ans -= MOD
print(ans)

Solution (Python):
import math
import fractions
import collections
import itertools
import pprint
from collections import deque
N,K=map(int,input().split())
A=list(map(int,input().split()))
plist=[]
mlist=[]
ans=1
p=10**9+7
for i in range(N):
    if A[i]>=0:
        plist.append(A[i])
    else:
        mlist.append(A[i])
if len(plist)==0:
    if K%2==1:
        A.sort(reverse=True)
    else:
        A.sort()
    for i in range(K):
        ans=(ans*A[i])%p
    print(ans%p)
elif len(plist)==1:
    
    mlist.sort()
    if K%2==1:
        ans=plist[0]
    for i in range(K-1):
        ans=(ans*mlist[i])%p
    print(ans%p)
else:
    ans=1
    plist.sort(reverse=True)
    mlist.sort()
    pmap=deque()
    mmap=deque()
    if K%2==1:
        ans=plist[0]%p
        plist.pop(0)
        K=K-1
    
    for i in range(0,len(plist)-1,2):
        pmap.append(plist[i]*plist[i+1])
    if len(plist)>0:
        pres=plist[len(plist)-1]
    if len(mlist)>0:
        mres=mlist[len(mlist)-1]
    for i in range(0,len(mlist)-1,2):
        mmap.append(mlist[i]*mlist[i+1])
    
    mapnum=K//2
    
    
    for i in range(mapnum):
        if (len(pmap)>0)and(len(mmap)>0):
            pl=pmap.popleft()
            mi=mmap.popleft()
            if pl>mi:
                ans=(ans*pl)%p
                mmap.appendleft(mi)
            else:
                ans=(ans*mi)%p
                pmap.appendleft(pl)
        elif (len(pmap)==0)and(len(mmap)>0):
            mi=mmap.popleft()
            ans = (ans *mi) % p
        elif (len(mmap)==0)and(len(pmap)>0):
            pl=pmap.popleft()
            ans = (ans *pl) % p
        else:
            ans=(ans*pres*mres)%p
    print(ans)

Solution (Python):


"""
Created on Sun May  3 16:22:00 2020

@author: neb0123
"""

def maxProductSubarrayOfSizeK(A, n, k): 
    A.sort() 
    product = 1
  
    if (A[n - 1] == 0 and (k & 1)): 
        return 0
  
    if (A[n - 1] <= 0 and (k & 1)) : 
        for i in range(n - 1, n - k - 1, -1): 
            product *= A[i] 
            product %= (10**9 + 7)
        return product 

    i = 0
    j = n - 1

    if (k & 1): 
        product *= A[j] 
        product %= (10**9 + 7)
        j-= 1
        k-=1
    k >>= 1

    for itr in range( k) : 
        left_product = A[i] * A[i + 1] 
        right_product = A[j] * A[j - 1] 
  
        if (left_product > right_product) : 
            product *= left_product 
            product %= (10**9 + 7)
            i += 2
          
        else : 
            product *= right_product
            product %= (10**9 + 7) 
            j -= 2

    return product

if __name__ == "__main__":
    N,K = map(int, input().split(' '))
    A = list(map(int,input().split(' ')))
    print(maxProductSubarrayOfSizeK(A,N,K))

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    if N == K:
        ans = 1
        for a in A:
            ans *= a
            ans %= MOD
        print(ans)
        return

    B = []
    n_plus = 0
    for a in A:
        if a >= 0:
            B.append((a, True))
            n_plus += 1
        else:
            B.append((-a, False))

    if n_plus == 0:
        if K % 2 == 0:
            A.sort()
        else:
            A.sort(reverse=True)
        ans = 1
        for i in range(K):
            ans *= A[i]
            ans %= MOD
        print(ans)
        return

    B.sort(key=lambda x: x[0], reverse=True)
    cnt = 0
    prev_minus = None
    prev_plus = None
    for i in range(K):
        b, flag = B[i]

        if flag:
            prev_plus = b
        else:
            cnt += 1
            prev_minus = b

    if prev_plus == 0:
        print(0)
        return

    if cnt % 2 == 0:
        ans = 1
        for i in range(K):
            b, flag = B[i]
            ans *= b
            ans %= MOD
        print(ans)
        return

    next_minus = None
    next_plus = None
    for i in range(K, N):
        b, flag = B[i]
        if flag:
            if next_plus is None:
                next_plus = b
        else:
            if next_minus is None:
                next_minus = b

        if b == 0:
            if next_minus is None:
                next_minus = 0

    if next_plus is not None and prev_minus is not None:
        ans1 = 1
        ignored = False
        for i in range(K):
            b, flag = B[i]

            if ignored or prev_minus != b:
                ans1 *= b
                ans1 %= MOD
                continue

            if prev_minus == b:
                ignored = True

        ans1 *= next_plus
        ans1 %= MOD
    else:
        ans1 = -INF

    if next_minus is not None and prev_plus is not None:
        ans2 = 1
        ignored = False
        for i in range(K):
            b, flag = B[i]

            if ignored or prev_plus != b:
                ans2 *= b
                ans2 %= MOD
                continue

            if prev_plus == b:
                ignored = True

        ans2 *= next_minus
        ans2 %= MOD
    else:
        ans2 = -INF

    if ans1 == -INF:
        print(ans2)
    elif ans2 == -INF:
        print(ans1)
    else:
        if prev_minus * next_minus > prev_plus * next_plus:
            print(ans2)
        else:
            print(ans1)


if __name__ == "__main__":
    main()

Solution (Python):
import sys, bisect, math, itertools, heapq, collections
from operator import itemgetter

from functools import lru_cache

sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9 + 7
eps = 10**-7


def inp():
    '''
    一つの整数
    '''
    return int(input())


def inpl():
    '''
    一行に複数の整数
    '''
    return list(map(int, input().split()))


def str_inp():
    '''
    文字列をリストとして読み込む
    '''
    return list(input()[:-1])


n, k = inpl()
a = inpl()
data = [[abs(i), i < 0] for i in a]
all_minus = 0
for t in data:
    if t[1]:
        all_minus += 1
if all_minus == n and k%2!=0:
    ans = 1
    data.sort()
    for i in range(k):
        ans = ans * data[i][0] % mod
    ans=ans*((-1)%mod)
    print(ans % mod)
    exit()
data.sort(reverse=True)
last_minus = 0
last_plus = 0
ans = 1
minus_amount = 0
for i in range(k):
    elem, sign = data[i][0], data[i][1]
    if elem == 0:
        print(0)
        exit()
    ans = ans * elem % mod
    minus_amount += sign
    if sign:
        last_minus = elem
    else:
        last_plus = elem
if minus_amount % 2 == 0:
    print(ans%mod)
    exit()
if n==k:
    print(-ans % mod)
    exit()
tmp_plus = 0
tmp_minus = 0
for i in range(k, n):
    elem, sign = data[i][0], data[i][1]
    if last_plus and sign and not tmp_minus:
        tmp_minus = elem
    elif last_minus and not sign and not tmp_plus:
        tmp_plus = elem
if tmp_plus == 0 and tmp_minus == 0:
    ans = 0
elif last_plus == 0:
    ans = ans * pow(last_minus, mod - 2, mod) * tmp_plus % mod
elif last_minus == 0:
    ans = ans * pow(last_plus, mod - 2, mod) * tmp_minus % mod
elif tmp_plus * last_plus > tmp_minus * last_minus:
    ans = ans * pow(last_minus, mod - 2, mod) * tmp_plus % mod
else:
    ans = ans * pow(last_plus, mod - 2, mod) * tmp_minus % mod
print(ans)