Language: C++

#include<bits/stdc++.h>
using namespace std;
#define MOD 1e9+7
#define ll long long
#define pb push_back
#define mp make_pair
bool isPrime(ll n) 
{ 
    
    if (n <= 1)  return false; 
    if (n <= 3)  return true; 
   
    if (n%2 == 0 || n%3 == 0) return false; 
  
    for (ll i=5; i*i<=n; i=i+6) 
        if (n%i == 0 || n%(i+2) == 0) 
           return false; 
  
    return true; 
} 
int minthis(int a, int b){
if(a < b){
return a;
}
return b;
}
int maxthis(int a, int b){
if(a > b){
return a;
}
return b;
}
int gcd(int a, int b){
    if( a<0 ) a = -a;
    if( b<0 ) b = -b;
    while( b!=0 ){
        a %= b;
        if( a==0 ) return b;
        b %= a;
    }
    return a;
}
 
int findLargestDivisor(int n) 
{ 
	int max = 1;
    for (int i = 2; i < n; i++) {        
        if(n %(i) == 0) { 
            max = i;
        } 
    }
    return max;     
} 
 
 
ll power(ll x, ll y)  
{  
    ll temp;  
    if(y == 0)  
        return 1;  
    temp = power(x, y / 2);  
    if (y % 2 == 0)  
        return temp * temp;  
    else
    {  
        if(y > 0)  
            return x * temp * temp;  
        else
            return (temp * temp) / x;  
    }  
}  


void solve(){		
	ll mod = 1000000007;
	int n,k;
	cin >> n >> k;
	vector<int> arr(n);
	bool haspos = false; 
	bool hasneg = false;
	ll ans = 1;
	vector<int> pos;
	vector<int> neg;
	vector<int> total;
	for(int i = 0 ; i < n ; i++){
		cin >> arr[i];
		if(arr[i]>=0) haspos = true;pos.pb(arr[i]);
		if(arr[i] < 0) hasneg = true;neg.pb(arr[i]);
	}
	if(!haspos){		
		if(k%2 == 0){
			sort(arr.begin(), arr.end());
			int i = 0;
			while(k--){
				ans*=arr[i];i++;
			}
			cout << ((-1*ans)%mod+mod)%mod << "\n";return;
		}
		else{
			sort(arr.rbegin() , arr.rend());
			int i = 0;
			while(k--){
				ans *= arr[i];i++;
			}
			cout << (mod+ans)%mod << "\n";return;
		}		
	}
	if(!hasneg){
		sort(arr.rbegin() , arr.rend());
		int i = 0;
		while(k--){
			ans *= arr[i];i++;
		}
		cout << ans%mod << "\n";return;
	}
	if(k == n){
		for(int i = 0 ; i < n ; i++){
			ans*=arr[i];
		}
		cout << ans << "\n";return;
	}
	
	sort(neg.begin(), neg.end());
	for(int i = 0 ; i < neg.size() ; i+=2){
		total.pb(neg[i]*neg[i+1]);
	}
	
	int po = pos.size();
	int tot = total.size();
	int idxpos = 0;
	int idxtotal = 0;
	sort(pos.rbegin(), pos.rend());
	while(k>=2){
		if(idxpos < po && idxtotal < tot && (ans*pos[idxpos] >= ans*total[idxtotal])){
			ans*=pos[idxpos]; idxpos++; k--;
		}
		else if(idx < po-1 && idxtotal < tot && (ans*pos[idxpos]*pos[idxpos+1] >= ans*total[idxtotal])) ans*=pos[idxpos]*pos[idxpos+1];k-=2;idxpos+=2;
		else if(idxtotal < tot){
			ans*=total[idxtotal];idxtotal++;k-=2;
		}
	}
	if(k && idxpos < po)
	cout << (ans*pos[idxpos])%mod << "\n";
	else if(k) cout << ((ans*neg[neg.size()-1])+mod)%mod << "\n";	
	else cout << ans%mod << "\n";
	
}
  
int main(){ 
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t = 1;    
    //cin >> t;
    while(t--){
		solve();
	}
    return 0; 
}


Compiler output:
a.cc: In function 'void solve()':
a.cc:137:25: error: 'idx' was not declared in this scope
  137 |                 else if(idx < po-1 && idxtotal < tot && (ans*pos[idxpos]*pos[idxpos+1] >= ans*total[idxtotal])) ans*=pos[idxpos]*pos[idxpos+1];k-=2;idxpos+=2;
      |                         ^~~
a.cc:138:17: error: 'else' without a previous 'if'
  138 |                 else if(idxtotal < tot){
      |                 ^~~~