Language: C++

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
int n, k;
ll  ans = 1;
int l_neg = -1, l_pos = -1, r_neg = -1, r_pos = -1;
const ll M = 1e9+7;

ll f(ll a,ll b)
{
    ll k=0;
    while(b)
    {
        if(b&1) k=(k+a)%M;
        a=(a+a)%M;
        b>>=1;
    }
    return k;
}

bool cmp(ll a, ll b)
{
	return abs(a) < abs(b);
}

ll calc(vector<ll> a)
{
	ll temp = 1;
	for(int i = n - k; i < n; i++)
		temp =temp * a[i] % M;
	return temp%M;
}

bool v_cmp(vector<ll> a, vector<ll> b)
{
	ll sum1 = 0, sum2 = 0;
	for(int i = n - k; i < n; i++){
		sum1 += abs(a[i]);
		sum2 += abs(b[i]);
	}
	return sum1 > sum2;
}

int main() {
	cin >> n >> k;
	vector<ll> a(n, 0);
	for(int i = 0; i < n; i++)
		cin >> a[i];
	sort(a.begin(), a.end(), cmp);

	for(int i = n - k; i < n; i++){
		ans = (ans * a[i]) % M;
		if(r_pos == -1 && a[i] > 0) r_pos = i;
		if(r_neg == -1 && a[i] < 0) r_neg = i;
	}
	for(int i = n - k - 1; i >= 0; i--){
		if(l_pos == -1 && a[i] > 0) l_pos = i;
		if(l_neg == -1 && a[i] < 0) l_neg = i;
	}

	if(ans < 0 && n != k){
		if(r_pos == -1){
			if(l_pos == -1){
				ans = 1;
				for(int i = 0; i < k; i++)
					ans = (ans * a[i]) % M;
			}
			else {
				swap(a[l_pos], a[r_neg]);
				ans = calc(a);
			}
		}
		else {
			if(l_neg == -1){
				swap(a[l_pos], a[r_neg]);
				ans = calc(a);
			}
			else if(l_pos == -1){
				swap(a[l_neg], a[r_pos]);
				ans = calc(a);
			}
			else {
				vector<ll> c1 = a;
				vector<ll> c2 = a;
				swap(c1[l_neg], c1[r_pos]);
				swap(c2[l_pos], c2[r_neg]);
				ans = v_cmp(c1, c2) == true? calc(c1): calc(c2);
			}
		}
	}
	while(ans<0) ans+=M;
	cout << ans%M << endl;
	return 0;
}


Compiler output:
a.cc: In function 'bool cmp(ll, ll)':
a.cc:23:19: error: call of overloaded 'abs(ll&)' is ambiguous
   23 |         return abs(a) < abs(b);
      |                ~~~^~~
In file included from /usr/include/c++/14/cstdlib:79,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:42,
                 from a.cc:1:
/usr/include/stdlib.h:980:12: note: candidate: 'int abs(int)'
  980 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
In file included from /usr/include/c++/14/cstdlib:81:
/usr/include/c++/14/bits/std_abs.h:137:3: note: candidate: 'constexpr __float128 std::abs(__float128)'
  137 |   abs(__float128 __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:85:3: note: candidate: 'constexpr __int128 std::abs(__int128)'
   85 |   abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~
a.cc:23:28: error: call of overloaded 'abs(ll&)' is ambiguous
   23 |         return abs(a) < abs(b);
      |                         ~~~^~~
/usr/include/stdlib.h:980:12: note: candidate: 'int abs(int)'
  980 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
/usr/include/c++/14/bits/std_abs.h:137:3: note: candidate: 'constexpr __float128 std::abs(__float128)'
  137 |   abs(__float128 __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:85:3: note: candidate: 'constexpr __int128 std::abs(__int128)'
   85 |   abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~
a.cc: In function 'bool v_cmp(std::vector<long long unsigned int>, std::vector<long long unsigned int>)':
a.cc:38:28: error: call of overloaded 'abs(__gnu_cxx::__alloc_traits<std::allocator<long long unsigned int>, long long unsigned int>::value_type&)' is ambiguous
   38 |                 sum1 += abs(a[i]);
      |                         ~~~^~~~~~
/usr/include/stdlib.h:980:12: note: candidate: 'int abs(int)'
  980 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
/usr/include/c++/14/bits/std_abs.h:137:3: note: candidate: 'constexpr __float128 std::abs(__float128)'
  137 |   abs(__float128 __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:85:3: note: candidate: 'constexpr __int128 std::abs(__int128)'
   85 |   abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~
a.cc:39:28: error: call of overloaded 'abs(__gnu_cxx::__alloc_traits<std::allocator<long long unsigned int>, long long unsigned int>::value_type&)' is ambiguous
   39 |                 sum2 += abs(b[i]);
      |                         ~~~^~~~~~
/usr/include/stdlib.h:980:12: note: candidate: 'int abs(int)'
  980 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
/usr/include/c++/14/bits/std_abs.h:137:3: note: candidate: 'constexpr __float128 std::abs(__float128)'
  137 |   abs(__float128 __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:85:3: note: candidate: 'constexpr __int128 std::abs(__int128)'
   85 |   abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~