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 <bits/stdc++.h>
using namespace std;

#define ll long long int
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define all(A) A.begin(), A.end()
#define sz(A) (int)A.size()
typedef vector <int> vi;
typedef vector <ll> vl;
typedef pair <int, int> pii;
typedef tuple <int, int, int> tii;
const ll mod = 1e9 + 7;
//const ll mod = 998244353;
const int inf = 1e9;
//const ll inf = 1e18;
const int N = 2e5;

ll power (ll n, ll k) {
	ll res = 1;
	while (k != 0) {
		if (k & 1) 
			res = (res * n) % mod;
		n = (n * n) % mod;
		k /= 2;
	}
	return res;
}

void solve () {
	int n, k;
	cin >> n >> k;
	ll a[n];
	vector <pair<ll, ll>> v; 
	int cnt = 0;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		if (a[i] >= 0) {
			v.pb(mp(a[i], 0));
		}
		else {
			v.pb(mp(-a[i], 1));
			cnt++;
		}
	}
	ll mul = 1;
	if (cnt == n) {
		if (k & 1) sort(all(v));
		else sort(all(v), greater <pair <ll, ll>>());
		for (int i = 0; i < k; i++) {
			mul = (mul * v[i].fi) % mod;
			if (v[i].se) 
				mul = (mod - mul) % mod;
		}
		cout << mul << "\n";
		return;
	}
	if (n == k) {
		for (int i = 0; i < k; i++) {
			mul = (mul * v[i].fi) % mod;
			if (v[i].se) 
				mul = (mod - mul) % mod;
		}
		cout << mul << "\n";
		return;
	}
	sort(all(v), greater<pair<ll, ll>>());
	cnt = 0;
	for (int i = 0; i < k; i++) {
		mul = (mul * v[i].fi) % mod;
		if (v[i].se) 
			cnt++;
	}
	ll x, y;
	if (cnt & 1) {
		x = -1;
		for (int i = k - 1; i >= 0; i--) {
			if (v[i].se == 0) {
				x = v[i].fi;
				break;
			}
		}
		y = -1;
		for (ll i = k - 1; i >= 0; i--) {
			if (v[i].se == 1) {
				y = v[i].fi;
				break;
			}
		}
		if (x == -1) {
			mul = (mul * power(y, mod - 2)) % mod;
			for (int i = k; i < n; i++) {
				if (v[i].se == 0) {
					mul = (mul * v[i].fi) % mod;
					break;
				}
			}
		}
		else {
			ll p = -1, q = -1;
			for (int i = k; i < n; i++) {
				if (v[i].se == 0) {
					p = v[i].fi;
					break;
				}
			}
			for (int i = k; i < n; i++) {
				if (v[i].se == 1) {
					q = v[i].fi;
					break;
				}
			}
			if (q == -1) {
				mul = (mul * power(y, mod - 2)) % mod;
				mul = (mul * p) % mod;
			}
			else if (p == -1) {
				mul = (mul * power(x, mod - 2)) % mod;
				mul = (mul * q) % mod;
			}
			else if (q * y > p * x) {
				mul = (mul * power(x, mod - 2)) % mod;
				mul = (mul * q) % mod;
			}
			else {
				mul = (mul * power(y, mod - 2)) % mod;
				mul = (mul * p) % mod;
			}
		}
	}
	cout << mul << "\n";
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
  #endif
	int t = 1;
	while (t--) {
		solve();
	}
 	return 0;
}