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++ (Clang 10.0.0) (judge verdict: Wrong Answer)


// --------------------<optimizations>--------------------
#pragma GCC optimize("O3")

//(UNCOMMENT WHEN HAVING LOTS OF RECURSIONS)
#pragma comment(linker, "/stack:200000000")
//(UNCOMMENT WHEN TRYING TO BRUTEFORCE WITH A LOT OF LOOPS)
#pragma GCC optimize("unroll-loops")
// -------------------</optimizations>--------------------
#include <iostream>
#include <bitset>
#include <algorithm>        // sort()
#include <vector>           // for vectors
#include <map>              // for ordered map
#include <unordered_map>
#include <list>             // for list
#include <iomanip>          // for setprecision()
#include <cmath>            // for pow(), sqrt(), log(), log10(), ....
#include <cstring>          // for memset()
#include <unordered_set>    // for unordered_set
#include <climits>          // for INT_MAX & INT_MIN
#include <set>              // for ordered set

#define C ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
typedef long long ll;
#define INF INT_MAX
#define NINF INT_MIN
#define MOD 1000000007
const double pi=acos(-1); // value of pi


bool isVowel(char ch) {
    if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u') {
        return 1;
    }
    return 0;
}

bool compare(pair<ll, ll> a, pair<ll, ll> b) {
    return a.second<b.second;
}


bool isPrime(ll n) {
    for(ll i=3; i*i<=n; i+=2) {
        if(n%i==0) {
            return false;
        }
    }
    return true;
}

ll binary_exp(ll x, ll n, ll mod)
{
    if (n == 0) {
        return 1;
    } else if (n == 1) {
        return x % mod;
    } else {
        ll temp = binary_exp(x, n / 2, mod);
        temp = (temp * temp) % mod;

        if (n % 2 == 0) {
            return temp;
        } else {
            return ((x % mod) * temp) % mod;
        }
    }
}

map<ll, list<ll>> m;
// map<ll, ll> m;

int main() {
    C
//     freopen("input.txt","r",stdin);
// 	freopen("output.txt","w",stdout);
    int t=1;
    // cin >> t;
    while(t--) {
        ll n, k, x, y, ans=1, mul=1;
        cin >> n >> k;
        vector<ll> a, b;
        for(ll i=0; i<n; i++) {
            cin >> x;
            mul*=x;
            mul%=MOD;
            if(x>=0) {
                a.push_back(x);
            }
            else {
                b.push_back(x);
            }
        }
        sort(a.begin(), a.end(), greater<ll>());
        sort(b.begin(), b.end(), greater<ll>());
        x=0, y=0;
        if(k==n) {
            if(mul<0) {
                mul=MOD+mul;
            }
            cout << mul << endl;
            return 0;
        }
        if(k%2==0) {
            while(1) {
                if(x+1<a.size() && y+1<b.size()) {
                    if(a[x]*a[x+1]>b[y]*b[y+1]) {
                        ans*=a[x]*a[x+1];
                        ans%=MOD;
                        x+=2;
                    }
                    else {
                        ans*=b[y]*b[y+1];
                        ans%=MOD;
                        y+=2;
                    }
                }
                else {
                    if(x+1>=a.size()) {
                        ans*=b[y]*b[y+1];
                        y+=2;
                        ans%=MOD;
                    }
                    else if(y+1>=b.size()) {
                        ans*=a[x]*a[x+1];
                        ans%=MOD;
                        x+=2;
                    }
                }
                if(x+y==k) {
                    cout << ans << endl;
                    return 0;
                }

            }
        }
        else {
            while(1) {
                if(x+y+1==k) {
                    if(x<a.size()) {
                        ans*=a[x];
                    }
                    else {
                        ans*=b[y];
                    }
                    ans%=MOD;
                    if(ans<0) {
                        ans=MOD+ans;
                    }
                    cout << ans << endl;
                    return 0;
                }
                if(x+1<a.size() && y+1<b.size()) {
                    if(a[x]*a[x+1]>b[y]*b[y+1]) {
                        ans*=a[x]*a[x+1];
                        x+=2;
                    }
                    else {
                        ans*=b[y]*b[y+1];
                        y+=2;
                    }
                }
                else {
                    if(x+1>=a.size()) {
                        ans*=b[y]*b[y+1];
                        y+=2;
                    }
                    else if(y+1>=b.size()) {
                        ans*=a[x]*a[x+1];
                        x+=2;
                    }
                }
                ans%=MOD;
            }
        }
    }
}