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: Runtime Error)

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
inline ll lcm(ll a, ll b) { return a / gcd(a, b)*b; }
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T>& V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> >& Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T>& mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }
template<typename T>vector<T> make_v(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));}
template<typename T,typename V> typename enable_if<is_class<T>::value==0>::type fill_v(T &t,const V &v){t=v;}
template<typename T,typename V> typename enable_if<is_class<T>::value!=0>::type fill_v(T &t,const V &v){for(auto &e:t) fill_v(e,v);}
/*
 <url:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

const ll MOD = 1e9+7;
// a^b % MOD;
ll powmod(ll a,ll b) {ll res=1;a%=MOD; for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// 逆元 1/a % MOD
ll inversemod(ll a) { return powmod(a,MOD - 2);}

template<class Type>
Type solve(Type res = Type()){
    ll N,K; cin >> N >> K;
    vector<ll> A(N); for(auto& in:A) cin >>in;
    res = 1;
    if(N == K){
        // 全部取る
        for(int i = 0; i < N;i++){
            res *= A[i];
            res = (res%MOD + MOD)%MOD;
        }
        return res;
    }
    if(A.back() < 0){
        // 全部負となる場合
        if(K%2 == 0){
            // 偶数なら絶対値が大きい順 := 正
            sort(A.begin(),A.end());
            for(int i = 0; i < K;i++){
                res *= A[i];
                res = (res%MOD + MOD)%MOD;
            }
        }else{
            // 奇数なら絶対値が小さい順 := 負
            sort(A.rbegin(),A.rend());
            for(int i = 0; i < K;i++){
                res *= A[i];
                res = (res%MOD + MOD)%MOD;
                
            }
        }
        return res;
    }
    
    // 正が少なくとも1個あるので、必ず正にできる
    vector<ll> pos,neg;
    for(int i = 0; i < N;i++){
        if(A[i] >= 0) pos.emplace_back(A[i]);
        else neg.emplace_back(-A[i]);
    }
    
    auto mul_val = [&](ll& T,ll V){
        T *= V;
        T = (T%MOD + MOD)%MOD;
    };
    
    pos.emplace_back(-INF); pos.emplace_back(-INF);
    neg.emplace_back(-INF); neg.emplace_back(-INF);
    sort(pos.begin(),pos.end());
    sort(neg.begin(),neg.end());
    
    for(int k = 0; k < K;){
        ll x1 = pos.back(); pos.pop_back();
        ll x2 = pos.back(); pos.pop_back();
        
        ll y1 = neg.back(); neg.pop_back();
        ll y2 = neg.back(); neg.pop_back();
        
        if(k == K-1){
            mul_val(res,x1);
            k++;
        }else{
            if(x2 == -INF){
                mul_val(res,-y1);
                mul_val(res,-y2);
                k+=2;
            }else if(y2 == -INF){
                mul_val(res,x1);
                k++;
            }else{
                if(x1*x2 >= y1*y2){
                    mul_val(res,x1);
                    k++;
                }else{
                    mul_val(res,-y1);
                    mul_val(res,-y2);
                    k+=2;
                }
            }
        }
    }
    
    
    
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    // solve(0);
    cout << fixed << setprecision(15) << solve<ll>() << endl;
    return 0;
}