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.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class Main {
    static final FS sc = new FS();  // 封装输入类
    static final PrintWriter pw = new PrintWriter(System.out);
    static final int mod = (int)1e9 + 7;
    static int[] a;
    static int n, k;
    public static void main(String[] args) {
        n = sc.nextInt();
        k = sc.nextInt();
        a = sc.nextArray(n);
        List<Integer> nums = new ArrayList<>();
        for(int num : a) {
            nums.add(num);
        }
        nums.sort(Comparator.comparingInt(Math::abs));
        for(int i = 0;i < n;i++) {
            a[i] = nums.get(i);
        }
        long res = 1;
        int rightPos = -1, rightNeg = -1, leftPos = -1, leftNeg = -1;
        for(int i = n - k; i < n; i++){
            res = (res * a[i]) % mod;
            if(rightPos == -1 && a[i] > 0) rightPos = i;
            if(rightNeg == -1 && a[i] < 0) rightNeg = i;
        }
        for(int i = n - k - 1; i >= 0; i--){
            if(leftPos == -1 && a[i] > 0) leftPos = i;
            if(leftNeg == -1 && a[i] < 0) leftNeg = i;
        }

        if(res < 0 && n != k){
            if(rightPos == -1){
                if(leftPos == -1){
                    res = 1;
                    for(int i = 0; i < k; i++)
                        res = (res * a[i]) % mod;
                } else {
                    swap(a, leftPos, rightNeg);
                    res = calc(a);
                }
            } else {
                if(leftNeg == -1){
                    swap(a, leftPos, rightNeg);
                    res = calc(a);
                } else if(leftPos == -1){
                    swap(a, leftNeg, rightPos);
                    res = calc(a);
                } else {
                    int[] c1 = a, c2 = a;
                    swap(c1, leftNeg, rightPos);
                    swap(c2, leftPos, rightNeg);
                    res = Math.max(calc(c1), calc(c2));
                }
            }
        }

        pw.println(res < 0 ? res + mod : res);
        pw.flush();
    }


    private static long calc(int[] nums) {
        long temp = 1;
        for(int i = n - k; i < n; i++)
            temp = (temp * nums[i]) % mod;
        return temp;
    }

    private static void swap(int[] nums, int i, int j) {
        int t = nums[i];
        nums[i] = nums[j];
        nums[j] = t;
    }

    static class FS {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");
        String next() {
            while(!st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch(Exception ignored) {}
            }
            return st.nextToken();
        }
        int[] nextArray(int n) {
            int[] a = new int[n];
            for(int i = 0;i < n;i++) {
                a[i] = nextInt();
            }
            return a;
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
    }
}