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 rep(i,n) for(int i=0; i<(n); i++)
#define reps(i,s,n) for(int i=(s); i<(n); i++)
#define all(v) v.begin(),v.end()
#define outve(v) for(auto i : v) cout << i << " ";cout << endl
#define outmat(v) for(auto i : v){for(auto j : i) cout << j << " ";cout << endl;}
#define in(n,v) for(int i=0; i<(n); i++){cin >> v[i];}
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define si(v) int(v.size())
#define len(v) int(v.length())
#define lob(v,n) lower_bound(all(v),n)
#define lobi(v,n) lower_bound(all(v),n) - v.begin()
#define upb(v,n) upper_bound(all(v),n)
#define upbi(v,n) upper_bound(all(v),n) - v.begin()
#define mod 1000000007
#define infi 1900000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vs = vector<string>;
using vc = vector<char>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vvp = vector<vector<pair<int,int>>>;
template<typename T> using ve = vector<T>;
template<typename T> using pq2 = priority_queue<T>;
template<typename T> using pq1 = priority_queue<T,vector<T>,greater<T>>;
template<typename T> bool chmax(T &a, T b) {if(a < b) {a = b;return 1;}return 0;}
template<typename T> bool chmin(T &a, T b) {if(a > b) {a = b;return 1;}return 0;}




int main(){
    
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    
    int K,N;
    cin >> N >> K;
    vl A(N);
    in(N,A);
    
    ve<pair<ll,int>> B;
    rep(i,N) {
        if(A[i] > 0) B.pb({A[i],1});
        else if(A[i] < 0) B.pb({-A[i],-1});
        else B.pb({0,0});
    }
    
    sort(all(B));
    reverse(all(B));
    
    pq1<ll> B_p,B_m;
    
    rep(i,K) {
        if(B[i].se >= 0) B_p.push(B[i].fi);
        else B_m.push(B[i].fi);
    }
    
    if(si(B_m) % 2 == 0){
        ll ans = 1;
        while (!B_m.empty()) {
            ans *= B_m.top()%mod;
            ans %= mod;
            B_m.pop();
        }
        while (!B_p.empty()) {
            ans *= B_p.top()%mod;
            ans %= mod;
            B_p.pop();
        }
        cout << ans%mod << endl;
        return 0;
    }
    
    int aaa = -1;
    int bbb = -1;
    
    reps(i,K,N){
        
        if(B[i].se == 1 && aaa == -1){
            if(B_m.size() >= 1) aaa = B[i].fi;
        }else if(B[i].se == -1 && bbb == -1){
            if(B_p.size() >= 1) bbb = B[i].fi;
        }
    }
    //cout << aaa csp bbb << endl;
    //cout << B_p.size() csp B_m.size() << endl;
    int ff = 1;
    if(aaa >= 0){
        
        if(bbb >= 0){
            if(aaa-B_m.top() < bbb-B_p.top()){
                B_m.push(bbb);
                B_p.pop();
                
            }else{
                B_p.push(aaa);
                B_m.pop();
                
            }
        }else{
            B_p.push(aaa);
            B_m.pop();
        }
        
    }else{
        if(bbb >= 0){
            B_m.push(bbb);
            B_p.pop();
        }else{
            ff = 0;
        }
    }
    
    if(ff){
        ll ans = 1;
        while (!B_m.empty()) {
            ans *= B_m.top()%mod;
            ans %= mod;
            B_m.pop();
        }
        while (!B_p.empty()) {
            ans *= B_p.top()%mod;
            ans %= mod;
            B_p.pop();
        }
        cout << ans%mod << endl;
        return 0;
    }
    
    reverse(all(B));
    ll ans = 1;
    rep(i,K){
        ans *= B[i].fi%mod;
        ans %= mod;
    }
    cout << (mod-ans)%mod << endl;
    
    

    return 0;
}