Computational Ichthyology
InterviewTime limit2sMemory limit1024 MB
Aquariums in a row each spawn fish at times set by their population; Igor starts at tank 1, walks one tank per second, and must be present at each birth. Output the time of the first birth he misses.
- Level
Hard8 of 10
- Topics
- Greedy, Simulation, Math, Implementation
- Solved
- No attempts yet
Problem
Igor works as a junior laboratory assistant at an ichthyology research institute. He is entrusted with aquariums standing in a row, each housing a colony of guppies. The size of each colony is known in advance.
In the laboratory conditions of the ichthyology institute, a guppy colony grows by the following rule: once it reaches a population of fish, the colony stays that size for seconds, after which a new fish is born. From the starting moment until the birth of the first fish, a colony of size also waits seconds.
For example, a colony with an initial size of 996 reproduces as follows:
Igor must record the birth of every new fish in a special journal. He writes the entry instantly, but at the moment a new fish is born he must be next to the aquarium where it happened.
Moving from one aquarium to a neighboring one takes Igor one second. At the starting moment Igor stands next to the first aquarium.
Determine the longest period of time during which Igor can faithfully do his job.
Input
The first line of the input file contains an integer (), the number of aquariums with guppies at the ichthyology institute. Each of the following lines contains a single integer (), the size of the -th colony.
Output
Output the moment in time when the first guppy is born whose birth Igor cannot record.
Hint
In the example, Igor first waits at the first aquarium for a fish to appear at second 4. After that he runs to the third aquarium (which takes him 2 seconds) and arrives exactly at the birth of a fish at second 6. However, he can no longer make it back to the first aquarium, where the next fish will be born at second 7.