fn main() { input! { n: usize, m: usize, p: [(i64, i64); n], } let mut h = std::collections::BinaryHeap::new(); let mut line = vec![]; for (a, d) in p { if d < 0 { h.push((a, d)); } else { // k * a + k * (k - 1) / 2 * d // k 確定 2a + (k - 1)d line.push((-d, -(2 * a - d))); } } if h.is_empty() { let m = m as i64; let ans = line.iter().map(|(a, d)| -m * (m * a + d) / 2).max().unwrap(); println!("{}", ans); return; } let mut neg = vec![0; m + 1]; for i in 1..(m + 1) { let (a, d) = h.pop().unwrap(); neg[i] = neg[i - 1] + a; h.push((a + d, d)); } if line.is_empty() { println!("{}", neg[m]); return; } let cht = ConvexHullTrick::new(line); let mut ans = std::i64::MIN; for (k, neg) in neg.iter().enumerate() { let k = (m - k) as i64; let v = cht.find(k); ans = ans.max(*neg + k * -v / 2); } println!("{}", ans); } // ---------- begin input macro ---------- // reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 #[macro_export] macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } #[macro_export] macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } #[macro_export] macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, bytes) => { read_value!($iter, String).bytes().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // ---------- end input macro ---------- // 以下のクエリを処理する // add_line(a, b): 直線 ax + b を追加 // find(x): min (ax + b) を返す。空のとき呼ぶとREになる // 計算量 // 直線追加クエリがN回飛んでくるとする // 直線追加 償却O(log N) // 点質問: O((log N)^2) // ---------- begin incremental convex hull trick (min) ---------- // reference: https://yukicoder.me/wiki/decomposable_searching_problem // verify: https://old.yosupo.jp/submission/35150 #[derive(Clone)] struct ConvexHullTrick { line: Vec<(i64, i64)>, } impl ConvexHullTrick { fn new(mut line: Vec<(i64, i64)>) -> Self { assert!(line.len() > 0); line.sort(); line.dedup_by(|a, b| a.0 == b.0); let mut stack: Vec<(i64, i64)> = vec![]; for (a, b) in line { while stack.len() >= 2 { let len = stack.len(); let (c, d) = stack[len - 1]; let (e, f) = stack[len - 2]; let x = (d - b).div_euclid(a - c); let y = (f - d).div_euclid(c - e); if x >= y { stack.pop(); } else { break; } } stack.push((a, b)); } ConvexHullTrick { line: stack } } fn find(&self, x: i64) -> i64 { let mut l = 0; let mut r = self.line.len() - 1; let line = &self.line; let func = |k: usize| -> i64 { let (a, b) = line[k]; a * x + b }; while r - l >= 3 { let ll = (2 * l + r) / 3; let rr = (l + 2 * r) / 3; if func(ll) <= func(rr) { r = rr; } else { l = ll; } } line[l..=r].iter().map(|p| p.0 * x + p.1).min().unwrap() } } #[derive(Clone, Default)] pub struct IncrementalCHT { size: usize, cht: Vec<(ConvexHullTrick, usize)>, } impl IncrementalCHT { pub fn new() -> Self { IncrementalCHT { size: 0, cht: vec![], } } pub fn add_line(&mut self, a: i64, b: i64) { self.size += 1; let mut line = vec![(a, b)]; let mut p = 0; while self.cht.last().map_or(false, |q| q.1 == p) { p += 1; line.append(&mut self.cht.pop().unwrap().0.line); } let cht = ConvexHullTrick::new(line); self.cht.push((cht, p)); } pub fn find(&self, x: i64) -> i64 { self.cht.iter().map(|p| p.0.find(x)).min().unwrap() } pub fn append(&mut self, other: &mut Self) { if self.size < other.size { std::mem::swap(self, other); } for (mut cht, _) in other.cht.drain(..) { for (a, b) in cht.line.drain(..) { self.add_line(a, b); } } other.size = 0; } } // ---------- end incremental convex hull trick (min) ----------