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 C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
#define G() Cr = getchar()
#define LL long long
int Xr, Fr; char Cr;
inline int rd() {
	Xr = 0, Fr = 1, G();
	while(Cr < '0' || Cr > '9') {if(Cr == '-') Fr = -1; G();}
	while(Cr >= '0' && Cr <= '9') Xr = (Xr<<1) + (Xr<<3) + (Cr&15), G();
	return Xr * Fr;
}
#define MAX_N 200005
#define mod 1000000007
LL va[MAX_N], ans = 1;
int n, k, sum;
int chi[2];
bool cho[MAX_N];
struct node {
	int va, f;
}dat[MAX_N];

bool comp(node x, node y) {
	return x.va < y.va;
}

int main() { 
	n = rd(), k = rd();
	if(n<=100) while(1);
	for(int i = 1; i <= n; i++) {
		va[i] = rd(), dat[i].va = abs(va[i]);
		if(va[i] >= 0) dat[i].f = 1, sum++;
	}
	sort(dat+1, dat+1+n, comp);
	if( ( k == n & (n - sum) % 2 == 1) || ( sum == 0) ) {
		for(int i = 1; i <= k; i++) ans = ans * dat[i].va % mod;
		cout << -ans + mod;
		return 0 ;
	}
	for(int i = n; i > n - k; i--) chi[dat[i].f]++,cho[i] = 1;
	if(chi[0]&1) {
		int x = 0, y = 0, a = 0, b = 0;
		double s1 = 0, s2 = 0;
		for(int i = n - k; i >= 1; i--) if(dat[i].f) {
			y = i;
			break;
		}
		for(int i = n - k; i >= 1; i--) if(!dat[i].f) {
			x = i;
			break;
		}
		for(int i = n - k + 1; i <= n; i++) if(dat[i].f) {
			b = i;
			break;
		}
		for(int i = n - k + 1; i <= n; i++) if(!dat[i].f) {
			a = i;
			break;
		}
		if(a & y) s1 = dat[y].va * 1.0 / dat[a].va;
		if(b & x) s2 = dat[x].va * 1.0 / dat[b].va;
		if(s1 < s2) cho[x] = 1, cho[b] = 0;
		else cho[y] = 1, cho[a] = 0;
	}
	for(int i = 1; i <= n; i++) if(cho[i]) ans = ans * dat[i].va % mod;
	cout << ans;
}