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.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using static System.Console;
using static System.Math;
 
public class Solve{
    static public int mod = 1000000007;
    static public string al = "abcdefghijklmnopqrstuvwxyz";
    public static void Main(){
        // •ûj
        //
        var tn = inta();
        var n = tn[0];
        var choose = tn[1];
        var a = longa();
        var plus = new List<long>();
        var minus = new List<long>();
        var zero = 0;
        for(int i=0;i<n;i++){
            if(a[i] > 0){
                plus.Add(a[i]);
            }else if(a[i] < 0){
                minus.Add(a[i]);
            }else{
                zero++;
            }
        }
        plus.Sort();
        plus.Reverse();
        minus.Sort();
        if(plus.Count == 0){
            if(choose %2 ==1){
                if(zero > 0){
                    WriteLine(0);
                    return;
                }else{
                    minus.Reverse();
                    long ans = 1;
                    for(int i=0;i<choose;i++){
                        ans = (ans * Abs(minus[i])) %mod;
                    }
                    ans *= -1;
                    while(ans < 0){
                        ans += mod;
                    }
                    WriteLine(ans);
                    return;
                }
            }else{
                if(choose <= minus.Count){
                    long ans = 1;
                    for(int i=0;i<choose;i++){
                        ans = (ans * Abs(minus[i])) %mod;
                    }
                    WriteLine(ans);
                    return;
                }else{
                    WriteLine(0);
                    return;
                }
            }
        }
        if(choose > plus.Count+minus.Count){
            WriteLine(0);
            return;
        }
        if(choose == 1){
            if(plus.Count > 0){
                WriteLine(plus[0]);
                return;
            }else if(zero > 0){
                WriteLine(0);
                return;
            }else{
                WriteLine(minus[minus.Count-1]);
                return;
            }
        }
        var anslist = new List<long>();
        if(choose <= plus.Count){
            for(int i=0;i<choose;i++){
                anslist.Add(plus[i]);
            }
            var anslistminus = new List<long>();
            for(int i=anslist.Count-1;i>=1;i-=2){
                if(minus.Count < 2) break;
                long tempp = anslist[i]*anslist[i-1];
                long tempm = minus[0]*minus[1];
                if(tempm > tempp){
                    anslist.RemoveAt(i);
                    anslist.RemoveAt(i-1);
                    anslistminus.Add(minus[0]);
                    anslistminus.Add(minus[1]);
                    minus.RemoveAt(1);
                    minus.RemoveAt(0);
                }else{
                    break;
                }
            }
            long ans = 1;
            for(int i=0;i<anslist.Count;i++){
                ans = (ans * anslist[i]) %mod;
            }
            for(int i=0;i<anslistminus.Count;i++){
                ans = (ans * Abs(anslistminus[i])) %mod;
            }
            WriteLine(ans);
            return;
        }else{
            if(choose %2 == 1){
                anslist.Add(plus[0]);
                for(int i=1;i<plus.Count-1;i+=2){
                    anslist.Add(plus[i]);
                    anslist.Add(plus[i+1]);
                }
            }else{
                for(int i=0;i<plus.Count-1;i+=2){
                    anslist.Add(plus[i]);
                    anslist.Add(plus[i+1]);
                }
            }
            var anslistminus = new List<long>();
            for(int i=anslist.Count-1;i>=1;i-=2){
                if(minus.Count < 2) break;
                long tempp = anslist[i] * anslist[i-1];
                long tempm = minus[0] * minus[1];
                if(tempp < tempm){
                    anslist.RemoveAt(i);
                    anslist.RemoveAt(i-1);
                    anslistminus.Add(minus[0]);
                    anslistminus.Add(minus[1]);
                    minus.RemoveAt(1);
                    minus.RemoveAt(0);
                }else{
                    break;
                }
            }
            long ans = 1;
            for(int i=0;i<anslist.Count;i++){
                ans = (ans * anslist[i]) %mod;
            }
            for(int i=0;i<anslistminus.Count;i++){
                ans = (ans * Abs(anslistminus[i])) %mod;
            }
            WriteLine(ans);
            return;
        }
        
	
	
    }
    public static void swap(ref int a,ref int b){int temp = a;a= b;b = temp;}
    static void charswap(ref char a,ref char b){char temp = a;a= b;b = temp;}
    static int ncr(int n,int r){if(n<r)return 0;r = Min(r,n-r);long nn = 1;for(int i=n-r+1;i<=n;i++){nn = nn*i%mod;}long rr = 1;for(int i=1;i<=r;i++){rr = rr*i%mod;}rr = square((int)rr,mod-2);nn = nn * rr %mod;return (int)nn;}
    // a^b mod
    static int square(int a,int b){string binary = Convert.ToString(b,2);int bileng = binary.Length;long a_power = a;long value = 1;for(int i=bileng-1;i>=0;i--){if(binary[i] == '1'){value = value*a_power%mod;}a_power = a_power*a_power%mod;}return (int)value;}
    static int square2(int a,int b){long output = 1;var list = new List<long>();int sh = 1;long n = a;list.Add(a);while(sh < b){sh *= 2;n = n*n%mod;list.Add(n);}for(int i=list.Count-1;i>=0;i--){if(b > sh){b -= sh;sh /= 2;output = output*list[i]%mod;}}return (int)output;}
    //Šeí“Çæ
    static string rstr(){ return ReadLine(); }
    static int rint(){ return int.Parse(ReadLine()); }
    static long rlong(){ return long.Parse(ReadLine()); }
    static string[] stra(){ return ReadLine().Split(' '); }
    static char[] chara(){ string[] a=stra();string b="";for(int i=0;i<a.Length;i++){b+=a[i];}return b.ToCharArray();}
    static int[] inta(){ string[] read_str_array = ReadLine().Split(' '); int[] return_int_array = new int[read_str_array.Length]; for(int countup_i=0;countup_i<read_str_array.Length;countup_i++){ return_int_array[countup_i] = int.Parse(read_str_array[countup_i]); } return return_int_array; }
    static int[,] inta2(int num_array,int in_array){ int[,] int_array2 = new int[num_array,in_array]; for(int i=0;i<num_array;i++){ int[] temp_array = inta(); for(int j=0;j<in_array;j++){ int_array2[i,j] = temp_array[j]; } } return int_array2; }
    static long[] longa(){ string[] read_str_array = ReadLine().Split(' '); long[] return_long_array = new long[read_str_array.Length]; for(long countup_i=0;countup_i<read_str_array.Length;countup_i++){ return_long_array[countup_i] = long.Parse(read_str_array[countup_i]); } return return_long_array; }
    static double[] doublea(){ string[] read_str_array = ReadLine().Split(' '); double[] return_double_array = new double[read_str_array.Length]; for(long countup_i=0;countup_i<read_str_array.Length;countup_i++){ return_double_array[countup_i] = long.Parse(read_str_array[countup_i]); } return return_double_array; }
    // -----------------------------
    static long divideup(long divided,long divid){ long temp_divide = divided/divid; if(divided % divid > 0){ temp_divide++; } return temp_divide; }
    static long GCD(long a,long b){ if(a < b){ long temp = a; a = b; b = temp; } if(a % b == 0){ return b; } else{ long temp = b; b = a%b; a = temp; return GCD(a,b); } }
    static long LCM(long a,long b){ return a * b / GCD(a,b); }
    static void WriteArray(int[,] a,int b,int c){for(int i=0;i<b;i++){for(int j=0;j<c;j++){if(j!=0) Write(" ");Write(a[i,j]);}WriteLine();}}
}