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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>   
using namespace std;
using namespace __gnu_pbds;
typedef long long ll; 
#define int long long
typedef unsigned long long lu;
typedef vector<ll> v;
typedef vector<vector<ll> > vv;
typedef vector<string>  vs;
typedef vector<pair<ll,ll>> vpr;
typedef vector<bool>vb;
typedef vector<double>vd;
typedef long double ld;
#define f(i,n) for(ll i = 0; i < n; i++)
#define ff(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define endl "\n"
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define amax(x,y) if(y>x) x=y;
#define amin(x,y) if(y<x)x=y;
#define bg(x) x.begin()
#define sz(x) (ll)x.size()
#define in(x,n) for(ll i=0;i<n;i++)cin>>x[i]
#define out(x,n) for(ll i=0;i<n;i++)cout<<x[i]<<" " 
#define mxt(a) *(max_element(a.begin(),a.end()))
#define mnt(a) *(min_element(a.begin(),a.end())
#define tc ll t;cin>>t;while(t--)
typedef pair<ll,ll> pi;
#define yes cout<<"YES\n";
#define no cout<<"NO\n";
#define yesno(f) if(f) yes else no
const v dx = {1, -1, 0, 0};
const v dy = {0, 0, 1, -1};
const ld PI = 2 * acos(0.0);
ll cel(ll x1,ll y1){if((x1%y1)==0)return x1/y1;else return x1/y1+1;}
ll power(ll a,ll b,ll m)
{
    if(b==0)
        return 1;
    ll d=power(a,b/2,m);
    d=(d*d)%m;
    if(b&1)
        d=(d*a)%m;
    return d;
}
const ll mod=1e9+7;
int MOD(int a)
{
    if(a<0)
        a+=mod;
    if(a>=mod)
        a%=mod;
    return a;
}
// set_name.find_by_order(k) It returns to an iterator to the kth element (counting from zero) in the set in O(logn) time
// set_name.order_of_key(k) It returns to the number of items that are strictly smaller than our item k in O(logn) time.
/*string operations :
str.substr (x,y) : returns a substring str[x],str[x+1],...str[x+y-1]
str.substr (x) : returns a substring str[x],... end of string
str.find(qtr) : returns the first occurenece of qtr in str */
int32_t main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    int n,k;cin>>n>>k;
    v a(n);in(a,n);
    v neg,pos;
    int zero=0;
    f(i,n)
    {
        if(a[i] < 0)
        {
            neg.pb(abs(a[i]));
        }
        else
        {
            pos.pb(a[i]);
            if(a[i]==0)
                zero++;
        }
    }
    if(k==n)
    {
        int ans=1;
        f(i,n)
        {
            ans*=a[i];
            ans=MOD(ans);
        }
        cout<<ans<<endl;
        return 0;
    }
    if(sz(pos)==0 && (k&1))
    {
        sort(all(neg));
        int ans=1;
        f(i,k)
        {
            ans*=(-neg[i]);
            ans=MOD(ans);
        }
        cout<<ans<<endl;
        return 0;
    }
    sort(rall(pos));
    sort(rall(neg));
    v Neg,Pos;
    for(int i=0;i<sz(neg)-1;i+=2)
    {
        Neg.pb(neg[i]*neg[i+1]);
    }
    int ans=1;
    if(k&1)
    {
        ans=pos[0];
        k--;
        for(int i=1;i<sz(pos)-1;i+=2)
        {
            Pos.pb(pos[i]*pos[i+1]);
        }
    }
    else
    {
        for(int i=0;i<sz(pos)-1;i+=2)
        {
            Pos.pb(pos[i]*pos[i+1]);
        }
    }
    k/=2;
    int p1=0,p2=0;
    int i;
    for(i=0;i<k;i++)
    {
        if(p1==sz(Pos) && p2==sz(Neg))
            break;
        else if(p1==sz(Pos))
        {
            int val=MOD(Neg[p2]);
            ans*=val;ans=MOD(ans);
            p2++;
        }
        else if(p2==sz(Neg))
        {
            int val=MOD(Pos[p1]);
            ans*=val;
            ans=MOD(ans);
            p1++;
        }
        else 
        {
            if(Pos[p1] < Neg[p2])
            {
                int val=MOD(Neg[p2]);
                ans*=val;
                ans=MOD(ans);
                p2++;
            }
            else
            {
                int val=MOD(Pos[p1]);
                ans*=val;
                ans=MOD(ans);
                p1++;
            }
        }
    }
    ans=MOD(ans);
    cout<<ans<<endl;
    return 0;
}