#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}
 
#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};
 
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}
 
#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };
 
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
 
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
 
    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };
 
    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };
 
    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}
 
use std::cmp::Ordering;
use std::cmp;
use std::cmp::min;
use std::collections::BTreeMap;
use std::process;
use std::cmp::Ord;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
use std::collections::BTreeSet;
use std::mem;

/// Equivalent to std::lowerbound and std::upperbound in c++
pub trait BinarySearch<T> {
    fn lower_bound(&self, &T) -> usize;
    fn upper_bound(&self, &T) -> usize;
}
 
impl<T: Ord> BinarySearch<T> for [T] {
    fn lower_bound(&self, x: &T) -> usize {
        let mut low = 0;
        let mut high = self.len();
 
        while low != high {
            let mid = (low + high) / 2;
            match self[mid].cmp(x) {
                Ordering::Less => {
                    low = mid + 1;
                }
                Ordering::Equal | Ordering::Greater => {
                    high = mid;
                }
            }
        }
        low
    }
 
    fn upper_bound(&self, x: &T) -> usize {
        let mut low = 0;
        let mut high = self.len();
 
        while low != high {
            let mid = (low + high) / 2;
            match self[mid].cmp(x) {
                Ordering::Less | Ordering::Equal => {
                    low = mid + 1;
                }
                Ordering::Greater => {
                    high = mid;
                }
            }
        }
        low
    }
}
 
fn matmul(A:&Vec<Vec<i64>>, B:&Vec<Vec<i64>>) -> Vec<Vec<i64>>{
    let mut C = vec![vec![0;B[0].len()];A.len()];
    for i in 0..A.len(){
        for k in 0..B.len(){
            for j in 0..B[0].len(){
                C[i][j] += A[i][k]*B[k][j];
                C[i][j] %= MOD;
            }
        }
    }
    return C;
}
fn matpow(A:&mut Vec<Vec<i64>>, n:usize) -> Vec<Vec<i64>>{
    let mut B = vec![vec![0;A.len()];A.len()];
    for i in 0..A.len(){
        B[i][i] = 1;
    }
    let mut n = n;
    let mut tmp = A.clone();
    while(n>0){
        if n&1 == 1{B = matmul(&B, &tmp);}
        tmp = matmul(&tmp, &tmp);
        n>>=1;
    }
    return B;
}

 
 
 
 
pub static MOD:i64 = 1000000007;
fn main(){
    input!{
        n:usize,
        c:i64,
        xv:[[i64;2];n],
    }
    let mut t = vec![0;n+1];
    let mut tm = vec![0;n+1];
    let mut t2 = vec![0;n+1];
    let mut t2m = vec![0;n+1];
    let mut now = 0;
    let mut ma = 0;
    let mut ma2 = 0;
    for i in 0..n{
        now += xv[i][1];
        t[i+1] = now - xv[i][0];
        ma = cmp::max(ma, t[i+1]);
        tm[i+1] = ma;

        t2[i+1] = now-2*xv[i][0];
        ma2 = cmp::max(ma2, t2[i+1]);
        t2m[i+1] = ma2;
    }

    let mut tr = vec![0;n+1];
    let mut tmr = vec![0;n+1];
    let mut t2r = vec![0;n+1];
    let mut t2mr = vec![0;n+1];
    now = 0;
    ma = 0;
    ma2 = 0;
    for i in 0..n{
        now += xv[n-i-1][1];
        tr[i+1] = now - (c-xv[n-i-1][0]);
        ma = cmp::max(tr[i+1], ma);
        tmr[i+1] = ma;

        t2r[i+1] = now - 2*(c-xv[n-i-1][0]);
        ma2 = cmp::max(t2r[i+1], ma2);
        t2mr[i+1] = ma2;
    }
    
    
    let mut res = 0;
    for i in 0..n{
        res = cmp::max(res, t2m[i+1] + tmr[n-i-1]);
        res = cmp::max(res, t2mr[i+1] + tm[n-i-1]);
    }
    res = cmp::max(res, tm[n]);
    res = cmp::max(res, tmr[n]);
    println!("{}", res);

        
}





Measured memory_bytes: 16636