let f (s:string) = s.Split ' ' |> Array.map int |> fun (a:int[]) -> (a.[0], a.[1]) type Buf = { mutable tm: int mutable km: int d: int } with member this.max = max this.tm this.km member this.update(s:string) = let (tm, km) = s |> f |> fun (t:int, k:int) -> (t + (max this.tm (this.km - this.d)), k + (max (this.tm - this.d) this.km)) this.tm <- tm this.km <- km let (n, d) = stdin.ReadLine () |> f let (t, k) = stdin.ReadLine () |> f let b:Buf = { tm = t; km = k - d; d = d } seq { for _ in 1..n-1 -> stdin.ReadLine () } |> Seq.iter (fun s -> b.update(s)) b.max |> printfn "%d"