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;

#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define read()          freopen("in.txt","r",stdin)
#define write()         freopen("out.txt","w",stdout)
#define sf(n)           scanf("%d",&n)
#define sfd(n)          scanf("%lf",&n)
#define sl(x)           scanf("%I64d",&x)
#define sfl(n)          scanf("%lld", &n)
#define sfc(n)          scanf(" %c",&n)
#define sful(n)         scanf("%llu",&n)
#define ull             unsigned long long
#define endll           pf("\n")
#define pf              printf
#define PI              acos(-1.0)
#define eps             1e-12
#define mem(a,b)        memset(a,b,sizeof(a))
#define pb              push_back
#define xx              first
#define yy              second
#define pii             pair<int, int>
#define pll             pair<ll, ll>
#define MP make_pair
#define ll              long long
#define uniq(a)      a.erase(unique(a.begin(), a.end()), a.end())
#define cnd tree[idx]
#define lnd tree[idx<<1]
#define rnd tree[(idx<<1)+1]
#define lndp (idx<<1),(b),((b+e)>>1)
#define rndp ((idx<<1)+1),(((b+e)>>1)+1),(e)
#define inf 1999999999
#define MOD  1000000007
#define mod1 1000000009
#define var    int i,j,c=0,t,l,r,mid,z=0,k,x,y
#define aloc(n,t) ((t*)malloc((n)*sizeof(t)))

int dx[8]={0,0,1,1,-1,-1,1,-1};  //8 direction
int dy[8]={1,-1,1,-1,1,-1,0,0};
//int dx[4]={1,-1,0,0};  //4 direction
//int dy[4]={0,0,1,-1};


ll n,k,x;
vector<ll> v,p,z;
stack<ll> q,s;

int main()
{
    sfl(n);sfl(k);

    for(int i=0;i<n;i++)
    {
        sfl(x);
        if(x>0)v.pb(x);
        if(x<0)p.pb(x);
        if(x==0)z.pb(x);
    }

    if((v.size()+p.size()) <k)
    {
        pf("0\n");
        return 0;
    }
    if(v.size()==0 && p.size() && v.size())
    {
        if(k&1)
        {
            pf("0\n");
            return 0;
        }
    }
    int c = k;
    sort(v.begin(),v.end());
    reverse(v.begin(),v.end());
    sort(p.begin(),p.end());
    for(int i=0;i<v.size() && c>0;i++,c--)
    {
        q.push(v[i]);
    }

    if(v.size()==0 && z.size()==0 && k&1)
    {
        for(int i=p.size()-1;i>=0 && c>0;i--,c--)
        {
            //cout<<p[i]<<endl;
            s.push(p[i]);
        }
        ll ans =1;
            while(s.empty()==0)
            {
                //cout<<s.top()<<endl;
                ans*=(s.top());
                ans%=MOD;
                ans+=MOD;
                s.pop();
                ans%=MOD;
            }
            pf("%lld\n",ans);
            return 0;
    }

    if(c>0)
    {
        if(c&1)
        {
            if(q.empty() && z.size()>0)
            {
                pf("0\n");
                return 0;
            }
            if(q.empty()==0)
            {
                q.pop();
                c++;
            }
            if(p.size()<c && z.size()>0)
            {
                pf("0\n");
                return 0;
            }


            for(int i=0;i<p.size() && c>0;i++,c--)
            {
                s.push(p[i]);
            }


        }
        else
        {
            for(int i=0;i<p.size() && c>0;i++,c--)
            {
                s.push(p[i]);
            }
        }
    }
    for(int i=s.size();i<p.size();i+=2)
    {
        x = q.size();
        if(i+1<p.size() && x-2>=0)
        {
            ll x2 = q.top();
            q.pop();
            ll x1 = q.top();
            q.pop();

            if(x1*x2 > (p[i]*p[i+1]))
            {
                q.push(x1);
                q.push(x2);
                break;
            }
            else
            {
                s.push(p[i]);
                s.push(p[i+1]);
            }
        }
    }
    ll ans = 1;
    while(q.empty()==0)
    {
        ans*=q.top();
        q.pop();
        ans%=MOD;
    }
    while(s.empty()==0)
    {
        //cout<<s.top()<<endl;
        ans*=(s.top());
        ans%=MOD;
        ans+=MOD;
        s.pop();
        ans%=MOD;
    }
    pf("%lld\n",ans);
}