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: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import collections
import math
import heapq

"""
#### 入力例 ####
N, X = map(int, input().split())

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

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())
"""
N, K = map(int, input().split())
A = list(map(int, input().split()))
mod = 10**9 +7

def main():
    ans = 1
    A.sort(reverse=True)
    if N == K:
        for n in range(N):
            ans*=A[n]
        print(ans % mod)
        return
    
    M = collections.deque()
    P = collections.deque()
    for n in range(N):
        if A[n] < 0:
            M.append(A[n])
        elif 0 < A[n]:
            P.append(A[n])
    if len(P) + len(M) < K:
        print(0)
        return
    if len(P) == 0:
        if(K%2 == 1):
            if (len(M)< N):
                print(0)
                return
            for k in range(K):
                ans = ans * M.popleft() % mod
            print(ans)
            return
        else:
            for k in range(K):
                ans = ans * M.pop() % mod
            print(ans)
            return
    if len(P) + len(M) == K:
        if K < N and len(M) % 2 == 1:
            print(0)
            return
        ans = 1
        while not len(P) == 0:
            ans = ans * P.pop() % mod
        while not len(M) == 0:
            ans = ans * M.pop() % mod
        print(ans)
        return 
    k = 0
    while k <= K-2:
        if len(P) < 2:
            ans *= M.pop() * M.pop() % mod
        elif len(M) <2:
            ans *= P.pop() * P.pop() % mod
        else:
            if (P[0]*P[1] <= M[-1]*M[-2]):
                ans *= M.pop() * M.popleft() % mod
            else:
                ans *= P.pop() * P.pop() % mod
        k+=2
    if k == K-1:
        ans *= P.pop() % mod

    print(ans)
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import collections
import math
import heapq

"""
#### 入力例 ####
N, X = map(int, input().split())

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

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())
"""
N, K = map(int, input().split())
A = list(map(int, input().split()))
mod = 10**9 +7

def main():
    ans = 1
    A.sort(reverse=True)
    if N == K:
        for n in range(N):
            ans*=A[n]
        print(ans % mod)
        return
    
    M = collections.deque()
    P = collections.deque()
    for n in range(N):
        if A[n] < 0:
            M.append(A[n])
        elif 0 < A[n]:
            P.append(A[n])
    if len(P) + len(M) < K:
        print(0)
        return
    if len(P) == 0:
        if(K%2 == 1):
            if (len(M)< N):
                print(0)
                return
            for k in range(K):
                ans = ans * M.popleft() % mod
            print(ans)
            return
        else:
            for k in range(K):
                ans = ans * M.pop() % mod
            print(ans)
            return
    k = 0
    while k <= K-2:
        if len(P) < 2:
            ans *= M.pop() * M.pop() % mod
        elif len(M) <2:
            ans *= P.pop() * P.pop() % mod
        else:
            if (P[0]*P[1] <= M[-1]*M[-2]):
                ans *= M.pop() * M.popleft() % mod
            else:
                ans *= P.pop() * P.pop() % mod
        k+=2
    if k == K-1:
        ans *= P.pop() % mod

    print(ans)
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import collections
import math
import heapq

"""
#### 入力例 ####
N, X = map(int, input().split())

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

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())
"""
N, K = map(int, input().split())
A = list(map(int, input().split()))
mod = 10**9 +7

def main():
    ans = 1
    A.sort(reverse=True)
    if N == K:
        for n in range(N):
            ans*=A[n]% mod
        print(ans)
        return
    
    M = collections.deque()
    P = collections.deque()
    for n in range(N):
        if A[n] < 0:
            M.append(A[n])
        elif 0 < A[n]:
            P.append(A[n])
    if len(P) + len(M) < K:
        return 0
    if len(P) == 0:
        if(K%2 == 1):
            for k in range(K):
                ans = ans * M.popleft() % mod
            print(ans)
            return
        else:
            for k in range(K):
                ans = ans * M.pop() % mod
            print(ans)
            return
    k = 0
    while k < K:
        if k == K-1:
            ans *= P.pop() % mod
            break
        if len(P) < 2:
            ans *= M.pop() %mod 
            ans *= M.pop() % mod
            k+=2
        elif len(M) <2:
            ans *= P.popleft() % mod
            k+=1
        else:
            if (P[0]*P[1] <= M[-1]*M[-2]):
                ans *= M.pop() %mod 
                ans *= M.pop() % mod
                k+=2
            else:
                ans *= P.popleft() % mod
                k+=1
    print(ans)
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import collections
import math
import heapq

"""
#### 入力例 ####
N, X = map(int, input().split())

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

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())
"""
N, K = map(int, input().split())
A = list(map(int, input().split()))
mod = 10**9 +7

def main():
    ans = 1
    A.sort(reverse=True)
    if N == K:
        for n in range(N):
            ans*=A[n]
        print(ans % mod)
        return
    
    M = collections.deque()
    P = collections.deque()
    for n in range(N):
        if A[n] < 0:
            M.append(A[n])
        elif 0 < A[n]:
            P.append(A[n])
    if len(P) + len(M) < K:
        return 0
    if len(P) == 0:
        if(K%2 == 1):
            for k in range(K):
                ans = ans * M.popleft() % mod
            print(ans)
            return
        else:
            for k in range(K):
                ans = ans * M.pop() % mod
            print(ans)
            return
    k = 0
    while k < K:
        if k == K-1:
            ans *= P.pop() % mod
            break
        if len(P) < 2:
            ans *= M.pop() * M.pop() % mod
            k+=2
        elif len(M) <2:
            ans *= P.popleft() * P.popleft() % mod
            k+=2
        else:
            if (P[0]*P[1] <= M[-1]*M[-2]):
                ans *= M.pop() * M.pop() % mod
                k+=2
            else:
                ans *= P.popleft() % mod
                k+=1
    print(ans)
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import collections
import math
import heapq

"""
#### 入力例 ####
N, X = map(int, input().split())

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

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())
"""
N, K = map(int, input().split())
A = list(map(int, input().split()))
mod = 10**9 +7

def main():
    ans = 1
    A.sort(reverse=True)
    if N == K:
        for n in range(N):
            ans*=A[n]
        print(ans % mod)
        return
    
    M = collections.deque()
    P = collections.deque()
    for n in range(N):
        if A[n] < 0:
            M.append(A[n])
        elif 0 < A[n]:
            P.append(A[n])
    if len(P) + len(M) < K:
        return 0
    if len(P) == 0:
        if(K%2 == 1):
            for k in range(K):
                ans = ans * M.popleft() % mod
            print(ans)
            return
        else:
            for k in range(K):
                ans = ans * M.pop() % mod
            print(ans)
            return
    k = 0
    while k < K:
        if k == K-1:
            ans *= P.pop() % mod
            break
        if len(P) < 2:
            ans *= M.pop() * M.pop() % mod
            k+=2
        elif len(M) <2:
            ans *= P.popleft() * P.popleft() % mod
            k+=2
        else:
            if (P[0]*P[1] <= M[-1]*M[-2]):
                ans *= M.pop() * M.pop() % mod
                k+=2
            else:
                ans *= P.popleft() * P.popleft() % mod
                k+=2
    print(ans)
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import collections
import math
import heapq

"""
#### 入力例 ####
N, X = map(int, input().split())

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

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())
"""
N, K = map(int, input().split())
A = list(map(int, input().split()))
mod = 10**9 +7

def main():
    ans = 1
    A.sort(reverse=True)
    if N == K:
        for n in range(N):
            ans*=A[n]
        print(ans % mod)
        return
    
    M = collections.deque()
    P = collections.deque()
    for n in range(N):
        if A[n] < 0:
            M.append(A[n])
        elif 0 < A[n]:
            P.append(A[n])
    if len(P) + len(M) < K:
        return 0
    if len(P) == 0:
        if(K%2 == 1):
            for k in range(K):
                ans = ans * M.popleft() % mod
            print(ans)
            return
        else:
            for k in range(K):
                ans = ans * M.pop() % mod
            print(ans)
            return
    k = 0
    while k < K:
        if k == K-1:
            ans *= P.pop() % mod
            break
        if len(P) < 2:
            ans *= M.pop() * M.pop() % mod
            k+=2
        elif len(M) <2:
            ans *= P.pop() * P.pop() % mod
            k+=2
        else:
            if (P[0]*P[1] <= M[-1]*M[-2]):
                ans *= M.pop() * M.pop() % mod
                k+=2
            else:
                ans *= P.popleft() * P.popleft() % mod
                k+=2
    print(ans)
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import collections
import math
import heapq

"""
#### 入力例 ####
N, X = map(int, input().split())

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

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())
"""
N, K = map(int, input().split())
A = list(map(int, input().split()))
mod = 10**9 +7

def main():
    ans = 1
    A.sort(reverse=True)
    if N == K:
        for n in range(N):
            ans*=A[n]
        print(ans % mod)
        return
    
    M = collections.deque()
    P = collections.deque()
    for n in range(N):
        if A[n] < 0:
            M.append(A[n])
        elif 0 < A[n]:
            P.append(A[n])
    if len(P) + len(M) < K:
        return 0
    if len(P) == 0:
        if(K%2 == 1):
            for k in range(K):
                ans = ans * M.popleft() % mod
            print(ans)
            return
        else:
            for k in range(K):
                ans = ans * M.pop() % mod
            print(ans)
            return
    
    
    S = collections.deque()
    while len(S) != K:
        p_stock = 0
        m_stock = 0
        if not len(M) < 2:
            m_stock = M[-1]*M[-2]
        if not len(P) < 2:
            p_stock = P[0]*P[1]
        if len(S) == K-1 or m_stock < p_stock:
            if not len(P) < 1:
                S.append(P.pop())
            else:
                S.append(M.pop())
                S.append(M.pop())
        else:
            if not len(M) < 2:
                S.append(M.pop())
                S.append(M.pop())
            else:
                S.append(P.pop())
    while len(S) != 0:
        ans = ans * S.pop() % mod
    print(ans)
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import collections
import math
import heapq

"""
#### 入力例 ####
N, X = map(int, input().split())

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

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())
"""
N, K = map(int, input().split())
A = list(map(int, input().split()))
mod = 10**9 +7

def main():
    ans = 1
    A.sort(reverse=True)
    if N == K:
        for n in range(N):
            ans*=A[n]
        print(ans % mod)
        return
    
    M = collections.deque()
    P = collections.deque()
    for n in range(N):
        if A[n] < 0:
            M.append(A[n])
        elif 0 < A[n]:
            P.append(A[n])
    if len(P) + len(M) < K:
        return 0
    if len(P) == 0:
        if(K%2 == 1):
            for k in range(K):
                ans = ans * M.popleft() % mod
            print(ans)
            return
        else:
            for k in range(K):
                ans = ans * M.pop() % mod
            print(ans)
            return
    
    
    S = collections.deque()
    while len(S) != K:
        p_stock = 0
        m_stock = 0
        if not len(M) < 2:
            m_stock = M[-1]*M[-2]
        if not len(P) < 2:
            p_stock = P[0]*P[1]
        if len(S) == K-1 or m_stock < p_stock:
            if not len(P) < 1:
                S.append(P.pop())
        else:
            if not len(M) < 2:
                S.append(M.pop())
                S.append(M.pop())
    while len(S) != 0:
        ans = ans * S.pop() % mod
    print(ans)
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import collections
import math

"""
#### 入力例 ####
N, X = map(int, input().split())

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

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())
"""

N, K = map(int, input().split())
A = list(map(int, input().split()))
mod = 10**9 +7

def main():
    ans = 1
    A.sort(reverse=True)
    if N == K:
        for n in range(N):
            ans*=A[n]
        print(ans % mod)
        return
    
    M = collections.deque()
    P = collections.deque()
    for n in range(N):
        if A[n] < 0:
            M.append(A[n])
        else:
            P.append(A[n])

    if len(P) == 0:
        if(K%2 == 1):
            for k in range(K):
                ans *= M.popleft()
            print(ans % mod)
            return
        else:
            for k in range(K):
                ans *= M.pop()
            print(ans%mod)
            return
    
    
    p_stock = 0
    m_stock = 0
    M = list(M)
    M.sort()
    #print(M)
    P = list(P)
    idx_p = 0
    idx_m = 0
    for k in range(0, K//2):
        if  len(M)-1 <= idx_m and idx_p < len(P) -1:
            ans = P[idx_p] * P[idx_p +1 ]
            idx_p += 2
            continue
            
        elif len(P)-1 <= idx_p and idx_m < len(M)-1:
            ans *= M[idx_m] * M[idx_m + 1]  
            idx_m += 2
            continue
        elif idx_m < len(M)-1 and idx_p < len(P) -1:
            m_stock = M[idx_m] * M[idx_m + 1] 
            p_stcok = P[idx_p] * P[idx_p + 1]
            if m_stock < p_stcok:
                ans *= p_stock
                idx_p += 2
            else:
                ans *= m_stock
                idx_m += 2
            continue
        else:
            continue

    if K % 2 == 1:
        ans *= P[idx_p]

    print(ans%mod)
    return
    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import collections
import math

"""
#### 入力例 ####
N, X = map(int, input().split())

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

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())
"""

N, K = map(int, input().split())
A = list(map(int, input().split()))
mod = 10**9 +7

def main():
    ans = 1
    A.sort(reverse=True)
    if N == K:
        for n in range(N):
            ans*=A[n]
        print(ans % mod)
        return
    
    M = collections.deque()
    P = collections.deque()
    for n in range(N):
        if A[n] < 0:
            M.append(A[n])
        else:
            P.append(A[n])

    if len(P) == 0:
        if(K%2 == 1):
            for k in range(K):
                ans *= M.popleft()
            print(ans % mod)
            return
        else:
            for k in range(K):
                ans *= M.pop()
            print(ans%mod)
            return
    
    
    p_stock = 0
    m_stock = 0
    M = list(M)
    M.sort()
    #print(M)
    P = list(P)
    idx_p = 0
    idx_m = 0
    for k in range(0, K//2, 2):
        if  len(M)-1 <= idx_m and idx_p < len(P) -1:
            ans = P[idx_p] * P[idx_p +1 ]
            idx_p += 2
            continue
            
        elif len(P)-1 <= idx_p and idx_m < len(M)-1:
            ans *= M[idx_m] * M[idx_m + 1]  
            idx_m += 2
            continue
        elif idx_m < len(M)-1 and idx_p < len(P) -1:
            m_stock = M[idx_m] * M[idx_m + 1] 
            p_stcok = P[idx_p] * P[idx_p + 1]
            if m_stock < p_stcok:
                ans *= p_stock
                idx_p += 2
            else:
                ans *= m_stock
                idx_m += 2
            continue
        else:
            continue

    if K % 2 == 1:
        ans *= P[idx_p]

    print(ans%mod)
    return
    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import collections
import math

"""
#### 入力例 ####
N, X = map(int, input().split())

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

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())
"""

N, K = map(int, input().split())
A = list(map(int, input().split()))
mod = 10**9 +7

def main():
    ans = 1
    A.sort(reverse=True)
    if N == K:
        for n in range(N):
            ans*=A[n]
        print(ans % mod)
        return
    
    M = collections.deque()
    P = collections.deque()
    for n in range(N):
        if A[n] < 0:
            M.append(A[n])
        else:
            P.append(A[n])

    if len(P) == 0:
        if(K%2 != 0):
            for k in range(K):
                ans *= M.popleft()
            print(ans % mod)
            return
        else:
            for k in range(K):
                ans *= M.pop()
            print(ans%mod)
            return
    
    if K % 2 == 1:
        ans *= P.popleft()
    
    p_stock = 0
    m_stock = 0
    M = list(M)
    M.sort()
    #print(M)
    P = list(P)
    idx_p = 0
    idx_m = 0
    for k in range(0, K//2, 2):
        if  len(M)-1 <= idx_m and idx_p < len(P) -1:
            ans = P[idx_p] * P[idx_p +1 ]
            idx_p += 2
            continue
            
        elif len(P)-1 <= idx_p and idx_m < len(M)-1:
            ans *= M[idx_m] * M[idx_m + 1]  
            idx_m += 2
            continue
        elif idx_m < len(M)-1 and idx_p < len(P) -1:
            m_stock = M[idx_m] * M[idx_m + 1] 
            p_stcok = P[idx_p] * P[idx_p + 1]
            if m_stock < p_stcok:
                ans *= p_stock
                idx_p += 2
            else:
                ans *= m_stock
                idx_m += 2
            continue
        else:
            continue

    print(ans%mod)
    return
    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import collections
import math

"""
#### 入力例 ####
N, X = map(int, input().split())

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

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())
"""

N, K = map(int, input().split())
A = list(map(int, input().split()))
mod = 10**9 +7

def main():
    ans = 1
    A.sort(reverse=True)
    if N == K:
        for n in range(N):
            ans*=A[n]
        print(ans % mod)
        return
    
    M = collections.deque()
    P = collections.deque()
    for n in range(N):
        if A[n] < 0:
            M.append(A[n])
        else:
            P.append(A[n])

    if len(P) == 0:
        if(K%2 != 0):
            for k in range(K):
                ans *= M.popleft()
            print(ans % mod)
            return
        else:
            for k in range(K):
                ans *= M.pop()
            print(ans%mod)
            return
    
    if K % 2 == 1:
        ans *= P.popleft()
    
    p_stock = 0
    m_stock = 0
    M = list(M)
    #print(M)
    P = list(P)
    idx_p = 0
    idx_m = 0
    for k in range(0, K//2, 2):
        if  len(M)-1 <= idx_m and idx_p < len(P) -1:
            ans *= P[idx_p] * P[idx_p +1 ]
            idx_p += 2
            
        elif len(P)-1 <= idx_p and idx_m < len(M)-1:
            ans *= M[idx_m] * M[idx_m + 1]  
            idx_m += 2
        elif idx_m < len(M)-1 and idx_p < len(P) -1:
            m_stock = M[idx_m] * M[idx_m + 1] 
            p_stcok = P[idx_p] * P[idx_p + 1]
            if m_stock < p_stcok:
                ans *= p_stock
                idx_p += 2
            else:
                ans *= m_stock
                idx_m += 2
        else:
            break

    print(ans%mod)
    return
    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import collections
import math

"""
#### 入力例 ####
N, X = map(int, input().split())

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

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())
"""

N, K = map(int, input().split())
A = list(map(int, input().split()))
mod = 10**9 +7

def main():
    ans = 1
    A.sort(reverse=True)
    if N == K:
        for n in range(N):
            ans*=A[n]
        print(ans % mod)
        return
    
    M = collections.deque()
    P = collections.deque()
    for n in range(N):
        if A[n] < 0:
            M.append(A[n])
        else:
            P.append(A[n])

    if len(P) == 0:
        if(K%2 != 0):
            for k in range(K):
                ans *= M.popleft()
            print(ans % mod)
            return
        else:
            for k in range(K):
                ans *= M.pop()
            print(ans%mod)
            return
    
    if K % 2 == 1:
        ans *= P.popleft()
    
    p_stock = 0
    m_stock = 0
    M = list(M)
    #print(M)
    P = list(P)
    idx_p = 0
    idx_m = 0
    for k in range(0, K//2, 2):
        if  len(M)-1 <= idx_m:
            ans *= P[idx_p] * P[idx_p +1 ]
            idx_p += 2
            
        elif len(P)-1 <= idx_p:
            ans *= M[idx_m] * M[idx_m + 1]  
            idx_m += 2
        elif idx_m < len(M)-1 and idx_p < len(P) -1:
            m_stock = M[idx_m] * M[idx_m + 1] 
            p_stcok = P[idx_p] * P[idx_p + 1]
            if m_stock < p_stcok:
                ans *= p_stock
                idx_p += 2
            else:
                ans *= m_stock
                idx_m += 2
        else:
            break

    print(ans%mod)
    return
    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys

def main():
    N,K = list(map(int, sys.stdin.readline().split()))
    A_list = list(map(int, sys.stdin.readline().split()))


    A_list.sort(key=lambda x: -abs(x))
    idx_plus  = [] # the Index of minus value in A_list between 0 to (K-1)
    idx_minus = [] # the Index of plus value in A_list between K to end
    cnt_minus = 0
    mod = 10**9 + 7


    for i in range(N):
        if (K <= i) and (A_list[i] >= 0):
            idx_plus.append(i)
        
        if (i <= (K-1)) and (A_list[i] < 0):
            idx_minus.append(i)
            cnt_minus += 1


    if cnt_minus % 2 ==0:
        target_idx = [0,K-1]

    else:
        if len(idx_plus) == 0:
            target_idx = [len(A_list)-K, len(A_list)-1]
        else:
            A_list[ idx_minus[-1] ], A_list[ idx_plus[0] ] = A_list[ idx_plus[0] ], A_list[ idx_minus[-1] ]
            target_idx = [0,K-1]


    ans = 1

    for i in range( target_idx[0], (target_idx[1] + 1) ):
        ans *= A_list[i]
        ans %= mod
    
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

X = sorted([a for a in A if a > 0])
Y = sorted([a for a in A if a < 0], key=lambda x: abs(x))

if 2 * (min(K, len(Y)) // 2) + len(X) >= K:
    ans = 1
    if K % 2 == 1:
        ans *= X.pop()
        K -= 1

    XX = [(x1 * x2) for x1, x2 in zip(*[iter(X)] * 2)]
    YY = [(y1 * y2) for y1, y2 in zip(*[iter(Y)] * 2)]
    ZZ = sorted(XX + YY)

    for i in range(K // 2):
        ans *= ZZ.pop()
        ans %= MOD

    print(ans)

elif 0 in A:
    print(0)

else:
    ans = 1
    Z = sorted(X + Y, key=lambda x: abs(x), reverse=True)
    for i in range(K):
        ans *= Z.pop()
        ans %= MOD
    print(ans)

Submission status: Wrong Answer

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

X = sorted([a for a in A if a > 0])
Y = sorted([a for a in A if a < 0], key=lambda x: abs(x))

if min(K, len(Y)) // 2 + len(X) >= K:
    ans = 1
    if K % 2 == 1:
        ans *= X.pop()
        K -= 1

    XX = [(x1 * x2) % MOD for x1, x2 in zip(*[iter(X)] * 2)]
    YY = [(y1 * y2) % MOD for y1, y2 in zip(*[iter(Y)] * 2)]
    ZZ = sorted(XX + YY)

    for i in range(K // 2):
        ans *= ZZ.pop()
        ans %= MOD

    print(ans)

elif 0 in A:
    print(0)

else:
    ans = 1
    Z = sorted(X + Y, key=lambda x: abs(x), reverse=True)
    for i in range(K):
        ans *= Z.pop()
        ans %= MOD
    print(ans)

Submission status: Wrong Answer

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

X = sorted([a for a in A if a >= 0])
Y = sorted([a for a in A if a < 0], key=lambda x: abs(x))

if min(K, len(Y)) // 2 + len(X) >= K:
    ans = 1
    if K % 2 == 1:
        ans *= X.pop()
        K -= 1

    XX = [(x1 * x2) % MOD for x1, x2 in zip(*[iter(X)] * 2)]
    YY = [(y1 * y2) % MOD for y1, y2 in zip(*[iter(Y)] * 2)]
    ZZ = sorted(XX + YY)

    for i in range(K // 2):
        ans *= ZZ.pop()
        ans %= MOD

    print(ans)

else:
    ans = 1
    Z = sorted(X + Y, key=lambda x: abs(x), reverse=True)
    for i in range(K):
        ans *= Z.pop()
        ans %= MOD
    print(ans)

Submission status: Wrong Answer

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

X = sorted([a for a in A if a >= 0])
Y = sorted([a for a in A if a < 0], key=lambda x: abs(x))

if len(Y) // 2 + len(X) >= K:
    ans = 1
    if K % 2 == 1:
        ans *= X.pop()
        K -= 1

    XX = [(x1 * x2) % MOD for x1, x2 in zip(*[iter(X)] * 2)]
    YY = [(y1 * y2) % MOD for y1, y2 in zip(*[iter(Y)] * 2)]
    ZZ = sorted(XX + YY)

    for i in range(K // 2):
        ans *= ZZ.pop()
        ans %= MOD

    print(ans)

else:
    ans = 1
    Z = sorted(X + Y, key=lambda x: abs(x), reverse=True)
    for i in range(K):
        ans *= Z.pop()
        ans %= MOD
    print(ans)

Submission status: Wrong Answer

Solution code:
mod=10**9+7
n,k=map(int,input().split())
arr=list(map(int,input().split()))
arr=sorted(arr,reverse=True,key=lambda x:abs(x)) #絶対値の順にソートする
if k==n: #k=nのときはすべてを掛け合わせるほかない
  ans=1
  for i in range(n):
    ans*=arr[i]
    ans%=mod
  print(ans)
else:
  if k%2==1 and max(arr)<0: #答えが負になる場合は絶対値の小さい順にK個掛け合わせる
    ans=1
    for i in range(k):
      ans*=arr[n-1-i]
      ans%=mod
      print(ans)
  else:
    ans=1
    cnt=0
    for i in range(k): #絶対値の大きい順にK個掛け合わせる
      if arr[i]<0: #負数を掛けた回数を数えておく
        cnt+=1
      ans*=arr[i]
      ans%=mod
    if cnt%2==0: #負数を偶数回掛けていれば答えは非負なのでそのまま出力
      print(ans)
    else: #そうでなければ調整を行う
      min_plus=-1
      min_minus=1
      for i in range(k): #絶対値の大きい順にK個を見て最小の非負の数・負の数を探す
        if arr[i]>=0:
          min_plus=arr[i]
        else:
          min_minus=arr[i]
      max_plus=-1
      max_minus=1
      for i in range(k,n): #残るN-K個を見て最大の非負の数・負の数を探す
        if arr[i]>=0 and max_plus==-1:
          max_plus=arr[i]
        if arr[i]<0 and max_minus==1:
          max_minus=arr[i]
      if min_plus==-1: #もし最小の非負の数がなければ、最小の負の数を取り除くしかない
        arr.remove(min_minus)
        ans=1
        for i in range(k-1):
          ans*=arr[i]
          ans%=mod
        ans*=max_plus
        ans%=mod
      elif min_minus==1: #もし最小の負の数がなければ、最小の非負の数を取り除くしかない
        arr.remove(min_plus)
        ans=1
        for i in range(k-1):
          ans*=arr[i]
          ans%=mod
        ans*=max_minus
        ans%=mod
      elif min_plus*max_plus>=min_minus*max_minus: #そうでなければより倍率の大きいほうを選ぶ
        arr.remove(min_minus)
        ans=1
        for i in range(k-1):
          ans*=arr[i]
          ans%=mod
        ans*=max_plus
        ans%=mod
      else:
        arr.remove(min_plus)
        ans=1
        for i in range(k-1):
          ans*=arr[i]
          ans%=mod
        ans*=max_minus
        ans%=mod
      print(ans)

Submission status: Wrong Answer

Solution code:
mod=10**9+7
n,k=map(int,input().split())
a=list(map(int,input().split()))
 
a=sorted(a,reverse=True,key=lambda x:abs(x))
 
if k==n:
  ans=1
  for i in range(n):
    ans *= a[i]
    ans %= mod
  print(ans)
else:
  if k%2 and a[0]<0:
    ans=1
    for i in range(k):
      ans *=a[n-i-1]
      ans %= mod
    print(ans)
  else:
    ans=1
    count=0
    for i in range(k):
      if a[i]<0:
        count +=1
      ans *= a[i]
      ans %= mod
    if count%2==0:
      print(ans)
    else:
      min_plus=-1 #Kこの中の絶対値最小の非負数
      min_minus=1 #Kこの中の絶対値最大の負数
      for i in range(k):
        if a[i]<0:
          min_minus=a[i]
        else:
          min_plus=a[i]
      max_plus=-1 #N-Kこの中の絶対し最小の非負数
      max_minus=1 #N-Kこの中の絶対値最大の負数
      for i in range(k,n):
        if a[i]<0:
          max_minus=a[i]
          break
      for i in range(k,n):
        if a[i]>=0:
          max_plus=a[i]
          break
      if min_plus==-1: #Kこの中に非負数が存在しない
        a.remove(min_minus)
        ans=1
        for i in range(k-1):
          ans *= a[i]
          ans %= mod
        ans*=max_plus
        print(ans)
      elif min_minus==1: #Kこの中に負数が存在しない
        a.remove(min_plus)
        ans=1
        for i in range(k-1):
          ans *= a[i]
          ans %= mod
        ans*=max_minus
        print(ans) 
      elif min_plus*max_plus>=min_minus*max_minus: #Kこの中に非負数が存在しない
        a.remove(min_plus)
        ans=1
        for i in range(k-1):
          ans *= a[i]
          ans %= mod
        ans*=max_minus
        print(ans) 
      else:
        a.remove(min_minus)
        ans=1
        for i in range(k-1):
          ans *= a[i]
          ans %= mod
        ans*=max_plus
        print(ans)