Installations
Time limit1sMemory limit128 MB
Order jobs with given service times and deadlines to minimize the sum of the two largest lateness penalties.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Sorting, Greedy
- Solved
- No attempts yet
Problem
In the morning, a service engineer at a telecom company receives a list of jobs to carry out that day: installing telephones, internet, IPTV, and repairing existing facilities. Each job comes with a deadline by which the client wants it finished, but because of the workload the engineer cannot always meet every deadline.
The engineer works on one job at a time. Every job has a serving time and a deadline . Starting from time , the jobs are carried out one after another in some order, and each job runs to completion once it starts. If finishes at time , its penalty is , that is, how far it runs past its deadline. All values are positive integers with .
Arrange the jobs so that the sum of the two largest penalties is as small as possible.
For example, take six jobs whose are for . Figure 1 shows a schedule that minimizes the sum of the two largest penalties. Here the two largest penalties belong to and , equal to and , so their sum is .

Input
The first line contains the number of test cases .
Each test case begins with a line containing an integer (), the number of jobs. Each of the next lines contains two integers and (): the serving time and the deadline of job .
Output
For each test case, print a single line with the sum of the two largest penalties over an optimal schedule.