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 <iomanip>
#include <string>
#include <vector>
#include <map>
#include <algorithm>  // std::unique
#include <cmath>
#include <limits.h>
#include <queue>
#include <unordered_map>
#include <sstream>      // std::ostringstream
#include <bitset>
#include <iomanip>
#include <functional>
#include <set>
#include <list>

#define rep(i,a,b) for(LL i=a;i<=b;++i)
#define repb(i,a,b) for(LL i=a;i>=b;--i)

#define _USE_MATH_DEFINES
#include <math.h>
using namespace std;

using ll = long long;
constexpr auto PrimeBigNum10_9_7 = 1000000007;
const long long INF = 1LL << 60;

#define OUTPUT_DEBUG_LOG

template <typename T>
void DisplayAnswer(const T& v) {
	cout << v << endl;
}
template <typename T>
void DisplayAnswers(const vector<T>& vs) {
	for (const auto& v : vs) {
		cout << v << endl;
	}
}

template <typename T>
void DisplayAnswerOneLine(const vector<T>& vs) {
	for (const auto& v : vs) {
		cout << v << ' ';
	}
	cout << endl;
}


std::vector<std::string> split(const std::string &str, char sep)
{
	std::vector<std::string> v;
	std::stringstream ss(str);
	std::string buffer;
	while (std::getline(ss, buffer, sep)) {
		v.push_back(buffer);
	}
	return v;
}



template<class T>
void chmin(T& a, T b) {
	if (a > b) {
		a = b;
	}
}


// 素数判定
template <typename T>
bool IsPrimeNumber(T value) {

	for (int i = 2; i * i <= value; ++i) {
		if ((value % i) == 0) {
			return true;
		}
	}
	return false;
}

// 桁数判定
template <typename T>
T GetFigureLength(T value) {
	T figure = 1;
	while (value >= 10) {
		value /= 10;
		++figure;
	}
	return figure;
}

// 素因数分解結果
template <typename T>
map<T, T> GetPrimeFactorize(T value) {

	map<T, T> res;

	for (T i = 2; i * i <= value; ++i) {
		if ((value%i) != 0)	continue;

		T count = 0;
		while ((value % i) == 0) {
			value /= i;
			++count;
		}

		res[i] = count;
	}

	if (value != 1) {
		res[value] = 1;
	}
	return res;
}


void DebugPrint(const vector<ll>& vec) {
	cout << "{";
	for (auto itr = vec.begin(); itr != vec.end(); ++itr) {
		cout << *itr << ",";
	}
	cout << "}" << endl;
}

// valueが2のx乗以下である計算
ll calc2x(ll value) {
	ll n = 0;
	while (value > 0) {
		value /= 2;
		++n;
	}
	return n;
}



struct Vec2 {
	double x;
	double y;

	void rotate(double rad) {
		double s = sin(rad);
		double c = cos(rad);
		double tmpX = this->x*c - this->y*s;
		double tmpY = this->x*s + this->y*c;
		this->x = tmpX;
		this->y = tmpY;
	}
};

void FuncA() {
	ll m;
	cin >> m;

	while (m > 0) {
		m -= 1000;
	}
	DisplayAnswer(-m);
}

void FuncB() {
	unordered_map<string, int> map;

	map["AC"]=0;
	map["WA"] = 0;
	map["TLE"] = 0;
	map["RE"] = 0;

	ll n;
	cin >> n;
	while (n--) {
		string str;
		cin >> str;
		map[str]++;
	}

	cout << "AC x " << map["AC"] << endl;
	cout << "WA x " << map["WA"] << endl;
	cout << "TLE x " << map["TLE"] << endl;
	cout << "RE x " << map["RE"] << endl;
}
namespace C {
	ll map[6][6] = {};
	int calcSum(bitset<6> bh, bitset<6>bw) {
		ll sum = 0;
		for (ll i = 0; i < 6; ++i) {
			if (bh[i] == 0)	continue;
			for (ll j = 0; j < 6; ++j) {
				if (bw[j]) {
					sum += map[i][j];
				}
			}
		}
		return sum;

	}

	int CalcFuncC() {
		ll H, W, K;
		cin >> H >> W >> K;
		ll mapHs[6] = {};

		ll black = 0;
		for (ll i = 0; i < H; ++i) {
			for (ll j = 0; j < W; ++j) {
				char c;
				cin >> c;
				map[i][j] = (c == '#') ? 1 : 0;
				black += map[i][j];
				mapHs[i] += map[i][j];
			}
		}
		ll hMax = 1 << H;
		ll wMax = 1 <<W;

		ll answer = 0;
		for (ll i = 0; i < hMax; ++i) {
			bitset<6> bh = i;
			ll n = 0;
			for (ll k = 0; k < 6; ++k) {
				if (bh[k]) {
					n += mapHs[k];
				}
			}
			if (n < K)	continue;

			for (ll j = 0; j < wMax; ++j) {
				ll sum = calcSum(bh, j);
				if (sum == K) {
					bitset<6> bw = j;
					answer++;
				}
			}
		}

		return answer;

	}
}

void FuncC() {
	DisplayAnswer(C::CalcFuncC());
}

void FuncD() {
	ll N;
	cin >> N;
	vector<ll> players(N);
	for ( ll i = 0; i < N; ++ i ){
		cin >> players[i];
	}
	sort(players.rbegin(), players.rend());

	std::list<ll> list;
	list.push_back(players[0]);
	ll sum = players[0];
	for (ll i = 1; i < N/2; ++i) {
		sum += players[i] * 2;
	}

	DisplayAnswer(sum);
}

ll calcFuncE() {
	ll N, K;
	cin >> N >> K;
	vector<ll> vecPositive;
	vector<ll> vecNegative;
	while (N--) {
		ll n;
		cin >> n;

		if (n > 0) {
			vecPositive.push_back(n);
		}
		else {
			vecNegative.push_back(n);
		}
	}
	if (vecPositive.size() + vecNegative.size() < K)	return 0;

	sort(vecPositive.rbegin(), vecPositive.rend());
	sort(vecNegative.begin(), vecNegative.end());

	if (N == K) {
		ll ans = vecPositive[0]% PrimeBigNum10_9_7;
		for (ll i = 1; i < vecPositive.size(); ++i) {
			ans *= vecPositive[i];
			ans %= PrimeBigNum10_9_7;
		}
		for (ll i = 0; i < vecNegative.size(); ++i) {
			ans *= vecNegative[i];
			ans %= PrimeBigNum10_9_7;
		}
		return ans;
	}
	else if (vecPositive.size() == 0) {
		if ((K % 2) == 1) {
			ll ans = vecNegative[vecNegative.size() - 1] % PrimeBigNum10_9_7;
			for (ll i = 1; i < K; ++i) {
				ans *= vecNegative[vecNegative.size() - 1 - i];
				ans %= PrimeBigNum10_9_7;
			}
			return ans;
		}
		else
		{
			sort(vecNegative.begin(), vecNegative.end());
			ll ans = vecNegative[0];
			for (ll i = 1; i < vecNegative.size(); ++i) {
				ans *= vecNegative[i];
				ans %= PrimeBigNum10_9_7;
			}
			return ans;
		}
	}
	else {	
		ll ans = 0;
		ll indexP = 0;
		ll indexN = 0;
		for (ll count = 0; count < K; ) {
			bool addP = false;
			if (count + 1 == K) {
				addP = true;
			}
			else if (indexP == vecPositive.size()) {
				addP = false;
			}
			else if (indexN == vecNegative.size()) {
				addP = true;
			}
			else if (indexP == vecPositive.size() - 1&&(K%2)==1) {
				addP = false;
			}
			else {
				ll p = vecPositive[indexP] * vecPositive[indexP + 1];
				ll n = vecNegative[indexN] * vecNegative[indexN + 1];
				addP = p > n;
			}
			
			if (addP) {
				if (count == 0) {
					ans = vecPositive[indexP];
				}
				else {
					ans *= vecPositive[indexP];
				}
				indexP++;
				count++;
			}
			else {
				if (count == 0) {
					ans = vecNegative[indexN] * vecNegative[indexN + 1];
				}
				else {
					ans *= vecNegative[indexN];
					ans %= PrimeBigNum10_9_7;
					ans *=vecNegative[indexN + 1];
				}
				indexN += 2;
				count += 2;
			}


			ans %= PrimeBigNum10_9_7;

		}
		return ans;
	}
}

void FuncE() {

	auto ans = calcFuncE();
	if (ans < 0) {
		ans += PrimeBigNum10_9_7;
	}
	
	DisplayAnswer(ans% PrimeBigNum10_9_7);

}
int main(void) {

	FuncE();

	return 0;
}