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)

/*
ID: aryan_luffy
LANG: C++
TASK: ride
*/
#include <bits/stdc++.h>
#define lli long long int
#define pb push_back
#define mod 1000000007
#pragma GCC optimize ("-O2")
#define mod2 998244353
#define MAXN 1000000000
#define v32 vector<int>
#define v64 vector<lli>
#define v1024 vector <vector <int>>
#define v4096 vector <vector <lli>>
#define vt vector
#define f(x, y, z) for (lli x = y; x < z; x++)
#define fd(x, y, z) for (lli x = y; x > z; x--)
#define lb lower_bound
#define ld long double
#define m64 map<lli,lli>
#define m32 map<int,int>
#define m64it map<lli,lli>::iterator
#define m32it map<int,int>::iterator
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define ist insert
#define endl "\n"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set tree<pair<int,int>, null_type,less<pair<int,int>>, rb_tree_tag,tree_order_statistics_node_update> 
#define p_q priority_queue 
#define min_p_q priority_queue <pair<lli,int>,vt <pair<lli,int>>,greater <pair<lli,int>>>
using namespace std;
using namespace __gnu_pbds; 

//USE AT YOUR OWN RISK
lli tmod(lli x,lli m=mod){return (x%m+m)%m;}

template <typename T> 
void DEBUG_ARR(vt<T> a,char c)
{
	f(i,0,a.size())cout<<a[i]<<c;
	cout<<"\n";
}

//make sure x is below mod
lli power(lli x, lli y,lli m=mod) 
{
    lli res = 1;  
   
    while (y > 0) 
    { 
        if (y & 1) 
            res = (res*x)%m; 
        y = y>>1; 
        x = (x*x)%m;  
    }
    if(res<0)
       res+=m; 
    return res; 
}

// lli fact[2000009];
// lli ncr(lli n,lli r){
//     lli ans=fact[n];
//     if(r>n)return 1;
//     if(r<0)return 0;
//     if(n<0)return 0;
//     ans=(ans*power(fact[n-r],mod-2))%mod;
//     ans=(ans*power(fact[r],mod-2))%mod;
//     return ans;
// }
void normalize(lli &a,lli &b){
    if(a==0 && b==0)return;
    if(a<0){
        a=-a;
        b=-b;
    }
    else if(a==0){
        if(b<0)b=-b;
    }
    lli g=__gcd(abs(a),abs(b));
    if(g!=0){
        a/=g;
        b/=g;
    }
}
const int N=2e5+5;

//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
// int smallestprime[(int)1e7+10];
// int nodivisors[(int)1e7+5];

int main() 
{ 
    // fact[0]=1;
    // f(i,1,2000001)fact[i]=(i*fact[i-1])%mod;
    // f(i,2,10000001){
    //     if(smallestprime[i]==0)
    //     for(lli j=1;j*i<=10000000;j++){
    //         if(smallestprime[j*i]==0)
    //         smallestprime[j*i]=i;
    //     }
    // }
    // nodivisors[1]=1;
    fastio;
    int t=1;
    // cin>>t;
    // int temp=t;
    // cout<<smvalallestprime[5]<<"\n";
	while(t--){
        // cout<<"Case #"<<temp-t<<": ";
        // clock_t t=clock();
        ifstream in("ride.in");
        ofstream out("ride.out");
        // lli n,k;cin>>n>>k;
        // int mat[n][n],isvalid[n][n];
        // f(i,0,n)f(j,0,n)isvalid[i][j]=0;
        lli n,k;cin>>n>>k;
        vt <pair<lli,lli>> a;
        lli zero=0;
        f(i,0,n){
            int x;cin>>x;
            if(x>0)a.push_back({x,0});
            else if(x<0) a.push_back({-x,1});
            else zero++;
        }
        int sum2=0;
        n=a.size();
        if(k>a.size()){
            cout<<0<<"\n";
            return 0;
        }
        sort(a.rbegin(),a.rend());
        f(i,0,n)sum2+=a[i].second;
        if(sum2==n){
            lli ans=1;
            if(k&1){
                reverse(a.begin(),a.end());
                // cout<<"KAMI SAMA\n";
                f(i,0,k)ans=(ans*a[i].first)%mod;
                ans=tmod(-ans);
                if(zero)ans=0;
            }
            else{
                f(i,0,k)ans=(ans*a[i].first)%mod;
            }
            cout<<tmod(ans)<<"\n";
            return 0;
        }
        lli ans=1,sum=0;
        f(i,0,k)ans=(ans*a[i].first)%mod;
        f(i,0,k)sum+=a[i].second;
        if((sum&1)==0){
            cout<<tmod(ans)<<"\n";
            return 0;
        }
        else{
            pair<lli,lli>posr={-1,-1},negr={-1,-1};
            fd(i,k-1,-1){
                if(a[i].second==0){
                    posr.first=a[i].first;
                    break;
                }
            }
            f(i,k,n){
                if(a[i].second){
                    posr.second=a[i].first;
                    break;
                }
            }
            fd(i,k-1,-1){
                if(a[i].second==1){
                    negr.first=a[i].first;
                    break;
                }
            }
            f(i,k,n){
                if(a[i].second==0){
                    negr.second=a[i].first;
                    break;
                }
            }
            if(zero){
                if((posr.first==-1 || posr.second==-1) && (negr.first==-1 || negr.second==-1)){ans=0;
                cout<<0<<"\n";
                return 0;}
            }
            if(posr.first==-1 || posr.second==-1){
                posr=negr;
                ans=tmod(ans*posr.second);
                ans=tmod(ans*power(posr.first,mod-2));
            }
            else if(negr.first==-1 || negr.second==-1){
                ans=tmod(ans*posr.second);
                ans=tmod(ans*power(posr.first,mod-2));
            }
            else if(posr.second*negr.first>posr.first*negr.second){
                ans=tmod(ans*posr.second);
                ans=tmod(ans*power(posr.first,mod-2));
            }
            else{
                posr=negr;
                ans=tmod(ans*posr.second);
                ans=tmod(ans*power(posr.first,mod-2));
            }
            
            cout<<ans<<"\n";
        }
        // cout<<clock()-t<<"\n";
    }   
    return 0; 	
}