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: Accepted)

#include <bits/stdc++.h>
using namespace std;

#define io ios::sync_with_stdio(false)
#define pb push_back
#define pqueue priority_queue
#define fi first
#define se second
#define ls rt<<1
#define rs rt<<1|1
#define sz(x) (int)(x).size()
#define dbg(x) cout<<#x<<" --- "<<x<<endl
#define mst(x,a) memset(x,a,sizeof(x))
#define fin(a) freopen(a,"r",stdin)
#define fout(a) freopen(a,"w",stdout)
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll gcd(ll a,ll b){ return b==0?a:gcd(b,a%b); }
ll qpow(ll a,ll b,ll m){ ll r=1;a%=m;for(;b;b>>=1){if(b&1)r=r*a%m;a=a*a%m;}return r; }
const int inf=0x7fffffff;
const ll mod=1e9+7;
const int maxn=2e5+10;
vector<int> a,b,bb;
ll c[maxn],cc[maxn];
int n,k;

bool cmp(int x,int y){
	return abs(x)>abs(y);
}

int main(){
	io;
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		int x;cin>>x;
		bb.pb(x);
		if(x<0)b.pb(x);
		else a.pb(x);
	}
	sort(a.begin(),a.end());
	sort(b.begin(),b.end());
	sort(bb.begin(),bb.end(),cmp);
	for(int i=0;i<sz(b);i++){ b[i]+=mod;b[i]%=mod; }
	if(n==k){
		ll res=1;
		for(int x:a){ res*=x;res%=mod; }
		for(int x:b){ res*=x;res%=mod; }
		cout<<res<<endl;
		return 0;
	}
	if(sz(a)==0){
		ll ans=1;
		if(k%2==1){
			// ans=(b.back()%mod+mod)%mod;
			for(int i=sz(b)-1;i>=sz(b)-k;i--){
				// ll xx=b[i]*b[i-1];xx%=mod;
				ans*=b[i];ans%=mod;
			}
			cout<<ans<<endl;
		}
		else {
			for(int i=0;i<k;i++){
				ans*=b[i];ans%=mod;
				// ll xx=b[i]*b[i+1];xx%=mod;
				// ans*=xx;ans%=mod;
			}
			cout<<ans<<endl;
		}
	}
	else {
		int cnt1=0,cnt2=0;
		for(int i=0;i<k;i++){
			if(bb[i]<0)cnt1++;
			else cnt2++;
		}
		if(cnt1%2==1){
			bool ff=false;
			if(cnt2==0)ff=true;
			cnt1--;cnt2--;
			ll ans=1;
			for(int i=0;i<k;i++){
				if(bb[i]<0&&cnt1>0){
					ans*=(bb[i]+mod)%mod;
					ans%=mod;
					cnt1--;
				}
				if(bb[i]>=0&&cnt2>0){
					ans*=bb[i];ans%=mod;
					cnt2--;
				}
			}
			ll x1=1,x2=1;
			bool f=false;
			for(int i=k;i<sz(bb);i++){
				if(bb[i]>=0){
					f=true;
					x1*=bb[i];
					break;
				}
			}
			for(int i=k;i<sz(bb);i++){
				if(bb[i]<0){
					x2*=bb[i];
					break;
				}
			}
			for(int i=k-1;i>=0;i--){
				if(bb[i]>=0){
					x1*=bb[i];
					break;
				}
			}
			for(int i=k-1;i>=0;i--){
				if(bb[i]<0){
					x2*=bb[i];
					break;
				}
			}
			if(!f){
				ans*=x2%mod;
				ans%=mod;
			}
			else if(ff){
				ans*=x1%mod;
				ans%=mod;
			}
			else if(x1>x2){
				ans*=x1%mod;
				ans%=mod;
			}
			else {
				ans*=x2%mod;
				ans%=mod;
			}
			cout<<ans<<endl;
		}
		else {
			ll ans=1;
			for(int i=0;i<k;i++){
				ans*=(bb[i]+mod)%mod;ans%=mod;
			}
			cout<<ans<<endl;
		}
	}
	return 0;
}