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(ll i=0; i<(n); i++)
#define chmin(a,b) a = min(a,b)
#define chmax(a,b) a = max(a,b)
using namespace std;
using ll = long long;
using vi = vector<int>;
using vv = vector<vi>;
using P  = pair<int,int>;//(cost,to)
using vP = vector<P>;
using vvP= vector<vP>;

const int INF = 1001001001;
const int MOD = 1000000007;

int main(){
    ll n,k;cin>>n>>k;
    
    vector<pair<ll, ll>> a(n);
    
    ll scount=0;
    rep(i,n){
        cin>>a[i].first;
        if(a[i].first<0){
            a[i].first = -a[i].first;
            a[i].second = 1;
            scount++;
        }
    }
    
    if(scount==n && k%2==1){//マイナス確定
        sort(a.begin(), a.end());
        ll ans = 1;
        rep(i,k){
            ans *= a[i].first;
            ans %= MOD;
        }
        cout << MOD-ans << endl;
        return 0;
    }
    
    sort(a.rbegin(), a.rend());
    scount = 0;
    ll last_m=INF, last_p=INF;
    ll next_m=INF, next_p=INF;
    
    
    rep(i,k){
        scount += a[i].second;
        if(a[i].second)last_m = i;
        else           last_p = i;
    }
    
    if(scount%2==0 || a[k-1].first==0 || last_m==INF){
        ll ans = 1;
        rep(i,k){
            ans *= a[i].first;
            ans %= MOD;
        }
        cout << ans << endl;
        return 0;
    }
    
    //上からk個の中に負が存在し奇数個含まれるとき
    for(ll i=n-1; i>=k; i--){
        if(a[i].second){
            next_m = i;
        }else
            next_p = i;
    }
    
    if(next_m==INF){
        if(next_p==INF){
            ll ans = 1;
            rep(i,k){
                ans *= a[i].first;
                ans %= MOD;
            }
            if(scount%2==0)
                cout << ans << endl;
            else{
                if(ans==0)
                    cout << ans << endl;
                else cout << MOD-ans << endl;
            }
        }
        else{
            swap(a[last_m], a[next_p]);
            ll ans = 1;
            rep(i,k){
                ans *= a[i].first;
                ans %= MOD;
            }
            cout << ans << endl;
        }
    }else{
        if(next_p==INF){
            swap(a[last_p], a[next_m]);//last_p==INFは最初にはらった
            ll ans = 1;
            rep(i,k){
                ans *= a[i].first;
                ans %= MOD;
            }
            cout << ans << endl;
        }
        else{
            ll prd_m = a[next_m].first * a[last_m].first;
            ll prd_p;
            if(last_p==INF)
                prd_p = 0;
            else
                prd_p = a[next_p].first * a[last_p].first;
            if(prd_m>prd_p){
                if(last_p==INF)
                    swap(a[k-1], a[next_m]);
                else swap(a[last_p], a[next_m]);
            }else{
                swap(a[last_m], a[next_p]);
            }
            ll ans = 1;
            rep(i,k){
                ans *= a[i].first;
                ans %= MOD;
            }
            cout << ans << endl;
        }
    }
    return 0;
}