package main import . "fmt" import . "os" import bf "bufio" import "container/heap" import . "sort" func main() { rd:=bf.NewReader(Stdin) var n,y,z int Fscan(rd,&n,&y,&z) ss := []*S{} hp := &H{} for i := 0; i < n; i++ { s := new(S) Fscan(rd,&s.c,&s.l,&s.x) if s.l <= y { heap.Push(hp, s) } else { ss = append(ss, s) } } Slice(ss, func(i, j int) bool { return ss[i].l < ss[j].l }) ans := 0 for y < z && hp.Len() > 0 { zz := z if len(ss) > 0 { zz = min(zz, ss[0].l) } s := heap.Pop(hp).(*S) c := min((zz-y+s.x-1)/s.x,s.c) y += s.x*c s.c -= c ans += c if y >= z { break } if s.c > 0 { heap.Push(hp, s) } for len(ss) > 0 && ss[0].l <= y { heap.Push(hp, ss[0]) ss = ss[1:] } } if y < z { Println(-1) } else { Println(ans) } } type S struct { c, l, x int } type H []*S func (h H) Len() int { return len(h) } func (h H) Less(i, j int) bool { return h[i].x > h[j].x } func (h H) Swap(i, j int) { h[i],h[j] = h[j],h[i] } func (h *H) Push(v any) { *h = append(*h, v.(*S)) } func (h *H) Pop() any { old := *h n := len(old) v := old[n-1] *h = old[:n-1] return v }