Cow Roller Coaster
InterviewTime limit1sMemory limit128 MB
Choose components that tile [0, L] with no gaps or overlaps, maximizing total fun while keeping total cost within budget B.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Sorting, Intervals, Array
- Solved
- No attempts yet
Problem
The cows are building a roller coaster, and they want it to be as fun as possible while staying within their budget.
The track is a straight line of length . There are interchangeable components. Component has a fixed length and, because of the terrain, can only be placed starting at position , so it covers the interval . The cows connect components so that the coaster starts at position and ends at position , with the end of each component (except the last) being exactly the start of the next one. In other words, the chosen components must tile the whole segment with no gaps and no overlaps.
Each component has a fun rating and a cost . The coaster's total fun is the sum of the fun ratings of the components used, and its total cost is the sum of their costs. The total budget is . Determine the maximum total fun of a coaster that spans and costs at most .
Constraints
Input
- Line 1: three space-separated integers , , and .
- Lines : line contains four space-separated integers , , , and .
Output
- A single integer: the maximum total fun of a coaster that covers the entire track while staying within the budget. If no such coaster can be built, output .
Hint
For the first test case, one optimal choice is to take the components on input rows 3, 5, and 6: they form a connected coaster covering with total fun and total cost , which is within the budget of . Taking the first two components would give more fun (), but their combined cost is , which exceeds the budget.