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# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Console;
using static System.Math;

namespace AtTest.ABC_173
{
    class E
    {
        static void Main(string[] args)
        {
            var sw = new System.IO.StreamWriter(OpenStandardOutput()) { AutoFlush = false };
            SetOut(sw);

            Method(args);

            Out.Flush();
        }

        static void Method(string[] args)
        {
            int[] nk = ReadInts();
            int n = nk[0];
            int k = nk[1];
            long[] array = ReadLongs();

            List<long> pluses = new List<long>();
            List<long> minuses = new List<long>();
            int zeroCnt = 0;
            for (int i = 0; i < n; i++)
            {
                if (array[i] >= 0)
                {
                    pluses.Add(array[i]);
                } 
                else
                {
                    minuses.Add(array[i]);
                }
            }
            pluses.Sort();
            pluses.Reverse();
            minuses.Sort();

            long mask = 1000000000 + 7;

            if (n == k)
            {
                long res = 1;
                for(int i = 0; i < n; i++)
                {
                    res *= (array[i] + mask) % mask;
                    res %= mask;
                }
                WriteLine(res);
                return;
            }

            if (minuses.Count == n && k % 2 == 1)
            {// Minus
                long res = 1;
                for(int i = 0; i <k; i++)
                {
                    res *= (mask + minuses[minuses.Count - i - 1]) % mask;
                    res %= mask;
                }
                WriteLine(res);
                return;
            }
            else
            {
                long res = 1;
                int pI = 0;
                int mI = 0;
                int remain = k;
                while (remain > 0)
                {
                    if (remain >= 2)
                    {
                        if (mI < minuses.Count - 1)
                        {
                            if (pI < pluses.Count - 1)
                            {
                                if (minuses[mI] * minuses[mI + 1] 
                                    >= pluses[pI] * pluses[pI + 1])
                                {
                                    res *= (minuses[mI] * minuses[mI + 1]) % mask;
                                    res %= mask;
                                    mI += 2;
                                    remain -= 2;
                                }
                                else
                                {
                                    res *= pluses[pI];
                                    res %= mask;
                                    pI++;
                                    remain--;
                                }
                            }
                            else
                            {
                                res *= (minuses[mI] * minuses[mI + 1]) % mask;
                                res %= mask;
                                mI += 2;
                                remain -= 2;
                            }
                        }
                        else
                        {
                            res *= pluses[pI];
                            pI++;
                            remain--;
                        }
                    }
                    else
                    {
                        res *= pluses[pI];
                        remain--;
                    }
                    res %= mask;
                }

                WriteLine(res);
            }
        }

        private static string Read() { return ReadLine(); }
        private static char[] ReadChars() { return Array.ConvertAll(Read().Split(), a => a[0]); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }
    }
}