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++ (Clang 10.0.0) (judge verdict: Wrong Answer)

#include<iostream>
#include<map>
#include<set>
#include<unordered_set>
#include<unordered_map>
#include<vector>
#include<cstring>
#include<sstream>
#include<algorithm>
//#include<bits/stdc++.h>
#include<cstdio>
using namespace std;

#define MOD 1000000007

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, k;
    cin >> n >> k;

    vector<long long> pos1, pos2, neg1, neg2;
    long long tmp;
    for (int i = 0; i < n; i++) {
        cin >> tmp;

        if (tmp < 0) {
            neg1.push_back(tmp);
            neg2.push_back(tmp);
        }
        else {
            pos1.push_back(tmp);
            pos2.push_back(tmp);
        }
    }

    sort(pos1.begin(), pos1.end());
    sort(pos2.rbegin(), pos2.rend());
    sort(neg1.begin(), neg1.end());
    sort(neg2.rbegin(), neg2.rend());

    vector<long long> prod11(pos1.size()), prod12(pos1.size()), prod21(neg1.size()), prod22(neg1.size()); 

    if (pos1.size() > 0) {
        prod11[0] = pos1[0];
        prod12[0] = pos2[0];
    }

    if (neg1.size() > 0) {
        prod21[0] = neg1[0];
        prod22[0] = neg2[0];

        while (prod21[0] < 0) prod21[0] += MOD;
        while (prod22[0] < 0) prod22[0] += MOD;

        prod21[0] %= MOD;
        prod22[0] %= MOD;
    }

    for (int i = 1; i < pos1.size(); i++) {
        prod11[i] = prod11[i-1]*pos1[i];
        prod12[i] = prod12[i-1]*pos2[i];
        prod11[i] %= MOD;
        prod12[i] %= MOD;
    }

    for (int i = 1; i < neg1.size(); i++) {
        if (i%2 == 0) {

            long long tmp1 = neg1[i];
            long long tmp2 = neg2[i];

            while (tmp1 < 0) tmp1 += MOD;
            while (tmp2 < 0) tmp2 += MOD;

            prod21[i] = (prod21[i-1]*tmp1)%MOD;
            prod22[i] = (prod22[i-1]*tmp2)%MOD;
        } else {
            long long tmp1 = ((prod21[i-1] - MOD) * -1)%MOD;
            long long tmp2 = ((prod22[i-1]-MOD)*-1)%MOD;

            prod21[i] = (tmp1*(neg1[i]*-1))%MOD;
            prod22[i] = (tmp2*(neg2[i]*-1))%MOD;
        }
    }

    vector<long long> vals1, vals2;
    for (int i = 0; i <= k; i++) {
        if (neg1.size() < i) break;
        if (pos1.size() < (k-i)) continue;

        long long a = (i == 0) ? 1 : prod21[i-1];
        long long b = (i == 0) ? 1 : prod22[i-1];
        long long c = (i == k) ? 1 : prod11[k-i-1];
        long long d = (i == k) ? 1 : prod12[k-i-1];

        if (a != 1 && i%2 != 0) {
            while (a > 0) a -= MOD;
            while (b > 0) b -= MOD;
        }

        if (i%2 == 0) {
            vals1.push_back(a*c);
            vals1.push_back(a*d);
            vals1.push_back(b*c);
            vals1.push_back(b*d);
        } else {
            vals2.push_back(a*c);
            vals2.push_back(a*d);
            vals2.push_back(b*c);
            vals2.push_back(b*d);
        }
    }

    sort(vals1.begin(), vals1.end());
    sort(vals2.rbegin(), vals2.rend());

    while (vals2.size() > 0 && vals2[0] < 0) vals2[0] += MOD;

    if (vals1.size() > 0) cout << vals1[vals1.size()-1]%MOD << endl;
    else cout << vals2[0]%MOD << endl;

    return 0;
}