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: Runtime Error)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.solve();
    }
    public void solve() {
        Scanner scan = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        long mod = 1000000007;
        int N = scan.nextInt();
        int K = scan.nextInt();
        List<Long> A = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            A.add(scan.nextLong());
        }
        A.sort(Comparator.naturalOrder());
        if (N == K) {
            long ans = multiple(A, 0, N, mod);
            System.out.println(ans);
            return;
        }
        if (0 < A.get(0)) {
            long ans = multiple(A, N-K, N, mod);
            System.out.println(ans);
            return;
        }
        if (A.get(N-1) < 0) {
            if (K % 2 == 0) {
                long ans = multiple(A, 0, K, mod);
                System.out.println(ans);
                return;
            }
            long ans = multiple(A, N-K, N, mod);
            System.out.println(ans);
            return;
        }
        List<Long> negatives = new ArrayList<>();
        List<Long> positives = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            long x = A.get(i);
            if (x == 0) {
                continue;
            }
            if (x < 0) {
                negatives.add(x);
                continue;
            }
            positives.add(x);
        }
        int negative_length = negatives.size();
        int positive_length = positives.size();
        if (negatives.size()+positives.size() < K) {
            System.out.println(0);
            return;
        }
        long ans = 1;
        if (K % 2 == 1) {
            K -= 1;
            ans = positives.get(positive_length-1);
        }
        PriorityQueue<Long> queue = new PriorityQueue<>(Comparator.reverseOrder());
        for (int i = 0; i < negative_length/2; i++) {
            queue.add(negatives.get(2 * i) * negatives.get(2 * i + 1));
        }
        int start = positive_length-1;
        if (K % 2 == 1) {
            start -= 1;
        }
        while (0 < start) {
            long x1 = positives.get(start);
            long x2 = positives.get(start-1);
            queue.add(x1 * x2);
            start -= 2;
        }
        for (int i = 0; i < K / 2; i++) {
            ans *= queue.poll();
            ans %= mod;
        }
        System.out.println(ans);
    }
    private long multiple(List<Long> A, int start, int end, long mod) {
        long ans = 1;
        for (int i = start; i < end; i++) {
            ans *= A.get(i);
            if (ans < 0) {
                ans += mod;
            }
            ans %= mod;
        }
        return ans;
    }
}