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.IO;
using System.Linq;
using System.Numerics;
using static System.Math;

namespace CSharpSpiral {
	class Program {
		public static void Main(string[] args) {
			var sw = new StreamWriter(Console.OpenStandardOutput()) {
				AutoFlush=
#if DEBUG
			true
#else
			false
#endif
			};
			Console.SetOut(sw);
			new Program().Solve(new ConsoleInput(
#if DEBUG
			new StreamReader("../../../inputData.txt")
#else
			Console.In
#endif
			, ' '));
			Console.Out.Flush();
		}
		public void Solve(ConsoleInput cin) {
			var datum = cin.ReadIntArray(2);
			var N = datum[0];
			var K = datum[1];
			var A = cin.ReadLongArray(N).ToList();
			var C = Comparer<long>.Create((i, j) => Abs(j).CompareTo(Abs(i)));
			A.MergeSort(C);
			var Posis = new List<long>();
			var Negas = new List<long>();
			foreach(var a in A) {
				if(a>0) Posis.Add(a);
				else if(a<0) Negas.Add(a);
			}
			var posic = Posis.Count();
			var negac = Negas.Count();
			var hasZero = posic+negac<N;
			var freedom = Min(negac+posic-K, Min(negac, posic));
			int? GainCorner() {
				if(posic+negac<K) return 0;

				if(posic+negac==K) {
					//Product All
					if(negac%2==0) return Prod(Posis.Concat(Negas));
					else return hasZero ? 0 : Prod(Posis.Concat(Negas));
				}
				//#NonZero>K
				if(posic==0) {
					if(K%2==0) return Prod(Negas.Take(K));
					else {
						return hasZero ? 0 : Prod(Negas.TakeLast(K));
					}
				}
				if(negac==0) return Prod(Posis.Take(K));

				//for the case freedom==1
				if(posic==1) {
					var posi = Posis.First();
					if((K-1)%2==0) return Prod(Negas.Take(K-1).Append(posi));
					else {
						return Prod(Negas.Take(K));
					}
				}
				if(negac==1) { 
					//posic>=K
					return Prod(Posis); 
				}
				if(posic+negac-K==1) {
					if(negac%2==0) {
						return Prod(Negas.Concat(Posis.Take(K-negac)));
					} else {
						return Prod(Negas.SkipLast(1).Concat(Posis.Take(K-negac-1)));
					}
				}

				return null;
			}
			//posic>=2 && posic+negac>=K+2
			int GainRegular() {
				var negaTaken = (K-posic+1)/2*2;  //Now Minimum
				if(negaTaken<0) negaTaken=0;
				var posiTaken = K-negaTaken;
				while(negaTaken+2<=negac && posiTaken-2>=0) {
					var lastPosi = Posis[posiTaken-2]*Posis[posiTaken-1];
					var nextNega = Negas[negaTaken]*Negas[negaTaken+1];
					if(nextNega>lastPosi) {
						negaTaken+=2;
						posiTaken-=2;
					} else break;
				}
				return Prod(Negas.Take(negaTaken).Concat(Posis.Take(posiTaken)));
			}
			var res = freedom<2 ? GainCorner() : GainRegular();
			Console.WriteLine(res);
		}


		public int Prod(long l) {
			var modulo = 1_000_000_007;
			l%=modulo;
			return (int)((l+modulo)%modulo);
		}
		public int Prod(IEnumerable<long> I) {
			var modulo = 1_000_000_007;
			var res = 1L;
			foreach(var i in I) {
				res*=i;
				res%=modulo;
			}
			return (int)(res+modulo)%modulo;
		}
	}

	static class ExtendClass {
		public static string Join<T>(this IEnumerable<T> X, string separator = " ")
			=> string.Join(separator, X);

		#region MergeSort
		public static void MergeSort<T>(this List<T> A, bool desending) where T : IComparable<T> {
			if(desending)
				MergeSort(A, Comparer<T>.Create((x, y) => y.CompareTo(x)));
			else
				MergeSort(A, Comparer<T>.Create((x, y) => x.CompareTo(y)));
		}
		public static void MergeSort<T>(this List<T> A, IComparer<T> comparer) where T : IComparable<T> {
			MergeSort1(0, A.Count(), A, comparer);
		}
		private static void Merge<T>(int left, int mid, int right, List<T> A, IComparer<T> comparer)
			where T : IComparable<T> {
			var nl = mid-left;
			var nr = right-mid;
			var L = new T[nl];
			var R = new T[nr];
			for(int i = 0; i<nl; i++)
				L[i]=A[i+left];
			for(int i = 0; i<nr; i++)
				R[i]=A[i+mid];
			int il = 0, ir = 0;
			for(int k = left; k<right; k++) {
				if(comparer.Compare(L[il], R[ir])<=0) {
					A[k]=L[il++];
					if(il==nl) {
						k++;
						for(; ir<nr;)
							A[k++]=R[ir++];
						break;
					}
				} else {
					A[k]=R[ir++];
					if(ir==nr) {
						k++;
						for(; il<nl;)
							A[k++]=L[il++];
						break;
					}
				}
			}
		}
		private static void MergeSort1<T>(int left, int right, List<T> A, IComparer<T> comparer)
		where T : IComparable<T> {
			if(left+1<right) {
				var mid = (left+right)/2;
				MergeSort1(left, mid, A, comparer);
				MergeSort1(mid, right, A, comparer);
				Merge(left, mid, right, A, comparer);
			}
		}

		#endregion
	}
	class ConsoleInput {
		private readonly TextReader _stream;
		private readonly char _separator = ' ';
		private readonly Queue<string> inputStream;
		public ConsoleInput(TextReader stream, char separator = ' ') {
			this._separator=separator;
			this._stream=stream;
			inputStream=new Queue<string>();
		}
		public string Read {
			get {
				if(inputStream.Count!=0) return inputStream.Dequeue();
				string[] tmp = _stream.ReadLine().Split(_separator);
				for(int i = 0; i<tmp.Length; ++i)
					inputStream.Enqueue(tmp[i]);
				return inputStream.Dequeue();
			}
		}
		public string ReadLine { get { return _stream.ReadLine(); } }
		public int ReadInt { get { return int.Parse(Read); } }
		public long ReadLong { get { return long.Parse(Read); } }
		public double ReadDouble { get { return double.Parse(Read); } }
		public string[] ReadStrArray(long N) {
			var ret = new string[N];
			for(long i = 0; i<N; ++i)
				ret[i]=Read;
			return ret;
		}
		public int[] ReadIntArray(long N) { var ret = new int[N]; for(long i = 0; i<N; ++i) ret[i]=ReadInt; return ret; }
		public long[] ReadLongArray(long N) { var ret = new long[N]; for(long i = 0; i<N; ++i) ret[i]=ReadLong; return ret; }
		public double[] ReadDoubleArray(long N) { var ret = new double[N]; for(long i = 0; i<N; ++i) ret[i]=ReadDouble; return ret; }
		public bool isEOF {
			get { return _stream.Peek()==-1; }
		}
	}
}