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)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<usize>();
    let (n, k) = (v[0], v[1]);
    let mut a = read_vec::<i128>();
    if *a.iter().max().unwrap() < 0 && k % 2 == 1 {
        a.sort();
        a.reverse();
        let mut ans = 1;
        for i in 0..k {
            ans *= a[i];
            ans += 1000_000_000 + 7;
            ans %= 1000_000_000 + 7;
        }
        println!("{}", ans);
        return;
    }
    if *a.iter().max().unwrap() < 0 {
        a.sort();
        let mut ans = 1;
        for i in 0..k {
            ans *= a[i];
            ans %= 1000_000_000 + 7;
        }
        println!("{}", ans);
        return;
    }
    if n == k {
        a.sort();
        a.reverse();
        let mut ans = 1;
        for i in 0..k {
            ans *= a[i] + 1000_000_000 + 7;
            ans %= 1000_000_000 + 7;
        }
        println!("{}", ans);
        return;
    }
    if k == 1 {
        a.sort();
        a.reverse();
        let mut ans = 1;
        for i in 0..k {
            ans *= a[i] + 1000_000_000 + 7;
            ans %= 1000_000_000 + 7;
        }
        println!("{}", ans);
        return;
    }

    let mut positives = a.iter().filter(|&&x| x >= 0).cloned().collect::<Vec<_>>();
    positives.sort();
    positives.reverse();
    let mut negatives = a.iter().filter(|&&x| x < 0).cloned().collect::<Vec<_>>();
    negatives.sort();
    negatives.reverse();
    let mut negatives = negatives.into_iter().collect::<Vec<_>>();
    debug!(negatives);

    let mut uses = VecDeque::new();
    for i in 0..positives.len() {
        uses.push_back(positives[i]);
        if uses.len() == k {
            break;
        }
    }
    if (k - uses.len()) % 2 == 1 {
        uses.pop_back();
        uses.push_front(negatives.pop().unwrap());
        uses.push_front(negatives.pop().unwrap());
    }
    while uses.len() < k {
        uses.push_front(negatives.pop().unwrap());
        uses.push_front(negatives.pop().unwrap());
    }
    debug!(uses);

    // assert_eq!(uses.len(), k);
    while negatives.len() >= 2 {
        assert_eq!(uses.len(), k);
        let cur_last_two = uses[uses.len() - 1] * uses[uses.len() - 2];
        let next_last_two = negatives[negatives.len() - 1] * negatives[negatives.len() - 2];
        if cur_last_two < next_last_two {
            uses.pop_back();
            uses.pop_back();
            uses.push_front(negatives.pop().unwrap());
            uses.push_front(negatives.pop().unwrap());
        } else {
            break;
        }
    }

    let neg = uses.iter().filter(|&&x| x < 0).count();
    assert!(neg % 2 == 0);

    let mut ans = 1;
    for num in uses {
        ans *= num + 1000_000_000 + 7;
        ans %= 1000_000_000 + 7;
    }
    println!("{}", ans);
    return;
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}