Level Up
Time limit2sMemory limit256 MB
Given quests that give experience and cost time on two levels, find the minimum total time to clear both levels, with overflow carrying over, or -1 if impossible.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Array
- Solved
- No attempts yet
Problem
Being a fan of MMORPGs, Steve was really excited when World of Warcraft Classic was announced. He started playing from the first day and now has only two levels to go till the maximum level. Of course, he doesn't have as much time as he used to have when the game first appeared, so he really wants to finish these two levels as fast as possible.
In order to pass the first level, Steve needs experience. Only after he gains it, he can move to the second level, in which he needs to get other experience to pass it.
Steve has a list of available quests. He knows that he may finish two levels with these quests. To pass the -th quest, Steve needs minutes. As a result, he will get experience for this quest.
When Steve finishes a quest that takes him to the next level, the extra experience overflow is subtracted from the next level's required experience. Once he levels up, all the quests that are left will offer less experience , but they will also require less time .
Note that if Steve finishes a quest, he cannot repeat this quest anymore (even in another level).
Given the list of quests, help Steve choose the order in which he will do the quests so that he finishes the last two levels as fast as possible.
Input
The first line contains three integers , , (), the number of quests, the experience required for the first level, and the experience required for the second level.
Each of the next lines contains four integers , , , (, ), where and are the experience you gain from the -th quest on the first and second level, and and are the minutes you need to pass this quest on the first and second level.
Output
Print one number, the minimum number of minutes Steve needs to finish both levels, or if there is no way to complete the quests to finish both levels.