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# (Mono-csc 3.5.0) (judge verdict: Runtime Error)

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

namespace Contest
{
  class Program
  {
    static void Main(string[] args)
    {
      ABC173.E();
    }

    class ABC173
    {
      public static void A()
      {
        var N = Read.Int();
        Console.WriteLine((20000 - N) % 1000);
      }
      public static void B()
      {
        var N = Read.Int();
        var S = Enumerable.Range(0, N).Select(i => Read.Str()).ToArray();
        var freq = Util.Frequency(S);
        Console.WriteLine("AC x {0}", freq.ContainsKey("AC") ? freq["AC"] : 0);
        Console.WriteLine("WA x {0}", freq.ContainsKey("WA") ? freq["WA"] : 0);
        Console.WriteLine("TLE x {0}", freq.ContainsKey("TLE") ? freq["TLE"] : 0);
        Console.WriteLine("RE x {0}", freq.ContainsKey("RE") ? freq["RE"] : 0);
      }
      public static void C()
      {
        var H = Read.Int();
        var W = Read.Int();
        var K = Read.Int();
        var C = Enumerable.Range(0, H).Select(i => Read.Str().Select(c => c == '#').ToArray()).ToArray();
        int count = 0;
        for(int i = 0; i < (1 << H); ++i)
        {
          for(int j = 0; j < (1 << W); ++j)
          {
            int x = 0;
            for(int k = 0; k < H; ++k)
            {
              for(int l = 0; l < W; ++l)
              {
                if(C[k][l] && ((1<<k) & i)>0 && ((1 << l) & j) > 0)
                {
                  x++;
                }
              }
            }
            if (K == x) { count++; }
          }
        }
        Console.WriteLine(count);
      }
      public static void D()
      {
        var N = Read.Int();
        var A = Read.LongN().OrderBy(x => -x).ToArray();
        Console.WriteLine(A.Sum() - A.Min());
      }
      public static void E()
      {
        var N = Read.Int();
        var K = Read.Int();
        var A = Read.LongN();
        var A_Pos = new List<long>();
        var A_Neg = new List<long>();
        foreach(var a in A)
        {
          if (a > 0) { A_Pos.Add(a); }
          if (a < 0) { A_Neg.Add(a); }
        }
        var C_Zero = N - A_Pos.Count - A_Neg.Count;
        var C_Pos = A_Pos.Count;
        var C_Neg = A_Neg.Count;
        if (C_Pos + C_Neg < K) { Console.WriteLine(0); return; }
        if (K - Math.Min(K / 2 * 2, C_Neg / 2 * 2) <= C_Pos)
        {
          A_Pos = A_Pos.OrderBy(x => -x).ToList();
          A_Neg = A_Neg.OrderBy(x => x).ToList();
          var A_Cand = new List<long>();
          foreach (var a in A_Pos)
          {
            A_Cand.Add(a);
          }
          foreach (var a in A_Neg)
          {
            A_Cand.Add(Math.Abs(a));
          }
          A_Cand = A_Cand.OrderBy(x => -x).ToList();
          var Kth = A_Cand[K - 1];
          var NegK = A_Neg.Count(x => Math.Abs(x) >= Kth);
          if (NegK % 2 == 0)
          {
            MOD ans = 1;
            for(int i = 0; i < K; ++i)
            {
              ans *= A_Cand[i];
            }
            Console.WriteLine(ans);
          }
          else
          {
            var Neg2 = NegK == C_Neg ? 0 : A_Neg[NegK - 1] * A_Neg[NegK];
            var Pos2 = C_Pos <= K - NegK ? 0 : A_Pos[K - NegK - 1] * A_Pos[K - NegK];
            MOD ans = 1;
            for(int i = 0; i < NegK - 1; ++i)
            {
              ans *= Math.Abs(A_Neg[i]);
            }
            for(int i = 0; i < K - NegK - 1; ++i)
            {
              ans *= A_Pos[i];
            }
            if(Pos2 > Neg2)
            {
              ans *= Pos2 % MOD.P;
            }
            else
            {
              ans *= Neg2 % MOD.P;
            }
            Console.WriteLine(ans.V);
          }
        }
        else
        {
          if (C_Zero > 0) { Console.WriteLine(0); return; }
          if (C_Pos > 0)
          {
            MOD ans = 1;
            foreach(var a in A)
            {
              ans *= Math.Abs(a);
            }
            ans = 0 - ans;
            Console.WriteLine(ans.V);
          }
          else
          {
            A_Neg = A_Neg.OrderBy(x => -x).ToList();
            MOD ans = 1;
            for (int i = 0; i < K; ++i)
            {
              ans *= Math.Abs(A_Neg[i]);
            }
            ans = 0 - ans;
            Console.WriteLine(ans.V);
          }
        }
      }
      public static void F()
      {

      }
    }

    class Util
    {
      public static T[,] Gen<T>(int r, int c, T v)
      {
        var matrix = new T[r, c];
        for (int i = 0; i < r; ++i)
        {
          for (int j = 0; j < c; ++j)
          {
            matrix[i, j] = v;
          }
        }
        return matrix;
      }
      public static long[] ASum(long[] array)
      {
        var res = new long[array.Length + 1];
        for (int i = 0; i < array.Length; ++i)
        {
          res[i + 1] = res[i] + array[i];
        }
        return res;
      }
      public static long[,] ASum(long[,] matrix)
      {
        var N = matrix.GetLength(0);
        var M = matrix.GetLength(1);
        var res = new long[N + 1, M + 1];
        for(int i = 0; i < N; ++i)
        {
          for(int j = 0; j < M; ++j)
          {
            res[i + 1, j + 1] = res[i, j + 1] + res[i + 1, j] - res[i, j] + matrix[i, j];
          }
        }
        return res;
      }
      public static Dictionary<T, long> Frequency<T>(T[] array)
      {
        var dict = new Dictionary<T, long>();
        foreach (var a in array)
        {
          if (!dict.ContainsKey(a)) { dict.Add(a, 0); }
          dict[a]++;
        }
        return dict;
      }

    }
    struct MOD
    {
      public static long P = 1000000007;
      public readonly long V;

      public MOD(long a) { this.V = a % P; }
      public static implicit operator MOD(long a) { return new MOD(a); }
      public static MOD operator +(MOD a, MOD b) { return new MOD((a.V + b.V) % P); }
      public static MOD operator -(MOD a, MOD b) { return new MOD((a.V - b.V + P) % P); }
      public static MOD operator *(MOD a, MOD b) { return new MOD((a.V * b.V) % P); }
      public static MOD operator /(MOD a, MOD b) { return new MOD((a.V * b.Inverse().V) % P); }
      public MOD Inverse()
      {
        long a = V, p = P; long b = p, c = 1, d = 0, e = 0, f = 0;
        while (b != 0) { e = a / b; f = b; b = a - e * b; a = f; f = d; d = c - e * d; c = f; }
        c %= p;
        return c + (c < 0 ? p : 0);
      }
      public MOD Power(long n)
      {
        long a = V, p = P; long res = 1; long mult = a;
        for (int i = 0; i < 63; ++i) { if ((n & ((long)1 << i)) > 0) { res = (res * mult) % p; } mult = (mult * mult) % p; }
        return res;
      }
      public override string ToString() { return V.ToString(); }
    }


    static class Read
{
static Queue<string> queue = new Queue<string>();
static void Enqueue() { var ss = Console.ReadLine().Split(' '); foreach (var s in ss) { queue.Enqueue(s); } }
public static int Int() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt32(queue.Dequeue()); }
public static long Long() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt64(queue.Dequeue()); }
public static string Str() { if (queue.Count == 0) { Enqueue(); } return queue.Dequeue(); }
public static int[] IntN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToArray(); }
public static long[] LongN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt64(s)).ToArray(); }
public static ulong[] ULongN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToUInt64(s)).ToArray(); }
public static string[] StrN() { return Console.ReadLine().Split(' '); }
public static Tuple<int, int>[] TupleInt(int n) { List<Tuple<int, int>> list = new List<Tuple<int, int>>(); for (int i = 0; i < n; ++i) { var ab = Read.IntN(); list.Add(new Tuple<int, int>(ab[0], ab[1])); } return list.ToArray(); }
public static Tuple<long, long>[] TupleLong(int n) { List<Tuple<long, long>> list = new List<Tuple<long, long>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<long, long>(ab[0], ab[1])); } return list.ToArray(); }
public static Tuple<string, string>[] TupleStr(int n) { List<Tuple<string, string>> list = new List<Tuple<string, string>>(); for (int i = 0; i < n; ++i) { var ab = Read.StrN(); list.Add(new Tuple<string, string>(ab[0], ab[1])); } return list.ToArray(); }
public static Tuple<double, double>[] TupleDouble(int n) { List<Tuple<double, double>> list = new List<Tuple<double, double>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<double, double>(ab[0], ab[1])); } return list.ToArray(); }
public static long[,] LongMatrix(int r, int c) { var mat = new long[r, c]; for (int i = 0; i < r; ++i) { var x = Read.LongN(); for (int j = 0; j < c; ++j) { mat[i, j] = x[j]; } } return mat; }
public static Dictionary<int, HashSet<int>> AdjacencyList(int N, int M, bool isDirectional = false)
{
Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>();
for (int i = 0; i <= N; ++i) { dict.Add(i, new HashSet<int>()); }
        for (int i = 0; i < M; ++i) { var ab = Read.IntN(); if (!dict[ab[0]].Contains(ab[1])) { dict[ab[0]].Add(ab[1]); } if (!isDirectional && !dict[ab[1]].Contains(ab[0])) { dict[ab[1]].Add(ab[0]); } }
        return dict;
}
public static Dictionary<int, Dictionary<int, long>> AdjacencyListWithWeight(int N, int M)
{
Dictionary<int, Dictionary<int, long>> dict = new Dictionary<int, Dictionary<int, long>>(); for (int i = 0; i <= N; ++i) { dict.Add(i, new Dictionary<int, long>()); }
for (int i = 0; i < M; ++i) { var ab = Read.IntN(); dict[ab[0]].Add(ab[1], ab[2]); dict[ab[1]].Add(ab[0], ab[2]); }
return dict;
}
}
    }
}