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 <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define idfc ios_base::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
//#define ll long long
#define pb emplace_back
#define mp make_pair
#define nt _ll128
#define ld long double
long double PI =3.14159265358979323846;
using ll = long long;



const ll modo=1e9+7;
const ll inf=LONG_LONG_MAX;
const ll ms=(2e5)+5;

ll ar[ms];
ll br[ms]={0};
ll cr[ms]={0};

bool comp(ll x,ll y)
{
    return abs(x)<abs(y);
}


ll help(ll ar[ms],int k,int n)
{
    ll ans=1;
    ll e;
    while(k--)
    {
        e=(ar[n]+modo)%modo;
        ans=(e*ans)%modo;
        n--;
    }
    return ans;
}

int main()
{
    idfc;
    int n,k;
    cin>>n>>k;
    int i,j;
    for(i=1;i<=n;i++)cin>>ar[i];

    sort(ar+1,ar+n+1,comp);


    ll ans=help(ar,k,n);

    j=n;
    i=k;
    int tot=0;
    int z=0;
    int x=0;
    int y=0;
    while(i--)
    {
        if(ar[j]==0)z=1;
        else if(ar[j]<0)tot++;
        j--;
    }
    tot%=2;
    if(z||(!tot)){cout<<ans;return 0;}

    int a=-1;
    int b=-1;
    i=j;
    //cout<<"Sdsd"<<endl;

    while(i>=1)
    {
        if(ar[i]>=0)
        {
            a=i;break;
        }
        i--;
    }
    i=j+1;
    while(i<=n)
    {
        if(0>ar[i])
        {
            b=i;
            break;
        }
        i++;
    }
    ll x1;
    ll x2;
    ll y1;
    ll y2;
    if(a!=-1&&b!=-1)
    {
        x=1;
        x1=ar[a];
        x2=ar[b];
        for(i=1;i<=n;i++)br[i]=ar[i];
        swap(br[a],br[b]);
    }

    a=-1;
    b=-1;
    i=j;

    while(i>=1)
    {
        if(0>=ar[i])
        {
            a=i;
            break;
        }
        i--;
    }

    i=j+1;

    while(i<=n)
    {
        if(ar[i]>0)
        {
            b=i;
            break;
        }
        i++;
    }

    if(a!=-1&&b!=-1)
    {
        y=1;
        y1=ar[a];
        y2=ar[b];
        for(i=1;i<=n;i++)cr[i]=ar[i];
        swap(cr[a],cr[b]);
    }
    ll d;
    if(x&&y)
    {
        d=(x1*y2)-(y1*x2);
        if(d>=0)
        {
            ans=help(br,k,n);
        }
        else ans=help(cr,k,n);
    }
    else if(x)
    {
        ans=help(br,k,n);
    }
    else if(y)
    {
        ans=help(cr,k,n);
    }
    else{
        sort(ar+1,ar+n+1);
        ans=help(ar,k,n);
    }
    cout<<ans;



    return 0;
}