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;

public class AtCoderA
{
    static void Main()
    {
        var sc = new Scanner();

        int n = sc.NextInt();
        int k = sc.NextInt();
        long[] a = sc.LongArray();

        long totalNegaCount = a.Where(num => num < 0).Count();
        if (totalNegaCount == n && k % 2 == 1)
        {
            a = a.OrderBy(num => Math.Abs(num)).ToArray();

            ModInt negaAnswer = 1;
            for (int i = 0; i < k; i++)
            {
                negaAnswer *= a[i];
            }
            Console.WriteLine(negaAnswer);
            return;
        }

        if (k == n)
        {
            ModInt tmpAnswer = 1;
            foreach (var num in a)
            {
                tmpAnswer *= num;
            }
            Console.WriteLine(tmpAnswer);
            return;
        }

        long[] posi = a.Where(num => num >= 0).OrderByDescending(num => Math.Abs(num)).ToArray();
        long[] nega = a.Where(num => num < 0).OrderByDescending(num => Math.Abs(num)).ToArray();

        ModInt answer = 1;

        if (k % 2 == 1)
        {
            answer *= posi[0];
            posi = posi.Where((num, index) => index != 0).ToArray();
            k--;
        }

        long posiIndex = 0;
        long negaIndex = 0;

        while (k > 0)
        {
            long nextP = -1;
            long nextN = -1;
            if (posi.Count() > posiIndex + 1)
            {
                nextP = posi[posiIndex] * posi[posiIndex + 1];
            }
            if (nega.Count() > negaIndex + 1)
            {
                nextN = nega[negaIndex] * nega[negaIndex + 1];
            }
            if (nextP >= nextN)
            {
                answer *= posi[posiIndex];
                answer *= posi[posiIndex + 1];
                posiIndex += 2;
            }
            else
            {
                answer *= nega[negaIndex];
                answer *= nega[negaIndex + 1];
                negaIndex += 2;
            }
            k -= 2;
        }

        Console.WriteLine(answer);
    }

    // ModInt
    class ModInt
    {
        public const long Mod = (int)1e9 + 7;

        public long num;

        public ModInt(long n) { num = n; }

        public override string ToString() { return num.ToString(); }
        public static ModInt operator +(ModInt l, ModInt r) { l.num += r.num; if (l.num >= Mod) l.num -= Mod; return l; }
        public static ModInt operator -(ModInt l, ModInt r) { l.num -= r.num; if (l.num < 0) l.num += Mod; return l; }
        public static ModInt operator *(ModInt l, ModInt r) { return new ModInt(l.num * r.num % Mod); }
        public static implicit operator ModInt(long n) { n %= Mod; if (n < 0) n += Mod; return new ModInt(n); }

        public static ModInt Pow(ModInt v, long k) { return Pow(v.num, k); }

        public static ModInt Pow(long v, long k)
        {
            long ret = 1;
            for (k %= Mod - 1; k > 0; k >>= 1, v = v * v % Mod)
                if ((k & 1) == 1) ret = ret * v % Mod;
            return new ModInt(ret);
        }

        public static ModInt Inverse(ModInt v) { return Pow(v, Mod - 2); }
    }

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            while (_index >= _line.Length)
            {
                _line = Console.ReadLine().Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            _line = Console.ReadLine().Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}