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 <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i,n) for(int i=0, i##_len=(int)(n); i<i##_len; i++)
#define reps(i,n) for(int i=1 , i##_len=(int)(n);i<=i##_len;i++)
#define rrep(i,n) for(int i=((int)(n)-1);i>=0;i--)
#define rreps(i,n) for(int i=((int)(n));i>0;i--)
#define repi(i,x) for(auto i=(x).begin(),i##_fin=(x).end();i!=i##_fin;i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define solve(a) ((a)?"Yes":"No")
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int , int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long , long long> P;
typedef vector<P> VP;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1;} return 0;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1;} return 0;}
const long long INFLL = 1LL<<60;
const int INF = 1<<30;
const double PI=acos(-1);
ll mod =1e9+7;
int main(){
	int n,k;
	V po;
	V ne;
	pair<ll,bool>up=mp(1LL,true),down(1LL,true);
	cin >>n>>k;
	ll ans=0;
	V a(n);
	rep(i,n){
		cin>>a[i];
		if(a[i]>0LL){
			po.push_back(a[i]);
		}else if(a[i]<0LL){
			ne.push_back(a[i]);
		}
	}
//	clog<<"--"<<po.size()<<" "<<ne.size()<<endl;
	sort(all(po),greater<ll>());
	sort(all(ne));
	if(po.size()+ne.size()!= n){
		if(n-ne.size()/2  >po.size()){
			cout<<0<<endl;
			return 0;
		}
	}
	int ter1=0,ter2=0;
	ll bef1=0LL;
	ll bef2=0LL;
	//upper
	for(int i=0;i<k and (ter1<po.size() or ter2<ne.size());){
		bef1=0LL;
		bef2=0LL;
		if(i!=k-1){
		if(ter1 + 1 <po.size()){
			bef1=po[ter1]*po[ter1+1];
		}if(ter2+1 <ne.size()){
			bef2=ne[ter2]*ne[ter2+1];
		}
	//	printf("(%lld %lld)\n",bef1,bef2);
		if(bef1 < bef2){
			up.F*=bef2%mod;
			up.F%=mod;
			ter2+=2;
			i+=2;
		}else if (bef1 !=0LL){
			up.F*=bef1%mod;
			up.F%=mod;
			ter1+=2;
			i+=2;
		}else if(ter1 <po.size()){
			up.F*=po[ter1]%mod;
			up.F%=mod;
			ter1++;
			i++;
		}else{
			up.F*=ne[ter2]%mod;
			up.F %= mod;
			ter2++;
			up.S^=true;
			i++;
		}

		}else{
			if (ter1 < po.size())
			{
				up.F *= po[ter1] % mod;
				up.F %= mod;
				ter1++;
				i++;
			}
			else
			{
				up.F *= ne[ter2]%mod;
				up.F %= mod;
				ter2++;
				up.S ^= true;
				i++;
			}
	}}
	if(ter1+ter2<k){
		up.F=INFLL;
		up.S=false;
	}
	sort(all(ne), greater<ll>());
	sort(all(po));
	ter1=0;
	ter2=0;
	for (int i = 0; i < k and (ter1 < po.size() or ter2 < ne.size());)
	{
		bef1 = 0LL;
		bef2 = 0LL;
	//	clog<<" :: "<<down.F<<endl;
		if (i == 0 and ne.size() != 0){
			down.F *= ne[0];
			down.F %= mod;
			ter2++;
			i++;
			down.S ^= true;
		}else if (i != k - 1){
			if (ter1 + 1 < po.size()){
				bef1 = po[ter1] * po[ter1 + 1];
			}
			if (ter2 + 1 < ne.size())	{
				bef2 = ne[ter2] * ne[ter2 + 1];
			}
		//	printf("(%3d %3d)\n", bef1, bef2);
			if (bef1 > bef2 and bef2 != 0LL)
			{
				down.F *= bef2 % mod;
				down.F %= mod;
				ter2 += 2;
				i += 2;
			}else if(bef1!=0LL){
				down.F *= bef1 % mod;
				down.F %= mod;
				ter1 += 2;
				i += 2;
			}
			else if (ter1 < po.size())
			{
				down.F *= po[ter1] % mod;
				down.F %= mod;
				ter1++;
				i++;
			}
			else
			{
				down.F *= ne[ter2];
				down.F %= mod;
				ter2++;
				down.S ^= true;
				i++;
			}
		}
		else
		{
			if (ter1 < po.size())
			{
				down.F *= po[ter1] % mod;
				down.F %= mod;
				ter1++;
				i++;
			}
			else
			{
				down.F *= ne[ter2];
				down.F %= mod;
				ter2++;
				down.S ^= true;
				i++;
			}
		}
	}
	down.F+=mod;
	down.F%=mod;
	if (ter1 + ter2 < k)
	{
		down.F = INFLL;
		down.S = true;
	}
	if(up.S==true){
		cout<<up.F%mod<<endl;
	}else if (up.S=false and up.F==INFLL){
		cout<<0<<endl;
	}else if(down.S==false){
		cout<<down.F%mod<<endl;
	}else{
		cout<<0<<endl;
	}
//	clog<<up.F<<" "<<up.S<<endl;
//	clog << down.F <<" "<< down.S << endl;
}
/*
1 2 -3 -4
-3 -4 2 -> 12
-4 -3 2 -> 24

1 -2 -3 -4
-2 -3 -4 -5 3
3 0 9 4 -> +
1 0 8 9 -> -
1 0 8 8 -> +
*/