Morning Routine and Roll Call
InterviewTime limit1.5sMemory limit1024 MB
Choose actions whose stage prerequisites are met and whose total time is at most T, then output the maximum total satisfaction.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Array
- Solved
- No attempts yet
Problem
Even today, students at Daejeon Science High School keep piling up demerit points for arriving late to roll call. Daegwak has been developing his own systematic morning routine, but a late arrival at roll call has already brought his demerit points to . He must never be late for tomorrow's roll call.
Daegwak's morning routine has actions. The -th action () has a stage , a non-negative integer; a time , a positive integer for how long the action takes; and a satisfaction value , a positive integer for the satisfaction he gets from performing it.
The time from when Daegwak wakes up until the roll call deadline is not very long, so he may not be able to finish the entire routine. He chooses zero or more actions and performs them in a suitable order. The actions he performs must satisfy these conditions.
- Daegwak can perform each action at most once.
- If the stage of the -th action is a positive integer, Daegwak must have performed at least one action of stage before performing the -th action. If , there is no additional constraint.
- The sum of the times needed for the actions he performs must be less than or equal to the remaining time before the roll call deadline.
The satisfaction Daegwak gets is the sum of the satisfaction values of the actions he performs. He wants to choose the actions and their order to maximize this sum. Help him reach the maximum satisfaction.
Input
The first line contains and , separated by a space.
Each of the next lines contains , , and , separated by spaces. Two or more actions may have the same stage, time, and satisfaction value.
Output
Print the maximum satisfaction Daegwak can get within the remaining time before the roll call deadline.
Hint
- If Daegwak chooses zero actions, the sum of the times and the sum of the satisfaction values are both .
- In languages such as C, C++, and Java, declaring some variables as 32-bit integers can cause overflow. Be careful.