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: Accepted)

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__;in(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;in(__VA_ARGS__)
#define vec(type,name,...) vector<type> name(__VA_ARGS__)
#define VEC(type,name,size) vector<type> name(size);in(name)
#define vv(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define VV(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__));in(name)
#define vvv(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
#define SYOU(x) fixed<<setprecision(x) //小数点桁数を指定する
inline int scan(){ return getchar(); }
inline void scan(int &a){ scanf("%d", &a); }
inline void scan(unsigned &a){ scanf("%u", &a); }
inline void scan(long &a){ scanf("%ld", &a); }
inline void scan(long long &a){ scanf("%lld", &a); }
inline void scan(unsigned long long &a){ scanf("%llu", &a); }
inline void scan(char &a){ cin >> a; }
inline void scan(float &a){ scanf("%f", &a); }
inline void scan(double &a){ scanf("%lf", &a); }
inline void scan(long double &a){ scanf("%Lf", &a); }
inline void scan(vector<bool> &vec){ for(unsigned i = 0; i < vec.size(); i++) { int a; scan(a); vec[i] = a; } }
inline void scan(string &a){ cin >> a; }
template<class T> inline void scan(vector<T> &vec);
template<class T, size_t size> inline void scan(array<T, size> &vec);
template<class T, class L> inline void scan(pair<T, L> &p);
template<class T, size_t size> inline void scan(T (&vec)[size]);
template<class T> inline void scan(vector<T> &vec){ for(auto &i : vec) scan(i); }
template<class T, size_t size> inline void scan(array<T, size> &vec){ for(auto &i : vec) scan(i); }
template<class T, class L> inline void scan(pair<T, L> &p){ scan(p.first); scan(p.second); }
template<class T, size_t size> inline void scan(T (&vec)[size]){ for(auto &i : vec) scan(i); }
template<class T> inline void scan(T &a){ cin>>a; }
inline void in(){}
template <class Head, class... Tail> inline void in(Head &head, Tail&... tail){ scan(head); in(tail...); }
inline constexpr ll gcd(ll a,ll b){if(!a||!b)return 0;while(b){ll c=b;b=a%b;a=c;}return a;}
inline constexpr ll lcm(ll a,ll b){if(!a||!b)return 0;return a*b/gcd(a,b);}
inline ll mod(ll val, ll m) {long long res = val % m;if (res < 0) res += m;return res;}
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using Graph = vector<vector<int>>;
/* 4方向への隣接頂点への移動を表すベクトル */
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

const long long INF = 1LL << 60;
const ll MOD = 1000000007;
int main(){
    INT(n,k);
    vec(ll,pos,0,0);
    vec(ll,neg,0,0);
    vec(ll,ans,0,0);
    ll answer = 1LL;
    for(int i = 0; i < n; ++i) {
        LL(a);
        if(a >=0 ) {
            pos.push_back(a);
        } else {
            neg.push_back(a);
        }
    }
    sort(pos.begin(),pos.end(),greater<ll>());
    sort(neg.begin(),neg.end());

    ll pcnt = 0;
    ll ncnt = 0;
    ll rncnt = neg.size() - 1;
    while(ans.size() < k) {
        
        // +が0で-が1又は2～の場合
        if(pos.size() - pcnt == 0) {
            if(neg.size() - ncnt == 1) {
                ans.push_back(neg[ncnt]);
                ncnt++;
            } else {
                if((k-ans.size())%2 == 1){
                    while(ans.size() <= k ){
                        ans.push_back(neg[rncnt]);
                        rncnt--;
                    }
                } else {
                    ans.push_back(neg[ncnt]);
                    ans.push_back(neg[ncnt+1]);
                    ncnt += 2;
                }
            }
            continue;
        }

        // +が1で-が0,1,2~の場合
        if(pos.size() - pcnt == 1) {
            if(neg.size() - ncnt == 0 || neg.size() - ncnt == 1) {
               ans.push_back(pos[pcnt]);
               pcnt++; 
            } else {
                if((k-ans.size())%2 == 1){
                    ans.push_back(pos[pcnt]);
                     pcnt++;  
                } else {
                    ans.push_back(neg[ncnt]);
                    ans.push_back(neg[ncnt+1]);
                    ncnt += 2;                    
                }
            }
            continue;
        }
        // +が2～で-が0,1の場合
        if(pos.size() - pcnt >= 2) {
            if(neg.size() - ncnt == 0 || neg.size() - ncnt == 1) {
               ans.push_back(pos[pcnt]);
               pcnt++; 
            } else {
                if(pos[pcnt]*pos[pcnt+1] >= neg[ncnt]*neg[ncnt+1] 
                   || ans.size() == (k-1)) {
                       ans.push_back(pos[pcnt]);
                       pcnt++;
                       } else {
                           ans.push_back(neg[ncnt]);
                           ans.push_back(neg[ncnt+1]);
                           ncnt += 2;
                           }
                    }
            continue;
        }
    }

    for(int i = 0; i < k; ++i) {
        answer = (answer * mod(ans[i],MOD)) % MOD;
        //answer *= ans[i];
    }

    cout << answer << endl;
    return 0;
}