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>
using namespace std;
const long long MOD=1e9+7;
typedef long long ll;

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
int main(){
    long long n,k;
    cin >> n >> k;
    vector<long long>a(n);
    vector<long long>pos;//0以上の数を受け取る
    vector<long long>neg;//負の値を受け取る
    for(long long i=0;i<n;i++){
        long long x;
        cin >> x;
        a[i]=x;
        if(x>=0)pos.push_back(x);
        else neg.push_back(x);
    }
    sort(pos.begin(),pos.end());
    sort(neg.begin(),neg.end());//絶対値が大きい順
    reverse(pos.begin(),pos.end());//大きい順にする
    long long ans=1;
    //k=nの時
    if(k==n){
        for(long long i=0;i<n;i++){
            ans*=a[i];
            ans%=MOD;
        }
        cout << ans << endl;
        return 0;
    }
    //正の数がないとき
    if(pos.empty()){
        //答えが負になるとき絶対値の小さい順
        if(k%2){
            reverse(neg.begin(),neg.end());
            for(long long i=0;i<k;i++){
                ans*=(MOD+neg[i]);
                ans%=MOD;
            }
            cout << ans << endl;
            return 0;
        }
        //答えが正になるとき大きい順でとる
        else{
            for(long long i=0;i<k;i++){
                ans*=(MOD+neg[i]);
                ans%=MOD;
            }
            cout << ans << endl;
            return 0;
        }
    }
	mint answer=1;
    //kが奇数の時正の数の最大値をあらかじめ取っておく
    if(k%2){
        answer*=pos[0];
        pos.erase(pos.begin());
    }
    long long p=pos.size();
    long long ne=neg.size();
    vector<long long>pos_pair(p/2);
    vector<long long>neg_pair(ne/2);
    for(long long i=0;i<p/2;i++){
        pos_pair[i]=pos[i*2]*pos[i*2+1];
    }
    for(long long i=0;i<ne/2;i++){
        neg_pair[i]=neg[i*2]*neg[i*2+1];
    }
    long long pk=0,nk=0;
    for(long long i=0;i<k/2;i++){
        if(pk==p/2){
            answer*=neg_pair[nk];
            nk++;
        }
        else if(nk==ne/2){
            answer*=pos_pair[pk];
            pk++;
        }
        else{
            if(pos_pair[pk]>neg_pair[nk]){
                answer*=pos_pair[pk];
                pk++;
            }
            else{
                answer*=neg_pair[nk];
                nk++;
            }
        }
    }
    cout << answer << endl;
}