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: Wrong Answer)

using System;
using System.Linq;
using System.ComponentModel;
using System.Collections.Generic;

namespace ConsoleApp1 {
    public class Class {
        static void Main(string[] args) {
            var (N, K) = ReadValue<int, long>();
            var A = longArray.ToList();
            long ans = 1;
            long mod = 1000000007;

            if (A.Where(x => x > 0).Count() < K) {
                var a = A.OrderByDescending((x) => Math.Abs(x))
                         .ThenByDescending(x => x)
                         .Select((x) => x).ToList();
                while (K > 0) {
                    ans *= a.Last() % mod;
                    ans %= mod;
                    a.RemoveAt(N - 1);
                    N--;
                    K--;
                }
                ans += mod;
            } else {
                A.Sort();
                while (K > 0) {
                    if (A[0] * A[1] >= A[N - 1] + A[N - 2]) {
                        ans *= (A[0] * A[1]) % mod;
                        ans %= mod;
                        A.RemoveAt(0);
                        A.RemoveAt(0);
                    } else {
                        ans *= (A[N - 1] + A[N - 2]) % mod;
                        ans %= mod;
                        A.RemoveAt(N - 1);
                        A.RemoveAt(N - 2);
                    }
                    N -= 2;
                    K -= 2;
                }
                if (K == 1) {
                    ans *= A.Last() % mod;
                    ans %= mod;
                    K--;
                    A.RemoveAt(N - 1);
                    N--;
                }
            }
            Console.Write(ans);
        }

        static int BinarySearch<T>(T[] arr,Func<T,bool> predicate) {
            int min = 0;
            int max = arr.Length - 1;
            while (true) {
                if (min == max) {
                    return max;
                }
                var mid = (min + max) / 2;
                if (predicate(arr[mid])) {
                    min = mid + 1;
                } else {
                    max = mid - 1;
                }
            }
        }

        static T1 ReadValue<T1>() {
            var t1 = TypeDescriptor.GetConverter(typeof(T1));
            return (T1)t1.ConvertFromString(Console.ReadLine());
        }

        static (T1, T2) ReadValue<T1, T2>() {
            var rl = Console.ReadLine().Trim().Split().ToArray();
            var t1 = TypeDescriptor.GetConverter(typeof(T1));
            var t2 = TypeDescriptor.GetConverter(typeof(T2));
            return ((T1)t1.ConvertFromString(rl[0]),
                    (T2)t2.ConvertFromString(rl[1]));
        }

        static (T1, T2, T3) ReadValue<T1, T2, T3>() {
            var rl = Console.ReadLine().Trim().Split().ToArray();
            var t1 = TypeDescriptor.GetConverter(typeof(T1));
            var t2 = TypeDescriptor.GetConverter(typeof(T2));
            var t3 = TypeDescriptor.GetConverter(typeof(T3));
            return ((T1)t1.ConvertFromString(rl[0]),
                    (T2)t2.ConvertFromString(rl[1]),
                    (T3)t3.ConvertFromString(rl[2]));
        }

        static (T1, T2, T3, T4) ReadValue<T1, T2, T3, T4>() {
            var rl = Console.ReadLine().Trim().Split().ToArray();
            var t1 = TypeDescriptor.GetConverter(typeof(T1));
            var t2 = TypeDescriptor.GetConverter(typeof(T2));
            var t3 = TypeDescriptor.GetConverter(typeof(T3));
            var t4 = TypeDescriptor.GetConverter(typeof(T4));
            return ((T1)t1.ConvertFromString(rl[0]),
                    (T2)t2.ConvertFromString(rl[1]),
                    (T3)t3.ConvertFromString(rl[2]),
                    (T4)t4.ConvertFromString(rl[3]));
        }

        static (T1, T2, T3, T4,T5) ReadValue<T1, T2, T3, T4,T5>() {
            var rl = Console.ReadLine().Trim().Split().ToArray();
            var t1 = TypeDescriptor.GetConverter(typeof(T1));
            var t2 = TypeDescriptor.GetConverter(typeof(T2));
            var t3 = TypeDescriptor.GetConverter(typeof(T3));
            var t4 = TypeDescriptor.GetConverter(typeof(T4));
            var t5 = TypeDescriptor.GetConverter(typeof(T5));
            return ((T1)t1.ConvertFromString(rl[0]),
                    (T2)t2.ConvertFromString(rl[1]),
                    (T3)t3.ConvertFromString(rl[2]),
                    (T4)t4.ConvertFromString(rl[3]),
                    (T5)t5.ConvertFromString(rl[4]));
        }

        static string[] stringArray => Console.ReadLine().Split().ToArray();
        static int[] intArray => Console.ReadLine().Trim().Split().Select(int.Parse).ToArray();
        static long[] longArray => Console.ReadLine().Trim().Split().Select(long.Parse).ToArray();
        static double[] doubleArray => Console.ReadLine().Trim().Split().Select(double.Parse).ToArray();
        static decimal[] decimalArray => Console.ReadLine().Trim().Split().Select(decimal.Parse).ToArray();
    }
}