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: Wrong Answer)

//  ____            _                  _                       _       _
// |  _ \ _   _ ___| |_    ___ _ __   | |_ ___ _ __ ___  _ __ | | __ _| |_ ___
// | |_) | | | / __| __|  / __| '_ \  | __/ _ \ '_ ` _ \| '_ \| |/ _` | __/ _ \
// |  _ <| |_| \__ \ |_  | (__| |_) | | ||  __/ | | | | | |_) | | (_| | ||  __/
// |_| \_\\__,_|___/\__|  \___| .__/___\__\___|_| |_| |_| .__/|_|\__,_|\__\___|
//                            |_| |_____|               |_|

//https://github.com/manta1130/Competitive_Programming_Template_Rust


mod segtree {

///汎用のセグメント木
pub struct SegTreeBasic<T, F> {
    //n: usize,
    v: Vec<T>,
    init: T,
    parent_generator: F,
}

impl<T, F> SegTreeBasic<T, F>
where
    T: PartialOrd + Clone + Copy,
    F: Fn(T, T) -> T,
{
    ///セグメント木を構築する。
    ///
    /// # Examples
    /// ```ignore
    /// let mut segtree = SegTreeBasic::new(100, usize::max_value(), |a, b| std::cmp::min(a, b));
    /// //要素数100のRMQを構築する。
    /// ```
    pub fn new(n: usize, init: T, parent_generator: F) -> SegTreeBasic<T, F> {
        let mut size = 1;
        while size < n {
            size *= 2;
        }
        let v = vec![init; size * 2 - 1];
        SegTreeBasic {
            //n: size,
            v: v,
            init: init,
            parent_generator: parent_generator,
        }
    }

    ///[l,r)の要素をparent_generatorに基づいて計算する。
    pub fn get(&mut self, l: usize, r: usize) -> T {
        let vlen = self.v.len();
        self._get(l, r, 0, 0, (vlen + 1) / 2)
    }

    fn _get(&mut self, l: usize, r: usize, now: usize, a: usize, b: usize) -> T {
        if a >= r || b <= l {
            self.init
        } else if l <= a && b <= r {
            self.v[now]
        } else {
            let bufa = self._get(l, r, now * 2 + 1, a, (a + b) / 2);
            let bufb = self._get(l, r, now * 2 + 2, (a + b) / 2, b);
            let cond = &self.parent_generator;
            cond(bufa, bufb)
        }
    }

    ///任意の要素を書き換える。
    pub fn update(&mut self, index: usize, value: T) {
        let offset = (self.v.len() + 1) / 2 - 1;
        self.v[offset + index] = value;
        let mut parent = index + offset;
        loop {
            parent = (parent - 1) / 2;
            let cond = &self.parent_generator;
            self.v[parent] = cond(self.v[parent * 2 + 1], self.v[parent * 2 + 2]);
            if parent == 0 {
                break;
            }
        }
    }
}

///汎用の遅延セグメント木
pub struct SegTreeLazy<T, F, G, H> {
    //n: usize,
    v: Vec<T>,
    lazy: Vec<T>,
    lazy_flag: Vec<bool>,
    init: T,
    lazy_init: T,
    lazy_convert: F,
    lazy_propagation: G,
    parent_generator: H,
}

impl<T, F, G, H> SegTreeLazy<T, F, G, H>
where
    T: PartialOrd + Clone + Copy,
    F: Fn(T, T) -> T,
    G: Fn(T, T) -> T,
    H: Fn(T, T) -> T,
{
    ///遅延セグメント木を構築する。
    ///
    /// # Examples
    /// ```ignore
    ///　let mut segtree = SegTreeLazy::new(100, 0, 0, |a, b| a + b, |a, b| a + b, |a, b| a + b / 2);
    /// //要素数100の区間加算・区間取得のセグ木を構築する。
    /// ```
    pub fn new(
        n: usize,
        init: T,
        lazy_init: T,
        parent_generator: H,
        lazy_convert: F,
        lazy_propagation: G,
    ) -> SegTreeLazy<T, F, G, H> {
        let mut size = 1;
        while size < n {
            size *= 2;
        }
        let v = vec![init; size * 2 - 1];
        let lazy = vec![lazy_init; size * 2 - 1];
        let lazy_flag = vec![false; size * 2 - 1];
        SegTreeLazy {
            v: v,
            lazy: lazy,
            init: init,
            lazy_init: lazy_init,
            lazy_flag: lazy_flag,
            lazy_convert: lazy_convert,
            lazy_propagation: lazy_propagation,
            parent_generator: parent_generator,
        }
    }

    ///[l,r)の要素をparent_generatorに基づいて計算する。
    pub fn get(&mut self, l: usize, r: usize) -> T {
        let vlen = self.v.len();
        self._get(l, r, 0, 0, (vlen + 1) / 2)
    }

    fn _eval(&mut self, now: usize) {
        if now >= self.lazy_flag.len() {
            return;
        }

        if now * 2 + 2 < self.lazy.len() && self.lazy[now] != self.lazy_init {
            let propagation = &self.lazy_propagation;
            self.lazy[now * 2 + 1] = propagation(self.lazy[now * 2 + 1], self.lazy[now]);
            self.lazy[now * 2 + 2] = propagation(self.lazy[now * 2 + 2], self.lazy[now]);
        }
        if self.lazy[now] != self.lazy_init {
            let convert = &self.lazy_convert;
            self.v[now] = convert(self.v[now], self.lazy[now]);
        }
        self.lazy[now] = self.lazy_init;
        if self.lazy_flag[now] {
            self._eval(now * 2 + 1);
            self._eval(now * 2 + 2);
            let generator = &self.parent_generator;
            if now * 2 + 2 < self.v.len() {
                self.v[now] = generator(self.v[now * 2 + 1], self.v[now * 2 + 2]);
            }
        }
        self.lazy_flag[now] = false;
    }

    fn _get(&mut self, l: usize, r: usize, now: usize, a: usize, b: usize) -> T {
        self._eval(now);
        if a >= r || b <= l {
            self.init
        } else if l <= a && b <= r {
            self.v[now]
        } else {
            let bufa = self._get(l, r, now * 2 + 1, a, (a + b) / 2);
            let bufb = self._get(l, r, now * 2 + 2, (a + b) / 2, b);
            let cond = &self.parent_generator;
            cond(bufa, bufb)
        }
    }

    ///任意の要素を書き換える。
    pub fn update(&mut self, l: usize, r: usize, v: T) {
        let vlen = self.v.len();
        self._update(l, r, 0, 0, (vlen + 1) / 2, v);
    }

    fn _update(&mut self, l: usize, r: usize, now: usize, a: usize, b: usize, v: T) {
        if now < self.v.len() {
            self._eval(now);
        }
        if l <= a && b <= r {
            let propagation = &self.lazy_propagation;
            self.lazy[now] = propagation(self.lazy[now], v);
            self.lazy_flag[now] = true;
        } else if !(a >= r || b <= l) {
            let v_next;
            {
                let propagation = &self.lazy_propagation;
                v_next = propagation(self.lazy_init, v);
            }
            self._update(l, r, now * 2 + 1, a, (a + b) / 2, v_next);
            self._update(l, r, now * 2 + 2, (a + b) / 2, b, v_next);
            self.lazy_flag[now] = true;
        }
    }
}
}

use proconio::{
    fastout, input,
    marker::{Bytes, Chars, Isize1, Usize1},
};
use std::cmp::Reverse;

use segtree::*;

const MOD: isize = 1_000_000_007;

fn twosum(a: f64, b: f64) -> (f64, f64) {
    let x = a + b;
    let tmp = x - a;
    let y = b - tmp;
    (x, y)
}

fn add(a: (f64, f64), b: (f64, f64)) -> (f64, f64) {
    let mut z = twosum(a.0, b.0);
    if z.0.is_infinite() {
        return (z.0, 0.0);
    }
    z.1 = z.1 + a.1 + b.1;
    let z = twosum(z.0, z.1);
    if z.0.is_infinite() {
        return (z.0, 0.0);
    }
    z
}

fn main() {
    input! {
        n:usize,k:usize,
        a:[isize;n],
    }
    if n == k {
        let mut ans = 1;
        for mut i in a {
            if i < 0 {
                ans *= MOD - 1;
                ans %= MOD;
                i *= -1;
            }
            ans *= i;
            ans %= MOD;
        }
        println!("{}", ans);
        return;
    }
    let mut plus_vec = vec![];
    let mut minus_vec = vec![];
    for i in a {
        if i < 0 {
            minus_vec.push(-1 * i);
        } else {
            plus_vec.push(i);
        }
    }

    if plus_vec.len() == 0 && k % 2 != 0 {
        minus_vec.sort();
        let mut ans = MOD - 1;
        for i in 0..k {
            ans *= minus_vec[i];
            ans %= MOD;
        }
        println!("{}", ans);
        return;
    }
    let mut pluslog_segtree = SegTreeBasic::new(plus_vec.len() + 1, (0.0, 0.0), |a, b| add(a, b));
    let mut minuslog_segtree = SegTreeBasic::new(minus_vec.len() + 1, (0.0, 0.0), |a, b| add(a, b));
    let mut plus_segtree = SegTreeBasic::new(plus_vec.len() + 1, 1, |a, b| (a * b) % MOD);
    let mut minus_segtree = SegTreeBasic::new(minus_vec.len() + 1, 1, |a, b| (a * b) % MOD);

    plus_vec.sort_by_key(|&x| Reverse(x));
    minus_vec.sort_by_key(|&x| Reverse(x));

    for (i, x) in plus_vec.iter().enumerate() {
        plus_segtree.update(i, *x);
        pluslog_segtree.update(
            i,
            if *x == 0 {
                (-9999.0, 0.0)
            } else {
                ((*x as f64).ln(), 0.0)
            },
        );
    }
    for (i, x) in minus_vec.iter().enumerate() {
        minus_segtree.update(i, *x);
        minuslog_segtree.update(i, ((*x as f64).ln(), 0.0));
    }

    let mut ans = 0;
    let mut anslog = -9999999999999999.0;
    let mut anslog_2 = 0.0;
    for minus_count in (0..=k).step_by(2) {
        let plus_count = k - minus_count;
        if plus_count > plus_vec.len() || minus_count > minus_vec.len() {
            continue;
        }
        let minus_log = minuslog_segtree.get(0, minus_count);
        let plus_log = pluslog_segtree.get(0, plus_count);
        if (anslog - (minus_log.0 + plus_log.0)).abs() < 0.00000000000001 {
            //dbg!(minus_count, anslog, anslog_2, minus_log.1 + plus_log.1);
            if anslog_2 < minus_log.1 + plus_log.1 + 0.0000000000000000001 {
                //dbg!("hoge");
                anslog = minus_log.0 + plus_log.0;
                anslog_2 = minus_log.1 + plus_log.1;
                ans = (plus_segtree.get(0, plus_count) * minus_segtree.get(0, minus_count)) % MOD;
            }
        } else {
            if anslog < minus_log.0 + plus_log.0 {
                anslog = minus_log.0 + plus_log.0;
                anslog_2 = minus_log.1 + plus_log.1;
                ans = (plus_segtree.get(0, plus_count) * minus_segtree.get(0, minus_count)) % MOD;
            }
        }
    }
    //println!("{} {}", anslog, anslog_2);
    println!("{}", ans);
}