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> 
/*#ifndef M_PI
    #define M_PI 3.14159265358979323846
#endif
*/

using namespace std;
#define m 1000000007
#define rep(i,a,b) for(int i=a;i<b;++i)
#define repb(i,a,b) for(int i=a;i>=b;--i)
#define repv(arr)  for(auto it=x.begin();it!=x.end();++it)  
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define vl vector<long long int>
#define ssortA(arr)   stable_sort(arr.begin(),arr.end())
#define search(arr,c)  binary_search(arr.begin(),arr.end(),c)
#define pb   push_back
#define ll long long
#define mp make_pair 
#define LCM(a,b) boost::math::lcm(a,b)
#define cl_bf cin.ignore(numeric_limits<streamsize>::max(), '\n');
int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n1,k;
    cin>>n1>>k;
    vl arr(n1);
    vector<pair<ll,int> > p,n;
    int c1=0,c2=0;

    rep(i,0,n1){
        int x;
        cin>>x;
        arr[i]=abs(x);
        if(x<0)
        {c2++;
        n.pb(mp(abs(x),i));
        }
        else{
            c1++;
            p.pb(mp(abs(x),i));
        }
    }
    ll pro=1;
    if(c1==0&&k%2!=0){
        ssortA(arr);
        rep(i,0,k){
            pro=(pro*arr[i])%m;
        }
        pro=(m+(pro*-1)%m)%m;
    }
    else{
        if(c2%2!=0&&k==n1){
            ssortA(arr);
        rep(i,0,k){
            pro=(pro*arr[i])%m;
        }
        pro=(m+(pro*-1)%m)%m;
        }
        else{

        
    
    stable_sort(p.begin(),p.end(),greater<pair<ll,int> >());
    stable_sort(n.begin(),n.end(),greater<pair<ll,int> >());
    vector<pair<ll,int> > f;
    for(int i=0;i<2*(p.size()/2);i+=2){
            ll y=p[i].first;
           ll  x=p[i+1].first;
            ll p=(x*y)%m;
           // cout<<p<<endl;
            f.pb(mp(p,1));
    }
    for(int i=0;i<2*(n.size()/2);i+=2){
            ll y=n[i].first;
           ll  x=n[i+1].first;
            ll p=(x*y)%m;
            //cout<<p<<endl;
            f.pb(mp(p,2));
    }
    stable_sort(f.begin(),f.end(),greater<pair<ll,int> >());
    if(k%2==0){
        rep(i,0,k/2){
            pro=(pro*f[i].first)%m;
        }
    }
    
    else{
        int r=0;
        rep(i,0,k/2){
           if(f[i].second==1)
           ++r;
            pro=(pro*f[i].first)%m;
        }
        r*=2;
        pro=pro*(p[r].first);
    }
        }
    }
    cout<<pro<<endl;
}