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:
import functools

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

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

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

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

Submission status: Runtime Error

Solution code:
import functools

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

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

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

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

Submission status: Runtime Error

Solution code:
import functools

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

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

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

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

Submission status: Runtime Error

Solution code:
import functools

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

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

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

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

Submission status: Runtime Error

Solution code:
import functools

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

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

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

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

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
l = list(map(int, input().split()))
l.sort(key=lambda x:abs(x), reverse=True)
a = 10**9 + 7

if N == K:
    m = 1
    for i in range(K):
        m = m * l[i]
    print(m%a)

elif K == 1:
    print(max(l)%a)

else:
    lp = []
    lm = []
    m = 1
    for i in range(K):
        m = m * l[i]
        if l[i] < 0:
            lm += [l[i]]
        else:
            lp += [l[i]]

    if m > 0:
        print(m%a)
    else:
        restp = []
        restm = []
        for i in range(K, N):
            if l[i] < 0:
                restm += [l[i]]
            else:
                restp += [l[i]]

        if len(lp) == 0:
            if len(restp) > 0:
                m2 = 1
                lm.pop()
                for v in lp:
                    m2 = m2 * v
                for v in lm:
                    m2 = m2 * v
                m2 = m2 * restp[0]   
                print(m2%a)             
            else:
                l.sort(reverse=True)
                m = 1
                for i in range(K):
                    m = m * l[i]
                print(m%a)
        else:               
            if len(restm) > 0:
                m1 = 1
                lp.pop()
                for v in lp:
                    m1 = m1 * v
                for v in lm:
                    m1 = m1 * v
                m1 = m1 * restm[0]

            if len(restp) > 0:
                m2 = 1
                lm.pop()
                for v in lp:
                    m2 = m2 * v
                for v in lm:
                    m2 = m2 * v
                m2 = m2 * restp[0]

            if m1 and m2:
                if m1 > m2:
                    print(m1%a)
                else:
                    print(m2%a)
            elif m1:
                print(m1%a)
            else:
                print(m2%a)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
l = list(map(int, input().split()))
l.sort(key=lambda x:abs(x), reverse=True)
a = 10**9 + 7

if N == K:
    m = 1
    for i in range(K):
        m = m * l[i]
    print(m%a)

else:
    lp = []
    lm = []
    m = 1
    for i in range(K):
        m = m * l[i]
        if l[i] < 0:
            lm += [l[i]]
        else:
            lp += [l[i]]

    if m > 0:
        print(m%a)
    else:
        restp = []
        restm = []
        for i in range(K, N):
            if l[i] < 0:
                restm += [l[i]]
            else:
                restp += [l[i]]

        if len(lp) == 0:
            if len(restp) > 0:
                m2 = 1
                lm.pop()
                for v in lp:
                    m2 = m2 * v
                for v in lm:
                    m2 = m2 * v
                m2 = m2 * restp[0]   
                print(m2%a)             
            else:
                l.sort(reverse=True)
                m = 1
                for i in range(K):
                    m = m * l[i]
                print(m%a)
        else:               
            if len(restm) > 0:
                m1 = 1
                lp.pop()
                for v in lp:
                    m1 = m1 * v
                for v in lm:
                    m1 = m1 * v
                m1 = m1 * restm[0]

            if len(restp) > 0:
                m2 = 1
                lm.pop()
                for v in lp:
                    m2 = m2 * v
                for v in lm:
                    m2 = m2 * v
                m2 = m2 * restp[0]

            if m1 and m2:
                if m1 > m2:
                    print(m1%a)
                else:
                    print(m2%a)
            elif m1:
                print(m1%a)
            else:
                print(m2%a)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
l = list(map(int, input().split()))
l.sort(key=lambda x:abs(x), reverse=True)
a = 10**9 + 7

if N == K:
    m = 1
    for i in range(K):
        m = m * l[i]
    print(m%a)

else:
    lp = []
    lm = []
    m = 1
    for i in range(K):
        m = m * l[i]
        if l[i] < 0:
            lm += [l[i]]
        else:
            lp += [l[i]]
    restp = []
    restm = []
    for i in range(K, N):
        if l[i] < 0:
            restm += [l[i]]
        else:
            restp += [l[i]]

    if m > 0:
        print(m%a)
    else:
        if len(restm) > 0:
            m1 = 1
            lp.pop()
            for v in lp:
                m1 = m1 * v
            for v in lm:
                m1 = m1 * v
            m1 = m1 * restm[0]

        if len(restp) > 0:
            m2 = 1
            lm.pop()
            for v in lp:
                m2 = m2 * v
            for v in lm:
                m2 = m2 * v
            m2 = m2 * restp[0]

        if m1 and m2:
            if m1 > m2:
                print(m1%a)
            else:
                print(m2%a)
        elif m1:
            print(m1%a)
        else:
            print(m2%a)

Submission status: Runtime Error

Solution code:
import functools

def sign(x):
    if x>0:
        return 1
    elif x==0:
        return 0
    else:
        return -1

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

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

    for i in range(K):
        if abs(A[0]) > abs(A[-1]):
            combi.insert(0,A.pop(0))
        else:
            combi.append(A.pop(-1))

    signs = functools.reduce(lambda x,y: x*y,map(sign,combi))

    if signs == -1:
        if combi[-1] <0:
            del combi[0]
            combi.append(A[-1])
        else:
            if A[0]>0:
                del combi[0]
                combi.append(A[-1])
            elif A[0]<0 and A[-1] >0:
                if combi[0]*A[0] > combi[-1]*A[-1]:
                    del combi[-1]
                    combi.insert(0,A[0])
                else:
                    del combi[0]
                    combi.append(A[-1])
            else:
                del combi[-1]
                combi.insert(0,A[0])

    answer =  functools.reduce(lambda x,y: x*y%(10**9+7),map(abs,combi))
print(answer)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
MOD=10**9+7
A=list(map(int,input().split()))
A.sort(reverse=True,key=lambda x:abs(x))

if max(A)<=0:
  """
  if A[K-1]==0:
    print(0)
    exit()
  """
  ans=1
  A.sort(reverse=True)
  for i in range(0,K):
    ans*=A[i]
    ans%=MOD
  ans+=MOD
  print(ans%MOD)
  exit()

ans=1
Flag=True
for i in range(0,K):
  if A[i]<0:
    Flag= not Flag
  ans*=A[i]
  ans%=MOD
if Flag or K==N or ans==0:
  ans+=MOD
  print(ans%MOD)
  exit()
elif A[K]==0:
  print(0)
  exit()

mainA=A[:K]
restA=A[K:]
#print(mainA)

#数えている正の中で絶対値最小
minP,ind_minP=-1,-1
if max(mainA)>0:
  minP=min(map(lambda x:x if x>0 else float("inf"),mainA))
  ind_minP=mainA.index(minP)
  #print(minP)
#数えている負の中で絶対値最小
minM,ind_minM=1,-1
if min(mainA)<0:
  minM=-min(map(lambda x:abs(x) if x<0 else float("inf"),mainA))
  ind_minM=mainA.index(minM)
  #print(minM)

#数えていない正の中で絶対値最大
maxrestP=max(max(restA),-1)
#数えていない負の中で絶対値最大
maxrestM=min(min(restA),1)
if not(maxrestM<=-1 and minP>=1):#残りが全て負か0
  mainA.pop[ind_minM]
  mainA.append(maxrestP)
elif not(maxrestP>=1 and minM<= -1):
  mainA.pop[ind_minP]
  mainA.append(maxrestM)
else:
  if maxrestP*minP>maxrestM*minM:
    mainA.pop(ind_minM)
    mainA.append(maxrestP)
  else:
    mainA.pop(ind_minP)
    mainA.append(maxrestM)
  
ans=1
for i in range(0,K):
  ans*=mainA[i]
  ans%=MOD

ans+=MOD
print(ans%MOD)
exit()

Submission status: Runtime Error

Solution code:


MOD = 10**9 + 7
n, k = map(int, input().split())
a = list(map(int, input().split()))
plus,minus=[],[]
for i in a:
    if i<0:
        minus.append(i)
    else:
        plus.append(i)
plus.sort(reverse = True)
minus.sort()
plus.append(-float('inf'))
plus.append(float('inf'))
minus.append(float('inf'))
minus.append(-float('inf'))
ans = 1
if n == k:
    for i in range(n):
        ans *= a[i]
        ans %= MOD
    print(ans)
elif len(plus) == 2 and k % 2 == 1:
    for i in range(3, k + 3):
        ans *= minus[-i]
        ans %= MOD
    print(ans)
elif k == n - 1:
    if len(minus) % 2 == 1:
        for i in range(len(minus) - 3):
            ans *= minus[i]
            ans %= MOD
        for i in range(len(plus) - 2):
            ans *= plus[i]
            ans %= MOD
    else:
        for i in range(len(minus) - 2):
            ans *= minus[i]
            ans %= MOD
        for i in range(len(plus) - 3):
            ans *= plus[i]
            ans %= MOD
    print(ans)
else:
    cplus = 0
    cminus = 0
    p = k // 2
    if k % 2 == 1:
        ans *= plus[0]
        cplus = 1
    else:
        for _ in range(p):
            if plus[cplus] * plus[cplus + 1] > minus[cminus] * minus[cminus]:
                ans *= plus[cplus] * plus[cplus + 1]
                ans %= MOD
                cplus += 2
            else:
                ans *= minus[cminus] * minus[cminus]
                ans %= MOD
                cminus += 2
    print(ans)

Submission status: Runtime Error

Solution code:


MOD = 10**9 + 7
n, k = map(int, input().split())
a = list(map(int, input().split()))
plus,minus=[],[]
for i in a:
    if i<0:
        minus.append(i)
    else:
        plus.append(i)
plus.sort(reverse = True)
minus.sort()
plus.append(-float('inf'))
plus.append(float('inf'))
minus.append(float('inf'))
minus.append(-float('inf'))
ans = 1
if n == k:
    for i in range(n):
        ans *= a[i]
        ans %= MOD
    print(ans)
elif len(plus) == 2 and k % 2 == 1:
    for i in range(3, k + 3):
        ans *= minus[-i]
        ans %= MOD
    print(ans)
else:
    cplus = 0
    cminus = 0
    p = k // 2
    if k % 2 == 1:
        ans *= plus[0]
        cplus = 1
    else:
        for _ in range(p):
            if plus[cplus] * plus[cplus + 1] > minus[cminus] * minus[cminus]:
                ans *= plus[cplus] * plus[cplus + 1]
                ans %= MOD
                cplus += 2
            else:
                ans *= minus[cminus] * minus[cminus]
                ans %= MOD
                cminus += 2
    print(ans)

Submission status: Runtime Error

Solution code:


MOD = 10**9 + 7
n, k = map(int, input().split())
a = list(map(int, input().split()))
plus,minus=[],[]
for i in a:
    if i<0:
        minus.append(i)
    else:
        plus.append(i)
plus.sort(reverse = True)
minus.sort()
plus.append(-float('inf'))
plus.append(float('inf'))
minus.append(float('inf'))
minus.append(-float('inf'))
ans = 1
if n == k:
    for i in range(n):
        ans *= a[i]
        ans %= MOD
    print(ans)
elif len(plus) == 2 and k % 2 == 1:
    for i in range(3, k + 3):
        ans *= minus[-i]
        ans %= MOD
    print(ans)
else:
    cplus = 0
    cminus = 0
    p = k // 2
    if k % 2 == 1:
        ans *= plus[0]
        cplus = 1
    for _ in range(p):
        if plus[cplus] * plus[cplus + 1] > minus[cminus] * minus[cminus]:
            ans *= plus[cplus] * plus[cplus + 1]
            ans %= MOD
            cplus += 2
        else:
            ans *= minus[cminus] * minus[cminus]
            ans %= MOD
            cminus += 2
    print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
N,K = map(int, input().split())
p = int(10 ** 9 + 7)
A = list(map(int, input().split()))
A_np = np.array(A)
A_pos = A_np[A_np>=0]
A_neg = A_np[A_np<0]
judge = False
s = 1
if len(A_pos) > 0:
  if N > K:
    judge = True
  else:
    if len(A_neg) % 2 == 0:
      judge = True    
else:
  if (K % 2) == 0:
    judge = True
if judge == True:
  A_pos = np.sort(A_pos)[::-1]
  A_neg = np.sort(A_neg)
  if (K % 2) == 1:
    s = A_pos[0]
    A_pos = A_pos[1:]
    K = K - 1
  K = int(K / 2)
  print(A_pos)
  print(A_neg)
  stack = np.array([])
  num = min(K,int(len(A_pos)/2))
  for n in range(0,num):
    stack = np.append(stack, int(A_pos[2*n]*A_pos[2*n+1]) % p)
  num = min(K,int(len(A_neg)/2))
  for n in range(0,num):
    stack = np.append(stack, int(A_neg[2*n]*A_neg[2*n+1]) % p)
  stack = np.sort(stack)[::-1]
  for n in range(0,K):
    s = int(s * stack[n]) % p
else:
  Index = np.sort(abs(A_np))
  A_sort = A_np[Index-1]
  for n in range(0,K):
    s = int(s * A_sort[n]) % p
print(int(s))

Submission status: Runtime Error

Solution code:
import numpy as np
N,K = map(int, input().split())
p = 10 ** 9 + 7
A = list(map(int, input().split()))
A_np = np.array(A)
A_pos = A_np[A_np>=0]
A_neg = A_np[A_np<0]
judge = False
s = 1
if len(A_pos) > 0:
  if N > K:
    judge = True
  else:
    if len(A_neg) % 2 == 0:
      judge = True    
else:
  if (K % 2) == 0:
    judge = True
if judge == True:
  A_pos = np.sort(A_pos)[::-1]
  A_neg = np.sort(A_neg)
  if (K % 2) == 1:
    s = A_pos[0]
    A_pos = A_pos[1:]
    K = K - 1
  K = int(K / 2)
  stack = np.array([])
  num = min(K,int(len(A_pos)/2))
  for n in range(0,num):
    stack = np.append(stack, (A_pos[2*n]*A_pos[2*n+1]) % p)
  num = min(K,int(len(A_neg)/2))
  for n in range(0,num):
    stack = np.append(stack, (A_neg[2*n]*A_neg[2*n+1]) % p)
  stack = np.sort(stack)[::-1]
  for n in range(0,K):
    s = (s * stack[n]) % p
else:
  Index = np.sort(abs(A_np))
  A_sort = A_np[Index-1]
  for n in range(0,K):
    s = (s * A_sort[n]) % p
print(int(s))

Submission status: Runtime Error

Solution code:
import numpy as np
N,K = map(int, input().split())
p = 10 ** 9 + 7
A = list(map(int, input().split()))
A_np = np.array(A)
A_pos = A_np[A_np>=0]
A_neg = A_np[A_np<0]
judge = False
s = 1
if len(A_pos) > 0:
  if N > K:
    judge = True
  else:
    if len(A_neg) % 2 == 0:
      judge = True    
else:
  if (K % 2) == 0:
    judge = True
if judge == True:
  A_pos = np.sort(A_pos)[::-1]
  A_neg = np.sort(A_neg)
  if (K % 2) == 1:
    s = A_pos[0]
    A_pos = A_pos[1:]
    K = K - 1
  K = int(K / 2)
  stack = np.array([])
  for n in range(0,int(len(A_pos)/2)):
    stack = np.append(stack, (A_pos[2*n]*A_pos[2*n+1]) % p)
    if n == K:
      break
  for n in range(0,int(len(A_neg)/2)):
    stack = np.append(stack, (A_neg[2*n]*A_neg[2*n+1]) % p)
    if n == K:
      break
  stack = np.sort(stack)[::-1]
  for n in range(0,K):
    s = (s * stack[n]) % p
else:
  Index = np.sort(abs(A_np))
  A_sort = A_np[Index-1]
  for n in range(0,K):
    s = (s * A_sort[n]) % p
print(int(s))

Submission status: Runtime Error

Solution code:
import numpy as np
N,K = map(int, input().split())
p = 10 ** 9 + 7
A = list(map(int, input().split()))
A_np = np.array(A)
A_pos = A_np[A_np>=0]
A_neg = A_np[A_np<0]
judge = False
s = 1
if len(A_pos) > 0:
  if N > K:
    judge = True
  else:
    if len(A_neg) % 2 == 0:
      judge = True    
else:
  if (K % 2) == 0:
    judge = True
if judge == True:
  A_pos = np.sort(A_pos)[::-1]
  A_neg = np.sort(A_neg)
  if (K % 2) == 1:
    s = A_pos[0]
    A_pos = A_pos[1:]
    K = K - 1
  K = int(K / 2)
  stack = np.array([])
  for n in range(0,int(len(A_pos)/2)):
    stack = np.append(stack, (A_pos[2*n]*A_pos[2*n+1]) % p)
  for n in range(0,int(len(A_neg)/2)):
    stack = np.append(stack, (A_neg[2*n]*A_neg[2*n+1]) % p)
  stack = np.sort(stack)[::-1]
  for n in range(0,K):
    s = (s * stack[n]) % p
else:
  Index = np.sort(abs(A_np))
  A_sort = A_np[Index-1]
  for n in range(0,K):
    s = (s * A_sort[n]) % p
print(int(s))

Submission status: Runtime Error

Solution code:
"""
E.
"""
from functools import reduce
from collections import deque


def prod(x, y):
    return x * y % MOD

    
N, K = map(int, input().split())
a_list = list(map(int, input().split()))
a_list.sort(reverse=True)
# abs_a_list = sorted(a_list, key=abs, reverse=True)
MOD = 10**9 + 7

if K%2 == 1:
    flg = True
    for a in a_list:
        if a >= 0:
            flg = False
            break
    if flg:
        print(reduce(prod, a_list[:K]))
        exit()

S = []
i = 0
a_list = deque(a_list)
while i < K:
    if a_list[0]*a_list[1] >= a_list[-1]*a_list[-2]:
        S.append(a_list.popleft())
        i += 1
    elif a_list[0]*a_list[1] < a_list[-1]*a_list[-2] and i <= K-2:
        S.append(a_list.pop())
        S.append(a_list.pop())
        i += 2
    else:
        S.append(a_list.popleft())
        i += 1

ans = reduce(prod, S)
print(ans)

Submission status: Runtime Error

Solution code:
"""
E.
"""
from functools import reduce
from collections import deque


def prod(x, y):
    return x * y % MOD

    
N, K = map(int, input().split())
a_list = list(map(int, input().split()))
a_list.sort(reverse=True)
# abs_a_list = sorted(a_list, key=abs, reverse=True)
MOD = 10**9 + 7

if K%2 == 1:
    flg = True
    for a in a_list:
        if a >= 0:
            flg = False
            break
    if flg:
        print(reduce(prod, a_list[:K]))
        exit()

S = []
i = 0
a_list = deque(a_list)
while i < K:
    if a_list[0]*a_list[1] > a_list[-1]*a_list[-2]:
        S.append(a_list.popleft())
        i += 1
    elif a_list[0]*a_list[1] <= a_list[-1]*a_list[-2] and i <= K-2:
        S.append(a_list.pop())
        S.append(a_list.pop())
        i += 2
    else:
        S.append(a_list.popleft())
        i += 1

ans = reduce(prod, S)
print(ans)

Submission status: Runtime Error

Solution code:
"""
E.
"""
from functools import reduce
from collections import deque


def prod(x, y):
    return x * y % MOD

    
N, K = map(int, input().split())
a_list = list(map(int, input().split()))
a_list.sort(reverse=True)
abs_a_list = sorted(a_list, key=abs, reverse=True)
MOD = 10**9 + 7

if K%2 == 1:
    flg = True
    for a in a_list:
        if a >= 0:
            flg = False
            break
    if flg:
        print(reduce(prod, a_list[:K])%MOD)
        exit()

# ans2
S = []
a_list = deque(a_list)
for _ in range(K//2):
    if a_list[0]*a_list[1] >= a_list[-1]*a_list[-2]:
        S.append(a_list.pop(0))
        S.append(a_list.pop(0))
    else:
        S.append(a_list.pop(-1))
        S.append(a_list.pop(-1))
if K%2 == 1:
    S.append(a_list.pop(0))

ans = reduce(prod, S)
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

MOD = 10**9 + 7
n, k = map(int, input().split())
a = np.array(list(map(int, input().split())))
ans = 1
plus = a[a >= 0]
plus = np.sort(plus)[::-1]
plus = np.append(plus, [-np.inf, np.inf])
minus = a[a < 0]
minus = np.sort(minus)
minus = np.append(minus, [np.inf, -np.inf])
if len(plus) == 2 and k % 2 == 1:
    for i in range(3, k + 3):
        ans *= minus[-i]
        ans %= MOD
else:
    c = 0
    cplus = 0
    cminus = 0
    while c < k - 1:
        if plus[cplus] * plus[cplus + 1] > minus[cminus] * minus[cminus + 1]:
            ans *= plus[cplus]
            ans %= MOD
            cplus += 1
            c += 1
        else:
            ans *= minus[cminus] * minus[cminus + 1]
            ans %= MOD
            cminus += 2
            c += 2
    if c == k - 1:
        ans *= plus[cplus]
        ans %= MOD
print(int(ans))

Submission status: Runtime Error

Solution code:
#import sys
#input = sys.stdin.readline
Q = 10**9+7
def main():
    N, K = map( int, input().split())
    A = list( map( int, input().split()))
    if K == 1:
        print(max(A))
        return
    positive = 0
    zero = 0
    negative = 0
    for a in A:
        if a > 0:
            positive += 1
        elif a == 0:
            zero += 1
        else:
            negative += 1
    if positive+negative < K:
        print(0)
        return
    if positive == 0:
        if K%2 == 1:
            if zero > 0:
                print(0)
                return
            else:
                A.sort(reverse=True)
                ans = 1
                for a in A[:K]:
                    ans *= a
                    ans %= Q
                print(ans)
                return
        else:
            A.sort()
            ans = 1
            for a in A[:K]:
                ans *= a
                ans %= Q
            print(ans)
            return
    # if K == 2:
    #     if negative <= 1:
    #         A.sort(reverse=True)
    #         print(A[0]*A[1]%Q)
    #         return
    if negative <= 1:
        A.sort(reverse=True)
        ans = 1
        for a in A[:K]:
            ans *= a
            ans %= Q
        print(ans)
        return
    if K == negative + positive:
        if negative%2 == 1:
            if zero > 0:
                print(0)
                return
        ans = 1
        for a in A:
            if a != 0:
                ans *= a
                ans %= Q
        print(ans)
        return
            
    B = [(abs(a), a) for a in A]
    B.sort(reverse = True)
    minus = False
    minus_value = 0
    plus_value = 0
    ans = 1
    for b in B[:K]:
        ans *= b[0]
        ans %= Q
        if b[1] < 0:
            minus = not minus
            minus_value = b[0]
        elif b[1] > 0:
            plus_value = b[0]
    if minus:
        ans_plus = ans_minus = ans
        plus_replaced_value = 0
        minus_replaced_value = 0
        if minus_value > 0
            for b in B[K:]:
                if b[1] > 0:
                    ans_plus = ans*pow(minus_value,Q-2,Q)%Q*b[0]%Q
                    plus_replaced_value = plus_value*b[0]
                    break
        if plus_value > 0
            for b in B[K:]:
                if b[1] < 0:
                    ans_minus = ans*pow(plus_value,Q-2,Q)%Q*b[0]%Q
                    minus_replaced_value = minus_value*b[0]
                    break
        if plus_replaced_value == 0 and minus_replaced_value == 0:
            pass
        elif plus_replaced_value < minus_replaced_value:
            ans = ans_minus
        else:
            ans = ans_plus
        
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from functools import partial, reduce
def abc_e():
    md = 1e9 + 7
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    a = sorted(a, key=abs)
    m1 = reduce(lambda x, y: (x * y) % md, a[-k:])
    a_max = max(a)
    if m1 > 0 and a_max > 0:
        result = m1
    elif a_max < 0:
        result = reduce(lambda x, y: (x * y) % md, a[:k])
    else:
        i_n = -k
        while i_n < 0:
            if a[i_n] < 0:
                break
            i_n += 1
        i_n_p = -k
        while i_n_p > -n:
            if a[i_n_p] < 0:
                break
            i_n_p -= 1
        i_p = -k
        while i_p < 0:
            if a[i_p] > 0:
                break
            i_p += 1
        i_p_p = -k
        while i_p_p > -n:
            if a[i_p_p] > 0:
                break
            i_p_p -= 1
        result = max(m1/a[i_n]*a[i_p_p], m1/a[i_p]*a[i_n_p])
    print(int(result))

if __name__ == '__main__':
    abc_e()