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++ (Clang 10.0.0) (judge verdict: Wrong Answer)


#include <iostream>
#include<stdio.h>
#include<cmath>
#include<set>
#include<unordered_set>
#include<map>
#include<unordered_map>
#include<string.h>
#include<algorithm>
#include<vector>
#include<bitset>
#include<string.h>
#include<stack>
#include<queue>
#include<deque>
 
#define ll long long
#define ld long double
#define FOR(x,y) for(ll i=x;i<=y;i++)
#define pb(x) push_back(x)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vii vector<int>
#define vll vector<ll>
#define vss vector<string>
#define PI (2*acos(0.0))
#define sqr(x) ((x)*(x))
#define sf(a)   scanf("%d",&a)
#define sfl(a)  scanf("%lld",&a)
#define sff(a,b)    scanf("%d %d",&a,&b)
#define sffl(a,b)   scanf("%lld %lld",&a,&b)
#define sfff(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)    scanf("%lld %lld %lld",&a,&b,&c)
#define ms(a,b) memset(a, b, sizeof(a))
#define ff  first
#define ss  second
#define lcm(a, b)   ((a)*((b)/__gcd(a,b)))
#define all(a)  a.begin(),a.end()
#define readfile freopen("input.txt","r",stdin);
#define writefile freopen("output.txt","w",stdout);
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
#define gap " "
//#define mx 104
#define inf (ll)1e9
#define WHITE 1
#define GRAY 2
#define BLACK 3
#define EMPTY_VALUE -1
#define mod 1000000007
#define MOD(a,b) (a%b + b)%b
using namespace std;
 
ll mul(ll a, ll b)
{
    return MOD((MOD(a,mod) * MOD(b,mod)),mod);
}
 
int main()
{
    //fastio
    ll n,k;
    sffl(n,k);
    ll x;
    vll pos,neg;
    for(ll i=0;i<n;i++)
    {
        sfl(x);
        if(x<0) neg.pb(x);
        else
        {
            pos.pb(x);
        }
    }
    if(pos.size()>0)
    {
        ll ans =1;
        sort(all(pos));
        if(k%2==1)
        {
            
            //ans =  MOD((MOD(ans,mod)*MOD(pos[pos.size()-1],mod)),mod);
            //ans = pos[pos.size()-1]*ans;
            ans = mul(ans,pos.back());
            //k--;
            --k;
            pos.pop_back();
        }
        sort(all(pos),greater<int>());
        sort(all(neg));
            vll ok;
 
            ll temp;
            if(pos.size()>0)
            {
                for(ll i=0;i+1<pos.size();i+=2)
                {
                    //temp = MOD((MOD(pos[i],mod)*MOD(pos[i+1],mod)),mod);
                    //temp = pos[i]*pos[i+1];
                    temp = mul(pos[i],pos[i+1]);
                    //cout<<"pos temp = "<<temp<<endl;
                    ok.pb(temp);
                }
            }
            
            if(neg.size()>0)
            {
                for(ll i=0;i+1<neg.size();i+=2)
                {
                    //temp = MOD((MOD(neg[i],mod)*MOD(neg[i+1],mod)),mod);
                    //temp = neg[i]*neg[i+1];
                    temp = mul(neg[i],neg[i+1]);
                    //cout<<"neg temp = "<<temp<<endl;
                    ok.pb(temp);
                }
                if(pos.size()%2 && neg.size()%2)
                {
                    //temp = MOD((MOD(neg[neg.size()-1],mod)*MOD(pos[pos.size()-1],mod)),mod);
                    //temp = neg[neg.size()-1] * pos[pos.size()-1];
                    temp = mul(neg.back(),pos.back());
                    ok.pb(temp);
                } 
            }
                
            if(ok.size()>0)
            {
                sort(all(ok),greater<int>());
           
                for(ll i=0;i<k/2;i++)
                {
                    //cout<<" ok value = "<<ok[i]<<endl;
                    //ans = MOD((MOD(ans,mod)*MOD(ok[i],mod)),mod);
                    ans = mul(ans,ok[i]);
                } 
            }
            cout<<ans<<endl;
    }
    else
    {
        ll ans =1; 
        sort(all(neg));  
        if(k%2)
        {
            sort(all(neg),greater<int>());
        }
       
            for(ll i=0;i<k;i++)
            {
                //ans = MOD((MOD(ans,mod)*MOD(neg[i],mod)),mod);
                ans = mul(ans,neg[i]);
            }
        
        cout<<ans<<endl;
        
    }
    
    return 0;
}