Language: C++

#include <bits/stdc++.h>
using namespace std;

#define int long long

const long long mod = 1000000007;

long long mul_mod (long long x, long long y) {
	return (x * y) % mod;
}

long long pow_mod (long long x, long long y) {
	long long res = 1, t = x;
	while (y) {
		if (y & 1)
			(res *= t) %= mod;
		(t *= t) %= mod;
		y >>= 1;
	}
	return res;
}

long long div_mod (long long x, long long y) {
	return mul_mod(x, pow_mod(y, mod - 2));
}

bool cmp_abs (long long x, long long y) {
	return abs(x) < abs(y);
}

signed main () {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	int n, k;
	cin >> n >> k;
	vector < long long > a(n);
	deque < long long > pos, neg;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
		if (a[i] > 0)
			pos.push_back(a[i]);
		else
			neg.push_back(a[i]);
	}
	sort(pos.rbegin(), pos.rend());
	sort(neg.begin(), neg.end());
	deque < long long > negn = neg;
	if (int(pos.size()) >= k) {
		long long res = 1;
		deque < long long > took;
		for (int i = 0; i < k / 2; ++i) {
			res = mul_mod(res, mul_mod(pos[2 * i], pos[2 * i + 1]));
			took.push_back(pos[2 * i]);
			took.push_back(pos[2 * i + 1]);
		}
		if (k & 1) {
			res = mul_mod(res, pos[k - 1]);
			took.push_back(pos[k - 1]);
		}
		int ops = 0;
		while (int(neg.size()) >= 2 && ops < k / 2) {
			long long prv = took.back();
			took.pop_back();
			if (neg[0] * neg[1] > prv * took.back()) {
				res = div_mod(res, mul_mod(prv, took.back()));
				took.pop_back();
				res = mul_mod(res, mul_mod(neg[0], neg[1]));
				neg.pop_front();
				neg.pop_front();
			}
			else
				break;
			++ops;
		}
		cout << res << '\n';
	}
	else {
		long long res = 1;
		deque < long long > took;
		for (int i = 0; i < int(pos.size()) - (k % 2 - int(pos.size()) % 2 + 2) % 2; ++i) {
			res = mul_mod(res, pos[i]);
			took.push_back(pos[i]);
		}
		while (int(took.size()) < (k - k % 2) && neg.size() >= 2) {
			res = mul_mod(res, mul_mod(neg[0], neg[1]));
			took.push_back(abs(neg[0]));
			took.push_back(abs(neg[1]));
			neg.pop_front();
			neg.pop_front();
		}
		if (int(took.size()) < k) {
			sort(a.begin(), a.end(), cmp_abs);
			res = 1;
			for (int i = 0; i < k; ++i)
				res = mul_mod(res, a[i]);
			res = (res + mod) % mod;
			cout << res << '\n';
		}
		else {
			int ops = 0;
			while (int(neg.size()) >= 2 && ops < k / 2) {
				long long prv = took.back();
				took.pop_back();
				if (neg[0] * neg[1] > prv * took.back()) {
					res = div_mod(res, mul_mod(prv, took.back()));
					took.pop_back();
					res = mul_mod(res, mul_mod(neg[0], neg[1]));
					neg.pop_front();
					neg.pop_front();
				}
				else
					break;
				++ops;
			}
			cout << res << '\n';
		}
	}
}


Compiler output:
a.cc: In function 'int main()':
a.cc:4:13: error: expected primary-expression before 'long'
    4 | #define int long long
      |             ^~~~
a.cc:49:13: note: in expansion of macro 'int'
   49 |         if (int(pos.size()) >= k) {
      |             ^~~
a.cc:49:13: error: expected ')' before 'long'
   49 |         if (int(pos.size()) >= k) {
      |            ~^
      |             )
a.cc:4:13: error: expected primary-expression before 'long'
    4 | #define int long long
      |             ^~~~
a.cc:62:24: note: in expansion of macro 'int'
   62 |                 while (int(neg.size()) >= 2 && ops < k / 2) {
      |                        ^~~
a.cc:62:24: error: expected ')' before 'long'
   62 |                 while (int(neg.size()) >= 2 && ops < k / 2) {
      |                       ~^
      |                        )
a.cc:4:13: error: expected primary-expression before 'long'
    4 | #define int long long
      |             ^~~~
a.cc:62:24: note: in expansion of macro 'int'
   62 |                 while (int(neg.size()) >= 2 && ops < k / 2) {
      |                        ^~~
a.cc:4:13: error: expected primary-expression before 'long'
    4 | #define int long long
      |             ^~~~
a.cc:81:37: note: in expansion of macro 'int'
   81 |                 for (int i = 0; i < int(pos.size()) - (k % 2 - int(pos.size()) % 2 + 2) % 2; ++i) {
      |                                     ^~~
a.cc:81:36: error: expected ';' before 'long'
   81 |                 for (int i = 0; i < int(pos.size()) - (k % 2 - int(pos.size()) % 2 + 2) % 2; ++i) {
      |                                    ^
      |                                    ;
a.cc:4:13: error: expected primary-expression before 'long'
    4 | #define int long long
      |             ^~~~
a.cc:81:37: note: in expansion of macro 'int'
   81 |                 for (int i = 0; i < int(pos.size()) - (k % 2 - int(pos.size()) % 2 + 2) % 2; ++i) {
      |                                     ^~~
a.cc:81:36: error: expected ')' before 'long'
   81 |                 for (int i = 0; i < int(pos.size()) - (k % 2 - int(pos.size()) % 2 + 2) % 2; ++i) {
      |                     ~              ^
      |                                    )
a.cc:4:13: error: expected primary-expression before 'long'
    4 | #define int long long
      |             ^~~~
a.cc:81:37: note: in expansion of macro 'int'
   81 |                 for (int i = 0; i < int(pos.size()) - (k % 2 - int(pos.size()) % 2 + 2) % 2; ++i) {
      |                                     ^~~
a.cc:81:96: error: 'i' was not declared in this scope
   81 |                 for (int i = 0; i < int(pos.size()) - (k % 2 - int(pos.size()) % 2 + 2) % 2; ++i) {
      |                                                                                                ^
a.cc:4:13: error: expected primary-expression before 'long'
    4 | #define int long long
      |             ^~~~
a.cc:85:24: note: in expansion of macro 'int'
   85 |                 while (int(took.size()) < (k - k % 2) && neg.size() >= 2) {
      |                        ^~~
a.cc:85:24: error: expected ')' before 'long'
   85 |                 while (int(took.size()) < (k - k % 2) && neg.size() >= 2) {
      |                       ~^
      |                        )
a.cc:4:13: error: expected primary-expression before 'long'
    4 | #define int long long
      |             ^~~~
a.cc:85:24: note: in expansion of macro 'int'
   85 |                 while (int(took.size()) < (k - k % 2) && neg.size() >= 2) {
      |                        ^~~
a.cc:4:13: error: expected primary-expression before 'long'
    4 | #define int long long
      |             ^~~~
a.cc:92:21: note: in expansion of macro 'int'
   92 |                 if (int(took.size()) < k) {
      |                     ^~~
a.cc:92:21: error: expected ')' before 'long'
   92 |                 if (int(took.size()) < k) {
      |                    ~^
      |                     )
a.cc:4:13: error: expected primary-expression before 'long'
    4 | #define int long long
      |             ^~~~
a.cc:102:32: note: in expansion of macro 'int'
  102 |                         while (int(neg.size()) >= 2 && ops < k / 2) {
      |                                ^~~
a.cc:102:32: error: expected ')' before 'long'
  102 |                         while (int(neg.size()) >= 2 && ops < k / 2) {
      |                               ~^
      |                                )
a.cc:4:13: error: expected primary-expression before 'long'
    4 | #define int long long
      |             ^~~~
a.cc:102:32: note: in expansion of macro 'int'
  102 |                         while (int(neg.size()) >= 2 && ops < k / 2) {
      |                                ^~~