Warm Up
Time limit4sMemory limit512 MB
Given n dishes with warm-up and eating times, find the minimum total time to warm up and eat all of them while following the microwave readiness rules.
- Level
Medium5 of 10
- Topics
- Brute force, Simulation
- Solved
- No attempts yet
Problem
Sasha has convenience food dishes. He has to warm them up in the microwave before eating. Each dish takes seconds to be warmed up and seconds to be consumed. Each dish has a readiness , which starts at .
After the -th dish is placed in the microwave, its increases by 1 every second until the dish is pulled out. The microwave can contain at most one dish at any moment. must never exceed under any circumstances. Every -th second, all dishes that are not being eaten and are not in the microwave have their decreased by exactly 1, unless , in which case it stays the same. Sasha may start eating a dish only if , and he will not eat any other dish until he finishes the one he started.
Sasha can operate the microwave and eat at the same time. Placing a dish in or pulling it out takes a negligibly small amount of time. He may do these actions only at integer moments of time. Find the minimum total time Sasha needs to finish eating all dishes.
Input
The first line contains two integers and (, ).
Each of the next lines contains two integers and ().
Output
Print a single integer, the minimum amount of time Sasha needs to finish eating all dishes.