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:
x = list(input().split())
a = list((int(i) for i in input().split()))
n, k = int(x[0]), int(x[1])

MOD = 10**9 + 7

a_abs = []
for i in range(len(a)):
    a_abs.append(abs(a[i]))

res = sorted(list(zip(a_abs, a)))
val = []
for i in range(len(res)): val.append(res.pop()[1])

# check negative mode
signA, signB, neg = True, False, False
for i in range(len(val)):
    if val[i] < 0:
        signA = not signA
    else:
        signB = True
if (n==k and not signA) or (not signB):
    neg = True # answer will be negative

index_replaced, index_replace = None, None
index_replaced_a, index_replace_a = None, None
if (not neg): # positive
    # take top k
    ans = val[0:k]
    sign = True
    for i in range(len(ans)):
        if ans[i] < 0:
            sign = not sign
            
    # replace if necessary
    if (not sign):
        # find smallest negative
        for i in range(len(ans), 0, -1):
            if ans[i-1] < 0:
                index_replaced = i-1
                break
        # find largest positive
        for i in range(k, len(val)):
            if val[i] > 0:
                index_replace = i
                break
        # find smallest positive
        for i in range(len(ans), 0, -1):
            if ans[i-1] > 0:
                index_replaced_a = i-1
                break
        # find largest negative
        for i in range(k, len(val)):
            print(i, val[i])
            if val[i] < 0:
                index_replace_a = i
                break

        if index_replaced==None or index_replace==None:
            ans[index_replaced_a] = val[index_replace_a]
        elif index_replaced_a==None or index_replace_a==None:
            ans[index_replaced] = val[index_replace]
        elif (ans[index_replaced]/val[index_replace] > 
            ans[index_replaced_a]/val[index_replace_a]):
            ans[index_replaced] = val[index_replace]
        else:
            ans[index_replaced_a] = val[index_replace_a]

    # multiplication
    result = 1
    for i in range(len(ans)):
        result = (result * ans[i]) % MOD

    print(result)

else: # negative case
    ans = val[len(val)-k:]
    result = 1
    for i in range(len(ans)):
        result = (result * ans[i]) % MOD

    print(result)

Submission status: Runtime Error

Solution code:
x = list(input().split())
a = list((int(i) for i in input().split()))
n, k = int(x[0]), int(x[1])

MOD = 10**9 + 7

a_abs = []
for i in range(len(a)):
    a_abs.append(abs(a[i]))

res = sorted(list(zip(a_abs, a)))
val = []
for i in range(len(res)): val.append(res.pop()[1])

# check negative mode
signA, signB, neg = True, False, False
for i in range(len(val)):
    if val[i] < 0:
        signA = not signA
    else:
        signB = True
if (n==k and not signA) or (not signB):
    neg = True # answer will be negative

if (not neg): # positive
    # take top k
    ans = val[0:k]
    sign = True
    for i in range(len(ans)):
        if ans[i] < 0:
            sign = not sign
            
    # replace if necessary
    if (not sign):
        index_replaced, index_replace = 0, 0
        # find smallest negative
        for i in range(k, 0, -1):
            if ans[i] < 0:
                index_replaced = i
                break
        # find largest positive
        for i in range(k, len(val)):
            if val[i] > 0:
                index_replace = i
        ans[index_replaced] = val[index_replace]

    # multiplication
    result = 1
    for i in range(len(ans)):
        result = (result * ans[i]) % MOD

    print(result)

else: # negative case
    ans = val[len(val)-k:]
    result = 1
    for i in range(len(ans)):
        result = (result * ans[i]) % MOD

    print(result)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
al = list(map(int, input().split()))
al_abs = []
al_p = []
al_m = []
zero_cnt = 0
MOD = 10**9+7

for a in al:
    if a == 0:
        zero_cnt+=1
    elif a > 0: 
        al_abs.append((abs(a),1))
    else a < 0: 
        al_abs.append((abs(a),-1))
al_abs.sort(reverse=True)


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


if len(al_abs) < k:
    print(0)
    exit()



# if len(al_abs) == k:
#     ans = 1
#     m_cnt = 0
#     for a,s in al_abs:
#         if s == -1: m_cnt += 1
#         ans *= a
#         ans %= MOD
#     if m_cnt%2 == 0:
#         print(ans)
#     else:
#         print(0)
#     exit()
    

# if not al_p:
#     if k%2 == 1 and zero_cnt > 0:
#         print(0)
#     else:
#         if k%2 == 1:
#             al_abs.sort()
#         ans = 1
#         for a,s in al_abs[:k]:
#             ans *= (a*(-1))
#             ans %= MOD
#         print(ans)
#     exit()



val = 1
m_cnt = 0
last_m = 0
last_p = 0
for i in range(k):
    v,sig = al_abs[i]
    val *= v
    val %= MOD
    if sig == -1:
        m_cnt += 1
        last_m = v
    else:
        last_p = v

if m_cnt %2 == 0:
    print(val)
    exit()


# minus -> plus 

## search next p
next_p = 0
for i in range(k,len(al_abs)):
    v,sig = al_abs[i]
    if sig == 1:
        next_p = v
        break

## search next m
next_m = 0
for i in range(k,len(al_abs)):
    v,sig = al_abs[i]
    if sig == -1:
        next_m = v
        break

if next_p == 0 and next_m == 0:
    print(0)
    exit()

# last_m -> next_p
if next_p*last_p >= next_m*last_m or last_p == 0:
# if next_p > 0 and next_m == 0:
    ans = 1
    skip_flag = False
    for i in range(k):
        v,sig = al_abs[i]
        if not skip_flag and v == last_m and sig == -1:
            ans *= next_p
            skip_flag = True
        else:
            ans *= v
        # print(ans)
        ans %= MOD
    print(ans)
    exit()


# last_p -> next_m
# if next_p == 0 and next_m > 0:
elif next_p*last_p <= next_m*last_m:
    ans = 1
    skip_flag = False
    for i in range(k):
        v,sig = al_abs[i]
        if not skip_flag and v == last_p and sig == 1:
            ans *= next_m
            skip_flag = True
        else:
            ans *= v
        ans %= MOD
    print(ans)
    exit()

Submission status: Runtime Error

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

s = sorted(a,reverse=True)
mod = 10**9 + 7
ans = 1
#全部の要素が負の数であり奇数個の場合は大きい順に取ったものが答え
if k % 2 == 1 and s[0] < 0:
        for i in range(k):
            ans = ans * s[i] % mod
        print(ans)
        exit()　#終了

l = 0
r = n - 1
#次の処理のための計算　（kが奇数だった場合)
#この処理の必要性
#奇数個だった場合三個のうち二つで正の数の最大値を作れば良い
if k % 2 == 1:
  ans = ans * s[l]
  l += 1


for _ in range(k // 2):
  ml = s[l] * s[l + 1]
  mr = s[r] * s[r - 1]
  if ml > mr:
    ans = ans * ml % mod
    l += 2
  else:
    ans = ans * mr % mod
    r -= 2

print(ans)

Submission status: Runtime Error

Solution code:
mod=10**9+7
nums=input().rstrip().split()
num=int(nums[0])
k=int(nums[1])
mai=list(map(int, input().rstrip().split()))
main=sorted(mai,key=abs,reverse=True)
result=1
def findplus_plus(x):
    for i in range(x,len(main)):
        if main[i]>=0:
            return main[i]
    return ''
def findplus_minus(x):
    for i in range(x,len(main)):
        if main[i]<0:
            return main[i]
    return ''
def findminus_plus(x):
    for i in range(x-1,-1,-1):
        if main[i]>=0:
            return main[i]
    return ''
def findminus_minus(x):
    for i in range(x-1,-1,-1):
        if main[i]<0:
            return main[i]
    return ''

if num==k:
    for i in main:
        result*=i
        result%=mod
elif sorted(main)[len(sorted(main))-1]<0:
    if (k%2)==0:
        for i in range(k):
            result*=main[i]
            result%=mod
    else:
        for i in range(len(main)-1,len(main)-1-k,-1):
            result*=main[i]
            result%=mod

else:
    count=0
    for i in range(k):
        if main[i]<0:
            count+=1
        result*=main[i]
        result%=mod
    if (count%2)==1:
        minplus=findminus_plus(k)
        minminus=findminus_minus(k)
        maxplus=findplus_plus(k)
        maxminus=findplus_minus(k)
        if type(maxplus)==type(''):
            plusplus=-1
            minusminus=minminus*maxminus
        else:
            if type(maxminus)==type('') or type(minplus)==type(''):
                minusminus=-1
                plusplus=0
            else:
                minusminus=minminus*maxminus
                plusplus=minplus*maxplus
        if plusplus>minusminus:
            main.remove(minmius)
            result=1
            for i in range(k-1):
                result*=main[i]
                result%=mod
            result*=maxplus
        elif plusplus<=minusminus:
            main.remove(minplus)
            result=1
            for i in range(k-1):
                result*=main[i]
                result%=mod
            result*=maxminus
            
print(result%mod)

Submission status: Runtime Error

Solution code:
nums=input().rstrip().split()
num=int(nums[0])
k=int(nums[1])
mai=list(map(int, input().rstrip().split()))
main=sorted(mai,key=abs,reverse=True)
result=1
def findplus_plus(x):
    for i in range(x,len(main)):
        if main[i]>=0:
            return main[i]
    return ''
def findplus_minus(x):
    for i in range(x,len(main)):
        if main[i]<0:
            return main[i]
    return ''
def findminus_plus(x):
    for i in range(x-1,-1,-1):
        if main[i]>=0:
            return main[i]
    return ''
def findminus_minus(x):
    for i in range(x-1,-1,-1):
        if main[i]<0:
            return main[i]
    return ''
    
if num==k:
    for i in main:
        if i>0:
            result*=i%(10**9+7)
        else:
            result*=i
elif sorted(main)[len(sorted(main))-1]<0:
    if (k%2)==0:
        for i in range(k):
            result*=main[i]
            result%=10**9+7
    else:
        for i in range(len(main)-1,len(main)-1-k,-1):
            result*=main[i]
            result%=10**9+7

else:
    for i in range(k):
        result*=main[i]
    if result<0:
        minplus=findminus_plus(k)
        minminus=findminus_minus(k)
        maxplus=findplus_plus(k)
        maxminus=findplus_minus(k)
        if type(maxplus)==type(''):
            plusplus=-1
            minusminus=minminus*maxminus
        else:
            if type(maxminus)==type('') or type(minplus)==type(''):
                minusminus=-1
                plusplus=0
            else:
                minusminus=minminus*maxminus
                plusplus=minplus*maxplus
        if plusplus>minusminus:
            main.remove(minmius)
            result=1
            for i in range(k-1):
                result*=main[i]
                result%=10**9+7
            result*=maxplus
        elif plusplus<=minusminus:
            main.remove(minplus)
            result=1
            for i in range(k-1):
                result*=main[i]
                result%=10**9+7
            result*=maxminus
            
print(result%(10**9+7))

Submission status: Runtime Error

Solution code:
nums=input().rstrip().split()
num=int(nums[0])
k=int(nums[1])
mai=list(map(int, input().rstrip().split()))
main=sorted(mai,key=abs,reverse=True)
result=1
def findplus_plus(x):
    for i in range(x,len(main)):
        if main[i]>=0:
            return main[i]
    return ''
def findplus_minus(x):
    for i in range(x,len(main)):
        if main[i]<0:
            return main[i]
    return ''
def findminus_plus(x):
    for i in range(x-1,-1,-1):
        if main[i]>=0:
            return main[i]
    return ''
def findminus_minus(x):
    for i in range(x-1,-1,-1):
        if main[i]<0:
            return main[i]
    return ''
    
if num==k:
    for i in main:
        if i>0:
            result*=i%(10**9+7)
        else:
            result*=i
elif sorted(main)[len(sorted(main))-1]<0:
    if (k%2)==0:
        for i in range(k):
            result*=main[i]
    else:
        for i in range(len(main)-1,len(main)-1-k,-1):
            result*=main[i]

else:
    for i in range(k):
        result*=main[i]
    if result<0:
        minplus=findminus_plus(k)
        minminus=findminus_minus(k)
        maxplus=findplus_plus(k)
        maxminus=findplus_minus(k)
        if type(maxplus)==type(''):
            plusplus=-1
            minusminus=minminus*maxminus
        else:
            if type(maxminus)==type('') or type(minplus)==type(''):
                minusminus=-1
                plusplus=0
            else:
                minusminus=minminus*maxminus
                plusplus=minplus*maxplus
        if plusplus>minusminus:
            result=int(result*maxplus/minminus)
        elif plusplus<=minusminus:
            result=int(result*maxminus/minplus)
            
print(result%(10**9+7))

Submission status: Runtime Error

Solution code:
nums=input().rstrip().split()
num=int(nums[0])
k=int(nums[1])
mai=list(map(int, input().rstrip().split()))
main=sorted(mai,key=abs,reverse=True)
result=1
def findplus_plus(x):
    for i in range(x,len(main)):
        if main[i]>=0:
            return main[i]
    return ''
def findplus_minus(x):
    for i in range(x,len(main)):
        if main[i]<0:
            return main[i]
    return ''
def findminus_plus(x):
    for i in range(x-1,-1,-1):
        if main[i]>=0:
            return main[i]
    return ''
def findminus_minus(x):
    for i in range(x-1,-1,-1):
        if main[i]<0:
            return main[i]
    return ''
    
if num==k:
    for i in main:
        result*=i
elif sorted(main)[len(sorted(main))-1]<0:
    if (k%2)==0:
        for i in range(k):
            result*=main[i]
    else:
        for i in range(len(main)-1,len(main)-1-k,-1):
            result*=main[i]

else:
    for i in range(k):
        result*=main[i]
    if result<0:
        if type(findplus_plus(k))==type(''):
            plusplus=-1
            minusminus=findminus_minus(k)*findplus_minus(k)
        else:
            if type(findplus_minus(k))==type('') or type(findminus_plus(k))==type(''):
                minusminus=-1
                plusplus=0
            else:
                minusminus=findminus_minus(k)*findplus_minus(k)
                plusplus=findplus_plus(k)*findminus_plus(k)
        if plusplus>minusminus:
            result=int(result*findplus_plus(k)/findminus_minus(k))
        elif plusplus<=minusminus:
            result=int(result*findplus_minus(k)/findminus_plus(k))
            
print(result%(10**9+7))

Submission status: Runtime Error

Solution code:
nums=input().rstrip().split()
num=int(nums[0])
k=int(nums[1])
mains=input().rstrip().split()
mai=[]
for i in range(len(mains)):
    mai.append(int(mains[i]))
main=sorted(mai,key=abs,reverse=True)
result=1
def findplus_plus(x):
    for i in range(x,len(main)):
        if main[i]>=0:
            return main[i]
    return ''
def findplus_minus(x):
    for i in range(x,len(main)):
        if main[i]<0:
            return main[i]
    return ''
def findminus_plus(x):
    for i in range(x-1,-1,-1):
        if main[i]>=0:
            return main[i]
    return ''
def findminus_minus(x):
    for i in range(x-1,-1,-1):
        if main[i]<0:
            return main[i]
    return ''
    
if num==k:
    for i in main:
        result*=i
elif sorted(main)[len(sorted(main))-1]<0:
    if (k%2)==0:
        for i in range(k):
            result*=main[i]
    else:
        for i in range(len(main)-1,len(main)-1-k,-1):
            result*=main[i]

else:
    for i in range(k):
        result*=main[i]
    if result<0:
        if type(findplus_plus(k))==type(''):
            plusplus=-1
            minusminus=findminus_minus(k)*findplus_minus(k)
        else:
            if type(findplus_minus(k))==type('') or type(findminus_plus(k))==type(''):
                minusminus=-1
                plusplus=0
            else:
                minusminus=findminus_minus(k)*findplus_minus(k)
                plusplus=findplus_plus(k)*findminus_plus(k)
        if plusplus>minusminus:
            result=int(result*findplus_plus(k)/findminus_minus(k))
        elif plusplus<=minusminus:
            result=int(result*findplus_minus(k)/findminus_plus(k))
            
print(result%(10**9+7))

Submission status: Runtime Error

Solution code:
nums=input().rstrip().split()
num=int(nums[0])
k=int(nums[1])
mains=input().rstrip().split()
mai=[]
for i in range(len(mains)):
    mai.append(int(mains[i]))
main=sorted(mai,key=abs,reverse=True)
result=1
def findplus_plus(x):
    for i in range(x,len(main)):
        if main[i]>=0:
            return main[i]
    return -1
def findplus_minus(x):
    for i in range(x,len(main)):
        if main[i]<0:
            return main[i]
    return 0
def findminus_plus(x):
    for i in range(x-1,-1,-1):
        if main[i]>=0:
            return main[i]
    return 0
def findminus_minus(x):
    for i in range(x-1,-1,-1):
        if main[i]<0:
            return main[i]
    return 0
    
if num==k:
    for i in main:
        result*=i
elif sorted(main)[len(sorted(main))-1]<0:
    if (k%2)==0:
        for i in range(k):
            result*=main[i]
    else:
        for i in range(len(main)-1,len(main)-1-k,-1):
            result*=main[i]

else:
    for i in range(k):
        result*=main[i]
    if result<0:
        if findplus_plus(k)==-1:
            plusplus=-1
            minusminus=findminus_minus(k)*findplus_minus(k)
        else:
            plusplus=findplus_plus(k)*findminus_plus(k)
            if findplus_minus(k)==0 or findminus_plus(k)==0:
                minusminus=-1
            else:
                minusminus=findminus_minus(k)*findplus_minus(k)
        if plusplus>minusminus:
            result=int(result*findplus_plus(k)/findminus_minus(k))
        elif plusplus<=minusminus:
            result=int(result*findplus_minus(k)/findminus_plus(k))
            
print(result%(10**9+7))

Submission status: Runtime Error

Solution code:
import sys
import heapq

mod = 10**9 + 7

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

A = list(map(int,input().split()))

P = []
Q = []
Z = []

for i in range(N):
    if A[i] > 0:
        P.append(A[i])
    elif A[i] < 0:
        Q.append(A[i])
    else:
        Z.append(0)

if len(P)+len(Q) < K:
    print(0)
    sys.exit(0)

elif K == 1:
    if P:
        P.sort(reverse = True)
        print(P[0])
    elif Z:
        print(0)
    else:
        Q.sort(reverse = True)
        print(Q[0])

elif P != []:
    ###正の値にすることができる
    ###絶対値が大きいものを貪欲にとる
    ###負になってしまった場合は、1つ交換する
    ###交換できない場合は、個数がギリギリでどうやっても負になってしまう
    ###Z=Trueなら0, Falseならその負の値が答え

    P = list(map(lambda x:-x,P))
    ###P,Q共に負の値が入っていることに注意

    heapq.heapify(P)
    heapq.heapify(Q)
    v = 1
    sgn = 1
    for i in range(K-2):
        if P and Q:
            if P[0] < Q[0]:
                v *= - heapq.heappop(P)
                v %= mod
            else:
                v *= - heapq.heappop(Q)
                v %= mod
                sgn *= -1
        elif P:
            v *= - heapq.heappop(P)
            v %= mod
        elif Q:
            v *= - heapq.heappop(Q)
            v %= mod
            sgn *= -1
    
    can = []
    if Z:
        can.append(0)
    if len(P) >= 2:
        a = heapq.heappop(P)
        can.append(a*P[0])
    if len(Q) >= 2:
        b = heapq.heappop(Q)
        can.append(b*Q[0])
    if P != [] and Q != []:
        can.append(- P[0]*Q[0])
    
    if sgn == 1:
        print((v*max(can))%mod)
    else:
        print((-v*min(can))%mod)


else:
    if K % 2 == 0:
        ans = 1
        heapq.heapify(Q)
        for i in range(K):
            ans *= heaq.heappop(Q)
            ans %= mod
        print(ans)
    elif Z:
        print(0)
    else:
        Q.sort(reverse = True)
        ans = 1
        for i in range(K):
            ans *= Q[i]
            ans %= mod
        print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
lst=list(map(int,input().split()))
lst=sorted(lst)
mult=1
mod=10**9+7
if(lst[0]>0):
    i=1
    while(i<=k):
        mult*=lst[-i]
        i+=1

    print(mult % mod)
elif(lst[-1]<0):
    i=1
    while (i <= k):
        mult *= lst[-i]
        i += 1

    print(mult % mod)

else:
    i=1
    while (i <= k):
        mult *= lst[-i]
        i += 1
    i=0
    j=k
    check=0
   
    while(True):
        newmult=mult//(lst[-j]*lst[-j+1])
        if(lst[i+1]<0):
            newmult=newmult*lst[i]*lst[i+1]
        else:
            break
        if(newmult<=mult):
            break
        check+=2
        
        mult=newmult
        if (check == k or k - check == 1):
            break

        i+=2
        j-=2

    print(mult%mod)

Submission status: Runtime Error

Solution code:
import math
from operator import itemgetter

ABS = 0
VAL = 1
BIG_NUM = math.pow(10, 9) + 7

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

A = []
isMinusOnly = True
for i in range(len(tmp)):
    A.append([
        abs(tmp[i]),
        tmp[i]
    ])
    if tmp[i] > 0:
        isMinusOnly = False
result = 1
if len(A) == K:
    for i in range(K):
        result *= A[i][VAL]
        result %= BIG_NUM
    print(int(result))
elif K % 2 == 1 and isMinusOnly:
    A = sorted(A, key=itemgetter(ABS))
    for i in range(K):
        result *= A[i][VAL]
        result %= BIG_NUM
    print(int(result))
else:
    A = sorted(A, key=itemgetter(ABS, VAL), reverse=True)
    willPositive = None
    for i in range(K):
        isPositive = A[i][VAL] > 0
        if willPositive == None:
            willPositive = isPositive
        elif willPositive == isPositive:
            willPositive = True
        else:
            willPositive = False
    if willPositive:
        for i in range(K):
            result *= A[i][VAL]
            result %= BIG_NUM
        print(int(result))
    else:
        A_former = A[:K]
        A_later = A[K:]
        f_found_minus = False
        f_found_plus = False
        i = K
        for a in list(reversed(A_former)):
            i -= 1
            if not f_found_minus and a[VAL] < 0:
                last_minus = i
                f_found_minus = True
            if not f_found_plus and a[VAL] > 0:
                last_plus = i
                f_found_plus = True
            if f_found_minus and f_found_plus:
                break
        l_found_minus = False
        l_found_plus = False
        i = K - 1
        for a in A_later:
            i += 1
            if not l_found_minus and a[VAL] < 0:
                first_minus = i
                l_found_minus = True
            if not l_found_plus and a[VAL] > 0:
                first_plus = i
                l_found_plus = True
            if l_found_minus and l_found_plus:
                break
        # Kまでマイナスのみの場合 || 残りにマイナスがない場合
        if not f_found_plus or not l_found_minus:
            remove_i = last_minus
            add_i = first_plus
        # 残りがマイナスのみの場合
        elif not l_found_plus:
            remove_i = last_plus
            add_i = first_minus
        elif A[first_minus][ABS] / A[last_plus][ABS] > A[first_plus][ABS] / A[last_minus][ABS]:
            remove_i = last_plus
            add_i = first_minus
        else:
            remove_i = last_minus
            add_i = first_plus
        for i in range(K):
            if i != remove_i:
                result *= A[i][VAL]
                result %= BIG_NUM
        result *= A[add_i][VAL]
        result %= BIG_NUM
        print(int(result))

Submission status: Runtime Error

Solution code:
nums=input().rstrip().split()
num=int(nums[0])
k=int(nums[1])
mains=input().rstrip().split()
mai=[]
for i in range(len(mains)):
    mai.append(int(mains[i]))
main=sorted(mai,key=abs,reverse=True)
result=1
def findplus_plus(x):
    for i in range(x,len(main)-1):
        if main[i]>=0:
            return main[i]
    return -1
def findplus_minus(x):
    for i in range(x,len(main)-1):
        if main[i]<0:
            return main[i]
    return 0
def findminus_plus(x):
    for i in range(x-1,-1,-1):
        if main[i]>=0:
            return main[i]
    return -1
def findminus_minus(x):
    for i in range(x-1,-1,-1):
        if main[i]<0:
            return main[i]
    return 0
    
if num==k:
    for i in main:
        result*=i
elif sorted(main)[len(sorted(main))-1]<0:
    if (k%2)==0:
        for i in range(k):
            result*=main[i]
    else:
        for i in range(len(main)-1,len(main)-1-k,-1):
            result*=main[i]
else:
    for i in range(k):
        result*=main[i]
    if result<0:
        if findplus_plus(k)==-1:
            plusplus=0
            minusminus=findminus_minus(k)*findplus_minus(k)
        else:
            plusplus=findplus_plus(k)*findminus_plus(k)
            if findplus_minus(k)==0 or findminus_plus(k)==-1:
                minusminus=0
            else:
                minusminus=findminus_plus(k)*findminus_minus(k)
        if plusplus>minusminus:
            result=int(result*findplus_plus(k)/findminus_minus(k))
        elif plusplus<=minusminus:
            result=int(result*findplus_minus(k)/findminus_plus(k))
print(result%(10**9+7))

Submission status: Runtime Error

Solution code:
nums=input().rstrip().split()
num=int(nums[0])
k=int(nums[1])
mains=input().rstrip().split()
mai=[]
for i in range(len(mains)):
    mai.append(int(mains[i]))
main=sorted(mai,key=abs,reverse=True)
result=1
def findplus_plus(x):
    for i in range(x,len(main)-1):
        if main[i]>=0:
            return main[i]
    return -1
def findplus_minus(x):
    for i in range(x,len(main)-1):
        if main[i]<0:
            return main[i]
    return 0
def findminus_plus(x):
    for i in range(x-1,-1,-1):
        if main[i]>=0:
            return main[i]
    return -1
def findminus_minus(x):
    for i in range(x-1,-1,-1):
        if main[i]<0:
            return main[i]
    return 0
    
if num==k:
    for i in main:
        result*=i
elif sorted(main)[len(sorted(main))-1]<0:
    if (k%2)==0:
        for i in range(k):
            result*=main[i]
    else:
        for i in range(len(main)-1,len(main)-1-k,-1):
            result*=main[i]
else:
    for i in range(k):
        result*=main[i]
    if result<0:
        if findplus_plus(k)==-1:
            result1=0
            result2=int(result*findplus_minus(k)/findminus_plus(k))
        else:
            result1=int(result*findplus_plus(k)/findminus_minus(k))
            if findplus_minus(k)==0 or findminus_plus(k)==-1:
                result2=0
            else:
                result2=int(result*findplus_minus(k)/findminus_plus(k))
        result=max[result1,result2]
print(result%(10**9+7))

Submission status: Runtime Error

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, K = mapint()
As = list(mapint())
mod = 10**9+7

pos = []
neg = []
for a in As:
    if a>=0:
        pos.append(a)
    else:
        neg.append(a)
pos.sort(reverse=True)
neg.sort(reverse=True)
ans = 1
lenp, lenn = len(pos), len(neg)
if (lenp+lenn)==K:
    for a in As:
        ans *= a
        ans %= mod
    print(ans)
elif lenn==0:
    for i in range(K):
        ans *= pos[i]
        ans %= mod
    print(ans)
elif lenp==0:
    if K%2==0:
        for i in range(K):
            ans *= neg[i]
            ans %= mod
        print(ans)
    else:
        for i in range(K-1, -1, -1):
            ans *= neg[i]
            ans %= mod
        print(ans)        
else:
    p, n = 0, 0
    while p+n+1<K:
        if p+2<lenp and n+2<lenn:
            if pos[p]*pos[p+1]>=neg[n]*neg[n+1]:
                ans *= pos[p]
                p += 1
            else:
                ans *= neg[n]*neg[n+1]
                n += 2
        elif n+2<lenn:
            ans *= neg[n]*neg[n+1]
            n += 2
        else:
            ans *= pos[p]
            p += 1
        ans %= mod
    if p+n<K:
        ans *= pos[p]
        ans %= mod
    print(ans)

Submission status: Runtime Error

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, K = mapint()
As = list(mapint())
mod = 10**9+7

pos = []
neg = []
for a in As:
    if a>=0:
        pos.append(a)
    else:
        neg.append(a)
pos.sort(reverse=True)
neg.sort(reverse=True)
ans = 1
lenp, lenn = len(pos), len(neg)
if (lenp+lenn)==K:
    for a in As:
        ans *= a
        ans %= mod
    print(ans)
elif lenn==0:
    for i in range(K):
        ans *= pos[i]
        ans %= mod
    print(ans)
elif lenp==0:
    if K%2==0:
        for i in range(K):
            ans *= neg[i]
            ans %= mod
        print(ans)
    else:
        for i in range(K-1, -1, -1):
            ans *= neg[i]
            ans %= mod
        print(ans)        
else:
    cur = 0
    p, n = 0, 0
    while p+n+1<K:
        if p+2<=lenp and n+2<=lenn:
            if pos[p]*pos[p+1]>=neg[n]*neg[n+1]:
                ans *= pos[p]
                p += 1
            else:
                ans *= neg[n]*neg[n+1]
                n += 2
        elif n+2<=lenn:
            ans *= neg[n]*neg[n+1]
            n += 2
        else:
            ans *= pos[p]
            p += 1
        ans %= mod
    if p+n<K:
        ans *= pos[p+1]
        ans %= mod
    print(ans)

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 nlm != len(lal) and 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 nlm != len(lal) and mmr < 0:
          lal[nlm] = mmr
        print(prod_mod(lal, MAX))

if __name__=='__main__':
  main()