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)

    // wrong answer
    #include <bits/stdc++.h>
    #define ll long long

    using namespace std;
    const int N = 2e5 + 10;
    const ll inf = 1e9 + 7;
    ll n,k,a[N],sport[N];
    vector <ll> small,big;
    ll power(ll x,ll y,ll mod){
        if (y == 1) return x % mod;
        if (y == 0) return 1;
        ll temp = power(x,y/2,mod) % mod;
        temp = (temp * temp) % mod;
        if (y & 1) return (temp * (x % mod)) % mod; return temp;
    }
    int main(){
        cin >> n >> k;
        for (int i = 1; i <= n; i++) cin >> a[i];
        /*sort(a + 1,a + 1 + n);
        a[0] = 1;
        //for (int i = 1; i <= n; i++) cout << a[i] << " " ; cout << "\n";
        for (int i = 1; i <= n; i++)
            if (a[i] < 0) sport[i] = sport[i - 1] + 1; else sport[i] = sport[i - 1];
        for (int i = 1; i <= n; i++) a[i] = (a[i - 1] * abs(a[i])) % inf;
        //for (int i = 1; i <= n; i++) cout << a[i] << " " ; cout << "\n";
        //for (int i = 1; i <= n; i++) cout << sport[i] << " "; cout << "\n";
        ll ans = -1e18;
        for (int x = 0; x <= k; x++){
            ll mul = (a[x] * (power(a[n - (k - x)],inf - 2,inf) * a[n]) % inf) % inf;
            ll gr = sport[x] + sport[n] - sport[n - (k - x)];
            if (gr % 2 == 1) mul = - mul;
            if (ans < mul)
                ans = max(ans,(mul + inf) % inf);
            //cout << x << " " << n - (k - x) << " " << mul << " " << gr << "\n";
        }
        cout << ans;*/
        for (int i = 1; i <= n; i++){
            if (a[i] <= 0) small.push_back(a[i]);
            else big.push_back(a[i]);
        }
        sort(small.begin(),small.end());
        sort(big.begin(),big.end(),greater<ll>());
        int i = 0 , j = 0;
        ll ans = 1;
        while (k > 0){
            if ((int)small.size() - i - 1 > 0 && (int)big.size() - j - 1 > 0 && k % 2 == 0 && k > 1){
                if (big[j] * big[j + 1] > small[i] * small[i + 1]){
                    ll temp = (big[j] * big[j + 1]) % inf;
                    ans = (ans * temp) % inf;
                    j += 2; k -= 2;
                }
                else{
                    ll temp = (((small[i] + inf) % inf) * ((small[i + 1] + inf) % inf)) % inf;
                    ans = (ans * temp) % inf;
                    i += 2; k -= 2;
                }
            }
            else
            if ((int)big.size() - j > 0){
                ans = (ans * big[j]) % inf;
                //cout << " + " << big[j] << "\n";
                j++; k--; 
            }
            else
            if ((int)small.size() - i > 0){
                ans = (ans * (small[(int)small.size() - 1] + inf) % inf) % inf;
                //cout << " - " << small[small.size() - 1] << "\n";
                small.pop_back(); k--;
            }
        }
        cout << ans;
        return 0;
    }