Eagle
Time limit2sMemory limit512 MB
Each day pick one cell, route from an edge, empty every cell passed over, and lose one sheep per cell at night; maximize the sheep eaten.
- Level
Medium4 of 10
- Topics
- Simulation, Greedy
- Solved
- No attempts yet
Problem
An eagle lives by eating sheep. The place where the sheep live can be represented as a rectangle divided into cells. The cells are numbered from left to right. The number of sheep living in cell is .
The eagle goes out to eat sheep every morning. It starts flying from the left of cell or the right of cell and flies to the cell whose sheep it wants to eat. The eagle cannot fly outside the cells. If the sheep it wants to eat are in cell , it flies to cell and then eats all the sheep in cell . In one day the eagle can eat the sheep of only one cell.
Sheep are very afraid of the eagle, so they run away when they see it flying. A sheep runs away when it sees the eagle flying above its cell. When sheep run away, the number of sheep in that cell becomes . For example, if the eagle starts flying from the left of cell and arrives at cell , then all the sheep that were in cells through run away and become . If it starts flying from the right of cell , then all the sheep in cells through run away.
Also, because this is a dangerous place, every night the number of sheep in every cell decreases by .
How many sheep the eagle can eat depends on which cell's sheep it eats each day and from which side it starts flying.
Given the numbers of sheep, write a program to find the maximum number of sheep the eagle can eat.
Input
The first line gives the number of cells . ()
The second line gives the number of sheep in each cell, . ()
Output
On the first line, print the maximum number of sheep the eagle can eat.