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)

/*
    author : Aryan Agarwal, IIT KGP
    created : 07-July-2020 17:37:29
*/

#include<bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef unsigned long long ull;
#define int ll

const int INF=(int)2e18;
const int MOD = 1000000007; /*more than (10)^9*/  /*7 + 1e9*/
const int PEG = 998244353; /*less than (10)^9*/   /*1 + 7*17*(2)^23*/
long double pie=acos(-1);

typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int, int > pii;
typedef vector< pii > vpii;
#define X first
#define Y second
#define pb push_back
#define sz(a) (ll)(a).size()
#define all(a) (a).begin(),(a).end()

#define F(i,a,b) for(ll i=a;i<=b;i++)
#define RF(i,a,b) for(ll i=a;i>=b;i--)
#define Fo(i,a,b,j) for(ll i=a;i<=b;i+=j)
#define RFo(i,a,b,j) for(ll i=a;i>=b;i-=j)
#define in(a,n) F(i,0,n-1)cin>>a[i]
#define in1(a,n) F(i,1,n)cin>>a[i]
#define out(a,n) F(i,0,n-1)cout<<a[i]<<" "
#define out1(a,n) F(i,1,n)cout<<a[i]<<" "

#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define popcount __builtin_popcountll

#define pfy cout<<"YES"
#define pfn cout<<"NO"
#define pfn1 cout<<"-1"  // printf negative 1
#define pf1 cout<<"1"
#define nl cout<<"\n"

#define watch(x) cout<<#x<<" is "<<x<<"\n"
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>void __f(const char* name,Arg1&& arg1);
template <typename Arg1,typename... Args>void __f(const char* names,Arg1&& arg1,Args&&... args);

ll binpow(ll x,ll y,ll p);

bool comp(int a,int b)
{
    return abs(a)>abs(b);
}

bool comp2(int a,int b)
{
    return abs(a)<abs(b);
}

signed main()
{
    #ifndef ONLINE_JUDGE
    // freopen("input.txt","r",stdin);
    #endif
    fast;

    int n,k;
    cin>>n>>k;

    int a[n];
    F(i,0,n-1)
    {
        cin>>a[i];
    }

    sort(a,a+n,comp);

    F(i,0,k-1)
    {
        if(a[i]==0)
        {
            cout<<0;
            return 0;
        }
    }

    int cntn=0;

    F(i,0,k-1)
    {
        if(a[i]<0)cntn++;
    }

    int cntn2=0;

    F(i,0,n-1)
    {
        if(a[i]<0)cntn2++;
    }


    int ans=1;

    // dbg(cntn2,n,k);

    if(n==k || (cntn2==n && k%2))
    {
        // cout<<"**";
        sort(a,a+n,comp2);
        ans=1;
        F(i,0,k-1)
        {
            ans*=((a[i]%MOD+MOD)%MOD);
            ans=(ans%MOD+MOD)%MOD;
        }

        cout<<ans;
        return 0;
    }

    if(cntn%2==0)
    {
        F(i,0,k-1)
        {
            ans*=a[i];
            ans=(ans%MOD+MOD)%MOD;
        }
        
        cout<<ans;
        return 0;
    }

    else
    {
        int denp=0,denn=0,nump=0,numn=0;
        
        F(i,0,k-1)
        {
            if(a[i]>0)denp=a[i];
            if(a[i]<0)denn=a[i];
        }

        RF(i,n-1,k)
        {
            if(a[i]>0)nump=a[i];
            if(a[i]<0)numn=a[i];
        }

        if(nump==0 && numn==0)
        {
            cout<<0;
            return 0;
        }

        if(denp==0)
        {
            ans=nump;

            F(i,0,k-2)
            {
                ans*=a[i];
                ans=(ans%MOD+MOD)%MOD;
            }
            cout<<ans;
            return 0;
        }

        if(nump==0)
        {
            
            ans=numn;
            ans=(ans%MOD+MOD)%MOD;

            int ok=1;
            F(i,0,k-1)
            {
                if(a[i]==denp && ok){ok=0;continue;}
                ans*=a[i];
                ans=(ans%MOD+MOD)%MOD;
            }
            cout<<ans;
            return 0;
        }

        if(numn==0)
        {
            
            ans=nump;
            ans=(ans%MOD+MOD)%MOD;

            int ok=1;
            F(i,0,k-1)
            {
                if(a[i]==denn && ok){ok=0;continue;}
                ans*=a[i];
                ans=(ans%MOD+MOD)%MOD;
            }
            cout<<ans;
            return 0;
        }

        if(nump*denp>numn*denn)
        {
            ans=nump;
            ans=(ans%MOD+MOD)%MOD;

            int ok=1;
            F(i,0,k-1)
            {
                if(a[i]==denn && ok){ok=0;continue;}
                ans*=a[i];
                ans=(ans%MOD+MOD)%MOD;
            }
            cout<<ans;
            return 0;
        }
        else
        {
            ans=numn;
            ans=(ans%MOD+MOD)%MOD;

            int ok=1;
            F(i,0,k-1)
            {
                if(a[i]==denp && ok){ok=0;continue;}
                ans*=a[i];
                ans=(ans%MOD+MOD)%MOD;
            }
            cout<<ans;
            return 0;
        }
        

    }
    

}

/* ########## TIPS ##########
#
#   mod_inverse(n,p) is binpow(n,p-2,p)
#
#   while using ceil() function,always type cast the parameters to double like ceil((1.0*a)/b)
#
#   for creating a 2d vector "vec" with n rows & m columns then initialise it with it 0
#   vvi vec(n,vi(m,0))
#   vector<vector<int> > vec( n , vector<int> (m, 0))
#
#   if you want to insert an integer and a pair in a set then,
#   set <pair<int,int>,int> is wrong
#   set< pair < pii,int> > is correct                       // according to my macros
#   set < pair<pair<int,int> , int> > is correct            // pair of a pair & a integer
#
#   if you want just the sum of smallest k elements of a array
#   use nth_element(arr,arr+n) ,
#         use nth_element(arr,arr+n,greater <int> ()) if you want largest k elements
#   then take sum of first k elements ,it works in O(n) ,
#   rather than first sorting then taking sum of first k elements, which works in O(n*logn)
#
#   if you want to check that a given no. 'N' is power of 2
#   use N&(N-1)==0  ,   rather than log2(N)==ceil(log2(N))
#
#   upper_bound is > val in a non-decreasing array
#   lower_bound is >= val in a non-decreasing array
#   upper_bound is < val in a non-increasing array (use greater <int> () in 4th parameter) 
#   lower_bound is <= val in a non-increasing array (use greater <int> () in 4th parameter) 
#   
#   don't use stoi() , it doesn't works for large strings
#   use stoi() to convert string to integer, (2nd & 3rd parameter are optional)
#   1st parameter is string (address of char array)
#   2nd parameter is starting index, 0 most of the times
#   3rd parameter is base of integer, like 2 (for binary no.s), 10 (for decimel no.s) etc.
#
#   for creating a string 'S' of length 'len' , having each character 'c'
#   type    string S(len,c);    while creating the string
#
#   gcd(x-y,x)=gcd(x,y)=gcd(x+y,y)      (for all x and y)
#   gcd(a,b)=gcd(b,a)                   (for all a and b)
#
#   a+b = a|b + a&b
#   a+b = a^b + 2*(a&b)
#   x<<y = x*(2^y)
#   x>>y = x/(2^y)
#
*/

ll binpow(ll x, ll y, ll p) /* (x^y)%p in O(log y) */
{
    ll res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % p;
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

template <typename Arg1>
void __f(const char *name, Arg1 &&arg1)
{
    std::cout << name << " : " << arg1 << endl;
}

template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args)
{
    const char *comma = strchr(names + 1, ',');
    std::cout.write(names, comma - names) << " : " << arg1 << "\n";
    __f(comma + 1, args...);
}