Milking Time
InterviewTime limit1sMemory limit128 MB
Choose non-overlapping milking intervals, each separated by at least R rest hours, to maximize the total milk produced over N hours.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Binary search, Sorting, Intervals
- Solved
- No attempts yet
Problem
Bessie is a very hard-working cow, and she wants to maximize how much milk she produces. She plans her next () hours, labeled , so that she produces as much milk as possible during them.
Farmer John has a list of () possibly overlapping intervals during which he is available for milking. Interval has a starting hour (), an ending hour (), and an efficiency (), which is the number of gallons of milk Bessie produces during that interval. Milking starts at the beginning of the starting hour and stops at the beginning of the ending hour. If Bessie is milked during an interval, she must be milked through the entire interval.
After being milked during any interval, Bessie must rest () hours before she can start milking again. That is, if she is milked during an interval that ends at hour , the next interval she is milked in must start at hour or later. Given Farmer John's list of intervals, determine the maximum amount of milk Bessie can produce during the hours.
Input
- Line 1: three space-separated integers , , and .
- Lines 2 to : line describes the -th milking interval with three space-separated integers , , and .
Output
- Line 1: the maximum number of gallons of milk Bessie can produce during the hours.