// Learn more about F# at http://fsharp.org open System type S = { X: int V: int W: int64 } let min a b = if a < b then a else b let max a b = if a > b then a else b [] let main argv = let [| n; v; l |] = stdin.ReadLine().Split() |> Array.map int let mutable stands = [| for i in 1 .. n -> (let [| x; v; w |] = stdin.ReadLine().Split() |> Array.map int { X = x V = v W = w |> int64 }) |] if stands.[0].X > 0 then stands <- Array.append [| { X = 0 V = 0 W = 0 |> int64 } |] stands if stands.[stands.Length - 1].X < l then stands <- Array.append stands [| { X = l V = 0 W = 0 |> int64 } |] let nn = stands.Length let inf = Int64.MaxValue / 2L let mutable memo = Array2D.create (nn + 1) (v + 1) inf // printfn "%A" stands let rec solve i j = // printfn "%A %A" i j if memo.[i, j] < inf then memo.[i, j] else if i = 0 then if j = v then 0L else inf else let mutable res = inf let d = stands.[i].X - stands.[i - 1].X if j + d <= v then res <- min res (solve (i - 1) (j + d)) if j > 0 && stands.[i].V > 0 then res <- min res ((solve i (max 0 (j - stands.[i].V))) + stands.[i].W) memo.[i, j] <- res res let mutable ans = inf for j = 0 to v do ans <- min ans (solve (nn - 1) j) if ans < inf then printfn "%d" ans else printfn "-1" 0 // return an integer exit code