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: Runtime Error)

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define deb(x) cout<< #x << " " << x << "\n";
#define INF 9223372036854775807
const ll mod=1e9+7;

bool sort1(ll a,ll b){
    return abs(a)>abs(b);
}

bool sort2(ll a,ll b){
    return abs(a)<abs(b);
}
 
int main() {
    
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    ll n,k;
    cin>>n>>k;
    vector<ll> a(n);
    ll p=0,nz=0,z=0;
    for(ll i=0;i<n;i++){
        cin>>a[i];
        if(a[i]==0) z=1;
        if(a[i]!=0) nz++;
        if(a[i]>0) p++;
    }
    if(k==1){
        sort(a.begin(),a.end(),greater<ll>());
        cout<<a[0];
        return 0;
    }
    if(k>nz){
        cout<<0;
        return 0;
    }
    vector<ll> b=a;
    a.clear();
    for(ll i=0;i<n;i++){
        if(b[i]!=0)
            a.pb(b[i]);
    }
    n=a.size();
    if(k==n){
        ll ans=1;
        for(ll i=0;i<n;i++)
            ans=(ans*a[i])%mod;
        if(ans<0) ans+=mod;
        cout<<ans;
        return 0;
    }
    if(p==0){
        if(k%2==0) sort(a.begin(),a.end());
        else{
            if(z==1){
                cout<<0;
                return 0;
            }
            sort(a.begin(),a.end(),greater<ll>());
        }
        ll ans=1;
        for(ll i=0;i<k;i++)
            ans=(ans*a[i])%mod;
        assert(ans<0);
        ans+=mod;
        cout<<ans;
        return 0;
    }
    if(k>p){
        sort(a.begin(),a.end(),sort1);
        ll ans=1,mnp=INF,mnn=-1e9;
        bool neg=false;
        vector<ll> fin;
        for(ll i=0;i<k;i++){
            fin.pb(a[i]);
            if(a[i]>0) mnp=min(mnp,a[i]);
            if(a[i]<0) mnn=max(mnn,a[i]);
            if(a[i]<0) neg=!neg;
        }
        if(neg){
            if(a[k]>0){
                fin.erase(find(fin.begin(),fin.end(),mnn));
                fin.pb(a[k]);
            }
            else{
                fin.erase(find(fin.begin(),fin.end(),mnp));
                fin.pb(a[k]);
            }
        }
        for(ll i=0;i<fin.size();i++)
            ans=(ans*fin[i])%mod;
        assert(ans>0);
        cout<<ans;
        return 0;
    }
    vector<ll> pos,neg;
    for(ll i=0;i<n;i++){
        if(a[i]>0) pos.pb(a[i]);
        else if(a[i]<0) neg.pb(a[i]);
    }
    sort(pos.begin(),pos.end(),greater<ll>());
    sort(neg.begin(),neg.end(),greater<ll>());
    vector<ll> fin,fin1;
    for(ll i=0;i<k;i++)
        fin.pb(pos[i]);
    //cout<<fin.size()<<" ";
    while(neg.size()>1 && fin.size()>1){
        ll n1=fin.size(),n2=neg.size();
        ll a1=fin[n1-1], a2=fin[n1-2];
        ll b1=neg[n2-1], b2=neg[n2-2];
        if(a1*a2>=b1*b2){
            neg.pop_back();
            neg.pop_back();
        }
        else{
            fin.pop_back();
            fin.pop_back();
            fin1.pb(neg.back());
            neg.pop_back();
            fin1.pb(neg.back());
            neg.pop_back();
        }
    }
    ll ans=1;
    for(ll i=0;i<fin.size();i++)
        ans=(ans*fin[i])%mod;
    for(ll i=0;i<fin1.size();i++)
        ans=(ans*abs(fin1[i]))%mod;
    assert(ans>0);
    cout<<ans;

    return 0;
}