Scanf.scanf "%d %d" (fun n c ->
    let xv = Array.init n (fun _ -> Scanf.scanf " %d %d" (fun x v -> x, v)) in
    Array.sort compare xv;
    let as1 = Array.make n 0 in
    let as2 = Array.make n 0 in
    let rec loop i cur mxdiff =
        if i < n then (
            let (x, v) = xv.(i) in
            let cur = cur + v in
            let mxdiff = max mxdiff (cur - x) in
            as1.(i) <- mxdiff;
            loop (i + 1) cur mxdiff
        )
    in
    loop 0 0 0;
    let rec loop i cur mxdiff =
        if i >= 0 then (
            let (x, v) = xv.(i) in
            let cur = cur + v in
            let mxdiff = max mxdiff (cur - (c - x)) in
            as2.(i) <- mxdiff;
            loop (i - 1) cur mxdiff
        )
    in
    loop (n - 1) 0 0;

    let rec loop i cur mx =
        if i = n then mx else
            let (x, v) = xv.(i) in
            let cur = cur + v in
            let cost = cur - x * 2 + (if i = n - 1 then x else as2.(i + 1)) in
            loop (i + 1) cur (max mx cost)
    in
    let a1 = loop 0 0 as2.(0) in

    let rec loop i cur mx =
        if i < 0 then mx else
            let (x, v) = xv.(i) in
            let cur = cur + v in
            let cost = cur - (c - x) * 2 + (if i = 0 then c - x else as1.(i - 1)) in
            loop (i - 1) cur (max mx cost)
    in
    let a2 = loop (n - 1) 0 as1.(n - 1) in
    Printf.printf "%d\n" @@ max a1 a2
)