Problem:
Score : 500 points

Problem Statement

Given are N integers A_1,\ldots,A_N.

We will choose exactly K of these elements. Find the maximum possible product of the chosen elements.

Then, print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Constraints

1 \leq K \leq N \leq 2\times 10^5

|A_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N K
A_1 \ldots A_N

Output

Print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Sample Input 1

4 2
1 2 -3 -4

Sample Output 1

12

The possible products of the two chosen elements are 2, -3, -4, -6, -8, and 12, so the maximum product is 12.

Sample Input 2

4 3
-1 -2 -3 -4

Sample Output 2

1000000001

The possible products of the three chosen elements are -24, -12, -8, and -6, so the maximum product is -6.

We print this value modulo (10^9+7), that is, 1000000001.

Sample Input 3

2 1
-1 1000000000

Sample Output 3

1000000000

The possible products of the one chosen element are -1 and 1000000000, so the maximum product is 1000000000.

Sample Input 4

10 10
1000000000 100000000 10000000 1000000 100000 10000 1000 100 10 1

Sample Output 4

999983200

Be sure to print the product modulo (10^9+7).

Solution in Java (OpenJDK 11.0.6) (judge verdict: Wrong Answer)

import java.io.*; 
import java.util.*; 
  
class Main{ 
    // Function to find maximum possible product 
    static int maxProductSubarrayOfSizeK(int A[], int n, int k) 
    { 
        // sorting given input array 
        Arrays.sort(A); 
  
        // variable to store final product of all element 
        // of sub-sequence of size k 
        int product = 1; 
  
        // CASE I 
        // If max element is 0 and 
        // k is odd then max product will be 0 
        if (A[n - 1] == 0 && k % 2 != 0) 
            return 0; 
  
        // CASE II 
        // If all elements are negative and 
        // k is odd then max product will be 
        // product of rightmost-subarray of size k 
        if (A[n - 1] <= 0 && k % 2 != 0) { 
            for (int i = n - 1; i >= n - k; i--) 
                product *= A[i]; 
            return product; 
        } 
  
        // else 
        // i is current left pointer index 
        int i = 0; 
  
        // j is current right pointer index 
        int j = n - 1; 
  
        // CASE III 
        // if k is odd and rightmost element in 
        // sorted array is positive then it 
        // must come in subsequence 
        // Multiplying A[j] with product and 
        // correspondingly changing j 
        if (k % 2 != 0) { 
            product *= A[j]; 
            j--; 
            k--; 
        } 
  
        // CASE IV 
        // Now k is even 
        // Now we deal with pairs 
        // Each time a pair is multiplied to product 
        // ie.. two elements are added to subsequence each time 
        // Effectively k becomes half 
        // Hence, k >>= 1 means k /= 2 
        k >>= 1; 
  
        // Now finding k corresponding pairs 
        // to get maximum possible value of product 
        for (int itr = 0; itr < k; itr++) { 
            // product from left pointers 
            int left_product = A[i] * A[i + 1]; 
  
            // product from right pointers 
            int right_product = A[j] * A[j - 1]; 
  
            // Taking the max product from two choices 
            // Correspondingly changing the pointer's position 
            if (left_product > right_product) { 
                product *= left_product; 
                i += 2; 
            } 
            else { 
                product *= right_product; 
                j -= 2; 
            } 
        } 
  
        // Finally return product 
        return product; 
    } 
  
    // driver program 
    public static void main(String[] args) 
    { 
        int A[] = { 1, 2, -1, -3, -6, 4 }; 
        int n = A.length; 
        int k = 4; 
        System.out.println(maxProductSubarrayOfSizeK(A, n, k)); 
    } 
}