/** * _ _ __ _ _ _ _ _ _ _ * | | | | / / | | (_) | (_) | | (_) | | * | |__ __ _| |_ ___ ___ / /__ ___ _ __ ___ _ __ ___| |_ _| |_ ___ _____ ______ _ __ _ _ ___| |_ ______ ___ _ __ _ _ __ _ __ ___| |_ ___ * | '_ \ / _` | __/ _ \ / _ \ / / __/ _ \| '_ ` _ \| '_ \ / _ \ __| | __| \ \ / / _ \______| '__| | | / __| __|______/ __| '_ \| | '_ \| '_ \ / _ \ __/ __| * | | | | (_| | || (_) | (_) / / (_| (_) | | | | | | |_) | __/ |_| | |_| |\ V / __/ | | | |_| \__ \ |_ \__ \ | | | | |_) | |_) | __/ |_\__ \ * |_| |_|\__,_|\__\___/ \___/_/ \___\___/|_| |_| |_| .__/ \___|\__|_|\__|_| \_/ \___| |_| \__,_|___/\__| |___/_| |_|_| .__/| .__/ \___|\__|___/ * | | | | | | * |_| |_| |_| * * https://github.com/hatoo/competitive-rust-snippets */ #[allow(unused_imports)] use std::cmp::{max, min, Ordering}; #[allow(unused_imports)] use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque}; #[allow(unused_imports)] use std::iter::FromIterator; #[allow(unused_imports)] use std::io::{stdin, stdout, BufWriter, Write}; mod util { use std::io::{stdin, stdout, BufWriter, StdoutLock}; 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 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 with_bufwriter) -> ()>(f: F) { let out = stdout(); let writer = BufWriter::new(out.lock()); f(writer) } } #[allow(unused_macros)] macro_rules ! get { ( $ t : ty ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . trim ( ) . parse ::<$ t > ( ) . unwrap ( ) } } ; ( $ ( $ t : ty ) ,* ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; let mut iter = line . split_whitespace ( ) ; ( $ ( iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) , ) * ) } } ; ( $ t : ty ; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ ( $ t : ty ) ,*; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ ( $ t ) ,* ) ) . collect ::< Vec < _ >> ( ) } ; ( $ t : ty ;; ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . split_whitespace ( ) . map ( | t | t . parse ::<$ t > ( ) . unwrap ( ) ) . collect ::< Vec < _ >> ( ) } } ; } #[allow(unused_macros)] macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { println ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } } type ConvexHullType = i64; #[allow(dead_code)] fn convex_hull_check( (a1, b1): (ConvexHullType, ConvexHullType), (a2, b2): (ConvexHullType, ConvexHullType), (a3, b3): (ConvexHullType, ConvexHullType), ) -> bool { (a2 as f64 - a1 as f64) * (b3 as f64 - b2 as f64) >= (b2 as f64 - b1 as f64) * (a3 as f64 - a2 as f64) } fn f(x: i64, (a, b): (i64, i64)) -> i64 { x * a + b } fn main() { let (n, a, b, w) = get!(usize, i64, i64, i64); let ds = get!(i64;;); let mut ls = VecDeque::new(); ls.push_back((0, 0)); let mut dp = vec![0; n + 1]; for (&d, i) in ds.iter().zip(1..) { while ls.len() >= 2 && f(i, ls[0]) >= f(i, ls[1]) { ls.pop_front(); } dp[i as usize] = d - a * i + a + b * i * (i - 1) / 2 + f(i, ls[0]); let l = (-b * i, dp[i as usize] + b * i * (i + 1) / 2 + a * i); while ls.len() >= 2 && convex_hull_check(ls[ls.len() - 2], ls[ls.len() - 1], l) { ls.pop_back(); } ls.push_back(l); } println!( "{}", dp.iter() .zip(0i64..) .map(|(&x, i)| w + x - (n as i64 - i) * a + b * (n as i64 - i) * (n as i64 - i + 1) / 2) .min() .unwrap() ); }