Dams
Time limit1sMemory limit512 MB
Each of n sectors fills at its own rate behind dams of given heights, and you compute when water first spills past an end dam.
- Level
Hard8 of 10
- Topics
- Heap, Union-find, Simulation
- Solved
- No attempts yet
Problem
A large water reservoir was built in Byteotia. It is divided into several sectors of equal length. Between every two neighboring sectors stands a dam of some height, and dams also stand in front of the first sector and behind the last sector.
At this moment the water level is the same across the whole reservoir. But heavy rain has begun and the water level is rising fast. The king wants to know how long it will take before water spills over the first dam or the last dam, which would surely flood the whole country. The calculation is tricky because the rain may fall with a different intensity over each sector.
Compute the time left until water spills out of the reservoir. If the water level is exactly equal to a dam's height, the water is not yet spilling. When a flooded part of the reservoir is bounded on both sides by dams of the same height, water spills from it to both sides equally fast.
Input
The first line contains one integer (), the number of sectors the reservoir is divided into.
The second line contains integers () separated by single spaces, giving the heights of the successive dams from left to right, measured above the initial water level.
The third line contains integers () separated by single spaces, where is the number of levels by which the water in the -th sector rises during one second.
Output
Print one integer: the smallest integer not smaller than the number of seconds after which water spills out of the reservoir.
Hint
