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 1.8.0) (judge verdict: Wrong Answer)

//Created by Aminul on 7/5/2020.

import java.io.*;
import java.util.*;

import static java.lang.Math.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner in = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        int n = in.nextInt(), k = in.nextInt();
        PriorityQueue<Long> pos = new PriorityQueue<>(),
                neg = new PriorityQueue<>();
        PriorityQueue<Long> posRev = new PriorityQueue<>(Collections.reverseOrder()),
                negRev = new PriorityQueue<>(Collections.reverseOrder());

        long mod = (long) 1e9 + 7;
        boolean hasZero = false;
        for (int i = 0; i < n; i++) {
            long x = in.nextInt();
            if (x > 0) {
                pos.add(x);
                posRev.add(x);
            } else if (x < 0) {
                neg.add(x);
                negRev.add(x);
            } else hasZero = true;
        }

        if (pos.size() + neg.size() < k) {
            pw.println(0);
        } else {
            boolean isPositive = false;
            for (int i = 0; i <= neg.size() && i <= k; i += 2) {
                if (i + pos.size() >= k) {
                    isPositive = true;
                    break;
                }
            }
            
            if (!isPositive) {
                if (hasZero) pw.println(0);
                else {
                    long res = -1;
                    while (k > 0) {
                        if (!posRev.isEmpty() && !negRev.isEmpty()) {
                            if (abs(negRev.peek()) <= abs(posRev.peek())) {
                                res = mod(res * abs(negRev.poll()), mod);
                            } else {
                                res = mod(res * abs(posRev.poll()), mod);
                            }
                        } else if (!posRev.isEmpty()) {
                            res = mod(res * abs(posRev.poll()), mod);
                        } else {
                            res = mod(res * abs(negRev.poll()), mod);
                        }
                        k--;
                    }

                    pw.println(res);
                }
            } else {
                long res = 1;
                while (k > 0) {
                    if (k >= 2 && pos.size() >= 2 && neg.size() >= 2) {
                        long p1 = pos.poll(), p2 = pos.poll();
                        long n1 = neg.poll(), n2 = neg.poll();
                        if (p1 * p2 >= n1 * n2) {
                            res = (res * ((p1 * p2) % mod)) % mod;
                            neg.add(n1);
                            neg.add(n2);
                        } else {
                            res = (res * ((n1 * n2) % mod)) % mod;
                            pos.add(p1);
                            pos.add(p2);
                        }
                        k -= 2;
                    } else if (k >= 2 && neg.size() >= 2) {
                        long n1 = neg.poll(), n2 = neg.poll();
                        res = (res * ((n1 * n2) % mod)) % mod;
                        k -= 2;
                    } else if (pos.size() > 0) {
                        res = (res * pos.poll()) % mod;
                        k--;
                    }
                }

                pw.println(res);
            }
        }

        pw.close();
    }

    public static long mod(long a, long m) {
        long A = (a % m);
        return A >= 0 ? A : A + m;
    }

    static void debug(Object... obj) {
        System.err.println(Arrays.deepToString(obj));
    }
}