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 namespace std;
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
ll mod=1e9+7;			

			ll mul(ll a,ll b)
			{
				ll ans=(a%mod * b%mod +mod)%mod;
				return ans;
			}
	   
int main() {
			fast
			// #ifndef ONLINE_JUDGE
			// freopen("input.txt","r",stdin);
			// freopen("output.txt","w",stdout);
			// #endif

			ll t;
			t=1;
			while(t--)
			{
				ll n,k;
				cin>>n>>k;
				vector<pair<ll,ll>>arr;
				vector<ll>pos,neg;
				
				for(ll i=0;i<n;i++)
				{
					ll x,k;
					cin>>x;
					if(x<0)
					{
						
						// arr.pb({abs(x),1ll})
						neg.pb(abs(x));
					}
					else
					{
					
						// arr.pb({abs(x),0ll})
						pos.pb(abs(x));
					}
					
				}
				sort(arr.rbegin(),arr.rend());
				sort(pos.rbegin(),pos.rend());
				sort(neg.rbegin(),neg.rend());

				ll po=pos.size();
				ll ne=neg.size();

				// cout<<po<<" "<<ne<<endl;
				ll count=0;
				ll i=0;
				ll j=0;
				ll flag=0;
				ll ans=1;
				while(count<k)
				{
					ll rem=k-count;
					if(rem==1)
					{
						if(i<po)
						{
						ans=mul(ans,pos[i]);
						break;
						}
						else
						{
							flag=1;
							break;
						}
						
					}



					ll x1=0;
					ll check=0;

					// cout<<i<<" "<<po<<endl;
					if(i<po-1)
					{
						x1=pos[i]*pos[i+1];
					}
					else
					{
						ans=mul(ans,neg[j]);
						ans=mul(ans,neg[j+1]);
						count+=2;
						check=1;
						j+=2;
					}
					
					
					ll x2=0;
					// cout<<check<<" "<<x1<<" "<<x2<<endl;
					if(j<ne-1 and check==0)
					{
						x2=neg[j]*neg[j+1];
					}
					else if(check==0)
					{
						ans=mul(ans,pos[i]);
						ans=mul(ans,pos[i+1]);
						count+=2;
						check=1;
						i+=2;
					}
					
					if(check==0)
					{
						if(x2>=x1)
						{
							ans=mul(ans,x2);
							count+=2;
							j+=2;
						}
						else
						{
							ans=mul(ans,x1);
							count+=2;
							check=1;
							i+=2;
						}
						
					}
					
				}	
				// cout<<flag<<endl;
				
				if(flag==1)
				{
					ans=1;
					reverse(pos.begin(),pos.end());
					reverse(neg.begin(),neg.end());
					i=0;j=0;
					count=0;
					while(count<k)
					{
						if(i<po and j<ne)
						{
							if(pos[i]<neg[j])
							{
								ans=mul(ans,pos[i]);
								count+=1;
								i+=1;
							}
							else
							{
								ans=mul(ans,-1*neg[j]);
								count+=1;
								j+=1;
							}
							
						}
						else if(i<po)
						{
							ans=mul(ans,pos[i]);
								count+=1;
								i+=1;
						}
						else
						{
							ans=mul(ans,-1*neg[j]);
								count+=1;
								j+=1;
						}
					}
				}
				cout<<ans<<endl;		

				

				
			}
			return 0;
		 }