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 Kotlin (1.3.71) (judge verdict: Accepted)

import kotlin.math.abs

fun main() = Thread(null, ::abc173e, "(^o^)", 128 * 1024 * 1024).apply { start() }.join()

@OptIn(ExperimentalStdlibApi::class)
fun abc173e() {
    val (n, k) = readLine()!!.split(' ').map { it.toInt() }
    val aList = readLine()!!.split(' ').map { it.toLong() }

    if (k == 1) return println(aList.max()!!)
    if (n == k) return println(aList.fold(Mint(1), Mint::times))

    val sortedByAbsDesc = aList.sortedByDescending { abs(it) }
    val take = sortedByAbsDesc.take(k)
    val (takePos, takeNeg) = take.partition { it >= 0 }

    if (take.contains(0)) return println(0)

    val rem = sortedByAbsDesc.drop(k)
    val (remPos, remNeg) = rem.partition { it >= 0 }

    val fold = take.fold(Mint(1), Mint::times)

    val answer = if (take.count { it < 0 } % 2 == 0) {
        fold
    } else {
        val takeHavePos = take.count { it >= 0 } > 0
        val takeHaveNeg = take.count { it < 0 } > 0
        val remAHavePos = rem.count { it >= 0 } > 0
        val remAHaveNeg = rem.count { it < 0 } > 0
        if (takeHavePos && takeHaveNeg && remAHavePos && remAHaveNeg) {
            if (takeNeg.max()!! * remNeg.min()!! > takePos.min()!! * remPos.max()!!) {
                fold / takePos.min()!! * remNeg.min()!!
            } else {
                fold / takeNeg.max()!! * remPos.max()!!
            }
        } else if (takeHavePos && remAHaveNeg) {
            fold / takePos.min()!! * remNeg.min()!!
        } else if (takeHaveNeg && remAHavePos) {
            fold / takeNeg.max()!! * remPos.max()!!
        } else {
            val takeLast = sortedByAbsDesc.filter { it < 0 }.takeLast(k)
            if (takeLast.size == k) takeLast.fold(Mint(1), Mint::times) else Mint(0)
        }
    }

    println(answer)
}

private const val MOD = 1_000_000_007L
private fun Long.toMint() = Mint(this)
private fun Int.toMint() = Mint(this.toLong())

@Suppress("unused")
private class Mint(value: Long = 0L) {
    constructor(other: Mint) : this(other.value)

    private val value = if (value < 0) (value % MOD) + MOD else value % MOD
    operator fun unaryPlus() = this
    operator fun unaryMinus() = Mint(MOD - this.value)
    operator fun inc() = this.plus(1)
    operator fun dec() = this.minus(1)
    operator fun plus(other: Long) = Mint(this.value + other)
    operator fun plus(other: Mint) = this.plus(other.value)
    operator fun minus(other: Long) = Mint(this.value - other)
    operator fun minus(other: Mint) = this.plus(-other)
    operator fun times(other: Long) = Mint(this.value * other)
    operator fun times(other: Mint) = this.times(other.value)
    operator fun div(other: Long) = this.times(modPow(other, MOD - 2))
    operator fun div(other: Mint) = this.div(other.value)
    fun mod(mod: Long) = Mint(this.value % mod)
    fun modPow(other: Long) = Mint(modPow(this.value, other))
    fun toLong() = this.value
    override fun toString() = this.value.toString()
    override fun hashCode() = value.hashCode()
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false
        other as Mint
        return value == other.value
    }

    companion object {
        private fun modPow(n: Long, p: Long): Long {
            var x = n % MOD
            var y = p
            var result = 1L
            while (y > 0) {
                if (y % 2 == 1L) result = (result * x) % MOD
                y = y shr 1
                x = (x * x) % MOD
            }
            return result
        }
    }
}