#[allow(unused_imports)] use std::cmp::{max, min, Ordering, Reverse}; #[allow(unused_imports)] use std::collections::{HashMap, HashSet}; mod util { use std::io::stdin; use std::str::FromStr; use std::fmt::Debug; #[allow(dead_code)] pub fn line() -> String { let mut line: String = String::new(); stdin().read_line(&mut line).unwrap(); line.trim().to_string() } #[allow(dead_code)] pub fn get() -> T where ::Err: Debug, { let mut line: String = String::new(); stdin().read_line(&mut line).unwrap(); line.trim().parse().unwrap() } #[allow(dead_code)] pub fn gets() -> Vec where ::Err: Debug, { let mut line: String = String::new(); stdin().read_line(&mut line).unwrap(); line.split_whitespace() .map(|t| t.parse().unwrap()) .collect() } #[allow(dead_code)] pub fn get2() -> (T, U) where ::Err: Debug, ::Err: Debug, { let mut line: String = String::new(); stdin().read_line(&mut line).unwrap(); let mut iter = line.split_whitespace(); ( iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap(), ) } #[allow(dead_code)] pub fn get3() -> (S, T, U) where ::Err: Debug, ::Err: Debug, ::Err: Debug, { let mut line: String = String::new(); stdin().read_line(&mut line).unwrap(); let mut iter = line.split_whitespace(); ( iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap(), ) } } #[allow(unused_macros)] macro_rules! debug { ($x: expr) => { println!("{}: {:?}", stringify!($x), $x) } } #[derive(Eq, PartialEq, Clone)] struct Rev(pub T); impl PartialOrd for Rev { fn partial_cmp(&self, other: &Rev) -> Option { other.0.partial_cmp(&self.0) } } impl Ord for Rev { fn cmp(&self, other: &Rev) -> Ordering { other.0.cmp(&self.0) } } struct SEG { n: usize, buf: Vec<(u64, Rev)>, } impl SEG { fn new(n: usize) -> SEG { let n = (1..).find(|&d| 2usize.pow(d) > n).unwrap(); SEG { n: 2usize.pow(n), buf: vec![(0, Rev(0)); 1000000], } } fn update(&mut self, k: usize, a: (u64, Rev)) { let mut k = k + self.n - 1; self.buf[k] = a; while k > 0 { k = (k - 1) / 2; self.buf[k] = max(self.buf[k * 2 + 1].clone(), self.buf[k * 2 + 2].clone()); } } fn query(&self, a: usize, b: usize, k: usize, l: usize, r: usize) -> (u64, Rev) { if r <= a || b <= l { return (0, Rev(0)); } if a <= l && r <= b { self.buf[k].clone() } else { let vl = self.query(a, b, k * 2 + 1, l, (l + r) / 2); let vr = self.query(a, b, k * 2 + 2, (l + r) / 2, r); max(vl, vr) } } fn q(&self, a: usize, b: usize) -> (u64, Rev) { self.query(a, b, 0, 0, self.n) } } /* Can't impl Clone for Reverse { fn clone(&self) -> Reverse { Reverse(self.0.clone()) } } */ fn main() { let (n, d, k): (usize, usize, u64) = util::get3(); let xs: Vec = (0..n).map(|_| util::get()).collect(); let mut seg = SEG::new(n); for (i, &x) in xs.iter().enumerate() { seg.update(i, (x, Rev(i))); } let ans = (0..n - 1) .filter_map(|i| { let r = min(i + 1 + d, n); let (maximum, rev) = seg.q(i + 1, r); if maximum > xs[i] { Some((maximum - xs[i], Rev((i, rev.0)))) } else { None } }) .max(); if let Some((profit, Rev((buy, sell)))) = ans { println!("{}", profit * k); println!("{} {}", buy, sell); } else { println!("0"); } }