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 <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <climits>

constexpr int SINT_MAX = std::numeric_limits<int>::max();
constexpr int SINT_MIN = std::numeric_limits<int>::min();

using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;

template<class T>
T choose(bool b, T t, T f)
{
    if (b) return t;
    else return f;    
}

const char *YesNo(bool b)
{
    return choose(b, "Yes", "No");
}

const char *YESNO(bool b)
{
    return choose(b, "YES", "NO");
}

template<class NumT>
NumT diffabs(NumT l, NumT r)
{
    if (l < r) return r-l;
    else return l-r;    
}

struct myinout_t {} io;

template<class T>
myinout_t &operator >>(myinout_t &my, T &i)
{
    cin >> i;
    return my;
}

myinout_t &operator >>(myinout_t &my, int &i)
{
    int r = scanf("%d", &i);
    if (r != 1) exit(EXIT_FAILURE);
    return my;
}

myinout_t &operator >>(myinout_t &my, ll &i)
{
    int r = scanf("%lld", &i);
    if (r != 1) exit(EXIT_FAILURE);
    return my;
}

template<class T>
myinout_t &operator <<(myinout_t &my, const T &i)
{
    cout << i;
    return my;
}

myinout_t &operator <<(myinout_t &my, int i)
{
    printf("%d", i);
    return my;
}

myinout_t &operator <<(myinout_t &my, ll i)
{
    printf("%lld", i);
    return my;
}

myinout_t &operator <<(myinout_t &my, double i)
{
    printf("%.20f", i);
    return my;
}

constexpr char BR = '\n';


///////////////////////////////////////////////////
constexpr int MOD = 1e9+7;


int main()
{
    int N;
    size_t K;
    io >> N >> K;
    vi pA, mA;
    for (int i = 0; i < N; i++)
    {
        int a;
        io >> a;
        if (a >= 0)
            pA.push_back(a);
        else
            mA.push_back(a);
    }

    sort(begin(pA), end(pA), greater<int>());
    sort(begin(mA), end(mA), less<int>());

    ll ans = 1;

    if (K <= pA.size())
    {
        size_t i = 0;
        for (; i + 1 < min(mA.size(), pA.size()); i += 2)
        {
            ll mmul = (ll)mA[i] * mA[i+1];
            ll pmul = (ll)pA[K-i] * pA[K-i-1];
            if (pmul > mmul) break;
        }
        for (int j = 0; j < K-i; j++)
        {
            ans = (ans * pA[j]) % MOD;
        }
        for (int j = 0; j < i; j++)
        {
            ans = (ans * -mA[j]) % MOD;
        }
    }
    else
    {
        size_t mK = K - pA.size();
        if (mK % 2 == 0)
        {
            size_t i = 0;
            for (; mK+i+1 < mA.size() && i+2 <= pA.size(); i += 2)
            {
                ll mmul = (ll)mA[mK+i] * mA[mK+i+1];
                ll pmul = (ll)pA[pA.size()-i-1] * pA[pA.size()-i-2];
                if (pmul > mmul) break;
            }
//            printf("mk = %d, i = %d\n", mK, i);
            for (int j = 0; j < pA.size()- i ; j++)
            {
                ans = (ans * pA[j]) % MOD;
            }
            for (int j = 0; j < mK + i; j++)
            {
                ans = (ans * -mA[j]) % MOD;
            }
        }
        else
        {
            sort(begin(pA), end(pA), less<int>());
            sort(begin(mA), end(mA), greater<int>());

            size_t i = 0;
            for (; mK+i+1 < mA.size() && i+2 <= pA.size(); i += 2)
            {
                ll mmul = (ll)mA[mK+i] * mA[mK+i+1];
                ll pmul = (ll)pA[pA.size()-i-1] * pA[pA.size()-i-2];
                if (pmul < mmul) break;
            }
            printf("mk = %d, i = %d\n", mK, i);
            for (int j = 0; j < pA.size()- i ; j++)
            {
                ans = (ans * pA[j]) % MOD;
            }
            for (int j = 0; j < mK + i; j++)
            {
                ans = (ans * -mA[j]) % MOD;
            }
            ans = MOD - ans;
        }
        
    }

    io << ans << BR;;
}