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 <bits/stdc++.h>
using namespace std;

vector<long long> numbers;
//vector<long long> chosen;

bool cmp(long long num1, long long num2)
{
    if(abs(num1) != abs(num2))
        return abs(num1) > abs(num2);

    else
        return num1 > num2;
}

const long long modConst = 1000000007;
int main()
{
    int N, K;
    scanf("%d%d", &N, &K);

    for(int i = 0; i < N; i++)
    {
        long long Ai;
        scanf("%lld", &Ai);
        numbers.push_back(Ai);
    }
    sort(numbers.begin(), numbers.end(), cmp);

    int sign = 1;
    for(int i = 0; i < K; i++)
    {
        //chosen.push_back(numbers[i]);
        if(numbers[i] < 0)
            sign = (-sign);

        else if(numbers[i] == 0)
            sign = 0;
    }

    if(sign >= 0)
    {
        long long ans = 1;
        for(int i = 0; i < K; i++)
            ans = (ans * numbers[i]) % modConst;

        printf("%lld", ans);
        return 0;
    }

    else
    {
        long long smallestAbsNeg = modConst, smallestAbsPos = modConst, largestAbsPos = -1, largestAbsNeg = -1;
        for(int i = 0; i < K; i++)
        {
            if(numbers[i] < 0)
            {
                smallestAbsNeg = min(smallestAbsNeg, abs(numbers[i]));
                largestAbsNeg = max(largestAbsNeg, abs(numbers[i]));
            }

            else if(numbers[i] > 0)
            {
                smallestAbsPos = min(smallestAbsPos, abs(numbers[i]));
                largestAbsPos = max(largestAbsPos, abs(numbers[i]));
            }
        }

        smallestAbsNeg *= -1;
        largestAbsNeg *= -1;

        //printf("smallestAbsNeg = %lld, smallestAbsPos = %lld, largestAbsPos = %lld, largestAbsNeg = %lld\n", smallestAbsNeg, smallestAbsPos, largestAbsPos, largestAbsNeg);

        if(smallestAbsNeg >= 0 || smallestAbsPos <= 0 || largestAbsPos <= 0 || largestAbsNeg >= 0)
        {
            long long ans = 1;
            for(int i = 0; i < K; i++)
                ans = (ans * ((numbers[numbers.size()  - 1 - i] + modConst) % modConst) ) % modConst;

            printf("%lld", ans);
            return 0;
        }

        else
        {
            long long product1 = largestAbsPos * smallestAbsPos, product2 = largestAbsNeg * smallestAbsNeg;
            if(product1 <= product2)
            {
                bool found = false;
                long long ans = largestAbsPos % modConst;
                for(int i = 0; i < K; i++)
                {
                    if(numbers[i] == smallestAbsNeg && !found)
                    {
                        found = true;
                        continue;
                    }

                    else
                        ans = (ans * numbers[i]) % modConst;
                }

                printf("%lld", ans);
            }

            else
            {
                bool found = false;
                long long ans = ((largestAbsNeg % modConst) + modConst) % modConst;

                for(int i = 0; i < K; i++)
                {
                    if(numbers[i] == smallestAbsPos && !found)
                        found = true;

                    else
                        ans = (ans * numbers[i]) % modConst;
                }

                printf("Case 2: %lld", ans);
            }
        }
    }
}