Time Management
InterviewTime limit1sMemory limit128 MB
Given each chore's duration and deadline, find the latest start time that lets John finish all chores before their deadlines, or print -1.
- Level
Medium4 of 10
- Topics
- Greedy, Sorting, Intervals, Implementation
- Solved
- No attempts yet
Problem
Farmer John has decided to manage his time efficiently. He has numbered his chores () — such as milking cows, cleaning the barn, and mending fences.
Completing chore takes units of time (), and it must be finished by its deadline (). John starts his day at , and once he begins a chore he works on nothing else until it is done.
John loves to sleep in. Print the latest time at which he can start working while still finishing every chore by its deadline.
Input
The first line contains the number of chores .
Each of the next lines contains and , separated by a space.
Output
Print the latest time at which John can start working. If he cannot finish all chores on time, print .