This page is still under construction.

Parts of this page are still being built. What you see may change.

Computational Ichthyology

Interview

Time limit2sMemory limit1024 MB

Summary
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 nn 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 ff fish, the colony stays that size for max⁡(1000−f,1)\max(1000 - f, 1) seconds, after which a new fish is born. From the starting moment until the birth of the first fish, a colony of size ff also waits max⁡(1000−f,1)\max(1000 - f, 1) seconds.

For example, a colony with an initial size of 996 reproduces as follows:

timecolony sizetime until the next fish
09964
49973
79982
99991
1010001
1110011
1210021
.........

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 nn (2≤n≤502 \le n \le 50), the number of aquariums with guppies at the ichthyology institute. Each of the following nn lines contains a single integer aia_i (1≤ai≤20071 \le a_i \le 2007), the size of the ii-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.

Examples1

  1. Example 1

    Input
    3
    996
    1
    994
    
    Expected output
    7