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>
using namespace std;
typedef		 long long int     ll;
typedef		vector<ll>      vll;
#define     fi              first
#define     se              second
#define		pb              push_back
#define		nn              "\n"
#define		all(p)          p.begin(),p.end()
#define		zz(v)           (ll)v.size()
#define 	S(a)            scanf("%lld",&a)
#define 	SS(a,b)         scanf("%lld %lld",&a,&b)
#define 	SSS(a,b,c)      scanf("%lld %lld %lld",&a,&b,&c)
#define		ss              ' '
#define     pii             pair<ll,ll>
#define		gcd(a,b)        __gcd(a,b)
#define		lcm(a,b)        (a*b)/gcd(a,b)
const double eps = 1e-8;







int main()
{
//    ios::sync_with_stdio(0);
//    cin.tie(0)


    ll n,k,mod=1e9+7;
    cin>>n>>k;
    vll v;
    ll p=0,m=0;
    for(ll i=0; i<n; ++i)
    {
        ll a;
        cin>>a;
        if(a!=0)
            v.pb(a);

        if(a>0)
            p++;
        else if(a<0)
            m++;
    }
    ll ans=1;
    if(p+m<k)
    {
        cout<<0<<nn;
        return 0;
    }
    else if(n==k)
    {
        for(ll i=0; i<k; i++)
            ans=(ans*v[i])%mod;
        cout<<ans<<nn;
        return 0;

    }
    sort(all(v));

    if(p==0)
    {

        if(k%2)
            reverse(all(v));
        for(ll i=0; i<k; i++)
            ans=(ans*v[i])%mod;
    }
    else if(m==0)
    {
        reverse(all(v));
        for(ll i=0; i<k; i++)
            ans=(ans*v[i])%mod;
    }
    else
    {
        reverse(all(v));
        vll p1,p2,pp;
        for(ll i=0; i<k; i++)
        {
            if(v[i]>0)
                p1.pb(v[i]);
            else
                pp.pb(v[i]);
        }
        ll f=1;

        for(ll i=k; i<zz(v); i++)
            if(v[i]<0)
                p2.pb(abs(v[i]));
        sort(all(p2));
        if(zz(p2))
        {

            if(p<k)
            {
                ll b=p2.back();
                p2.pop_back();
                sort(all(p1));
                p1.erase(p1.begin());
                ll df=k-p;
                if(df&1)
                {
                    p1.pb(b);
                }
            }
            for(ll i=0; i<zz(pp); i++)
            {
                p1.pb(pp[i]);
            }
            for(ll i=0; i<zz(p1); i++)
                p1[i]=abs(p1[i]);
            sort(all(p1));
            reverse(all(p2));
            vll in,out;
            ll i=1,j=1;
            while(1)
            {
                if(i<zz(p1)&&j<zz(p2))
                {
                    ll sum1=p1[i]*p1[i-1];
                    ll sum2=p2[j]*p2[j-1];
                    if(sum1<sum2)
                    {
                        in.pb(p2[j]);
                        in.pb(p2[j-1]);
                        out.pb(i);
                        out.pb(i-1);
                        i+=2;
                        j+=2;
                    }
                    else
                        break;
                }
                else
                {
                    break;
                }
            }
            if(zz(in))
            {
                for(ll i=1; i<zz(out); i+=2)
                {
                    p1[out[i]]=in[i];
                    p1[out[i-1]]=in[i-1];
                }
            }

            for(ll i=0; i<zz(p1); i++)
            {
                ans=(ans*p1[i])%mod;
            }
            //  cout<<nn;


        }



    }

    if(ans<0)
        cout<<mod-abs(ans)<<nn;
    else
        cout<<ans<<nn;















}


/* Final check before submit :

1. array size or integer overflow.

2. Think twice,code once.check all possible counter test case.

3. Be careful about corner case! n=1?k=1? something about 0?

4. avoid stupid mistake!complexity(t/m)?precision error?submit same code twice?

5. if got WA than remember that you are missing something common.
   *** Be confident!!! who knows? may be your one step back to AC ***

*/