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 <iostream>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <string>
#include <sstream>
#include <algorithm>
#include <random>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <cassert>
#include <climits>
#include <bitset>
#include <functional>
#include <iomanip>
#include <random>

#define FOR_LT(i, beg, end) for (int i = (int)(beg); i < (int)(end); i++)
#define FOR_LE(i, beg, end) for (int i = (int)(beg); i <= (int)(end); i++)
#define FOR_DW(i, beg, end) for (int i = (int)(beg); (int)(end) <= i; i--)
#define REP(n)              for (int repeat_index = 0; repeat_index < (int)n; repeat_index++)


using namespace std;


using namespace std;
template<int64_t M = 1000000007>
class ModInt {
public:
	ModInt() : val_(0) {};

	ModInt(int64_t val) {
		this->val_ = val;
		this->val_ %= M;
		while (this->val_ >= M) this->val_ -= M;
		while (this->val_ < 0)  this->val_ += M;
	}

	operator int64_t() const {
		return this->val_;
	}

	ModInt& operator += (int64_t val) {
		this->val_ += val;
		this->val_ %= M;
		while (this->val_ >= M) this->val_ -= M;
		while (this->val_ < 0) this->val_ += M;

		return *this;
	}

	ModInt& operator -= (int64_t val) {
		this->val_ -= val;
		this->val_ %= M;
		while (this->val_ >= M) this->val_ -= M;
		while (this->val_ < 0) this->val_ += M;

		return *this;
	}

	ModInt& operator *= (int64_t val) {
		this->val_ *= val;
		this->val_ %= M;

		return *this;
	}

	ModInt& operator /= (int64_t val) {
		ModInt v = ModInt::pow(val, M - 2);
		*this *= v;

		return *this;
	}

	ModInt operator + (int64_t v) const {
		ModInt ret = *this;
		return (ret += v);
	}

	ModInt operator - (int64_t v) const {
		ModInt ret = *this;
		return (ret -= v);
	}

	ModInt operator * (int64_t v) const {
		ModInt ret = *this;
		return (ret *= v);
	}

	ModInt operator / (int64_t v) const {
		ModInt ret = *this;
		return (ret /= v);
	}

	static ModInt pow(ModInt n, int64_t m) {
		if (m == 0) {
			ModInt ret(1);
			return ret;
		}
		if (m == 1) {
			return n;
		}

		ModInt v = pow(n, m / 2);
		ModInt ret = v * v;
		if (m & 1) {
			ret *= n;
		}
		return ret;
	}

	ostream& operator << (ostream& out) {
		out << this->val_;
		return out;
	}

private:
	int64_t val_;
};

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout << fixed << setprecision(20);

	int n, k; cin >> n >> k;

	vector<int64_t> as(n);

	bool all_minus = true;
	FOR_LT(i, 0, n) {
		cin >> as[i];
		if (as[i] >= 0) all_minus = false;
	}

	if (all_minus && (k & 1)) {
		sort(as.begin(), as.end(), [](const int64_t lhs, const int64_t rhs) {
			return abs(lhs) < abs(rhs);
		});
		ModInt<> ans(1);
		FOR_LT(i, 0, k) {
			ans *= abs(as[i]);
		}
		cout << ModInt<>(0) - ans << endl;
		return 0;
	}

	sort(as.begin(), as.end(), [](const int64_t lhs, const int64_t rhs) {
		return abs(lhs) > abs(rhs);
		});

	int cm = 0;
	FOR_LT(i, 0, k) {
		if (as[i] < 0) cm++;
	}
	if (as[k - 1] == 0) {
		cout << 0 << endl;
		return 0;
	}

	ModInt<> ans(1);
	FOR_LT(i, 0, k) {
		ans *= abs(as[i]);
	}
	if ((cm & 1) == 0) {
		cout << ans << endl;
		return 0;
	}

	int lidx = -1, ridx = -1;
	FOR_DW(i, k - 1, 0) {
		if (as[i] < 0) {
			lidx = i;
			break;
		}
	}
	FOR_LT(i, k, n) {
		if (as[i] >= 0) {
			ridx = i;
			break;
		}
	}

	int l2idx = -1, r2idx = -1;
	FOR_DW(i, k - 1, 0) {
		if (as[i] > 0) {
			l2idx = i;
			break;
		}
	}
	FOR_LT(i, k, n) {
		if (as[i] <= 0) {
			r2idx = i;
			break;
		}
	}

	//cout << lidx << " " << ridx << " " << l2idx << r2idx << endl;

	if (lidx == -1 || ridx == -1) {
		ans /= abs(as[l2idx]);
		ans *= abs(as[r2idx]);
		cout << ans << endl;
		return 0;
	}
	if (l2idx == -1 || r2idx == -1) {
		ans /= abs(as[lidx]);
		ans *= abs(as[ridx]);
		cout << ans << endl;
		return 0;
	}

	uint64_t d1 = abs(as[lidx]);
	uint64_t d2 = abs(as[l2idx]);
	uint64_t m1 = abs(as[ridx]);
	uint64_t m2 = abs(as[r2idx]);

	if (m1 * d2 >= m2 * d1) {
		ans /= d1;
		ans *= m1;
	}
	else {
		ans /= d2;
		ans *= m2;
	}
	cout << ans << endl;

	return 0;
}