Problems > Minimize the Worst Delay

Minimize the Worst Delay

Difficulty: 1500 5000 ms 128 MB

greedy sorting

N jobs run on one machine (no overlap); job i takes di time and is due at ti. If a job finishes at time f its lateness is max(0, f − ti). Order the jobs to minimize the MAXIMUM lateness and print that value.

Input: the first line has N; each of the next N lines has duration and deadline.

Output: the minimum possible maximum lateness.

Constraints:

  • 1 ≤ N ≤ 105
  • 1 ≤ di ≤ 1000
  • 1 ≤ ti ≤ 106

Sample Tests

Input 1
2
3 5
2 4
Output 1
0
Input 2
1
5 3
Output 2
2

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.