Lifeguards (Platinum)
Time limit2sMemory limit512 MB
Fire exactly K of N lifeguard shifts to maximize the total time covered by at least one remaining shift.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Sorting, Greedy, Intervals
- Solved
- No attempts yet
Problem
Farmer John opened a swimming pool for his cows, figuring it will help them relax and produce more milk.
To keep the pool safe he hires cows as lifeguards. Each lifeguard works one shift that covers a contiguous stretch of the day. The pool is open from time until time every day, so a shift is given by two integers, the time the cow starts and the time she ends. A lifeguard who starts at and ends at watches three units of time. The endpoints are points in time, so a point by itself has no length.
Farmer John hired more lifeguards than he can pay for, so he must fire exactly of them. A stretch of time is watched when at least one of the remaining lifeguards is on duty. Find the largest total amount of time the remaining shifts can still watch.
Input
The first line contains and (, ).
Each of the next lines contains the start time and the end time of one lifeguard's shift, two integers between and . The start time is smaller than the end time. The times in the input are all distinct. Shifts of different lifeguards may overlap.
Output
Print on one line the largest total amount of time that can be watched after Farmer John fires exactly lifeguards.
Hint
In the first sample Farmer John should fire the lifeguard working from to and the one working from to . The shift from to that remains watches 12 units of time.