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)


/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,fma")
*/
#include <bits/stdc++.h>
#define LL              long long
#define ll              long long
#define SL(n)           scanf("%lld",&n)
#define LF              double
#define SF(n)           scanf("%lf",&n)
 
#define pb              push_back
#define mp              make_pair
 
#define ON(n,i)         (n|(1LL<<i))
#define OFF(n,i)        (n&(~(1LL<<i)))
#define CHK(n,i)        (n&(1LL<<i))
 
#define ff              first
#define ss              second
 
#define SET(arr)        memset( arr , -1 , sizeof arr)
#define RESET(arr)      memset( arr,0,sizeof arr)
 
#define srt(v)          sort(v.begin(),v.end())
#define uniq(v)			v.resize(distance(v.begin(),unique(v.begin(),v.end())))
#define found( s,d )    (s.find(d) != s.end())
 
#define FOR(i,s,e)      for( LL i = s ; i<=e ; i++ )
#define RFOR(i,e,s)     for( LL i = e ; i>=s ; i-- )
         
 
#define FILEIN          freopen("in.txt","r",stdin)
#define FILEOUT         freopen("WA.txt","w",stdout)
 
#define DEBUG(x)        cerr<<#x<<" = "<<x<<endl
#define PRINTALL(v)     {cerr<<endl<<"Printing: "<<#v<<endl;for(LL i = 0 ; i<v.size(); i++) DEBUG(v[i]); cerr<<endl;}
 
using namespace std;

const LL lim = 2e5+10;
const LL mod = 1000000007;
LL a[lim];

priority_queue < LL > neg,pos;

int main(){
	
	LL N,K;
	SL(N); SL(K);
	FOR(i,1,N) {
		SL(a[i]);
		if(a[i]<0) neg.push(a[i]);
		else pos.push(a[i]);
	}
	
	LL ans = 1;
	
	if( neg.empty() ){
		while(K--){
			ans*=(( pos.top() + mod)%mod) ; pos.pop();
			ans%=mod;
		}
	}
	else if(pos.empty()){
		sort(a+1,a+N+1);
		if(K%2){
			for( LL i = N , j = K ; j ; j--,i-- ){
				ans*=((a[i]+mod)%mod);
				ans%=mod;
			}
		}
		else{
			FOR(i,1,K) {
				ans*=(( a[i] + mod)%mod);
				ans%=mod;
			}
		}
	}
	else{
		sort(a+1,a+N+1);
		LL l = 1 ,r = N;
		while(K){
			if(K==1){
				if( a[l]>a[r] ){
					ans *= ((a[l]+mod)%mod);
					ans %= mod;
					l++;
				}
				else{
					ans *= ((a[r]+mod)%mod);
					ans %= mod;
					r--;
				}
				K--;
			}
			else{
				if(  a[l+1]<0 && a[r-1]>0 ){
					if( a[l]*a[l+1] > a[r]*a[r-1]){
						ans *= ((a[l]+mod)%mod);
						ans %= mod;
						
						ans *= ((a[l+1]+mod)%mod);
						ans %= mod;
						
						l+=2; 
						K-=2;
					}
					else{
						ans *= ((a[r]+mod)%mod);
						ans %= mod;
						ans *= ((a[r-1]+mod)%mod);
						ans %= mod;
						r-=2;
						K-=2;
					}
				}
				else{
					if( a[l]>a[r] ){
						ans *= ((a[l]+mod)%mod);
						ans %= mod;
						l++;
					}
					else{
						ans *= ((a[r]+mod)%mod);
						ans %= mod;
						r--;
					}
					K--;
				}
			}
		}
	}
	
	cout<<ans<<endl;
	
return 0;}