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>
 
#define int         long long
#define uint        unsigned int
#define ld          long double
#define showoff     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb          push_back
#define pii         pair<int,int>
#define FOR(i,a,b)  for(int i=a;i<b;++i)
#define RFOR(i,a,b) for(int i=a;i>b;--i)
#define f           first
#define se          second
#define maxn        200005
#define all(v)      v.begin(),v.end()
#define sz(x)       (int)x.size()
#define mod         1000000007
#define pqueue      priority_queue<int>
#define pdqueue     priority_queue< int,vector<int> ,greater< int >>
 
using namespace std;
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 
 
int power(int a,int n)
{
    a %= mod;
    if(n == 1)return a;
    if(n == 0)return 1;
    if(n%2)return (a*(power((a*a)%mod,n/2)%mod))%mod;
    return power((a*a)%mod,n/2)%mod;
}
const int inf = (int) 1e18;
 
int inverse(int x){
    return power(x,mod-2)%mod;//little fermat....
}   

void solve(){
    int n,k;
    cin >> n >> k;
    vector<int>v(n+5),p,ne;
    FOR(i,1,n+1){
        cin >> v[i];
        if(v[i] >= 0)p.pb(v[i]);
        else ne.pb(-v[i]);
    }
    if(sz(ne)+sz(p) == k){
        int ans = 1;
        FOR(i,1,n+1)ans *= (v[i]+mod)%mod,ans %= mod;
        cout << ans << "\n";
        return ;
    }
    sort(ne.begin(),ne.end(),greater<int>());
    sort(p.begin(),p.end(),greater<int>());
    int ans = 0;
    if(!sz(p)){
        if(k%2 != 0){
            ans = 1;
            sort(ne.begin(),ne.end());
            FOR(i,0,k){
                ne[i] = (-1*ne[i]+mod)%mod;
                ans *= ne[i]%mod,ans %= mod;
            }
        }
        else {
            ans = 1;
            FOR(i,0,k)ans *= ne[i],ans %= mod;
        }
    }
    else {
        int nt = min((sz(ne)/2)*2,k),pt = k-nt;
        while(nt > 0){
            if(pt+2 <= sz(p) && ne[nt-1]*ne[nt-2] < p[pt]*p[pt+1]){
                pt += 2;
                nt -= 2;
            }
            else break;
        }
        ans = 1;
        FOR(i,0,pt)ans *= p[i],ans %= mod;
        FOR(i,0,nt)ans *= ne[i],ans %= mod;
    }
    cout << ans << "\n";
}

signed main()
{
    showoff;
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}
//*->for large size of matrix take int not long long if possible......
//*->always take maximum as inf for safer side ...#include <bits/stdc++.h>