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.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		List<Long> a = new ArrayList<Long>();
		List<Long> minus = new ArrayList<Long>();
		boolean b= false;
		for (int i=0;i<N;i++) {
			long temp = sc.nextLong();
			if (temp>0) {
				a.add(temp);
			} else if (temp==0) {
				b = true;
			} else {
				minus.add(temp);
			}
		}
		Collections.sort(a);
		Collections.sort(minus);
		if (K==N&&b==false) {
			long result=1;
			for (int i=0;i<a.size();i++) {
				result = (result *a.get(i))%1000000007;
			}
			for (int i=0;i<minus.size();i++) {
				result = (result *minus.get(i))%1000000007;
			}
			System.out.println(result);
		} else if (K>a.size()+minus.size()) {
			System.out.println(0);
		} else if (a.size()>0){
			BigDecimal big = new BigDecimal(1);
			int start=0;
			int count =a.size();
			int startB=0;
			if (K>a.size()) {
				if ((K-a.size())%2==1) {
					for (int i=1;i<a.size();i++) {
						big = big.multiply(new BigDecimal(a.get(i)));
						start =1;
						count +=a.size()-1;
					}
				} else {
					for (int i=0;i<a.size();i++) {
						big = big.multiply(new BigDecimal(a.get(i)));
					}
				}
				for (int i=0;i<K-count;i++) {
					big = big.multiply(new BigDecimal(minus.get(i)));
					startB= i;
				}
			} else {
				for (int i=0;i<K;i++) {
					big = big.multiply(new BigDecimal(a.get(a.size()-1-i)));
				}
			}
			int max =start;
			for (int i=start;i+1<a.size();i+=2) {
				BigDecimal temp = big;
				temp = big.divide(new BigDecimal(a.get(i)));
				temp = temp.divide(new BigDecimal(a.get(i+1)));
				if (startB+1>=minus.size()) {
					break;
				}
				temp = temp.multiply(new BigDecimal(minus.get(startB)));
				temp = temp.multiply(new BigDecimal(minus.get(startB+1)));
				startB+=2;
				if (temp.compareTo(big)==1) {
					max = i+2;
				}
				big = temp;
			}
			long result=1;
			for (int i=max;i<a.size();i++) {
				result = (result *(a.get(i)))%1000000007;
			}
			for (int i=0;i<K-(a.size()-max);i++) {
				result = (result *(minus.get(i)))%1000000007;
			}
			if (result<0) {
				result +=1000000007;
			}
			System.out.println(result%1000000007);
		} else {
			long result =1;
			for (int i=0;i<K;i++) {
				result = (result *(minus.get(minus.size()-1-i)))%1000000007;
			}
			if (result<0) {
				result +=1000000007;
			}
			System.out.println(result%1000000007);
		}
	}
}