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: Wrong Answer)

#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include<algorithm> 
#include <iomanip>
#include <math.h>
#include <iostream>
#include <bitset>
#include <map>

#define db double
#define vi vector<int>
#define pb push_back
#define fi first
#define se second
#define ll long long
#define ld long double
#define maxn 5005
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vll vector<ll>
#define vpi vector<pii>
#define vpll vector<pll>
#define vv(z,a,b) vector<vector<int>>z(a,vector<int>(b))
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(c) begin(c),end(c)
#define pp pair<int,int>
#define fix(n) fixed << setprecision(n)
#define gmax(a,b) __gcd(a,b)
#define gmin(a,b) a/__gcd(a,b)*b
using namespace std;
long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m; 
    if (u < 0) u += m;
    return u;
}
const int mod = 1000000007;

struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

//obj;
class binary{
public:
    bool isOK(int index, int key,vi a) {
    if (a[index] >= key) return true;
    else return false;
}
int binary_search(int key,vi a) {
   
    int ng = -1; //「index = 0」が条件を満たすこともあるので、初期値は -1
    int ok = (int)a.size(); // 「index = a.size()-1」が条件を満たさないこともあるので、初期値は a.size()

    /* ok と ng のどちらが大きいかわからないことを考慮 */
    while (abs(ok - ng) > 1) {
        int mid = (ok + ng) / 2;

        if (isOK(mid, key,a)) ok = mid;
        else ng = mid;
    }
    return ok;
}
}obj;

ll hantei(int a){
    ll ans=1;
   for (int i = 2; i <= sqrt(a); i++) {
    ll cnt = 0;
    while (a % i == 0) {
      cnt++;
      a /= i;
    }
    ans *= (cnt + 1);
    if (a == 1) break;
   }
   if (a != 1) ans *= 2;
ans=ans*a;
    return ans;
   
}
#define MAX 1000000007

int main(){
    int n,k;cin>>n>>k;
	vll a(n+1,-1);
	vll b(n+1,1);
	rep(i,n){
ll an=0;
	cin>>an;
	if(an<0)b.at(i)=an;
	else a.at(i)=an%MAX;
	}
ll ans=1;
	sort(all(a));
	reverse(all(a));
	sort(all(b));
  ll ansn=1;
	if(a.at(0)==-1){
	
		if(k%2==0){rep(i,k){
			ansn*=b.at(i);
	  		ansn%=MAX;
	        if(ansn<0)ansn+=MAX;
			ansn%=MAX;
			
		}
		ansn%=MAX;
		 if(ansn<0)ansn+=MAX;
		 ansn%=MAX;
		cout<<ansn<<endl;
		return 0;
		}else{
		reverse(all(b));
		for(int i=1;i<=k;i++){
					ansn*=b.at(i);
			  		ansn%=MAX;
			        if(ansn<0)ansn+=MAX;
					ansn%=MAX;
			
				}
          //cout<<ansn<<endl;
		  		ansn%=MAX;
		        if(ansn<0)ansn+=MAX;
				ansn%=MAX;
				cout<<ansn<<endl;
				return 0;
		
		}
	}else {
	if(k%2==0){
		for(int j=0;j<n;j+=2){
			a.at(j)=(a.at(j)*a.at(j+1))%MAX;
			b.at(j)=(b.at(j)*b.at(j+1))%MAX;
		}
		int h=0,in=0,jn=0;
		while(h<k){
			ans*=max(a.at(in),b.at(jn));
		ans%=MAX;
      if(ans<0)ans+=MAX;
			if(a.at(in)<b.at(jn))jn+=2;
			else in+=2;
			h+=2;
			
		}
  		ans%=MAX;
        if(ans<0)ans+=MAX;
		ans%=MAX;
		cout<<ans<<endl;
		return 0;
	
	}else{
	for(int j=0;j<n;j+=2){
		b.at(j)=(b.at(j)*b.at(j+1))%MAX;

		
     
	}
	for(int j=1;j<n;j+=2){
		a.at(j)=(a.at(j)*a.at(j+1))%MAX;
	}
	int h=0,in=0,jn=0;
	while(h<k){
		ans*=max(a.at(in),b.at(jn));
		ans%=MAX;
      if(ans<0)ans+=MAX;
		if(a.at(in)<b.at(jn)){
		jn+=2;
		h+=2;}
		else if(in==0&&a.at(in)>b.at(jn)){
		in++;
		h++;}
		else {
		in+=2;
		h+=2;
	}
	}
	ans%=MAX;
    if(ans<0)ans+=MAX;
	ans%=MAX;
	cout<<ans<<endl;
	return 0;
	
	
	}
	
	
	}
	
	
	
	return 0;
 }