use std::cmp::*; use std::io::*; use std::str::*; use std::fmt::*; fn readw(s: &mut R) -> Option { let s = s.bytes().map(|c| c.unwrap() as char) .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect::(); if s.is_empty() { None } else { s.parse::().ok() } } fn read(s: &mut R) -> T { readw(s).unwrap() } #[allow(dead_code)] fn p(a: &A) { println!("{}", a); } #[allow(dead_code)] fn p2(a: &A, b: &B) { println!("{} {}", a, b); } #[allow(dead_code)] fn p3(a: &A, b: &B, c: &C) { println!("{} {} {}", a, b, c); } fn main(){ let s = stdin(); let mut s = BufReader::new(s.lock()); let s = &mut s; while let Some(n) = readw(s) { let d: i64 = read(s); let mut t = vec![0; n]; let mut k = vec![0; n]; for i in 0..n { t[i] = read(s); k[i] = read(s); } let mut a = 0; let mut b = -100000000_000000000; for i in 0..n { let na = max(a + t[i], b - d + t[i]); let nb = max(b + k[i], a - d + k[i]); a = na; b = nb; } println!("{}", max(a, b)); } }