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 Rust (1.42.0) (judge verdict: Runtime Error)

#[allow(unused_imports)]
use std::{cmp::Reverse, collections::*};

#[allow(unused_imports)]
use proconio::{
    fastout, input,
    marker::{Chars, Usize1},
};

#[allow(unused_imports)]
use itertools::Itertools;

#[allow(unused_imports)]
use num::Integer;

#[fastout]
pub fn main() {
    input! {
        n: usize,
        k: usize,
        a: [i64; n],
    }
    let negatives: Vec<_> = a.iter().filter(|&&a| a < 0).copied().sorted().collect();
    let positives: Vec<_> = a
        .iter()
        .filter(|&&a| a > 0)
        .copied()
        .sorted()
        .rev()
        .collect();

    if (negatives.len() + positives.len() < k) || (positives.len() == k - 1 && negatives.len() == 1)
    {
        println!("0");
        return;
    }

    if k == 1 {
        println!(
            "{}",
            positives
                .get(0)
                .unwrap_or(&0)
                .max(&negatives.get(0).unwrap_or(&0).abs())
        );
        return;
    }

    if positives.len() == 0 {
        if k % 2 == 0 {
            println!(
                "{}",
                negatives.into_iter().take(k).product::<i64>() % 1000000007
            );
        } else {
            println!(
                "{}",
                (negatives.into_iter().sorted().take(k).product::<i64>() + 1000000007) % 1000000007
            );
        }
    } else if positives.len() < k {
        let mut chosen = positives;
        if chosen.len() % 2 == 1 {
            chosen = chosen[..chosen.len() - 1].to_vec();
        }
        let mut head = k - chosen.len();
        chosen.extend(negatives.iter().take(head).copied());
        chosen.sort_by_key(|c| Reverse(c.abs()));
        let mut neg_tops = if head + 1 < negatives.len() {
            negatives[head] * negatives[head + 1]
        } else {
            0
        };
        let mut chosen_min = chosen[k - 1] * chosen[k - 2];
        while chosen_min < neg_tops && head + 1 < negatives.len() {
            chosen[k - 2] = negatives[head + 1].abs();
            chosen[k - 1] = negatives[head].abs();
            chosen.sort_by_key(|c| Reverse(c.abs()));
            chosen_min = chosen[k - 1] * chosen[k - 2];
            head += 2;
            if head + 1 < negatives.len() {
                neg_tops = negatives[head] * negatives[head + 1];
            }
        }
        let result = chosen.iter().fold(1, |acc, x| (acc * x) % 1000000007);
        println!("{}", (result + 1000000007) % 1000000007);
    } else {
        let mut chosen: Vec<_> = positives.into_iter().take(k).collect();
        let mut head = 0;
        let mut neg_tops = if negatives.len() < 2 {
            0
        } else {
            negatives[head] * negatives[head + 1]
        };
        let mut chosen_min = chosen[k - 1] * chosen[k - 2];
        while chosen_min < neg_tops && head + 1 < negatives.len() {
            chosen[k - 2] = negatives[head + 1].abs();
            chosen[k - 1] = negatives[head].abs();
            chosen.sort_by_key(|c| Reverse(*c));
            chosen_min = chosen[k - 1] * chosen[k - 2];
            head += 2;
            if head + 1 < negatives.len() {
                neg_tops = negatives[head] * negatives[head + 1];
            }
        }
        let result = chosen.iter().fold(1, |acc, x| (acc * x) % 1000000007);
        println!("{}", result);
    }
}