package main import ( "bufio" "fmt" "math" "os" "strconv" ) func configure(scanner *bufio.Scanner) { scanner.Split(bufio.ScanWords) scanner.Buffer(make([]byte, 1000005), 1000005) } func getNextString(scanner *bufio.Scanner) string { scanned := scanner.Scan() if !scanned { panic("scan failed") } return scanner.Text() } func getNextInt(scanner *bufio.Scanner) int { i, _ := strconv.Atoi(getNextString(scanner)) return i } func getNextInt64(scanner *bufio.Scanner) int64 { i, _ := strconv.ParseInt(getNextString(scanner), 10, 64) return i } func getNextFloat64(scanner *bufio.Scanner) float64 { i, _ := strconv.ParseFloat(getNextString(scanner), 64) return i } func main() { fp := os.Stdin wfp := os.Stdout extra := 0 if os.Getenv("I") == "IronMan" { fp, _ = os.Open(os.Getenv("END_GAME")) extra = 100 } scanner := bufio.NewScanner(fp) configure(scanner) writer := bufio.NewWriter(wfp) defer func() { r := recover() if r != nil { fmt.Fprintln(writer, r) } writer.Flush() }() solve(scanner, writer) for i := 0; i < extra; i++ { fmt.Fprintln(writer, "-----------------------------------") solve(scanner, writer) } } func solve(scanner *bufio.Scanner, writer *bufio.Writer) { n := getNextInt(scanner) v := getNextInt(scanner) l := getNextInt(scanner) xx := make([]int, n) vv := make([]int, n) ww := make([]int, n) for i := 0; i < n; i++ { xx[i] = getNextInt(scanner) vv[i] = getNextInt(scanner) ww[i] = getNextInt(scanner) } xx = append(xx, l) for i := n; i > 0; i-- { xx[i] -= xx[i-1] } dp := makeGrid(n+2, v+1) for i := 0; i < n+2; i++ { for j := 0; j < v+1; j++ { dp[i][j] = cint(math.MaxInt64) } } dp[0][v] = 0 for i := 0; i < n+1; i++ { for j := xx[i]; j < v+1; j++ { dp[i+1][j-xx[i]].minAs(dp[i][j]) } for j := 1; i < n && j <= vv[i] && v-j >= 0; j++ { if dp[i+1][v-j].sub(math.MaxInt64) == 0 { continue } dp[i+1][v].minAs(dp[i+1][v-j].add(ww[i])) } for j := v; i < n && j >= vv[i]; j-- { if dp[i+1][j-vv[i]].sub(math.MaxInt64) == 0 { continue } dp[i+1][j].minAs(dp[i+1][j-vv[i]].add(ww[i])) } } var ans cint ans = dp[n+1][0] for i := 0; i < v; i++ { ans.minAs(dp[n][i+1]) } if ans.sub(math.MaxInt64) == 0 { ans = -1 } fmt.Fprintln(writer, ans) } func makeGrid(h, w int) [][]cint { index := make([][]cint, h, h) data := make([]cint, h*w, h*w) for i := 0; i < h; i++ { index[i] = data[i*w : (i+1)*w] } return index } type cint int64 func (ct *cint) cintize(x interface{}) cint { if y, isCint := x.(cint); isCint { return y } if y, isInt64 := x.(int64); isInt64 { return cint(y) } return cint(x.(int)) } func (ct *cint) minAs(x interface{}) *cint { *ct = ct.min(x) return ct } func (ct *cint) maxAs(x interface{}) *cint { *ct = ct.max(x) return ct } func (ct cint) min(x interface{}) cint { y := ct.cintize(x) if ct > y { return y } return ct } func (ct cint) max(x interface{}) cint { y := ct.cintize(x) if ct < y { return y } return ct } func (ct cint) add(x interface{}) cint { return ct + ct.cintize(x) } func (ct cint) sub(x interface{}) cint { return ct - ct.cintize(x) } func (ct cint) mul(x interface{}) cint { return ct * ct.cintize(x) } func (ct cint) div(x interface{}) cint { return ct / ct.cintize(x) }