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>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ALL(x) x.begin(), x.end()
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
random_device rnd;
mt19937 mt(rnd());
using ll = long long;
using lld = long double;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using PII = pair<int, int>;
const int IINF = 1 << 30;
const ll INF = 1ll << 60;
const ll MOD = 1000000007;

ll calc(priority_queue<ll, vector<ll>, greater<ll>> &qp, priority_queue<ll> &qm)
{
    ll ans = 1;
    while (!qp.empty())
    {
        ans *= qp.top();
        ans %= MOD;
        qp.pop();
    }
    while (!qm.empty())
    {
        cerr << qm.top() << endl;
        ans *= MOD + qm.top();
        ans %= MOD;
        qm.pop();
    }
    while (ans < 0)
        ans += MOD;
    return ans;
}

int main()
{
    int n, k;
    priority_queue<ll> pp;
    priority_queue<ll, vector<ll>, greater<ll>> pm;
    VL mmm;
    ll temp = 1;
    cin >> n >> k;
    VL v(n);
    rep(i, n)
    {
        ll t;
        cin >> t;
        v[i] = llabs(t);
        if (t >= 0)
        {
            pp.push(t);
            temp *= t;
        }
        else
        {
            pm.push(t);
            mmm.push_back(t);
            temp *= MOD + t;
        }

        temp %= MOD;
    }
    if (n == k)
    {
        cout << temp << endl;
        return 0;
    }
    sort(ALL(mmm), greater<ll>());
    temp = 1;
    if ((int)mmm.size() >= k)
    {
        rep(i, k)
        {
            temp *= MOD + mmm[i];
            temp %= MOD;
        }
    }
    priority_queue<ll, vector<ll>, greater<ll>> qp;
    priority_queue<ll> qm;
    rep(i, k)
    {
        if (pp.empty())
        {
            qm.push(pm.top());
            pm.pop();
        }
        else if (pm.empty())
        {
            qp.push(pp.top());
            pp.pop();
        }
        else if (llabs(pp.top()) > llabs(pm.top()))
        {
            qp.push(pp.top());
            pp.pop();
        }
        else
        {
            qm.push(pm.top());
            pm.pop();
        }
    }
    if (qm.size() % 2 == 0)
    {
        cout << calc(qp, qm) << endl;
        return 0;
    }
    else
    {
        bool canp = false;
        if (pp.size() == 0 && pp.size() == 0)
        {
        }
        else if (pp.size() == 0 && !qp.empty())
        {
            qp.pop();
            qm.push(pm.top());
            canp = true;
        }
        else if (pm.size() == 0 && !qm.empty())
        {
            qm.pop();
            qp.push(pp.top());
            canp = true;
        }
        else
        {
            if (qm.empty())
            {

                //a +出し-入れ
                ll ain = pm.top();
                ll aout = qp.top();
                qp.pop();
                qm.push(ain);
                canp = true;
            }
            else if (qp.empty())
            {
                //b -出し +入れ
                ll bin = pp.top();
                ll bout = qm.top();
                qm.pop();
                qp.push(bin);
                canp = true;
            }
            else
            {
                //a +出し-入れ
                ll ain = pm.top();
                ll aout = qp.top();
                //b -出し +入れ
                ll bin = pp.top();
                ll bout = qm.top();
                if (llabs(ain) * llabs(bout) < llabs(bin) * llabs(aout))
                {
                    qm.pop();
                    qp.push(bin);
                    canp = true;
                }
                else
                {
                    qp.pop();
                    qm.push(ain);
                    canp = true;
                }
            }
        }
        cerr << canp << endl;
        if (canp)
        {
            cout << calc(qp, qm) << endl;
        }
        else
        {
            if ((int)mmm.size() >= k)
            {
                cout << temp << endl;
            }
            else
            {
                assert((k - (n - mmm.size())) % 2 == 1);
                assert((k > (n - mmm.size())));
                sort(ALL(v));
                ll ans = 1;
                rep(i, k)
                {
                    ans *= MOD - v[i];
                    ans %= MOD;
                }

                cout << ans << endl;
            }
        }
    }

    return 0;
}