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: Accepted)

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <map>
#include <string.h>
#include <algorithm>
#include <bits/stdc++.h>
#include <vector>

using namespace std;
#define set_precision cout << fixed << setprecision(10)
#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 replb(i, a, b) for (ll i = a; i >= b; i--)
#define repl(i, a, b) for (ll i = a; i < b; i++)
#define repit(arr) for (auto it = arr.begin(); it != arr.end(); it++)
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define vl vector<long long int>
#define Vi vector<vector<int>>
#define seti set<int>
#define setl set<ll>
#define dseti set<int, greater<int>>
#define dsetl set<ll, greater<ll>>
#define mseti multiset<int>
#define msetl multiset<ll>
#define dmseti multiset<int, greater<int>>
#define dmsetl multiset<ll, greater<ll>>
#define sortA(arr) sort(arr.begin(), arr.end())
#define dsortA(arr) sort(arr.begin(), arr.end(), greater<int>())
#define ssort(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 cl_bf cin.ignore(numeric_limits<streamsize>::max(), '\n');
#define castl static_cast<ll>
int fact(int n) 
{ 
    int res = 1; 
    for (int i = 2; i <= n; i++) 
        res = res * i; 
    return res; 
}
int nCr(int n, int r) 
{ 
    return fact(n) / (fact(r) * fact(n - r)); 
}
int cel(int a,int b){return((a-1)/b+1);} 
int gcd(int a, int b)
{
    if (a < b)
        swap(a, b);
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
int lcm(int a,int b){
    return (a*b)/gcd(a,b);
} 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);int
    ll k,x,y,z,w,n;ll a=1000000007;;
    cin>>n>>k;ll p=1,ans=1,ans1=1,c=0,c1=0,c2=0;vl v,u,v1,u1;
    rep(i,0,n){
        cin>>x;p=(p*x)%a;
        if(x<0){u.pb(x);u1.pb(x);c1++;}
        else if(x>=0){v.pb(x);v1.pb(x);c2++;}
    }
    dsortA(u);sortA(v);sortA(u1);dsortA(v1);
    if(k==n){
        if(p<0)cout<<(p+a)%a;
        else cout<<p;}
    else if(c2==0){
         if((k%2)==0){
             rep(i,0,k){ans=(ans*u1[i])%a;}//cout<<ans<<" ";}
             cout<<(ans)%a;
         }
         else if(k%2){
             rep(i,0,k){ans=(ans*u[i])%a;}//cout<<ans<<" ";}
             cout<<(a+(ans))%a;
         }
    }
    else if(c1==0){
        rep(i,0,k){ans=(ans*v1[i])%a;}
        if(ans<0)cout<<((ans)+a)%a;
        else cout<<(ans);
    }
    else{
        ans1=1;c=0;
        while(1){
            if(c==k)break;
            else if(c==k-1){
                ans1=(ans1*v.back())%a;v.pop_back();c++;}
            else if(int(u.size())<=1){ans1=(ans1*v.back())%a;v.pop_back();c++;}
            else if(int(v.size())<=1){ans1=(ans1*u.back())%a;u.pop_back();ans1=(ans1*u.back())%a;u.pop_back();c+=2;}
            else{
                x=u.back();y=v.back();u.pop_back();v.pop_back();w=u.back();z=v.back();u.pop_back();v.pop_back();
                if(y*z>x*w){ans1=(ans1*(y))%a;u.pb(w);u.pb(x);v.pb(z);c++;}
                else{ans1=((ans1%a)*((x*w)%a))%a;v.pb(z);v.pb(y);c+=2;}
            }
        }
        cout<<(ans1)%a; 
    }
    return 0;
}