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 int long long
#define ALL(x) (x).begin(),(x).end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

typedef pair<int,int> PI;
typedef pair<int,pair<int,int>> PII;
static const int INF=1010000000000000017LL;
static const double eps=1e-12;
static const double pi=3.14159265358979323846;
static const int dx[4]={1,-1,0,0};
static const int dy[4]={0,0,1,-1};
static const int ddx[8]={1,-1,0,0,1,1,-1,-1};
static const int ddy[8]={0,0,1,-1,1,-1,1,-1};

template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;}

const int mod=1000000007;

int N,K;
int A[200005];

vector<PI> pls;
vector<PI> mns;
bool used[200005];

int modPow(int a, int n){
    if(n==1)return a%mod;
    if(n%2==1)return(a*modPow(a,n-1))%mod;
    int t=modPow(a,n/2);
    return (t*t)%mod;
}

signed main(){
    cin>>N>>K;
    for(int i=0;i<N;++i)cin>>A[i];
    for(int i=0;i<N;++i){
        if(A[i]>=0)pls.push_back(PI(A[i],i));
        else mns.push_back(PI(A[i],i));
    }
    sort(pls.begin(),pls.end(),greater<PI>());
    sort(mns.begin(),mns.end());
    int ans=1;
    if(N==K){
        for(int i=0;i<N;++i){
            ans*=A[i];
            ans%=mod;
        }
    }
    else if(pls.size()==0){
        for(int i=0;i<K;++i){
            ans*=mns[mns.size()-i-1].first;
            ans%=mod;
        }
    }
    else {
        vector<int> res;
        int p=0,m=0;
        while(res.size()<K){
            if(p!=pls.size()&&m!=mns.size()){
                if(pls[p].first>abs(mns[m].first)){
                    res.push_back(pls[p].first);
                    used[pls[p].second]=true;
                    p++;
                }
                else {
                    res.push_back(mns[m].first);
                    used[mns[m].second]=true;
                    m++;
                }
            }
            else if(p!=pls.size()){
                res.push_back(pls[p].first);
                used[pls[p].second]=true;
                p++;
            }
            else {
                res.push_back(mns[m].first);
                used[mns[m].second]=true;
                m++;
            }
        }
        for(int i=0;i<K;++i){
            ans*=abs(res[i]);
            ans%=mod;
        }
        if(m%2!=0){
            int mi1=INF,mi2=INF;
            int ma1=-INF,ma2=-INF;
            for(int i=0;i<N;++i){
                if(used[i]&&A[i]<0)chmin(mi1,abs(A[i]));
                else if(!used[i]&&A[i]>=0) chmax(ma1,A[i]);
            }
            for(int i=0;i<N;++i){
                if(used[i]&&A[i]>0)chmin(mi2,A[i]);
                else if(!used[i]&&A[i]<0) chmax(ma2,abs(A[i]));
            }
            if(ma1!=-INF&&mi1!=INF&&ma1*mi2>ma2*mi1){
                ans*=(ma1*modPow(mi1,mod-2))%mod;
                ans%=mod;
            }
            else {
                ans*=(ma2*modPow(mi2,mod-2))%mod;
                ans%=mod;
            }
        }
    }
    cout<<(ans+mod)%mod<<endl;
}