TV War
InterviewTime limit1sMemory limit256 MB
Pick non-overlapping weekly TV programs to maximize the total preference score.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Sorting, Binary search, Intervals
- Solved
- No attempts yet
Problem
There is only one television in the house, so the family argues every time about which program to watch. You are building a machine that settles the argument automatically.
The programs the family wants to watch during the year are already fixed. Every program starts at the same time each week and ends at the same time each week, so one weekly viewing plan is enough to cover the whole year. Each program has a preference score given by the family, and a higher score means the family wants to watch that program more. Because there is only one television, two programs whose times overlap cannot both be watched.
Choose programs whose viewing times do not overlap so that the total preference is as large as possible, and report that maximum.
Input
The first line contains the number of test cases .
The first line of each test case contains the number of programs . Each of the next lines contains three integers , , separated by spaces. is the time the program starts, is how long the program lasts, and is its preference score.
If equals , the program ends exactly at time , and another program can be watched starting exactly at time .
Output
For each test case, print the maximum total preference on its own line.
Hint
In the first example, taking the program that starts at time and lasts together with the program that starts at time and lasts gives a total preference of , which is the largest the family can reach.