#![allow(non_snake_case)] use std::cmp; use std::io::{ self, prelude::* }; macro_rules! pick { ($tokens:expr) => { $tokens.next().unwrap().parse().expect("parse error") } } struct Solver { N: usize, D: i64, A: [Vec; 2], } impl Solver { fn f(&self, i: usize, j: usize) -> i64 { assert!(i < self.N); assert!(j == 0 || j == 1); if i == self.N-1 { match j { 0 => cmp::max(self.A[0][i], self.A[1][i]-self.D), 1 => cmp::max(self.A[1][i], self.A[0][i]-self.D), _ => unreachable!(), } } else { match j { 0 => cmp::max(self.A[0][i]+self.f(i+1,0), self.A[1][i]+self.f(i+1,1)-self.D), 1 => cmp::max(self.A[1][i]+self.f(i+1,1), self.A[0][i]+self.f(i+1,0)-self.D), _ => unreachable!(), } } } } fn main() { let mut s = String::new(); io::stdin().read_to_string(&mut s).expect("i/o error"); let mut tokens = s.split_whitespace(); let N: usize = pick!(tokens); let D: i64 = pick!(tokens); let mut T = Vec::::with_capacity(N); let mut K = Vec::::with_capacity(N); for _ in 0..N { T.push(pick!(tokens)); K.push(pick!(tokens)); } let solver = Solver { N, D, A: [T, K], }; let ans = solver.f(0,0); println!("{}", ans); }