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;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

static const long long mod = 1000000007;
struct mint {
  long long x;
  mint(ll x = 0):x(x%mod) {}
  mint& operator+=(const mint a) {
    (x += a.x) %= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    (x += mod-a.x) %= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint ret(*this);
    return ret += a;
  }
  mint operator-(const mint a) const {
    mint ret(*this);
    return ret -= a;
  }
  mint operator*(const mint a) const {
    mint ret(*this);
    return ret *= a;
  }
  mint pow(ll t) const {
    if(t==0) return mint(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 {
    mint ret(*this);
    return ret /= a;
  }
};
ostream &operator<<(ostream& os, const mint& x) {
  os << x.x;
  return os;
}
class modutils {
  vector<mint> fact, invfact;
public:
  modutils(int n = 2000005):fact(n+1),invfact(n+1) {
    fact[0] = 1;
    for(int i=1; i<=n; i++) fact[i] = fact[i-1] * i;
    invfact[n] = fact[n].inv();
    for(int i=n; i>=1; i--) invfact[i-1] = invfact[i] * i;
  }
  mint pow(mint x, ll n) {
    return x.pow(n);
  }
  mint comb(ll n, ll k) {
    if(n<0 || k<0 || n<k) return 0;
    return fact[n] * invfact[k] * invfact[n-k];
  }
  mint perm(ll n, ll k) {
    if(n<0 || k<0 || n<k) return 0;
    return fact[n] * invfact[n-k];
  }
  mint fac(ll n) {
    return fact[n];
  }
};
modutils mutil;


struct ST {
  ll eval;
  vector<ll> v;
};
bool operator<(const ST& a, const ST& b){
  if(a.eval == b.eval) return a.v.size() > b.v.size();
  return a.eval > b.eval;
}


int main(){
  int N, K;
  cin >> N >> K;
  vector<ll> v_n,v_z,v_p;
  rep(i,N){
    ll a;
    cin >> a;

    if(a < 0) v_n.push_back(a);
    if(a == 0) v_z.push_back(a);
    if(a > 0) v_p.push_back(a);
  }

  if(v_z.size() > 0){
    if(v_n.size() + v_p.size() < K){
      cout << 0 << endl;
      return 0;
    }
  }
  sort(ALLOF(v_p));
  reverse(ALLOF(v_p));
  sort(ALLOF(v_n));
  
  if(K==1){
    if(v_p.size() > 0){
      cout << v_p[0] << endl;
    }
    else if(v_z.size() > 0){
      cout << 0 << endl;
    }
    else {
      cout << v_n[v_n.size()-1] << endl;
    }
    return 0;
  }

  vector<ST> v;  
  rep(i,v_p.size()){
    ST tmp;
    tmp.eval = v_p[i] * 2;
    tmp.v.push_back(v_p[i]);
    v.push_back(tmp);
  }
  for(int i=0; i<v_n.size(); i+=2){
    if(i+1 >= v_n.size()) continue;
    ST tmp;
    tmp.eval = v_n[i] * v_n[i+1];
    tmp.v.push_back(v_n[i]);
    tmp.v.push_back(v_n[i+1]);
    v.push_back(tmp);
  }
  sort(ALLOF(v));
  mint ret(1);
  int k = 0;
  rep(i,v.size()){
    if(k >= K) continue;
    if(k+v[i].v.size() <= K){
      rep(j,v[i].v.size()){
        ret *= v[i].v[j];
      }
      k += v[i].v.size();
    }
  }
  

  if(k != K){
    if(v_z.size()>0){
      cout << 0 << endl;
    }else{
      vector<ll> v;
      rep(i,v_n.size()){
        v.push_back(-v_n[i]);
      }
      rep(i,v_p.size()){
        v.push_back(v_p[i]);
      }
      sort(ALLOF(v));
      mint tmp(1);
      tmp *= mod-1;
      rep(i,K){
        tmp *= v[i];
      }
      cout << tmp << endl;
    }
    return 0;
  }

  cout << ret << endl;
  
  return 0;
}