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 ll;
#define pa pair<int, int>
#define pal pair<long long, long long>
#define pali pair<long long, int>
#define pad pair<double, double>
#define pb push_back
#define mp make_pair
#define COUT(v)                              \
    for (int64_t i = 0; i < (v).size(); ++i) \
    {                                        \
        cout << v.at(i) << endl;             \
    }
#define REP(i, n) for (int64_t i = 0; i < n; ++i)
#define FOR(i, r, n) for (int64_t i = (r); i < n; ++i)
#define VIN(v)                               \
    for (int64_t i = 0; i < (v).size(); ++i) \
    {                                        \
        cin >> (v).at(i);                    \
    }
 
typedef vector<bool> bvec;
typedef vector<int> ivec;
typedef vector<long long> lvec;
typedef vector<double> dvec;
typedef vector<pa> pavec;
typedef vector<vector<pa> > pamat;
typedef vector<pali> palivec;
typedef vector<pal> palvec;
 
typedef vector<vector<bool> > bmat;
typedef vector<vector<int> > imat;
typedef vector<vector<long long> > lmat;
 
typedef vector<string> svec;
typedef vector<vector<string> > smat;
const ll infll = (1LL << 60) - 1;
const int inf = (1 << 30) - 1;
const ll MOD = 1000000007;
 
ll gcd(ll x, ll y)
{
    ll r = x % y;
    if (r == 0)
        return y;
    else
        return gcd(y, r);
}

ll lcm(ll x, ll y)
{
    return x * y / gcd(x, y);
}
 
lvec mfactor(ll n)
{
    bvec ip(n, true);
    lvec mf(n, -1);
    ip[0] = false;
    ip[1] = false;
    mf[0] = 0;
    mf[1] = 1;
    REP(i, n)
    {
        if (ip[i])
        {
            mf[i] = i;
            for (ll j = i * i; j < n; j += i)
            {
                ip[j] = false;
                if (mf[j] == -1)
                    mf[j] = i;
            }
        }
    }
    return mf;
}

palivec get_prime(ll n, const lvec &mf)
{
    palivec plist;
    while (n != 1)
    {
        int cnt = 0;
        ll m = mf[n];
        while (mf[n] == m)
        {
            cnt++;
            n /= m;
        }
        plist.pb(pali(m, cnt));
    }
    return plist;
}
 
void COMinit(int m, lvec &fac, lvec &finv)
{
    lvec inv(m);
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < m; i++)
    {
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
 
    }
}
 
ll pow_mod(ll a, ll n)
{
    ll x = 1;
    while (n > 0)
    {
        if (n & 1)
        {
            x = x * a % MOD;
        }
        a = a * a % MOD;
        n >>= 1;
    }
    return x;
}
 
 
ll COM(int n, int k, const lvec &fac, const lvec &finv)
{
    if (n < k)
        return 0;
    if (n < 0 || k < 0)
        return 0;
    return (fac[n] * (finv[k] * finv[n - k] % MOD)) % MOD;
}

ll modinv(ll a, ll m)
{
    ll b = m, u = 1, v = 0;
    while (b)
    {
        ll t = a / b;
        a -= t * b;
        swap(a, b);
        u -= t * v;
        swap(u, v);
    }
    u %= m;
    if (u < 0)
        u += m;
    return u;
}

typedef map<ll, double> Prob;

void calc(Prob &firsts, Prob &seconds, Prob &thirds){
    vector< pair<double, lvec> > pr;
    for (auto fv: firsts){        
        for (auto sv: seconds){
            for (auto tv: thirds){
                if(fv.first == sv.first || sv.first == tv.first || fv.first == tv.first) continue;
                lvec s(3);
                s[0] = (fv.first);
                s[1] = (sv.first);
                s[2] = (tv.first);
                double p = fv.second*sv.second*tv.second;
                pr.pb( pair<double, lvec >(p, s));
            }
        }
    }

    sort(pr.begin(), pr.end(), greater<pair<double, lvec> >());
    for (auto v: pr){
        cout << "prob: " <<  v.first << " : odds : " << 1/v.first << " : ";
        for (auto vv: v.second){
            cout << vv << ' ';
        }
        cout << endl;
    }
}

ll cnt(ll x){
    ll s = 0;
    ll k = 26;
    for (int i=0; i<x; i++){
        s = s + k;
        k *= 26;
    }
    return s;
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, k;
    cin >> n >> k;
    lvec ab(n);
    lvec p;
    lvec m;
    for (ll i=0; i<n; i++){
        ll a;
        cin >> a;
        if (a>=0) p.pb(a);
        else m.pb(a);
    }
    sort(p.rbegin(), p.rend());
    sort(m.begin(), m.end());
    ll nm = m.size() , np = p.size();
    if(n==k){
        ll ans = 1;
        for (auto v: p){
            ans*=v;
            ans%=MOD;
        }
        for (auto v: m){
            ans*=abs(v);
            ans%=MOD;
        }
        cout << ans << endl;
        return 0;
    }

    if(np == 0 && k%2){
        ll ans = 1;
        for (ll i=nm-1; i>=nm-k; i--){
            ans*=(MOD + m[i]);
            ans%=MOD;
        }
        cout << ans << endl;
        return 0;
    }

    ll ip=0, im=0;
    ll ans=1;
    if(k%2){
        ans = p[0];
        --k;
        ++ip;
    }

    while(k>0){
        if((im+1)>(nm-1)){
            for (ll i=ip; i<ip+k; i++){
                ans *= p[i];
                ans%=MOD;
            }
            break;
        }
        else if ((ip+1)>(np-1)){            
            for (ll i=im; i<im+k; i++){
                ans *= m[i];
                ans%=MOD;
            }
            break;
        }
        else{            
            ll pp = p[ip]*p[ip+1];
            ll pm = m[im]*m[im+1];
            //cout << pp << ' ' << pm << endl;
            if(pp > pm){
                ans *= pp%MOD;
                ans %= MOD;
                ip+=2;
            }
            else{
                ans *= pm%MOD;
                ans %= MOD;
                im+=2;
            }
        }
        k-=2;
    }
    cout << ans << endl;




}