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>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
template<class T>bool chmax(T &a, const T &b) {if(a<b) {a=b; return 1;} return 0;}
template<class T>bool chmin(T &a, const T &b) {if(b<a) {a=b; return 1;} return 0;}
static const ll INF=1e18;
static const ll MAX=101010;
static const ll MOD=1e9+7;

//for(i=0; i<N; i++) cin >> a[i];
struct mll {
  ll val;
  mll(ll val=0):val((val%MOD+MOD)%MOD){}
  
  mll pow(ll t) const {
    if (!t) return 1;
    mll a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }
  mll inv() const { return pow(MOD-2); }
  
  mll operator-() const { return mll(-val);}
  mll& operator+=(const mll a) {
    if ((val += a.val) >= MOD) val -= MOD;
    return *this;
  }
  mll& operator-=(const mll a) {
    if ((val += MOD-a.val) >= MOD) val -= MOD;
    return *this;
  }
  mll& operator*=(const mll a) {
    (val *= a.val%MOD + MOD) %= MOD;
    return *this;
  }
  mll& operator/=(const mll a) {
    return (*this) *= a.inv();
  }
  
  mll operator+(const mll a) const {
    mll res(*this);
    return res+=a;
  }
  mll operator-(const mll a) const {
    mll res(*this);
    return res-=a;
  }
  mll operator*(const mll a) const {
    mll res(*this);
    return res*=a;
  }
  mll operator/(const mll a) const {
    mll res(*this);
    return res/=a;
  }
  
  mll& operator++(   ) { val++; return *this; }
  mll  operator++(int) { mll res(*this); val++; return res; }
  mll& operator--(   ) { val--; return *this; }
  mll  operator--(int) { mll res(*this); val--; return res; }
  
  bool operator< (const mll a) const { return val< a.val; }
  bool operator<=(const mll a) const { return val<=a.val; }
  bool operator> (const mll a) const { return val> a.val; }
  bool operator>=(const mll a) const { return val>=a.val; }
  bool operator==(const mll a) const { return val==a.val; }
  
  friend istream& operator>>(istream &input, mll &a) {
    input >> a.val;
    return input;
  }
  
  friend ostream& operator<<(ostream &output, mll &a) {
    output << a.val;
    return output;;
  }
  
};


int main(void) {
  ll i, j, k;
  
  
  ll N, K;
  cin >> N >> K;
  ll a[MAX*2];
  
  vector<ll> nv, pv;
  for(i=0; i<N; i++) {
    cin >> a[i];
    if(a[i]>0) pv.push_back(a[i]);
    else if(a[i]<0) nv.push_back(a[i]);
  }
  sort(a, a+N);
  reverse(a, a+N); //降順
  sort(pv.rbegin(), pv.rend()); //降順
  sort(nv.begin(), nv.end()); //昇順
  
  ll nc=nv.size();
  ll pc=pv.size();
  
  
  mll ans=1;
  if(pc==0) {
    for(i=0; i<K; i++) ans*=a[i];
  }else if(pc>=K) {
    
    if(K&1) {
      ans*=pv[0];
      pv.erase(pv.begin());
    }
    
    priority_queue<ll> q;
    for(i=0; i<K/2; i++) {
      j=i*2;
      ans*=pv[j];
      ans*=pv[j+1];
      q.push(-pv[j]*pv[j+1]);
    }
    
    mll t=ans;
    for(i=0; i<min(nc/2, K/2); i++) {
      j=i*2;
      t*=nv[j];
      t*=nv[j+1];
      
      t/=-q.top(); q.pop();
      q.push(-nv[j]*nv[j+1]);
      
      chmax(ans, t);
    }
    
  }else {
    ll f=(pc%2!=K%2);
    if((pc-f)&1) {
      ans*=pv[0];
      pv.erase(pv.begin());
    }
    
    priority_queue<mll> q;
    for(i=0; i<(pc-f)/2; i++) {
      j=i*2;
      ans*=pv[j];
      ans*=pv[j+1];
      q.push(-pv[j]*pv[j+1]);
    }
    
    for(i=0; i<(K-pc+f)/2; i++) {
      j=i*2;
      ans*=nv[j];
      ans*=nv[j+1];
      q.push(-nv[j]*nv[j+1]);
    }
    
    if(nc+pc-f<K) {
      ll b[MAX*2];
      for(i=0; i<N; i++) b[i]=abs(a[i]);
      sort(b, b+N);
      ans=-1;
      for(i=0; i<K; i++) ans*=b[i];
      pt(ans);
    }else {
      mll t=ans;
      for(i=(K-pc+f)/2; i<nc/2; i++) {
        j=i*2;
        t*=nv[j];
        t*=nv[j+1];
        
        t/=-q.top(); q.pop();
        q.push(-nv[j]*nv[j+1]);
        
        chmax(ans, t);
      }
    }
    
  }
  
  
  pt(ans);
  
  
}