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 int long long
#define double long double
using namespace std;
const int MOD = 1000000007;
const int INF = 1e14;
using Graph = vector<vector<int>>;

signed main(){
  int N, K;
  cin >> N >> K;

  vector<int> plus;
  vector<int> minus;
  int kake = 1;
  for (int i = 0; i < N; i++){
    int A;
    cin >> A;
    if (A > 0) plus.push_back(A);
    else minus.push_back(A);
    kake *= A;
    kake %= MOD;
  }

  if (N == K){
    kake %= MOD;
    if (kake < 0) kake += MOD;
    cout << kake << endl;
    return 0;
  }

  sort(plus.rbegin(), plus.rend());

  int ans = 1;
  //正の数が1つもなかったら
  int m = (int)minus.size();
  if (plus.size() == 0){
    if (K % 2 == 1){
      sort(minus.rbegin(), minus.rend());
      for (int i = 0; i < K; i++){
        ans *= minus[i];
        ans %= MOD;
      }
    }
    else{
      sort(minus.begin(), minus.end());
      for (int i = 0; i < K; i++){
        ans *= minus[i];
        ans %= MOD;
      }
    }
  }
  else{ //正の数が一つ以上あったら
    sort(minus.begin(), minus.end());
    int cnt = 0;
    int pnow = 0;
    int mnow = 0;
    if (K % 2 == 1){
      ans *= plus[0];
      ans %= MOD;
      pnow++;
      cnt++;
    }
    while (cnt < K){
      int pkouho = -1;
      int mkouho = -1;
      if (pnow + 1 < (int)plus.size() ){
        pkouho = plus.at(pnow) * plus.at(pnow + 1);
      }
      if (mnow + 1 < (int)minus.size() ){
        mkouho = minus.at(mnow) * minus.at(mnow + 1);
      }
      if (pkouho > mkouho){
        ans *= pkouho;
        ans %= MOD;
        pnow += 2;
      }
      else{
        ans *= mkouho;
        ans %= MOD;
        mnow += 2;
      }
      cnt += 2;
    }
  }

  if (ans < 0){
    ans %= MOD;
    ans += MOD;
  }

  cout << ans << endl;
}