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.Collections;
using System.Collections.Generic;

namespace ABC173_E
{
    using static Ex;
    public class E
    {
        public static void Main(string[] args)
        {
            new ABC_E();
        }
    }

    public class ABC_E
    {
        long mod = (long)1e9 + 7;
        int n, k;
        long ans = 1;
        public ABC_E()
        {
            (n, k) = ReadIntArray().ToTpl2();
            var a = ReadLongArray().OrderByDescending(v => Math.Abs(v));

            int minasMax = k / 2;

            var mi = new List<long>();
            var p = new List<long>();
            var all = new List<(long, bool)>();
            int zero = 0;
            foreach (var v in a)
            {
                if (v == 0) zero++;
                else if (v > 0) p.Add(v);
                else mi.Add(v);

                all.Add((Math.Abs(v), (v < 0)));
            }

            long tmp_mi = 0;
            var ttt = new List<long>();
            for (int i = k, j = 0, mm = 0; i > 0;)
            {
                if (j >= all.Count)
                {
                    if (ttt.Any())
                    {
                        ans *= ttt.Last();
                        ans %= mod;

                        i--;
                        ttt.RemoveAt(ttt.Count - 1);
                        continue;
                    }
                    "a".Cw();
                    return;
                }

                var v = all[j++];
                if (v.Item1 == 0)
                {
                    if (ttt.Any())
                    {
                        ans *= ttt.Last();
                        ans %= mod;

                        i--;
                        ttt.RemoveAt(ttt.Count - 1);
                        continue;
                    }
                    0.Cw();
                    continue;
                }
                if (v.Item2 && v.Item1 == tmp_mi)
                {
                    tmp_mi = 0;
                    continue;
                }

                if (v.Item2)
                {
                    mm++;
                    if (i >= 2 && mm < mi.Count)
                    {
                        tmp_mi = mi[mm++];

                        ans *= -tmp_mi;
                        ans %= mod;
                        ans *= v.Item1;
                        ans %= mod;

                        i -= 2;
                        continue;
                    }
                    else
                    {
                        ttt.Add(-v.Item1);
                    }
                }
                else
                {
                    ans *= v.Item1;
                    ans %= mod;

                    i--;
                    continue;
                }
            }
            ans.Cw();
        }
    }

    public static class Ex
    {
        public static void Cw(this object s) { Console.WriteLine(s); }



        public static bool Mins<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) > 0) { a = b; return true; } return false; }
        public static bool Maxs<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; }
        public static void Swap<T>(ref T a, ref T b) { var t = b; b = a; a = t; }
        public static void Swap<T>(List<T> list, int i, int j) { T tmp = list[j]; list[j] = list[i]; list[i] = tmp; }

        public static long Lcm(int x, int y) { return x / Gcd(x, y) * y; }
        public static long Gcd(long m, long n) { if (n == 0) return m; return Gcd(n, m % n); }
        public static List<string> FastSort(this List<string> s) { s.Sort(StringComparer.Ordinal); return s.ToList(); }
        public static void yesno(this bool b) { (b ? "yes" : "no").Cw(); }
        public static void YesNo(this bool b) { (b ? "Yes" : "No").Cw(); }
        public static void YESNO(this bool b) { (b ? "YES" : "NO").Cw(); }

        public static string Read { get { return Console.ReadLine().Trim(); } }
        public static int ReadInt { get { return int.Parse(Read); } }
        public static long ReadLong { get { return long.Parse(Read); } }
        public static double ReadDouble { get { return double.Parse(Read); } }
        public static string[] ReadArray() { return Read.Split(' '); }
        public static int[] ReadIntArray() { return Read.Split(' ').Select(int.Parse).ToArray(); }
        public static long[] ReadLongArray() { return Read.Split(' ').Select(long.Parse).ToArray(); }
        public static string[] ReadArray(long N) { var v = new string[N]; for (long i = 0; i < N; ++i) v[i] = Read; return v; }
        public static int[] ReadIntArray(long N) { var v = new int[N]; for (long i = 0; i < N; ++i) v[i] = ReadInt; return v; }
        public static long[] ReadLongArray(long N) { var v = new long[N]; for (long i = 0; i < N; ++i) v[i] = ReadLong; return v; }

        public static (T, T) ToTpl2<T>(this T[] v) { return (v[0], v[1]); }
        public static (T, T, T) ToTpl3<T>(this T[] v) { return (v[0], v[1], v[2]); }
        public static (T, T, T, T) ToTpl4<T>(this T[] v) { return (v[0], v[1], v[2], v[3]); }
        public static (T, T, T, T, T) ToTpl5<T>(this T[] v) { return (v[0], v[1], v[2], v[3], v[4]); }
    }
}