Lifeguards
Time limit2sMemory limit512 MB
Given N shifts, remove exactly one so that the total time covered by the remaining shifts is maximized.
- Level
Easy3 of 10
- Topics
- Intervals, Brute force, Array
- Solved
- No attempts yet
Problem
Farmer John has opened a swimming pool for his cows, expecting that it helps them relax and produce more milk.
For safety he hires cows as lifeguards, and each of them works a shift that covers one contiguous interval of the day. The pool is open every day from time until time , so a shift is described by two integers: the time a cow starts her shift and the time she ends it. A lifeguard who starts at time and ends at time covers three units of time. Times are points, and the length of a shift is the end time minus the start time.
Farmer John hired one more lifeguard than his budget supports. Given that he must fire exactly one of them, what is the maximum amount of time that the shifts of the remaining lifeguards still cover? A unit of time is covered when at least one lifeguard is on duty during it.
Input
The first line contains (). Each of the next lines describes one lifeguard with two integers, the starting point and the ending point of her shift. Both values are between and , and the starting point is smaller than the ending point. The endpoints in the input are all distinct. Shifts of different lifeguards might overlap.
Output
Print a single integer, the maximum amount of time that can still be covered after Farmer John fires one lifeguard.