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:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

import numpy

MAX = 10 ** 9 + 7

def prod_mod(la, mod):
  x = 1
  for a in la:
    x *= a
    x %= mod
  return x

def main():
  n, k = map(int, input().strip().split())
  la = numpy.array(list(map(int, input().strip().split())))
  la = la[numpy.argsort(numpy.abs(la))][::-1]
  if n == k:
    print(prod_mod(la, MAX))
  else:
    if k % 2 == 1 and (la >= 0).sum() == 0:
      print(prod_mod(la[-k:], MAX))
    else:
      lal, lar = la[:k], la[k:]
      lal.sort()
      nlm = (lal < 0).sum()
      if nlm % 2 == 0:
        print(prod_mod(lal, MAX))
      else:
        mpr = numpy.max(lar)
        mmr = numpy.min(lar)
        if mpr > 0 and mmr < 0:
          if abs(mpr * lal[nlm]) > abs(mmr * lal[nlm-1]) and nlm != len(lal):
            lal[nlm-1] = mpr
          else:
            lal[nlm] = mmr
        elif mpr > 0:
          lal[nlm-1] = mpr
        elif mmr < 0 and nlm != len(lal):
          lal[nlm] = mmr
        print(prod_mod(lal, MAX))

if __name__=='__main__':
  main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

import numpy

MAX = 10 ** 9 + 7

def prod_mod(la, mod):
  x = 1
  for a in la:
    x *= a
    x %= mod
  return x

def main():
  n, k = map(int, input().strip().split())
  la = numpy.array(list(map(int, input().strip().split())))
  la = la[numpy.argsort(numpy.abs(la))][::-1]
  if n == k:
    print(prod_mod(la, MAX))
  else:
    if k % 2 == 1 and (la >= 0).sum() == 0:
      print(prod_mod(la[-k:], MAX))
    else:
      lal, lar = la[:k], la[k:]
      lal.sort()
      nlm = (lal < 0).sum()
      if nlm % 2 == 0:
        print(prod_mod(lal, MAX))
      else:
        mpr = numpy.max(lar)
        mmr = numpy.min(lar)
        if mpr > 0 and mmr < 0:
          if abs(mpr * lal[nlm]) > abs(mmr * lal[nlm-1]):
            lal[nlm-1] = mpr
          else:
            lal[nlm] = mmr
        elif mpr > 0:
          lal[nlm-1] = mpr
        elif mmr < 0:
          lal[nlm] = mmr
        print(prod_mod(lal, MAX))

if __name__=='__main__':
  main()

Submission status: Runtime Error

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

if lst[-1] < 0 or k % 2 == 1:
    ans1 = 1
    for i in range(k):
        ans1 *= lst[- i - 1] % mod
        ans %= mod
    print(ans1)
    exit()
	
ans2 = 1
l = 0
r = n - 1
if k % 2 == 1:
    ans2 = lst[-1]
    r -= 1
	
for i in range(k // 2):
    left = lst[l] * lst[l + 1]
    right = lst[r - 1] * lst[r]
    if left > right:
        ans2 *= left % mod
        ans2 %= mod
        l += 2
    else:
        ans2 *= right % mod
        ans2 %= mod
        r -= 2
		
print(ans2 % mod)

Submission status: Runtime Error

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

if lst[-1] < 0 or k % 2 == 1:
	ans1 = 1
	for i in range(k):
		ans1 *= lst[- i - 1] % mod
		ans %= mod
	print(ans1)
	exit()
	
ans2 = 1
l = 0
r = n - 1
if k % 2 == 1:
	ans2 = lst[-1]
	r -= 1
	
for i in range(k // 2):
	left = lst[l] * lst[l + 1]
	right = lst[r - 1] * lst[r]
	if left > right:
		ans2 *= left % mod
		ans2 %= mod
		l += 2
	else:
		ans2 *= right % mod
		ans2 %= mod
		r -= 2
		
print(ans2 % mod)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
M = []
m = []
for a in A:
    if a < 0:
        m.append(abs(a))
    else:
        M.append(a)

i = 0
j = 0
tmp = 1
P = 10**9 + 7

if (K%2 == 0 and (len(m)//2*2 + len(M)//2*2) >= K) or (K%2 == 1 and (len(M) + len(m)//2*2) >= K and len(M) > 0):
    m.sort(reverse=True)
    M.sort(reverse=True)
    while K > 0:
        if K >= 2:
            if j+2 <= len(M) and i+2 <= len(m):
                if m[i]*m[i+1] < M[j]*M[j+1]:
                    tmp *= M[j]*M[j+1]
                    j += 2
                else:
                    tmp *= m[i]*m[i+1]
                    i += 2
            elif j+2 <= len(M):
                tmp *= M[j]*M[j+1]
                j += 2
            elif i+2 <= len(m):
                tmp *= m[i]*m[i+1]
                i += 2
            else:
                tmp *= m[i]*M[j]
            K -= 2
        else:
            tmp *= M[j]
            K -= 1
        tmp %= P
else:
    m.sort()
    M.sort()
    for _ in range(K):
        if j+1 <= len(M) and i+1 <= len(m):
            if M[j] < m[i]:
                tmp *= M[j]
                j += 1
            else:
                tmp *= m[i]
                i += 1
        elif j+1 < len(M):
            tmp *= M[j]
            j += 1
        else:
            tmp *= m[i]
            i += 1
        tmp %= P
    tmp *= -1
    tmp %= P
        
print(tmp)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
M = []
m = []
for a in A:
    if a < 0:
        m.append(abs(a))
    else:
        M.append(a)
m.sort(reverse=True)
M.sort(reverse=True)
i = 0
j = 0
tmp = 1
P = 10**9 + 7

if (K%2 == 0 and (len(m)//2*2 + len(M)//2*2) >= K) or (K%2 == 1 and (len(M) + len(m)//2*2) >= K and len(M) > 0):
    while K > 0:
        if K >= 2:
            if j+2 <= len(M) and i+2 <= len(m):
                if m[i]*m[i+1] < M[j]*M[j+1]:
                    tmp *= M[j]*M[j+1]
                    j += 2
                else:
                    tmp *= m[i]*m[i+1]
                    i += 2
            elif j+2 <= len(M):
                tmp *= M[j]*M[j+1]
                j += 2
            elif i+2 <= len(m):
                tmp *= m[i]*m[i+1]
                i += 2
            else:
                tmp *= m[i]*M[j]
            K -= 2
        else:
            if j+1 <= len(M):
                tmp *= M[j]
            else:
                tmp *= m[i] * -1
            K -= 1
        tmp %= P
else:
    m.sort()
    M.sort()
    for _ in range(K):
        if j+1 < len(M) and i+1 < len(m):
            if M[j] < m[i]:
                tmp *= M[j]
                j += 1
            else:
                tmp *= m[i]
                i += 1
        elif j+1 < len(M):
            tmp *= M[j]
            j += 1
        else:
            tmp *= m[i]
            i += 1
        tmp %= P
    tmp *= -1
    tmp %= P
        
print(tmp)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
L = list(map(int,input().split()))
mod = 10**9+7
posi = [x for x in L if x > 0]
nega = [abs(x) for x in L if x < 0]
posi.sort(reverse = True)
nega.sort(reverse = True)
#print(posi,nega)
posi_acc,nega_acc = [],[]
a = 1
for i in range(len(posi)):
    a = a*posi[i]%mod
    posi_acc.append(a)
a = 1
for i in range(len(nega)):
    a = a*nega[i]%mod
    nega_acc.append(a)
#print(posi_acc,nega_acc)

ans = 0
for i in range(k,0,-2):
    com = 1
    if k%2 == 0:
        if i-3 < 0:
            com = posi_acc[i-1]
            
            ans = max(ans,com)
        else:
            com = posi_acc[i-1]*nega_acc[i-3]
            #print(posi_acc[i-1],nega_acc[i-3],i-1,i-2)
    else:
        if i-3>=0:
            com = posi_acc[i-1]*nega_acc[i-2]
            #print(posi_acc[i-1],nega_acc[i-3])
            ans = max(ans,com)
print(ans%mod)

Submission status: Runtime Error

Solution code:
# AFTER EDITORIAL, WIP

def solve(ls, k, debug=0):
    modulo = 10 ** 9 + 7
    n = len(ls)
    n_neg = 0
    for i in range(n):
        n_neg += ls[i] < 0
    n_pos = n - n_neg

    # Sort by abs
    ls_abs = [abs(x) for x in ls]
    ls_abs_argsort = sorted(list(range(n)), key=lambda i: -ls_abs[i])

    # Check if positive result is impossible
    if n_neg >= k:
        positive = (k % 2 == 0) or (n_pos > 0)
    else:
        positive = (n_neg % 2 == 0) or (n > k)

    # If positive is impossible, pick "abs" small ones
    if not positive:
        p = 1
        for i in range(k):
            x = ls[ls_abs_argsort[-1 - i]]
            p *= x
            p %= modulo
        return p

    #
    # If positive is possible, ...
    #
    # PROP.
    #   At most one swap is needed for making product non-negative.
    #   PROOF. TODO
    #

    # Check sign when we pick "abs" large ones
    s = 1
    for i in range(k):
        x = ls[ls_abs_argsort[i]]
        s *= -1 if x < 0 else 1

    # If non-negative, we take as it is
    if s > 0:
        p = 1
        for i in range(k):
            x = ls[ls_abs_argsort[i]]
            p *= x
            p %= modulo
        return p

    # If negative, ...
    #   1. replace small negative with large positive, or
    #   2. replace small positive with large negative
    opt1 = None
    opt2 = None
    neg1, pos1 = None
    pos2, neg2 = None

    # Check if "1" is possible
    swap_pos = None
    for i in range(k, n):
        if ls[ls_abs_argsort[i]] >= 0:
            swap_pos = i
            pos1 = ls[ls_abs_argsort[i]]
            break

    swap_neg = None
    for i in range(k)[::-1]:
        if ls[ls_abs_argsort[i]] < 0:
            swap_neg = i
            neg1 = ls[ls_abs_argsort[i]]
            break

    if swap_pos is not None and swap_neg is not None:
        p = 1
        p *= ls[ls_abs_argsort[swap_pos]]
        p %= modulo
        for i in range(k):
            if i == swap_neg:
                continue
            x = ls[ls_abs_argsort[i]]
            p *= x
            p %= modulo
        opt1 = p

    # Check if "2" is possible
    swap_pos = None
    for i in range(k)[::-1]:
        if ls[ls_abs_argsort[i]] >= 0:
            swap_pos = i
            pos2 = ls[ls_abs_argsort[i]]
            break

    swap_neg = None
    for i in range(k, n):
        if ls[ls_abs_argsort[i]] < 0:
            swap_neg = i
            neg2 = ls[ls_abs_argsort[i]]
            break

    if swap_pos is not None and swap_neg is not None:
        p = 1
        p *= ls[ls_abs_argsort[swap_neg]]
        p %= modulo
        for i in range(k):
            if i == swap_pos:
                continue
            x = ls[ls_abs_argsort[i]]
            p *= x
            p %= modulo
            opt2 = p

    # Pick opt1 or opt2
    if opt1 is None:
        return opt2

    if opt2 is None:
        return opt1

    if pos1 * pos2 > neg1 * neg2:
        return opt1

    return opt2


def main(istr, ostr):
    n, k = list(map(int, istr.readline().strip().split()))
    ls = list(map(int, istr.readline().strip().split()))
    result = solve(ls, k)
    print(result, file=ostr)


if __name__ == "__main__":
    import sys

    main(sys.stdin, sys.stdout)

Submission status: Runtime Error

Solution code:

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

MOD = 10 ** 9 + 7

if K % 2 == 1 and all(v < 0 for v in X):
    # Minimize abs
    X.sort(key=lambda x: -x)
    ans = 1
    for i in range(K):
        ans = ans * X[i] % MOD
    print(ans)
else:
    pos = sorted(v for v in X if v >= 0)
    neg = sorted(-v for v in X if v < 0)

    ans = 1
    if K % 2 == 1:
        ans *= pos.pop()

    cand = []
    while len(pos) >= 2:
        tmp = pos.pop() * pos.pop()
        cand.append(tmp)

    while len(neg) >= 2:
        tmp = neg.pop() * neg.pop()
        cand.append(tmp)

    cand.sort(reverse=True)
    for i in range(K // 2):
        ans = ans * cand[i] % MOD

    print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
lst = list(map(int, input().split()))
lst_abs = sorted(lst, key = abs, reverse = True)
#print("lst_abs", lst_abs)
lst_abs_rev = sorted(lst, key = abs, reverse = False)
#print("lst_abs_rev", lst_abs_rev)
lst_posi = [i for i in lst_abs if i >= 0]
#print("lst_posi", lst_posi)
lst_nega = [i for i in lst_abs if i < 0]
#print("lst_nega", lst_nega)
S = []
ans = 1

if lst_posi == [] and k % 2 == 1:
#	print("only nega and k is odd")
	for i in range(k):
		ans *= lst_abs_rev[i]

else:
	while len(S) != k:
		if len(S) == k - 1:
			S.append(lst_posi.pop(0))
#			print(S)
			
		elif len(lst_posi) < 2:
			if (k - len(S)) % 2 == 1:
				S.append(lst_posi.pop(0))
#				print(S)
				
			else:
				for i in range(k - len(S)):
					S.append(lst_nega.pop(0))
#					print(S)
						
		elif len(lst_nega) < 2:
			if (k - len(S)) % 2 == 1:
				S.append(lst_nega.pop(0))
#				print(S)
			else:
				for i in range(k - len(S)):
					S.append(lst_posi.pop(0))
#					print(S)
		elif lst_posi[0] * lst_posi[1] >= lst_nega[0] * lst_nega[1]:
			S.append(lst_posi.pop(0))
#			print(S)
		
		else:
			S.append(lst_nega.pop(0))
			S.append(lst_nega.pop(0))
#			print(S)

	for i in range(k):
		ans *= S[i]
			
print(ans % (10 ** 9 + 7))

Submission status: Runtime Error

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

a.sort()

ans=1

while k>0:
  if k>=2:
    if a[0]*a[1]>a[-1]*a[-2]:
      ans*=a[0]
      ans%=mod
      ans*a[1]
      ans%=mod
      ans.pop(0)
      ans.pop(0)
      k-=2
    else:
      ans*=a[-1]
      ans%=mod
      a.pop()
      k-=1

  elif k==1:
    ans*=a[-1]
    ans%=mod
    k-=1

print(ans)

Submission status: Runtime Error

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

a.sort()

ans=1

while k>0:
  if k>=2:
    if a[0]*a[1]>a[-1]*a[-2]:
      ans*=a[0]
      ans%=mod
      ans*a[1]
      ans%=mod
      ans.pop(0)
      ans.pop(0)
    else:
      ans*=a[-1]
      ans%=mod
      a.pop()

  elif k==1:
    ans*=a[-1]
    ans%=mod

print(ans)

Submission status: Runtime Error

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

aa=sorted(a,key=abs)
aa.reverse()

if x==0:
  ans=1
  for i in a:
    ans*=(i%mod)
    ans%=mod
    
  print(ans)

else:
  aaa=aa[:k]
  xxx=aa[k:]
  if aaa.count(0)>=1:
    print(0)

  else:
    plus=[]
    minus=[]
    for i in aaa:
      if i>0:
        plus+=[i]
      elif i<0:
        minus+=[i]

    if len(minus)%2==0:
      ans=1
      for j in aaa:
        ans*=(j%mod)
        ans%=mod
      print(ans)

    else:
      if xxx[0]==0:
        print(0)
      else:
        plus2=[]
        minus2=[]
        for j in xxx:
          if j>0:
            plus2+=[j]
          else:
            minus2+=[j]
        if plus2==[]:
          lst=minus+minus2[0]
          ans=1
          for k in lst:
            ans*=(k%mod)
            ans%=mod
          if len(plus)>=2:
            for m in plus[:-1]:
              ans*=(m%mod)
              ans%=mod
          print(ans)
        elif minus2==[]:
          lst=plus+plus2[0]
          for k in lst:
            ans*=(k%mod)
            ans%=mod
          if len(minus)>=2:
            for m in minus[:-1]:
              ans*=(m%mod)
              ans%=mod
          print(ans)
        elif plus[-1]/minus2[0]>minus[-1]/plus2[0]:
          lst=minus+minus2[0]
          ans=1
          for k in lst:
            ans*=(k%mod)
            ans%=mod
          if len(plus)>=2:
            for m in plus[:-1]:
              ans*=(m%mod)
              ans%=mod
          print(ans)

        else:
          lst=plus+plus2[0]
          for k in lst:
            ans*=(k%mod)
            ans%=mod
          if len(minus)>=2:
            for m in minus[:-1]:
              ans*=(m%mod)
              ans%=mod
          print(ans)

Submission status: Runtime Error

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

aa=sorted(a,key=abs)
aa.reverse()

if x==0:
  ans=1
  for i in a:
    ans*=(i%mod)
    ans%=mod
    
  print(ans)

else:
  aaa=aa[:k]
  xxx=aa[k:]
  if aaa.count(0)>=1:
    print(0)

  else:
    plus=[]
    minus=[]
    for i in aaa:
      if i>0:
        plus+=[i]
      elif i<0:
        minus+=[i]

    if len(minus)%2==0:
      ans=1
      for j in aaa:
        ans*=(j%mod)
        ans%=mod
      print(ans)

    else:
      if xxx[0]==0:
        print(0)
      else:
        plus2=[]
        minus2=[]
        for j in xxx:
          if j>0:
            plus2+=[j]
          else:
            minus2+=[j]
        if plus2==[]:
          lst=minus+plus[:-1]+minus2[0]
          ans=1
          for k in lst:
            ans*=(k%mod)
            ans%=mod
          print(ans)
        elif minus2==[]:
          lst=plus+minus[:-1]+plus2[0]
          for k in lst:
            ans*=(k%mod)
            ans%=mod
          print(ans)
        elif plus[-1]/minus2[0]>minus[-1]/plus2[0]:
          lst=minus+plus[:-1]+minus2[0]
          ans=1
          for k in lst:
            ans*=(k%mod)
            ans%=mod
          print(ans)

        else:
          lst=plus+minus[:-1]+plus2[0]
          for k in lst:
            ans*=(k%mod)
            ans%=mod
          print(ans)

Submission status: Runtime Error

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

aa=sorted(a,key=abs)
aa.reverse()

if x==0:
  ans=1
  for i in a:
    ans*=(i%mod)
    ans%=mod
    
  print(ans)

else:
  aaa=aa[:k]
  xxx=aa[k:]
  if aaa.count(0)>=1:
    print(0)

  else:
    plus=[]
    minus=[]
    for i in aaa:
      if i>0:
        plus+=[i]
      elif i<0:
        minus+=[i]

    if len(minus)%2==0:
      ans=1
      for j in aaa:
        ans*=(j%mod)
        ans%=mod
      print(ans)

    else:
      if xxx[0]==0:
        print(0)
      else:
        plus2=[]
        minus2=[]
        for j in xxx:
          if j>0:
            plus2+=[j]
          else:
            minus2+=[j]
        if plus[-1]/minus2[0]>minus[-1]/plus2[0] or plus2==[]:
          lst=minus+plus[:-1]+minus2[0]
          ans=1
          for k in lst:
            ans*=(k%mod)
            ans%=mod
          print(ans)

        else:
          lst=plus+minus[:-1]+plus2[0]
          for k in lst:
            ans*=(k%mod)
            ans%=mod
          print(ans)

Submission status: Runtime Error

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

aa=[]
for i in a:
  aa+=[abs(i)]

a.sort()
aa.sort()

zero=[]
minus=[]
plus=[]

for i in a:
  if i==0:
    zero.append(0)
  elif i>0:
    plus.append(i)
  else:
    minus.append(i)

minus.sort()
plus.sort(reverse=True)


if x==0:
  ans=1
  for i in a:
    ans*=(i%mod)
    ans%=mod
  print(ans)
  exit()

elif x<len(zero):
  print(0)
  exit()

elif x==len(zero):
  if len(minus)%2==1:
    print(0)

else:
  ans=1
  while k>0:
    if len(minus)==0 and len(plus)==0:
      ans=0
      break
    elif k>=2:
      if len(minus)>=2 and minus[0]*minus[1]>=plus[0]*plus[1] or len(plus)<=1:
        ans*=(minus[0]*minus[1])%mod
        ans%=mod

        minus.pop(0)
        minus.pop(0)
      else:
        ans*=(plus[0]%mod)
        ans%=mod
        plus.pop(0)

    elif k==1:
      ans*=(plus[0])%mod
      ans%=mod
      plus.pop(0)
  
  print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
A=sorted(list(map(int,input().split())) , key=abs , reverse=1)
import bisect as bi
# print(A)
# import numpy as np
# G=np.prod(A[:k])
def seki(A,mod=10**9+7):
    now=1
    for i in A:
        i=abs(i)
        if i>0 : now*=i
        # if i<0 : now*=i
        if i==0 :return 0
        if now>0 : now%=mod
    return now

CH=A[:k] 
NO=A[k:]
# G=seki(A[:k])

mod=10**9+7
# if G>=0 : print(G%mod) ; exit()
if sum([A[i]>0 for i in range(n)])==0 and k%2==1:
  print((-seki(A[n-k:]))%mod) ;exit()
if n==k and len(A[bi.bisect_left(A,-1):])%2==1 :
    print((-seki(A))%mod) ;exit()
if sum([i<0 for i in A[:k]])%2==0 :
    print(seki(A[:k])%mod) ; exit()
if 0 in CH:
    print(0);exit()
  
# done=0 ; done2=0
# # ch=-73267983297 ; ch2=-9263498381621
# 
# for i in range(k-1,-1,-1):
#     if A[i]<0 and done==0:
#         for j in range(i+1,n):
#             if A[j]>=0: 
#                 # ch=G*A[j]//A[i]
#                 x1=A[j] ; y1=A[i]
#                 done=1
#                 break
#     if A[i]>0 and done2==0:
#         for j in range(i+1,n):
#             if A[j]<=0: 
#                 # ch2=G*A[j]//A[i]
#                 x2=A[j] ; y2=A[i]
#                 done2=1
#                 break
# #     if done==1 and done2==1 : break
# 
# G=seki(A[:k])
# G=-G
# if done and done2 :
#     if x1 * y2 > x2 * y1 :
#         print((G*x1//y1)%mod)
#     else:
#         print((G*x2//y2)%mod)
# elif done==1:
#     print((G*x1//y1)%mod)
# elif done2==1:
#     print((G*x2//y2)%mod)
        

done=0 ; done2=0
muri,muri2=0,0

# x1,x2,y1,y2=0,0,0,0
if sum([i>=0 for i in NO])==0 : #NO全部-
    muri=1
if sum([i>=0 for i in CH])==0 : #CH全部-
    muri2=1
if sum([i<0 for i in NO])==0: #-を入れられない
    muri2=1
    
if muri==0:
    y1= sorted([i for i in CH if i<0])[-1]
    x1= sorted([i for i in NO if i>=0])[-1]
    done=1
if muri2==0:
    y2= sorted([i for i in CH if i>0])[0]
    x2= sorted([i for i in NO if i<0])[0]
    done2=1

if done and done2:
    if x1 * y2 > x2 * y1 :
        print((-seki(CH)*x1//y1)%mod)
    else:
        print((-seki(CH)*x2//y2)%mod)
elif muri:
    print((-seki(CH)*x2//y2)%mod)
else:
    print((-seki(CH)*x1//y1)%mod)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
A=sorted(list(map(int,input().split())) , key=abs , reverse=1)
import bisect as bi
# print(A)
# import numpy as np
# G=np.prod(A[:k])
def seki(A,mod=10**9+7):
    now=1
    for i in A:
        i=abs(i)
        if i>0 : now*=i
        # if i<0 : now*=i
        if i==0 :return 0
        if now>0 : now%=mod
    return now

CH=A[:k]
NO=A[k:]
# G=seki(A[:k])

mod=10**9+7
# if G>=0 : print(G%mod) ; exit()
if sum([A[i]>0 for i in range(n)])==0 and k%2==1:
  print((-seki(A[n-k:]))%mod) ;exit()
if n==k and len(A[bi.bisect_left(A,-1):])%2==1 :
    print((-seki(A))%mod) ;exit()
if sum([i<0 for i in A[:k]])%2==0 :
    print(seki(A[:k])%mod) ; exit()
if 0 in CH:
    print(0);exit()
  
# done=0 ; done2=0
# # ch=-73267983297 ; ch2=-9263498381621
# 
# for i in range(k-1,-1,-1):
#     if A[i]<0 and done==0:
#         for j in range(i+1,n):
#             if A[j]>=0: 
#                 # ch=G*A[j]//A[i]
#                 x1=A[j] ; y1=A[i]
#                 done=1
#                 break
#     if A[i]>0 and done2==0:
#         for j in range(i+1,n):
#             if A[j]<=0: 
#                 # ch2=G*A[j]//A[i]
#                 x2=A[j] ; y2=A[i]
#                 done2=1
#                 break
# #     if done==1 and done2==1 : break
# 
# G=seki(A[:k])
# G=-G
# if done and done2 :
#     if x1 * y2 > x2 * y1 :
#         print((G*x1//y1)%mod)
#     else:
#         print((G*x2//y2)%mod)
# elif done==1:
#     print((G*x1//y1)%mod)
# elif done2==1:
#     print((G*x2//y2)%mod)
        

done=0 ; done2=0
muri,muri2=0,0

# x1,x2,y1,y2=0,0,0,0
if sum([i>=0 for i in NO])==0 : #NO全部-
    muri=1
if sum([i>=0 for i in CH])==0 : #CH全部-
    muri2=1
if sum([i<=0 for i in NO])==0: #-を入れられない
    muri2=1
    
if muri==0:
    y1= [i for i in CH if i<0][-1]
    x1= [i for i in NO if i>=0][0]
    done=1
if muri2==0:
    y2= [i for i in CH if i>0][-1]
    x2= [i for i in NO if i<=0][0]
    done2=1

if done and done2:
    if x1 * y2 > x2 * y1 :
        print((-seki(CH)*x1//y1)%mod)
    else:
        print((-seki(CH)*x2//y2)%mod)
elif muri:
    print((-seki(CH)*x2//y2)%mod)
else:
    print((-seki(CH)*x1//y1)%mod)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
A=sorted(list(map(int,input().split())) , key=abs , reverse=1)
import bisect as bi
# print(A)
# import numpy as np
# G=np.prod(A[:k])
def seki(A,mod=10**9+7):
    now=1
    for i in A:
        i=abs(i)
        if i>0 : now*=i
        # if i<0 : now*=i
        if i==0 :return 0
        if now>0 : now%=mod
    return now

CH=A[:k]
NO=A[k:]
# G=seki(A[:k])

mod=10**9+7
# if G>=0 : print(G%mod) ; exit()
if sum([A[i]>0 for i in range(n)])==0 and k%2==1:
  print((-seki(A[n-k:]))%mod) ;exit()
if n==k and len(A[bi.bisect_left(A,-1):])%2==1 :
    print((-seki(A))%mod) ;exit()
if sum([i<0 for i in A[:k]])%2==0 :
    print(seki(A[:k])%mod) ; exit()
if 0 in CH:
    print(0);exit()
  
# done=0 ; done2=0
# # ch=-73267983297 ; ch2=-9263498381621
# 
# for i in range(k-1,-1,-1):
#     if A[i]<0 and done==0:
#         for j in range(i+1,n):
#             if A[j]>=0: 
#                 # ch=G*A[j]//A[i]
#                 x1=A[j] ; y1=A[i]
#                 done=1
#                 break
#     if A[i]>0 and done2==0:
#         for j in range(i+1,n):
#             if A[j]<=0: 
#                 # ch2=G*A[j]//A[i]
#                 x2=A[j] ; y2=A[i]
#                 done2=1
#                 break
# #     if done==1 and done2==1 : break
# 
# G=seki(A[:k])
# G=-G
# if done and done2 :
#     if x1 * y2 > x2 * y1 :
#         print((G*x1//y1)%mod)
#     else:
#         print((G*x2//y2)%mod)
# elif done==1:
#     print((G*x1//y1)%mod)
# elif done2==1:
#     print((G*x2//y2)%mod)
        

done=0 ; done2=0
muri,muri2=0,0

# x1,x2,y1,y2=0,0,0,0
if sum([i<0 for i in NO])==n-k : #NO全部-
    muri=1
if sum([i<0 for i in CH])==k : #CH全部-
    muri2=1
if sum([i<0 for i in NO])==0: #-を入れられない
    muri2=1
    
if muri==0:
    y1= [i for i in CH if i<0][-1]
    x1= [i for i in NO if i>0][0]
    done=1
if muri2==0:
    y2= [i for i in CH if i>0][-1]
    x2= [i for i in NO if i<0][0]
    done2=1

if done and done2:
    if x1 * y2 > x2 * y1 :
        print((-seki(CH)*x1//y1)%mod)
    else:
        print((-seki(CH)*x2//y2)%mod)
elif muri:
    print((-seki(CH)*x2//y2)%mod)
else:
    print((-seki(CH)*x1//y1)%mod)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
A=sorted(list(map(int,input().split())) , key=abs , reverse=1)
import bisect as bi
# print(A)
# import numpy as np
# G=np.prod(A[:k])
def seki(A,mod=10**9+7):
    now=1
    for i in A:
        i=abs(i)
        if i>0 : now*=i
        # if i<0 : now*=i
        if i==0 :return 0
        if now>0 : now%=mod
    return now

CH=A[:k]
NO=A[k:]
# G=seki(A[:k])

mod=10**9+7
# if G>=0 : print(G%mod) ; exit()
if sum([A[i]>0 for i in range(n)])==0 and k%2==1:
  print((-seki(A[n-k:]))%mod) ;exit()
if n==k and len(A[bi.bisect_left(A,-1):])%2==1 :
    print((-seki(A))%mod) ;exit()
if sum([i<0 for i in A[:k]])%2==0 :
    print(seki(A[:k])%mod) ; exit()
if 0 in CH:
    print(0);exit()
  
# done=0 ; done2=0
# # ch=-73267983297 ; ch2=-9263498381621
# 
# for i in range(k-1,-1,-1):
#     if A[i]<0 and done==0:
#         for j in range(i+1,n):
#             if A[j]>=0: 
#                 # ch=G*A[j]//A[i]
#                 x1=A[j] ; y1=A[i]
#                 done=1
#                 break
#     if A[i]>0 and done2==0:
#         for j in range(i+1,n):
#             if A[j]<=0: 
#                 # ch2=G*A[j]//A[i]
#                 x2=A[j] ; y2=A[i]
#                 done2=1
#                 break
# #     if done==1 and done2==1 : break
# 
# G=seki(A[:k])
# G=-G
# if done and done2 :
#     if x1 * y2 > x2 * y1 :
#         print((G*x1//y1)%mod)
#     else:
#         print((G*x2//y2)%mod)
# elif done==1:
#     print((G*x1//y1)%mod)
# elif done2==1:
#     print((G*x2//y2)%mod)
        

done=0 ; done2=0
muri,muri2=0,0

# x1,x2,y1,y2=0,0,0,0
if sum([i<0 for i in NO])==n-k : #NO全部-
    muri=1
if sum([i<0 for i in CH])==k : #CH全部-
    if sum([i<0 for i in NO])==0: #-を入れられない
        muri2=1
if muri==0:
    y1= [i for i in CH if i<0][-1]
    x1= [i for i in NO if i>0][0]
    done=1
if muri2==0:
    y2= [i for i in CH if i>0][-1]
    x2= [i for i in NO if i<0][0]
    done2=1

if done and done2:
    if x1 * y2 > x2 * y1 :
        print((-seki(CH)*x1//y1)%mod)
    else:
        print((-seki(CH)*x2//y2)%mod)
elif muri:
    print((-seki(CH)*x2//y2)%mod)
else:
    print((-seki(CH)*x1//y1)%mod)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
A=sorted(list(map(int,input().split())) , key=abs , reverse=1)
import bisect as bi
# print(A)
# import numpy as np
# G=np.prod(A[:k])
def seki(A,mod=10**9+7):
    now=1
    for i in A:
        i=abs(i)
        if i>0 : now*=i
        # if i<0 : now*=i
        if i==0 :return 0
        if now>0 : now%=mod
    return now

CH=A[:k]
NO=A[k:]
# G=seki(A[:k])

mod=10**9+7
# if G>=0 : print(G%mod) ; exit()
if sum([A[i]>0 for i in range(n)])==0 and k%2==1:
  print((-seki(A[n-k:]))%mod) ;exit()
if n==k and len(A[bi.bisect_left(A,-1):])%2==1 :
    print((-seki(A))%mod) ;exit()
if sum([i<0 for i in A[:k]])%2==0 :
    print(seki(A[:k])%mod) ; exit()
if 0 in CH:
    print(0);exit()
  
# done=0 ; done2=0
# # ch=-73267983297 ; ch2=-9263498381621
# 
# for i in range(k-1,-1,-1):
#     if A[i]<0 and done==0:
#         for j in range(i+1,n):
#             if A[j]>=0: 
#                 # ch=G*A[j]//A[i]
#                 x1=A[j] ; y1=A[i]
#                 done=1
#                 break
#     if A[i]>0 and done2==0:
#         for j in range(i+1,n):
#             if A[j]<=0: 
#                 # ch2=G*A[j]//A[i]
#                 x2=A[j] ; y2=A[i]
#                 done2=1
#                 break
# #     if done==1 and done2==1 : break
# 
# G=seki(A[:k])
# G=-G
# if done and done2 :
#     if x1 * y2 > x2 * y1 :
#         print((G*x1//y1)%mod)
#     else:
#         print((G*x2//y2)%mod)
# elif done==1:
#     print((G*x1//y1)%mod)
# elif done2==1:
#     print((G*x2//y2)%mod)
        

done=0 ; done2=0
muri,muri2=0,0

x1,x2,y1,y2=0,0,0,0
if sum([i<0 for i in NO])==n-k : #NO全部-
    muri=1
if sum([i<0 for i in CH])==k : #CH全部-
    muri2=1
if muri==0:
    y1= [i for i in CH if i<0][-1]
    x1= [i for i in NO if i>0][0]
    done=1
if muri2==0:
    y2= [i for i in CH if i>0][-1]
    x2= [i for i in NO if i<0][0]
    done2=1
    
if done and done2:
    if x1 * y2 > x2 * y1 :
        print((-seki(CH)*x1//y1)%mod)
    else:
        print((-seki(CH)*x2//y2)%mod)
elif muri:
    print((-seki(CH)*x2//y2)%mod)
else:
    print((-seki(CH)*x1//y1)%mod)