Language: C++

#include <bits/stdc++.h>

using namespace std;

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

#define int ll

const ll mod = 1e9 + 7;

ll a[200000];

vector<ll> plus,minus;

main(){
	int n,k;
	cin >> n >> k;
	for(int i=0;i<n;i++) cin >> a[i];

	
	for(int i=0;i<n;i++){
		if(a[i]>=0) plus.push_back(a[i]);
		else minus.push_back(a[i]);
	}
	sort(plus.begin(),plus.end());
	reverse(plus.begin(),plus.end());
	sort(minus.begin(),minus.end());

	ll ans = 1;
	if(k%2==1 && plus.size()==0){
		ans = 1;
		for(int i=0;i<k;i++){
			ans *= minus[minus.size()-1-i];
			ans %= mod;
		}
		ans = (ans+mod)%mod;
	}else{
		int t = 0;
		for(int i=0;i+1<minus.size()&&i+1<k;i+=2){
			ll tmp1 = minus[i]*minus[i+1];
			ll tmp2 = plus[k-1-i]*plus[k-1-(i+1)];
			if(tmp1<tmp2){
				t = i;
				break;
			}else{
				t = i+2;
			}
		}
		ans = 1;
		for(int i=0;i<t;i+=2){
			ans *= (minus[i]*minus[i+1]);
			ans %= mod;
		}
		for(int i=0;i<k-t;i++){
			ans *= plus[i];
			ans %= mod;
		}
	}


	cout << ans << endl;
	


	return 0;
}


Compiler output:
a.cc:16:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   16 | main(){
      | ^~~~
a.cc: In function 'int main()':
a.cc:23:29: error: reference to 'plus' is ambiguous
   23 |                 if(a[i]>=0) plus.push_back(a[i]);
      |                             ^~~~
In file included from /usr/include/c++/14/string:49,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_function.h:160:12: note: candidates are: 'template<class _Tp> struct std::plus'
  160 |     struct plus;
      |            ^~~~
a.cc:14:12: note:                 'std::vector<long long int> plus'
   14 | vector<ll> plus,minus;
      |            ^~~~
a.cc:24:22: error: reference to 'minus' is ambiguous
   24 |                 else minus.push_back(a[i]);
      |                      ^~~~~
/usr/include/c++/14/bits/stl_function.h:163:12: note: candidates are: 'template<class _Tp> struct std::minus'
  163 |     struct minus;
      |            ^~~~~
a.cc:14:17: note:                 'std::vector<long long int> minus'
   14 | vector<ll> plus,minus;
      |                 ^~~~~
a.cc:26:14: error: reference to 'plus' is ambiguous
   26 |         sort(plus.begin(),plus.end());
      |              ^~~~
/usr/include/c++/14/bits/stl_function.h:160:12: note: candidates are: 'template<class _Tp> struct std::plus'
  160 |     struct plus;
      |            ^~~~
a.cc:14:12: note:                 'std::vector<long long int> plus'
   14 | vector<ll> plus,minus;
      |            ^~~~
a.cc:26:27: error: reference to 'plus' is ambiguous
   26 |         sort(plus.begin(),plus.end());
      |                           ^~~~
/usr/include/c++/14/bits/stl_function.h:160:12: note: candidates are: 'template<class _Tp> struct std::plus'
  160 |     struct plus;
      |            ^~~~
a.cc:14:12: note:                 'std::vector<long long int> plus'
   14 | vector<ll> plus,minus;
      |            ^~~~
a.cc:27:17: error: reference to 'plus' is ambiguous
   27 |         reverse(plus.begin(),plus.end());
      |                 ^~~~
/usr/include/c++/14/bits/stl_function.h:160:12: note: candidates are: 'template<class _Tp> struct std::plus'
  160 |     struct plus;
      |            ^~~~
a.cc:14:12: note:                 'std::vector<long long int> plus'
   14 | vector<ll> plus,minus;
      |            ^~~~
a.cc:27:30: error: reference to 'plus' is ambiguous
   27 |         reverse(plus.begin(),plus.end());
      |                              ^~~~
/usr/include/c++/14/bits/stl_function.h:160:12: note: candidates are: 'template<class _Tp> struct std::plus'
  160 |     struct plus;
      |            ^~~~
a.cc:14:12: note:                 'std::vector<long long int> plus'
   14 | vector<ll> plus,minus;
      |            ^~~~
a.cc:28:14: error: reference to 'minus' is ambiguous
   28 |         sort(minus.begin(),minus.end());
      |              ^~~~~
/usr/include/c++/14/bits/stl_function.h:163:12: note: candidates are: 'template<class _Tp> struct std::minus'
  163 |     struct minus;
      |            ^~~~~
a.cc:14:17: note:                 'std::vector<long long int> minus'
   14 | vector<ll> plus,minus;
      |                 ^~~~~
a.cc:28:28: error: reference to 'minus' is ambiguous
   28 |         sort(minus.begin(),minus.end());
      |                            ^~~~~
/usr/include/c++/14/bits/stl_function.h:163:12: note: candidates are: 'template<class _Tp> struct std::minus'
  163 |     struct minus;
      |            ^~~~~
a.cc:14:17: note:                 'std::vector<long long int> minus'
   14 | vector<ll> plus,minus;
      |                 ^~~~~
a.cc:31:22: error: reference to 'plus' is ambiguous
   31 |         if(k%2==1 && plus.size()==0){
      |                      ^~~~
/usr/include/c++/14/bits/stl_function.h:160:12: note: candidates are: 'template<class _Tp> struct std::plus'
  160 |     struct plus;
      |            ^~~~
a.cc:14:12: note:                 'std::vector<long long int> plus'
   14 | vector<ll> plus,minus;
      |            ^~~~
a.cc:34:32: error: reference to 'minus' is ambiguous
   34 |                         ans *= minus[minus.size()-1-i];
      |                                ^~~~~
/usr/include/c++/14/bits/stl_function.h:163:12: note: candidates are: 'template<class _Tp> struct std::minus'
  163 |     struct minus;
      |            ^~~~~
a.cc:14:17: note:                 'std::vector<long long int> minus'
   14 | vector<ll> plus,minus;
      |                 ^~~~~
a.cc:34:38: error: reference to 'minus' is ambiguous
   34 |                         ans *= minus[minus.size()-1-i];
      |                                      ^~~~~
/usr/include/c++/14/bits/stl_function.h:163:12: note: candidates are: 'template<class _Tp> struct std::minus'
  163 |     struct minus;
      |            ^~~~~
a.cc:14:17: note:                 'std::vector<long long int> minus'
   14 | vector<ll> plus,minus;
      |                 ^~~~~
a.cc:40:33: error: reference to 'minus' is ambiguous
   40 |                 for(int i=0;i+1<minus.size()&&i+1<k;i+=2){
      |                                 ^~~~~
/usr/include/c++/14/bits/stl_function.h:163:12: note: candidates are: 'template<class _Tp> struct std::minus'
  163 |     struct minus;
      |            ^~~~~
a.cc:14:17: note:                 'std::vector<long long int> minus'
   14 | vector<ll> plus,minus;
      |                 ^~~~~
a.cc:41:35: error: reference to 'minus' is ambiguous
   41 |                         ll tmp1 = minus[i]*minus[i+1];
      |                                   ^~~~~
/usr/include/c++/14/bits/stl_function.h:163:12: note: candidates are: 'template<class _Tp> struct std::minus'
  163 |     struct minus;
      |            ^~~~~
a.cc:14:17: note:                 'std::vector<long long int> minus'
   14 | vector<ll> plus,minus;
      |                 ^~~~~
a.cc:41:44: error: reference to 'minus' is ambiguous
   41 |                         ll tmp1 = minus[i]*minus[i+1];
      |                                            ^~~~~
/usr/include/c++/14/bits/stl_function.h:163:12: note: candidates are: 'template<class _Tp> struct std::minus'
  163 |     struct minus;
      |            ^~~~~
a.cc:14:17: note:                 'std::vector<long long int> minus'
   14 | vector<ll> plus,minus;
      |                 ^~~~~
a.cc:42:35: error: reference to 'plus' is ambiguous
   42 |                         ll tmp2 = plus[k-1-i]*plus[k-1-(i+1)];
      |                                   ^~~~
/usr/include/c++/14/bits/stl_function.h:160:12: note: candidates are: 'template<class _Tp> struct std::plus'
  160 |     struct plus;
      |            ^~~~
a.cc:14:12: note:                 'std::vector<long long int> plus'
   14 | vector<ll> plus,minus;
      |            ^~~~
a.cc:42:47: error: reference to 'plus' is ambiguous
   42 |                         ll tmp2 = plus[k-1-i]*plus[k-1-(i+1)];
      |                                               ^~~~
/usr/include/c++/14/bits/stl_function.h:160:12: note: candidates are: 'template<class _Tp> struct std::plus'
  160 |     struct plus;
      |            ^~~~
a.cc:14:12: note:                 'std::vector<long long int> plus'
   14 | vector<ll> plus,minus;
      |            ^~~~
a.cc:52:33: error: reference to 'minus' is ambiguous
   52 |                         ans *= (minus[i]*minus[i+1]);
      |                                 ^~~~~
/usr/include/c++/14/bits/stl_function.h:163:12: note: candidates are: 'template<class _Tp> struct std::minus'
  163 |     struct minus;
      |            ^~~~~
a.cc:14:17: note:                 'std::vector<long long int> minus'
   14 | vector<ll> plus,minus;
      |                 ^~~~~
a.cc:52:42: error: reference to 'minus' is ambiguous
   52 |                         ans *= (minus[i]*minus[i+1]);
      |                                          ^~~~~
/usr/include/c++/14/bits/stl_function.h:163:12: note: candidates are: 'template<class _Tp> struct std::minus'
  163 |     struct minus;
      |            ^~~~~
a.cc:14:17: note:                 'std::vector<long long int> minus'
   14 | vector<ll> plus,minus;
      |                 ^~~~~
a.cc:56:32: error: reference to 'plus' is ambiguous
   56 |                         ans *= plus[i];
      |                                ^~~~
/usr/include/c++/14/bits/stl_function.h:160:12: note: candidates are: 'template<class _Tp> struct std::plus'
  160 |     struct plus;
      |            ^~~~
a.cc:14:12: note:                 'std::vector<long long int> plus'
   14 | vector<ll> plus,minus;
      |            ^~~~