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: Accepted)

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>
#include <queue>
#include <stack>
#include <cmath>

using namespace std;

typedef long long int ll;
typedef pair<int, int> Pii;

const ll mod = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (auto &x: a) cin >> x;

  sort(a.rbegin(), a.rend(), [](auto &x, auto &y){return abs(x) < abs(y);});

  vector<ll> neg, pos;
  for (int i = 0; i < k; i++) {
    if (a[i] < 0) neg.push_back(a[i]);
    else if (a[i] > 0) pos.push_back(a[i]);
    else {
      cout << 0 << endl;
      return 0;
    }
  }
  if (neg.size() % 2 == 1) {
    ll next_neg = 0;
    bool next_neg_found = false;
    ll next_pos = 0;
    bool next_pos_found = false;
    for (int i = k; i < n; i++) {
      if (a[i] < 0) {
        next_neg = a[i];
        next_neg_found = true;
        break;
      }
    }
    for (int i = k; i < n; i++) {
      if (a[i] > 0) {
        next_pos = a[i];
        next_pos_found = true;
        break;
      }
    }
    if ((!next_neg_found && !next_pos_found) || (pos.size() == 0 && !next_pos_found)) {
      ll ans = 1;
      for (int i = n-1; i >= n - k; i--) {
        ans = (ans * a[i]) % mod;
        if (ans < 0) ans += mod;
      }
      cout << ans << endl;
      return 0;
    }
    else if (pos.size() == 0) {
      neg.pop_back();
      pos.push_back(next_pos);
    }
    else if (!next_neg_found) {
      neg.pop_back();
      pos.push_back(next_pos);
    }
    else if (!next_pos_found) {
      pos.pop_back();
      neg.push_back(next_neg);
    }
    else if (pos[pos.size()-1] * next_pos > neg[neg.size()-1] * next_neg) {
      neg.pop_back();
      pos.push_back(next_pos);
    }
    else {
      pos.pop_back();
      neg.push_back(next_neg);
    }
  }

  ll ans = 1;
  for (auto &x: pos) {
    ans = (ans * x) % mod;
  }
  for (auto &x: neg) {
    ans = (ans * x) % mod;
    if (ans < 0) ans += mod;
  }

  cout << ans << endl;

  return 0;
}