#[allow(unused_imports)]
use std::cmp::{Ordering};

#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};

#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};

#[allow(unused_imports)]
use std::iter::FromIterator;

#[allow(unused_macros)]
macro_rules! min {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        std::cmp::min($x, min!($($z),*))
    }}
}

#[allow(unused_macros)]
macro_rules! max {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        std::cmp::max($x, max!($($z),*))
    }}
}

#[allow(unused_macros)]
macro_rules! get { 
    ([$t: ty]) => { 
        { 
            let mut line = String::new(); 
            stdin().read_line(&mut line).unwrap(); 
            line.split_whitespace().map(|t|t.parse::<$t>().unwrap()).collect::<Vec<_>>()
        }
    };
    ([$t: ty];$n: expr) => {
        (0..$n).map(|_|get!([$t])).collect::<Vec<_>>()
    };
    ($t: ty) => {
        {
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            line.trim().parse::<$t>().unwrap()
        }
    };
    ($($t: ty),*) => {
        { 
            let mut line = 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<_>>()
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        #[cfg(debug_assertions)]
        writeln!(&mut std::io::stderr(), concat!("[DEBUG] ", $(stringify!($a), "={:?} "),*), $($a),*).unwrap();
    }
}

const BIG_STACK_SIZE: bool = true;

#[allow(dead_code)]
fn main() {
    use std::thread;
    if BIG_STACK_SIZE {
        thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .name("solve".into())
            .spawn(solve)
            .unwrap()
            .join()
            .unwrap();
    } else {
        solve();
    }
}

#[derive(Debug)]
struct SegmentTree<T, U> {
    len: usize,
    nodes: Vec<T>,
    phantom: std::marker::PhantomData<U>,
}
impl<T, U> SegmentTree<T, U>
where
U: Monoid<T>,
T: Copy + Clone + std::fmt::Debug,
{
#[allow(dead_code)]
    fn new(n: usize) -> Self {
        let mut len = 1;
        while len < n {
            len *= 2;
        }
        SegmentTree {
            len: len,
            nodes: vec![U::identity(); 2 * len - 1],
            phantom: Default::default(),
        }
    }
#[allow(dead_code)]
    fn from_vec(vec: &Vec<T>) -> Self {
        let mut len = 1;
        while len < vec.len() {
            len *= 2;
        }
        let mut nodes = vec![U::identity(); 2 * len - 1];
        for i in 0..vec.len() {
            nodes[i + len - 1] = vec[i];
        }
        for i in (0..len - 1).rev() {
            nodes[i] = U::operation(nodes[2 * i + 1], nodes[2 * i + 2]);
        }
        SegmentTree {
            len: len,
            nodes: nodes,
            phantom: Default::default(),
        }
    }
#[allow(dead_code)]
    fn update(&mut self, index: usize, val: T) {
        let mut index = index;
        index += self.len - 1;
        self.nodes[index] = val;
        while index > 0 {
            index = (index - 1) / 2;
            self.nodes[index] = U::operation(self.nodes[2 * index + 1], self.nodes[2 * index + 2]);
        }
    }
    fn get(&self, l: usize, r: usize) -> T {
        self._get(l, r, 0, 0, self.len)
    }
    fn _get(&self, ql: usize, qr: usize, index: usize, l: usize, r: usize) -> T {
        if r <= ql || qr <= l {
            return U::identity();
        }
        if ql <= l && r <= qr {
            return self.nodes[index];
        }
        let vl = self._get(ql, qr, 2 * index + 1, l, (l + r) / 2);
        let vr = self._get(ql, qr, 2 * index + 2, (l + r) / 2, r);
        U::operation(vl, vr)
    }
}
trait Monoid<T>: SemiGroup<T> {
    fn identity() -> T;
}
trait SemiGroup<T>: Magma<T> {}
trait Magma<T> {
    fn operation(lhs: T, rhs: T) -> T;
}

struct Calc {}

impl Magma<isize> for Calc {
    fn operation(lhs: isize, rhs: isize) -> isize {
        max!(lhs, rhs)
    }
}

impl SemiGroup<isize> for Calc {}

impl Monoid<isize> for Calc {
    fn identity() -> isize {
        0
    }
}

fn solve() {
    let (n,c) = get!(usize, isize);
    let xv = get!(isize, isize; n);

    let mut right1 = SegmentTree::<isize, Calc>::new(n);
    let mut right2 = SegmentTree::<isize, Calc>::new(n);
    let mut left1 = SegmentTree::<isize, Calc>::new(n);
    let mut left2 = SegmentTree::<isize, Calc>::new(n);

    let sum = xv.iter().map(|&(_,v)| v).sum::<isize>();
    let mut csum = 0;
    for (i, (x,v)) in xv.into_iter().enumerate() {
        csum += v;
        right1.update(i, csum - x);
        right2.update(i, csum - 2*x);
        left1.update(i, sum-csum+v-c+x);
        left2.update(i, sum-csum+v-2*c+2*x);
    }
    let mut mx = max!(right1.get(0,n), left1.get(0,n),0);
    for i in 0..n {
        mx = max!(mx, max!(0, right1.get(0, i+1))+max!(0, left2.get(i+1, n)));
        mx = max!(mx, max!(0, right2.get(0, i+1))+max!(0, left1.get(i+1, n)));
    }
    println!("{}", mx);
}


Measured memory_bytes: 18812